cae22ca0bcdfa5c9678d215600506032a8362bcc
[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 /* Implementation of the Diffi-Hellman key agreement scheme based on
24  * GOST-94 keys */
25
26 /* Computes Diffie-Hellman key and stores it into buffer in
27  * little-endian byte order as expected by both versions of GOST 94
28  * algorigthm
29  */
30 static int compute_pair_key_le(unsigned char *pair_key,BIGNUM *pub_key,DH *dh) 
31         {
32         unsigned char be_key[128];
33         int i,key_size;
34         key_size=DH_compute_key(be_key,pub_key,dh);
35         if (!key_size) return 0;
36         memset(pair_key,0,128);
37         for (i=0;i<key_size;i++)
38                 {
39                 pair_key[i]=be_key[key_size-1-i];
40                 }
41         return key_size;        
42         }       
43
44 /*
45  * Computes 256 bit Key exchange key as specified in RFC 4357 
46  */
47 static int make_cp_exchange_key(BIGNUM *priv_key,EVP_PKEY *pubk, unsigned char *shared_key)
48         {
49         unsigned char dh_key [128];
50         int ret;
51         gost_hash_ctx hash_ctx;
52         DH *dh = DH_new();
53         
54         memset(dh_key,0,128);
55         dh->g = BN_dup(pubk->pkey.dsa->g);
56         dh->p = BN_dup(pubk->pkey.dsa->p);
57         dh->priv_key = BN_dup(priv_key);
58         ret=compute_pair_key_le(dh_key,((DSA *)(EVP_PKEY_get0(pubk)))->pub_key,dh) ;
59         DH_free(dh);
60         if (!ret)       return 0;
61         init_gost_hash_ctx(&hash_ctx,&GostR3411_94_CryptoProParamSet);
62         start_hash(&hash_ctx);
63         hash_block(&hash_ctx,dh_key,128);
64         finish_hash(&hash_ctx,shared_key);
65         done_gost_hash_ctx(&hash_ctx);
66         return 1;
67         }
68
69 /* EVP_PKEY_METHOD callback derive. Implements VKO R 34.10-94 */
70
71 int pkey_gost94_derive(EVP_PKEY_CTX *ctx,unsigned char *key,size_t *keylen)
72         {
73                 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_peerkey(ctx);
74                 EVP_PKEY *mykey = EVP_PKEY_CTX_get0_pkey(ctx);
75                 *keylen = 32;
76                 if (key == NULL) return 1;
77
78                 return make_cp_exchange_key(gost_get0_priv_key(mykey), pubk, key);
79         }
80
81 /* EVP_PKEY_METHOD callback encrypt for
82  * GOST R 34.10-94 cryptopro modification
83  */
84
85
86 int pkey_GOST94cp_encrypt(EVP_PKEY_CTX *ctx, unsigned char *out, size_t *outlen, const unsigned char* key, size_t key_len ) 
87         {
88         GOST_KEY_TRANSPORT *gkt=NULL;
89         unsigned char shared_key[32], ukm[8],crypted_key[44];
90         const struct gost_cipher_info *param=get_encryption_params(NULL);
91         EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(ctx);
92         struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
93         int size=-1;
94         gost_ctx cctx;
95         int key_is_ephemeral=1;
96         EVP_PKEY *mykey = EVP_PKEY_CTX_get0_peerkey(ctx);
97
98         /* Do not use vizir cipher parameters with cryptopro */
99         if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS) && param ==  gost_cipher_list)
100                 {
101                 param= gost_cipher_list+1;
102                 }       
103
104         if (mykey) 
105                 {
106                 /* If key already set, it is not ephemeral */
107                 key_is_ephemeral=0;
108                 if (!gost_get0_priv_key(mykey)) 
109                         {
110                         GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
111                         GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
112                         goto err;
113                         }       
114                 } 
115         else 
116                 {
117                 /* Otherwise generate ephemeral key */
118                 key_is_ephemeral = 1;
119                 if (out) 
120                         {
121                         mykey = EVP_PKEY_new();
122                         EVP_PKEY_assign(mykey, EVP_PKEY_base_id(pubk),DSA_new());
123                         EVP_PKEY_copy_parameters(mykey,pubk);
124                         if (!gost_sign_keygen(EVP_PKEY_get0(mykey))) 
125                                 {
126                                 goto err;
127                                 }       
128                         }
129                 }       
130         if (out)
131                 make_cp_exchange_key(gost_get0_priv_key(mykey),pubk,shared_key);
132         if (data->shared_ukm) 
133                 {
134                 memcpy(ukm,data->shared_ukm,8);
135                 }
136         else if (out) 
137                 {       
138                 if (RAND_bytes(ukm,8)<=0)
139                         {
140                         GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
141                                         GOST_R_RANDOM_GENERATOR_FAILURE);
142                         goto err;
143                         }       
144                 }
145                 
146         if (out) {
147                 gost_init(&cctx,param->sblock);
148                 keyWrapCryptoPro(&cctx,shared_key,ukm,key,crypted_key);
149         }       
150         gkt = GOST_KEY_TRANSPORT_new();
151         if (!gkt)
152                 {
153                 goto memerr;
154                 }       
155         if(!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv,
156                         ukm,8))
157                 {
158                 goto memerr;
159                 }       
160         if (!ASN1_OCTET_STRING_set(gkt->key_info->imit,crypted_key+40,4))
161                 {
162                 goto memerr;
163                 }
164         if (!ASN1_OCTET_STRING_set(gkt->key_info->encrypted_key,crypted_key+8,32))
165                 {
166                 goto memerr;
167                 }
168         if (key_is_ephemeral) { 
169         if (!X509_PUBKEY_set(&gkt->key_agreement_info->ephem_key,out?mykey:pubk))
170                 {
171                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
172                 goto err;
173                 }
174                 if (out) EVP_PKEY_free(mykey);
175         }       
176         ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
177         gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
178         *outlen = i2d_GOST_KEY_TRANSPORT(gkt,out?&out:NULL);
179         if (!size)
180                 {
181                 GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,GOST_R_ERROR_PACKING_KEY_TRANSPORT_INFO);
182                 size=-1;
183                 }
184         GOST_KEY_TRANSPORT_free(gkt);
185         return 1;       
186         memerr:
187                 if (key_is_ephemeral) {
188                         EVP_PKEY_free(mykey);
189                 }       
190         GOSTerr(GOST_F_PKEY_GOST94CP_ENCRYPT,
191                 GOST_R_MALLOC_FAILURE);
192         err:            
193         GOST_KEY_TRANSPORT_free(gkt);
194         return -1;
195         }
196
197         
198 /* EVP_PLEY_METHOD callback decrypt for
199  * GOST R 34.10-94 cryptopro modification
200  */
201 int pkey_GOST94cp_decrypt(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *key_len,const unsigned char *in, size_t in_len) {
202         const unsigned char *p = in;
203         GOST_KEY_TRANSPORT *gkt = NULL;
204         unsigned char wrappedKey[44];
205         unsigned char sharedKey[32];
206         gost_ctx cctx;
207         const struct gost_cipher_info *param=NULL;
208         EVP_PKEY *eph_key=NULL;
209         EVP_PKEY *priv= EVP_PKEY_CTX_get0_pkey(ctx); 
210         
211         if (!key)
212                 {
213                 *key_len = 32;
214                 return 1;
215                 }       
216         
217         gkt = d2i_GOST_KEY_TRANSPORT(NULL,(const unsigned char **)&p,
218                 in_len);
219         if (!gkt)
220                 {
221                 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
222                 return 0;
223                 }       
224         eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
225         /* No ephemeral key in the structure. Check peer key in the context
226          */
227         if (!eph_key) {
228                 eph_key = EVP_PKEY_CTX_get0_peerkey(ctx);
229                 if (! eph_key) {
230                         GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
231                                 GOST_R_NO_PEER_KEY);
232                         goto err;
233                 }
234                 /* Increment reference count of peer key */
235                 CRYPTO_add(&(eph_key->references),1 ,CRYPTO_LOCK_EVP_PKEY);
236         }       
237
238
239         param = get_encryption_params(gkt->key_agreement_info->cipher);
240         gost_init(&cctx,param->sblock); 
241         OPENSSL_assert(gkt->key_agreement_info->eph_iv->length==8);
242         memcpy(wrappedKey,gkt->key_agreement_info->eph_iv->data,8);
243         OPENSSL_assert(gkt->key_info->encrypted_key->length==32);
244         memcpy(wrappedKey+8,gkt->key_info->encrypted_key->data,32);
245         OPENSSL_assert(gkt->key_info->imit->length==4);
246         memcpy(wrappedKey+40,gkt->key_info->imit->data,4);      
247         make_cp_exchange_key(gost_get0_priv_key(priv),eph_key,sharedKey);
248         if (!keyUnwrapCryptoPro(&cctx,sharedKey,wrappedKey,key))
249                 {
250                 GOSTerr(GOST_F_PKEY_GOST94CP_DECRYPT,
251                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
252                 goto err;
253                 }       
254                                 
255         EVP_PKEY_free(eph_key);
256         GOST_KEY_TRANSPORT_free(gkt);
257         return 1;
258 err:
259         EVP_PKEY_free(eph_key);
260         GOST_KEY_TRANSPORT_free(gkt);
261         return -1;
262         }       
263