Updated GOST MAC support.
[openssl.git] / engines / ccgost / gost94_keyx.c
1 /**********************************************************************
2  *                             gost94_keyx.c                          *
3  *             Copyright (c) 2005-2006 Cryptocom LTD                  *
4  *         This file is distributed under the same license as OpenSSL *
5  *                                                                    *
6  *     Implements generation and parsing of GOST_KEY_TRANSPORT for    *
7  *                      GOST R 34.10-94 algorithms                            *
8  *                                                                                                                                        *
9  *          Requires OpenSSL 0.9.9 for compilation                    *
10  **********************************************************************/
11 #include <string.h>
12 #include <openssl/dh.h>
13 #include <openssl/rand.h>
14 #include <openssl/evp.h>
15 #include <openssl/objects.h>
16
17 #include "gost89.h"
18 #include "gosthash.h"
19 #include "e_gost_err.h"
20 #include "gost_keywrap.h"
21 #include "gost_lcl.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)
25         {
26         int i;
27         int j;
28         int blocks = crypted_key_len >>3;
29         unsigned char gamma[8];
30         if (max_key_len <crypted_key_len)
31                 {
32                 GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_NOT_ENOUGH_SPACE_FOR_KEY);
33                 return 0;
34                 }       
35         if ((crypted_key_len & 7) !=0) 
36                 {
37                 GOSTerr(GOST_F_DECRYPT_CRYPTOCOM_KEY,GOST_R_INVALID_ENCRYPTED_KEY_SIZE);
38                 return 0;
39                 }       
40         for (i=blocks-1;i>0;i--) 
41                 {
42                 gostcrypt(ctx,crypted_key+(i-1)*8,gamma);
43                 for(j=0;j<8;j++) 
44                         {
45                         sess_key[i*8+j]=gamma[j]^crypted_key[i*8+j];
46                         }
47                 }       
48         gostcrypt(ctx,sess_key+crypted_key_len-8,gamma);        
49         for(j=0;j<8;j++) 
50                 {
51                 sess_key[j]=gamma[j]^crypted_key[j];
52                 }
53         return 1;
54         }
55 int encrypt_cryptocom_key(const unsigned char *sess_key,int key_len,
56         unsigned char *crypted_key, gost_ctx *ctx)
57         {
58         int i;
59         int j;
60         unsigned char gamma[8];
61         memcpy(gamma,sess_key+key_len-8,8);
62         for (i=0;i<key_len;i+=8)
63                 {
64                 gostcrypt(ctx,gamma,gamma);
65                 for (j=0;j<8;j++)
66                         gamma[j]=crypted_key[i+j]=sess_key[i+j]^gamma[j];
67                 }
68         return 1;
69         }
70 /* Implementation of the Diffi-Hellman key agreement scheme based on
71  * GOST-94 keys */
72
73 /* Computes Diffie-Hellman key and stores it into buffer in
74  * little-endian byte order as expected by both versions of GOST 94
75  * algorigthm
76  */
77 static int compute_pair_key_le(unsigned char *pair_key,BIGNUM *pub_key,DH *dh) 
78         {
79         unsigned char be_key[128];
80         int i,key_size;
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++)
85                 {
86                 pair_key[i]=be_key[key_size-1-i];
87                 }
88         return key_size;        
89         }       
90 /*
91  * Computes 256 bit key exchange key for CryptoCom variation of GOST 94
92  * algorithm
93  */
94 static int make_gost_shared_key(DH *dh,EVP_PKEY *pubk,unsigned char *shared_key) 
95         {
96         unsigned char dh_key [128];
97         int i;
98         /* Compute key */
99         memset(dh_key,0,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
104          */
105         
106         if (DH_size(dh)==128)
107                 {
108                 for (i=0;i<64;i++)
109                         {
110                         dh_key[i]^=dh_key[64+i];
111                         }
112                 }
113         for (i=0;i<32;i++)
114                 {
115                 shared_key[i]=dh_key[i]^dh_key[32+i];
116                 }
117         return 1;
118         }
119
120 static DH *make_ephemeral_key(EVP_PKEY *pubk,BIGNUM *ephemeral_key)
121         {
122         DH *dh = DH_new();
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))
128                 {
129                 DH_free(dh);
130                 return NULL;
131                 }       
132         return dh;
133         }       
134 /*
135  * Computes 256 bit Key exchange key as specified in RFC 4357 
136  */
137 static int make_cp_exchange_key(DH *dh,EVP_PKEY *pubk, unsigned char *shared_key)
138         {
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);
148         return 1;
149         }
150
151 /* EVP_PKEY_METHOD callback encrypt for
152  * GOST R 34.10-94 cryptopro modification
153  */
154
155 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len ) 
156         {
157         GOST_KEY_TRANSPORT *gkt=NULL;
158         DH *dh = 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);
163         int size=-1;
164         gost_ctx cctx;
165
166         if (!(data->eph_seckey))
167                 {
168                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
169                         GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
170                 return -1;
171                 }       
172
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)
177                 {
178                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
179                         GOST_R_RANDOM_GENERATOR_FAILURE);
180                 return -1;
181                 }       
182         keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key);
183         gkt = GOST_KEY_TRANSPORT_new();
184         if (!gkt)
185                 {
186                 goto memerr;
187                 }       
188         if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
189                         ukm,8))
190                 {
191                 goto memerr;
192                 }       
193         if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4))
194                 {
195                 goto memerr;
196                 }
197         if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32))
198                 {
199                 goto memerr;
200                 }
201         if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
202                 {
203                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
204                 goto err;
205                 }       
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);
209         if (!size)
210                 {
211                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
212                 size=-1;
213                 }
214         GOST_KEY_TRANSPORT_free(gkt);
215         DH_free(dh);
216         return 1;       
217         memerr:
218         GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
219                 GOST_R_MALLOC_FAILURE);
220         err:            
221         GOST_KEY_TRANSPORT_free(gkt);
222         DH_free(dh);
223         return -1;
224         }
225
226 /* EVP_PKEY_METHOD callback encrypt for
227  * GOST R 34.10-94 cryptocom modification
228  */
229
230 int pkey_GOST94cc_encrypt (EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen,  const unsigned char *   key,size_t key_len) 
231         {
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 */
235         DH *dh = NULL;
236         GOST_KEY_TRANSPORT *gkt = NULL;
237         const struct gost_cipher_info *cipher_info;
238         gost_ctx cctx;
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};
242
243         if (! data->eph_seckey)
244                 {
245                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
246                         GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
247                 return -1;
248                 }       
249         dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
250         if (!dh) goto err;
251         /* compute shared key */
252         if (!make_gost_shared_key(dh,pubk,shared_key)) 
253                 {
254                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
255                 goto err;
256                 }       
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)) 
264                 {
265                 DH_free(dh);
266                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
267                 return -1;
268                 }
269         gkt = GOST_KEY_TRANSPORT_new();
270         if (!gkt) 
271                 {
272                 DH_free(dh);
273                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_NO_MEMORY);
274                 return -1;
275                 }       
276         /* Store IV which is always zero in our case */
277         if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
278                 {
279                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
280                 goto err;
281                 }       
282         if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4)) 
283                 {
284                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
285                 goto err;
286                 }       
287         if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
288                 {       
289                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
290                 goto err;
291                 }
292         if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
293                 {
294                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
295                 goto err;
296                 }       
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);
300         err:
301         if (gkt) GOST_KEY_TRANSPORT_free(gkt);
302         if (dh) DH_free(dh);
303         if (newkey) EVP_PKEY_free(newkey);
304         return 1;
305         }       
306         
307 /* EVP_PLEY_METHOD callback decrypt for
308  * GOST R 34.10-94 cryptopro modification
309  */
310 int pkey_GOST94cp_decrypt (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) {
311         DH *dh = DH_new();
312         const unsigned char *p = in;
313         GOST_KEY_TRANSPORT *gkt = NULL;
314         unsigned char wrappedKey[44];
315         unsigned char sharedKey[32];
316         gost_ctx cctx;
317         const struct gost_cipher_info *param=NULL;
318         EVP_PKEY *eph_key=NULL;
319         EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx); 
320         
321         if (!key)
322                 {
323                 *key_len = 32;
324                 return 1;
325                 }       
326         
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,
331                 in_len);
332         if (!gkt)
333                 {
334                 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
335                 DH_free(dh);
336                 return 0;
337                 }       
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))
349                 {
350                 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
351                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
352                 goto err;
353                 }       
354                                 
355         EVP_PKEY_free(eph_key);
356         GOST_KEY_TRANSPORT_free(gkt);
357         DH_free(dh);
358         return 1;
359 err:
360         EVP_PKEY_free(eph_key);
361         GOST_KEY_TRANSPORT_free(gkt);
362         DH_free(dh);
363         return -1;
364         }       
365
366 /* EVP_PKEY_METHOD callback decrypt for
367  * GOST R 34.10-94 cryptocom modification
368  */
369
370 int pkey_GOST94cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
371         {
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];
377         unsigned char iv[8];
378         int i;
379         const struct gost_cipher_info *cipher_info;
380         gost_ctx ctx;
381         DH *dh = DH_new();
382         EVP_PKEY *eph_key;
383         EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
384         
385         if (!key)
386                 {
387                 *key_len = 32;
388                 return 1;
389                 }
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,
396                 in_len);
397         if (!gkt)
398                 {
399                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
400                 DH_free(dh);
401                 return 0;
402                 }       
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);
413         DH_free(dh);
414         if (!i) 
415                 {
416                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
417                 GOST_KEY_TRANSPORT_free(gkt);
418                 return 0;
419                 }
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);
424         
425         if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data, 
426                         gkt->key_info->encrypted_key->length, &ctx)) 
427                 {
428                 GOST_KEY_TRANSPORT_free(gkt);
429                 return 0;
430                 }
431         GOST_KEY_TRANSPORT_free(gkt);
432         /* check HMAC of session key*/
433         if (!gost_mac(&ctx,32,key,32,hmac_comp))
434                 {
435                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
436                 return 0;
437                 }
438         /* HMAC of session key is not correct */
439     if (memcmp(hmac,hmac_comp,4)!=0)
440                 {
441                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);
442                 return 0;
443                 }       
444         return 1; 
445         }