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/x509v3.h> /*For string_to_hex */
17 #include "gost_params.h"
19 #include "e_gost_err.h"
20 /*-------init, cleanup, copy - uniform for all algs ---------------*/
21 /* Allocates new gost_pmeth_data structure and assigns it as data */
22 static int pkey_gost_init(EVP_PKEY_CTX *ctx)
24 struct gost_pmeth_data *data;
25 data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
27 memset(data,0,sizeof(struct gost_pmeth_data));
28 EVP_PKEY_CTX_set_data(ctx,data);
32 /* Copies contents of gost_pmeth_data structure */
33 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
35 struct gost_pmeth_data *dst_data,*src_data;
36 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 -> shared_ukm) {
44 dst_data->shared_ukm=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->shared_ukm) OPENSSL_free(data->shared_ukm);
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;
83 case EVP_PKEY_CTRL_SET_IV:
84 pctx->shared_ukm=OPENSSL_malloc((int)p1);
85 memcpy(pctx->shared_ukm,p2,(int) p1);
93 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
94 const char *type, const char *value)
97 if(!strcmp(type, param_ctrl_string))
103 if (strlen(value) == 1)
105 switch(toupper(value[0]))
108 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
111 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
114 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
117 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
124 else if ((strlen(value) == 2) && (toupper(value[0]) == 'X'))
126 switch (toupper(value[1]))
129 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
132 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
135 param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
144 R3410_params *p = R3410_paramset;
145 param_nid = OBJ_txt2nid(value);
146 if (param_nid == NID_undef)
150 for (;p->nid != NID_undef;p++)
152 if (p->nid == param_nid) break;
154 if (p->nid == NID_undef)
156 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
157 GOST_R_INVALID_PARAMSET);
162 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
168 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
169 const char *type, const char *value)
172 if(!strcmp(type, param_ctrl_string))
178 if (strlen(value) == 1)
180 switch(toupper(value[0]))
183 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
186 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
189 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
192 param_nid = NID_id_GostR3410_2001_TestParamSet;
199 else if ((strlen(value) == 2) && (toupper(value[0]) == 'X'))
201 switch (toupper(value[1]))
204 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
207 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
216 R3410_2001_params *p = R3410_2001_paramset;
217 param_nid = OBJ_txt2nid(value);
218 if (param_nid == NID_undef)
222 for (;p->nid != NID_undef;p++)
224 if (p->nid == param_nid) break;
226 if (p->nid == NID_undef)
228 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
229 GOST_R_INVALID_PARAMSET);
234 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
240 /* --------------------- key generation --------------------------------*/
243 /* Generates Gost_R3410_94_cp key */
244 static int pkey_gost94cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
246 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
248 if (data->sign_param_nid == NID_undef)
250 GOSTerr(GOST_F_PKEY_GOST94_KEYGEN,
251 GOST_R_NO_PARAMETERS_SET);
255 if (!fill_GOST94_params(dsa,data->sign_param_nid))
260 gost_sign_keygen(dsa);
261 EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa);
265 /* Generates GOST_R3410 2001 key and assigns it using specified type */
266 static int pkey_gost01cp_keygen (EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
268 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
270 if (data->sign_param_nid == NID_undef)
272 GOSTerr(GOST_F_PKEY_GOST01_KEYGEN,
273 GOST_R_NO_PARAMETERS_SET);
277 if (!fill_GOST2001_params(ec,data->sign_param_nid))
284 EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec);
290 /* ----------- sign callbacks --------------------------------------*/
292 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
293 const unsigned char *tbs, size_t tbs_len)
295 DSA_SIG *unpacked_sig=NULL;
296 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
297 if (!siglen) return 0;
300 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
303 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
308 return pack_sign_cp(unpacked_sig,32,sig,siglen);
311 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
312 const unsigned char *tbs, size_t tbs_len)
314 DSA_SIG *unpacked_sig=NULL;
315 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
316 if (!siglen) return 0;
319 *siglen= 64; /* better to check size of curve order*/
322 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
327 return pack_sign_cp(unpacked_sig,32,sig,siglen);
330 /* ------------------- verify callbacks ---------------------------*/
332 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
333 size_t siglen, const unsigned char *tbs, size_t tbs_len)
336 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
337 DSA_SIG *s=unpack_cp_signature(sig,siglen);
339 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
345 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
346 size_t siglen, const unsigned char *tbs, size_t tbs_len)
349 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
350 DSA_SIG *s=unpack_cp_signature(sig,siglen);
353 fprintf(stderr,"R=");
354 BN_print_fp(stderr,s->r);
355 fprintf(stderr,"\nS=");
356 BN_print_fp(stderr,s->s);
357 fprintf(stderr,"\n");
359 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
364 /* ------------- encrypt init -------------------------------------*/
365 /* Generates ephermeral key */
366 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
370 /* --------------- Derive init ------------------------------------*/
371 int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
375 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
376 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
378 struct gost_mac_pmeth_data *data;
379 data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data));
381 memset(data,0,sizeof(struct gost_mac_pmeth_data));
382 EVP_PKEY_CTX_set_data(ctx,data);
385 static void pkey_gost_mac_cleanup (EVP_PKEY_CTX *ctx)
387 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
390 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
392 struct gost_mac_pmeth_data *dst_data,*src_data;
393 if (!pkey_gost_mac_init(dst))
397 src_data = EVP_PKEY_CTX_get_data(src);
398 dst_data = EVP_PKEY_CTX_get_data(dst);
399 *dst_data = *src_data;
403 static int pkey_gost_mac_ctrl (EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
405 struct gost_mac_pmeth_data *data =
406 (struct gost_mac_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
410 case EVP_PKEY_CTRL_MD:
412 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC)
414 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_DIGEST_TYPE);
417 data->md = (EVP_MD *)p2;
422 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
423 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
424 case EVP_PKEY_CTRL_PKCS7_SIGN:
426 case EVP_PKEY_CTRL_SET_MAC_KEY:
429 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
430 GOST_R_INVALID_MAC_KEY_LENGTH);
434 memcpy(data->key,p2,32);
437 case EVP_PKEY_CTRL_DIGESTINIT:
439 EVP_MD_CTX *mctx = p2;
443 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
446 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
449 key = EVP_PKEY_get0(pkey);
452 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
458 return mctx->digest->md_ctrl(mctx,EVP_MD_CTRL_SET_KEY,32,key);
463 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
464 const char *type, const char *value)
466 if (!strcmp(type, key_ctrl_string))
468 if (strlen(value)!=32)
470 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
471 GOST_R_INVALID_MAC_KEY_LENGTH);
474 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
477 if (!strcmp(type, hexkey_ctrl_string))
479 long keylen; int ret;
480 unsigned char *keybuf=string_to_hex(value,&keylen);
483 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
484 GOST_R_INVALID_MAC_KEY_LENGTH);
487 ret= pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
489 OPENSSL_free(keybuf);
496 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
498 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
499 unsigned char *keydata;
502 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN,GOST_R_MAC_KEY_NOT_SET);
505 keydata = OPENSSL_malloc(32);
506 memcpy(keydata,data->key,32);
507 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
511 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
516 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, EVP_MD_CTX *mctx)
518 unsigned int tmpsiglen=*siglen; /* for platforms where sizeof(int)!=sizeof(size_t)*/
525 ret=EVP_DigestFinal_ex(mctx,sig,&tmpsiglen);
529 /* ----------------------------------------------------------------*/
530 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags)
532 *pmeth = EVP_PKEY_meth_new(id, flags);
533 if (!*pmeth) return 0;
537 case NID_id_GostR3410_94:
538 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
539 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
540 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
541 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
542 EVP_PKEY_meth_set_encrypt(*pmeth,
543 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
544 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
545 EVP_PKEY_meth_set_derive(*pmeth,
546 pkey_gost_derive_init, pkey_gost94_derive);
548 case NID_id_GostR3410_2001:
549 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
550 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
551 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
553 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
555 EVP_PKEY_meth_set_encrypt(*pmeth,
556 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
557 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
558 EVP_PKEY_meth_set_derive(*pmeth,
559 pkey_gost_derive_init, pkey_gost2001_derive);
561 case NID_id_Gost28147_89_MAC:
562 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_mac_ctrl, pkey_gost_mac_ctrl_str);
563 EVP_PKEY_meth_set_signctx(*pmeth,pkey_gost_mac_signctx_init, pkey_gost_mac_signctx);
564 EVP_PKEY_meth_set_keygen(*pmeth,NULL, pkey_gost_mac_keygen);
565 EVP_PKEY_meth_set_init(*pmeth,pkey_gost_mac_init);
566 EVP_PKEY_meth_set_cleanup(*pmeth,pkey_gost_mac_cleanup);
567 EVP_PKEY_meth_set_copy(*pmeth,pkey_gost_mac_copy);
569 default: /*Unsupported method*/
572 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
573 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
575 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
576 /*FIXME derive etc...*/