1 /**********************************************************************
3 * Copyright (c) 2005-2006 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 0.9.9 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 */
18 #include "gost_params.h"
20 #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_94:
35 data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey));
37 case NID_id_GostR3410_2001:
38 data->sign_param_nid =
39 EC_GROUP_get_curve_name(EC_KEY_get0_group
40 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
46 EVP_PKEY_CTX_set_data(ctx, data);
50 /* Copies contents of gost_pmeth_data structure */
51 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
53 struct gost_pmeth_data *dst_data, *src_data;
54 if (!pkey_gost_init(dst)) {
57 src_data = EVP_PKEY_CTX_get_data(src);
58 dst_data = EVP_PKEY_CTX_get_data(dst);
59 *dst_data = *src_data;
60 if (src_data->shared_ukm) {
61 dst_data->shared_ukm = NULL;
66 /* Frees up gost_pmeth_data structure */
67 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
69 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
71 OPENSSL_free(data->shared_ukm);
75 /* --------------------- control functions ------------------------------*/
76 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
78 struct gost_pmeth_data *pctx =
79 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
81 case EVP_PKEY_CTRL_MD:
83 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) {
84 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
87 pctx->md = (EVP_MD *)p2;
91 case EVP_PKEY_CTRL_GET_MD:
92 *(const EVP_MD **)p2 = pctx->md;
95 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
96 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
97 case EVP_PKEY_CTRL_PKCS7_SIGN:
98 case EVP_PKEY_CTRL_DIGESTINIT:
99 #ifndef OPENSSL_NO_CMS
100 case EVP_PKEY_CTRL_CMS_ENCRYPT:
101 case EVP_PKEY_CTRL_CMS_DECRYPT:
102 case EVP_PKEY_CTRL_CMS_SIGN:
106 case EVP_PKEY_CTRL_GOST_PARAMSET:
107 pctx->sign_param_nid = (int)p1;
109 case EVP_PKEY_CTRL_SET_IV:
110 pctx->shared_ukm = OPENSSL_malloc((int)p1);
111 if (pctx->shared_ukm == NULL) {
112 GOSTerr(GOST_F_PKEY_GOST_CTRL, ERR_R_MALLOC_FAILURE);
115 memcpy(pctx->shared_ukm, p2, (int)p1);
117 case EVP_PKEY_CTRL_PEER_KEY:
118 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
120 if (p1 == 2) /* TLS: peer key used? */
121 return pctx->peer_key_used;
122 if (p1 == 3) /* TLS: peer key used! */
123 return (pctx->peer_key_used = 1);
129 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
130 const char *type, const char *value)
134 if (strcmp(type, param_ctrl_string) == 0) {
138 if (strlen(value) == 1) {
139 switch (toupper((unsigned char)value[0])) {
141 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
144 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
147 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
150 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
155 } else if ((strlen(value) == 2)
156 && (toupper((unsigned char)value[0]) == 'X')) {
157 switch (toupper((unsigned char)value[1])) {
159 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
162 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
165 param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
171 R3410_params *p = R3410_paramset;
172 param_nid = OBJ_txt2nid(value);
173 if (param_nid == NID_undef) {
176 for (; p->nid != NID_undef; p++) {
177 if (p->nid == param_nid)
180 if (p->nid == NID_undef) {
181 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR, GOST_R_INVALID_PARAMSET);
186 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
192 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
193 const char *type, const char *value)
197 if (strcmp(type, param_ctrl_string) == 0) {
201 if (strlen(value) == 1) {
202 switch (toupper((unsigned char)value[0])) {
204 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
207 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
210 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
213 param_nid = NID_id_GostR3410_2001_TestParamSet;
218 } else if ((strlen(value) == 2)
219 && (toupper((unsigned char)value[0]) == 'X')) {
220 switch (toupper((unsigned char)value[1])) {
222 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
225 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
231 R3410_2001_params *p = R3410_2001_paramset;
232 param_nid = OBJ_txt2nid(value);
233 if (param_nid == NID_undef) {
236 for (; p->nid != NID_undef; p++) {
237 if (p->nid == param_nid)
240 if (p->nid == NID_undef) {
241 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR, GOST_R_INVALID_PARAMSET);
246 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
252 /* --------------------- key generation --------------------------------*/
254 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
259 static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
261 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
263 if (data->sign_param_nid == NID_undef) {
264 GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
268 if (!fill_GOST94_params(dsa, data->sign_param_nid)) {
272 EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa);
276 static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
278 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
281 if (data->sign_param_nid == NID_undef) {
282 GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
287 if (!fill_GOST2001_params(ec, data->sign_param_nid)) {
291 EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec);
295 /* Generates Gost_R3410_94_cp key */
296 static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
299 if (!pkey_gost94_paramgen(ctx, pkey))
301 dsa = EVP_PKEY_get0(pkey);
302 gost_sign_keygen(dsa);
306 /* Generates GOST_R3410 2001 key and assigns it using specified type */
307 static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
310 if (!pkey_gost01_paramgen(ctx, pkey))
312 ec = EVP_PKEY_get0(pkey);
317 /* ----------- sign callbacks --------------------------------------*/
319 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
320 size_t *siglen, const unsigned char *tbs,
323 DSA_SIG *unpacked_sig = NULL;
324 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
328 *siglen = 64; /* better to check size of pkey->pkey.dsa-q */
331 unpacked_sig = gost_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
335 return pack_sign_cp(unpacked_sig, 32, sig, siglen);
338 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
339 size_t *siglen, const unsigned char *tbs,
342 DSA_SIG *unpacked_sig = NULL;
343 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
347 *siglen = 64; /* better to check size of curve order */
350 unpacked_sig = gost2001_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
354 return pack_sign_cp(unpacked_sig, 32, sig, siglen);
357 /* ------------------- verify callbacks ---------------------------*/
359 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
360 size_t siglen, const unsigned char *tbs,
364 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
365 DSA_SIG *s = unpack_cp_signature(sig, siglen);
369 ok = gost_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
374 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
375 size_t siglen, const unsigned char *tbs,
379 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
380 DSA_SIG *s = unpack_cp_signature(sig, siglen);
384 fprintf(stderr, "R=");
385 BN_print_fp(stderr, s->r);
386 fprintf(stderr, "\nS=");
387 BN_print_fp(stderr, s->s);
388 fprintf(stderr, "\n");
391 ok = gost2001_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
396 /* ------------- encrypt init -------------------------------------*/
397 /* Generates ephermeral key */
398 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
403 /* --------------- Derive init ------------------------------------*/
404 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
409 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
410 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
412 struct gost_mac_pmeth_data *data = OPENSSL_malloc(sizeof(*data));
416 memset(data, 0, sizeof(*data));
417 EVP_PKEY_CTX_set_data(ctx, data);
421 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
423 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
427 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
429 struct gost_mac_pmeth_data *dst_data, *src_data;
430 if (!pkey_gost_mac_init(dst)) {
433 src_data = EVP_PKEY_CTX_get_data(src);
434 dst_data = EVP_PKEY_CTX_get_data(dst);
435 *dst_data = *src_data;
439 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
441 struct gost_mac_pmeth_data *data =
442 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
445 case EVP_PKEY_CTRL_MD:
447 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC) {
448 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
449 GOST_R_INVALID_DIGEST_TYPE);
452 data->md = (EVP_MD *)p2;
456 case EVP_PKEY_CTRL_GET_MD:
457 *(const EVP_MD **)p2 = data->md;
460 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
461 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
462 case EVP_PKEY_CTRL_PKCS7_SIGN:
464 case EVP_PKEY_CTRL_SET_MAC_KEY:
466 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
470 memcpy(data->key, p2, 32);
473 case EVP_PKEY_CTRL_DIGESTINIT:
475 EVP_MD_CTX *mctx = p2;
477 if (!data->key_set) {
478 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
480 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
481 GOST_R_MAC_KEY_NOT_SET);
484 key = EVP_PKEY_get0(pkey);
486 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
487 GOST_R_MAC_KEY_NOT_SET);
493 return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32, key);
499 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
500 const char *type, const char *value)
502 if (strcmp(type, key_ctrl_string) == 0) {
503 if (strlen(value) != 32) {
504 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
505 GOST_R_INVALID_MAC_KEY_LENGTH);
508 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
511 if (strcmp(type, hexkey_ctrl_string) == 0) {
514 unsigned char *keybuf = string_to_hex(value, &keylen);
515 if (!keybuf || keylen != 32) {
516 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
517 GOST_R_INVALID_MAC_KEY_LENGTH);
518 OPENSSL_free(keybuf);
521 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
522 OPENSSL_free(keybuf);
529 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
531 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
532 unsigned char *keydata;
533 if (!data->key_set) {
534 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN, GOST_R_MAC_KEY_NOT_SET);
537 keydata = OPENSSL_malloc(32);
540 memcpy(keydata, data->key, 32);
541 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
545 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
550 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
551 size_t *siglen, EVP_MD_CTX *mctx)
553 unsigned int tmpsiglen = *siglen; /* for platforms where
554 * sizeof(int)!=sizeof(size_t) */
560 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
565 /* ----------------------------------------------------------------*/
566 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
568 *pmeth = EVP_PKEY_meth_new(id, flags);
573 case NID_id_GostR3410_94:
574 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl94_str);
575 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost94cp_keygen);
576 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
577 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
578 EVP_PKEY_meth_set_encrypt(*pmeth,
579 pkey_gost_encrypt_init,
580 pkey_GOST94cp_encrypt);
581 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
582 EVP_PKEY_meth_set_derive(*pmeth,
583 pkey_gost_derive_init, pkey_gost94_derive);
584 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
585 pkey_gost94_paramgen);
587 case NID_id_GostR3410_2001:
588 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl01_str);
589 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
590 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
592 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
594 EVP_PKEY_meth_set_encrypt(*pmeth,
595 pkey_gost_encrypt_init,
596 pkey_GOST01cp_encrypt);
597 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
598 EVP_PKEY_meth_set_derive(*pmeth,
599 pkey_gost_derive_init, pkey_gost2001_derive);
600 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
601 pkey_gost01_paramgen);
603 case NID_id_Gost28147_89_MAC:
604 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
605 pkey_gost_mac_ctrl_str);
606 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
607 pkey_gost_mac_signctx);
608 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
609 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
610 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
611 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
613 default: /* Unsupported method */
616 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
617 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
619 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
621 * FIXME derive etc...