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;
75 if (src_data->shared_ukm) {
76 dst_data->shared_ukm = NULL;
81 /* Frees up gost_pmeth_data structure */
82 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
84 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
87 OPENSSL_free(data->shared_ukm);
91 /* --------------------- control functions ------------------------------*/
92 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
94 struct gost_pmeth_data *pctx =
95 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
100 case EVP_PKEY_CTRL_MD:
102 EVP_PKEY *key = EVP_PKEY_CTX_get0_pkey(ctx);
103 int pkey_nid = (key == NULL) ? NID_undef : EVP_PKEY_base_id(key);
105 OPENSSL_assert(p2 != NULL);
107 switch (EVP_MD_type((const EVP_MD *)p2)) {
108 case NID_id_GostR3411_94:
109 if (pkey_nid == NID_id_GostR3410_2001
110 || pkey_nid == NID_id_GostR3410_94) {
111 pctx->md = (EVP_MD *)p2;
116 case NID_id_GostR3411_2012_256:
117 if (pkey_nid == NID_id_GostR3410_2012_256) {
118 pctx->md = (EVP_MD *)p2;
123 case NID_id_GostR3411_2012_512:
124 if (pkey_nid == NID_id_GostR3410_2012_512) {
125 pctx->md = (EVP_MD *)p2;
131 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
135 case EVP_PKEY_CTRL_GET_MD:
136 *(const EVP_MD **)p2 = pctx->md;
139 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
140 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
141 case EVP_PKEY_CTRL_PKCS7_SIGN:
142 case EVP_PKEY_CTRL_DIGESTINIT:
143 #ifndef OPENSSL_NO_CMS
144 case EVP_PKEY_CTRL_CMS_ENCRYPT:
145 case EVP_PKEY_CTRL_CMS_DECRYPT:
146 case EVP_PKEY_CTRL_CMS_SIGN:
150 case EVP_PKEY_CTRL_GOST_PARAMSET:
151 pctx->sign_param_nid = (int)p1;
153 case EVP_PKEY_CTRL_SET_IV:
154 OPENSSL_assert(p2 != NULL);
155 pctx->shared_ukm = OPENSSL_malloc((int)p1);
156 if (pctx->shared_ukm == NULL) {
157 GOSTerr(GOST_F_PKEY_GOST_CTRL, ERR_R_MALLOC_FAILURE);
160 memcpy(pctx->shared_ukm, p2, (int)p1);
161 pctx->shared_ukm_size = p1;
163 case EVP_PKEY_CTRL_CIPHER:
164 pctx->cipher_nid = p1;
166 case EVP_PKEY_CTRL_PEER_KEY:
167 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
169 if (p1 == 2) /* TLS: peer key used? */
170 return pctx->peer_key_used;
171 if (p1 == 3) /* TLS: peer key used! */
172 return (pctx->peer_key_used = 1);
176 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_CTRL_CALL_FAILED);
180 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
181 const char *type, const char *value)
185 if (strcmp(type, param_ctrl_string) == 0) {
189 if (strlen(value) == 1) {
190 switch (toupper((unsigned char)value[0])) {
192 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
195 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
198 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
201 param_nid = NID_id_GostR3410_2001_TestParamSet;
206 } else if ((strlen(value) == 2)
207 && (toupper((unsigned char)value[0]) == 'X')) {
208 switch (toupper((unsigned char)value[1])) {
210 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
213 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
218 } else if ((strlen(value) == 3)
219 && (toupper((unsigned char)value[0]) == 'T')
220 && (toupper((unsigned char)value[1]) == 'C')) {
221 switch (toupper((unsigned char)value[2])) {
223 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetA;
226 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetB;
229 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetC;
232 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetD;
238 R3410_ec_params *p = R3410_2001_paramset;
239 param_nid = OBJ_txt2nid(value);
240 if (param_nid == NID_undef) {
243 for (; p->nid != NID_undef; p++) {
244 if (p->nid == param_nid)
247 if (p->nid == NID_undef) {
248 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
249 GOST_R_INVALID_PARAMSET);
254 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
260 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
261 const char *type, const char *value)
263 int param_nid = NID_undef;
265 if (strcmp(type, param_ctrl_string))
271 if (strlen(value) == 1) {
272 switch (toupper((unsigned char)value[0])) {
274 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
278 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
282 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetC;
289 R3410_ec_params *p = R3410_2012_512_paramset;
290 param_nid = OBJ_txt2nid(value);
291 if (param_nid == NID_undef)
294 while (p->nid != NID_undef && p->nid != param_nid)
297 if (p->nid == NID_undef) {
298 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
299 GOST_R_INVALID_PARAMSET);
304 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
307 /* --------------------- key generation --------------------------------*/
309 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
314 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
316 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
319 if (!data || data->sign_param_nid == NID_undef) {
320 GOSTerr(GOST_F_PKEY_GOST2001_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
325 if (!fill_GOST_EC_params(ec, data->sign_param_nid)
326 || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
333 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
335 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
339 if (!data || data->sign_param_nid == NID_undef) {
340 GOSTerr(GOST_F_PKEY_GOST2012_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
345 if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
350 switch (data->sign_param_nid) {
351 case NID_id_tc26_gost_3410_2012_512_paramSetA:
352 case NID_id_tc26_gost_3410_2012_512_paramSetB:
353 case NID_id_tc26_gost_3410_2012_512_paramSetC:
355 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
358 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
359 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
360 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
361 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
362 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
363 case NID_id_GostR3410_2001_TestParamSet:
364 case NID_id_tc26_gost_3410_2012_256_paramSetA:
365 case NID_id_tc26_gost_3410_2012_256_paramSetB:
366 case NID_id_tc26_gost_3410_2012_256_paramSetC:
367 case NID_id_tc26_gost_3410_2012_256_paramSetD:
369 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
382 /* ----------- keygen callbacks --------------------------------------*/
383 /* Generates GOST_R3410 2001 key and assigns it using specified type */
384 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
387 if (!pkey_gost2001_paramgen(ctx, pkey))
389 ec = EVP_PKEY_get0(pkey);
394 /* Generates GOST_R3410 2012 key and assigns it using specified type */
395 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
397 if (!pkey_gost2012_paramgen(ctx, pkey))
400 gost_ec_keygen(EVP_PKEY_get0(pkey));
404 /* ----------- sign callbacks --------------------------------------*/
406 * Packs signature according to Cryptopro rules
407 * and frees up ECDSA_SIG structure
409 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
411 const BIGNUM *sig_r = NULL, *sig_s = NULL;
412 ECDSA_SIG_get0(s, &sig_r, &sig_s);
414 memset(sig, 0, *siglen);
415 store_bignum(sig_s, sig, order);
416 store_bignum(sig_r, sig + order, order);
421 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
422 size_t *siglen, const unsigned char *tbs,
425 ECDSA_SIG *unpacked_sig = NULL;
426 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
434 switch (EVP_PKEY_base_id(pkey)) {
435 case NID_id_GostR3410_2001:
436 case NID_id_GostR3410_2012_256:
439 case NID_id_GostR3410_2012_512:
450 unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
454 return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
457 /* ------------------- verify callbacks ---------------------------*/
458 /* Unpack signature according to cryptopro rules */
459 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
462 BIGNUM *r = NULL, *s = NULL;
464 sig = ECDSA_SIG_new();
466 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
469 s = BN_bin2bn(sigbuf, siglen / 2, NULL);
470 r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
471 ECDSA_SIG_set0(sig, r, s);
475 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
476 size_t siglen, const unsigned char *tbs,
480 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
481 ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
485 fprintf(stderr, "R=");
486 BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
487 fprintf(stderr, "\nS=");
488 BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
489 fprintf(stderr, "\n");
492 ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
497 /* ------------- encrypt init -------------------------------------*/
498 /* Generates ephermeral key */
499 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
504 /* --------------- Derive init ------------------------------------*/
505 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
510 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
511 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
513 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
514 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
518 memset(data, 0, sizeof(*data));
520 data->mac_param_nid = NID_undef;
523 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
525 data->mac_param_nid = key->mac_param_nid;
526 data->mac_size = key->mac_size;
530 EVP_PKEY_CTX_set_data(ctx, data);
534 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
536 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
537 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
541 memset(data, 0, sizeof(*data));
542 data->mac_size = mac_size;
543 data->mac_param_nid = NID_undef;
546 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
548 data->mac_param_nid = key->mac_param_nid;
549 data->mac_size = key->mac_size;
553 EVP_PKEY_CTX_set_data(ctx, data);
557 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
559 return pkey_gost_omac_init(ctx, 8);
562 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
564 return pkey_gost_omac_init(ctx, 16);
567 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
569 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
574 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
576 struct gost_mac_pmeth_data *dst_data, *src_data;
577 if (!pkey_gost_mac_init(dst)) {
580 src_data = EVP_PKEY_CTX_get_data(src);
581 dst_data = EVP_PKEY_CTX_get_data(dst);
582 if (!src_data || !dst_data)
585 *dst_data = *src_data;
589 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
591 struct gost_mac_pmeth_data *data =
592 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
595 case EVP_PKEY_CTRL_MD:
597 int nid = EVP_MD_type((const EVP_MD *)p2);
598 if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
599 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
600 GOST_R_INVALID_DIGEST_TYPE);
603 data->md = (EVP_MD *)p2;
607 case EVP_PKEY_CTRL_GET_MD:
608 *(const EVP_MD **)p2 = data->md;
611 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
612 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
613 case EVP_PKEY_CTRL_PKCS7_SIGN:
615 case EVP_PKEY_CTRL_SET_MAC_KEY:
617 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
621 memcpy(data->key, p2, 32);
624 case EVP_PKEY_CTRL_GOST_PARAMSET:
626 struct gost_cipher_info *param = p2;
627 data->mac_param_nid = param->nid;
630 case EVP_PKEY_CTRL_DIGESTINIT:
632 EVP_MD_CTX *mctx = p2;
633 struct gost_mac_key *key;
634 if (!data->key_set) {
635 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
637 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
638 GOST_R_MAC_KEY_NOT_SET);
641 key = EVP_PKEY_get0(pkey);
643 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
644 GOST_R_MAC_KEY_NOT_SET);
647 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
648 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
650 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
651 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
654 case EVP_PKEY_CTRL_MAC_LEN:
656 if (p1 < 1 || p1 > 8) {
658 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
668 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
669 const char *type, const char *value)
671 if (strcmp(type, key_ctrl_string) == 0) {
672 if (strlen(value) != 32) {
673 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
674 GOST_R_INVALID_MAC_KEY_LENGTH);
677 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
680 if (strcmp(type, hexkey_ctrl_string) == 0) {
683 unsigned char *keybuf = string_to_hex(value, &keylen);
684 if (!keybuf || keylen != 32) {
685 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
686 GOST_R_INVALID_MAC_KEY_LENGTH);
687 OPENSSL_free(keybuf);
690 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
691 OPENSSL_free(keybuf);
695 if (!strcmp(type, maclen_ctrl_string)) {
697 long size = strtol(value, &endptr, 10);
698 if (*endptr != '\0') {
699 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
702 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
704 if (strcmp(type, param_ctrl_string) == 0) {
705 ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
706 const struct gost_cipher_info *param = NULL;
708 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
712 param = get_encryption_params(obj);
713 ASN1_OBJECT_free(obj);
715 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
720 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
726 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
728 struct gost_mac_pmeth_data *data =
729 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
732 case EVP_PKEY_CTRL_MD:
734 int nid = EVP_MD_type((const EVP_MD *)p2);
735 if (nid != NID_magma_mac && nid != NID_grasshopper_mac
736 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
737 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
738 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
739 GOST_R_INVALID_DIGEST_TYPE);
742 data->md = (EVP_MD *)p2;
746 case EVP_PKEY_CTRL_GET_MD:
747 *(const EVP_MD **)p2 = data->md;
750 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
751 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
752 case EVP_PKEY_CTRL_PKCS7_SIGN:
754 case EVP_PKEY_CTRL_SET_MAC_KEY:
756 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
760 memcpy(data->key, p2, 32);
763 case EVP_PKEY_CTRL_DIGESTINIT:
765 EVP_MD_CTX *mctx = p2;
766 struct gost_mac_key *key;
767 if (!data->key_set) {
768 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
770 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
771 GOST_R_MAC_KEY_NOT_SET);
774 key = EVP_PKEY_get0(pkey);
776 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
777 GOST_R_MAC_KEY_NOT_SET);
780 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
781 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
783 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
784 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
787 case EVP_PKEY_CTRL_MAC_LEN:
789 if (p1 < 1 || p1 > max_size) {
791 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
801 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
803 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
806 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
808 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
811 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
812 const char *type, const char *value, size_t max_size)
814 if (strcmp(type, key_ctrl_string) == 0) {
815 if (strlen(value) != 32) {
816 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
817 GOST_R_INVALID_MAC_KEY_LENGTH);
820 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
823 if (strcmp(type, hexkey_ctrl_string) == 0) {
826 unsigned char *keybuf = string_to_hex(value, &keylen);
827 if (!keybuf || keylen != 32) {
828 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
829 GOST_R_INVALID_MAC_KEY_LENGTH);
830 OPENSSL_free(keybuf);
833 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
834 OPENSSL_free(keybuf);
838 if (!strcmp(type, maclen_ctrl_string)) {
840 long size = strtol(value, &endptr, 10);
841 if (*endptr != '\0') {
842 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
845 return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
850 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
851 const char *type, const char *value)
853 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
856 static int pkey_gost_grasshopper_mac_ctrl_str(EVP_PKEY_CTX *ctx,
857 const char *type, const char *value)
859 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
862 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
863 EVP_PKEY *pkey, int mac_nid)
865 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
866 struct gost_mac_key *keydata;
867 if (!data || !data->key_set) {
868 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
871 keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
874 memcpy(keydata->key, data->key, 32);
875 keydata->mac_param_nid = data->mac_param_nid;
876 keydata->mac_size = data->mac_size;
877 EVP_PKEY_assign(pkey, mac_nid, keydata);
881 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
883 return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
886 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
888 return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
891 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
893 return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
896 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
898 return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
901 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
903 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
906 pkey_gost_mac_init(ctx);
909 data = EVP_PKEY_CTX_get_data(ctx);
911 GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
918 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
920 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
923 pkey_gost_omac_init(ctx, 4);
926 data = EVP_PKEY_CTX_get_data(ctx);
928 GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
935 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
937 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
940 pkey_gost_omac_init(ctx, 8);
943 data = EVP_PKEY_CTX_get_data(ctx);
945 GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
952 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
953 size_t *siglen, EVP_MD_CTX *mctx)
955 unsigned int tmpsiglen;
957 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
961 tmpsiglen = *siglen; /* for platforms where sizeof(int) !=
965 *siglen = data->mac_size;
969 EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
970 (mctx, EVP_MD_CTRL_MAC_LEN, data->mac_size, NULL);
971 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
972 *siglen = data->mac_size;
976 /* ----------- misc callbacks -------------------------------------*/
978 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
979 static int pkey_gost_check(EVP_PKEY *pkey)
981 return EC_KEY_check_key(EVP_PKEY_get0(pkey));
984 /* ----------------------------------------------------------------*/
985 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
987 *pmeth = EVP_PKEY_meth_new(id, flags);
992 case NID_id_GostR3410_2001:
993 EVP_PKEY_meth_set_ctrl(*pmeth,
994 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
995 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
996 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
998 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
1000 EVP_PKEY_meth_set_encrypt(*pmeth,
1001 pkey_gost_encrypt_init,
1003 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1004 EVP_PKEY_meth_set_derive(*pmeth,
1005 pkey_gost_derive_init, pkey_gost_ec_derive);
1006 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1007 pkey_gost2001_paramgen);
1008 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1009 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1011 case NID_id_GostR3410_2012_256:
1012 EVP_PKEY_meth_set_ctrl(*pmeth,
1013 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1014 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1015 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1017 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1019 EVP_PKEY_meth_set_encrypt(*pmeth,
1020 pkey_gost_encrypt_init,
1022 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1023 EVP_PKEY_meth_set_derive(*pmeth,
1024 pkey_gost_derive_init, pkey_gost_ec_derive);
1025 EVP_PKEY_meth_set_paramgen(*pmeth,
1026 pkey_gost_paramgen_init,
1027 pkey_gost2012_paramgen);
1028 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1029 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1031 case NID_id_GostR3410_2012_512:
1032 EVP_PKEY_meth_set_ctrl(*pmeth,
1033 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1034 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1035 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1037 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1039 EVP_PKEY_meth_set_encrypt(*pmeth,
1040 pkey_gost_encrypt_init,
1042 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1043 EVP_PKEY_meth_set_derive(*pmeth,
1044 pkey_gost_derive_init, pkey_gost_ec_derive);
1045 EVP_PKEY_meth_set_paramgen(*pmeth,
1046 pkey_gost_paramgen_init,
1047 pkey_gost2012_paramgen);
1048 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1049 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1051 case NID_id_Gost28147_89_MAC:
1052 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1053 pkey_gost_mac_ctrl_str);
1054 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1055 pkey_gost_mac_signctx);
1056 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1057 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1058 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1059 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1061 case NID_gost_mac_12:
1062 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1063 pkey_gost_mac_ctrl_str);
1064 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1065 pkey_gost_mac_signctx);
1066 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1067 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1068 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1069 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1072 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1073 pkey_gost_magma_mac_ctrl_str);
1074 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1075 pkey_gost_mac_signctx);
1076 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1077 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1078 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1079 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1081 case NID_grasshopper_mac:
1082 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1083 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1084 pkey_gost_grasshopper_mac_ctrl_str);
1085 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1086 pkey_gost_mac_signctx);
1087 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1088 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_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 default: /* Unsupported method */
1095 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1096 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1098 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1100 * FIXME derive etc...