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>
18 #include "gost_asn1.h"
22 #include "e_gost_err.h"
26 /* Common functions for both 94 and 2001 key exchange schemes */
27 int decrypt_cryptocom_key(unsigned char *sess_key,int max_key_len,
28 const unsigned char *crypted_key,int crypted_key_len, gost_ctx *ctx)
32 int blocks = crypted_key_len >>3;
33 unsigned char gamma[8];
34 if (max_key_len <crypted_key_len) {
35 GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_NOT_ENOUGH_SPACE_FOR_KEY);
38 if ((crypted_key_len & 7) !=0)
40 GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_INVALID_ENCRYPTED_KEY_SIZE);
43 for (i=blocks-1;i>0;i--)
45 gostcrypt(ctx,crypted_key+(i-1)*8,gamma);
48 sess_key[i*8+j]=gamma[j]^crypted_key[i*8+j];
51 gostcrypt(ctx,sess_key+crypted_key_len-8,gamma);
54 sess_key[j]=gamma[j]^crypted_key[j];
58 int encrypt_cryptocom_key(const unsigned char *sess_key,int key_len,
59 unsigned char *crypted_key, gost_ctx *ctx)
63 unsigned char gamma[8];
64 memcpy(gamma,sess_key+key_len-8,8);
65 for (i=0;i<key_len;i+=8)
67 gostcrypt(ctx,gamma,gamma);
69 gamma[j]=crypted_key[i+j]=sess_key[i+j]^gamma[j];
73 /* Implementation of the Diffi-Hellman key agreement scheme based on
76 /* Computes Diffie-Hellman key and stores it into buffer in
77 * little-endian byte order as expected by both versions of GOST 94
80 static int compute_pair_key_le(unsigned char *pair_key,BIGNUM *pub_key,DH *dh)
82 unsigned char be_key[128];
84 key_size=DH_compute_key(be_key,pub_key,dh);
85 if (!key_size) return 0;
86 memset(pair_key,0,128);
87 for (i=0;i<key_size;i++) {
88 pair_key[i]=be_key[key_size-1-i];
93 * Computes 256 bit key exchange key for CryptoCom variation of GOST 94
96 static int make_gost_shared_key(DH *dh,EVP_PKEY *pubk,unsigned char *shared_key)
98 unsigned char dh_key [128];
101 memset(dh_key,0,128);
102 if (!compute_pair_key_le(dh_key,((DSA *)EVP_PKEY_get0(pubk))->pub_key,dh)) return 0;
103 /* Fold it down to 256 bit */
104 /* According to GOST either 2^1020<p<2^1024 or
105 * 2^509<p<2^512, so DH_size can be exactly 128 or exactly 64 only
108 if (DH_size(dh)==128)
110 dh_key[i]^=dh_key[64+i];
113 shared_key[i]=dh_key[i]^dh_key[32+i];
118 static DH *make_ephemeral_key(EVP_PKEY *pubk,BIGNUM *ephemeral_key)
121 dh->g = BN_dup(pubk->pkey.dsa->g);
122 dh->p = BN_dup(pubk->pkey.dsa->p);
123 dh->priv_key = BN_dup(ephemeral_key);
124 /* Generate ephemeral key pair */
125 if (!DH_generate_key(dh)) {
132 * Computes 256 bit Key exchange key as specified in RFC 4357
134 static int make_cp_exchange_key(DH *dh,EVP_PKEY *pubk, unsigned char *shared_key)
136 unsigned char dh_key [128];
137 gost_hash_ctx hash_ctx;
138 memset(dh_key,0,128);
139 if (!compute_pair_key_le(dh_key,((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,dh)) return 0;
140 init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet);
141 start_hash(&hash_ctx);
142 hash_block(&hash_ctx,dh_key,128);
143 finish_hash(&hash_ctx,shared_key);
144 done_gost_hash_ctx(&hash_ctx);
147 /* EVP_PKEY_METHOD callback encrypt for
148 * GOST R 34.10-94 cryptopro modification
151 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len )
153 GOST_KEY_TRANSPORT *gkt=NULL;
155 unsigned char shared_key[32], ukm[8],crypted_key[44];
156 const struct gost_cipher_info *param=get_encryption_params(NULL);
157 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
158 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
163 if (!(data->eph_seckey)) {
164 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
165 GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
169 dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
170 gost_init(&cctx,param->sblock);
171 make_cp_exchange_key(dh,pubk,shared_key);
172 if (RAND_bytes(ukm,8)<=0) {
173 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
174 GOST_R_RANDOM_GENERATOR_FAILURE);
177 keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key);
178 gkt = GOST_KEY_TRANSPORT_new();
182 if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
186 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4)) {
189 if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32)) {
192 if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey)) {
193 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
196 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
197 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
198 *outlen = i2d_GOST_KEY_TRANSPORT(gkt,&out);
200 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
203 GOST_KEY_TRANSPORT_free(gkt);
207 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
208 GOST_R_MALLOC_FAILURE);
210 GOST_KEY_TRANSPORT_free(gkt);
214 /* EVP_PKEY_METHOD callback encrypt for
215 * GOST R 34.10-94 cryptocom modification
218 int pkey_GOST94cc_encrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char * key,size_t key_len)
220 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
221 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
222 /* create DH structure filling parameters from passed pub_key */
224 GOST_KEY_TRANSPORT *gkt = NULL;
226 EVP_PKEY *newkey=NULL;
227 unsigned char shared_key[32],encrypted_key[32],hmac[4],
228 iv[8]={0,0,0,0,0,0,0,0};
230 if (! data->eph_seckey) {
231 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
232 GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
235 dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
237 /* compute shared key */
238 if (!make_gost_shared_key(dh,pubk,shared_key))
240 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
243 /* encrypt session key */
244 gost_init(&cctx, &GostR3411_94_CryptoProParamSet);
245 gost_key(&cctx,shared_key);
246 encrypt_cryptocom_key(key,key_len,encrypted_key,&cctx);
247 /* compute hmac of session key */
248 if (!gost_mac(&cctx,32,key,32,hmac))
251 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
254 gkt = GOST_KEY_TRANSPORT_new();
258 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_NO_MEMORY);
261 /* Store IV which is always zero in our case */
262 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
264 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
267 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4))
269 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
272 if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
274 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
277 if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey)) {
278 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
281 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
282 gkt->key_agreement_info->cipher = OBJ_nid2obj(NID_id_Gost28147_89_cc);
283 *outlen = i2d_GOST_KEY_TRANSPORT(gkt,&out);
285 if (gkt) GOST_KEY_TRANSPORT_free(gkt);
287 if (newkey) EVP_PKEY_free(newkey);
291 /* EVP_PLEY_METHOD callback decrypt for
292 * GOST R 34.10-94 cryptopro modification
294 int pkey_GOST94cp_decrypt (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) {
296 const unsigned char *p = in;
297 GOST_KEY_TRANSPORT *gkt = NULL;
298 unsigned char wrappedKey[44];
299 unsigned char sharedKey[32];
301 const struct gost_cipher_info *param=NULL;
302 EVP_PKEY *eph_key=NULL;
303 EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx);
310 dh->g = BN_dup(priv->pkey.dsa->g);
311 dh->p = BN_dup(priv->pkey.dsa->p);
312 dh->priv_key = BN_dup(priv->pkey.dsa->priv_key);
313 gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
316 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
320 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
321 param = get_encryption_params(gkt->key_agreement_info->cipher);
322 gost_init(&cctx,param->sblock);
323 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
324 memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8);
325 OPENSSL_assert(gkt->key_info->encrypted_key->length==32);
326 memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32);
327 OPENSSL_assert(gkt->key_info->imit->length==4);
328 memcpy(wrappedKey+40,gkt->key_info->imit->data,4);
329 make_cp_exchange_key(dh,eph_key,sharedKey);
330 if (!keyUnwrapCryptoPro(&cctx,sharedKey,wrappedKey,key)) {
331 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
332 GOST_R_ERROR_COMPUTING_SHARED_KEY);
336 EVP_PKEY_free(eph_key);
337 GOST_KEY_TRANSPORT_free(gkt);
341 EVP_PKEY_free(eph_key);
342 GOST_KEY_TRANSPORT_free(gkt);
347 /* EVP_PKEY_METHOD callback decrypt for
348 * GOST R 34.10-94 cryptocom modification
351 int pkey_GOST94cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
353 /* Form DH params from compute shared key */
354 GOST_KEY_TRANSPORT *gkt = NULL;
355 const unsigned char *p=in;
356 unsigned char shared_key[32];
357 unsigned char hmac[4],hmac_comp[4];
363 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
369 /* Construct DH structure from the our GOST private key */
370 dh->g = BN_dup(priv->pkey.dsa->g);
371 dh->p = BN_dup(priv->pkey.dsa->p);
372 dh->priv_key = BN_dup(priv->pkey.dsa->priv_key);
373 /* Parse passed octet string and find out public key, iv and HMAC*/
374 gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
377 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
381 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
382 /* Initialization vector is really ignored here */
383 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
384 memcpy(iv,gkt->key_agreement_info->eph_iv->data,8);
385 /* HMAC should be computed and checked */
386 OPENSSL_assert(gkt->key_info->imit->length==4);
387 memcpy(hmac,gkt->key_info->imit->data,4);
388 /* Compute shared key */
389 i=make_gost_shared_key(dh,eph_key,shared_key);
390 EVP_PKEY_free(eph_key);
394 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
395 GOST_KEY_TRANSPORT_free(gkt);
398 /* Decrypt session key */
399 gost_init(&ctx, &GostR3411_94_CryptoProParamSet);
400 gost_key(&ctx,shared_key);
402 if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data,
403 gkt->key_info->encrypted_key->length, &ctx))
405 GOST_KEY_TRANSPORT_free(gkt);
408 GOST_KEY_TRANSPORT_free(gkt);
409 /* check HMAC of session key*/
410 if (!gost_mac(&ctx,32,key,32,hmac_comp)) {
411 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
414 /* HMAC of session key is not correct */
415 if (memcmp(hmac,hmac_comp,4)!=0) {
416 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);