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