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