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>
16 #include "gost_params.h"
18 #include "e_gost_err.h"
19 /*-------init, cleanup, copy - uniform for all algs ---------------*/
20 /* Allocates new gost_pmeth_data structure and assigns it as data */
21 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
23 struct gost_pmeth_data *data;
24 data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
26 memset(data,0,sizeof(struct gost_pmeth_data));
27 EVP_PKEY_CTX_set_data(ctx,data);
31 /* Copies contents of gost_pmeth_data structure */
32 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
34 struct gost_pmeth_data *dst_data,*src_data;
35 if (!pkey_gost_init(dst))
39 src_data = EVP_PKEY_CTX_get_data(src);
40 dst_data = EVP_PKEY_CTX_get_data(dst);
41 *dst_data = *src_data;
42 if (src_data -> eph_seckey)
44 dst_data ->eph_seckey = NULL;
49 /* Frees up gost_pmeth_data structure */
50 static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx)
52 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
53 if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
57 /* --------------------- control functions ------------------------------*/
58 static int pkey_gost_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
60 struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
63 case EVP_PKEY_CTRL_MD:
65 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)
67 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
70 pctx->md = (EVP_MD *)p2;
75 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
76 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
77 case EVP_PKEY_CTRL_PKCS7_SIGN:
80 case EVP_PKEY_CTRL_GOST_PARAMSET:
81 pctx->sign_param_nid = (int)p1;
88 static int pkey_gost_ctrl94cc_str(EVP_PKEY_CTX *ctx,
89 const char *type, const char *value)
91 if(!strcmp(type, param_ctrl_string))
93 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
94 NID_id_GostR3410_94_CryptoPro_A_ParamSet,
100 static int pkey_gost_ctrl01cc_str(EVP_PKEY_CTX *ctx,
101 const char *type, const char *value)
103 if(!strcmp(type, param_ctrl_string))
105 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
106 NID_id_GostR3410_2001_ParamSet_cc,NULL);
111 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
112 const char *type, const char *value)
115 if(!strcmp(type, param_ctrl_string))
121 if (strlen(value) == 1)
123 switch(toupper(value[0]))
126 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
129 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
132 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
135 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
142 else if ((strlen(value) == 2) && (toupper(value[0]) == 'X'))
144 switch (toupper(value[1]))
147 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
150 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
153 param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
162 R3410_params *p = R3410_paramset;
163 param_nid = OBJ_txt2nid(value);
164 if (param_nid == NID_undef)
168 for (;p->nid != NID_undef;p++)
170 if (p->nid == param_nid) break;
172 if (p->nid == NID_undef)
174 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
175 GOST_R_INVALID_PARAMSET);
180 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
186 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
187 const char *type, const char *value)
190 if(!strcmp(type, param_ctrl_string))
196 if (strlen(value) == 1)
198 switch(toupper(value[0]))
201 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
204 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
207 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
210 param_nid = NID_id_GostR3410_2001_TestParamSet;
217 else if ((strlen(value) == 2) && (toupper(value[0]) == 'X'))
219 switch (toupper(value[1]))
222 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
225 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
234 R3410_2001_params *p = R3410_2001_paramset;
235 param_nid = OBJ_txt2nid(value);
236 if (param_nid == NID_undef)
240 for (;p->nid != NID_undef;p++)
242 if (p->nid == param_nid) break;
244 if (p->nid == NID_undef)
246 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
247 GOST_R_INVALID_PARAMSET);
252 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
258 /* --------------------- key generation --------------------------------*/
259 /* Generates GOST 94 key and assigns it setting specified type */
260 static int pkey_gost94_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
262 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
264 if (data->sign_param_nid == NID_undef)
266 if (type== NID_id_GostR3410_94_cc)
268 data->sign_param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
272 GOSTerr(GOST_F_PKEY_GOST94_KEYGEN,
273 GOST_R_NO_PARAMETERS_SET);
278 if (!fill_GOST94_params(dsa,data->sign_param_nid))
283 gost_sign_keygen(dsa);
284 EVP_PKEY_assign(pkey,type,dsa);
288 /* Generates Gost_R3410_94_cc key */
289 static int pkey_gost94cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
291 return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94_cc);
294 /* Generates Gost_R3410_94_cp key */
295 static int pkey_gost94cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
297 return pkey_gost94_keygen(ctx,pkey,NID_id_GostR3410_94);
300 /* Generates GOST_R3410 2001 key and assigns it using specified type */
301 static int pkey_gost01_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey,int type)
303 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
305 if (data->sign_param_nid == NID_undef)
307 if (type == NID_id_GostR3410_2001_cc)
309 data->sign_param_nid = NID_id_GostR3410_2001_ParamSet_cc;
312 GOSTerr(GOST_F_PKEY_GOST01_KEYGEN,
313 GOST_R_NO_PARAMETERS_SET);
318 if (!fill_GOST2001_params(ec,data->sign_param_nid))
325 EVP_PKEY_assign(pkey,type,ec);
329 /* Generates GOST R3410 2001_cc key */
330 static int pkey_gost01cc_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
332 return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001_cc);
335 /* Generates GOST R3410 2001_cp key */
336 static int pkey_gost01cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
338 return pkey_gost01_keygen(ctx,pkey,NID_id_GostR3410_2001);
341 /* ----------- sign callbacks --------------------------------------*/
342 static int pkey_gost94_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
343 const unsigned char *tbs, size_t tbs_len)
345 DSA_SIG *unpacked_sig=NULL;
346 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
347 if (!siglen) return 0;
350 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
353 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
358 return pack_sign_cc(unpacked_sig,32,sig,siglen);
361 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
362 const unsigned char *tbs, size_t tbs_len)
364 DSA_SIG *unpacked_sig=NULL;
365 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
366 if (!siglen) return 0;
369 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
372 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
377 return pack_sign_cp(unpacked_sig,32,sig,siglen);
380 static int pkey_gost01_cc_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
381 const unsigned char *tbs, size_t tbs_len)
383 DSA_SIG *unpacked_sig=NULL;
384 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
385 if (!siglen) return 0;
388 *siglen= 64; /* better to check size of curve order*/
391 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
396 return pack_sign_cc(unpacked_sig,32,sig,siglen);
399 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
400 const unsigned char *tbs, size_t tbs_len)
402 DSA_SIG *unpacked_sig=NULL;
403 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
404 if (!siglen) return 0;
407 *siglen= 64; /* better to check size of curve order*/
410 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
415 return pack_sign_cp(unpacked_sig,32,sig,siglen);
418 /* ------------------- verify callbacks ---------------------------*/
419 static int pkey_gost94_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
420 size_t siglen, const unsigned char *tbs, size_t tbs_len)
423 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
424 DSA_SIG *s=unpack_cc_signature(sig,siglen);
426 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
431 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
432 size_t siglen, const unsigned char *tbs, size_t tbs_len)
435 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
436 DSA_SIG *s=unpack_cp_signature(sig,siglen);
438 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
443 static int pkey_gost01_cc_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
444 size_t siglen, const unsigned char *tbs, size_t tbs_len)
447 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
448 DSA_SIG *s=unpack_cc_signature(sig,siglen);
449 fprintf(stderr,"R=");
450 BN_print_fp(stderr,s->r);
451 fprintf(stderr,"\nS=");
452 BN_print_fp(stderr,s->s);
453 fprintf(stderr,"\n");
455 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
460 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
461 size_t siglen, const unsigned char *tbs, size_t tbs_len)
464 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
465 DSA_SIG *s=unpack_cp_signature(sig,siglen);
467 fprintf(stderr,"R=");
468 BN_print_fp(stderr,s->r);
469 fprintf(stderr,"\nS=");
470 BN_print_fp(stderr,s->s);
471 fprintf(stderr,"\n");
472 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
477 /* ------------- encrypt init -------------------------------------*/
478 /* Generates ephermeral key */
479 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
481 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
482 EVP_PKEY *eph_key = EVP_PKEY_new();
483 EVP_PKEY *old_key =EVP_PKEY_CTX_get0_pkey(ctx);
485 if (data->eph_seckey) EVP_PKEY_free(data->eph_seckey);
486 EVP_PKEY_assign(eph_key,EVP_PKEY_base_id(old_key),NULL);
487 if (!EVP_PKEY_copy_parameters(eph_key,old_key)) return 0;
488 switch (EVP_PKEY_base_id(old_key))
490 case NID_id_GostR3410_2001:
491 case NID_id_GostR3410_2001_cc:
492 gost2001_keygen(EVP_PKEY_get0(eph_key));
494 case NID_id_GostR3410_94:
495 case NID_id_GostR3410_94_cc:
496 gost_sign_keygen(EVP_PKEY_get0(eph_key));
503 data->eph_seckey=eph_key;
507 /* ----------------------------------------------------------------*/
508 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags)
510 *pmeth = EVP_PKEY_meth_new(id, flags);
511 if (!*pmeth) return 0;
515 case NID_id_GostR3410_94_cc:
516 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94cc_str);
517 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cc_keygen);
518 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cc_sign);
519 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cc_verify);
520 EVP_PKEY_meth_set_encrypt(*pmeth,
521 pkey_gost_encrypt_init, pkey_GOST94cc_encrypt);
522 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cc_decrypt);
524 case NID_id_GostR3410_94:
525 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
526 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
527 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
528 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
529 EVP_PKEY_meth_set_encrypt(*pmeth,
530 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
531 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
535 case NID_id_GostR3410_2001_cc:
536 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01cc_str);
537 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cc_sign);
538 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cc_verify);
540 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cc_keygen);
542 EVP_PKEY_meth_set_encrypt(*pmeth,
543 pkey_gost_encrypt_init, pkey_GOST01cc_encrypt);
544 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cc_decrypt);
546 /* There is intentionally no break here */
547 case NID_id_GostR3410_2001:
548 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
549 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
550 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
552 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
554 EVP_PKEY_meth_set_encrypt(*pmeth,
555 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
556 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
558 default: //Unsupported method
561 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
562 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
564 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
565 //FIXME derive etc...