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 */
19 #include "e_gost_err.h"
21 /* -----init, cleanup, copy - uniform for all algs --------------*/
22 /* Allocates new gost_pmeth_data structure and assigns it as data */
23 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
25 struct gost_pmeth_data *data;
26 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
28 data = OPENSSL_malloc(sizeof(*data));
31 memset(data, 0, sizeof(*data));
32 if (pkey && EVP_PKEY_get0(pkey)) {
33 switch (EVP_PKEY_base_id(pkey)) {
34 case NID_id_GostR3410_2001:
35 case NID_id_GostR3410_2012_256:
36 case NID_id_GostR3410_2012_512:
38 const EC_GROUP *group =
39 EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey));
41 data->sign_param_nid = EC_GROUP_get_curve_name(group);
51 EVP_PKEY_CTX_set_data(ctx, data);
55 /* Copies contents of gost_pmeth_data structure */
56 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
58 struct gost_pmeth_data *dst_data, *src_data;
59 if (!pkey_gost_init(dst)) {
62 src_data = EVP_PKEY_CTX_get_data(src);
63 dst_data = EVP_PKEY_CTX_get_data(dst);
64 if (!src_data || !dst_data)
67 *dst_data = *src_data;
68 if (src_data->shared_ukm) {
69 dst_data->shared_ukm = NULL;
74 /* Frees up gost_pmeth_data structure */
75 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
77 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
80 OPENSSL_free(data->shared_ukm);
84 /* --------------------- control functions ------------------------------*/
85 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
87 struct gost_pmeth_data *pctx =
88 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
93 case EVP_PKEY_CTRL_MD:
95 EVP_PKEY *key = EVP_PKEY_CTX_get0_pkey(ctx);
96 int pkey_nid = (key == NULL) ? NID_undef : EVP_PKEY_base_id(key);
98 OPENSSL_assert(p2 != NULL);
100 switch (EVP_MD_type((const EVP_MD *)p2)) {
101 case NID_id_GostR3411_94:
102 if (pkey_nid == NID_id_GostR3410_2001
103 || pkey_nid == NID_id_GostR3410_94) {
104 pctx->md = (EVP_MD *)p2;
109 case NID_id_GostR3411_2012_256:
110 if (pkey_nid == NID_id_GostR3410_2012_256) {
111 pctx->md = (EVP_MD *)p2;
116 case NID_id_GostR3411_2012_512:
117 if (pkey_nid == NID_id_GostR3410_2012_512) {
118 pctx->md = (EVP_MD *)p2;
124 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
128 case EVP_PKEY_CTRL_GET_MD:
129 *(const EVP_MD **)p2 = pctx->md;
132 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
133 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
134 case EVP_PKEY_CTRL_PKCS7_SIGN:
135 case EVP_PKEY_CTRL_DIGESTINIT:
136 #ifndef OPENSSL_NO_CMS
137 case EVP_PKEY_CTRL_CMS_ENCRYPT:
138 case EVP_PKEY_CTRL_CMS_DECRYPT:
139 case EVP_PKEY_CTRL_CMS_SIGN:
143 case EVP_PKEY_CTRL_GOST_PARAMSET:
144 pctx->sign_param_nid = (int)p1;
146 case EVP_PKEY_CTRL_SET_IV:
147 OPENSSL_assert(p2 != NULL);
148 pctx->shared_ukm = OPENSSL_malloc((int)p1);
149 if (!pctx->shared_ukm)
151 memcpy(pctx->shared_ukm, p2, (int)p1);
153 case EVP_PKEY_CTRL_PEER_KEY:
154 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
156 if (p1 == 2) /* TLS: peer key used? */
157 return pctx->peer_key_used;
158 if (p1 == 3) /* TLS: peer key used! */
159 return (pctx->peer_key_used = 1);
163 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_CTRL_CALL_FAILED);
167 static int pkey_gost_ec_ctrl_str_256(EVP_PKEY_CTX *ctx,
168 const char *type, const char *value)
172 if (strcmp(type, param_ctrl_string) == 0) {
176 if (strlen(value) == 1) {
177 switch (toupper((unsigned char)value[0])) {
179 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
182 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
185 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
188 param_nid = NID_id_GostR3410_2001_TestParamSet;
193 } else if ((strlen(value) == 2)
194 && (toupper((unsigned char)value[0]) == 'X')) {
195 switch (toupper((unsigned char)value[1])) {
197 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
200 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
206 R3410_ec_params *p = R3410_2001_paramset;
207 param_nid = OBJ_txt2nid(value);
208 if (param_nid == NID_undef) {
211 for (; p->nid != NID_undef; p++) {
212 if (p->nid == param_nid)
215 if (p->nid == NID_undef) {
216 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_256,
217 GOST_R_INVALID_PARAMSET);
222 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
228 static int pkey_gost_ec_ctrl_str_512(EVP_PKEY_CTX *ctx,
229 const char *type, const char *value)
231 int param_nid = NID_undef;
233 if (strcmp(type, param_ctrl_string))
239 if (strlen(value) == 1) {
240 switch (toupper((unsigned char)value[0])) {
242 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetA;
246 param_nid = NID_id_tc26_gost_3410_2012_512_paramSetB;
253 R3410_ec_params *p = R3410_2012_512_paramset;
254 param_nid = OBJ_txt2nid(value);
255 if (param_nid == NID_undef)
258 while (p->nid != NID_undef && p->nid != param_nid)
261 if (p->nid == NID_undef) {
262 GOSTerr(GOST_F_PKEY_GOST_EC_CTRL_STR_512,
263 GOST_R_INVALID_PARAMSET);
268 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET, param_nid, NULL);
271 /* --------------------- key generation --------------------------------*/
273 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
278 static int pkey_gost2001_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
280 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
283 if (!data || data->sign_param_nid == NID_undef) {
284 GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
289 if (!fill_GOST_EC_params(ec, data->sign_param_nid)
290 || !EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec)) {
297 static int pkey_gost2012_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
299 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
303 if (!data || data->sign_param_nid == NID_undef) {
304 GOSTerr(GOST_F_PKEY_GOST12_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
309 if (!fill_GOST_EC_params(ec, data->sign_param_nid)) {
314 switch (data->sign_param_nid) {
315 case NID_id_tc26_gost_3410_2012_512_paramSetA:
316 case NID_id_tc26_gost_3410_2012_512_paramSetB:
318 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_512, ec)) ? 1 : 0;
321 case NID_id_GostR3410_2001_CryptoPro_A_ParamSet:
322 case NID_id_GostR3410_2001_CryptoPro_B_ParamSet:
323 case NID_id_GostR3410_2001_CryptoPro_C_ParamSet:
324 case NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet:
325 case NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet:
326 case NID_id_GostR3410_2001_TestParamSet:
328 (EVP_PKEY_assign(pkey, NID_id_GostR3410_2012_256, ec)) ? 1 : 0;
341 /* ----------- keygen callbacks --------------------------------------*/
342 /* Generates GOST_R3410 2001 key and assigns it using specified type */
343 static int pkey_gost2001cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
346 if (!pkey_gost2001_paramgen(ctx, pkey))
348 ec = EVP_PKEY_get0(pkey);
353 /* Generates GOST_R3410 2012 key and assigns it using specified type */
354 static int pkey_gost2012cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
356 if (!pkey_gost2012_paramgen(ctx, pkey))
359 gost_ec_keygen(EVP_PKEY_get0(pkey));
363 /* ----------- sign callbacks --------------------------------------*/
365 * Packs signature according to Cryptopro rules
366 * and frees up DSA_SIG structure
368 int pack_sign_cp(DSA_SIG *s, int order, unsigned char *sig, size_t *siglen)
371 memset(sig, 0, *siglen);
372 store_bignum(s->s, sig, order);
373 store_bignum(s->r, sig + order, order);
378 static int pkey_gost_ec_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
379 size_t *siglen, const unsigned char *tbs,
382 DSA_SIG *unpacked_sig = NULL;
383 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
391 switch (EVP_PKEY_base_id(pkey)) {
392 case NID_id_GostR3410_2001:
393 case NID_id_GostR3410_2012_256:
396 case NID_id_GostR3410_2012_512:
407 unpacked_sig = gost_ec_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
411 return pack_sign_cp(unpacked_sig, order / 2, sig, siglen);
414 /* ------------------- verify callbacks ---------------------------*/
415 /* Unpack signature according to cryptopro rules */
416 DSA_SIG *unpack_cp_signature(const unsigned char *sig, size_t siglen)
422 GOSTerr(GOST_F_UNPACK_CP_SIGNATURE, ERR_R_MALLOC_FAILURE);
425 s->s = BN_bin2bn(sig, siglen / 2, NULL);
426 s->r = BN_bin2bn(sig + siglen / 2, siglen / 2, NULL);
430 static int pkey_gost_ec_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
431 size_t siglen, const unsigned char *tbs,
435 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
436 DSA_SIG *s = (sig) ? unpack_cp_signature(sig, siglen) : NULL;
440 fprintf(stderr, "R=");
441 BN_print_fp(stderr, s->r);
442 fprintf(stderr, "\nS=");
443 BN_print_fp(stderr, s->s);
444 fprintf(stderr, "\n");
447 ok = gost_ec_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
452 /* ------------- encrypt init -------------------------------------*/
453 /* Generates ephermeral key */
454 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
459 /* --------------- Derive init ------------------------------------*/
460 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
465 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
466 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
468 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
472 memset(data, 0, sizeof(*data));
473 EVP_PKEY_CTX_set_data(ctx, data);
477 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
479 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
484 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
486 struct gost_mac_pmeth_data *dst_data, *src_data;
487 if (!pkey_gost_mac_init(dst)) {
490 src_data = EVP_PKEY_CTX_get_data(src);
491 dst_data = EVP_PKEY_CTX_get_data(dst);
492 if (!src_data || !dst_data)
495 *dst_data = *src_data;
499 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
501 struct gost_mac_pmeth_data *data =
502 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
505 case EVP_PKEY_CTRL_MD:
507 int nid = EVP_MD_type((const EVP_MD *)p2);
508 if (nid != NID_id_Gost28147_89_MAC && nid != NID_gost_mac_12) {
509 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
510 GOST_R_INVALID_DIGEST_TYPE);
513 data->md = (EVP_MD *)p2;
517 case EVP_PKEY_CTRL_GET_MD:
518 *(const EVP_MD **)p2 = data->md;
521 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
522 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
523 case EVP_PKEY_CTRL_PKCS7_SIGN:
525 case EVP_PKEY_CTRL_SET_MAC_KEY:
527 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
531 memcpy(data->key, p2, 32);
534 case EVP_PKEY_CTRL_DIGESTINIT:
536 EVP_MD_CTX *mctx = p2;
538 if (!data->key_set) {
539 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
541 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
542 GOST_R_MAC_KEY_NOT_SET);
545 key = EVP_PKEY_get0(pkey);
547 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
548 GOST_R_MAC_KEY_NOT_SET);
554 return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32, key);
560 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
561 const char *type, const char *value)
563 if (strcmp(type, key_ctrl_string) == 0) {
564 if (strlen(value) != 32) {
565 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
566 GOST_R_INVALID_MAC_KEY_LENGTH);
569 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
572 if (strcmp(type, hexkey_ctrl_string) == 0) {
575 unsigned char *keybuf = string_to_hex(value, &keylen);
576 if (!keybuf || keylen != 32) {
577 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
578 GOST_R_INVALID_MAC_KEY_LENGTH);
579 OPENSSL_free(keybuf);
582 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
583 OPENSSL_free(keybuf);
590 static int pkey_gost_mac_keygen_base(EVP_PKEY_CTX *ctx,
591 EVP_PKEY *pkey, int mac_nid)
593 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
594 unsigned char *keydata;
595 if (!data || !data->key_set) {
596 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN, GOST_R_MAC_KEY_NOT_SET);
599 keydata = OPENSSL_malloc(32);
602 memcpy(keydata, data->key, 32);
603 EVP_PKEY_assign(pkey, mac_nid, keydata);
607 static int pkey_gost_mac_keygen_12(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
609 return pkey_gost_mac_keygen_base(ctx, pkey, NID_gost_mac_12);
612 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
614 return pkey_gost_mac_keygen_base(ctx, pkey, NID_id_Gost28147_89_MAC);
617 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
622 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
623 size_t *siglen, EVP_MD_CTX *mctx)
625 unsigned int tmpsiglen;
630 tmpsiglen = *siglen; /* for platforms where sizeof(int) !=
637 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
642 /* ----------------------------------------------------------------*/
643 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
645 *pmeth = EVP_PKEY_meth_new(id, flags);
650 case NID_id_GostR3410_2001:
651 EVP_PKEY_meth_set_ctrl(*pmeth,
652 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
653 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
654 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
656 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2001cp_keygen);
658 EVP_PKEY_meth_set_encrypt(*pmeth,
659 pkey_gost_encrypt_init,
660 pkey_GOST_ECcp_encrypt);
661 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST_ECcp_decrypt);
662 EVP_PKEY_meth_set_derive(*pmeth,
663 pkey_gost_derive_init, pkey_gost_ec_derive);
664 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
665 pkey_gost2001_paramgen);
667 case NID_id_GostR3410_2012_256:
668 EVP_PKEY_meth_set_ctrl(*pmeth,
669 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_256);
670 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
671 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
673 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
675 EVP_PKEY_meth_set_encrypt(*pmeth,
676 pkey_gost_encrypt_init,
677 pkey_GOST_ECcp_encrypt);
678 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST_ECcp_decrypt);
679 EVP_PKEY_meth_set_derive(*pmeth,
680 pkey_gost_derive_init, pkey_gost_ec_derive);
681 EVP_PKEY_meth_set_paramgen(*pmeth,
682 pkey_gost_paramgen_init,
683 pkey_gost2012_paramgen);
685 case NID_id_GostR3410_2012_512:
686 EVP_PKEY_meth_set_ctrl(*pmeth,
687 pkey_gost_ctrl, pkey_gost_ec_ctrl_str_512);
688 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost_ec_cp_sign);
689 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost_ec_cp_verify);
691 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost2012cp_keygen);
693 EVP_PKEY_meth_set_encrypt(*pmeth,
694 pkey_gost_encrypt_init,
695 pkey_GOST_ECcp_encrypt);
696 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST_ECcp_decrypt);
697 EVP_PKEY_meth_set_derive(*pmeth,
698 pkey_gost_derive_init, pkey_gost_ec_derive);
699 EVP_PKEY_meth_set_paramgen(*pmeth,
700 pkey_gost_paramgen_init,
701 pkey_gost2012_paramgen);
703 case NID_id_Gost28147_89_MAC:
704 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
705 pkey_gost_mac_ctrl_str);
706 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
707 pkey_gost_mac_signctx);
708 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
709 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
710 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
711 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
713 case NID_gost_mac_12:
714 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
715 pkey_gost_mac_ctrl_str);
716 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
717 pkey_gost_mac_signctx);
718 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen_12);
719 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
720 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
721 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
723 default: /* Unsupported method */
726 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
727 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
729 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
731 * FIXME derive etc...