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_common(EVP_PKEY_CTX *ctx,
181 const char *type, const char *value)
183 if (0 == strcmp(type, ukm_ctrl_string)) {
184 unsigned char ukm_buf[32], *tmp = NULL;
186 tmp = OPENSSL_hexstr2buf(value, &len);
192 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_COMMON, GOST_R_CTRL_CALL_FAILED);
195 memcpy(ukm_buf, tmp, len);
198 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_SET_IV, len, ukm_buf);
203 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
204 const char *type, const char *value)
206 if (strcmp(type, param_ctrl_string) == 0) {
212 if (strlen(value) == 1) {
213 switch (toupper((unsigned char)value[0])) {
215 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
218 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
221 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
224 param_nid = NID_id_GostR3410_2001_TestParamSet;
229 } else if ((strlen(value) == 2)
230 && (toupper((unsigned char)value[0]) == 'X')) {
231 switch (toupper((unsigned char)value[1])) {
233 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
236 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
241 } else if ((strlen(value) == 3)
242 && (toupper((unsigned char)value[0]) == 'T')
243 && (toupper((unsigned char)value[1]) == 'C')) {
244 switch (toupper((unsigned char)value[2])) {
246 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetA;
249 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetB;
252 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetC;
255 param_nid = NID_id_tc26_gost_3410_2012_256_paramSetD;
261 R3410_ec_params *p = R3410_2001_paramset;
262 param_nid = OBJ_txt2nid(value);
263 if (param_nid == NID_undef) {
266 for (; p->nid != NID_undef; p++) {
267 if (p->nid == param_nid)
270 if (p->nid == NID_undef) {
271 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
272 GOST_R_INVALID_PARAMSET);
277 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
281 return pkey_gost_ec_ctrl_str_common(ctx, type, value);
284 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
285 const char *type, const char *value)
287 int param_nid = NID_undef;
289 if (strcmp(type, param_ctrl_string))
290 return pkey_gost_ec_ctrl_str_common(ctx, type, value);
295 if (strlen(value) == 1) {
296 switch (toupper((unsigned char)value[0])) {
298 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
302 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
306 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetC;
313 R3410_ec_params *p = R3410_2012_512_paramset;
314 param_nid = OBJ_txt2nid(value);
315 if (param_nid == NID_undef)
318 while (p->nid != NID_undef && p->nid != param_nid)
321 if (p->nid == NID_undef) {
322 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
323 GOST_R_INVALID_PARAMSET);
328 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
331 /* --------------------- key generation --------------------------------*/
333 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
338 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
340 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
343 if (!data || data->sign_param_nid == NID_undef) {
344 GOSTerr(GOST_F_PKEY_GOST2001_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
349 if (!fill_GOST_EC_params(ec, data->sign_param_nid)
350 || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
357 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
359 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
363 if (!data || data->sign_param_nid == NID_undef) {
364 GOSTerr(GOST_F_PKEY_GOST2012_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
369 if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
374 switch (data->sign_param_nid) {
375 case NID_id_tc26_gost_3410_2012_512_paramSetA:
376 case NID_id_tc26_gost_3410_2012_512_paramSetB:
377 case NID_id_tc26_gost_3410_2012_512_paramSetC:
378 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
380 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
383 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
384 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
385 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
386 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
387 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
388 case NID_id_GostR3410_2001_TestParamSet:
389 case NID_id_tc26_gost_3410_2012_256_paramSetA:
390 case NID_id_tc26_gost_3410_2012_256_paramSetB:
391 case NID_id_tc26_gost_3410_2012_256_paramSetC:
392 case NID_id_tc26_gost_3410_2012_256_paramSetD:
394 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
407 /* ----------- keygen callbacks --------------------------------------*/
408 /* Generates GOST_R3410 2001 key and assigns it using specified type */
409 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
412 if (!pkey_gost2001_paramgen(ctx, pkey))
414 ec = EVP_PKEY_get0(pkey);
419 /* Generates GOST_R3410 2012 key and assigns it using specified type */
420 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
422 if (!pkey_gost2012_paramgen(ctx, pkey))
425 gost_ec_keygen(EVP_PKEY_get0(pkey));
429 /* ----------- sign callbacks --------------------------------------*/
431 * Packs signature according to Cryptopro rules
432 * and frees up ECDSA_SIG structure
434 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
436 const BIGNUM *sig_r = NULL, *sig_s = NULL;
437 ECDSA_SIG_get0(s, &sig_r, &sig_s);
439 memset(sig, 0, *siglen);
440 store_bignum(sig_s, sig, order);
441 store_bignum(sig_r, sig + order, order);
446 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
447 size_t *siglen, const unsigned char *tbs,
450 ECDSA_SIG *unpacked_sig = NULL;
451 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
459 switch (EVP_PKEY_base_id(pkey)) {
460 case NID_id_GostR3410_2001:
461 case NID_id_GostR3410_2012_256:
464 case NID_id_GostR3410_2012_512:
475 unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
479 return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
482 /* ------------------- verify callbacks ---------------------------*/
483 /* Unpack signature according to cryptopro rules */
484 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
487 BIGNUM *r = NULL, *s = NULL;
489 sig = ECDSA_SIG_new();
491 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
494 s = BN_bin2bn(sigbuf, siglen / 2, NULL);
495 r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
496 ECDSA_SIG_set0(sig, r, s);
500 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
501 size_t siglen, const unsigned char *tbs,
505 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
506 ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
510 fprintf(stderr, "R=");
511 BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
512 fprintf(stderr, "\nS=");
513 BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
514 fprintf(stderr, "\n");
517 ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
522 /* ------------- encrypt init -------------------------------------*/
523 /* Generates ephermeral key */
524 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
529 /* --------------- Derive init ------------------------------------*/
530 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
535 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
536 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
538 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
539 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
543 memset(data, 0, sizeof(*data));
545 data->mac_param_nid = NID_undef;
548 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
550 data->mac_param_nid = key->mac_param_nid;
551 data->mac_size = key->mac_size;
555 EVP_PKEY_CTX_set_data(ctx, data);
559 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
561 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
562 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
566 memset(data, 0, sizeof(*data));
567 data->mac_size = mac_size;
568 data->mac_param_nid = NID_undef;
571 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
573 data->mac_param_nid = key->mac_param_nid;
574 data->mac_size = key->mac_size;
578 EVP_PKEY_CTX_set_data(ctx, data);
582 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
584 return pkey_gost_omac_init(ctx, 8);
587 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
589 return pkey_gost_omac_init(ctx, 16);
592 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
594 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
599 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
601 struct gost_mac_pmeth_data *dst_data, *src_data;
602 if (!pkey_gost_mac_init(dst)) {
605 src_data = EVP_PKEY_CTX_get_data(src);
606 dst_data = EVP_PKEY_CTX_get_data(dst);
607 if (!src_data || !dst_data)
610 *dst_data = *src_data;
614 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
616 struct gost_mac_pmeth_data *data =
617 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
620 case EVP_PKEY_CTRL_MD:
622 int nid = EVP_MD_type((const EVP_MD *)p2);
623 if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
624 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
625 GOST_R_INVALID_DIGEST_TYPE);
628 data->md = (EVP_MD *)p2;
632 case EVP_PKEY_CTRL_GET_MD:
633 *(const EVP_MD **)p2 = data->md;
636 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
637 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
638 case EVP_PKEY_CTRL_PKCS7_SIGN:
640 case EVP_PKEY_CTRL_SET_MAC_KEY:
642 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
646 memcpy(data->key, p2, 32);
649 case EVP_PKEY_CTRL_GOST_PARAMSET:
651 struct gost_cipher_info *param = p2;
652 data->mac_param_nid = param->nid;
655 case EVP_PKEY_CTRL_DIGESTINIT:
657 EVP_MD_CTX *mctx = p2;
658 if (!data->key_set) {
659 struct gost_mac_key *key;
660 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
662 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
663 GOST_R_MAC_KEY_NOT_SET);
666 key = EVP_PKEY_get0(pkey);
668 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
669 GOST_R_MAC_KEY_NOT_SET);
672 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
673 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
675 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
676 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
679 case EVP_PKEY_CTRL_MAC_LEN:
681 if (p1 < 1 || p1 > 8) {
683 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
693 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
694 const char *type, const char *value)
696 if (strcmp(type, key_ctrl_string) == 0) {
697 if (strlen(value) != 32) {
698 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
699 GOST_R_INVALID_MAC_KEY_LENGTH);
702 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
705 if (strcmp(type, hexkey_ctrl_string) == 0) {
708 unsigned char *keybuf = string_to_hex(value, &keylen);
709 if (!keybuf || keylen != 32) {
710 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
711 GOST_R_INVALID_MAC_KEY_LENGTH);
712 OPENSSL_free(keybuf);
715 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
716 OPENSSL_free(keybuf);
720 if (!strcmp(type, maclen_ctrl_string)) {
722 long size = strtol(value, &endptr, 10);
723 if (*endptr != '\0') {
724 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
727 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
729 if (strcmp(type, param_ctrl_string) == 0) {
730 ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
731 const struct gost_cipher_info *param = NULL;
733 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
737 param = get_encryption_params(obj);
738 ASN1_OBJECT_free(obj);
740 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
745 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
751 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
753 struct gost_mac_pmeth_data *data =
754 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
757 case EVP_PKEY_CTRL_MD:
759 int nid = EVP_MD_type((const EVP_MD *)p2);
760 if (nid != NID_magma_mac && nid != NID_grasshopper_mac
761 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
762 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
763 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
764 GOST_R_INVALID_DIGEST_TYPE);
767 data->md = (EVP_MD *)p2;
771 case EVP_PKEY_CTRL_GET_MD:
772 *(const EVP_MD **)p2 = data->md;
775 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
776 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
777 case EVP_PKEY_CTRL_PKCS7_SIGN:
779 case EVP_PKEY_CTRL_SET_MAC_KEY:
781 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
785 memcpy(data->key, p2, 32);
788 case EVP_PKEY_CTRL_DIGESTINIT:
790 EVP_MD_CTX *mctx = p2;
791 if (!data->key_set) {
792 struct gost_mac_key *key;
793 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
795 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
796 GOST_R_MAC_KEY_NOT_SET);
799 key = EVP_PKEY_get0(pkey);
801 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
802 GOST_R_MAC_KEY_NOT_SET);
805 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
806 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
808 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
809 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
812 case EVP_PKEY_CTRL_MAC_LEN:
814 if (p1 < 1 || p1 > max_size) {
816 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
826 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
828 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
831 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
833 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
836 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
837 const char *type, const char *value, size_t max_size)
839 if (strcmp(type, key_ctrl_string) == 0) {
840 if (strlen(value) != 32) {
841 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
842 GOST_R_INVALID_MAC_KEY_LENGTH);
845 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
848 if (strcmp(type, hexkey_ctrl_string) == 0) {
851 unsigned char *keybuf = string_to_hex(value, &keylen);
852 if (!keybuf || keylen != 32) {
853 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
854 GOST_R_INVALID_MAC_KEY_LENGTH);
855 OPENSSL_free(keybuf);
858 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
859 OPENSSL_free(keybuf);
863 if (!strcmp(type, maclen_ctrl_string)) {
865 long size = strtol(value, &endptr, 10);
866 if (*endptr != '\0') {
867 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
870 return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
875 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
876 const char *type, const char *value)
878 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
881 static int pkey_gost_grasshopper_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_mac_keygen_base(EVP_PKEY_CTX *ctx,
888 EVP_PKEY *pkey, int mac_nid)
890 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
891 struct gost_mac_key *keydata;
892 if (!data || !data->key_set) {
893 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
896 keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
899 memcpy(keydata->key, data->key, 32);
900 keydata->mac_param_nid = data->mac_param_nid;
901 keydata->mac_size = data->mac_size;
902 EVP_PKEY_assign(pkey, mac_nid, keydata);
906 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
908 return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
911 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
913 return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
916 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
918 return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
921 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
923 return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
926 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
928 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
931 pkey_gost_mac_init(ctx);
934 data = EVP_PKEY_CTX_get_data(ctx);
936 GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
943 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
945 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
948 pkey_gost_omac_init(ctx, 4);
951 data = EVP_PKEY_CTX_get_data(ctx);
953 GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
960 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
962 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
965 pkey_gost_omac_init(ctx, 8);
968 data = EVP_PKEY_CTX_get_data(ctx);
970 GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
977 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
978 size_t *siglen, EVP_MD_CTX *mctx)
980 unsigned int tmpsiglen;
982 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
986 tmpsiglen = *siglen; /* for platforms where sizeof(int) !=
990 *siglen = data->mac_size;
994 EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
995 (mctx, EVP_MD_CTRL_XOF_LEN, data->mac_size, NULL);
996 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
997 *siglen = data->mac_size;
1001 /* ----------- misc callbacks -------------------------------------*/
1003 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
1004 static int pkey_gost_check(EVP_PKEY *pkey)
1006 return EC_KEY_check_key(EVP_PKEY_get0(pkey));
1009 /* ----------------------------------------------------------------*/
1010 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
1012 *pmeth = EVP_PKEY_meth_new(id, flags);
1017 case NID_id_GostR3410_2001:
1018 EVP_PKEY_meth_set_ctrl(*pmeth,
1019 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1020 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1021 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1023 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
1025 EVP_PKEY_meth_set_encrypt(*pmeth,
1026 pkey_gost_encrypt_init,
1028 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1029 EVP_PKEY_meth_set_derive(*pmeth,
1030 pkey_gost_derive_init, pkey_gost_ec_derive);
1031 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1032 pkey_gost2001_paramgen);
1033 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1034 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1036 case NID_id_GostR3410_2012_256:
1037 EVP_PKEY_meth_set_ctrl(*pmeth,
1038 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1039 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1040 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1042 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1044 EVP_PKEY_meth_set_encrypt(*pmeth,
1045 pkey_gost_encrypt_init,
1047 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1048 EVP_PKEY_meth_set_derive(*pmeth,
1049 pkey_gost_derive_init, pkey_gost_ec_derive);
1050 EVP_PKEY_meth_set_paramgen(*pmeth,
1051 pkey_gost_paramgen_init,
1052 pkey_gost2012_paramgen);
1053 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1054 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1056 case NID_id_GostR3410_2012_512:
1057 EVP_PKEY_meth_set_ctrl(*pmeth,
1058 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1059 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1060 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1062 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1064 EVP_PKEY_meth_set_encrypt(*pmeth,
1065 pkey_gost_encrypt_init,
1067 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1068 EVP_PKEY_meth_set_derive(*pmeth,
1069 pkey_gost_derive_init, pkey_gost_ec_derive);
1070 EVP_PKEY_meth_set_paramgen(*pmeth,
1071 pkey_gost_paramgen_init,
1072 pkey_gost2012_paramgen);
1073 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1074 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1076 case NID_id_Gost28147_89_MAC:
1077 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1078 pkey_gost_mac_ctrl_str);
1079 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1080 pkey_gost_mac_signctx);
1081 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1082 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1083 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1084 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1086 case NID_gost_mac_12:
1087 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1088 pkey_gost_mac_ctrl_str);
1089 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1090 pkey_gost_mac_signctx);
1091 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1092 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1093 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1094 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1097 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1098 pkey_gost_magma_mac_ctrl_str);
1099 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1100 pkey_gost_mac_signctx);
1101 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1102 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1103 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1104 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1106 case NID_grasshopper_mac:
1107 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1108 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1109 pkey_gost_grasshopper_mac_ctrl_str);
1110 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1111 pkey_gost_mac_signctx);
1112 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1113 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_mac_init);
1114 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1115 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1117 default: /* Unsupported method */
1120 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1121 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1123 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1125 * FIXME derive etc...