]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_ec_keyx.c
Errors adjusted
[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(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 static 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 /*
346  * EVP_PKEY_METHOD callback decrypt
347  * Implementation of GOST2018 key transport
348  */
349 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
350                           size_t *out_len, const unsigned char *key,
351                           size_t key_len)
352 {
353     PSKeyTransport_gost *pst = NULL;
354     EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
355     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
356     int pkey_nid = EVP_PKEY_base_id(pubk);
357     unsigned char expkeys[64];
358     EVP_PKEY *sec_key = NULL;
359     int ret = 0;
360     int mac_nid = NID_undef;
361     size_t mac_len = 0;
362     int exp_len = 0, iv_len = 0;
363     unsigned char *exp_buf = NULL;
364
365     switch (data->cipher_nid) {
366     case NID_magma_ctr:
367         mac_nid = NID_magma_mac;
368         mac_len = 8;
369         iv_len = 4;
370         break;
371     case NID_grasshopper_ctr:
372         mac_nid = NID_grasshopper_mac;
373         mac_len = 16;
374         iv_len = 8;
375         break;
376     default:
377         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
378         return -1;
379         break;
380     }
381     exp_len = key_len + mac_len;
382     exp_buf = OPENSSL_malloc(exp_len);
383     if (!exp_buf) {
384         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
385         return -1;
386     }
387
388     sec_key = EVP_PKEY_new();
389     if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
390         || !EVP_PKEY_copy_parameters(sec_key, pubk)
391         || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
392         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
393                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
394         goto err;
395     }
396
397     if (gost_keg(data->shared_ukm, pkey_nid,
398                  EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
399                  EVP_PKEY_get0(sec_key), expkeys) <= 0) {
400         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
401                 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
402         goto err;
403     }
404
405     if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
406                     mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
407                     exp_buf, &exp_len) <= 0) {
408         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
409         goto err;
410     }
411
412     pst = PSKeyTransport_gost_new();
413     if (!pst) {
414         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
415         goto err;
416     }
417
418     if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
419         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
420         goto err;
421     }
422
423     if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
424         GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
425         goto err;
426     }
427
428     EVP_PKEY_free(sec_key);
429
430     if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
431         ret = 1;
432  err:
433     PSKeyTransport_gost_free(pst);
434     OPENSSL_free(exp_buf);
435     return ret;
436 }
437
438 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
439                       size_t *out_len, const unsigned char *key, size_t key_len)
440 {
441     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
442     if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
443         return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
444     else if (data->shared_ukm_size == 32)
445         return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
446     else {
447         GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
448         return -1;
449     }
450 }
451
452 /*
453  * EVP_PKEY_METHOD callback decrypt
454  * Implementation of GOST2001/12 key transport, cryptopro variation
455  */
456 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
457                            size_t *key_len, const unsigned char *in,
458                            size_t in_len)
459 {
460     const unsigned char *p = in;
461     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
462     GOST_KEY_TRANSPORT *gkt = NULL;
463     int ret = 0;
464     unsigned char wrappedKey[44];
465     unsigned char sharedKey[32];
466     gost_ctx ctx;
467     const struct gost_cipher_info *param = NULL;
468     EVP_PKEY *eph_key = NULL, *peerkey = NULL;
469     int dgst_nid = NID_undef;
470
471     if (!key) {
472         *key_len = 32;
473         return 1;
474     }
475     gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
476     if (!gkt) {
477         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
478                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
479         return -1;
480     }
481
482     /* If key transport structure contains public key, use it */
483     eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
484     if (eph_key) {
485         if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
486             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
487                     GOST_R_INCOMPATIBLE_PEER_KEY);
488             goto err;
489         }
490     } else {
491         /* Set control "public key from client certificate used" */
492         if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
493             <= 0) {
494             GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
495             goto err;
496         }
497     }
498     peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
499     if (!peerkey) {
500         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
501         goto err;
502     }
503
504     param = get_encryption_params(gkt->key_agreement_info->cipher);
505     if (!param) {
506         goto err;
507     }
508
509     gost_init(&ctx, param->sblock);
510     OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
511     memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
512     OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
513     memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
514     OPENSSL_assert(gkt->key_info->imit->length == 4);
515     memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
516
517     EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
518     if (dgst_nid == NID_id_GostR3411_2012_512)
519         dgst_nid = NID_id_GostR3411_2012_256;
520
521     if (!VKO_compute_key(sharedKey, 32,
522                          EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
523                          EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
524         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
525                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
526         goto err;
527     }
528     if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
529         GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
530                 GOST_R_ERROR_COMPUTING_SHARED_KEY);
531         goto err;
532     }
533
534     ret = 1;
535  err:
536     EVP_PKEY_free(eph_key);
537     GOST_KEY_TRANSPORT_free(gkt);
538     return ret;
539 }
540
541 /*
542  * EVP_PKEY_METHOD callback decrypt
543  * Implementation of GOST2018 key transport
544  */
545 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
546                           size_t *key_len, const unsigned char *in,
547                           size_t in_len)
548 {
549     const unsigned char *p = in;
550     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
551     EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
552     PSKeyTransport_gost *pst = NULL;
553     int ret = 0;
554     unsigned char expkeys[64];
555     EVP_PKEY *eph_key = NULL;
556     int pkey_nid = EVP_PKEY_base_id(priv);
557     int mac_nid = NID_undef;
558     int iv_len = 0;
559
560     switch (data->cipher_nid) {
561     case NID_magma_ctr:
562         mac_nid = NID_magma_mac;
563         iv_len = 4;
564         break;
565     case NID_grasshopper_ctr:
566         mac_nid = NID_grasshopper_mac;
567         iv_len = 8;
568         break;
569     default:
570         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
571         return -1;
572         break;
573     }
574     if (!key) {
575         *key_len = 32;
576         return 1;
577     }
578
579     pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
580     if (!pst) {
581         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
582                 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
583         return -1;
584     }
585
586     eph_key = X509_PUBKEY_get(pst->ephem_key);
587 /*
588  * TODO beldmit
589    1.  Checks the next three conditions fulfilling and terminates the
590    connection with fatal error if not.
591
592    o  Q_eph is on the same curve as server public key;
593
594    o  Q_eph is not equal to zero point;
595
596    o  q * Q_eph is not equal to zero point.
597 */
598     if (gost_keg(data->shared_ukm, pkey_nid,
599                  EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
600                  EVP_PKEY_get0(priv), expkeys) <= 0) {
601         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
602                 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
603         goto err;
604     }
605
606     if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
607                     ASN1_STRING_length(pst->psexp), data->cipher_nid,
608                     expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
609                     iv_len, key) <= 0) {
610         GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
611         goto err;
612     }
613
614     ret = 1;
615  err:
616     EVP_PKEY_free(eph_key);
617     PSKeyTransport_gost_free(pst);
618     return ret;
619 }
620
621 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
622                       size_t *key_len, const unsigned char *in, size_t in_len)
623 {
624     struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
625     if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
626         return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
627     else if (data->shared_ukm_size == 32)
628         return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
629     else {
630         GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);
631         return -1;
632     }
633 }