1 /**********************************************************************
3 * Copyright (c) 2005-2013 Cryptocom LTD *
4 * This file is distributed under the same license as OpenSSL *
6 * Implementation of RFC 4357 (GOST R 34.10) Publick key method *
8 * Requires OpenSSL 1.0.0+ for compilation *
9 **********************************************************************/
10 #include <openssl/evp.h>
11 #include <openssl/objects.h>
12 #include <openssl/ec.h>
13 #include <openssl/err.h>
14 #include <openssl/x509v3.h> /* For string_to_hex */
15 #include <openssl/opensslv.h> /* For OPENSSL_VERSION_MAJOR */
20 #include "e_gost_err.h"
23 #ifdef OPENSSL_VERSION_MAJOR
25 #define ossl3_const const
28 /* -----init, cleanup, copy - uniform for all algs --------------*/
29 /* Allocates new gost_pmeth_data structure and assigns it as data */
30 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
32 struct gost_pmeth_data *data;
33 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
35 data = OPENSSL_malloc(sizeof(*data));
38 memset(data, 0, sizeof(*data));
39 if (pkey && EVP_PKEY_get0(pkey)) {
40 switch (EVP_PKEY_base_id(pkey)) {
41 case NID_id_GostR3410_2001:
42 case NID_id_GostR3410_2001DH:
43 case NID_id_GostR3410_2012_256:
44 case NID_id_GostR3410_2012_512:
46 const EC_GROUP *group =
47 EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey));
49 data->sign_param_nid = EC_GROUP_get_curve_name(group);
59 EVP_PKEY_CTX_set_data(ctx, data);
63 /* Copies contents of gost_pmeth_data structure */
64 static int pkey_gost_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
66 struct gost_pmeth_data *dst_data, *src_data;
67 if (!pkey_gost_init(dst)) {
70 src_data = EVP_PKEY_CTX_get_data(src);
71 dst_data = EVP_PKEY_CTX_get_data(dst);
72 if (!src_data || !dst_data)
75 *dst_data = *src_data;
80 /* Frees up gost_pmeth_data structure */
81 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
83 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
89 /* --------------------- control functions ------------------------------*/
90 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
92 struct gost_pmeth_data *pctx =
93 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
98 case EVP_PKEY_CTRL_MD:
100 EVP_PKEY *key = EVP_PKEY_CTX_get0_pkey(ctx);
101 int pkey_nid = (key == NULL) ? NID_undef : EVP_PKEY_base_id(key);
103 OPENSSL_assert(p2 != NULL);
105 switch (EVP_MD_type((const EVP_MD *)p2)) {
106 case NID_id_GostR3411_94:
107 if (pkey_nid == NID_id_GostR3410_2001
108 || pkey_nid == NID_id_GostR3410_2001DH
109 || pkey_nid == NID_id_GostR3410_94) {
110 pctx->md = (EVP_MD *)p2;
115 case NID_id_GostR3411_2012_256:
116 if (pkey_nid == NID_id_GostR3410_2012_256) {
117 pctx->md = (EVP_MD *)p2;
122 case NID_id_GostR3411_2012_512:
123 if (pkey_nid == NID_id_GostR3410_2012_512) {
124 pctx->md = (EVP_MD *)p2;
130 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
134 case EVP_PKEY_CTRL_GET_MD:
135 *(const EVP_MD **)p2 = pctx->md;
138 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
139 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
140 case EVP_PKEY_CTRL_PKCS7_SIGN:
141 case EVP_PKEY_CTRL_DIGESTINIT:
142 #ifndef OPENSSL_NO_CMS
143 case EVP_PKEY_CTRL_CMS_ENCRYPT:
144 case EVP_PKEY_CTRL_CMS_DECRYPT:
145 case EVP_PKEY_CTRL_CMS_SIGN:
149 case EVP_PKEY_CTRL_GOST_PARAMSET:
150 pctx->sign_param_nid = (int)p1;
152 case EVP_PKEY_CTRL_SET_IV:
153 if (p1 > sizeof(pctx->shared_ukm) || !p2) {
154 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_UKM_NOT_SET);
157 memcpy(pctx->shared_ukm, p2, (int)p1);
158 pctx->shared_ukm_size = p1;
160 case EVP_PKEY_CTRL_SET_VKO:
162 case 0: /* switch to KEG */
163 case NID_id_GostR3411_2012_256:
164 case NID_id_GostR3411_2012_512:
167 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
170 pctx->vko_dgst_nid = p1;
172 case EVP_PKEY_CTRL_CIPHER:
174 case NID_magma_ctr_acpkm:
175 case NID_magma_ctr_acpkm_omac:
177 pctx->cipher_nid = NID_magma_ctr;
179 case NID_kuznyechik_ctr_acpkm:
180 case NID_kuznyechik_ctr_acpkm_omac:
181 case NID_kuznyechik_ctr:
182 pctx->cipher_nid = NID_kuznyechik_ctr;
185 pctx->cipher_nid = p1;
189 case EVP_PKEY_CTRL_PEER_KEY:
190 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
192 if (p1 == 2) /* TLS: peer key used? */
193 return pctx->peer_key_used;
194 if (p1 == 3) /* TLS: peer key used! */
195 return (pctx->peer_key_used = 1);
199 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_CTRL_CALL_FAILED);
203 static int pkey_gost_ec_ctrl_str_common(EVP_PKEY_CTX *ctx,
204 const char *type, const char *value)
206 if (0 == strcmp(type, ukm_ctrl_string)) {
207 unsigned char ukm_buf[32], *tmp = NULL;
209 tmp = OPENSSL_hexstr2buf(value, &len);
215 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_COMMON, GOST_R_CTRL_CALL_FAILED);
218 memcpy(ukm_buf, tmp, len);
221 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_SET_IV, len, ukm_buf);
222 } else if (strcmp(type, vko_ctrl_string) == 0) {
223 int bits = atoi(value);
224 int vko_dgst_nid = 0;
227 vko_dgst_nid = NID_id_GostR3411_2012_256;
228 else if (bits == 512)
229 vko_dgst_nid = NID_id_GostR3411_2012_512;
230 else if (bits != 0) {
231 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_COMMON, GOST_R_INVALID_DIGEST_TYPE);
234 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_SET_VKO, vko_dgst_nid, NULL);
239 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
240 const char *type, const char *value)
242 if (strcmp(type, param_ctrl_string) == 0) {
248 if (strlen(value) == 1) {
249 switch (toupper((unsigned char)value[0])) {
251 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
254 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
257 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
260 param_nid = NID_id_GostR3410_2001_TestParamSet;
265 } else if ((strlen(value) == 2)
266 && (toupper((unsigned char)value[0]) == 'X')) {
267 switch (toupper((unsigned char)value[1])) {
269 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
272 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
277 } else if ((strlen(value) == 3)
278 && (toupper((unsigned char)value[0]) == 'T')
279 && (toupper((unsigned char)value[1]) == 'C')) {
280 switch (toupper((unsigned char)value[2])) {
282 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetA;
285 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetB;
288 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetC;
291 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetD;
297 R3410_ec_params *p = R3410_2001_paramset;
298 param_nid = OBJ_txt2nid(value);
299 if (param_nid == NID_undef) {
302 for (; p->nid != NID_undef; p++) {
303 if (p->nid == param_nid)
306 if (p->nid == NID_undef) {
307 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
308 GOST_R_INVALID_PARAMSET);
313 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
317 return pkey_gost_ec_ctrl_str_common(ctx, type, value);
320 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
321 const char *type, const char *value)
323 int param_nid = NID_undef;
325 if (strcmp(type, param_ctrl_string))
326 return pkey_gost_ec_ctrl_str_common(ctx, type, value);
331 if (strlen(value) == 1) {
332 switch (toupper((unsigned char)value[0])) {
334 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
338 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
342 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetC;
349 R3410_ec_params *p = R3410_2012_512_paramset;
350 param_nid = OBJ_txt2nid(value);
351 if (param_nid == NID_undef)
354 while (p->nid != NID_undef && p->nid != param_nid)
357 if (p->nid == NID_undef) {
358 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
359 GOST_R_INVALID_PARAMSET);
364 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
367 /* --------------------- key generation --------------------------------*/
369 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
374 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
376 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
379 if (!data || data->sign_param_nid == NID_undef) {
380 GOSTerr(GOST_F_PKEY_GOST2001_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
385 if (!fill_GOST_EC_params(ec, data->sign_param_nid)
386 || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
393 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
395 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
399 if (!data || data->sign_param_nid == NID_undef) {
400 GOSTerr(GOST_F_PKEY_GOST2012_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
405 if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
410 switch (data->sign_param_nid) {
411 case NID_id_tc26_gost_3410_2012_512_paramSetA:
412 case NID_id_tc26_gost_3410_2012_512_paramSetB:
413 case NID_id_tc26_gost_3410_2012_512_paramSetC:
414 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
416 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
419 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
420 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
421 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
422 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
423 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
424 case NID_id_GostR3410_2001_TestParamSet:
425 case NID_id_tc26_gost_3410_2012_256_paramSetA:
426 case NID_id_tc26_gost_3410_2012_256_paramSetB:
427 case NID_id_tc26_gost_3410_2012_256_paramSetC:
428 case NID_id_tc26_gost_3410_2012_256_paramSetD:
430 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
443 /* ----------- keygen callbacks --------------------------------------*/
444 /* Generates GOST_R3410 2001 key and assigns it using specified type */
445 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
448 if (!pkey_gost2001_paramgen(ctx, pkey))
450 ec = EVP_PKEY_get0(pkey);
455 /* Generates GOST_R3410 2012 key and assigns it using specified type */
456 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
458 if (!pkey_gost2012_paramgen(ctx, pkey))
461 gost_ec_keygen(EVP_PKEY_get0(pkey));
465 /* ----------- sign callbacks --------------------------------------*/
467 * Packs signature according to Cryptopro rules
468 * and frees up ECDSA_SIG structure
470 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
472 const BIGNUM *sig_r = NULL, *sig_s = NULL;
473 ECDSA_SIG_get0(s, &sig_r, &sig_s);
475 memset(sig, 0, *siglen);
476 store_bignum(sig_s, sig, order);
477 store_bignum(sig_r, sig + order, order);
482 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
483 size_t *siglen, const unsigned char *tbs,
486 ECDSA_SIG *unpacked_sig = NULL;
487 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
495 switch (EVP_PKEY_base_id(pkey)) {
496 case NID_id_GostR3410_2001:
497 case NID_id_GostR3410_2001DH:
498 case NID_id_GostR3410_2012_256:
501 case NID_id_GostR3410_2012_512:
512 unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
516 return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
519 /* ------------------- verify callbacks ---------------------------*/
520 /* Unpack signature according to cryptopro rules */
521 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
524 BIGNUM *r = NULL, *s = NULL;
526 sig = ECDSA_SIG_new();
528 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
531 s = BN_bin2bn(sigbuf, siglen / 2, NULL);
532 r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
533 ECDSA_SIG_set0(sig, r, s);
537 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
538 size_t siglen, const unsigned char *tbs,
542 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
543 ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
547 fprintf(stderr, "R=");
548 BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
549 fprintf(stderr, "\nS=");
550 BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
551 fprintf(stderr, "\n");
554 ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
559 /* ------------- encrypt init -------------------------------------*/
560 /* Generates ephermeral key */
561 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
566 /* --------------- Derive init ------------------------------------*/
567 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
572 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
573 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
575 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
576 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
580 memset(data, 0, sizeof(*data));
582 data->mac_param_nid = NID_undef;
585 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
587 data->mac_param_nid = key->mac_param_nid;
588 data->mac_size = key->mac_size;
592 EVP_PKEY_CTX_set_data(ctx, data);
596 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
598 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
599 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
603 memset(data, 0, sizeof(*data));
604 data->mac_size = mac_size;
605 data->mac_param_nid = NID_undef;
608 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
610 data->mac_param_nid = key->mac_param_nid;
611 data->mac_size = key->mac_size;
615 EVP_PKEY_CTX_set_data(ctx, data);
619 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
621 return pkey_gost_omac_init(ctx, 8);
624 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
626 return pkey_gost_omac_init(ctx, 16);
629 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
631 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
636 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
638 struct gost_mac_pmeth_data *dst_data, *src_data;
639 if (!pkey_gost_mac_init(dst)) {
642 src_data = EVP_PKEY_CTX_get_data(src);
643 dst_data = EVP_PKEY_CTX_get_data(dst);
644 if (!src_data || !dst_data)
647 *dst_data = *src_data;
651 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
653 struct gost_mac_pmeth_data *data =
654 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
657 case EVP_PKEY_CTRL_MD:
659 int nid = EVP_MD_type((const EVP_MD *)p2);
660 if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
661 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
662 GOST_R_INVALID_DIGEST_TYPE);
665 data->md = (EVP_MD *)p2;
669 case EVP_PKEY_CTRL_GET_MD:
670 *(const EVP_MD **)p2 = data->md;
673 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
674 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
675 case EVP_PKEY_CTRL_PKCS7_SIGN:
677 case EVP_PKEY_CTRL_SET_MAC_KEY:
679 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
683 memcpy(data->key, p2, 32);
686 case EVP_PKEY_CTRL_GOST_PARAMSET:
688 struct gost_cipher_info *param = p2;
689 data->mac_param_nid = param->nid;
692 case EVP_PKEY_CTRL_DIGESTINIT:
694 EVP_MD_CTX *mctx = p2;
695 if (!data->key_set) {
696 struct gost_mac_key *key;
697 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
699 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
700 GOST_R_MAC_KEY_NOT_SET);
703 key = EVP_PKEY_get0(pkey);
705 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
706 GOST_R_MAC_KEY_NOT_SET);
709 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
710 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
712 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
713 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
716 case EVP_PKEY_CTRL_MAC_LEN:
718 if (p1 < 1 || p1 > 8) {
720 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
730 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
731 const char *type, const char *value)
733 if (strcmp(type, key_ctrl_string) == 0) {
734 if (strlen(value) != 32) {
735 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
736 GOST_R_INVALID_MAC_KEY_LENGTH);
739 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
742 if (strcmp(type, hexkey_ctrl_string) == 0) {
745 unsigned char *keybuf = string_to_hex(value, &keylen);
746 if (!keybuf || keylen != 32) {
747 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
748 GOST_R_INVALID_MAC_KEY_LENGTH);
749 OPENSSL_free(keybuf);
752 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
753 OPENSSL_free(keybuf);
757 if (!strcmp(type, maclen_ctrl_string)) {
759 long size = strtol(value, &endptr, 10);
760 if (*endptr != '\0') {
761 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
764 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
766 if (strcmp(type, param_ctrl_string) == 0) {
767 ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
768 const struct gost_cipher_info *param = NULL;
770 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
774 param = get_encryption_params(obj);
775 ASN1_OBJECT_free(obj);
777 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
782 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
788 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
790 struct gost_mac_pmeth_data *data =
791 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
794 case EVP_PKEY_CTRL_MD:
796 int nid = EVP_MD_type((const EVP_MD *)p2);
797 if (nid != NID_magma_mac && nid != NID_grasshopper_mac
798 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
799 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
800 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
801 GOST_R_INVALID_DIGEST_TYPE);
804 data->md = (EVP_MD *)p2;
808 case EVP_PKEY_CTRL_GET_MD:
809 *(const EVP_MD **)p2 = data->md;
812 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
813 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
814 case EVP_PKEY_CTRL_PKCS7_SIGN:
816 case EVP_PKEY_CTRL_SET_MAC_KEY:
818 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
822 memcpy(data->key, p2, 32);
825 case EVP_PKEY_CTRL_DIGESTINIT:
827 EVP_MD_CTX *mctx = p2;
828 if (!data->key_set) {
829 struct gost_mac_key *key;
830 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
832 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
833 GOST_R_MAC_KEY_NOT_SET);
836 key = EVP_PKEY_get0(pkey);
838 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
839 GOST_R_MAC_KEY_NOT_SET);
842 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
843 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
845 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
846 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
849 case EVP_PKEY_CTRL_MAC_LEN:
851 if (p1 < 1 || p1 > max_size) {
853 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
863 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
865 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
868 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
870 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
873 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
874 const char *type, const char *value, size_t max_size)
876 if (strcmp(type, key_ctrl_string) == 0) {
877 if (strlen(value) != 32) {
878 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
879 GOST_R_INVALID_MAC_KEY_LENGTH);
882 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
885 if (strcmp(type, hexkey_ctrl_string) == 0) {
888 unsigned char *keybuf = string_to_hex(value, &keylen);
889 if (!keybuf || keylen != 32) {
890 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
891 GOST_R_INVALID_MAC_KEY_LENGTH);
892 OPENSSL_free(keybuf);
895 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
896 OPENSSL_free(keybuf);
900 if (!strcmp(type, maclen_ctrl_string)) {
902 long size = strtol(value, &endptr, 10);
903 if (*endptr != '\0') {
904 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
907 return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
912 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
913 const char *type, const char *value)
915 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
918 static int pkey_gost_grasshopper_mac_ctrl_str(EVP_PKEY_CTX *ctx,
919 const char *type, const char *value)
921 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
924 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
925 EVP_PKEY *pkey, int mac_nid)
927 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
928 struct gost_mac_key *keydata;
929 if (!data || !data->key_set) {
930 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
933 keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
936 memcpy(keydata->key, data->key, 32);
937 keydata->mac_param_nid = data->mac_param_nid;
938 keydata->mac_size = data->mac_size;
939 EVP_PKEY_assign(pkey, mac_nid, keydata);
943 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
945 return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
948 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
950 return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
953 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
955 return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
958 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
960 return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
963 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
965 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
968 pkey_gost_mac_init(ctx);
971 data = EVP_PKEY_CTX_get_data(ctx);
973 GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
980 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
982 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
985 pkey_gost_omac_init(ctx, 4);
988 data = EVP_PKEY_CTX_get_data(ctx);
990 GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
997 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
999 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
1002 pkey_gost_omac_init(ctx, 8);
1005 data = EVP_PKEY_CTX_get_data(ctx);
1007 GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
1014 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
1015 size_t *siglen, EVP_MD_CTX *mctx)
1017 unsigned int tmpsiglen;
1019 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
1023 tmpsiglen = *siglen; /* for platforms where sizeof(int) !=
1027 *siglen = data->mac_size;
1031 EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
1032 (mctx, EVP_MD_CTRL_XOF_LEN, data->mac_size, NULL);
1033 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
1034 *siglen = data->mac_size;
1038 /* ----------- misc callbacks -------------------------------------*/
1040 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
1041 static int pkey_gost_check(EVP_PKEY *pkey)
1043 return EC_KEY_check_key(EVP_PKEY_get0(pkey));
1046 /* ----------------------------------------------------------------*/
1047 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
1049 *pmeth = EVP_PKEY_meth_new(id, flags);
1054 case NID_id_GostR3410_2001:
1055 case NID_id_GostR3410_2001DH:
1056 EVP_PKEY_meth_set_ctrl(*pmeth,
1057 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1058 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1059 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1061 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
1063 EVP_PKEY_meth_set_encrypt(*pmeth,
1064 pkey_gost_encrypt_init,
1066 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1067 EVP_PKEY_meth_set_derive(*pmeth,
1068 pkey_gost_derive_init, pkey_gost_ec_derive);
1069 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1070 pkey_gost2001_paramgen);
1071 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1072 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1074 case NID_id_GostR3410_2012_256:
1075 EVP_PKEY_meth_set_ctrl(*pmeth,
1076 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1077 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1078 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1080 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1082 EVP_PKEY_meth_set_encrypt(*pmeth,
1083 pkey_gost_encrypt_init,
1085 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1086 EVP_PKEY_meth_set_derive(*pmeth,
1087 pkey_gost_derive_init, pkey_gost_ec_derive);
1088 EVP_PKEY_meth_set_paramgen(*pmeth,
1089 pkey_gost_paramgen_init,
1090 pkey_gost2012_paramgen);
1091 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1092 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1094 case NID_id_GostR3410_2012_512:
1095 EVP_PKEY_meth_set_ctrl(*pmeth,
1096 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1097 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1098 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1100 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1102 EVP_PKEY_meth_set_encrypt(*pmeth,
1103 pkey_gost_encrypt_init,
1105 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1106 EVP_PKEY_meth_set_derive(*pmeth,
1107 pkey_gost_derive_init, pkey_gost_ec_derive);
1108 EVP_PKEY_meth_set_paramgen(*pmeth,
1109 pkey_gost_paramgen_init,
1110 pkey_gost2012_paramgen);
1111 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1112 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1114 case NID_id_Gost28147_89_MAC:
1115 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1116 pkey_gost_mac_ctrl_str);
1117 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1118 pkey_gost_mac_signctx);
1119 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1120 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1121 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1122 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1124 case NID_gost_mac_12:
1125 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1126 pkey_gost_mac_ctrl_str);
1127 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1128 pkey_gost_mac_signctx);
1129 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1130 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1131 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1132 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1135 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1136 pkey_gost_magma_mac_ctrl_str);
1137 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1138 pkey_gost_mac_signctx);
1139 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1140 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1141 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1142 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1144 case NID_grasshopper_mac:
1145 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1146 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1147 pkey_gost_grasshopper_mac_ctrl_str);
1148 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1149 pkey_gost_mac_signctx);
1150 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1151 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_mac_init);
1152 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1153 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1155 default: /* Unsupported method */
1158 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1159 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1161 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1163 * FIXME derive etc...