Updated GOST MAC support.
[openssl.git] / engines / ccgost / gost2001_keyx.c
1 /**********************************************************************
2  *                          gost_keyx.c                               *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *   VK0 34.10-2001 key exchange and GOST R 34.10-2001                *
7  *   based PKCS7/SMIME support                                        *
8  *          Requires OpenSSL 0.9.9 for compilation                    *
9  **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/rand.h>
12 #include <string.h>
13 #include <openssl/objects.h>
14 #include "gost89.h"
15 #include "gosthash.h"
16 #include "e_gost_err.h"
17 #include "gost_keywrap.h"
18 #include "gost_lcl.h"
19 #include "gost2001_keyx.h"
20
21 /* Transform ECDH shared key into little endian as required by Cryptocom
22  * key exchange */
23 static void *make_key_le(const void *in, size_t inlen, void *out, size_t *outlen)
24         {
25         const char* inbuf= in;
26         char* outbuf= out;
27         int i;
28         if (*outlen < inlen)
29                 {
30                 return NULL;
31                 }
32         for (i=0;i<inlen;i++)
33                 {
34                 outbuf[inlen-1-i]=inbuf[i];
35                 }
36         *outlen = inlen;
37         return out;
38         }       
39
40 /* Create gost 2001 ephemeral key with same parameters as peer key */
41 static EC_KEY *make_ec_ephemeral_key(EC_KEY *peer_key,BIGNUM *seckey)
42         {
43         EC_KEY *out = EC_KEY_new();
44         EC_KEY_copy(out,peer_key);
45         EC_KEY_set_private_key(out,seckey);
46         gost2001_compute_public(out);
47         return out;
48         }
49 /* Packs GOST elliptic curve key into EVP_PKEY setting same parameters
50  * as in passed pubkey
51  */
52 static EVP_PKEY *ec_ephemeral_key_to_EVP(EVP_PKEY *pubk,int type,EC_KEY *ephemeral) 
53         {
54         EVP_PKEY *newkey;
55         newkey = EVP_PKEY_new();
56         EVP_PKEY_assign(newkey,type,ephemeral);
57         return newkey;
58         }       
59
60 /*  
61  * EVP_PKEY_METHOD callback encrypt  
62  * Implementation of GOST2001 key transport, cryptocom variation 
63  */
64
65 int pkey_GOST01cc_encrypt (EVP_PKEY_CTX *pctx,unsigned char *out, 
66         size_t *out_len, const unsigned char *key,size_t key_len)
67         {
68         EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
69         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);     
70         GOST_KEY_TRANSPORT *gkt = NULL;
71         int ret=0;
72         const struct gost_cipher_info *cipher_info;
73         gost_ctx ctx;
74         EC_KEY *ephemeral=NULL;
75         const EC_POINT *pub_key_point=NULL;
76         unsigned char shared_key[32],encrypted_key[32],hmac[4],
77                 iv[8]={0,0,0,0,0,0,0,0};
78         ephemeral = make_ec_ephemeral_key(EVP_PKEY_get0(pubk), gost_get_priv_key(data->eph_seckey));
79         if (!ephemeral) goto err;
80         /* compute shared key */
81         pub_key_point=EC_KEY_get0_public_key(EVP_PKEY_get0(pubk));
82         if (!ECDH_compute_key(shared_key,32,pub_key_point,ephemeral,make_key_le)) 
83                 {
84                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
85                 goto err;
86                 }       
87         /* encrypt session key */
88         cipher_info = get_encryption_params(NULL);
89         gost_init(&ctx, cipher_info->sblock);
90         gost_key(&ctx,shared_key);
91         encrypt_cryptocom_key(key,key_len,encrypted_key,&ctx);
92         /* compute hmac of session key */
93         if (!gost_mac(&ctx,32,key,32,hmac)) 
94                 {
95                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
96                 return -1;
97                 }
98         gkt = GOST_KEY_TRANSPORT_new();
99         if (!gkt) 
100                 {
101                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_NO_MEMORY);
102                 return -1;
103                 }       
104         /* Store IV which is always zero in our case */
105         if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
106                 {
107                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
108                 goto err;
109                 }       
110         if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4)) 
111                 {
112                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
113                 goto err;
114                 }       
115         if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
116                 {       
117                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
118                 goto err;
119                 }
120         
121         if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
122                 {
123                 GOSTerr(GOST_F_PKEY_GOST01CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
124                 goto err;
125                 }       
126         ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
127         gkt->key_agreement_info->cipher = OBJ_nid2obj(cipher_info->nid);
128         if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt,&out))>0) ret = 1;
129         ;
130         err:
131         if (gkt) GOST_KEY_TRANSPORT_free(gkt);
132         return ret;
133         }
134 /*  
135  * EVP_PKEY_METHOD callback decrypt  
136  * Implementation of GOST2001 key transport, cryptocom variation 
137  */
138 int pkey_GOST01cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
139         {
140         /* Form DH params from compute shared key */
141         EVP_PKEY *priv=EVP_PKEY_CTX_get0_pkey(pctx);
142         GOST_KEY_TRANSPORT *gkt = NULL;
143         const unsigned char *p=in;
144         unsigned char shared_key[32];
145         unsigned char hmac[4],hmac_comp[4];
146         unsigned char iv[8];
147         int i;
148         const struct gost_cipher_info *cipher_info;
149         gost_ctx ctx;
150         const EC_POINT *pub_key_point;
151         EVP_PKEY *eph_key;
152
153         if (!key)
154                 {
155                 *key_len = 32;
156                 return 1;
157                 }       
158         /* Parse passed octet string and find out public key, iv and HMAC*/
159         gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
160                 in_len);
161         if (!gkt)
162                 {
163                 GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
164                 return 0;
165                 }       
166         eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
167         /* Initialization vector is really ignored here */
168         OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
169         memcpy(iv,gkt->key_agreement_info->eph_iv->data,8);
170         /* HMAC should be computed and checked */
171         OPENSSL_assert(gkt->key_info->imit->length==4);
172         memcpy(hmac,gkt->key_info->imit->data,4);       
173         /* Compute shared key */
174         pub_key_point=EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key));
175         i=ECDH_compute_key(shared_key,32,pub_key_point,EVP_PKEY_get0(priv),make_key_le);
176         EVP_PKEY_free(eph_key);
177         if (!i) 
178                 {
179                 GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
180                 GOST_KEY_TRANSPORT_free(gkt);
181                 return 0;
182                 }
183         /* Decrypt session key */
184         cipher_info = get_encryption_params(gkt->key_agreement_info->cipher);
185         gost_init(&ctx, cipher_info->sblock);
186         gost_key(&ctx,shared_key);
187         
188         if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data, 
189                         gkt->key_info->encrypted_key->length, &ctx)) 
190                 {
191                 GOST_KEY_TRANSPORT_free(gkt);
192                 return 0;
193                 }
194         GOST_KEY_TRANSPORT_free(gkt);
195         /* check HMAC of session key*/
196         if (!gost_mac(&ctx,32,key,32,hmac_comp))
197                 {
198                 GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
199                 return 0;
200                 }
201         /* HMAC of session key is not correct */
202     if (memcmp(hmac,hmac_comp,4)!=0)
203                 {
204                 GOSTerr(GOST_F_PKEY_GOST01CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);
205                 return 0;
206                 }       
207         return 1; 
208         }
209
210 /* Implementation of CryptoPro VKO 34.10-2001 algorithm */
211 static int VKO_compute_key(unsigned char *shared_key,size_t shared_key_size,const EC_POINT *pub_key,EC_KEY *priv_key,const unsigned char *ukm)
212         {
213         unsigned char ukm_be[8],databuf[64],hashbuf[64];
214         BIGNUM *UKM=NULL,*p=NULL,*order=NULL,*X=NULL,*Y=NULL;
215         const BIGNUM* key=EC_KEY_get0_private_key(priv_key);
216         EC_POINT *pnt=EC_POINT_new(EC_KEY_get0_group(priv_key));
217         int i;
218         gost_hash_ctx hash_ctx;
219         BN_CTX *ctx = BN_CTX_new();
220
221         for (i=0;i<8;i++)
222                 {
223                 ukm_be[7-i]=ukm[i];
224                 }
225         BN_CTX_start(ctx);
226         UKM=getbnfrombuf(ukm_be,8);
227         p=BN_CTX_get(ctx);
228         order = BN_CTX_get(ctx);
229         X=BN_CTX_get(ctx);
230         Y=BN_CTX_get(ctx);
231         EC_GROUP_get_order(EC_KEY_get0_group(priv_key),order,ctx);
232         BN_mod_mul(p,key,UKM,order,ctx);        
233         EC_POINT_mul(EC_KEY_get0_group(priv_key),pnt,NULL,pub_key,p,ctx);
234         EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
235                 pnt,X,Y,ctx);
236         /*Serialize elliptic curve point same way as we do it when saving
237          * key */
238         store_bignum(Y,databuf,32);
239         store_bignum(X,databuf+32,32);
240         /* And reverse byte order of whole buffer */
241         for (i=0;i<64;i++)
242                 {
243                 hashbuf[63-i]=databuf[i];
244                 }
245         init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet);
246         start_hash(&hash_ctx);
247         hash_block(&hash_ctx,hashbuf,64);
248         finish_hash(&hash_ctx,shared_key);
249         done_gost_hash_ctx(&hash_ctx);
250         BN_free(UKM);
251         BN_CTX_end(ctx);
252         BN_CTX_free(ctx);
253         EC_POINT_free(pnt);
254         return 32;
255         }
256
257 /* Generates ephemeral key based on pubk algorithm
258  * computes shared key using VKO and returns filled up
259  * GOST_KEY_TRANSPORT structure
260  */
261 /* Public, because it would be needed in SSL implementation */
262 GOST_KEY_TRANSPORT *make_rfc4490_keytransport_2001(EVP_PKEY *pubk,BIGNUM *eph_key,
263         const unsigned char *key,size_t keylen, unsigned char *ukm,
264         size_t ukm_len)
265         {
266
267         const struct gost_cipher_info *param=get_encryption_params(NULL);
268         EC_KEY *ephemeral = NULL;
269         GOST_KEY_TRANSPORT *gkt=NULL;
270         const EC_POINT *pub_key_point = EC_KEY_get0_public_key(EVP_PKEY_get0(pubk));
271         unsigned char shared_key[32],crypted_key[44];
272         gost_ctx ctx;
273         EVP_PKEY *newkey=NULL;
274         
275         /* Do not use vizir cipher parameters with cryptopro */
276         if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS) && param ==  gost_cipher_list)
277                 {
278                 param= gost_cipher_list+1;
279                 }       
280         ephemeral = make_ec_ephemeral_key(EVP_PKEY_get0(pubk),eph_key);
281     VKO_compute_key(shared_key,32,pub_key_point,ephemeral,ukm);
282         gost_init(&ctx,param->sblock);  
283         keyWrapCryptoPro(&ctx,shared_key,ukm,key,crypted_key);
284         gkt = GOST_KEY_TRANSPORT_new();
285         if (!gkt)
286                 {
287                 goto memerr;
288                 }       
289         if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
290                         ukm,8))
291                 {
292                 goto memerr;
293                 }       
294         if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4))
295                 {
296                 goto memerr;
297                 }
298         if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32))
299                 {
300                 goto memerr;
301                 }
302         newkey = ec_ephemeral_key_to_EVP(pubk,NID_id_GostR3410_2001,ephemeral);
303         if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,newkey))
304                 {
305                 GOSTerr(GOST_F_MAKE_RFC4490_KEYTRANSPORT_2001,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
306                 goto err;
307                 }       
308         ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
309         gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
310         EVP_PKEY_free(newkey);
311         return gkt;
312         memerr:
313         GOSTerr(GOST_F_MAKE_RFC4490_KEYTRANSPORT_2001,
314                 GOST_R_MALLOC_FAILURE);
315         err:            
316         GOST_KEY_TRANSPORT_free(gkt);
317         return NULL;
318         }
319
320 /*  
321  * EVP_PKEY_METHOD callback encrypt  
322  * Implementation of GOST2001 key transport, cryptopo variation 
323  */
324
325 int pkey_GOST01cp_encrypt (EVP_PKEY_CTX *pctx, unsigned char *out, size_t *out_len, const unsigned char *key,size_t key_len) 
326         {
327         GOST_KEY_TRANSPORT *gkt=NULL; 
328         EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
329         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
330         unsigned char ukm[8];
331         int ret=0;
332         if (RAND_bytes(ukm,8)<=0)
333                 {
334                 GOSTerr(GOST_F_PKEY_GOST01CP_ENCRYPT,
335                         GOST_R_RANDOM_GENERATOR_FAILURE);
336                 return 0;
337                 }       
338                 
339         if (!(gkt=make_rfc4490_keytransport_2001(pubk,gost_get_priv_key(data->eph_seckey),key, key_len,ukm,8)))
340                 {
341                 goto err;
342                 }       
343         if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt,&out))>0) ret =1;
344         GOST_KEY_TRANSPORT_free(gkt);
345         return ret;     
346         err:            
347         GOST_KEY_TRANSPORT_free(gkt);
348         return -1;
349         }
350 /* Public, because it would be needed in SSL implementation */
351 int decrypt_rfc4490_shared_key_2001(EVP_PKEY *priv,GOST_KEY_TRANSPORT *gkt,
352         unsigned char *key_buf,int key_buf_len) 
353         {
354         unsigned char wrappedKey[44];
355         unsigned char sharedKey[32];
356         gost_ctx ctx;
357         const struct gost_cipher_info *param=NULL;
358         EVP_PKEY *eph_key=NULL;
359         
360         eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
361         param = get_encryption_params(gkt->key_agreement_info->cipher);
362         gost_init(&ctx,param->sblock);  
363         OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
364         memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8);
365         OPENSSL_assert(gkt->key_info->encrypted_key->length==32);
366         memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32);
367         OPENSSL_assert(gkt->key_info->imit->length==4);
368         memcpy(wrappedKey+40,gkt->key_info->imit->data,4);      
369         VKO_compute_key(sharedKey,32,EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
370                 EVP_PKEY_get0(priv),wrappedKey);
371         if (!keyUnwrapCryptoPro(&ctx,sharedKey,wrappedKey,key_buf))
372                 {
373                 GOSTerr(GOST_F_PKCS7_GOST94CP_KEY_TRANSPORT_DECRYPT,
374                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
375                 goto err;
376                 }       
377                                 
378         EVP_PKEY_free(eph_key);
379         return 32;
380         err:
381         EVP_PKEY_free(eph_key);
382         return -1;
383         }
384 /*  
385  * EVP_PKEY_METHOD callback decrypt  
386  * Implementation of GOST2001 key transport, cryptopo variation 
387  */
388 int pkey_GOST01cp_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t * key_len, const unsigned char *in, size_t in_len)
389         {
390         const unsigned char *p = in;
391         EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
392         GOST_KEY_TRANSPORT *gkt = NULL;
393         int ret=0;      
394
395         if (!key)
396                 {
397                 *key_len = 32;
398                 return 1;
399                 }       
400         gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
401                 in_len);
402         if (!gkt)
403                 {
404                 GOSTerr(GOST_F_PKCS7_GOST94CP_KEY_TRANSPORT_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
405                 return -1;
406                 }       
407         ret =   decrypt_rfc4490_shared_key_2001(priv,gkt,key,*key_len);
408         GOST_KEY_TRANSPORT_free(gkt);
409         return ret;
410         }