1 /**********************************************************************
3 * Copyright (c) 2005-2013 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
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>
14 #include <openssl/objects.h>
16 #include "e_gost_err.h"
17 #include "gost_keywrap.h"
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)
26 unsigned char *databuf = NULL;
27 BIGNUM *scalar = NULL, *order = NULL, *X = NULL, *Y = NULL;
28 const EC_GROUP *grp = NULL;
31 EVP_MD_CTX *mdctx = NULL;
32 const EVP_MD *md = NULL;
33 int buf_len, half_len;
36 if ((ctx = BN_CTX_secure_new()) == NULL) {
37 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
42 md = EVP_get_digestbynid(vko_dgst_nid);
44 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_INVALID_DIGEST_TYPE);
48 grp = EC_KEY_get0_group(priv_key);
49 scalar = BN_CTX_get(ctx);
50 order = BN_CTX_get(ctx);
53 if ((Y = BN_CTX_get(ctx)) == NULL
54 || (pnt = EC_POINT_new(grp)) == NULL
55 || BN_lebin2bn(ukm, ukm_size, scalar) == NULL
56 || !BN_mod_mul(scalar, scalar, EC_KEY_get0_private_key(priv_key),
57 EC_GROUP_get0_order(grp), ctx))
60 /* these two curves have cofactor 4; the rest have cofactor 1 */
61 switch (EC_GROUP_get_curve_name(grp)) {
62 case NID_id_tc26_gost_3410_2012_256_paramSetA:
63 case NID_id_tc26_gost_3410_2012_512_paramSetC:
64 if (!BN_lshift(scalar, scalar, 2))
69 if (!gost_ec_point_mul(grp, pnt, NULL, pub_key, scalar, ctx)) {
70 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
73 if (!EC_POINT_get_affine_coordinates(grp, pnt, X, Y, ctx) ||
74 !EC_GROUP_get_order(grp, order, ctx)) {
75 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EC_LIB);
79 half_len = BN_num_bytes(order);
80 buf_len = 2 * half_len;
81 if ((databuf = OPENSSL_malloc(buf_len)) == NULL) {
82 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
87 * Serialize elliptic curve point same way as we do it when saving key
89 if (BN_bn2lebinpad(X, databuf, half_len) != half_len
90 || BN_bn2lebinpad(Y, databuf + half_len, half_len) != half_len)
93 if ((mdctx = EVP_MD_CTX_new()) == NULL) {
94 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
97 EVP_MD_CTX_init(mdctx);
98 EVP_DigestInit_ex(mdctx, md, NULL);
99 EVP_DigestUpdate(mdctx, databuf, buf_len);
100 EVP_DigestFinal_ex(mdctx, shared_key, NULL);
101 ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
107 EVP_MD_CTX_free(mdctx);
108 OPENSSL_free(databuf);
114 * keyout expected to be 64 bytes
116 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
117 const EC_POINT *pub_key, const EC_KEY *priv_key,
118 unsigned char *keyout)
121 unsigned char real_ukm[16];
124 memset(real_ukm, 0, 16);
125 if (memcmp(ukm_source, real_ukm, 16) == 0)
128 memcpy(real_ukm, ukm_source, 16);
129 BUF_reverse(real_ukm, NULL, 16);
133 case NID_id_GostR3410_2012_512:
135 VKO_compute_key(keyout, pub_key, priv_key, real_ukm, 16,
136 NID_id_GostR3411_2012_512);
137 return (keylen) ? keylen : 0;
140 case NID_id_GostR3410_2012_256:
142 unsigned char tmpkey[32];
144 VKO_compute_key(tmpkey, pub_key, priv_key, real_ukm, 16,
145 NID_id_GostR3411_2012_256);
150 if (gost_kdftree2012_256
151 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
152 ukm_source + 16, 8, 1) > 0)
157 OPENSSL_cleanse(tmpkey, 32);
158 return (keylen) ? keylen : 0;
166 * EVP_PKEY_METHOD callback derive.
167 * Implements VKO R 34.10-2001/2012 algorithms
169 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
172 * Public key of peer in the ctx field peerkey
173 * Our private key in the ctx pkey
174 * ukm is in the algorithm specific context data
176 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
177 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
178 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
179 int dgst_nid = NID_undef;
181 if (!data || !data->shared_ukm) {
182 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
186 * shared_ukm_size = 8 stands for pre-2018 cipher suites
187 * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
189 * shared_ukm_size = 32 stands for pre-2018 cipher suites
190 * It means 64 bytes of shared_key, 16 bytes of UKM and either
191 * 64 bytes of hash or 64 bytes of TLSTREE output
194 switch (data->shared_ukm_size) {
202 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
203 if (dgst_nid == NID_id_GostR3411_2012_512)
204 dgst_nid = NID_id_GostR3411_2012_256;
208 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
209 (EC_KEY *)EVP_PKEY_get0(my_key),
210 data->shared_ukm, 8, dgst_nid);
211 return (*keylen) ? 1 : 0;
221 *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
222 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
223 (EC_KEY *)EVP_PKEY_get0(my_key), key);
224 return (*keylen) ? 1 : 0;
234 * Generates ephemeral key based on pubk algorithm computes shared key using
235 * VKO and returns filled up GOST_KEY_TRANSPORT structure
239 * EVP_PKEY_METHOD callback encrypt
240 * Implementation of GOST2001/12 key transport, cryptopro variation
243 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
244 size_t *out_len, const unsigned char *key,
247 GOST_KEY_TRANSPORT *gkt = NULL;
248 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
249 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
250 int pkey_nid = EVP_PKEY_base_id(pubk);
251 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
252 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
253 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
254 const struct gost_cipher_info *param =
255 get_encryption_params(crypt_params_obj);
256 unsigned char ukm[8], shared_key[32], crypted_key[44];
258 int key_is_ephemeral = 1;
260 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
261 if (data->shared_ukm) {
262 memcpy(ukm, data->shared_ukm, 8);
264 if (RAND_bytes(ukm, 8) <= 0) {
265 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
271 /* Check for private key in the peer_key of context */
273 key_is_ephemeral = 0;
274 if (!gost_get0_priv_key(sec_key)) {
275 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
276 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
280 key_is_ephemeral = 1;
282 sec_key = EVP_PKEY_new();
283 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
284 || !EVP_PKEY_copy_parameters(sec_key, pubk)
285 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
286 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
287 GOST_R_ERROR_COMPUTING_SHARED_KEY);
293 int dgst_nid = NID_undef;
294 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
295 if (dgst_nid == NID_id_GostR3411_2012_512)
296 dgst_nid = NID_id_GostR3411_2012_256;
298 if (!VKO_compute_key(shared_key,
299 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
300 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
301 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
302 GOST_R_ERROR_COMPUTING_SHARED_KEY);
305 gost_init(&cctx, param->sblock);
306 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
308 gkt = GOST_KEY_TRANSPORT_new();
312 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
315 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
318 if (!ASN1_OCTET_STRING_set
319 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
322 if (key_is_ephemeral) {
324 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
325 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
326 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
330 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
331 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
332 if (key_is_ephemeral)
333 EVP_PKEY_free(sec_key);
334 if (!key_is_ephemeral) {
335 /* Set control "public key from client certificate used" */
336 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
338 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
342 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
344 OPENSSL_cleanse(shared_key, sizeof(shared_key));
345 GOST_KEY_TRANSPORT_free(gkt);
348 OPENSSL_cleanse(shared_key, sizeof(shared_key));
349 if (key_is_ephemeral)
350 EVP_PKEY_free(sec_key);
351 GOST_KEY_TRANSPORT_free(gkt);
356 * EVP_PKEY_METHOD callback decrypt
357 * Implementation of GOST2018 key transport
359 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
360 size_t *out_len, const unsigned char *key,
363 PSKeyTransport_gost *pst = NULL;
364 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
365 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
366 int pkey_nid = EVP_PKEY_base_id(pubk);
367 unsigned char expkeys[64];
368 EVP_PKEY *sec_key = NULL;
370 int mac_nid = NID_undef;
372 int exp_len = 0, iv_len = 0;
373 unsigned char *exp_buf = NULL;
374 int key_is_ephemeral = 0;
376 switch (data->cipher_nid) {
378 mac_nid = NID_magma_mac;
382 case NID_grasshopper_ctr:
383 mac_nid = NID_grasshopper_mac;
388 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
392 exp_len = key_len + mac_len;
393 exp_buf = OPENSSL_malloc(exp_len);
395 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
399 sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
402 sec_key = EVP_PKEY_new();
403 if (sec_key == NULL) {
404 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
408 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
409 || !EVP_PKEY_copy_parameters(sec_key, pubk)
410 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
411 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
412 GOST_R_ERROR_COMPUTING_SHARED_KEY);
415 key_is_ephemeral = 1;
418 if (gost_keg(data->shared_ukm, pkey_nid,
419 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
420 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
421 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
422 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
426 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
427 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
428 exp_buf, &exp_len) <= 0) {
429 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
433 pst = PSKeyTransport_gost_new();
435 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
439 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
440 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
444 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
445 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
449 if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
452 OPENSSL_cleanse(expkeys, sizeof(expkeys));
453 if (key_is_ephemeral)
454 EVP_PKEY_free(sec_key);
456 PSKeyTransport_gost_free(pst);
457 OPENSSL_free(exp_buf);
461 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
462 size_t *out_len, const unsigned char *key, size_t key_len)
464 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
465 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
466 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
467 else if (data->shared_ukm_size == 32)
468 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
470 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
476 * EVP_PKEY_METHOD callback decrypt
477 * Implementation of GOST2001/12 key transport, cryptopro variation
479 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
480 size_t *key_len, const unsigned char *in,
483 const unsigned char *p = in;
484 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
485 GOST_KEY_TRANSPORT *gkt = NULL;
487 unsigned char wrappedKey[44];
488 unsigned char sharedKey[32];
490 const struct gost_cipher_info *param = NULL;
491 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
492 int dgst_nid = NID_undef;
498 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
500 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
501 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
505 /* If key transport structure contains public key, use it */
506 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
508 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
509 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
510 GOST_R_INCOMPATIBLE_PEER_KEY);
514 /* Set control "public key from client certificate used" */
515 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
517 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
521 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
523 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
527 param = get_encryption_params(gkt->key_agreement_info->cipher);
532 gost_init(&ctx, param->sblock);
533 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
534 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
535 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
536 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
537 OPENSSL_assert(gkt->key_info->imit->length == 4);
538 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
540 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
541 if (dgst_nid == NID_id_GostR3411_2012_512)
542 dgst_nid = NID_id_GostR3411_2012_256;
544 if (!VKO_compute_key(sharedKey,
545 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
546 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
547 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
548 GOST_R_ERROR_COMPUTING_SHARED_KEY);
551 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
552 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
553 GOST_R_ERROR_COMPUTING_SHARED_KEY);
559 OPENSSL_cleanse(sharedKey, sizeof(sharedKey));
560 EVP_PKEY_free(eph_key);
561 GOST_KEY_TRANSPORT_free(gkt);
566 * EVP_PKEY_METHOD callback decrypt
567 * Implementation of GOST2018 key transport
569 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
570 size_t *key_len, const unsigned char *in,
573 const unsigned char *p = in;
574 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
575 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
576 PSKeyTransport_gost *pst = NULL;
578 unsigned char expkeys[64];
579 EVP_PKEY *eph_key = NULL;
580 int pkey_nid = EVP_PKEY_base_id(priv);
581 int mac_nid = NID_undef;
584 switch (data->cipher_nid) {
586 mac_nid = NID_magma_mac;
589 case NID_grasshopper_ctr:
590 mac_nid = NID_grasshopper_mac;
594 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
603 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
605 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
606 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
610 eph_key = X509_PUBKEY_get(pst->ephem_key);
613 1. Checks the next three conditions fulfilling and terminates the
614 connection with fatal error if not.
616 o Q_eph is on the same curve as server public key;
618 o Q_eph is not equal to zero point;
620 o q * Q_eph is not equal to zero point.
622 if (gost_keg(data->shared_ukm, pkey_nid,
623 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
624 EVP_PKEY_get0(priv), expkeys) <= 0) {
625 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
626 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
630 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
631 ASN1_STRING_length(pst->psexp), data->cipher_nid,
632 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
634 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
640 OPENSSL_cleanse(expkeys, sizeof(expkeys));
641 EVP_PKEY_free(eph_key);
642 PSKeyTransport_gost_free(pst);
646 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
647 size_t *key_len, const unsigned char *in, size_t in_len)
649 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
650 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
651 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
652 else if (data->shared_ukm_size == 32)
653 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
655 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);