]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_keyx.c
Update INSTALL.md
[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 int VKO_compute_key(unsigned char *shared_key,
22                     const EC_POINT *pub_key, const EC_KEY *priv_key,
23                     const unsigned char *ukm, const size_t ukm_size,
24                     const int vko_dgst_nid)
25 {
26     unsigned char *databuf = NULL;
27     BIGNUM *scalar = NULL, *order = NULL, *X = NULL, *Y = NULL;
28     const EC_GROUP *grp = NULL;
29     EC_POINT *pnt = NULL;
30     BN_CTX *ctx = NULL;
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 = BN_CTX_secure_new()) == NULL) {
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     grp = EC_KEY_get0_group(priv_key);
49     scalar = BN_CTX_get(ctx);
50     order  = BN_CTX_get(ctx);
51     X = BN_CTX_get(ctx);
52
53     if ((Y = BN_CTX_get(ctx)) == NULL
54         || (pnt = EC_POINT_new(grp)) == NULL
55         || BN_lebin2bn(ukm, ukm_size, scalar) == NULL
56         || !BN_mod_mul(scalar, scalar, EC_KEY_get0_private_key(priv_key),
57                        EC_GROUP_get0_order(grp), ctx))
58         goto err;
59
60     /* these two curves have cofactor 4; the rest have cofactor 1 */
61     switch (EC_GROUP_get_curve_name(grp)) {
62         case NID_id_tc26_gost_3410_2012_256_paramSetA:
63         case NID_id_tc26_gost_3410_2012_512_paramSetC:
64             if (!BN_lshift(scalar, scalar, 2))
65                 goto err;
66             break;
67     }
68
69     if (!gost_ec_point_mul(grp, pnt, NULL, pub_key, scalar, ctx)) {
70         GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
71         goto err;
72     }
73     if (!EC_POINT_get_affine_coordinates(grp, pnt, X, Y, ctx) ||
74                         !EC_GROUP_get_order(grp, order, ctx)) {
75         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EC_LIB);
76         goto err;
77     }
78
79     half_len = BN_num_bytes(order);
80     buf_len = 2 * half_len;
81     if ((databuf = OPENSSL_malloc(buf_len)) == NULL) {
82         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
83         goto err;
84     }
85
86     /*
87      * Serialize elliptic curve point same way as we do it when saving key
88      */
89     if (BN_bn2lebinpad(X, databuf, half_len) != half_len
90         || BN_bn2lebinpad(Y, databuf + half_len, half_len) != half_len)
91         goto err;
92
93     if ((mdctx = EVP_MD_CTX_new()) == NULL) {
94         GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
95         goto err;
96     }
97     EVP_MD_CTX_init(mdctx);
98     EVP_DigestInit_ex(mdctx, md, NULL);
99     EVP_DigestUpdate(mdctx, databuf, buf_len);
100     EVP_DigestFinal_ex(mdctx, shared_key, NULL);
101     ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
102
103  err:
104     BN_CTX_end(ctx);
105     BN_CTX_free(ctx);
106     EC_POINT_free(pnt);
107     EVP_MD_CTX_free(mdctx);
108     OPENSSL_free(databuf);
109
110     return ret;
111 }
112
113 /*
114  * keyout expected to be 64 bytes
115  * */
116 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
117                     const EC_POINT *pub_key, const EC_KEY *priv_key,
118                     unsigned char *keyout)
119 {
120 /* Adjust UKM */
121     unsigned char real_ukm[16];
122     size_t keylen = 0;
123
124     memset(real_ukm, 0, 16);
125     if (memcmp(ukm_source, real_ukm, 16) == 0)
126         real_ukm[15] = 1;
127     else {
128         memcpy(real_ukm, ukm_source, 16);
129         BUF_reverse(real_ukm, NULL, 16);
130     }
131
132     switch (pkey_nid) {
133     case NID_id_GostR3410_2012_512:
134         keylen =
135             VKO_compute_key(keyout, pub_key, priv_key, real_ukm, 16,
136                             NID_id_GostR3411_2012_512);
137         return (keylen) ? keylen : 0;
138         break;
139
140     case NID_id_GostR3410_2012_256:
141         {
142             unsigned char tmpkey[32];
143             keylen =
144               VKO_compute_key(tmpkey, pub_key, priv_key, real_ukm, 16,
145                   NID_id_GostR3411_2012_256);
146
147             if (keylen == 0)
148                 return 0;
149
150             if (gost_kdftree2012_256
151                 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
152                  ukm_source + 16, 8, 1) > 0)
153                 keylen = 64;
154             else
155                 keylen = 0;
156
157             OPENSSL_cleanse(tmpkey, 32);
158             return (keylen) ? keylen : 0;
159         }
160     default:
161         return 0;
162     }
163 }
164
165 /*
166  * EVP_PKEY_METHOD callback derive.
167  * Implements VKO R 34.10-2001/2012 algorithms
168  */
169 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
170 {
171     /*
172      * Public key of peer in the ctx field peerkey
173      * Our private key in the ctx pkey
174      * ukm is in the algorithm specific context data
175      */
176     EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
177     EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
178     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
179     int dgst_nid = NID_undef;
180
181     if (!data || !data->shared_ukm) {
182         GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
183         return 0;
184     }
185     /*
186      * shared_ukm_size = 8 stands for pre-2018 cipher suites
187      * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
188      *
189      * shared_ukm_size = 32 stands for pre-2018 cipher suites
190      * It means 64 bytes of shared_key, 16 bytes of UKM and either
191      * 64 bytes of hash or 64 bytes of TLSTREE output
192      * */
193
194     switch (data->shared_ukm_size) {
195     case 8:
196         {
197             if (key == NULL) {
198                 *keylen = 32;
199                 return 1;
200             }
201
202             EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
203             if (dgst_nid == NID_id_GostR3411_2012_512)
204                 dgst_nid = NID_id_GostR3411_2012_256;
205
206             *keylen =
207                 VKO_compute_key(key,
208                                 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
209                                 (EC_KEY *)EVP_PKEY_get0(my_key),
210                                 data->shared_ukm, 8, dgst_nid);
211             return (*keylen) ? 1 : 0;
212         }
213         break;
214     case 32:
215         {
216             if (key == NULL) {
217                 *keylen = 64;
218                 return 1;
219             }
220
221             *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
222                                EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
223                                (EC_KEY *)EVP_PKEY_get0(my_key), key);
224             return (*keylen) ? 1 : 0;
225         }
226
227         break;
228     default:
229         return 0;
230     }
231 }
232
233 /*
234  * Generates ephemeral key based on pubk algorithm computes shared key using
235  * VKO and returns filled up GOST_KEY_TRANSPORT structure
236  */
237
238 /*
239  * EVP_PKEY_METHOD callback encrypt
240  * Implementation of GOST2001/12 key transport, cryptopro variation
241  */
242
243 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
244                            size_t *out_len, const unsigned char *key,
245                            size_t key_len)
246 {
247     GOST_KEY_TRANSPORT *gkt = NULL;
248     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
249     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
250     int pkey_nid = EVP_PKEY_base_id(pubk);
251     ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001 || pkey_nid == NID_id_GostR3410_2001DH) ?
252         OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
253         OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
254     const struct gost_cipher_info *param =
255         get_encryption_params(crypt_params_obj);
256     unsigned char ukm[8], shared_key[32], crypted_key[44];
257     int ret = 0;
258     int key_is_ephemeral = 1;
259     gost_ctx cctx;
260     EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
261     if (data->shared_ukm) {
262         memcpy(ukm, data->shared_ukm, 8);
263     } else {
264         if (RAND_bytes(ukm, 8) <= 0) {
265             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
266             return 0;
267         }
268     }
269     if (!param)
270         goto err;
271     /* Check for private key in the peer_key of context */
272     if (sec_key) {
273         key_is_ephemeral = 0;
274         if (!gost_get0_priv_key(sec_key)) {
275             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
276                     GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
277             goto err;
278         }
279     } else {
280         key_is_ephemeral = 1;
281         if (out) {
282             sec_key = EVP_PKEY_new();
283             if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
284                 || !EVP_PKEY_copy_parameters(sec_key, pubk)
285                 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
286                 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
287                         GOST_R_ERROR_COMPUTING_SHARED_KEY);
288                 goto err;
289             }
290         }
291     }
292     if (out) {
293         int dgst_nid = NID_undef;
294         EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
295         if (dgst_nid == NID_id_GostR3411_2012_512)
296             dgst_nid = NID_id_GostR3411_2012_256;
297
298         if (!VKO_compute_key(shared_key,
299                              EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
300                              EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
301             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
302                     GOST_R_ERROR_COMPUTING_SHARED_KEY);
303             goto err;
304         }
305         gost_init(&cctx, param->sblock);
306         keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
307     }
308     gkt = GOST_KEY_TRANSPORT_new();
309     if (!gkt) {
310         goto err;
311     }
312     if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
313         goto err;
314     }
315     if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
316         goto err;
317     }
318     if (!ASN1_OCTET_STRING_set
319         (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
320         goto err;
321     }
322     if (key_is_ephemeral) {
323         if (!X509_PUBKEY_set
324             (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
325             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
326                     GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
327             goto err;
328         }
329     }
330     ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
331     gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
332     if (key_is_ephemeral)
333         EVP_PKEY_free(sec_key);
334     if (!key_is_ephemeral) {
335         /* Set control "public key from client certificate used" */
336         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
337             <= 0) {
338             GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
339             goto err;
340         }
341     }
342     if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
343         ret = 1;
344     OPENSSL_cleanse(shared_key, sizeof(shared_key));
345     GOST_KEY_TRANSPORT_free(gkt);
346     return ret;
347  err:
348     OPENSSL_cleanse(shared_key, sizeof(shared_key));
349     if (key_is_ephemeral)
350         EVP_PKEY_free(sec_key);
351     GOST_KEY_TRANSPORT_free(gkt);
352     return -1;
353 }
354
355 /*
356  * EVP_PKEY_METHOD callback decrypt
357  * Implementation of GOST2018 key transport
358  */
359 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
360                           size_t *out_len, const unsigned char *key,
361                           size_t key_len)
362 {
363     PSKeyTransport_gost *pst = NULL;
364     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
365     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
366     int pkey_nid = EVP_PKEY_base_id(pubk);
367     unsigned char expkeys[64];
368     EVP_PKEY *sec_key = NULL;
369     int ret = 0;
370     int mac_nid = NID_undef;
371     size_t mac_len = 0;
372     int exp_len = 0, iv_len = 0;
373     unsigned char *exp_buf = NULL;
374     int key_is_ephemeral = 0;
375
376     switch (data->cipher_nid) {
377     case NID_magma_ctr:
378         mac_nid = NID_magma_mac;
379         mac_len = 8;
380         iv_len = 4;
381         break;
382     case NID_grasshopper_ctr:
383         mac_nid = NID_grasshopper_mac;
384         mac_len = 16;
385         iv_len = 8;
386         break;
387     default:
388         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
389         return -1;
390         break;
391     }
392     exp_len = key_len + mac_len;
393     exp_buf = OPENSSL_malloc(exp_len);
394     if (!exp_buf) {
395         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
396         return -1;
397     }
398
399     sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
400     if (!sec_key)
401     {
402       sec_key = EVP_PKEY_new();
403       if (sec_key == NULL) {
404         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
405         goto err;
406       }
407
408       if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
409           || !EVP_PKEY_copy_parameters(sec_key, pubk)
410           || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
411         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
412             GOST_R_ERROR_COMPUTING_SHARED_KEY);
413         goto err;
414       }
415       key_is_ephemeral = 1;
416     }
417
418     if (gost_keg(data->shared_ukm, pkey_nid,
419                  EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
420                  EVP_PKEY_get0(sec_key), expkeys) <= 0) {
421         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
422                 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
423         goto err;
424     }
425
426     if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
427                     mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
428                     exp_buf, &exp_len) <= 0) {
429         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
430         goto err;
431     }
432
433     pst = PSKeyTransport_gost_new();
434     if (!pst) {
435         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
436         goto err;
437     }
438
439     if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
440         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
441         goto err;
442     }
443
444     if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
445         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
446         goto err;
447     }
448
449     if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
450         ret = 1;
451  err:
452     OPENSSL_cleanse(expkeys, sizeof(expkeys));
453     if (key_is_ephemeral)
454       EVP_PKEY_free(sec_key);
455
456     PSKeyTransport_gost_free(pst);
457     OPENSSL_free(exp_buf);
458     return ret;
459 }
460
461 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
462                       size_t *out_len, const unsigned char *key, size_t key_len)
463 {
464     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
465     if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
466         return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
467     else if (data->shared_ukm_size == 32)
468         return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
469     else {
470         GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
471         return -1;
472     }
473 }
474
475 /*
476  * EVP_PKEY_METHOD callback decrypt
477  * Implementation of GOST2001/12 key transport, cryptopro variation
478  */
479 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
480                            size_t *key_len, const unsigned char *in,
481                            size_t in_len)
482 {
483     const unsigned char *p = in;
484     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
485     GOST_KEY_TRANSPORT *gkt = NULL;
486     int ret = 0;
487     unsigned char wrappedKey[44];
488     unsigned char sharedKey[32];
489     gost_ctx ctx;
490     const struct gost_cipher_info *param = NULL;
491     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
492     int dgst_nid = NID_undef;
493
494     if (!key) {
495         *key_len = 32;
496         return 1;
497     }
498     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
499     if (!gkt) {
500         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
501                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
502         return -1;
503     }
504
505     /* If key transport structure contains public key, use it */
506     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
507     if (eph_key) {
508         if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
509             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
510                     GOST_R_INCOMPATIBLE_PEER_KEY);
511             goto err;
512         }
513     } else {
514         /* Set control "public key from client certificate used" */
515         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
516             <= 0) {
517             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
518             goto err;
519         }
520     }
521     peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
522     if (!peerkey) {
523         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
524         goto err;
525     }
526
527     param = get_encryption_params(gkt->key_agreement_info->cipher);
528     if (!param) {
529         goto err;
530     }
531
532     gost_init(&ctx, param->sblock);
533     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
534     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
535     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
536     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
537     OPENSSL_assert(gkt->key_info->imit->length == 4);
538     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
539
540     EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
541     if (dgst_nid == NID_id_GostR3411_2012_512)
542         dgst_nid = NID_id_GostR3411_2012_256;
543
544     if (!VKO_compute_key(sharedKey,
545                          EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
546                          EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
547         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
548                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
549         goto err;
550     }
551     if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
552         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
553                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
554         goto err;
555     }
556
557     ret = 1;
558  err:
559     OPENSSL_cleanse(sharedKey, sizeof(sharedKey));
560     EVP_PKEY_free(eph_key);
561     GOST_KEY_TRANSPORT_free(gkt);
562     return ret;
563 }
564
565 /*
566  * EVP_PKEY_METHOD callback decrypt
567  * Implementation of GOST2018 key transport
568  */
569 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
570                           size_t *key_len, const unsigned char *in,
571                           size_t in_len)
572 {
573     const unsigned char *p = in;
574     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
575     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
576     PSKeyTransport_gost *pst = NULL;
577     int ret = 0;
578     unsigned char expkeys[64];
579     EVP_PKEY *eph_key = NULL;
580     int pkey_nid = EVP_PKEY_base_id(priv);
581     int mac_nid = NID_undef;
582     int iv_len = 0;
583
584     switch (data->cipher_nid) {
585     case NID_magma_ctr:
586         mac_nid = NID_magma_mac;
587         iv_len = 4;
588         break;
589     case NID_grasshopper_ctr:
590         mac_nid = NID_grasshopper_mac;
591         iv_len = 8;
592         break;
593     default:
594         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
595         return -1;
596         break;
597     }
598     if (!key) {
599         *key_len = 32;
600         return 1;
601     }
602
603     pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
604     if (!pst) {
605         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
606                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
607         return -1;
608     }
609
610     eph_key = X509_PUBKEY_get(pst->ephem_key);
611 /*
612  * TODO beldmit
613    1.  Checks the next three conditions fulfilling and terminates the
614    connection with fatal error if not.
615
616    o  Q_eph is on the same curve as server public key;
617
618    o  Q_eph is not equal to zero point;
619
620    o  q * Q_eph is not equal to zero point.
621 */
622     if (gost_keg(data->shared_ukm, pkey_nid,
623                  EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
624                  EVP_PKEY_get0(priv), expkeys) <= 0) {
625         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
626                 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
627         goto err;
628     }
629
630     if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
631                     ASN1_STRING_length(pst->psexp), data->cipher_nid,
632                     expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
633                     iv_len, key) <= 0) {
634         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
635         goto err;
636     }
637
638     ret = 1;
639  err:
640     OPENSSL_cleanse(expkeys, sizeof(expkeys));
641     EVP_PKEY_free(eph_key);
642     PSKeyTransport_gost_free(pst);
643     return ret;
644 }
645
646 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
647                       size_t *key_len, const unsigned char *in, size_t in_len)
648 {
649     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
650     if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
651         return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
652     else if (data->shared_ukm_size == 32)
653         return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
654     else {
655         GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);
656         return -1;
657     }
658 }