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;
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 grp = EC_KEY_get0_group(priv_key);
55 scalar = BN_CTX_get(ctx);
58 if ((Y = BN_CTX_get(ctx)) == NULL
59 || (pnt = EC_POINT_new(grp)) == NULL
60 || BN_lebin2bn(ukm, ukm_size, scalar) == NULL
61 || !BN_mod_mul(scalar, scalar, EC_KEY_get0_private_key(priv_key),
62 EC_GROUP_get0_order(grp), ctx))
67 * These two curves have cofactor 4; the rest have cofactor 1.
68 * But currently gost_ec_point_mul takes care of the cofactor clearing,
69 * hence this code is not needed.
71 switch (EC_GROUP_get_curve_name(grp)) {
72 case NID_id_tc26_gost_3410_2012_256_paramSetA:
73 case NID_id_tc26_gost_3410_2012_512_paramSetC:
74 if (!BN_lshift(scalar, scalar, 2))
80 if (!gost_ec_point_mul(grp, pnt, NULL, pub_key, scalar, ctx)) {
81 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
84 if (!EC_POINT_get_affine_coordinates(grp, pnt, X, Y, ctx)) {
85 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EC_LIB);
89 half_len = BN_num_bytes(EC_GROUP_get0_field(grp));
90 buf_len = 2 * half_len;
91 if ((databuf = OPENSSL_malloc(buf_len)) == NULL) {
92 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
97 * Serialize elliptic curve point same way as we do it when saving key
99 if (BN_bn2lebinpad(X, databuf, half_len) != half_len
100 || BN_bn2lebinpad(Y, databuf + half_len, half_len) != half_len)
103 if ((mdctx = EVP_MD_CTX_new()) == NULL) {
104 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
108 if (EVP_MD_CTX_init(mdctx) == 0
109 || EVP_DigestInit_ex(mdctx, md, NULL) == 0
110 || EVP_DigestUpdate(mdctx, databuf, buf_len) == 0
111 || EVP_DigestFinal_ex(mdctx, shared_key, NULL) == 0) {
112 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EVP_LIB);
116 ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
122 EVP_MD_CTX_free(mdctx);
123 OPENSSL_free(databuf);
129 * KEG Algorithm described in R 1323565.1.020-2018 6.4.5.1.
130 * keyout expected to be 64 bytes
132 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
133 const EC_POINT *pub_key, const EC_KEY *priv_key,
134 unsigned char *keyout)
137 unsigned char real_ukm[16];
140 memset(real_ukm, 0, 16);
141 if (memcmp(ukm_source, real_ukm, 16) == 0)
144 memcpy(real_ukm, ukm_source, 16);
145 BUF_reverse(real_ukm, NULL, 16);
149 case NID_id_GostR3410_2012_512:
151 VKO_compute_key(keyout, pub_key, priv_key, real_ukm, 16,
152 NID_id_GostR3411_2012_512);
153 return (keylen) ? keylen : 0;
156 case NID_id_GostR3410_2012_256:
158 unsigned char tmpkey[32];
160 VKO_compute_key(tmpkey, pub_key, priv_key, real_ukm, 16,
161 NID_id_GostR3411_2012_256);
166 if (gost_kdftree2012_256
167 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
168 ukm_source + 16, 8, 1) > 0)
173 OPENSSL_cleanse(tmpkey, 32);
174 return (keylen) ? keylen : 0;
182 * EVP_PKEY_METHOD callback derive.
183 * Implements VKO R 34.10-2001/2012 algorithms
186 * Backend for EVP_PKEY_derive()
187 * It have KEG mode (default) and VKO mode (enable by EVP_PKEY_CTRL_SET_VKO).
189 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
192 * Public key of peer in the ctx field peerkey
193 * Our private key in the ctx pkey
194 * ukm is in the algorithm specific context data
196 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
197 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
198 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
199 int dgst_nid = NID_undef;
201 if (!data || data->shared_ukm_size == 0) {
202 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
207 if (data->vko_dgst_nid) {
209 *keylen = data->vko_dgst_nid == NID_id_GostR3411_2012_256? 32 : 64;
212 *keylen = VKO_compute_key(key,
213 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
214 (EC_KEY *)EVP_PKEY_get0(my_key),
215 data->shared_ukm, data->shared_ukm_size,
217 return (*keylen) ? 1 : 0;
221 * shared_ukm_size = 8 stands for pre-2018 cipher suites
222 * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
224 * shared_ukm_size = 32 stands for post-2018 cipher suites
225 * It means 64 bytes of shared_key, 16 bytes of UKM and either
226 * 64 bytes of hash or 64 bytes of TLSTREE output
229 switch (data->shared_ukm_size) {
236 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
237 if (dgst_nid == NID_id_GostR3411_2012_512)
238 dgst_nid = NID_id_GostR3411_2012_256;
242 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
243 (EC_KEY *)EVP_PKEY_get0(my_key),
244 data->shared_ukm, 8, dgst_nid);
245 return (*keylen) ? 1 : 0;
255 *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
256 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
257 (EC_KEY *)EVP_PKEY_get0(my_key), key);
258 return (*keylen) ? 1 : 0;
268 * Generates ephemeral key based on pubk algorithm computes shared key using
269 * VKO and returns filled up GOST_KEY_TRANSPORT structure
273 * EVP_PKEY_METHOD callback encrypt
274 * Implementation of GOST2001/12 key transport, cryptopro variation
277 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
278 size_t *out_len, const unsigned char *key,
281 GOST_KEY_TRANSPORT *gkt = NULL;
282 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
283 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
284 int pkey_nid = EVP_PKEY_base_id(pubk);
285 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001 || pkey_nid == NID_id_GostR3410_2001DH) ?
286 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
287 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
288 const struct gost_cipher_info *param =
289 get_encryption_params(crypt_params_obj);
290 unsigned char ukm[8], shared_key[32], crypted_key[44];
292 int key_is_ephemeral = 1;
294 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
295 if (data->shared_ukm_size) {
296 memcpy(ukm, data->shared_ukm, 8);
298 if (RAND_bytes(ukm, 8) <= 0) {
299 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
305 /* Check for private key in the peer_key of context */
307 key_is_ephemeral = 0;
308 if (!gost_get0_priv_key(sec_key)) {
309 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
310 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
314 key_is_ephemeral = 1;
316 sec_key = EVP_PKEY_new();
317 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
318 || !EVP_PKEY_copy_parameters(sec_key, pubk)
319 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
320 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
321 GOST_R_ERROR_COMPUTING_SHARED_KEY);
327 int dgst_nid = NID_undef;
328 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
329 if (dgst_nid == NID_id_GostR3411_2012_512)
330 dgst_nid = NID_id_GostR3411_2012_256;
332 if (!VKO_compute_key(shared_key,
333 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
334 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
335 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
336 GOST_R_ERROR_COMPUTING_SHARED_KEY);
339 gost_init(&cctx, param->sblock);
340 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
342 gkt = GOST_KEY_TRANSPORT_new();
346 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
349 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
352 if (!ASN1_OCTET_STRING_set
353 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
356 if (key_is_ephemeral) {
358 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
359 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
360 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
364 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
365 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
366 if (key_is_ephemeral)
367 EVP_PKEY_free(sec_key);
368 if (!key_is_ephemeral) {
369 /* Set control "public key from client certificate used" */
370 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
372 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
376 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
378 OPENSSL_cleanse(shared_key, sizeof(shared_key));
379 GOST_KEY_TRANSPORT_free(gkt);
382 OPENSSL_cleanse(shared_key, sizeof(shared_key));
383 if (key_is_ephemeral)
384 EVP_PKEY_free(sec_key);
385 GOST_KEY_TRANSPORT_free(gkt);
390 * EVP_PKEY_METHOD callback decrypt
391 * Implementation of GOST2018 key transport
393 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
394 size_t *out_len, const unsigned char *key,
397 PSKeyTransport_gost *pst = NULL;
398 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
399 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
400 int pkey_nid = EVP_PKEY_base_id(pubk);
401 unsigned char expkeys[64];
402 EVP_PKEY *sec_key = NULL;
404 int mac_nid = NID_undef;
406 int exp_len = 0, iv_len = 0;
407 unsigned char *exp_buf = NULL;
408 int key_is_ephemeral = 0;
410 switch (data->cipher_nid) {
412 mac_nid = NID_magma_mac;
416 case NID_grasshopper_ctr:
417 mac_nid = NID_grasshopper_mac;
422 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
426 exp_len = key_len + mac_len;
427 exp_buf = OPENSSL_malloc(exp_len);
429 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
433 sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
436 sec_key = EVP_PKEY_new();
437 if (sec_key == NULL) {
438 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
442 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
443 || !EVP_PKEY_copy_parameters(sec_key, pubk)
444 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
445 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
446 GOST_R_ERROR_COMPUTING_SHARED_KEY);
449 key_is_ephemeral = 1;
452 if (data->shared_ukm_size == 0) {
453 if (RAND_bytes(data->shared_ukm, 32) <= 0) {
454 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_INTERNAL_ERROR);
457 data->shared_ukm_size = 32;
460 if (gost_keg(data->shared_ukm, pkey_nid,
461 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
462 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
463 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
464 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
468 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
469 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
470 exp_buf, &exp_len) <= 0) {
471 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
475 pst = PSKeyTransport_gost_new();
477 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
481 pst->ukm = ASN1_OCTET_STRING_new();
482 if (pst->ukm == NULL) {
483 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
487 if (!ASN1_OCTET_STRING_set(pst->ukm, data->shared_ukm, data->shared_ukm_size)) {
488 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
492 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
493 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
497 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
498 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
502 if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
505 OPENSSL_cleanse(expkeys, sizeof(expkeys));
506 if (key_is_ephemeral)
507 EVP_PKEY_free(sec_key);
509 PSKeyTransport_gost_free(pst);
510 OPENSSL_free(exp_buf);
514 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
515 size_t *out_len, const unsigned char *key, size_t key_len)
517 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
518 switch (gctx->cipher_nid)
520 case NID_id_Gost28147_89:
521 case NID_undef: /* FIXME */
522 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
524 case NID_kuznyechik_ctr:
526 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
529 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
535 * EVP_PKEY_METHOD callback decrypt
536 * Implementation of GOST2001/12 key transport, cryptopro variation
538 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
539 size_t *key_len, const unsigned char *in,
542 const unsigned char *p = in;
543 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
544 GOST_KEY_TRANSPORT *gkt = NULL;
546 unsigned char wrappedKey[44];
547 unsigned char sharedKey[32];
549 const struct gost_cipher_info *param = NULL;
550 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
551 int dgst_nid = NID_undef;
557 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
559 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
560 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
564 /* If key transport structure contains public key, use it */
565 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
567 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
568 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
569 GOST_R_INCOMPATIBLE_PEER_KEY);
573 /* Set control "public key from client certificate used" */
574 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
576 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
580 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
582 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
586 param = get_encryption_params(gkt->key_agreement_info->cipher);
591 gost_init(&ctx, param->sblock);
592 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
593 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
594 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
595 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
596 OPENSSL_assert(gkt->key_info->imit->length == 4);
597 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
599 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
600 if (dgst_nid == NID_id_GostR3411_2012_512)
601 dgst_nid = NID_id_GostR3411_2012_256;
603 if (!VKO_compute_key(sharedKey,
604 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
605 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
606 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
607 GOST_R_ERROR_COMPUTING_SHARED_KEY);
610 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
611 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
612 GOST_R_ERROR_COMPUTING_SHARED_KEY);
618 OPENSSL_cleanse(sharedKey, sizeof(sharedKey));
619 EVP_PKEY_free(eph_key);
620 GOST_KEY_TRANSPORT_free(gkt);
625 * EVP_PKEY_METHOD callback decrypt
626 * Implementation of GOST2018 key transport
628 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
629 size_t *key_len, const unsigned char *in,
632 const unsigned char *p = in;
633 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
634 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
635 PSKeyTransport_gost *pst = NULL;
637 unsigned char expkeys[64];
638 EVP_PKEY *eph_key = NULL;
639 int pkey_nid = EVP_PKEY_base_id(priv);
640 int mac_nid = NID_undef;
643 switch (data->cipher_nid) {
645 mac_nid = NID_magma_mac;
648 case NID_grasshopper_ctr:
649 mac_nid = NID_grasshopper_mac;
653 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
662 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
664 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
665 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
669 eph_key = X509_PUBKEY_get(pst->ephem_key);
672 1. Checks the next three conditions fulfilling and terminates the
673 connection with fatal error if not.
675 o Q_eph is on the same curve as server public key;
677 o Q_eph is not equal to zero point;
679 o q * Q_eph is not equal to zero point.
681 if (eph_key == NULL || priv == NULL || data == NULL) {
682 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
683 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
688 if (data->shared_ukm_size == 0 && pst->ukm != NULL) {
689 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
690 ASN1_STRING_length(pst->ukm), (void *)ASN1_STRING_get0_data(pst->ukm)) < 0) {
691 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_UKM_NOT_SET);
696 if (gost_keg(data->shared_ukm, pkey_nid,
697 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
698 EVP_PKEY_get0(priv), expkeys) <= 0) {
699 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
700 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
704 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
705 ASN1_STRING_length(pst->psexp), data->cipher_nid,
706 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
708 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
714 OPENSSL_cleanse(expkeys, sizeof(expkeys));
715 EVP_PKEY_free(eph_key);
716 PSKeyTransport_gost_free(pst);
720 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
721 size_t *key_len, const unsigned char *in, size_t in_len)
723 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
724 switch (gctx->cipher_nid)
726 case NID_id_Gost28147_89:
727 case NID_undef: /* FIXME */
728 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
729 case NID_kuznyechik_ctr:
731 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
733 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);
737 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */