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;
238 EVP_PKEY *newkey=NULL;
239 unsigned char shared_key[32],encrypted_key[32],hmac[4],
240 iv[8]={0,0,0,0,0,0,0,0};
242 if (! data->eph_seckey)
244 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
245 GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
248 dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
250 /* compute shared key */
251 if (!make_gost_shared_key(dh,pubk,shared_key))
253 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
256 /* encrypt session key */
257 gost_init(&cctx, &GostR3411_94_CryptoProParamSet);
258 gost_key(&cctx,shared_key);
259 encrypt_cryptocom_key(key,key_len,encrypted_key,&cctx);
260 /* compute hmac of session key */
261 if (!gost_mac(&cctx,32,key,32,hmac))
264 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
267 gkt = GOST_KEY_TRANSPORT_new();
271 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_NO_MEMORY);
274 /* Store IV which is always zero in our case */
275 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
277 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
280 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4))
282 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
285 if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
287 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
290 if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
292 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
295 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
296 gkt->key_agreement_info->cipher = OBJ_nid2obj(NID_id_Gost28147_89_cc);
297 *outlen = i2d_GOST_KEY_TRANSPORT(gkt,&out);
299 if (gkt) GOST_KEY_TRANSPORT_free(gkt);
301 if (newkey) EVP_PKEY_free(newkey);
305 /* EVP_PLEY_METHOD callback decrypt for
306 * GOST R 34.10-94 cryptopro modification
308 int pkey_GOST94cp_decrypt (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) {
310 const unsigned char *p = in;
311 GOST_KEY_TRANSPORT *gkt = NULL;
312 unsigned char wrappedKey[44];
313 unsigned char sharedKey[32];
315 const struct gost_cipher_info *param=NULL;
316 EVP_PKEY *eph_key=NULL;
317 EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx);
325 dh->g = BN_dup(priv->pkey.dsa->g);
326 dh->p = BN_dup(priv->pkey.dsa->p);
327 dh->priv_key = BN_dup(priv->pkey.dsa->priv_key);
328 gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
332 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
336 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
337 param = get_encryption_params(gkt->key_agreement_info->cipher);
338 gost_init(&cctx,param->sblock);
339 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
340 memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8);
341 OPENSSL_assert(gkt->key_info->encrypted_key->length==32);
342 memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32);
343 OPENSSL_assert(gkt->key_info->imit->length==4);
344 memcpy(wrappedKey+40,gkt->key_info->imit->data,4);
345 make_cp_exchange_key(dh,eph_key,sharedKey);
346 if (!keyUnwrapCryptoPro(&cctx,sharedKey,wrappedKey,key))
348 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
349 GOST_R_ERROR_COMPUTING_SHARED_KEY);
353 EVP_PKEY_free(eph_key);
354 GOST_KEY_TRANSPORT_free(gkt);
358 EVP_PKEY_free(eph_key);
359 GOST_KEY_TRANSPORT_free(gkt);
364 /* EVP_PKEY_METHOD callback decrypt for
365 * GOST R 34.10-94 cryptocom modification
368 int pkey_GOST94cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
370 /* Form DH params from compute shared key */
371 GOST_KEY_TRANSPORT *gkt = NULL;
372 const unsigned char *p=in;
373 unsigned char shared_key[32];
374 unsigned char hmac[4],hmac_comp[4];
380 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
387 /* Construct DH structure from the our GOST private key */
388 dh->g = BN_dup(priv->pkey.dsa->g);
389 dh->p = BN_dup(priv->pkey.dsa->p);
390 dh->priv_key = BN_dup(priv->pkey.dsa->priv_key);
391 /* Parse passed octet string and find out public key, iv and HMAC*/
392 gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
396 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
400 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
401 /* Initialization vector is really ignored here */
402 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
403 memcpy(iv,gkt->key_agreement_info->eph_iv->data,8);
404 /* HMAC should be computed and checked */
405 OPENSSL_assert(gkt->key_info->imit->length==4);
406 memcpy(hmac,gkt->key_info->imit->data,4);
407 /* Compute shared key */
408 i=make_gost_shared_key(dh,eph_key,shared_key);
409 EVP_PKEY_free(eph_key);
413 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
414 GOST_KEY_TRANSPORT_free(gkt);
417 /* Decrypt session key */
418 gost_init(&ctx, &GostR3411_94_CryptoProParamSet);
419 gost_key(&ctx,shared_key);
421 if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data,
422 gkt->key_info->encrypted_key->length, &ctx))
424 GOST_KEY_TRANSPORT_free(gkt);
427 GOST_KEY_TRANSPORT_free(gkt);
428 /* check HMAC of session key*/
429 if (!gost_mac(&ctx,32,key,32,hmac_comp))
431 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
434 /* HMAC of session key is not correct */
435 if (memcmp(hmac,hmac_comp,4)!=0)
437 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);