1 /**********************************************************************
3 * Copyright (c) 2005-2006 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implements generation and parsing of GOST_KEY_TRANSPORT for *
7 * GOST R 34.10-94 algorithms *
9 * Requires OpenSSL 0.9.9 for compilation *
10 **********************************************************************/
12 #include <openssl/dh.h>
13 #include <openssl/rand.h>
14 #include <openssl/evp.h>
15 #include <openssl/objects.h>
19 #include "e_gost_err.h"
20 #include "gost_keywrap.h"
22 /* Common functions for both 94 and 2001 key exchange schemes */
23 int decrypt_cryptocom_key(unsigned char *sess_key,int max_key_len,
24 const unsigned char *crypted_key,int crypted_key_len, gost_ctx *ctx)
28 int blocks = crypted_key_len >>3;
29 unsigned char gamma[8];
30 if (max_key_len <crypted_key_len)
32 GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_NOT_ENOUGH_SPACE_FOR_KEY);
35 if ((crypted_key_len & 7) !=0)
37 GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_INVALID_ENCRYPTED_KEY_SIZE);
40 for (i=blocks-1;i>0;i--)
42 gostcrypt(ctx,crypted_key+(i-1)*8,gamma);
45 sess_key[i*8+j]=gamma[j]^crypted_key[i*8+j];
48 gostcrypt(ctx,sess_key+crypted_key_len-8,gamma);
51 sess_key[j]=gamma[j]^crypted_key[j];
55 int encrypt_cryptocom_key(const unsigned char *sess_key,int key_len,
56 unsigned char *crypted_key, gost_ctx *ctx)
60 unsigned char gamma[8];
61 memcpy(gamma,sess_key+key_len-8,8);
62 for (i=0;i<key_len;i+=8)
64 gostcrypt(ctx,gamma,gamma);
66 gamma[j]=crypted_key[i+j]=sess_key[i+j]^gamma[j];
70 /* Implementation of the Diffi-Hellman key agreement scheme based on
73 /* Computes Diffie-Hellman key and stores it into buffer in
74 * little-endian byte order as expected by both versions of GOST 94
77 static int compute_pair_key_le(unsigned char *pair_key,BIGNUM *pub_key,DH *dh)
79 unsigned char be_key[128];
81 key_size=DH_compute_key(be_key,pub_key,dh);
82 if (!key_size) return 0;
83 memset(pair_key,0,128);
84 for (i=0;i<key_size;i++)
86 pair_key[i]=be_key[key_size-1-i];
91 * Computes 256 bit key exchange key for CryptoCom variation of GOST 94
94 static int make_gost_shared_key(DH *dh,EVP_PKEY *pubk,unsigned char *shared_key)
96 unsigned char dh_key [128];
100 if (!compute_pair_key_le(dh_key,((DSA *)EVP_PKEY_get0(pubk))->pub_key,dh)) return 0;
101 /* Fold it down to 256 bit */
102 /* According to GOST either 2^1020<p<2^1024 or
103 * 2^509<p<2^512, so DH_size can be exactly 128 or exactly 64 only
106 if (DH_size(dh)==128)
110 dh_key[i]^=dh_key[64+i];
115 shared_key[i]=dh_key[i]^dh_key[32+i];
120 static DH *make_ephemeral_key(EVP_PKEY *pubk,BIGNUM *ephemeral_key)
123 dh->g = BN_dup(pubk->pkey.dsa->g);
124 dh->p = BN_dup(pubk->pkey.dsa->p);
125 dh->priv_key = BN_dup(ephemeral_key);
126 /* Generate ephemeral key pair */
127 if (!DH_generate_key(dh))
135 * Computes 256 bit Key exchange key as specified in RFC 4357
137 static int make_cp_exchange_key(DH *dh,EVP_PKEY *pubk, unsigned char *shared_key)
139 unsigned char dh_key [128];
140 gost_hash_ctx hash_ctx;
141 memset(dh_key,0,128);
142 if (!compute_pair_key_le(dh_key,((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,dh)) return 0;
143 init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet);
144 start_hash(&hash_ctx);
145 hash_block(&hash_ctx,dh_key,128);
146 finish_hash(&hash_ctx,shared_key);
147 done_gost_hash_ctx(&hash_ctx);
151 /* EVP_PKEY_METHOD callback encrypt for
152 * GOST R 34.10-94 cryptopro modification
155 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len )
157 GOST_KEY_TRANSPORT *gkt=NULL;
159 unsigned char shared_key[32], ukm[8],crypted_key[44];
160 const struct gost_cipher_info *param=get_encryption_params(NULL);
161 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
162 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
166 if (!(data->eph_seckey))
168 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
169 GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
173 dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
174 gost_init(&cctx,param->sblock);
175 make_cp_exchange_key(dh,pubk,shared_key);
176 if (RAND_bytes(ukm,8)<=0)
178 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
179 GOST_R_RANDOM_GENERATOR_FAILURE);
182 keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key);
183 gkt = GOST_KEY_TRANSPORT_new();
188 if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
193 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4))
197 if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32))
201 if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
203 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
206 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
207 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
208 *outlen = i2d_GOST_KEY_TRANSPORT(gkt,&out);
211 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
214 GOST_KEY_TRANSPORT_free(gkt);
218 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
219 GOST_R_MALLOC_FAILURE);
221 GOST_KEY_TRANSPORT_free(gkt);
226 /* EVP_PKEY_METHOD callback encrypt for
227 * GOST R 34.10-94 cryptocom modification
230 int pkey_GOST94cc_encrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char * key,size_t key_len)
232 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
233 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
234 /* create DH structure filling parameters from passed pub_key */
236 GOST_KEY_TRANSPORT *gkt = NULL;
237 const struct gost_cipher_info *cipher_info;
239 EVP_PKEY *newkey=NULL;
240 unsigned char shared_key[32],encrypted_key[32],hmac[4],
241 iv[8]={0,0,0,0,0,0,0,0};
243 if (! data->eph_seckey)
245 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
246 GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
249 dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
251 /* compute shared key */
252 if (!make_gost_shared_key(dh,pubk,shared_key))
254 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
257 /* encrypt session key */
258 cipher_info = get_encryption_params(NULL);
259 gost_init(&cctx, cipher_info->sblock);
260 gost_key(&cctx,shared_key);
261 encrypt_cryptocom_key(key,key_len,encrypted_key,&cctx);
262 /* compute hmac of session key */
263 if (!gost_mac(&cctx,32,key,32,hmac))
266 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
269 gkt = GOST_KEY_TRANSPORT_new();
273 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_NO_MEMORY);
276 /* Store IV which is always zero in our case */
277 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
279 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
282 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4))
284 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
287 if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
289 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
292 if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
294 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
297 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
298 gkt->key_agreement_info->cipher = OBJ_nid2obj(cipher_info->nid);
299 *outlen = i2d_GOST_KEY_TRANSPORT(gkt,&out);
301 if (gkt) GOST_KEY_TRANSPORT_free(gkt);
303 if (newkey) EVP_PKEY_free(newkey);
307 /* EVP_PLEY_METHOD callback decrypt for
308 * GOST R 34.10-94 cryptopro modification
310 int pkey_GOST94cp_decrypt (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) {
312 const unsigned char *p = in;
313 GOST_KEY_TRANSPORT *gkt = NULL;
314 unsigned char wrappedKey[44];
315 unsigned char sharedKey[32];
317 const struct gost_cipher_info *param=NULL;
318 EVP_PKEY *eph_key=NULL;
319 EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx);
327 dh->g = BN_dup(priv->pkey.dsa->g);
328 dh->p = BN_dup(priv->pkey.dsa->p);
329 dh->priv_key = BN_dup(priv->pkey.dsa->priv_key);
330 gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
334 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
338 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
339 param = get_encryption_params(gkt->key_agreement_info->cipher);
340 gost_init(&cctx,param->sblock);
341 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
342 memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8);
343 OPENSSL_assert(gkt->key_info->encrypted_key->length==32);
344 memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32);
345 OPENSSL_assert(gkt->key_info->imit->length==4);
346 memcpy(wrappedKey+40,gkt->key_info->imit->data,4);
347 make_cp_exchange_key(dh,eph_key,sharedKey);
348 if (!keyUnwrapCryptoPro(&cctx,sharedKey,wrappedKey,key))
350 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
351 GOST_R_ERROR_COMPUTING_SHARED_KEY);
355 EVP_PKEY_free(eph_key);
356 GOST_KEY_TRANSPORT_free(gkt);
360 EVP_PKEY_free(eph_key);
361 GOST_KEY_TRANSPORT_free(gkt);
366 /* EVP_PKEY_METHOD callback decrypt for
367 * GOST R 34.10-94 cryptocom modification
370 int pkey_GOST94cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
372 /* Form DH params from compute shared key */
373 GOST_KEY_TRANSPORT *gkt = NULL;
374 const unsigned char *p=in;
375 unsigned char shared_key[32];
376 unsigned char hmac[4],hmac_comp[4];
379 const struct gost_cipher_info *cipher_info;
383 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
390 /* Construct DH structure from the our GOST private key */
391 dh->g = BN_dup(priv->pkey.dsa->g);
392 dh->p = BN_dup(priv->pkey.dsa->p);
393 dh->priv_key = BN_dup(priv->pkey.dsa->priv_key);
394 /* Parse passed octet string and find out public key, iv and HMAC*/
395 gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
399 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
403 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
404 /* Initialization vector is really ignored here */
405 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
406 memcpy(iv,gkt->key_agreement_info->eph_iv->data,8);
407 /* HMAC should be computed and checked */
408 OPENSSL_assert(gkt->key_info->imit->length==4);
409 memcpy(hmac,gkt->key_info->imit->data,4);
410 /* Compute shared key */
411 i=make_gost_shared_key(dh,eph_key,shared_key);
412 EVP_PKEY_free(eph_key);
416 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
417 GOST_KEY_TRANSPORT_free(gkt);
420 /* Decrypt session key */
421 cipher_info = get_encryption_params(gkt->key_agreement_info->cipher);
422 gost_init(&ctx, cipher_info->sblock);
423 gost_key(&ctx,shared_key);
425 if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data,
426 gkt->key_info->encrypted_key->length, &ctx))
428 GOST_KEY_TRANSPORT_free(gkt);
431 GOST_KEY_TRANSPORT_free(gkt);
432 /* check HMAC of session key*/
433 if (!gost_mac(&ctx,32,key,32,hmac_comp))
435 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
438 /* HMAC of session key is not correct */
439 if (memcmp(hmac,hmac_comp,4)!=0)
441 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);