1 /**********************************************************************
4 * Copyright (c) 2005-2013 Cryptocom LTD *
5 * Copyright (c) 2018,2020 Dmitry Belyavskiy <beldmit@gmail.com> *
6 * Copyright (c) 2020 Billy Brumley <bbrumley@gmail.com> *
8 * This file is distributed under the same license as OpenSSL *
10 * VK0 R 50.1.113-2016 / RFC 7836 *
11 * KEG R 1323565.1.020-2018 *
12 * VK0 34.10-2001 key exchange and GOST R 34.10-2001 (RFC 4357) *
13 * based PKCS7/SMIME support *
14 * Requires OpenSSL 0.9.9 for compilation *
15 **********************************************************************/
16 #include <openssl/evp.h>
17 #include <openssl/err.h>
18 #include <openssl/rand.h>
20 #include <openssl/objects.h>
22 #include "e_gost_err.h"
23 #include "gost_keywrap.h"
26 /* Implementation of CryptoPro VKO 34.10-2001/2012 algorithm */
27 int VKO_compute_key(unsigned char *shared_key,
28 const EC_POINT *pub_key, const EC_KEY *priv_key,
29 const unsigned char *ukm, const size_t ukm_size,
30 const int vko_dgst_nid)
32 unsigned char *databuf = NULL;
33 BIGNUM *scalar = NULL, *X = NULL, *Y = NULL, *order = NULL;
34 const EC_GROUP *grp = NULL;
37 EVP_MD_CTX *mdctx = NULL;
38 const EVP_MD *md = NULL;
39 int buf_len, half_len;
42 if ((ctx = BN_CTX_secure_new()) == NULL) {
43 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
48 md = EVP_get_digestbynid(vko_dgst_nid);
50 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_INVALID_DIGEST_TYPE);
54 order = BN_CTX_get(ctx);
55 grp = EC_KEY_get0_group(priv_key);
56 scalar = BN_CTX_get(ctx);
58 EC_GROUP_get_order(grp, order, ctx);
60 if ((Y = BN_CTX_get(ctx)) == NULL
61 || (pnt = EC_POINT_new(grp)) == NULL
62 || BN_lebin2bn(ukm, ukm_size, scalar) == NULL
63 || !BN_mod_mul(scalar, scalar, EC_KEY_get0_private_key(priv_key),
64 EC_GROUP_get0_order(grp), ctx))
67 /* these two curves have cofactor 4; the rest have cofactor 1 */
68 switch (EC_GROUP_get_curve_name(grp)) {
69 case NID_id_tc26_gost_3410_2012_256_paramSetA:
70 case NID_id_tc26_gost_3410_2012_512_paramSetC:
71 if (!BN_lshift(scalar, scalar, 2))
76 if (!gost_ec_point_mul(grp, pnt, NULL, pub_key, scalar, ctx)) {
77 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
80 if (!EC_POINT_get_affine_coordinates(grp, pnt, X, Y, ctx)) {
81 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EC_LIB);
85 half_len = BN_num_bytes(order);
86 buf_len = 2 * half_len;
87 if ((databuf = OPENSSL_malloc(buf_len)) == NULL) {
88 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
93 * Serialize elliptic curve point same way as we do it when saving key
95 if (BN_bn2lebinpad(X, databuf, half_len) != half_len
96 || BN_bn2lebinpad(Y, databuf + half_len, half_len) != half_len)
99 if ((mdctx = EVP_MD_CTX_new()) == NULL) {
100 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
104 if (EVP_MD_CTX_init(mdctx) == 0
105 || EVP_DigestInit_ex(mdctx, md, NULL) == 0
106 || EVP_DigestUpdate(mdctx, databuf, buf_len) == 0
107 || EVP_DigestFinal_ex(mdctx, shared_key, NULL) == 0) {
108 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EVP_LIB);
112 ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
118 EVP_MD_CTX_free(mdctx);
119 OPENSSL_free(databuf);
125 * KEG Algorithm described in R 1323565.1.020-2018 6.4.5.1.
126 * keyout expected to be 64 bytes
128 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
129 const EC_POINT *pub_key, const EC_KEY *priv_key,
130 unsigned char *keyout)
133 unsigned char real_ukm[16];
136 memset(real_ukm, 0, 16);
137 if (memcmp(ukm_source, real_ukm, 16) == 0)
140 memcpy(real_ukm, ukm_source, 16);
141 BUF_reverse(real_ukm, NULL, 16);
145 case NID_id_GostR3410_2012_512:
147 VKO_compute_key(keyout, pub_key, priv_key, real_ukm, 16,
148 NID_id_GostR3411_2012_512);
149 return (keylen) ? keylen : 0;
152 case NID_id_GostR3410_2012_256:
154 unsigned char tmpkey[32];
156 VKO_compute_key(tmpkey, pub_key, priv_key, real_ukm, 16,
157 NID_id_GostR3411_2012_256);
162 if (gost_kdftree2012_256
163 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
164 ukm_source + 16, 8, 1) > 0)
169 OPENSSL_cleanse(tmpkey, 32);
170 return (keylen) ? keylen : 0;
178 * EVP_PKEY_METHOD callback derive.
179 * Implements VKO R 34.10-2001/2012 algorithms
182 * Backend for EVP_PKEY_derive()
183 * It have KEG mode (default) and VKO mode (enable by EVP_PKEY_CTRL_SET_VKO).
185 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
188 * Public key of peer in the ctx field peerkey
189 * Our private key in the ctx pkey
190 * ukm is in the algorithm specific context data
192 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
193 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
194 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
195 int dgst_nid = NID_undef;
197 if (!data || data->shared_ukm_size == 0) {
198 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
203 if (data->vko_dgst_nid) {
205 *keylen = data->vko_dgst_nid == NID_id_GostR3411_2012_256? 32 : 64;
208 *keylen = VKO_compute_key(key,
209 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
210 (EC_KEY *)EVP_PKEY_get0(my_key),
211 data->shared_ukm, data->shared_ukm_size,
213 return (*keylen) ? 1 : 0;
217 * shared_ukm_size = 8 stands for pre-2018 cipher suites
218 * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
220 * shared_ukm_size = 32 stands for post-2018 cipher suites
221 * It means 64 bytes of shared_key, 16 bytes of UKM and either
222 * 64 bytes of hash or 64 bytes of TLSTREE output
225 switch (data->shared_ukm_size) {
232 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
233 if (dgst_nid == NID_id_GostR3411_2012_512)
234 dgst_nid = NID_id_GostR3411_2012_256;
238 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
239 (EC_KEY *)EVP_PKEY_get0(my_key),
240 data->shared_ukm, 8, dgst_nid);
241 return (*keylen) ? 1 : 0;
251 *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
252 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
253 (EC_KEY *)EVP_PKEY_get0(my_key), key);
254 return (*keylen) ? 1 : 0;
264 * Generates ephemeral key based on pubk algorithm computes shared key using
265 * VKO and returns filled up GOST_KEY_TRANSPORT structure
269 * EVP_PKEY_METHOD callback encrypt
270 * Implementation of GOST2001/12 key transport, cryptopro variation
273 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
274 size_t *out_len, const unsigned char *key,
277 GOST_KEY_TRANSPORT *gkt = NULL;
278 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
279 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
280 int pkey_nid = EVP_PKEY_base_id(pubk);
281 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001 || pkey_nid == NID_id_GostR3410_2001DH) ?
282 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
283 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
284 const struct gost_cipher_info *param =
285 get_encryption_params(crypt_params_obj);
286 unsigned char ukm[8], shared_key[32], crypted_key[44];
288 int key_is_ephemeral = 1;
290 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
291 if (data->shared_ukm_size) {
292 memcpy(ukm, data->shared_ukm, 8);
294 if (RAND_bytes(ukm, 8) <= 0) {
295 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
301 /* Check for private key in the peer_key of context */
303 key_is_ephemeral = 0;
304 if (!gost_get0_priv_key(sec_key)) {
305 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
306 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
310 key_is_ephemeral = 1;
312 sec_key = EVP_PKEY_new();
313 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
314 || !EVP_PKEY_copy_parameters(sec_key, pubk)
315 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
316 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
317 GOST_R_ERROR_COMPUTING_SHARED_KEY);
323 int dgst_nid = NID_undef;
324 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
325 if (dgst_nid == NID_id_GostR3411_2012_512)
326 dgst_nid = NID_id_GostR3411_2012_256;
328 if (!VKO_compute_key(shared_key,
329 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
330 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
331 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
332 GOST_R_ERROR_COMPUTING_SHARED_KEY);
335 gost_init(&cctx, param->sblock);
336 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
338 gkt = GOST_KEY_TRANSPORT_new();
342 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
345 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
348 if (!ASN1_OCTET_STRING_set
349 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
352 if (key_is_ephemeral) {
354 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
355 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
356 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
360 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
361 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
362 if (key_is_ephemeral)
363 EVP_PKEY_free(sec_key);
364 if (!key_is_ephemeral) {
365 /* Set control "public key from client certificate used" */
366 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
368 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
372 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
374 OPENSSL_cleanse(shared_key, sizeof(shared_key));
375 GOST_KEY_TRANSPORT_free(gkt);
378 OPENSSL_cleanse(shared_key, sizeof(shared_key));
379 if (key_is_ephemeral)
380 EVP_PKEY_free(sec_key);
381 GOST_KEY_TRANSPORT_free(gkt);
386 * EVP_PKEY_METHOD callback decrypt
387 * Implementation of GOST2018 key transport
389 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
390 size_t *out_len, const unsigned char *key,
393 PSKeyTransport_gost *pst = NULL;
394 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
395 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
396 int pkey_nid = EVP_PKEY_base_id(pubk);
397 unsigned char expkeys[64];
398 EVP_PKEY *sec_key = NULL;
400 int mac_nid = NID_undef;
402 int exp_len = 0, iv_len = 0;
403 unsigned char *exp_buf = NULL;
404 int key_is_ephemeral = 0;
406 switch (data->cipher_nid) {
408 mac_nid = NID_magma_mac;
412 case NID_grasshopper_ctr:
413 mac_nid = NID_grasshopper_mac;
418 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
422 exp_len = key_len + mac_len;
423 exp_buf = OPENSSL_malloc(exp_len);
425 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
429 sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
432 sec_key = EVP_PKEY_new();
433 if (sec_key == NULL) {
434 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
438 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
439 || !EVP_PKEY_copy_parameters(sec_key, pubk)
440 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
441 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
442 GOST_R_ERROR_COMPUTING_SHARED_KEY);
445 key_is_ephemeral = 1;
448 if (data->shared_ukm_size == 0) {
449 if (RAND_bytes(data->shared_ukm, 32) <= 0) {
450 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_INTERNAL_ERROR);
453 data->shared_ukm_size = 32;
456 if (gost_keg(data->shared_ukm, pkey_nid,
457 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
458 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
459 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
460 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
464 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
465 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
466 exp_buf, &exp_len) <= 0) {
467 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
471 pst = PSKeyTransport_gost_new();
473 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
477 pst->ukm = ASN1_OCTET_STRING_new();
478 if (pst->ukm == NULL) {
479 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
483 if (!ASN1_OCTET_STRING_set(pst->ukm, data->shared_ukm, data->shared_ukm_size)) {
484 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
488 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
489 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
493 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
494 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
498 if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
501 OPENSSL_cleanse(expkeys, sizeof(expkeys));
502 if (key_is_ephemeral)
503 EVP_PKEY_free(sec_key);
505 PSKeyTransport_gost_free(pst);
506 OPENSSL_free(exp_buf);
510 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
511 size_t *out_len, const unsigned char *key, size_t key_len)
513 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
514 switch (gctx->cipher_nid)
516 case NID_id_Gost28147_89:
517 case NID_undef: /* FIXME */
518 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
520 case NID_kuznyechik_ctr:
522 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
525 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
531 * EVP_PKEY_METHOD callback decrypt
532 * Implementation of GOST2001/12 key transport, cryptopro variation
534 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
535 size_t *key_len, const unsigned char *in,
538 const unsigned char *p = in;
539 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
540 GOST_KEY_TRANSPORT *gkt = NULL;
542 unsigned char wrappedKey[44];
543 unsigned char sharedKey[32];
545 const struct gost_cipher_info *param = NULL;
546 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
547 int dgst_nid = NID_undef;
553 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
555 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
556 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
560 /* If key transport structure contains public key, use it */
561 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
563 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
564 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
565 GOST_R_INCOMPATIBLE_PEER_KEY);
569 /* Set control "public key from client certificate used" */
570 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
572 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
576 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
578 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
582 param = get_encryption_params(gkt->key_agreement_info->cipher);
587 gost_init(&ctx, param->sblock);
588 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
589 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
590 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
591 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
592 OPENSSL_assert(gkt->key_info->imit->length == 4);
593 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
595 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
596 if (dgst_nid == NID_id_GostR3411_2012_512)
597 dgst_nid = NID_id_GostR3411_2012_256;
599 if (!VKO_compute_key(sharedKey,
600 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
601 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
602 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
603 GOST_R_ERROR_COMPUTING_SHARED_KEY);
606 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
607 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
608 GOST_R_ERROR_COMPUTING_SHARED_KEY);
614 OPENSSL_cleanse(sharedKey, sizeof(sharedKey));
615 EVP_PKEY_free(eph_key);
616 GOST_KEY_TRANSPORT_free(gkt);
621 * EVP_PKEY_METHOD callback decrypt
622 * Implementation of GOST2018 key transport
624 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
625 size_t *key_len, const unsigned char *in,
628 const unsigned char *p = in;
629 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
630 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
631 PSKeyTransport_gost *pst = NULL;
633 unsigned char expkeys[64];
634 EVP_PKEY *eph_key = NULL;
635 int pkey_nid = EVP_PKEY_base_id(priv);
636 int mac_nid = NID_undef;
639 switch (data->cipher_nid) {
641 mac_nid = NID_magma_mac;
644 case NID_grasshopper_ctr:
645 mac_nid = NID_grasshopper_mac;
649 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
658 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
660 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
661 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
665 eph_key = X509_PUBKEY_get(pst->ephem_key);
668 1. Checks the next three conditions fulfilling and terminates the
669 connection with fatal error if not.
671 o Q_eph is on the same curve as server public key;
673 o Q_eph is not equal to zero point;
675 o q * Q_eph is not equal to zero point.
677 if (data->shared_ukm_size == 0 && pst->ukm != NULL) {
678 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
679 ASN1_STRING_length(pst->ukm), (void *)ASN1_STRING_get0_data(pst->ukm)) < 0) {
680 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_UKM_NOT_SET);
685 if (gost_keg(data->shared_ukm, pkey_nid,
686 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
687 EVP_PKEY_get0(priv), expkeys) <= 0) {
688 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
689 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
693 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
694 ASN1_STRING_length(pst->psexp), data->cipher_nid,
695 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
697 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
703 OPENSSL_cleanse(expkeys, sizeof(expkeys));
704 EVP_PKEY_free(eph_key);
705 PSKeyTransport_gost_free(pst);
709 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
710 size_t *key_len, const unsigned char *in, size_t in_len)
712 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
713 switch (gctx->cipher_nid)
715 case NID_id_Gost28147_89:
716 case NID_undef: /* FIXME */
717 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
718 case NID_kuznyechik_ctr:
720 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
722 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);
726 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */