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 static int VKO_compute_key(unsigned char *shared_key, size_t shared_key_size,
22 const EC_POINT *pub_key, EC_KEY *priv_key,
23 const unsigned char *ukm, size_t ukm_size,
26 unsigned char *databuf = NULL;
27 BIGNUM *UKM = NULL, *p = NULL, *order = NULL, *X = NULL, *Y = 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_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 = hashsum2bn(ukm, ukm_size);
50 order = BN_CTX_get(ctx);
53 EC_GROUP_get_order(EC_KEY_get0_group(priv_key), order, ctx);
54 BN_mod_mul(p, key, UKM, order, ctx);
55 if (!EC_POINT_mul(EC_KEY_get0_group(priv_key), pnt, NULL, pub_key, p, ctx)) {
56 GOSTerr(GOST_F_VKO_COMPUTE_KEY, GOST_R_ERROR_POINT_MUL);
59 EC_POINT_get_affine_coordinates_GFp(EC_KEY_get0_group(priv_key),
62 half_len = BN_num_bytes(order);
63 buf_len = 2 * half_len;
64 databuf = OPENSSL_zalloc(buf_len);
66 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
71 * Serialize elliptic curve point same way as we do it when saving key
73 store_bignum(Y, databuf, half_len);
74 store_bignum(X, databuf + half_len, half_len);
75 /* And reverse byte order of whole buffer */
76 BUF_reverse(databuf, NULL, buf_len);
78 mdctx = EVP_MD_CTX_new();
80 GOSTerr(GOST_F_VKO_COMPUTE_KEY, ERR_R_MALLOC_FAILURE);
83 EVP_MD_CTX_init(mdctx);
84 EVP_DigestInit_ex(mdctx, md, NULL);
85 EVP_DigestUpdate(mdctx, databuf, buf_len);
86 EVP_DigestFinal_ex(mdctx, shared_key, NULL);
87 ret = (EVP_MD_size(md) > 0) ? EVP_MD_size(md) : 0;
96 EVP_MD_CTX_free(mdctx);
98 OPENSSL_free(databuf);
104 * keyout expected to be 64 bytes
106 static int gost_keg(const unsigned char *ukm_source, int pkey_nid,
107 const EC_POINT *pub_key, EC_KEY *priv_key,
108 unsigned char *keyout)
111 unsigned char real_ukm[16];
114 memset(real_ukm, 0, 16);
115 if (memcmp(ukm_source, real_ukm, 16) == 0)
118 memcpy(real_ukm, ukm_source, 16);
121 case NID_id_GostR3410_2012_512:
123 VKO_compute_key(keyout, 64, pub_key, priv_key, real_ukm, 16,
124 NID_id_GostR3411_2012_512);
125 return (keylen) ? keylen : 0;
128 case NID_id_GostR3410_2012_256:
130 unsigned char tmpkey[32];
132 VKO_compute_key(tmpkey, 32, pub_key, priv_key, real_ukm, 16,
133 NID_id_GostR3411_2012_256);
138 if (gost_kdftree2012_256
139 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
140 ukm_source + 16, 8, 1) > 0)
145 OPENSSL_cleanse(tmpkey, 32);
146 return (keylen) ? keylen : 0;
155 * EVP_PKEY_METHOD callback derive.
156 * Implements VKO R 34.10-2001/2012 algorithms
158 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
161 * Public key of peer in the ctx field peerkey
162 * Our private key in the ctx pkey
163 * ukm is in the algorithm specific context data
165 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
166 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
167 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
168 int dgst_nid = NID_undef;
170 if (!data || !data->shared_ukm) {
171 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
175 * shared_ukm_size = 8 stands for pre-2018 cipher suites
176 * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
178 * shared_ukm_size = 32 stands for pre-2018 cipher suites
179 * It means 64 bytes of shared_key, 16 bytes of UKM and either
180 * 64 bytes of hash or 64 bytes of TLSTREE output
183 switch (data->shared_ukm_size) {
191 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
192 if (dgst_nid == NID_id_GostR3411_2012_512)
193 dgst_nid = NID_id_GostR3411_2012_256;
196 VKO_compute_key(key, 32,
197 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
198 (EC_KEY *)EVP_PKEY_get0(my_key),
199 data->shared_ukm, 8, dgst_nid);
200 return (*keylen) ? 1 : 0;
210 *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
211 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
212 (EC_KEY *)EVP_PKEY_get0(my_key), key);
213 return (*keylen) ? 1 : 0;
223 * Generates ephemeral key based on pubk algorithm computes shared key using
224 * VKO and returns filled up GOST_KEY_TRANSPORT structure
228 * EVP_PKEY_METHOD callback encrypt
229 * Implementation of GOST2001/12 key transport, cryptopro variation
232 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
233 size_t *out_len, const unsigned char *key,
236 GOST_KEY_TRANSPORT *gkt = NULL;
237 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
238 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
239 int pkey_nid = EVP_PKEY_base_id(pubk);
240 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
241 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
242 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
243 const struct gost_cipher_info *param =
244 get_encryption_params(crypt_params_obj);
245 unsigned char ukm[8], shared_key[32], crypted_key[44];
247 int key_is_ephemeral = 1;
249 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
250 if (data->shared_ukm) {
251 memcpy(ukm, data->shared_ukm, 8);
254 if (RAND_bytes(ukm, 8) <= 0) {
255 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
259 /* Check for private key in the peer_key of context */
261 key_is_ephemeral = 0;
262 if (!gost_get0_priv_key(sec_key)) {
263 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
264 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
268 key_is_ephemeral = 1;
270 sec_key = EVP_PKEY_new();
271 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
272 || !EVP_PKEY_copy_parameters(sec_key, pubk)
273 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
274 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
275 GOST_R_ERROR_COMPUTING_SHARED_KEY);
280 if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
281 && param == gost_cipher_list) {
282 param = gost_cipher_list;
285 int dgst_nid = NID_undef;
286 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
287 if (dgst_nid == NID_id_GostR3411_2012_512)
288 dgst_nid = NID_id_GostR3411_2012_256;
290 if (!VKO_compute_key(shared_key, 32,
291 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
292 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
293 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
294 GOST_R_ERROR_COMPUTING_SHARED_KEY);
297 gost_init(&cctx, param->sblock);
298 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
300 gkt = GOST_KEY_TRANSPORT_new();
304 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
307 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
310 if (!ASN1_OCTET_STRING_set
311 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
314 if (key_is_ephemeral) {
316 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
317 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
318 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
322 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
323 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
324 if (key_is_ephemeral)
325 EVP_PKEY_free(sec_key);
326 if (!key_is_ephemeral) {
327 /* Set control "public key from client certificate used" */
328 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
330 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
334 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
336 GOST_KEY_TRANSPORT_free(gkt);
339 if (key_is_ephemeral)
340 EVP_PKEY_free(sec_key);
341 GOST_KEY_TRANSPORT_free(gkt);
346 * EVP_PKEY_METHOD callback decrypt
347 * Implementation of GOST2018 key transport
349 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
350 size_t *out_len, const unsigned char *key,
353 PSKeyTransport_gost *pst = NULL;
354 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
355 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
356 int pkey_nid = EVP_PKEY_base_id(pubk);
357 unsigned char expkeys[64];
358 EVP_PKEY *sec_key = NULL;
360 int mac_nid = NID_undef;
362 int exp_len = 0, iv_len = 0;
363 unsigned char *exp_buf = NULL;
365 switch (data->cipher_nid) {
367 mac_nid = NID_magma_mac;
371 case NID_grasshopper_ctr:
372 mac_nid = NID_grasshopper_mac;
377 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
381 exp_len = key_len + mac_len;
382 exp_buf = OPENSSL_malloc(exp_len);
384 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
388 sec_key = EVP_PKEY_new();
389 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
390 || !EVP_PKEY_copy_parameters(sec_key, pubk)
391 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
392 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
393 GOST_R_ERROR_COMPUTING_SHARED_KEY);
397 if (gost_keg(data->shared_ukm, pkey_nid,
398 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
399 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
400 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
401 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
405 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
406 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
407 exp_buf, &exp_len) <= 0) {
408 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
412 pst = PSKeyTransport_gost_new();
414 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
418 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
419 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
423 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
424 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
428 EVP_PKEY_free(sec_key);
430 if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
433 PSKeyTransport_gost_free(pst);
434 OPENSSL_free(exp_buf);
438 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
439 size_t *out_len, const unsigned char *key, size_t key_len)
441 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
442 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
443 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
444 else if (data->shared_ukm_size == 32)
445 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
447 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
453 * EVP_PKEY_METHOD callback decrypt
454 * Implementation of GOST2001/12 key transport, cryptopro variation
456 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
457 size_t *key_len, const unsigned char *in,
460 const unsigned char *p = in;
461 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
462 GOST_KEY_TRANSPORT *gkt = NULL;
464 unsigned char wrappedKey[44];
465 unsigned char sharedKey[32];
467 const struct gost_cipher_info *param = NULL;
468 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
469 int dgst_nid = NID_undef;
475 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
477 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
478 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
482 /* If key transport structure contains public key, use it */
483 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
485 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
486 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
487 GOST_R_INCOMPATIBLE_PEER_KEY);
491 /* Set control "public key from client certificate used" */
492 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
494 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
498 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
500 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
504 param = get_encryption_params(gkt->key_agreement_info->cipher);
509 gost_init(&ctx, param->sblock);
510 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
511 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
512 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
513 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
514 OPENSSL_assert(gkt->key_info->imit->length == 4);
515 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
517 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
518 if (dgst_nid == NID_id_GostR3411_2012_512)
519 dgst_nid = NID_id_GostR3411_2012_256;
521 if (!VKO_compute_key(sharedKey, 32,
522 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
523 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
524 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
525 GOST_R_ERROR_COMPUTING_SHARED_KEY);
528 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
529 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
530 GOST_R_ERROR_COMPUTING_SHARED_KEY);
536 EVP_PKEY_free(eph_key);
537 GOST_KEY_TRANSPORT_free(gkt);
542 * EVP_PKEY_METHOD callback decrypt
543 * Implementation of GOST2018 key transport
545 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
546 size_t *key_len, const unsigned char *in,
549 const unsigned char *p = in;
550 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
551 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
552 PSKeyTransport_gost *pst = NULL;
554 unsigned char expkeys[64];
555 EVP_PKEY *eph_key = NULL;
556 int pkey_nid = EVP_PKEY_base_id(priv);
557 int mac_nid = NID_undef;
560 switch (data->cipher_nid) {
562 mac_nid = NID_magma_mac;
565 case NID_grasshopper_ctr:
566 mac_nid = NID_grasshopper_mac;
570 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
579 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
581 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
582 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
586 eph_key = X509_PUBKEY_get(pst->ephem_key);
589 1. Checks the next three conditions fulfilling and terminates the
590 connection with fatal error if not.
592 o Q_eph is on the same curve as server public key;
594 o Q_eph is not equal to zero point;
596 o q * Q_eph is not equal to zero point.
598 if (gost_keg(data->shared_ukm, pkey_nid,
599 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
600 EVP_PKEY_get0(priv), expkeys) <= 0) {
601 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
602 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
606 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
607 ASN1_STRING_length(pst->psexp), data->cipher_nid,
608 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
610 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
616 EVP_PKEY_free(eph_key);
617 PSKeyTransport_gost_free(pst);
621 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
622 size_t *key_len, const unsigned char *in, size_t in_len)
624 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
625 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
626 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
627 else if (data->shared_ukm_size == 32)
628 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
630 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);