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:
354 case NID_id_tc26_gost_3410_2012_512_paramSetTest:
356 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
359 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
360 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
361 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
362 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
363 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
364 case NID_id_GostR3410_2001_TestParamSet:
365 case NID_id_tc26_gost_3410_2012_256_paramSetA:
366 case NID_id_tc26_gost_3410_2012_256_paramSetB:
367 case NID_id_tc26_gost_3410_2012_256_paramSetC:
368 case NID_id_tc26_gost_3410_2012_256_paramSetD:
370 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
383 /* ----------- keygen callbacks --------------------------------------*/
384 /* Generates GOST_R3410 2001 key and assigns it using specified type */
385 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
388 if (!pkey_gost2001_paramgen(ctx, pkey))
390 ec = EVP_PKEY_get0(pkey);
395 /* Generates GOST_R3410 2012 key and assigns it using specified type */
396 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
398 if (!pkey_gost2012_paramgen(ctx, pkey))
401 gost_ec_keygen(EVP_PKEY_get0(pkey));
405 /* ----------- sign callbacks --------------------------------------*/
407 * Packs signature according to Cryptopro rules
408 * and frees up ECDSA_SIG structure
410 int pack_sign_cp(ECDSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
412 const BIGNUM *sig_r = NULL, *sig_s = NULL;
413 ECDSA_SIG_get0(s, &sig_r, &sig_s);
415 memset(sig, 0, *siglen);
416 store_bignum(sig_s, sig, order);
417 store_bignum(sig_r, sig + order, order);
422 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
423 size_t *siglen, const unsigned char *tbs,
426 ECDSA_SIG *unpacked_sig = NULL;
427 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
435 switch (EVP_PKEY_base_id(pkey)) {
436 case NID_id_GostR3410_2001:
437 case NID_id_GostR3410_2012_256:
440 case NID_id_GostR3410_2012_512:
451 unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
455 return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
458 /* ------------------- verify callbacks ---------------------------*/
459 /* Unpack signature according to cryptopro rules */
460 ECDSA_SIG *unpack_cp_signature(const unsigned char *sigbuf, size_t siglen)
463 BIGNUM *r = NULL, *s = NULL;
465 sig = ECDSA_SIG_new();
467 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
470 s = BN_bin2bn(sigbuf, siglen / 2, NULL);
471 r = BN_bin2bn(sigbuf + siglen / 2, siglen / 2, NULL);
472 ECDSA_SIG_set0(sig, r, s);
476 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
477 size_t siglen, const unsigned char *tbs,
481 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
482 ECDSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
486 fprintf(stderr, "R=");
487 BN_print_fp(stderr, ECDSA_SIG_get0_r(s));
488 fprintf(stderr, "\nS=");
489 BN_print_fp(stderr, ECDSA_SIG_get0_s(s));
490 fprintf(stderr, "\n");
493 ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
498 /* ------------- encrypt init -------------------------------------*/
499 /* Generates ephermeral key */
500 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
505 /* --------------- Derive init ------------------------------------*/
506 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
511 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
512 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
514 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
515 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
519 memset(data, 0, sizeof(*data));
521 data->mac_param_nid = NID_undef;
524 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
526 data->mac_param_nid = key->mac_param_nid;
527 data->mac_size = key->mac_size;
531 EVP_PKEY_CTX_set_data(ctx, data);
535 static int pkey_gost_omac_init(EVP_PKEY_CTX *ctx, size_t mac_size)
537 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
538 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
542 memset(data, 0, sizeof(*data));
543 data->mac_size = mac_size;
544 data->mac_param_nid = NID_undef;
547 struct gost_mac_key *key = EVP_PKEY_get0(pkey);
549 data->mac_param_nid = key->mac_param_nid;
550 data->mac_size = key->mac_size;
554 EVP_PKEY_CTX_set_data(ctx, data);
558 static int pkey_gost_magma_mac_init(EVP_PKEY_CTX *ctx)
560 return pkey_gost_omac_init(ctx, 8);
563 static int pkey_gost_grasshopper_mac_init(EVP_PKEY_CTX *ctx)
565 return pkey_gost_omac_init(ctx, 16);
568 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
570 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
575 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, ossl3_const EVP_PKEY_CTX *src)
577 struct gost_mac_pmeth_data *dst_data, *src_data;
578 if (!pkey_gost_mac_init(dst)) {
581 src_data = EVP_PKEY_CTX_get_data(src);
582 dst_data = EVP_PKEY_CTX_get_data(dst);
583 if (!src_data || !dst_data)
586 *dst_data = *src_data;
590 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
592 struct gost_mac_pmeth_data *data =
593 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
596 case EVP_PKEY_CTRL_MD:
598 int nid = EVP_MD_type((const EVP_MD *)p2);
599 if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
600 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
601 GOST_R_INVALID_DIGEST_TYPE);
604 data->md = (EVP_MD *)p2;
608 case EVP_PKEY_CTRL_GET_MD:
609 *(const EVP_MD **)p2 = data->md;
612 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
613 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
614 case EVP_PKEY_CTRL_PKCS7_SIGN:
616 case EVP_PKEY_CTRL_SET_MAC_KEY:
618 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
622 memcpy(data->key, p2, 32);
625 case EVP_PKEY_CTRL_GOST_PARAMSET:
627 struct gost_cipher_info *param = p2;
628 data->mac_param_nid = param->nid;
631 case EVP_PKEY_CTRL_DIGESTINIT:
633 EVP_MD_CTX *mctx = p2;
634 struct gost_mac_key *key;
635 if (!data->key_set) {
636 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
638 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
639 GOST_R_MAC_KEY_NOT_SET);
642 key = EVP_PKEY_get0(pkey);
644 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
645 GOST_R_MAC_KEY_NOT_SET);
648 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
649 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
651 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
652 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
655 case EVP_PKEY_CTRL_MAC_LEN:
657 if (p1 < 1 || p1 > 8) {
659 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_SIZE);
669 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
670 const char *type, const char *value)
672 if (strcmp(type, key_ctrl_string) == 0) {
673 if (strlen(value) != 32) {
674 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
675 GOST_R_INVALID_MAC_KEY_LENGTH);
678 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
681 if (strcmp(type, hexkey_ctrl_string) == 0) {
684 unsigned char *keybuf = string_to_hex(value, &keylen);
685 if (!keybuf || keylen != 32) {
686 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
687 GOST_R_INVALID_MAC_KEY_LENGTH);
688 OPENSSL_free(keybuf);
691 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
692 OPENSSL_free(keybuf);
696 if (!strcmp(type, maclen_ctrl_string)) {
698 long size = strtol(value, &endptr, 10);
699 if (*endptr != '\0') {
700 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
703 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL);
705 if (strcmp(type, param_ctrl_string) == 0) {
706 ASN1_OBJECT *obj = OBJ_txt2obj(value, 0);
707 const struct gost_cipher_info *param = NULL;
709 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
713 param = get_encryption_params(obj);
714 ASN1_OBJECT_free(obj);
716 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR, GOST_R_INVALID_MAC_PARAMS);
721 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, 0,
727 static int pkey_gost_omac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2, size_t max_size)
729 struct gost_mac_pmeth_data *data =
730 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
733 case EVP_PKEY_CTRL_MD:
735 int nid = EVP_MD_type((const EVP_MD *)p2);
736 if (nid != NID_magma_mac && nid != NID_grasshopper_mac
737 && nid != NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac /* FIXME beldmit */
738 && nid != NID_id_tc26_cipher_gostr3412_2015_magma_ctracpkm_omac) {
739 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
740 GOST_R_INVALID_DIGEST_TYPE);
743 data->md = (EVP_MD *)p2;
747 case EVP_PKEY_CTRL_GET_MD:
748 *(const EVP_MD **)p2 = data->md;
751 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
752 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
753 case EVP_PKEY_CTRL_PKCS7_SIGN:
755 case EVP_PKEY_CTRL_SET_MAC_KEY:
757 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
761 memcpy(data->key, p2, 32);
764 case EVP_PKEY_CTRL_DIGESTINIT:
766 EVP_MD_CTX *mctx = p2;
767 struct gost_mac_key *key;
768 if (!data->key_set) {
769 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
771 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
772 GOST_R_MAC_KEY_NOT_SET);
775 key = EVP_PKEY_get0(pkey);
777 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL,
778 GOST_R_MAC_KEY_NOT_SET);
781 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
782 (mctx, EVP_MD_CTRL_SET_KEY, 0, key);
784 return EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
785 (mctx, EVP_MD_CTRL_SET_KEY, 32, &(data->key));
788 case EVP_PKEY_CTRL_MAC_LEN:
790 if (p1 < 1 || p1 > max_size) {
792 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL, GOST_R_INVALID_MAC_SIZE);
802 static int pkey_gost_magma_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
804 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 8);
807 static int pkey_gost_grasshopper_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
809 return pkey_gost_omac_ctrl(ctx, type, p1, p2, 16);
812 static int pkey_gost_omac_ctrl_str(EVP_PKEY_CTX *ctx,
813 const char *type, const char *value, size_t max_size)
815 if (strcmp(type, key_ctrl_string) == 0) {
816 if (strlen(value) != 32) {
817 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
818 GOST_R_INVALID_MAC_KEY_LENGTH);
821 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
824 if (strcmp(type, hexkey_ctrl_string) == 0) {
827 unsigned char *keybuf = string_to_hex(value, &keylen);
828 if (!keybuf || keylen != 32) {
829 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR,
830 GOST_R_INVALID_MAC_KEY_LENGTH);
831 OPENSSL_free(keybuf);
834 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
835 OPENSSL_free(keybuf);
839 if (!strcmp(type, maclen_ctrl_string)) {
841 long size = strtol(value, &endptr, 10);
842 if (*endptr != '\0') {
843 GOSTerr(GOST_F_PKEY_GOST_OMAC_CTRL_STR, GOST_R_INVALID_MAC_SIZE);
846 return pkey_gost_omac_ctrl(ctx, EVP_PKEY_CTRL_MAC_LEN, size, NULL, max_size);
851 static int pkey_gost_magma_mac_ctrl_str(EVP_PKEY_CTX *ctx,
852 const char *type, const char *value)
854 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
857 static int pkey_gost_grasshopper_mac_ctrl_str(EVP_PKEY_CTX *ctx,
858 const char *type, const char *value)
860 return pkey_gost_omac_ctrl_str(ctx, type, value, 8);
863 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
864 EVP_PKEY *pkey, int mac_nid)
866 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
867 struct gost_mac_key *keydata;
868 if (!data || !data->key_set) {
869 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN_BASE, GOST_R_MAC_KEY_NOT_SET);
872 keydata = OPENSSL_malloc(sizeof(struct gost_mac_key));
875 memcpy(keydata->key, data->key, 32);
876 keydata->mac_param_nid = data->mac_param_nid;
877 keydata->mac_size = data->mac_size;
878 EVP_PKEY_assign(pkey, mac_nid, keydata);
882 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
884 return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
887 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
889 return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
892 static int pkey_gost_magma_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
894 return pkey_gost_mac_keygen_base(ctx, pkey, NID_magma_mac);
897 static int pkey_gost_grasshopper_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
899 return pkey_gost_mac_keygen_base(ctx, pkey, NID_grasshopper_mac);
902 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
904 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
907 pkey_gost_mac_init(ctx);
910 data = EVP_PKEY_CTX_get_data(ctx);
912 GOSTerr(GOST_F_PKEY_GOST_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
919 static int pkey_gost_magma_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
921 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
924 pkey_gost_omac_init(ctx, 4);
927 data = EVP_PKEY_CTX_get_data(ctx);
929 GOSTerr(GOST_F_PKEY_GOST_MAGMA_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
936 static int pkey_gost_grasshopper_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
938 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
941 pkey_gost_omac_init(ctx, 8);
944 data = EVP_PKEY_CTX_get_data(ctx);
946 GOSTerr(GOST_F_PKEY_GOST_GRASSHOPPER_MAC_SIGNCTX_INIT, GOST_R_MAC_KEY_NOT_SET);
953 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
954 size_t *siglen, EVP_MD_CTX *mctx)
956 unsigned int tmpsiglen;
958 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
962 tmpsiglen = *siglen; /* for platforms where sizeof(int) !=
966 *siglen = data->mac_size;
970 EVP_MD_meth_get_ctrl(EVP_MD_CTX_md(mctx))
971 (mctx, EVP_MD_CTRL_XOF_LEN, data->mac_size, NULL);
972 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
973 *siglen = data->mac_size;
977 /* ----------- misc callbacks -------------------------------------*/
979 /* Callback for both EVP_PKEY_check() and EVP_PKEY_public_check. */
980 static int pkey_gost_check(EVP_PKEY *pkey)
982 return EC_KEY_check_key(EVP_PKEY_get0(pkey));
985 /* ----------------------------------------------------------------*/
986 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
988 *pmeth = EVP_PKEY_meth_new(id, flags);
993 case NID_id_GostR3410_2001:
994 EVP_PKEY_meth_set_ctrl(*pmeth,
995 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
996 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
997 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
999 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
1001 EVP_PKEY_meth_set_encrypt(*pmeth,
1002 pkey_gost_encrypt_init,
1004 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1005 EVP_PKEY_meth_set_derive(*pmeth,
1006 pkey_gost_derive_init, pkey_gost_ec_derive);
1007 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
1008 pkey_gost2001_paramgen);
1009 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1010 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1012 case NID_id_GostR3410_2012_256:
1013 EVP_PKEY_meth_set_ctrl(*pmeth,
1014 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
1015 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1016 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1018 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1020 EVP_PKEY_meth_set_encrypt(*pmeth,
1021 pkey_gost_encrypt_init,
1023 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1024 EVP_PKEY_meth_set_derive(*pmeth,
1025 pkey_gost_derive_init, pkey_gost_ec_derive);
1026 EVP_PKEY_meth_set_paramgen(*pmeth,
1027 pkey_gost_paramgen_init,
1028 pkey_gost2012_paramgen);
1029 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1030 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1032 case NID_id_GostR3410_2012_512:
1033 EVP_PKEY_meth_set_ctrl(*pmeth,
1034 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
1035 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
1036 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
1038 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
1040 EVP_PKEY_meth_set_encrypt(*pmeth,
1041 pkey_gost_encrypt_init,
1043 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_gost_decrypt);
1044 EVP_PKEY_meth_set_derive(*pmeth,
1045 pkey_gost_derive_init, pkey_gost_ec_derive);
1046 EVP_PKEY_meth_set_paramgen(*pmeth,
1047 pkey_gost_paramgen_init,
1048 pkey_gost2012_paramgen);
1049 EVP_PKEY_meth_set_check(*pmeth, pkey_gost_check);
1050 EVP_PKEY_meth_set_public_check(*pmeth, pkey_gost_check);
1052 case NID_id_Gost28147_89_MAC:
1053 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1054 pkey_gost_mac_ctrl_str);
1055 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1056 pkey_gost_mac_signctx);
1057 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
1058 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1059 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1060 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1062 case NID_gost_mac_12:
1063 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
1064 pkey_gost_mac_ctrl_str);
1065 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
1066 pkey_gost_mac_signctx);
1067 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
1068 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
1069 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1070 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1073 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_magma_mac_ctrl,
1074 pkey_gost_magma_mac_ctrl_str);
1075 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_magma_mac_signctx_init,
1076 pkey_gost_mac_signctx);
1077 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_magma_mac_keygen);
1078 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_magma_mac_init);
1079 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1080 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1082 case NID_grasshopper_mac:
1083 case NID_id_tc26_cipher_gostr3412_2015_kuznyechik_ctracpkm_omac: /* FIXME beldmit */
1084 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_grasshopper_mac_ctrl,
1085 pkey_gost_grasshopper_mac_ctrl_str);
1086 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_grasshopper_mac_signctx_init,
1087 pkey_gost_mac_signctx);
1088 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_grasshopper_mac_keygen);
1089 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_grasshopper_mac_init);
1090 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
1091 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
1093 default: /* Unsupported method */
1096 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
1097 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
1099 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
1101 * FIXME derive etc...