Updated file.
[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         gost_ctx cctx;
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};
241
242         if (! data->eph_seckey)
243                 {
244                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
245                         GOST_R_CTX_NOT_INITIALIZED_FOR_ENCRYPT);
246                 return -1;
247                 }       
248         dh = make_ephemeral_key(pubk,gost_get_priv_key(data->eph_seckey));
249         if (!dh) goto err;
250         /* compute shared key */
251         if (!make_gost_shared_key(dh,pubk,shared_key)) 
252                 {
253                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
254                 goto err;
255                 }       
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)) 
262                 {
263                 DH_free(dh);
264                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_COMPUTING_MAC);
265                 return -1;
266                 }
267         gkt = GOST_KEY_TRANSPORT_new();
268         if (!gkt) 
269                 {
270                 DH_free(dh);
271                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_NO_MEMORY);
272                 return -1;
273                 }       
274         /* Store IV which is always zero in our case */
275         if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,iv,8))
276                 {
277                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_IV);
278                 goto err;
279                 }       
280         if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,hmac,4)) 
281                 {
282                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_MAC);
283                 goto err;
284                 }       
285         if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,encrypted_key,32))
286                 {       
287                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_ERROR_STORING_ENCRYPTED_KEY);
288                 goto err;
289                 }
290         if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,data->eph_seckey))
291                 {
292                 GOSTerr(GOST_F_PKEY_GOST94CC_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
293                 goto err;
294                 }       
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);
298         err:
299         if (gkt) GOST_KEY_TRANSPORT_free(gkt);
300         if (dh) DH_free(dh);
301         if (newkey) EVP_PKEY_free(newkey);
302         return 1;
303         }       
304         
305 /* EVP_PLEY_METHOD callback decrypt for
306  * GOST R 34.10-94 cryptopro modification
307  */
308 int pkey_GOST94cp_decrypt (EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) {
309         DH *dh = DH_new();
310         const unsigned char *p = in;
311         GOST_KEY_TRANSPORT *gkt = NULL;
312         unsigned char wrappedKey[44];
313         unsigned char sharedKey[32];
314         gost_ctx cctx;
315         const struct gost_cipher_info *param=NULL;
316         EVP_PKEY *eph_key=NULL;
317         EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx); 
318         
319         if (!key)
320                 {
321                 *key_len = 32;
322                 return 1;
323                 }       
324         
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,
329                 in_len);
330         if (!gkt)
331                 {
332                 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
333                 DH_free(dh);
334                 return 0;
335                 }       
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))
347                 {
348                 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
349                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
350                 goto err;
351                 }       
352                                 
353         EVP_PKEY_free(eph_key);
354         GOST_KEY_TRANSPORT_free(gkt);
355         DH_free(dh);
356         return 1;
357 err:
358         EVP_PKEY_free(eph_key);
359         GOST_KEY_TRANSPORT_free(gkt);
360         DH_free(dh);
361         return -1;
362         }       
363
364 /* EVP_PKEY_METHOD callback decrypt for
365  * GOST R 34.10-94 cryptocom modification
366  */
367
368 int pkey_GOST94cc_decrypt (EVP_PKEY_CTX *pctx, unsigned char *key, size_t *key_len, const unsigned char *in, size_t in_len)
369         {
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];
375         unsigned char iv[8];
376         int i;
377         gost_ctx ctx;
378         DH *dh = DH_new();
379         EVP_PKEY *eph_key;
380         EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
381         
382         if (!key)
383                 {
384                 *key_len = 32;
385                 return 1;
386                 }
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,
393                 in_len);
394         if (!gkt)
395                 {
396                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
397                 DH_free(dh);
398                 return 0;
399                 }       
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);
410         DH_free(dh);
411         if (!i) 
412                 {
413                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_SHARED_KEY);
414                 GOST_KEY_TRANSPORT_free(gkt);
415                 return 0;
416                 }
417         /* Decrypt session key */
418         gost_init(&ctx, &GostR3411_94_CryptoProParamSet);
419         gost_key(&ctx,shared_key);
420         
421         if (!decrypt_cryptocom_key(key,*key_len,gkt->key_info->encrypted_key->data, 
422                         gkt->key_info->encrypted_key->length, &ctx)) 
423                 {
424                 GOST_KEY_TRANSPORT_free(gkt);
425                 return 0;
426                 }
427         GOST_KEY_TRANSPORT_free(gkt);
428         /* check HMAC of session key*/
429         if (!gost_mac(&ctx,32,key,32,hmac_comp))
430                 {
431                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_ERROR_COMPUTING_MAC);
432                 return 0;
433                 }
434         /* HMAC of session key is not correct */
435     if (memcmp(hmac,hmac_comp,4)!=0)
436                 {
437                 GOSTerr(GOST_F_PKEY_GOST94CC_DECRYPT,GOST_R_SESSION_KEY_MAC_DOES_NOT_MATCH);
438                 return 0;
439                 }       
440         return 1; 
441         }