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 *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;
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 UKM = BN_lebin2bn(ukm, ukm_size, NULL);
50 order = BN_CTX_get(ctx);
51 cofactor = 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);
62 if (!EC_POINT_get_affine_coordinates(EC_KEY_get0_group(priv_key),
64 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_EC_LIB);
68 half_len = BN_num_bytes(order);
69 buf_len = 2 * half_len;
70 databuf = OPENSSL_zalloc(buf_len);
72 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
77 * Serialize elliptic curve point same way as we do it when saving key
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);
84 mdctx = EVP_MD_CTX_new();
86 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
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;
102 EVP_MD_CTX_free(mdctx);
104 OPENSSL_free(databuf);
110 * keyout expected to be 64 bytes
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)
117 unsigned char real_ukm[16];
120 memset(real_ukm, 0, 16);
121 if (memcmp(ukm_source, real_ukm, 16) == 0)
124 memcpy(real_ukm, ukm_source, 16);
125 BUF_reverse(real_ukm, NULL, 16);
129 case NID_id_GostR3410_2012_512:
131 VKO_compute_key(keyout, pub_key, priv_key, real_ukm, 16,
132 NID_id_GostR3411_2012_512);
133 return (keylen) ? keylen : 0;
136 case NID_id_GostR3410_2012_256:
138 unsigned char tmpkey[32];
140 VKO_compute_key(tmpkey, pub_key, priv_key, real_ukm, 16,
141 NID_id_GostR3411_2012_256);
146 if (gost_kdftree2012_256
147 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
148 ukm_source + 16, 8, 1) > 0)
153 OPENSSL_cleanse(tmpkey, 32);
154 return (keylen) ? keylen : 0;
163 * EVP_PKEY_METHOD callback derive.
164 * Implements VKO R 34.10-2001/2012 algorithms
166 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
169 * Public key of peer in the ctx field peerkey
170 * Our private key in the ctx pkey
171 * ukm is in the algorithm specific context data
173 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
174 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
175 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
176 int dgst_nid = NID_undef;
178 if (!data || !data->shared_ukm) {
179 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
183 * shared_ukm_size = 8 stands for pre-2018 cipher suites
184 * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
186 * shared_ukm_size = 32 stands for pre-2018 cipher suites
187 * It means 64 bytes of shared_key, 16 bytes of UKM and either
188 * 64 bytes of hash or 64 bytes of TLSTREE output
191 switch (data->shared_ukm_size) {
199 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
200 if (dgst_nid == NID_id_GostR3411_2012_512)
201 dgst_nid = NID_id_GostR3411_2012_256;
205 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
206 (EC_KEY *)EVP_PKEY_get0(my_key),
207 data->shared_ukm, 8, dgst_nid);
208 return (*keylen) ? 1 : 0;
218 *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
219 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
220 (EC_KEY *)EVP_PKEY_get0(my_key), key);
221 return (*keylen) ? 1 : 0;
231 * Generates ephemeral key based on pubk algorithm computes shared key using
232 * VKO and returns filled up GOST_KEY_TRANSPORT structure
236 * EVP_PKEY_METHOD callback encrypt
237 * Implementation of GOST2001/12 key transport, cryptopro variation
240 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
241 size_t *out_len, const unsigned char *key,
244 GOST_KEY_TRANSPORT *gkt = NULL;
245 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
246 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
247 int pkey_nid = EVP_PKEY_base_id(pubk);
248 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
249 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
250 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
251 const struct gost_cipher_info *param =
252 get_encryption_params(crypt_params_obj);
253 unsigned char ukm[8], shared_key[32], crypted_key[44];
255 int key_is_ephemeral = 1;
257 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
258 if (data->shared_ukm) {
259 memcpy(ukm, data->shared_ukm, 8);
261 if (RAND_bytes(ukm, 8) <= 0) {
262 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
268 /* Check for private key in the peer_key of context */
270 key_is_ephemeral = 0;
271 if (!gost_get0_priv_key(sec_key)) {
272 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
273 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
277 key_is_ephemeral = 1;
279 sec_key = EVP_PKEY_new();
280 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
281 || !EVP_PKEY_copy_parameters(sec_key, pubk)
282 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
283 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
284 GOST_R_ERROR_COMPUTING_SHARED_KEY);
290 int dgst_nid = NID_undef;
291 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
292 if (dgst_nid == NID_id_GostR3411_2012_512)
293 dgst_nid = NID_id_GostR3411_2012_256;
295 if (!VKO_compute_key(shared_key,
296 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
297 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
298 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
299 GOST_R_ERROR_COMPUTING_SHARED_KEY);
302 gost_init(&cctx, param->sblock);
303 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
305 gkt = GOST_KEY_TRANSPORT_new();
309 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
312 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
315 if (!ASN1_OCTET_STRING_set
316 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
319 if (key_is_ephemeral) {
321 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
322 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
323 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
327 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
328 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
329 if (key_is_ephemeral)
330 EVP_PKEY_free(sec_key);
331 if (!key_is_ephemeral) {
332 /* Set control "public key from client certificate used" */
333 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
335 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
339 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
341 GOST_KEY_TRANSPORT_free(gkt);
344 if (key_is_ephemeral)
345 EVP_PKEY_free(sec_key);
346 GOST_KEY_TRANSPORT_free(gkt);
351 * EVP_PKEY_METHOD callback decrypt
352 * Implementation of GOST2018 key transport
354 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
355 size_t *out_len, const unsigned char *key,
358 PSKeyTransport_gost *pst = NULL;
359 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
360 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
361 int pkey_nid = EVP_PKEY_base_id(pubk);
362 unsigned char expkeys[64];
363 EVP_PKEY *sec_key = NULL;
365 int mac_nid = NID_undef;
367 int exp_len = 0, iv_len = 0;
368 unsigned char *exp_buf = NULL;
369 int key_is_ephemeral = 0;
371 switch (data->cipher_nid) {
373 mac_nid = NID_magma_mac;
377 case NID_grasshopper_ctr:
378 mac_nid = NID_grasshopper_mac;
383 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
387 exp_len = key_len + mac_len;
388 exp_buf = OPENSSL_malloc(exp_len);
390 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
394 sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
397 sec_key = EVP_PKEY_new();
398 if (sec_key == NULL) {
399 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
403 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
404 || !EVP_PKEY_copy_parameters(sec_key, pubk)
405 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
406 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
407 GOST_R_ERROR_COMPUTING_SHARED_KEY);
410 key_is_ephemeral = 1;
413 if (gost_keg(data->shared_ukm, pkey_nid,
414 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
415 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
416 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
417 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
421 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
422 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
423 exp_buf, &exp_len) <= 0) {
424 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
428 pst = PSKeyTransport_gost_new();
430 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
434 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
435 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
439 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
440 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
444 if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
447 if (key_is_ephemeral)
448 EVP_PKEY_free(sec_key);
450 PSKeyTransport_gost_free(pst);
451 OPENSSL_free(exp_buf);
455 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
456 size_t *out_len, const unsigned char *key, size_t key_len)
458 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
459 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
460 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
461 else if (data->shared_ukm_size == 32)
462 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
464 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
470 * EVP_PKEY_METHOD callback decrypt
471 * Implementation of GOST2001/12 key transport, cryptopro variation
473 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
474 size_t *key_len, const unsigned char *in,
477 const unsigned char *p = in;
478 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
479 GOST_KEY_TRANSPORT *gkt = NULL;
481 unsigned char wrappedKey[44];
482 unsigned char sharedKey[32];
484 const struct gost_cipher_info *param = NULL;
485 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
486 int dgst_nid = NID_undef;
492 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
494 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
495 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
499 /* If key transport structure contains public key, use it */
500 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
502 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
503 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
504 GOST_R_INCOMPATIBLE_PEER_KEY);
508 /* Set control "public key from client certificate used" */
509 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
511 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
515 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
517 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
521 param = get_encryption_params(gkt->key_agreement_info->cipher);
526 gost_init(&ctx, param->sblock);
527 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
528 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
529 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
530 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
531 OPENSSL_assert(gkt->key_info->imit->length == 4);
532 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
534 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
535 if (dgst_nid == NID_id_GostR3411_2012_512)
536 dgst_nid = NID_id_GostR3411_2012_256;
538 if (!VKO_compute_key(sharedKey,
539 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
540 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
541 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
542 GOST_R_ERROR_COMPUTING_SHARED_KEY);
545 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
546 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
547 GOST_R_ERROR_COMPUTING_SHARED_KEY);
553 EVP_PKEY_free(eph_key);
554 GOST_KEY_TRANSPORT_free(gkt);
559 * EVP_PKEY_METHOD callback decrypt
560 * Implementation of GOST2018 key transport
562 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
563 size_t *key_len, const unsigned char *in,
566 const unsigned char *p = in;
567 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
568 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
569 PSKeyTransport_gost *pst = NULL;
571 unsigned char expkeys[64];
572 EVP_PKEY *eph_key = NULL;
573 int pkey_nid = EVP_PKEY_base_id(priv);
574 int mac_nid = NID_undef;
577 switch (data->cipher_nid) {
579 mac_nid = NID_magma_mac;
582 case NID_grasshopper_ctr:
583 mac_nid = NID_grasshopper_mac;
587 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
596 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
598 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
599 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
603 eph_key = X509_PUBKEY_get(pst->ephem_key);
606 1. Checks the next three conditions fulfilling and terminates the
607 connection with fatal error if not.
609 o Q_eph is on the same curve as server public key;
611 o Q_eph is not equal to zero point;
613 o q * Q_eph is not equal to zero point.
615 if (gost_keg(data->shared_ukm, pkey_nid,
616 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
617 EVP_PKEY_get0(priv), expkeys) <= 0) {
618 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
619 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
623 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
624 ASN1_STRING_length(pst->psexp), data->cipher_nid,
625 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
627 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
633 EVP_PKEY_free(eph_key);
634 PSKeyTransport_gost_free(pst);
638 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
639 size_t *key_len, const unsigned char *in, size_t in_len)
641 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
642 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
643 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
644 else if (data->shared_ukm_size == 32)
645 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
647 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);