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);
27 data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
30 memset(data, 0, sizeof(struct gost_pmeth_data));
31 if (pkey && EVP_PKEY_get0(pkey)) {
32 switch (EVP_PKEY_base_id(pkey)) {
33 case NID_id_GostR3410_94:
34 data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey));
36 case NID_id_GostR3410_2001:
37 data->sign_param_nid =
38 EC_GROUP_get_curve_name(EC_KEY_get0_group
39 (EVP_PKEY_get0((EVP_PKEY *)pkey)));
45 EVP_PKEY_CTX_set_data(ctx, data);
49 /* Copies contents of gost_pmeth_data structure */
50 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
52 struct gost_pmeth_data *dst_data, *src_data;
53 if (!pkey_gost_init(dst)) {
56 src_data = EVP_PKEY_CTX_get_data(src);
57 dst_data = EVP_PKEY_CTX_get_data(dst);
58 *dst_data = *src_data;
59 if (src_data->shared_ukm) {
60 dst_data->shared_ukm = NULL;
65 /* Frees up gost_pmeth_data structure */
66 static void pkey_gost_cleanup(EVP_PKEY_CTX *ctx)
68 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
70 OPENSSL_free(data->shared_ukm);
74 /* --------------------- control functions ------------------------------*/
75 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
77 struct gost_pmeth_data *pctx =
78 (struct gost_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
80 case EVP_PKEY_CTRL_MD:
82 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) {
83 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
86 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)
133 if (!strcmp(type, param_ctrl_string)) {
137 if (strlen(value) == 1) {
138 switch (toupper((unsigned char)value[0])) {
140 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
143 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
146 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
149 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;
172 R3410_params *p = R3410_paramset;
173 param_nid = OBJ_txt2nid(value);
174 if (param_nid == NID_undef) {
177 for (; p->nid != NID_undef; p++) {
178 if (p->nid == param_nid)
181 if (p->nid == NID_undef) {
182 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR, GOST_R_INVALID_PARAMSET);
187 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
193 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
194 const char *type, const char *value)
197 if (!strcmp(type, param_ctrl_string)) {
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;
219 } else if ((strlen(value) == 2)
220 && (toupper((unsigned char)value[0]) == 'X')) {
221 switch (toupper((unsigned char)value[1])) {
223 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
226 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
233 R3410_2001_params *p = R3410_2001_paramset;
234 param_nid = OBJ_txt2nid(value);
235 if (param_nid == NID_undef) {
238 for (; p->nid != NID_undef; p++) {
239 if (p->nid == param_nid)
242 if (p->nid == NID_undef) {
243 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR, GOST_R_INVALID_PARAMSET);
248 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
254 /* --------------------- key generation --------------------------------*/
256 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx)
261 static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
263 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
265 if (data->sign_param_nid == NID_undef) {
266 GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
270 if (!fill_GOST94_params(dsa, data->sign_param_nid)) {
274 EVP_PKEY_assign(pkey, NID_id_GostR3410_94, dsa);
278 static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
280 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
283 if (data->sign_param_nid == NID_undef) {
284 GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN, GOST_R_NO_PARAMETERS_SET);
289 if (!fill_GOST2001_params(ec, data->sign_param_nid)) {
293 EVP_PKEY_assign(pkey, NID_id_GostR3410_2001, ec);
297 /* Generates Gost_R3410_94_cp key */
298 static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
301 if (!pkey_gost94_paramgen(ctx, pkey))
303 dsa = EVP_PKEY_get0(pkey);
304 gost_sign_keygen(dsa);
308 /* Generates GOST_R3410 2001 key and assigns it using specified type */
309 static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
312 if (!pkey_gost01_paramgen(ctx, pkey))
314 ec = EVP_PKEY_get0(pkey);
319 /* ----------- sign callbacks --------------------------------------*/
321 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
322 size_t *siglen, const unsigned char *tbs,
325 DSA_SIG *unpacked_sig = NULL;
326 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
330 *siglen = 64; /* better to check size of pkey->pkey.dsa-q */
333 unpacked_sig = gost_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
337 return pack_sign_cp(unpacked_sig, 32, sig, siglen);
340 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
341 size_t *siglen, const unsigned char *tbs,
344 DSA_SIG *unpacked_sig = NULL;
345 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
349 *siglen = 64; /* better to check size of curve order */
352 unpacked_sig = gost2001_do_sign(tbs, tbs_len, EVP_PKEY_get0(pkey));
356 return pack_sign_cp(unpacked_sig, 32, sig, siglen);
359 /* ------------------- verify callbacks ---------------------------*/
361 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
362 size_t siglen, const unsigned char *tbs,
366 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
367 DSA_SIG *s = unpack_cp_signature(sig, siglen);
371 ok = gost_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
376 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
377 size_t siglen, const unsigned char *tbs,
381 EVP_PKEY *pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
382 DSA_SIG *s = unpack_cp_signature(sig, siglen);
386 fprintf(stderr, "R=");
387 BN_print_fp(stderr, s->r);
388 fprintf(stderr, "\nS=");
389 BN_print_fp(stderr, s->s);
390 fprintf(stderr, "\n");
393 ok = gost2001_do_verify(tbs, tbs_len, s, EVP_PKEY_get0(pub_key));
398 /* ------------- encrypt init -------------------------------------*/
399 /* Generates ephermeral key */
400 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
405 /* --------------- Derive init ------------------------------------*/
406 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
411 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
412 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
414 struct gost_mac_pmeth_data *data;
415 data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data));
418 memset(data, 0, sizeof(struct gost_mac_pmeth_data));
419 EVP_PKEY_CTX_set_data(ctx, data);
423 static void pkey_gost_mac_cleanup(EVP_PKEY_CTX *ctx)
425 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
429 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
431 struct gost_mac_pmeth_data *dst_data, *src_data;
432 if (!pkey_gost_mac_init(dst)) {
435 src_data = EVP_PKEY_CTX_get_data(src);
436 dst_data = EVP_PKEY_CTX_get_data(dst);
437 *dst_data = *src_data;
441 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
443 struct gost_mac_pmeth_data *data =
444 (struct gost_mac_pmeth_data *)EVP_PKEY_CTX_get_data(ctx);
447 case EVP_PKEY_CTRL_MD:
449 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC) {
450 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
451 GOST_R_INVALID_DIGEST_TYPE);
454 data->md = (EVP_MD *)p2;
459 case EVP_PKEY_CTRL_GET_MD:
460 *(const EVP_MD **)p2 = data->md;
463 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
464 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
465 case EVP_PKEY_CTRL_PKCS7_SIGN:
467 case EVP_PKEY_CTRL_SET_MAC_KEY:
469 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_MAC_KEY_LENGTH);
473 memcpy(data->key, p2, 32);
476 case EVP_PKEY_CTRL_DIGESTINIT:
478 EVP_MD_CTX *mctx = p2;
480 if (!data->key_set) {
481 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
483 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
484 GOST_R_MAC_KEY_NOT_SET);
487 key = EVP_PKEY_get0(pkey);
489 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
490 GOST_R_MAC_KEY_NOT_SET);
496 return mctx->digest->md_ctrl(mctx, EVP_MD_CTRL_SET_KEY, 32, key);
502 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
503 const char *type, const char *value)
505 if (!strcmp(type, key_ctrl_string)) {
506 if (strlen(value) != 32) {
507 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
508 GOST_R_INVALID_MAC_KEY_LENGTH);
511 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
514 if (!strcmp(type, hexkey_ctrl_string)) {
517 unsigned char *keybuf = string_to_hex(value, &keylen);
519 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
520 GOST_R_INVALID_MAC_KEY_LENGTH);
521 OPENSSL_free(keybuf);
524 ret = pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY, 32, keybuf);
525 OPENSSL_free(keybuf);
532 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
534 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
535 unsigned char *keydata;
536 if (!data->key_set) {
537 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN, GOST_R_MAC_KEY_NOT_SET);
540 keydata = OPENSSL_malloc(32);
543 memcpy(keydata, data->key, 32);
544 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
548 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
553 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig,
554 size_t *siglen, EVP_MD_CTX *mctx)
556 unsigned int tmpsiglen = *siglen; /* for platforms where
557 * sizeof(int)!=sizeof(size_t) */
563 ret = EVP_DigestFinal_ex(mctx, sig, &tmpsiglen);
568 /* ----------------------------------------------------------------*/
569 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth, int flags)
571 *pmeth = EVP_PKEY_meth_new(id, flags);
576 case NID_id_GostR3410_94:
577 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl94_str);
578 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost94cp_keygen);
579 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
580 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
581 EVP_PKEY_meth_set_encrypt(*pmeth,
582 pkey_gost_encrypt_init,
583 pkey_GOST94cp_encrypt);
584 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
585 EVP_PKEY_meth_set_derive(*pmeth,
586 pkey_gost_derive_init, pkey_gost94_derive);
587 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
588 pkey_gost94_paramgen);
590 case NID_id_GostR3410_2001:
591 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_ctrl, pkey_gost_ctrl01_str);
592 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
593 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
595 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
597 EVP_PKEY_meth_set_encrypt(*pmeth,
598 pkey_gost_encrypt_init,
599 pkey_GOST01cp_encrypt);
600 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
601 EVP_PKEY_meth_set_derive(*pmeth,
602 pkey_gost_derive_init, pkey_gost2001_derive);
603 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,
604 pkey_gost01_paramgen);
606 case NID_id_Gost28147_89_MAC:
607 EVP_PKEY_meth_set_ctrl(*pmeth, pkey_gost_mac_ctrl,
608 pkey_gost_mac_ctrl_str);
609 EVP_PKEY_meth_set_signctx(*pmeth, pkey_gost_mac_signctx_init,
610 pkey_gost_mac_signctx);
611 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost_mac_keygen);
612 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_mac_init);
613 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_mac_cleanup);
614 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_mac_copy);
616 default: /* Unsupported method */
619 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
620 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
622 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
624 * FIXME derive etc...