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