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 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
26 data = OPENSSL_malloc(sizeof(struct gost_pmeth_data));
28 memset(data,0,sizeof(struct gost_pmeth_data));
29 if (pkey && EVP_PKEY_get0(pkey))
31 switch (EVP_PKEY_base_id(pkey)) {
32 case NID_id_GostR3410_94:
33 data->sign_param_nid = gost94_nid_by_params(EVP_PKEY_get0(pkey));
35 case NID_id_GostR3410_2001:
36 data->sign_param_nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
42 EVP_PKEY_CTX_set_data(ctx,data);
46 /* Copies contents of gost_pmeth_data structure */
47 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
49 struct gost_pmeth_data *dst_data,*src_data;
50 if (!pkey_gost_init(dst))
54 src_data = EVP_PKEY_CTX_get_data(src);
55 dst_data = EVP_PKEY_CTX_get_data(dst);
56 *dst_data = *src_data;
57 if (src_data -> shared_ukm) {
58 dst_data->shared_ukm=NULL;
63 /* Frees up gost_pmeth_data structure */
64 static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx)
66 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
67 if (data->shared_ukm) OPENSSL_free(data->shared_ukm);
71 /* --------------------- control functions ------------------------------*/
72 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
74 struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
77 case EVP_PKEY_CTRL_MD:
79 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)
81 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
84 pctx->md = (EVP_MD *)p2;
89 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
90 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
91 case EVP_PKEY_CTRL_PKCS7_SIGN:
92 case EVP_PKEY_CTRL_DIGESTINIT:
93 #ifndef OPENSSL_NO_CMS
94 case EVP_PKEY_CTRL_CMS_ENCRYPT:
95 case EVP_PKEY_CTRL_CMS_DECRYPT:
96 case EVP_PKEY_CTRL_CMS_SIGN:
100 case EVP_PKEY_CTRL_GOST_PARAMSET:
101 pctx->sign_param_nid = (int)p1;
103 case EVP_PKEY_CTRL_SET_IV:
104 pctx->shared_ukm=OPENSSL_malloc((int)p1);
105 memcpy(pctx->shared_ukm,p2,(int) p1);
107 case EVP_PKEY_CTRL_PEER_KEY:
108 if (p1 == 0 || p1 == 1) /* call from EVP_PKEY_derive_set_peer */
110 if (p1 == 2) /* TLS: peer key used? */
111 return pctx->peer_key_used;
112 if (p1 == 3) /* TLS: peer key used! */
113 return (pctx->peer_key_used = 1);
120 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
121 const char *type, const char *value)
124 if(!strcmp(type, param_ctrl_string))
130 if (strlen(value) == 1)
132 switch(toupper((unsigned char)value[0]))
135 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
138 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
141 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
144 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
151 else if ((strlen(value) == 2) && (toupper((unsigned char)value[0]) == 'X'))
153 switch (toupper((unsigned char)value[1]))
156 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
159 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
162 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)
177 for (;p->nid != NID_undef;p++)
179 if (p->nid == param_nid) break;
181 if (p->nid == NID_undef)
183 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
184 GOST_R_INVALID_PARAMSET);
189 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
195 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
196 const char *type, const char *value)
199 if(!strcmp(type, param_ctrl_string))
205 if (strlen(value) == 1)
207 switch(toupper((unsigned char)value[0]))
210 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
213 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
216 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
219 param_nid = NID_id_GostR3410_2001_TestParamSet;
226 else if ((strlen(value) == 2) && (toupper((unsigned char)value[0]) == 'X'))
228 switch (toupper((unsigned char)value[1]))
231 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
234 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
243 R3410_2001_params *p = R3410_2001_paramset;
244 param_nid = OBJ_txt2nid(value);
245 if (param_nid == NID_undef)
249 for (;p->nid != NID_undef;p++)
251 if (p->nid == param_nid) break;
253 if (p->nid == NID_undef)
255 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
256 GOST_R_INVALID_PARAMSET);
261 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
267 /* --------------------- key generation --------------------------------*/
269 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx) {
272 static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
274 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
276 if (data->sign_param_nid == NID_undef)
278 GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN,
279 GOST_R_NO_PARAMETERS_SET);
283 if (!fill_GOST94_params(dsa,data->sign_param_nid))
288 EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa);
291 static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
293 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
296 if (data->sign_param_nid == NID_undef)
298 GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN,
299 GOST_R_NO_PARAMETERS_SET);
304 if (!fill_GOST2001_params(ec,data->sign_param_nid))
309 EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec);
313 /* Generates Gost_R3410_94_cp key */
314 static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
317 if (!pkey_gost94_paramgen(ctx,pkey)) return 0;
318 dsa = EVP_PKEY_get0(pkey);
319 gost_sign_keygen(dsa);
323 /* Generates GOST_R3410 2001 key and assigns it using specified type */
324 static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
327 if (!pkey_gost01_paramgen(ctx,pkey)) return 0;
328 ec = EVP_PKEY_get0(pkey);
335 /* ----------- sign callbacks --------------------------------------*/
337 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
338 const unsigned char *tbs, size_t tbs_len)
340 DSA_SIG *unpacked_sig=NULL;
341 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
342 if (!siglen) return 0;
345 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
348 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
353 return pack_sign_cp(unpacked_sig,32,sig,siglen);
356 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
357 const unsigned char *tbs, size_t tbs_len)
359 DSA_SIG *unpacked_sig=NULL;
360 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
361 if (!siglen) return 0;
364 *siglen= 64; /* better to check size of curve order*/
367 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
372 return pack_sign_cp(unpacked_sig,32,sig,siglen);
375 /* ------------------- verify callbacks ---------------------------*/
377 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
378 size_t siglen, const unsigned char *tbs, size_t tbs_len)
381 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
382 DSA_SIG *s=unpack_cp_signature(sig,siglen);
384 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
390 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
391 size_t siglen, const unsigned char *tbs, size_t tbs_len)
394 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
395 DSA_SIG *s=unpack_cp_signature(sig,siglen);
398 fprintf(stderr,"R=");
399 BN_print_fp(stderr,s->r);
400 fprintf(stderr,"\nS=");
401 BN_print_fp(stderr,s->s);
402 fprintf(stderr,"\n");
404 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
409 /* ------------- encrypt init -------------------------------------*/
410 /* Generates ephermeral key */
411 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
415 /* --------------- Derive init ------------------------------------*/
416 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
420 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
421 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
423 struct gost_mac_pmeth_data *data;
424 data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data));
426 memset(data,0,sizeof(struct gost_mac_pmeth_data));
427 EVP_PKEY_CTX_set_data(ctx,data);
430 static void pkey_gost_mac_cleanup (EVP_PKEY_CTX *ctx)
432 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
435 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
437 struct gost_mac_pmeth_data *dst_data,*src_data;
438 if (!pkey_gost_mac_init(dst))
442 src_data = EVP_PKEY_CTX_get_data(src);
443 dst_data = EVP_PKEY_CTX_get_data(dst);
444 *dst_data = *src_data;
448 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
450 struct gost_mac_pmeth_data *data =
451 (struct gost_mac_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
455 case EVP_PKEY_CTRL_MD:
457 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC)
459 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_DIGEST_TYPE);
462 data->md = (EVP_MD *)p2;
467 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
468 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
469 case EVP_PKEY_CTRL_PKCS7_SIGN:
471 case EVP_PKEY_CTRL_SET_MAC_KEY:
474 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
475 GOST_R_INVALID_MAC_KEY_LENGTH);
479 memcpy(data->key,p2,32);
482 case EVP_PKEY_CTRL_DIGESTINIT:
484 EVP_MD_CTX *mctx = p2;
488 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
491 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
494 key = EVP_PKEY_get0(pkey);
497 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
503 return mctx->digest->md_ctrl(mctx,EVP_MD_CTRL_SET_KEY,32,key);
508 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
509 const char *type, const char *value)
511 if (!strcmp(type, key_ctrl_string))
513 if (strlen(value)!=32)
515 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
516 GOST_R_INVALID_MAC_KEY_LENGTH);
519 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
522 if (!strcmp(type, hexkey_ctrl_string))
524 long keylen; int ret;
525 unsigned char *keybuf=string_to_hex(value,&keylen);
528 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
529 GOST_R_INVALID_MAC_KEY_LENGTH);
530 OPENSSL_free(keybuf);
533 ret= pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
535 OPENSSL_free(keybuf);
542 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
544 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
545 unsigned char *keydata;
548 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN,GOST_R_MAC_KEY_NOT_SET);
551 keydata = OPENSSL_malloc(32);
552 memcpy(keydata,data->key,32);
553 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
557 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
562 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, EVP_MD_CTX *mctx)
564 unsigned int tmpsiglen=*siglen; /* for platforms where sizeof(int)!=sizeof(size_t)*/
571 ret=EVP_DigestFinal_ex(mctx,sig,&tmpsiglen);
575 /* ----------------------------------------------------------------*/
576 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags)
578 *pmeth = EVP_PKEY_meth_new(id, flags);
579 if (!*pmeth) return 0;
583 case NID_id_GostR3410_94:
584 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
585 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
586 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
587 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
588 EVP_PKEY_meth_set_encrypt(*pmeth,
589 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
590 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
591 EVP_PKEY_meth_set_derive(*pmeth,
592 pkey_gost_derive_init, pkey_gost94_derive);
593 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost94_paramgen);
595 case NID_id_GostR3410_2001:
596 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
597 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
598 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
600 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
602 EVP_PKEY_meth_set_encrypt(*pmeth,
603 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
604 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
605 EVP_PKEY_meth_set_derive(*pmeth,
606 pkey_gost_derive_init, pkey_gost2001_derive);
607 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost01_paramgen);
609 case NID_id_Gost28147_89_MAC:
610 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_mac_ctrl, pkey_gost_mac_ctrl_str);
611 EVP_PKEY_meth_set_signctx(*pmeth,pkey_gost_mac_signctx_init, pkey_gost_mac_signctx);
612 EVP_PKEY_meth_set_keygen(*pmeth,NULL, pkey_gost_mac_keygen);
613 EVP_PKEY_meth_set_init(*pmeth,pkey_gost_mac_init);
614 EVP_PKEY_meth_set_cleanup(*pmeth,pkey_gost_mac_cleanup);
615 EVP_PKEY_meth_set_copy(*pmeth,pkey_gost_mac_copy);
617 default: /*Unsupported method*/
620 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
621 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
623 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
624 /*FIXME derive etc...*/