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>
22 #include "e_gost_err.h"
23 /*-------init, cleanup, copy - uniform for all algs ---------------*/
24 /* Allocates new gost_pmeth_data structure and assigns it as data */
25 static int pkey_gost_init(EVP_PKEY_CTX *ctx) {
26 struct gost_pmeth_data *data;
27 data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
29 memset(data,0,sizeof(struct gost_pmeth_data));
30 EVP_PKEY_CTX_set_data(ctx,data);
33 /* Copies contents of gost_pmeth_data structure */
34 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
36 struct gost_pmeth_data *dst_data,*src_data;
37 if (!pkey_gost_init(dst)) {
40 src_data = EVP_PKEY_CTX_get_data(src);
41 dst_data = EVP_PKEY_CTX_get_data(dst);
42 *dst_data = *src_data;
43 if (src_data -> eph_seckey) {
44 dst_data ->eph_seckey = NULL;
48 /* Frees up gost_pmeth_data structure */
49 static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx) {
50 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
51 if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
54 /* --------------------- control functions ------------------------------*/
55 static int pkey_gost_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
57 struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
60 case EVP_PKEY_CTRL_MD:
62 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94) {
63 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
66 pctx->md = (EVP_MD *)p2;
71 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
72 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
73 case EVP_PKEY_CTRL_PKCS7_SIGN:
76 case EVP_PKEY_CTRL_GOST_PARAMSET:
77 pctx->sign_param_nid = (int)p1;
78 pctx->crypt_param_nid= (int)p2;
85 static int pkey_gost_ctrl94cc_str(EVP_PKEY_CTX *ctx,
86 const char *type, const char *value)
88 if(!strcmp(type, param_ctrl_string)) {
89 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
90 NID_id_GostR3410_94_CryptoPro_A_ParamSet,
91 (void *)NID_id_Gost28147_89_cc);
96 static int pkey_gost_ctrl01cc_str(EVP_PKEY_CTX *ctx,
97 const char *type, const char *value)
99 if(!strcmp(type, param_ctrl_string)) {
100 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
101 NID_id_GostR3410_2001_ParamSet_cc,
103 NID_id_Gost28147_89_cc);
107 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
108 const char *type, const char *value)
111 if(!strcmp(type, param_ctrl_string)) {
115 if (strlen(value) == 1) {
116 switch(toupper(value[0])) {
118 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
121 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
124 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
127 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
133 } else if ((strlen(value) == 2) && (toupper(value[0]) == 'X')) {
134 switch (toupper(value[1])) {
136 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
139 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
142 param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
149 R3410_params *p = R3410_paramset;
150 param_nid = OBJ_txt2nid(value);
151 if (param_nid == NID_undef) {
154 for (;p->nid != NID_undef;p++) {
155 if (p->nid == param_nid) break;
157 if (p->nid == NID_undef) {
158 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
159 GOST_R_INVALID_PARAMSET);
164 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
165 param_nid, (void *)NID_id_Gost28147_89_CryptoPro_A_ParamSet);
170 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
171 const char *type, const char *value)
174 if(!strcmp(type, param_ctrl_string)) {
178 if (strlen(value) == 1) {
179 switch(toupper(value[0])) {
181 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
184 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
187 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
190 param_nid = NID_id_GostR3410_2001_TestParamSet;
196 } else if ((strlen(value) == 2) && (toupper(value[0]) == 'X')) {
197 switch (toupper(value[1])) {
199 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
202 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
209 R3410_2001_params *p = R3410_2001_paramset;
210 param_nid = OBJ_txt2nid(value);
211 if (param_nid == NID_undef) {
214 for (;p->nid != NID_undef;p++) {
215 if (p->nid == param_nid) break;
217 if (p->nid == NID_undef) {
218 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
219 GOST_R_INVALID_PARAMSET);
224 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
225 param_nid, (void *)NID_id_Gost28147_89_CryptoPro_A_ParamSet);
229 /* --------------------- key generation --------------------------------*/
230 /* Generates GOST 94 key and assigns it setting specified type */
231 static int pkey_gost94_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
233 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
235 if (data->sign_param_nid == NID_undef) {
236 if (type== NID_id_GostR3410_94_cc) {
237 data->sign_param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
239 GOSTerr(GOST_F_PKEY_GOST94_KEYGEN,
240 GOST_R_NO_PARAMETERS_SET);
245 if (!fill_GOST94_params(dsa,data->sign_param_nid)) {
249 gost_sign_keygen(dsa);
250 EVP_PKEY_assign(pkey,type,dsa);
253 /* Generates Gost_R3410_94_cc key */
254 static int pkey_gost94cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
255 return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94_cc);
258 /* Generates Gost_R3410_94_cp key */
259 static int pkey_gost94cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
260 return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94);
262 /* Generates GOST_R3410 2001 key and assigns it using specified type */
263 static int pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
265 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
267 if (data->sign_param_nid == NID_undef) {
268 if (type == NID_id_GostR3410_2001_cc) {
269 data->sign_param_nid = NID_id_GostR3410_2001_ParamSet_cc;
271 GOSTerr(GOST_F_PKEY_GOST01_KEYGEN,
272 GOST_R_NO_PARAMETERS_SET);
277 if (!fill_GOST2001_params(ec,data->sign_param_nid)) {
283 EVP_PKEY_assign(pkey,type,ec);
286 /* Generates GOST R3410 2001_cc key */
287 static int pkey_gost01cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
288 return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001_cc);
291 /* Generates GOST R3410 2001_cp key */
292 static int pkey_gost01cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey) {
293 return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001);
295 /* ----------- sign callbacks --------------------------------------*/
296 static int pkey_gost94_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
297 const unsigned char *tbs, size_t tbs_len)
299 DSA_SIG *unpacked_sig=NULL;
300 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
301 if (!siglen) return 0;
304 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
307 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
312 return pack_sign_cc(unpacked_sig,32,sig,siglen);
316 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
317 const unsigned char *tbs, size_t tbs_len)
319 DSA_SIG *unpacked_sig=NULL;
320 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
321 if (!siglen) return 0;
324 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
327 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
331 return pack_sign_cp(unpacked_sig,32,sig,siglen);
335 static int pkey_gost01_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
336 const unsigned char *tbs, size_t tbs_len)
338 DSA_SIG *unpacked_sig=NULL;
339 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
340 if (!siglen) return 0;
343 *siglen= 64; /* better to check size of curve order*/
346 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
350 return pack_sign_cc(unpacked_sig,32,sig,siglen);
352 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
353 const unsigned char *tbs, size_t tbs_len)
355 DSA_SIG *unpacked_sig=NULL;
356 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
357 if (!siglen) return 0;
360 *siglen= 64; /* better to check size of curve order*/
363 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
367 return pack_sign_cp(unpacked_sig,32,sig,siglen);
369 /* ------------------- verify callbacks ---------------------------*/
370 static int pkey_gost94_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
371 size_t siglen, const unsigned char *tbs, size_t tbs_len)
374 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
375 DSA_SIG *s=unpack_cc_signature(sig,siglen);
377 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
382 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
383 size_t siglen, const unsigned char *tbs, size_t tbs_len)
386 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
387 DSA_SIG *s=unpack_cp_signature(sig,siglen);
389 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
393 static int pkey_gost01_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
394 size_t siglen, const unsigned char *tbs, size_t tbs_len)
397 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
398 DSA_SIG *s=unpack_cc_signature(sig,siglen);
399 fprintf(stderr,"R=");
400 BN_print_fp(stderr,s->r);
401 fprintf(stderr,"\nS=");
402 BN_print_fp(stderr,s->s);
403 fprintf(stderr,"\n");
405 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
410 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
411 size_t siglen, const unsigned char *tbs, size_t tbs_len)
414 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
415 DSA_SIG *s=unpack_cp_signature(sig,siglen);
417 fprintf(stderr,"R=");
418 BN_print_fp(stderr,s->r);
419 fprintf(stderr,"\nS=");
420 BN_print_fp(stderr,s->s);
421 fprintf(stderr,"\n");
422 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
426 /* ------------- encrypt init -------------------------------------*/
427 /* Generates ephermeral key */
428 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
430 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
431 EVP_PKEY *eph_key = EVP_PKEY_new();
432 EVP_PKEY *old_key =EVP_PKEY_CTX_get0_pkey(ctx);
434 if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
435 EVP_PKEY_assign(eph_key,EVP_PKEY_base_id(old_key),NULL);
436 if (!EVP_PKEY_copy_parameters(eph_key,old_key)) return 0;
437 switch (EVP_PKEY_base_id(old_key)) {
438 case NID_id_GostR3410_2001:
439 case NID_id_GostR3410_2001_cc:
440 gost2001_keygen(EVP_PKEY_get0(eph_key));
442 case NID_id_GostR3410_94:
443 case NID_id_GostR3410_94_cc:
444 gost_sign_keygen(EVP_PKEY_get0(eph_key));
451 data->eph_seckey=eph_key;
454 /* ----------------------------------------------------------------*/
455 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags) {
456 *pmeth = EVP_PKEY_meth_new(id, flags);
457 if (!*pmeth) return 0;
460 case NID_id_GostR3410_94_cc:
461 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94cc_str);
462 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cc_keygen);
463 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cc_sign);
464 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cc_verify);
465 EVP_PKEY_meth_set_encrypt(*pmeth,
466 pkey_gost_encrypt_init, pkey_GOST94cc_encrypt);
467 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cc_decrypt);
469 case NID_id_GostR3410_94:
470 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
471 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
472 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
473 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
474 EVP_PKEY_meth_set_encrypt(*pmeth,
475 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
476 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
480 case NID_id_GostR3410_2001_cc:
481 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01cc_str);
482 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cc_sign);
483 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cc_verify);
485 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cc_keygen);
487 EVP_PKEY_meth_set_encrypt(*pmeth,
488 pkey_gost_encrypt_init, pkey_GOST01cc_encrypt);
489 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cc_decrypt);
491 /* There is intentionally no break here */
492 case NID_id_GostR3410_2001:
493 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
494 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
495 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
497 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
499 EVP_PKEY_meth_set_encrypt(*pmeth,
500 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
501 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
503 default: //Unsupported method
506 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
507 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
509 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
510 //FIXME derive etc...