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