]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_keyx.c
GOST key export, separate function
[openssl-gost/engine.git] / gost_ec_keyx.c
1 /**********************************************************************
2  *                        gost_ec_keyx.c                              *
3  *             Copyright (c) 2005-2013 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/err.h>
12 #include <openssl/rand.h>
13 #include <string.h>
14 #include <openssl/objects.h>
15 #include "gost89.h"
16 #include "e_gost_err.h"
17 #include "gost_keywrap.h"
18 #include "gost_lcl.h"
19
20 /* Implementation of CryptoPro VKO 34.10-2001/2012 algorithm */
21 static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size,
22                            const EC_POINT *pub_key, EC_KEY *priv_key,
23                            const unsigned char *ukm, size_t ukm_size,
24                            int vko_dgst_nid)
25 {
26     unsigned char *databuf = NULL;
27     BIGNUM *UKM = NULL, *p = NULL, *order = NULL, *X = NULL, *Y = NULL;
28     const BIGNUM *key = EC_KEY_get0_private_key(priv_key);
29     EC_POINT *pnt = EC_POINT_new(EC_KEY_get0_group(priv_key));
30     BN_CTX *ctx = BN_CTX_new();
31     EVP_MD_CTX *mdctx = NULL;
32     const EVP_MD *md = NULL;
33     int buf_len, half_len;
34     int ret = 0;
35
36     if (!ctx) {
37         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
38         return 0;
39     }
40     BN_CTX_start(ctx);
41
42     md = EVP_get_digestbynid(vko_dgst_nid);
43     if (!md) {
44         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_INVALID_DIGEST_TYPE);
45         goto err;
46     }
47
48     UKM = hashsum2bn(ukm, ukm_size);
49     p = BN_CTX_get(ctx);
50     order = BN_CTX_get(ctx);
51     X = BN_CTX_get(ctx);
52     Y = BN_CTX_get(ctx);
53     EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx);
54     BN_mod_mul(p, key, UKM, order, ctx);
55     if (!EC_POINT_mul(EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) {
56         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
57         goto err;
58     }
59     EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
60                                         pnt, X, Y, ctx);
61
62     half_len = BN_num_bytes(order);
63     buf_len = 2 * half_len;
64     databuf = OPENSSL_zalloc(buf_len);
65     if (!databuf) {
66         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
67         goto err;
68     }
69
70     /*
71      * Serialize elliptic curve point same way as we do it when saving key
72      */
73     store_bignum(Y, databuf, half_len);
74     store_bignum(X, databuf + half_len, half_len);
75     /* And reverse byte order of whole buffer */
76     BUF_reverse(databuf, NULL, buf_len);
77
78     mdctx = EVP_MD_CTX_new();
79     if (!mdctx) {
80         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
81         goto err;
82     }
83     EVP_MD_CTX_init(mdctx);
84     EVP_DigestInit_ex(mdctx, md, NULL);
85     EVP_DigestUpdate(mdctx, databuf, buf_len);
86     EVP_DigestFinal_ex(mdctx, shared_key, NULL);
87     ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
88
89  err:
90     BN_free(UKM);
91     BN_CTX_end(ctx);
92     BN_CTX_free(ctx);
93
94     EC_POINT_free(pnt);
95
96     EVP_MD_CTX_free(mdctx);
97
98     OPENSSL_free(databuf);
99
100     return ret;
101 }
102
103 /*
104  * keyout expected to be 64 bytes
105  * */
106 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
107                     const EC_POINT *pub_key, EC_KEY *priv_key,
108                     unsigned char *keyout)
109 {
110 /* Adjust UKM */
111     unsigned char real_ukm[16];
112     size_t keylen;
113
114     memset(real_ukm, 0, 16);
115     if (memcmp(ukm_source, real_ukm, 16) == 0)
116         real_ukm[15] = 1;
117     else
118         memcpy(real_ukm, ukm_source, 16);
119
120     switch (pkey_nid) {
121     case NID_id_GostR3410_2012_512:
122         keylen =
123             VKO_compute_key(keyout, 64, pub_key, priv_key, real_ukm, 16,
124                             NID_id_GostR3411_2012_512);
125         return (keylen) ? keylen : 0;
126         break;
127
128     case NID_id_GostR3410_2012_256:
129         {
130             unsigned char tmpkey[32];
131             keylen =
132                 VKO_compute_key(tmpkey, 32, pub_key, priv_key, real_ukm, 16,
133                                 NID_id_GostR3411_2012_256);
134
135             if (keylen == 0)
136                 return 0;
137
138             if (gost_kdftree2012_256
139                 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
140                  ukm_source + 16, 8, 1) > 0)
141                 keylen = 64;
142             else
143                 keylen = 0;
144
145             OPENSSL_cleanse(tmpkey, 32);
146             return (keylen) ? keylen : 0;
147             break;
148         }
149     default:
150         return 0;
151     }
152 }
153
154 /*
155  * EVP_PKEY_METHOD callback derive.
156  * Implements VKO R 34.10-2001/2012 algorithms
157  */
158 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
159 {
160     /*
161      * Public key of peer in the ctx field peerkey
162      * Our private key in the ctx pkey
163      * ukm is in the algorithm specific context data
164      */
165     EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
166     EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
167     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
168     int dgst_nid = NID_undef;
169
170     if (!data || !data->shared_ukm) {
171         GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
172         return 0;
173     }
174     /*
175      * shared_ukm_size = 8 stands for pre-2018 cipher suites
176      * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
177      *
178      * shared_ukm_size = 32 stands for pre-2018 cipher suites
179      * It means 64 bytes of shared_key, 16 bytes of UKM and either
180      * 64 bytes of hash or 64 bytes of TLSTREE output
181      * */
182
183     switch (data->shared_ukm_size) {
184     case 8:
185         {
186             if (key == NULL) {
187                 *keylen = 32;
188                 return 1;
189             }
190
191             EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
192             if (dgst_nid == NID_id_GostR3411_2012_512)
193                 dgst_nid = NID_id_GostR3411_2012_256;
194
195             *keylen =
196                 VKO_compute_key(key, 32,
197                                 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
198                                 (EC_KEY *)EVP_PKEY_get0(my_key),
199                                 data->shared_ukm, 8, dgst_nid);
200             return (*keylen) ? 1 : 0;
201         }
202         break;
203     case 32:
204         {
205             if (key == NULL) {
206                 *keylen = 64;
207                 return 1;
208             }
209
210             *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
211                                EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
212                                (EC_KEY *)EVP_PKEY_get0(my_key), key);
213             return (*keylen) ? 1 : 0;
214         }
215
216         break;
217     default:
218         return 0;
219     }
220 }
221
222 /*
223  * Generates ephemeral key based on pubk algorithm computes shared key using
224  * VKO and returns filled up GOST_KEY_TRANSPORT structure
225  */
226
227 /*
228  * EVP_PKEY_METHOD callback encrypt
229  * Implementation of GOST2001/12 key transport, cryptopro variation
230  */
231
232 int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
233                            size_t *out_len, const unsigned char *key,
234                            size_t key_len)
235 {
236     GOST_KEY_TRANSPORT *gkt = NULL;
237     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
238     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
239     int pkey_nid = EVP_PKEY_base_id(pubk);
240     ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
241         OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
242         OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
243     const struct gost_cipher_info *param =
244         get_encryption_params(crypt_params_obj);
245     unsigned char ukm[8], shared_key[32], crypted_key[44];
246     int ret = 0;
247     int key_is_ephemeral = 1;
248     gost_ctx cctx;
249     EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
250     if (data->shared_ukm) {
251         memcpy(ukm, data->shared_ukm, 8);
252     } else if (out) {
253
254         if (RAND_bytes(ukm, 8) <= 0) {
255             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
256             return 0;
257         }
258     }
259     /* Check for private key in the peer_key of context */
260     if (sec_key) {
261         key_is_ephemeral = 0;
262         if (!gost_get0_priv_key(sec_key)) {
263             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
264                     GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
265             goto err;
266         }
267     } else {
268         key_is_ephemeral = 1;
269         if (out) {
270             sec_key = EVP_PKEY_new();
271             if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
272                 || !EVP_PKEY_copy_parameters(sec_key, pubk)
273                 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
274                 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
275                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
276                 goto err;
277             }
278         }
279     }
280     if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
281         && param == gost_cipher_list) {
282         param = gost_cipher_list;
283     }
284     if (out) {
285         int dgst_nid = NID_undef;
286         EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
287         if (dgst_nid == NID_id_GostR3411_2012_512)
288             dgst_nid = NID_id_GostR3411_2012_256;
289
290         if (!VKO_compute_key(shared_key, 32,
291                              EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
292                              EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
293             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
294                     GOST_R_ERROR_COMPUTING_SHARED_KEY);
295             goto err;
296         }
297         gost_init(&cctx, param->sblock);
298         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
299     }
300     gkt = GOST_KEY_TRANSPORT_new();
301     if (!gkt) {
302         goto err;
303     }
304     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
305         goto err;
306     }
307     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
308         goto err;
309     }
310     if (!ASN1_OCTET_STRING_set
311         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
312         goto err;
313     }
314     if (key_is_ephemeral) {
315         if (!X509_PUBKEY_set
316             (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
317             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
318                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
319             goto err;
320         }
321     }
322     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
323     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
324     if (key_is_ephemeral)
325         EVP_PKEY_free(sec_key);
326     if (!key_is_ephemeral) {
327         /* Set control "public key from client certificate used" */
328         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
329             <= 0) {
330             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
331             goto err;
332         }
333     }
334     if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
335         ret = 1;
336     GOST_KEY_TRANSPORT_free(gkt);
337     return ret;
338  err:
339     if (key_is_ephemeral)
340         EVP_PKEY_free(sec_key);
341     GOST_KEY_TRANSPORT_free(gkt);
342     return -1;
343 }
344
345 int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
346                           size_t *out_len, const unsigned char *key,
347                           size_t key_len)
348 {
349     PSKeyTransport_gost *pst = NULL;
350     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
351     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
352     int pkey_nid = EVP_PKEY_base_id(pubk);
353     unsigned char expkeys[64];
354     EVP_PKEY *sec_key = NULL;
355     int ret = 0;
356     int mac_nid = NID_undef;
357     size_t mac_len = 0;
358     int exp_len = 0;
359     unsigned char *exp_buf = NULL;
360
361     switch (data->cipher_nid) {
362     case NID_magma_ctr:
363         mac_nid = NID_magma_mac;
364         mac_len = 8;
365         break;
366     case NID_grasshopper_ctr:
367         mac_nid = NID_grasshopper_mac;
368         mac_len = 16;
369         break;
370     default:
371         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
372         return -1;
373         break;
374     }
375     exp_len = key_len + mac_len;
376     exp_buf = OPENSSL_malloc(exp_len);
377     if (!exp_buf) {
378         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
379         return -1;
380     }
381
382     if (out) {
383         sec_key = EVP_PKEY_new();
384         if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
385             || !EVP_PKEY_copy_parameters(sec_key, pubk)
386             || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
387             GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
388                     GOST_R_ERROR_COMPUTING_SHARED_KEY);
389             goto err;
390         }
391     }
392     ret =
393         gost_keg(data->shared_ukm, pkey_nid,
394                  EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
395                  EVP_PKEY_get0(sec_key), expkeys);
396     if (ret <= 0) {
397         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
398                 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
399         goto err;
400     }
401
402     ret = gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
403                       mac_nid, expkeys + 0, data->shared_ukm + 24, 4, exp_buf,
404                       &exp_len);
405     if (ret <= 0) {
406         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
407         goto err;
408     }
409
410     pst = PSKeyTransport_gost_new();
411     if (!pst) {
412         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
413         goto err;
414     }
415
416     if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
417         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
418         goto err;
419     }
420
421     if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
422         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
423         goto err;
424     }
425
426     EVP_PKEY_free(sec_key);
427
428     if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
429         ret = 1;
430  err:
431     PSKeyTransport_gost_free(pst);
432     OPENSSL_free(exp_buf);
433     return ret;
434 }
435
436 /*
437  * EVP_PKEY_METHOD callback decrypt
438  * Implementation of GOST2001 key transport, cryptopro variation
439  */
440 int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
441                            size_t *key_len, const unsigned char *in,
442                            size_t in_len)
443 {
444     const unsigned char *p = in;
445     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
446     GOST_KEY_TRANSPORT *gkt = NULL;
447     int ret = 0;
448     unsigned char wrappedKey[44];
449     unsigned char sharedKey[32];
450     gost_ctx ctx;
451     const struct gost_cipher_info *param = NULL;
452     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
453     int dgst_nid = NID_undef;
454
455     if (!key) {
456         *key_len = 32;
457         return 1;
458     }
459     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
460     if (!gkt) {
461         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
462                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
463         return -1;
464     }
465
466     /* If key transport structure contains public key, use it */
467     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
468     if (eph_key) {
469         if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
470             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
471                     GOST_R_INCOMPATIBLE_PEER_KEY);
472             goto err;
473         }
474     } else {
475         /* Set control "public key from client certificate used" */
476         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
477             <= 0) {
478             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
479             goto err;
480         }
481     }
482     peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
483     if (!peerkey) {
484         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
485         goto err;
486     }
487
488     param = get_encryption_params(gkt->key_agreement_info->cipher);
489     if (!param) {
490         goto err;
491     }
492
493     gost_init(&ctx, param->sblock);
494     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
495     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
496     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
497     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
498     OPENSSL_assert(gkt->key_info->imit->length == 4);
499     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
500
501     EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
502     if (dgst_nid == NID_id_GostR3411_2012_512)
503         dgst_nid = NID_id_GostR3411_2012_256;
504
505     if (!VKO_compute_key(sharedKey, 32,
506                          EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
507                          EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
508         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
509                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
510         goto err;
511     }
512     if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
513         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
514                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
515         goto err;
516     }
517
518     ret = 1;
519  err:
520     EVP_PKEY_free(eph_key);
521     GOST_KEY_TRANSPORT_free(gkt);
522     return ret;
523 }