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