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