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,
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;
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(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, const 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);
119 BUF_reverse(real_ukm, NULL, 16);
123 case NID_id_GostR3410_2012_512:
125 VKO_compute_key(keyout, pub_key, priv_key, real_ukm, 16,
126 NID_id_GostR3411_2012_512);
127 return (keylen) ? keylen : 0;
130 case NID_id_GostR3410_2012_256:
132 unsigned char tmpkey[32];
134 VKO_compute_key(tmpkey, pub_key, priv_key, real_ukm, 16,
135 NID_id_GostR3411_2012_256);
140 if (gost_kdftree2012_256
141 (keyout, 64, tmpkey, 32, (const unsigned char *)"kdf tree", 8,
142 ukm_source + 16, 8, 1) > 0)
147 OPENSSL_cleanse(tmpkey, 32);
148 return (keylen) ? keylen : 0;
157 * EVP_PKEY_METHOD callback derive.
158 * Implements VKO R 34.10-2001/2012 algorithms
160 int pkey_gost_ec_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
163 * Public key of peer in the ctx field peerkey
164 * Our private key in the ctx pkey
165 * ukm is in the algorithm specific context data
167 EVP_PKEY *my_key = EVP_PKEY_CTX_get0_pkey(ctx);
168 EVP_PKEY *peer_key = EVP_PKEY_CTX_get0_peerkey(ctx);
169 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
170 int dgst_nid = NID_undef;
172 if (!data || !data->shared_ukm) {
173 GOSTerr(GOST_F_PKEY_GOST_EC_DERIVE, GOST_R_UKM_NOT_SET);
177 * shared_ukm_size = 8 stands for pre-2018 cipher suites
178 * It means 32 bytes of key length, 8 byte UKM, 32-bytes hash
180 * shared_ukm_size = 32 stands for pre-2018 cipher suites
181 * It means 64 bytes of shared_key, 16 bytes of UKM and either
182 * 64 bytes of hash or 64 bytes of TLSTREE output
185 switch (data->shared_ukm_size) {
193 EVP_PKEY_get_default_digest_nid(my_key, &dgst_nid);
194 if (dgst_nid == NID_id_GostR3411_2012_512)
195 dgst_nid = NID_id_GostR3411_2012_256;
199 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
200 (EC_KEY *)EVP_PKEY_get0(my_key),
201 data->shared_ukm, 8, dgst_nid);
202 return (*keylen) ? 1 : 0;
212 *keylen = gost_keg(data->shared_ukm, EVP_PKEY_id(my_key),
213 EC_KEY_get0_public_key(EVP_PKEY_get0(peer_key)),
214 (EC_KEY *)EVP_PKEY_get0(my_key), key);
215 return (*keylen) ? 1 : 0;
225 * Generates ephemeral key based on pubk algorithm computes shared key using
226 * VKO and returns filled up GOST_KEY_TRANSPORT structure
230 * EVP_PKEY_METHOD callback encrypt
231 * Implementation of GOST2001/12 key transport, cryptopro variation
234 static int pkey_GOST_ECcp_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
235 size_t *out_len, const unsigned char *key,
238 GOST_KEY_TRANSPORT *gkt = NULL;
239 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
240 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
241 int pkey_nid = EVP_PKEY_base_id(pubk);
242 ASN1_OBJECT *crypt_params_obj = (pkey_nid == NID_id_GostR3410_2001) ?
243 OBJ_nid2obj(NID_id_Gost28147_89_CryptoPro_A_ParamSet) :
244 OBJ_nid2obj(NID_id_tc26_gost_28147_param_Z);
245 const struct gost_cipher_info *param =
246 get_encryption_params(crypt_params_obj);
247 unsigned char ukm[8], shared_key[32], crypted_key[44];
249 int key_is_ephemeral = 1;
251 EVP_PKEY *sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
252 if (data->shared_ukm) {
253 memcpy(ukm, data->shared_ukm, 8);
255 if (RAND_bytes(ukm, 8) <= 0) {
256 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_RNG_ERROR);
260 /* Check for private key in the peer_key of context */
262 key_is_ephemeral = 0;
263 if (!gost_get0_priv_key(sec_key)) {
264 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
265 GOST_R_NO_PRIVATE_PART_OF_NON_EPHEMERAL_KEYPAIR);
269 key_is_ephemeral = 1;
271 sec_key = EVP_PKEY_new();
272 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
273 || !EVP_PKEY_copy_parameters(sec_key, pubk)
274 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
275 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
276 GOST_R_ERROR_COMPUTING_SHARED_KEY);
281 if (!get_gost_engine_param(GOST_PARAM_CRYPT_PARAMS)
282 && param == gost_cipher_list) {
283 param = gost_cipher_list;
286 int dgst_nid = NID_undef;
287 EVP_PKEY_get_default_digest_nid(pubk, &dgst_nid);
288 if (dgst_nid == NID_id_GostR3411_2012_512)
289 dgst_nid = NID_id_GostR3411_2012_256;
291 if (!VKO_compute_key(shared_key,
292 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
293 EVP_PKEY_get0(sec_key), ukm, 8, dgst_nid)) {
294 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
295 GOST_R_ERROR_COMPUTING_SHARED_KEY);
298 gost_init(&cctx, param->sblock);
299 keyWrapCryptoPro(&cctx, shared_key, ukm, key, crypted_key);
301 gkt = GOST_KEY_TRANSPORT_new();
305 if (!ASN1_OCTET_STRING_set(gkt->key_agreement_info->eph_iv, ukm, 8)) {
308 if (!ASN1_OCTET_STRING_set(gkt->key_info->imit, crypted_key + 40, 4)) {
311 if (!ASN1_OCTET_STRING_set
312 (gkt->key_info->encrypted_key, crypted_key + 8, 32)) {
315 if (key_is_ephemeral) {
317 (&gkt->key_agreement_info->ephem_key, out ? sec_key : pubk)) {
318 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT,
319 GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
323 ASN1_OBJECT_free(gkt->key_agreement_info->cipher);
324 gkt->key_agreement_info->cipher = OBJ_nid2obj(param->nid);
325 if (key_is_ephemeral)
326 EVP_PKEY_free(sec_key);
327 if (!key_is_ephemeral) {
328 /* Set control "public key from client certificate used" */
329 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
331 GOSTerr(GOST_F_PKEY_GOST_ECCP_ENCRYPT, GOST_R_CTRL_CALL_FAILED);
335 if ((*out_len = i2d_GOST_KEY_TRANSPORT(gkt, out ? &out : NULL)) > 0)
337 GOST_KEY_TRANSPORT_free(gkt);
340 if (key_is_ephemeral)
341 EVP_PKEY_free(sec_key);
342 GOST_KEY_TRANSPORT_free(gkt);
347 * EVP_PKEY_METHOD callback decrypt
348 * Implementation of GOST2018 key transport
350 static int pkey_gost2018_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
351 size_t *out_len, const unsigned char *key,
354 PSKeyTransport_gost *pst = NULL;
355 EVP_PKEY *pubk = EVP_PKEY_CTX_get0_pkey(pctx);
356 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
357 int pkey_nid = EVP_PKEY_base_id(pubk);
358 unsigned char expkeys[64];
359 EVP_PKEY *sec_key = NULL;
361 int mac_nid = NID_undef;
363 int exp_len = 0, iv_len = 0;
364 unsigned char *exp_buf = NULL;
365 int key_is_ephemeral = 0;
367 switch (data->cipher_nid) {
369 mac_nid = NID_magma_mac;
373 case NID_grasshopper_ctr:
374 mac_nid = NID_grasshopper_mac;
379 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_INVALID_CIPHER);
383 exp_len = key_len + mac_len;
384 exp_buf = OPENSSL_malloc(exp_len);
386 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
390 sec_key = EVP_PKEY_CTX_get0_peerkey(pctx);
393 sec_key = EVP_PKEY_new();
394 if (sec_key == NULL) {
395 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE );
399 if (!EVP_PKEY_assign(sec_key, EVP_PKEY_base_id(pubk), EC_KEY_new())
400 || !EVP_PKEY_copy_parameters(sec_key, pubk)
401 || !gost_ec_keygen(EVP_PKEY_get0(sec_key))) {
402 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
403 GOST_R_ERROR_COMPUTING_SHARED_KEY);
406 key_is_ephemeral = 1;
409 if (gost_keg(data->shared_ukm, pkey_nid,
410 EC_KEY_get0_public_key(EVP_PKEY_get0(pubk)),
411 EVP_PKEY_get0(sec_key), expkeys) <= 0) {
412 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT,
413 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
417 if (gost_kexp15(key, key_len, data->cipher_nid, expkeys + 32,
418 mac_nid, expkeys + 0, data->shared_ukm + 24, iv_len,
419 exp_buf, &exp_len) <= 0) {
420 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
424 pst = PSKeyTransport_gost_new();
426 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
430 if (!ASN1_OCTET_STRING_set(pst->psexp, exp_buf, exp_len)) {
431 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, ERR_R_MALLOC_FAILURE);
435 if (!X509_PUBKEY_set(&pst->ephem_key, out ? sec_key : pubk)) {
436 GOSTerr(GOST_F_PKEY_GOST2018_ENCRYPT, GOST_R_CANNOT_PACK_EPHEMERAL_KEY);
440 if ((*out_len = i2d_PSKeyTransport_gost(pst, out ? &out : NULL)) > 0)
443 if (key_is_ephemeral)
444 EVP_PKEY_free(sec_key);
446 PSKeyTransport_gost_free(pst);
447 OPENSSL_free(exp_buf);
451 int pkey_gost_encrypt(EVP_PKEY_CTX *pctx, unsigned char *out,
452 size_t *out_len, const unsigned char *key, size_t key_len)
454 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
455 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
456 return pkey_GOST_ECcp_encrypt(pctx, out, out_len, key, key_len);
457 else if (data->shared_ukm_size == 32)
458 return pkey_gost2018_encrypt(pctx, out, out_len, key, key_len);
460 GOSTerr(GOST_F_PKEY_GOST_ENCRYPT, ERR_R_INTERNAL_ERROR);
466 * EVP_PKEY_METHOD callback decrypt
467 * Implementation of GOST2001/12 key transport, cryptopro variation
469 static int pkey_GOST_ECcp_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
470 size_t *key_len, const unsigned char *in,
473 const unsigned char *p = in;
474 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
475 GOST_KEY_TRANSPORT *gkt = NULL;
477 unsigned char wrappedKey[44];
478 unsigned char sharedKey[32];
480 const struct gost_cipher_info *param = NULL;
481 EVP_PKEY *eph_key = NULL, *peerkey = NULL;
482 int dgst_nid = NID_undef;
488 gkt = d2i_GOST_KEY_TRANSPORT(NULL, (const unsigned char **)&p, in_len);
490 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
491 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
495 /* If key transport structure contains public key, use it */
496 eph_key = X509_PUBKEY_get(gkt->key_agreement_info->ephem_key);
498 if (EVP_PKEY_derive_set_peer(pctx, eph_key) <= 0) {
499 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
500 GOST_R_INCOMPATIBLE_PEER_KEY);
504 /* Set control "public key from client certificate used" */
505 if (EVP_PKEY_CTX_ctrl(pctx, -1, -1, EVP_PKEY_CTRL_PEER_KEY, 3, NULL)
507 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_CTRL_CALL_FAILED);
511 peerkey = EVP_PKEY_CTX_get0_peerkey(pctx);
513 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT, GOST_R_NO_PEER_KEY);
517 param = get_encryption_params(gkt->key_agreement_info->cipher);
522 gost_init(&ctx, param->sblock);
523 OPENSSL_assert(gkt->key_agreement_info->eph_iv->length == 8);
524 memcpy(wrappedKey, gkt->key_agreement_info->eph_iv->data, 8);
525 OPENSSL_assert(gkt->key_info->encrypted_key->length == 32);
526 memcpy(wrappedKey + 8, gkt->key_info->encrypted_key->data, 32);
527 OPENSSL_assert(gkt->key_info->imit->length == 4);
528 memcpy(wrappedKey + 40, gkt->key_info->imit->data, 4);
530 EVP_PKEY_get_default_digest_nid(priv, &dgst_nid);
531 if (dgst_nid == NID_id_GostR3411_2012_512)
532 dgst_nid = NID_id_GostR3411_2012_256;
534 if (!VKO_compute_key(sharedKey,
535 EC_KEY_get0_public_key(EVP_PKEY_get0(peerkey)),
536 EVP_PKEY_get0(priv), wrappedKey, 8, dgst_nid)) {
537 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
538 GOST_R_ERROR_COMPUTING_SHARED_KEY);
541 if (!keyUnwrapCryptoPro(&ctx, sharedKey, wrappedKey, key)) {
542 GOSTerr(GOST_F_PKEY_GOST_ECCP_DECRYPT,
543 GOST_R_ERROR_COMPUTING_SHARED_KEY);
549 EVP_PKEY_free(eph_key);
550 GOST_KEY_TRANSPORT_free(gkt);
555 * EVP_PKEY_METHOD callback decrypt
556 * Implementation of GOST2018 key transport
558 static int pkey_gost2018_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
559 size_t *key_len, const unsigned char *in,
562 const unsigned char *p = in;
563 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
564 EVP_PKEY *priv = EVP_PKEY_CTX_get0_pkey(pctx);
565 PSKeyTransport_gost *pst = NULL;
567 unsigned char expkeys[64];
568 EVP_PKEY *eph_key = NULL;
569 int pkey_nid = EVP_PKEY_base_id(priv);
570 int mac_nid = NID_undef;
573 switch (data->cipher_nid) {
575 mac_nid = NID_magma_mac;
578 case NID_grasshopper_ctr:
579 mac_nid = NID_grasshopper_mac;
583 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_INVALID_CIPHER);
592 pst = d2i_PSKeyTransport_gost(NULL, (const unsigned char **)&p, in_len);
594 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
595 GOST_R_ERROR_PARSING_KEY_TRANSPORT_INFO);
599 eph_key = X509_PUBKEY_get(pst->ephem_key);
602 1. Checks the next three conditions fulfilling and terminates the
603 connection with fatal error if not.
605 o Q_eph is on the same curve as server public key;
607 o Q_eph is not equal to zero point;
609 o q * Q_eph is not equal to zero point.
611 if (gost_keg(data->shared_ukm, pkey_nid,
612 EC_KEY_get0_public_key(EVP_PKEY_get0(eph_key)),
613 EVP_PKEY_get0(priv), expkeys) <= 0) {
614 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT,
615 GOST_R_ERROR_COMPUTING_EXPORT_KEYS);
619 if (gost_kimp15(ASN1_STRING_get0_data(pst->psexp),
620 ASN1_STRING_length(pst->psexp), data->cipher_nid,
621 expkeys + 32, mac_nid, expkeys + 0, data->shared_ukm + 24,
623 GOSTerr(GOST_F_PKEY_GOST2018_DECRYPT, GOST_R_CANNOT_UNPACK_EPHEMERAL_KEY);
629 EVP_PKEY_free(eph_key);
630 PSKeyTransport_gost_free(pst);
634 int pkey_gost_decrypt(EVP_PKEY_CTX *pctx, unsigned char *key,
635 size_t *key_len, const unsigned char *in, size_t in_len)
637 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(pctx);
638 if (data->shared_ukm == NULL || data->shared_ukm_size == 8)
639 return pkey_GOST_ECcp_decrypt(pctx, key, key_len, in, in_len);
640 else if (data->shared_ukm_size == 32)
641 return pkey_gost2018_decrypt(pctx, key, key_len, in, in_len);
643 GOSTerr(GOST_F_PKEY_GOST_DECRYPT, ERR_R_INTERNAL_ERROR);