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_2012_256:
43 case NID_id_GostR3410_2012_512:
45 const EC_GROUP *group =
46 EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey));
48 data->sign_param_nid = EC_GROUP_get_curve_name(group);
58 EVP_PKEY_CTX_set_data(ctx, data);
62 /* Copies contents of gost_pmeth_data structure */
63 static int pkey_gost_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
65 struct gost_pmeth_data *dst_data, *src_data;
66 if (!pkey_gost_init(dst)) {
69 src_data = EVP_PKEY_CTX_get_data(src);
70 dst_data = EVP_PKEY_CTX_get_data(dst);
71 if (!src_data || !dst_data)
74 *dst_data = *src_data;
79 /* Frees up gost_pmeth_data structure */
80 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
82 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
88 /* --------------------- control functions ------------------------------*/
89 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
91 struct gost_pmeth_data *pctx =
92 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
97 case EVP_PKEY_CTRL_MD:
99 EVP_PKEY *key = EVP_PKEY_CTX_get0_pkey(ctx);
100 int pkey_nid = (key == NULL) ? NID_undef : EVP_PKEY_base_id(key);
102 OPENSSL_assert(p2 != NULL);
104 switch (EVP_MD_type((const EVP_MD *)p2)) {
105 case NID_id_GostR3411_94:
106 if (pkey_nid == NID_id_GostR3410_2001
107 || pkey_nid == NID_id_GostR3410_94) {
108 pctx->md = (EVP_MD *)p2;
113 case NID_id_GostR3411_2012_256:
114 if (pkey_nid == NID_id_GostR3410_2012_256) {
115 pctx->md = (EVP_MD *)p2;
120 case NID_id_GostR3411_2012_512:
121 if (pkey_nid == NID_id_GostR3410_2012_512) {
122 pctx->md = (EVP_MD *)p2;
128 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
132 case EVP_PKEY_CTRL_GET_MD:
133 *(const EVP_MD **)p2 = pctx->md;
136 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
137 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
138 case EVP_PKEY_CTRL_PKCS7_SIGN:
139 case EVP_PKEY_CTRL_DIGESTINIT:
140 #ifndef OPENSSL_NO_CMS
141 case EVP_PKEY_CTRL_CMS_ENCRYPT:
142 case EVP_PKEY_CTRL_CMS_DECRYPT:
143 case EVP_PKEY_CTRL_CMS_SIGN:
147 case EVP_PKEY_CTRL_GOST_PARAMSET:
148 pctx->sign_param_nid = (int)p1;
150 case EVP_PKEY_CTRL_SET_IV:
151 OPENSSL_assert(p2 != NULL);
152 memcpy(pctx->shared_ukm, p2, (int)p1);
153 pctx->shared_ukm_size = p1;
155 case EVP_PKEY_CTRL_CIPHER:
157 case NID_magma_ctr_acpkm:
158 case NID_magma_ctr_acpkm_omac:
160 pctx->cipher_nid = NID_magma_ctr;
162 case NID_kuznyechik_ctr_acpkm:
163 case NID_kuznyechik_ctr_acpkm_omac:
164 case NID_kuznyechik_ctr:
165 pctx->cipher_nid = NID_kuznyechik_ctr;
168 pctx->cipher_nid = p1;
172 case EVP_PKEY_CTRL_PEER_KEY:
173 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
175 if (p1 == 2) /* TLS: peer key used? */
176 return pctx->peer_key_used;
177 if (p1 == 3) /* TLS: peer key used! */
178 return (pctx->peer_key_used = 1);
182 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_CTRL_CALL_FAILED);
186 static int pkey_gost_ec_ctrl_str_common(EVP_PKEY_CTX *ctx,
187 const char *type, const char *value)
189 if (0 == strcmp(type, ukm_ctrl_string)) {
190 unsigned char ukm_buf[32], *tmp = NULL;
192 tmp = OPENSSL_hexstr2buf(value, &len);
198 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_COMMON, GOST_R_CTRL_CALL_FAILED);
201 memcpy(ukm_buf, tmp, len);
204 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_SET_IV, len, ukm_buf);
209 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
210 const char *type, const char *value)
212 if (strcmp(type, param_ctrl_string) == 0) {
218 if (strlen(value) == 1) {
219 switch (toupper((unsigned char)value[0])) {
221 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
224 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
227 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
230 param_nid = NID_id_GostR3410_2001_TestParamSet;
235 } else if ((strlen(value) == 2)
236 && (toupper((unsigned char)value[0]) == 'X')) {
237 switch (toupper((unsigned char)value[1])) {
239 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
242 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
247 } else if ((strlen(value) == 3)
248 && (toupper((unsigned char)value[0]) == 'T')
249 && (toupper((unsigned char)value[1]) == 'C')) {
250 switch (toupper((unsigned char)value[2])) {
252 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetA;
255 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetB;
258 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetC;
261 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetD;
267 R3410_ec_params *p = R3410_2001_paramset;
268 param_nid = OBJ_txt2nid(value);
269 if (param_nid == NID_undef) {
272 for (; p->nid != NID_undef; p++) {
273 if (p->nid == param_nid)
276 if (p->nid == NID_undef) {
277 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
278 GOST_R_INVALID_PARAMSET);
283 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
287 return pkey_gost_ec_ctrl_str_common(ctx, type, value);
290 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
291 const char *type, const char *value)
293 int param_nid = NID_undef;
295 if (strcmp(type, param_ctrl_string))
296 return pkey_gost_ec_ctrl_str_common(ctx, type, value);
301 if (strlen(value) == 1) {
302 switch (toupper((unsigned char)value[0])) {
304 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
308 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
312 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetC;
319 R3410_ec_params *p = R3410_2012_512_paramset;
320 param_nid = OBJ_txt2nid(value);
321 if (param_nid == NID_undef)
324 while (p->nid != NID_undef && p->nid != param_nid)
327 if (p->nid == NID_undef) {
328 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
329 GOST_R_INVALID_PARAMSET);
334 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
337 /* --------------------- key generation --------------------------------*/
339 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
344 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
346 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
349 if (!data || data->sign_param_nid == NID_undef) {
350 GOSTerr(GOST_F_PKEY_GOST2001_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
355 if (!fill_GOST_EC_params(ec, data->sign_param_nid)
356 || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
363 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
365 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
369 if (!data || data->sign_param_nid == NID_undef) {
370 GOSTerr(GOST_F_PKEY_GOST2012_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
375 if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
380 switch (data->sign_param_nid) {
381 case NID_id_tc26_gost_3410_2012_512_paramSetA:
382 case NID_id_tc26_gost_3410_2012_512_paramSetB:
383 case NID_id_tc26_gost_3410_2012_512_paramSetC:
384 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
386 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
389 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
390 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
391 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
392 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
393 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
394 case NID_id_GostR3410_2001_TestParamSet:
395 case NID_id_tc26_gost_3410_2012_256_paramSetA:
396 case NID_id_tc26_gost_3410_2012_256_paramSetB:
397 case NID_id_tc26_gost_3410_2012_256_paramSetC:
398 case NID_id_tc26_gost_3410_2012_256_paramSetD:
400 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
413 /* ----------- keygen callbacks --------------------------------------*/
414 /* Generates GOST_R3410 2001 key and assigns it using specified type */
415 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
418 if (!pkey_gost2001_paramgen(ctx, pkey))
420 ec = EVP_PKEY_get0(pkey);
425 /* Generates GOST_R3410 2012 key and assigns it using specified type */
426 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
428 if (!pkey_gost2012_paramgen(ctx, pkey))
431 gost_ec_keygen(EVP_PKEY_get0(pkey));
435 /* ----------- sign callbacks --------------------------------------*/
437 * Packs signature according to Cryptopro rules
438 * and frees up ECDSA_SIG structure
440 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
442 const BIGNUM *sig_r = NULL, *sig_s = NULL;
443 ECDSA_SIG_get0(s, &sig_r, &sig_s);
445 memset(sig, 0, *siglen);
446 store_bignum(sig_s, sig, order);
447 store_bignum(sig_r, sig + order, order);
452 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
453 size_t *siglen, const unsigned char *tbs,
456 ECDSA_SIG *unpacked_sig = NULL;
457 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
465 switch (EVP_PKEY_base_id(pkey)) {
466 case NID_id_GostR3410_2001:
467 case NID_id_GostR3410_2012_256:
470 case NID_id_GostR3410_2012_512:
481 unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
485 return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
488 /* ------------------- verify callbacks ---------------------------*/
489 /* Unpack signature according to cryptopro rules */
490 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
493 BIGNUM *r = NULL, *s = NULL;
495 sig = ECDSA_SIG_new();
497 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
500 s = BN_bin2bn(sigbuf, siglen / 2, NULL);
501 r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
502 ECDSA_SIG_set0(sig, r, s);
506 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
507 size_t siglen, const unsigned char *tbs,
511 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
512 ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
516 fprintf(stderr, "R=");
517 BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
518 fprintf(stderr, "\nS=");
519 BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
520 fprintf(stderr, "\n");
523 ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
528 /* ------------- encrypt init -------------------------------------*/
529 /* Generates ephermeral key */
530 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
535 /* --------------- Derive init ------------------------------------*/
536 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
541 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
542 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
544 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
545 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
549 memset(data, 0, sizeof(*data));
551 data->mac_param_nid = NID_undef;
554 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
556 data->mac_param_nid = key->mac_param_nid;
557 data->mac_size = key->mac_size;
561 EVP_PKEY_CTX_set_data(ctx, data);
565 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
567 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
568 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
572 memset(data, 0, sizeof(*data));
573 data->mac_size = mac_size;
574 data->mac_param_nid = NID_undef;
577 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
579 data->mac_param_nid = key->mac_param_nid;
580 data->mac_size = key->mac_size;
584 EVP_PKEY_CTX_set_data(ctx, data);
588 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
590 return pkey_gost_omac_init(ctx, 8);
593 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
595 return pkey_gost_omac_init(ctx, 16);
598 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
600 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
605 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
607 struct gost_mac_pmeth_data *dst_data, *src_data;
608 if (!pkey_gost_mac_init(dst)) {
611 src_data = EVP_PKEY_CTX_get_data(src);
612 dst_data = EVP_PKEY_CTX_get_data(dst);
613 if (!src_data || !dst_data)
616 *dst_data = *src_data;
620 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
622 struct gost_mac_pmeth_data *data =
623 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
626 case EVP_PKEY_CTRL_MD:
628 int nid = EVP_MD_type((const EVP_MD *)p2);
629 if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
630 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
631 GOST_R_INVALID_DIGEST_TYPE);
634 data->md = (EVP_MD *)p2;
638 case EVP_PKEY_CTRL_GET_MD:
639 *(const EVP_MD **)p2 = data->md;
642 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
643 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
644 case EVP_PKEY_CTRL_PKCS7_SIGN:
646 case EVP_PKEY_CTRL_SET_MAC_KEY:
648 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
652 memcpy(data->key, p2, 32);
655 case EVP_PKEY_CTRL_GOST_PARAMSET:
657 struct gost_cipher_info *param = p2;
658 data->mac_param_nid = param->nid;
661 case EVP_PKEY_CTRL_DIGESTINIT:
663 EVP_MD_CTX *mctx = p2;
664 if (!data->key_set) {
665 struct gost_mac_key *key;
666 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
668 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
669 GOST_R_MAC_KEY_NOT_SET);
672 key = EVP_PKEY_get0(pkey);
674 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
675 GOST_R_MAC_KEY_NOT_SET);
678 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
679 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
681 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
682 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
685 case EVP_PKEY_CTRL_MAC_LEN:
687 if (p1 < 1 || p1 > 8) {
689 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
699 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
700 const char *type, const char *value)
702 if (strcmp(type, key_ctrl_string) == 0) {
703 if (strlen(value) != 32) {
704 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
705 GOST_R_INVALID_MAC_KEY_LENGTH);
708 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
711 if (strcmp(type, hexkey_ctrl_string) == 0) {
714 unsigned char *keybuf = string_to_hex(value, &keylen);
715 if (!keybuf || keylen != 32) {
716 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
717 GOST_R_INVALID_MAC_KEY_LENGTH);
718 OPENSSL_free(keybuf);
721 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
722 OPENSSL_free(keybuf);
726 if (!strcmp(type, maclen_ctrl_string)) {
728 long size = strtol(value, &endptr, 10);
729 if (*endptr != '\0') {
730 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
733 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
735 if (strcmp(type, param_ctrl_string) == 0) {
736 ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
737 const struct gost_cipher_info *param = NULL;
739 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
743 param = get_encryption_params(obj);
744 ASN1_OBJECT_free(obj);
746 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
751 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
757 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
759 struct gost_mac_pmeth_data *data =
760 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
763 case EVP_PKEY_CTRL_MD:
765 int nid = EVP_MD_type((const EVP_MD *)p2);
766 if (nid != NID_magma_mac && nid != NID_grasshopper_mac
767 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
768 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
769 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
770 GOST_R_INVALID_DIGEST_TYPE);
773 data->md = (EVP_MD *)p2;
777 case EVP_PKEY_CTRL_GET_MD:
778 *(const EVP_MD **)p2 = data->md;
781 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
782 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
783 case EVP_PKEY_CTRL_PKCS7_SIGN:
785 case EVP_PKEY_CTRL_SET_MAC_KEY:
787 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
791 memcpy(data->key, p2, 32);
794 case EVP_PKEY_CTRL_DIGESTINIT:
796 EVP_MD_CTX *mctx = p2;
797 if (!data->key_set) {
798 struct gost_mac_key *key;
799 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
801 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
802 GOST_R_MAC_KEY_NOT_SET);
805 key = EVP_PKEY_get0(pkey);
807 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
808 GOST_R_MAC_KEY_NOT_SET);
811 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
812 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
814 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
815 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
818 case EVP_PKEY_CTRL_MAC_LEN:
820 if (p1 < 1 || p1 > max_size) {
822 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
832 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
834 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
837 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
839 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
842 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
843 const char *type, const char *value, size_t max_size)
845 if (strcmp(type, key_ctrl_string) == 0) {
846 if (strlen(value) != 32) {
847 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
848 GOST_R_INVALID_MAC_KEY_LENGTH);
851 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
854 if (strcmp(type, hexkey_ctrl_string) == 0) {
857 unsigned char *keybuf = string_to_hex(value, &keylen);
858 if (!keybuf || keylen != 32) {
859 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
860 GOST_R_INVALID_MAC_KEY_LENGTH);
861 OPENSSL_free(keybuf);
864 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
865 OPENSSL_free(keybuf);
869 if (!strcmp(type, maclen_ctrl_string)) {
871 long size = strtol(value, &endptr, 10);
872 if (*endptr != '\0') {
873 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
876 return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
881 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
882 const char *type, const char *value)
884 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
887 static int pkey_gost_grasshopper_mac_ctrl_str(EVP_PKEY_CTX *ctx,
888 const char *type, const char *value)
890 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
893 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
894 EVP_PKEY *pkey, int mac_nid)
896 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
897 struct gost_mac_key *keydata;
898 if (!data || !data->key_set) {
899 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
902 keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
905 memcpy(keydata->key, data->key, 32);
906 keydata->mac_param_nid = data->mac_param_nid;
907 keydata->mac_size = data->mac_size;
908 EVP_PKEY_assign(pkey, mac_nid, keydata);
912 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
914 return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
917 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
919 return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
922 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
924 return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
927 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
929 return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
932 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
934 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
937 pkey_gost_mac_init(ctx);
940 data = EVP_PKEY_CTX_get_data(ctx);
942 GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
949 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
951 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
954 pkey_gost_omac_init(ctx, 4);
957 data = EVP_PKEY_CTX_get_data(ctx);
959 GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
966 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
968 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
971 pkey_gost_omac_init(ctx, 8);
974 data = EVP_PKEY_CTX_get_data(ctx);
976 GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
983 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
984 size_t *siglen, EVP_MD_CTX *mctx)
986 unsigned int tmpsiglen;
988 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
992 tmpsiglen = *siglen; /* for platforms where sizeof(int) !=
996 *siglen = data->mac_size;
1000 EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
1001 (mctx, EVP_MD_CTRL_XOF_LEN, data->mac_size, NULL);
1002 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
1003 *siglen = data->mac_size;
1007 /* ----------- misc callbacks -------------------------------------*/
1009 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
1010 static int pkey_gost_check(EVP_PKEY *pkey)
1012 return EC_KEY_check_key(EVP_PKEY_get0(pkey));
1015 /* ----------------------------------------------------------------*/
1016 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
1018 *pmeth = EVP_PKEY_meth_new(id, flags);
1023 case NID_id_GostR3410_2001:
1024 EVP_PKEY_meth_set_ctrl(*pmeth,
1025 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1026 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1027 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1029 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
1031 EVP_PKEY_meth_set_encrypt(*pmeth,
1032 pkey_gost_encrypt_init,
1034 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1035 EVP_PKEY_meth_set_derive(*pmeth,
1036 pkey_gost_derive_init, pkey_gost_ec_derive);
1037 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1038 pkey_gost2001_paramgen);
1039 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1040 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1042 case NID_id_GostR3410_2012_256:
1043 EVP_PKEY_meth_set_ctrl(*pmeth,
1044 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1045 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1046 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1048 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1050 EVP_PKEY_meth_set_encrypt(*pmeth,
1051 pkey_gost_encrypt_init,
1053 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1054 EVP_PKEY_meth_set_derive(*pmeth,
1055 pkey_gost_derive_init, pkey_gost_ec_derive);
1056 EVP_PKEY_meth_set_paramgen(*pmeth,
1057 pkey_gost_paramgen_init,
1058 pkey_gost2012_paramgen);
1059 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1060 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1062 case NID_id_GostR3410_2012_512:
1063 EVP_PKEY_meth_set_ctrl(*pmeth,
1064 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1065 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1066 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1068 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1070 EVP_PKEY_meth_set_encrypt(*pmeth,
1071 pkey_gost_encrypt_init,
1073 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1074 EVP_PKEY_meth_set_derive(*pmeth,
1075 pkey_gost_derive_init, pkey_gost_ec_derive);
1076 EVP_PKEY_meth_set_paramgen(*pmeth,
1077 pkey_gost_paramgen_init,
1078 pkey_gost2012_paramgen);
1079 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1080 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1082 case NID_id_Gost28147_89_MAC:
1083 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1084 pkey_gost_mac_ctrl_str);
1085 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1086 pkey_gost_mac_signctx);
1087 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1088 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1089 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1090 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1092 case NID_gost_mac_12:
1093 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1094 pkey_gost_mac_ctrl_str);
1095 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1096 pkey_gost_mac_signctx);
1097 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1098 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1099 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1100 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1103 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1104 pkey_gost_magma_mac_ctrl_str);
1105 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1106 pkey_gost_mac_signctx);
1107 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1108 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1109 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1110 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1112 case NID_grasshopper_mac:
1113 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1114 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1115 pkey_gost_grasshopper_mac_ctrl_str);
1116 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1117 pkey_gost_mac_signctx);
1118 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1119 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_mac_init);
1120 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1121 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1123 default: /* Unsupported method */
1126 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1127 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1129 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1131 * FIXME derive etc...