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);
297 if (data->shared_ukm_size) {
298 memcpy(ukm, data->shared_ukm, 8);
300 if (RAND_bytes(ukm, 8) <= 0) {
301 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
307 /* Check for private key in the peer_key of context */
309 key_is_ephemeral = 0;
310 if (!gost_get0_priv_key(sec_key)) {
311 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
312 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
316 key_is_ephemeral = 1;
318 sec_key = EVP_PKEY_new();
319 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
320 || !EVP_PKEY_copy_parameters(sec_key, pubk)
321 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
322 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
323 GOST_R_ERROR_COMPUTING_SHARED_KEY);
329 int dgst_nid = NID_undef;
330 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
331 if (dgst_nid == NID_id_GostR3411_2012_512)
332 dgst_nid = NID_id_GostR3411_2012_256;
334 if (!VKO_compute_key(shared_key,
335 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
336 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
337 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
338 GOST_R_ERROR_COMPUTING_SHARED_KEY);
341 gost_init(&cctx, param->sblock);
342 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
344 gkt = GOST_KEY_TRANSPORT_new();
348 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
351 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
354 if (!ASN1_OCTET_STRING_set
355 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
358 if (key_is_ephemeral) {
360 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
361 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
362 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
366 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
367 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
368 if (key_is_ephemeral)
369 EVP_PKEY_free(sec_key);
370 if (!key_is_ephemeral) {
371 /* Set control "public key from client certificate used" */
372 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
374 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
378 res_len = i2d_GOST_KEY_TRANSPORT(gkt, NULL);
380 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, ERR_R_ASN1_LIB);
388 if ((size_t)res_len > *out_len) {
389 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_INVALID_BUFFER_SIZE);
392 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, &out)) > 0)
395 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, ERR_R_ASN1_LIB);
398 OPENSSL_cleanse(shared_key, sizeof(shared_key));
399 GOST_KEY_TRANSPORT_free(gkt);
402 OPENSSL_cleanse(shared_key, sizeof(shared_key));
403 if (key_is_ephemeral)
404 EVP_PKEY_free(sec_key);
405 GOST_KEY_TRANSPORT_free(gkt);
410 * EVP_PKEY_METHOD callback decrypt
411 * Implementation of GOST2018 key transport
413 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
414 size_t *out_len, const unsigned char *key,
417 PSKeyTransport_gost *pst = NULL;
418 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
419 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
420 int pkey_nid = EVP_PKEY_base_id(pubk);
421 unsigned char expkeys[64];
422 EVP_PKEY *sec_key = NULL;
424 int mac_nid = NID_undef;
426 int exp_len = 0, iv_len = 0;
427 unsigned char *exp_buf = NULL;
428 int key_is_ephemeral = 0;
431 switch (data->cipher_nid) {
433 mac_nid = NID_magma_mac;
437 case NID_grasshopper_ctr:
438 mac_nid = NID_grasshopper_mac;
443 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
447 exp_len = key_len + mac_len;
448 exp_buf = OPENSSL_malloc(exp_len);
450 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
454 sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
457 sec_key = EVP_PKEY_new();
458 if (sec_key == NULL) {
459 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
463 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
464 || !EVP_PKEY_copy_parameters(sec_key, pubk)
465 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
466 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
467 GOST_R_ERROR_COMPUTING_SHARED_KEY);
470 key_is_ephemeral = 1;
473 if (data->shared_ukm_size == 0) {
474 if (RAND_bytes(data->shared_ukm, 32) <= 0) {
475 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_INTERNAL_ERROR);
478 data->shared_ukm_size = 32;
481 if (gost_keg(data->shared_ukm, pkey_nid,
482 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
483 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
484 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
485 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
489 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
490 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
491 exp_buf, &exp_len) <= 0) {
492 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
496 pst = PSKeyTransport_gost_new();
498 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
502 pst->ukm = ASN1_OCTET_STRING_new();
503 if (pst->ukm == NULL) {
504 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
508 if (!ASN1_OCTET_STRING_set(pst->ukm, data->shared_ukm, data->shared_ukm_size)) {
509 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
513 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
514 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
518 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
519 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
523 res_len = i2d_PSKeyTransport_gost(pst, NULL);
525 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_ASN1_LIB);
533 if ((size_t)res_len > *out_len) {
534 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_BUFFER_SIZE);
537 if ((*out_len = i2d_PSKeyTransport_gost(pst, &out)) > 0)
540 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_ASN1_LIB);
544 OPENSSL_cleanse(expkeys, sizeof(expkeys));
545 if (key_is_ephemeral)
546 EVP_PKEY_free(sec_key);
548 PSKeyTransport_gost_free(pst);
549 OPENSSL_free(exp_buf);
553 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
554 size_t *out_len, const unsigned char *key, size_t key_len)
556 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
557 switch (gctx->cipher_nid)
559 case NID_id_Gost28147_89:
560 case NID_undef: /* FIXME */
561 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
563 case NID_kuznyechik_ctr:
565 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
568 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
574 * EVP_PKEY_METHOD callback decrypt
575 * Implementation of GOST2001/12 key transport, cryptopro variation
577 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
578 size_t *key_len, const unsigned char *in,
581 const unsigned char *p = in;
582 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
583 GOST_KEY_TRANSPORT *gkt = NULL;
585 unsigned char wrappedKey[44];
586 unsigned char sharedKey[32];
588 const struct gost_cipher_info *param = NULL;
589 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
590 int dgst_nid = NID_undef;
592 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
594 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
595 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
599 /* If key transport structure contains public key, use it */
600 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
602 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
603 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
604 GOST_R_INCOMPATIBLE_PEER_KEY);
608 /* Set control "public key from client certificate used" */
609 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
611 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
615 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
617 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
621 param = get_encryption_params(gkt->key_agreement_info->cipher);
626 gost_init(&ctx, param->sblock);
627 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
628 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
629 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
630 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
631 OPENSSL_assert(gkt->key_info->imit->length == 4);
632 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
634 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
635 if (dgst_nid == NID_id_GostR3411_2012_512)
636 dgst_nid = NID_id_GostR3411_2012_256;
638 if (!VKO_compute_key(sharedKey,
639 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
640 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
641 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
642 GOST_R_ERROR_COMPUTING_SHARED_KEY);
645 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
646 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
647 GOST_R_ERROR_COMPUTING_SHARED_KEY);
654 OPENSSL_cleanse(sharedKey, sizeof(sharedKey));
655 EVP_PKEY_free(eph_key);
656 GOST_KEY_TRANSPORT_free(gkt);
661 * EVP_PKEY_METHOD callback decrypt
662 * Implementation of GOST2018 key transport
664 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
665 size_t *key_len, const unsigned char *in,
668 const unsigned char *p = in;
669 struct gost_pmeth_data *data;
671 PSKeyTransport_gost *pst = NULL;
673 unsigned char expkeys[64];
674 EVP_PKEY *eph_key = NULL;
676 int mac_nid = NID_undef;
679 if (!(data = EVP_PKEY_CTX_get_data(pctx)) ||
680 !(priv = EVP_PKEY_CTX_get0_pkey(pctx))) {
681 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
685 pkey_nid = EVP_PKEY_base_id(priv);
687 switch (data->cipher_nid) {
689 mac_nid = NID_magma_mac;
692 case NID_grasshopper_ctr:
693 mac_nid = NID_grasshopper_mac;
697 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
702 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
704 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
705 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
709 eph_key = X509_PUBKEY_get(pst->ephem_key);
712 1. Checks the next three conditions fulfilling and terminates the
713 connection with fatal error if not.
715 o Q_eph is on the same curve as server public key;
717 o Q_eph is not equal to zero point;
719 o q * Q_eph is not equal to zero point.
721 if (eph_key == NULL) {
722 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
723 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
728 if (data->shared_ukm_size == 0 && pst->ukm != NULL) {
729 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_SET_IV,
730 ASN1_STRING_length(pst->ukm), (void *)ASN1_STRING_get0_data(pst->ukm)) < 0) {
731 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_UKM_NOT_SET);
736 if (gost_keg(data->shared_ukm, pkey_nid,
737 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
738 EVP_PKEY_get0(priv), expkeys) <= 0) {
739 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
740 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
744 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
745 ASN1_STRING_length(pst->psexp), data->cipher_nid,
746 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
748 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
755 OPENSSL_cleanse(expkeys, sizeof(expkeys));
756 EVP_PKEY_free(eph_key);
757 PSKeyTransport_gost_free(pst);
761 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
762 size_t *key_len, const unsigned char *in, size_t in_len)
764 struct gost_pmeth_data *gctx = EVP_PKEY_CTX_get_data(pctx);
771 if (key != NULL && *key_len < 32) {
772 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_BUFFER_SIZE);
776 switch (gctx->cipher_nid)
778 case NID_id_Gost28147_89:
779 case NID_undef: /* FIXME */
780 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
781 case NID_kuznyechik_ctr:
783 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
785 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);
789 /* vim: set expandtab cinoptions=\:0,l1,t0,g0,(0 sw=4 : */