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