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));
29 memset(data,0,sizeof(struct gost_pmeth_data));
30 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 = EC_GROUP_get_curve_name(EC_KEY_get0_group(EVP_PKEY_get0((EVP_PKEY *)pkey)));
43 EVP_PKEY_CTX_set_data(ctx,data);
47 /* Copies contents of gost_pmeth_data structure */
48 static int pkey_gost_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
50 struct gost_pmeth_data *dst_data,*src_data;
51 if (!pkey_gost_init(dst))
55 src_data = EVP_PKEY_CTX_get_data(src);
56 dst_data = EVP_PKEY_CTX_get_data(dst);
57 *dst_data = *src_data;
58 if (src_data -> shared_ukm) {
59 dst_data->shared_ukm=NULL;
64 /* Frees up gost_pmeth_data structure */
65 static void pkey_gost_cleanup (EVP_PKEY_CTX *ctx)
67 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
68 if (data->shared_ukm) OPENSSL_free(data->shared_ukm);
72 /* --------------------- control functions ------------------------------*/
73 static int pkey_gost_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
75 struct gost_pmeth_data *pctx = (struct gost_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
78 case EVP_PKEY_CTRL_MD:
80 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_GostR3411_94)
82 GOSTerr(GOST_F_PKEY_GOST_CTRL, GOST_R_INVALID_DIGEST_TYPE);
85 pctx->md = (EVP_MD *)p2;
90 case EVP_PKEY_CTRL_GET_MD:
91 *(const EVP_MD **)p2 = pctx->md;
94 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
95 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
96 case EVP_PKEY_CTRL_PKCS7_SIGN:
97 case EVP_PKEY_CTRL_DIGESTINIT:
98 #ifndef OPENSSL_NO_CMS
99 case EVP_PKEY_CTRL_CMS_ENCRYPT:
100 case EVP_PKEY_CTRL_CMS_DECRYPT:
101 case EVP_PKEY_CTRL_CMS_SIGN:
105 case EVP_PKEY_CTRL_GOST_PARAMSET:
106 pctx->sign_param_nid = (int)p1;
108 case EVP_PKEY_CTRL_SET_IV:
109 pctx->shared_ukm=OPENSSL_malloc((int)p1);
110 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);
130 static int pkey_gost_ctrl94_str(EVP_PKEY_CTX *ctx,
131 const char *type, const char *value)
134 if(!strcmp(type, param_ctrl_string))
140 if (strlen(value) == 1)
142 switch(toupper((unsigned char)value[0]))
145 param_nid = NID_id_GostR3410_94_CryptoPro_A_ParamSet;
148 param_nid = NID_id_GostR3410_94_CryptoPro_B_ParamSet;
151 param_nid = NID_id_GostR3410_94_CryptoPro_C_ParamSet;
154 param_nid = NID_id_GostR3410_94_CryptoPro_D_ParamSet;
161 else if ((strlen(value) == 2) && (toupper((unsigned char)value[0]) == 'X'))
163 switch (toupper((unsigned char)value[1]))
166 param_nid = NID_id_GostR3410_94_CryptoPro_XchA_ParamSet;
169 param_nid = NID_id_GostR3410_94_CryptoPro_XchB_ParamSet;
172 param_nid = NID_id_GostR3410_94_CryptoPro_XchC_ParamSet;
181 R3410_params *p = R3410_paramset;
182 param_nid = OBJ_txt2nid(value);
183 if (param_nid == NID_undef)
187 for (;p->nid != NID_undef;p++)
189 if (p->nid == param_nid) break;
191 if (p->nid == NID_undef)
193 GOSTerr(GOST_F_PKEY_GOST_CTRL94_STR,
194 GOST_R_INVALID_PARAMSET);
199 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
205 static int pkey_gost_ctrl01_str(EVP_PKEY_CTX *ctx,
206 const char *type, const char *value)
209 if(!strcmp(type, param_ctrl_string))
215 if (strlen(value) == 1)
217 switch(toupper((unsigned char)value[0]))
220 param_nid = NID_id_GostR3410_2001_CryptoPro_A_ParamSet;
223 param_nid = NID_id_GostR3410_2001_CryptoPro_B_ParamSet;
226 param_nid = NID_id_GostR3410_2001_CryptoPro_C_ParamSet;
229 param_nid = NID_id_GostR3410_2001_TestParamSet;
236 else if ((strlen(value) == 2) && (toupper((unsigned char)value[0]) == 'X'))
238 switch (toupper((unsigned char)value[1]))
241 param_nid = NID_id_GostR3410_2001_CryptoPro_XchA_ParamSet;
244 param_nid = NID_id_GostR3410_2001_CryptoPro_XchB_ParamSet;
253 R3410_2001_params *p = R3410_2001_paramset;
254 param_nid = OBJ_txt2nid(value);
255 if (param_nid == NID_undef)
259 for (;p->nid != NID_undef;p++)
261 if (p->nid == param_nid) break;
263 if (p->nid == NID_undef)
265 GOSTerr(GOST_F_PKEY_GOST_CTRL01_STR,
266 GOST_R_INVALID_PARAMSET);
271 return pkey_gost_ctrl(ctx, EVP_PKEY_CTRL_GOST_PARAMSET,
277 /* --------------------- key generation --------------------------------*/
279 static int pkey_gost_paramgen_init(EVP_PKEY_CTX *ctx) {
282 static int pkey_gost94_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
284 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
286 if (data->sign_param_nid == NID_undef)
288 GOSTerr(GOST_F_PKEY_GOST94_PARAMGEN,
289 GOST_R_NO_PARAMETERS_SET);
293 if (!fill_GOST94_params(dsa,data->sign_param_nid))
298 EVP_PKEY_assign(pkey,NID_id_GostR3410_94,dsa);
301 static int pkey_gost01_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
303 struct gost_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
306 if (data->sign_param_nid == NID_undef)
308 GOSTerr(GOST_F_PKEY_GOST01_PARAMGEN,
309 GOST_R_NO_PARAMETERS_SET);
314 if (!fill_GOST2001_params(ec,data->sign_param_nid))
319 EVP_PKEY_assign(pkey,NID_id_GostR3410_2001,ec);
323 /* Generates Gost_R3410_94_cp key */
324 static int pkey_gost94cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
327 if (!pkey_gost94_paramgen(ctx,pkey)) return 0;
328 dsa = EVP_PKEY_get0(pkey);
329 gost_sign_keygen(dsa);
333 /* Generates GOST_R3410 2001 key and assigns it using specified type */
334 static int pkey_gost01cp_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
337 if (!pkey_gost01_paramgen(ctx,pkey)) return 0;
338 ec = EVP_PKEY_get0(pkey);
345 /* ----------- sign callbacks --------------------------------------*/
347 static int pkey_gost94_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
348 const unsigned char *tbs, size_t tbs_len)
350 DSA_SIG *unpacked_sig=NULL;
351 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
352 if (!siglen) return 0;
355 *siglen= 64; /* better to check size of pkey->pkey.dsa-q */
358 unpacked_sig = gost_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
363 return pack_sign_cp(unpacked_sig,32,sig,siglen);
366 static int pkey_gost01_cp_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
367 const unsigned char *tbs, size_t tbs_len)
369 DSA_SIG *unpacked_sig=NULL;
370 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
371 if (!siglen) return 0;
374 *siglen= 64; /* better to check size of curve order*/
377 unpacked_sig = gost2001_do_sign(tbs,tbs_len,EVP_PKEY_get0(pkey));
382 return pack_sign_cp(unpacked_sig,32,sig,siglen);
385 /* ------------------- verify callbacks ---------------------------*/
387 static int pkey_gost94_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
388 size_t siglen, const unsigned char *tbs, size_t tbs_len)
391 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
392 DSA_SIG *s=unpack_cp_signature(sig,siglen);
394 if (pub_key) ok = gost_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
400 static int pkey_gost01_cp_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig,
401 size_t siglen, const unsigned char *tbs, size_t tbs_len)
404 EVP_PKEY* pub_key = EVP_PKEY_CTX_get0_pkey(ctx);
405 DSA_SIG *s=unpack_cp_signature(sig,siglen);
408 fprintf(stderr,"R=");
409 BN_print_fp(stderr,s->r);
410 fprintf(stderr,"\nS=");
411 BN_print_fp(stderr,s->s);
412 fprintf(stderr,"\n");
414 if (pub_key) ok = gost2001_do_verify(tbs,tbs_len,s,EVP_PKEY_get0(pub_key));
419 /* ------------- encrypt init -------------------------------------*/
420 /* Generates ephermeral key */
421 static int pkey_gost_encrypt_init(EVP_PKEY_CTX *ctx)
425 /* --------------- Derive init ------------------------------------*/
426 static int pkey_gost_derive_init(EVP_PKEY_CTX *ctx)
430 /* -------- PKEY_METHOD for GOST MAC algorithm --------------------*/
431 static int pkey_gost_mac_init(EVP_PKEY_CTX *ctx)
433 struct gost_mac_pmeth_data *data;
434 data = OPENSSL_malloc(sizeof(struct gost_mac_pmeth_data));
436 memset(data,0,sizeof(struct gost_mac_pmeth_data));
437 EVP_PKEY_CTX_set_data(ctx,data);
440 static void pkey_gost_mac_cleanup (EVP_PKEY_CTX *ctx)
442 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
445 static int pkey_gost_mac_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
447 struct gost_mac_pmeth_data *dst_data,*src_data;
448 if (!pkey_gost_mac_init(dst))
452 src_data = EVP_PKEY_CTX_get_data(src);
453 dst_data = EVP_PKEY_CTX_get_data(dst);
454 *dst_data = *src_data;
458 static int pkey_gost_mac_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
460 struct gost_mac_pmeth_data *data =
461 (struct gost_mac_pmeth_data*)EVP_PKEY_CTX_get_data(ctx);
465 case EVP_PKEY_CTRL_MD:
467 if (EVP_MD_type((const EVP_MD *)p2) != NID_id_Gost28147_89_MAC)
469 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL, GOST_R_INVALID_DIGEST_TYPE);
472 data->md = (EVP_MD *)p2;
477 case EVP_PKEY_CTRL_GET_MD:
478 *(const EVP_MD **)p2 = data->md;
481 case EVP_PKEY_CTRL_PKCS7_ENCRYPT:
482 case EVP_PKEY_CTRL_PKCS7_DECRYPT:
483 case EVP_PKEY_CTRL_PKCS7_SIGN:
485 case EVP_PKEY_CTRL_SET_MAC_KEY:
488 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,
489 GOST_R_INVALID_MAC_KEY_LENGTH);
493 memcpy(data->key,p2,32);
496 case EVP_PKEY_CTRL_DIGESTINIT:
498 EVP_MD_CTX *mctx = p2;
502 EVP_PKEY *pkey = EVP_PKEY_CTX_get0_pkey(ctx);
505 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
508 key = EVP_PKEY_get0(pkey);
511 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL,GOST_R_MAC_KEY_NOT_SET);
517 return mctx->digest->md_ctrl(mctx,EVP_MD_CTRL_SET_KEY,32,key);
522 static int pkey_gost_mac_ctrl_str(EVP_PKEY_CTX *ctx,
523 const char *type, const char *value)
525 if (!strcmp(type, key_ctrl_string))
527 if (strlen(value)!=32)
529 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
530 GOST_R_INVALID_MAC_KEY_LENGTH);
533 return pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
536 if (!strcmp(type, hexkey_ctrl_string))
538 long keylen; int ret;
539 unsigned char *keybuf=string_to_hex(value,&keylen);
542 GOSTerr(GOST_F_PKEY_GOST_MAC_CTRL_STR,
543 GOST_R_INVALID_MAC_KEY_LENGTH);
544 OPENSSL_free(keybuf);
547 ret= pkey_gost_mac_ctrl(ctx, EVP_PKEY_CTRL_SET_MAC_KEY,
549 OPENSSL_free(keybuf);
556 static int pkey_gost_mac_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
558 struct gost_mac_pmeth_data *data = EVP_PKEY_CTX_get_data(ctx);
559 unsigned char *keydata;
562 GOSTerr(GOST_F_PKEY_GOST_MAC_KEYGEN,GOST_R_MAC_KEY_NOT_SET);
565 keydata = OPENSSL_malloc(32);
568 memcpy(keydata,data->key,32);
569 EVP_PKEY_assign(pkey, NID_id_Gost28147_89_MAC, keydata);
573 static int pkey_gost_mac_signctx_init(EVP_PKEY_CTX *ctx, EVP_MD_CTX *mctx)
578 static int pkey_gost_mac_signctx(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen, EVP_MD_CTX *mctx)
580 unsigned int tmpsiglen=*siglen; /* for platforms where sizeof(int)!=sizeof(size_t)*/
587 ret=EVP_DigestFinal_ex(mctx,sig,&tmpsiglen);
591 /* ----------------------------------------------------------------*/
592 int register_pmeth_gost(int id, EVP_PKEY_METHOD **pmeth,int flags)
594 *pmeth = EVP_PKEY_meth_new(id, flags);
595 if (!*pmeth) return 0;
599 case NID_id_GostR3410_94:
600 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl94_str);
601 EVP_PKEY_meth_set_keygen(*pmeth,NULL,pkey_gost94cp_keygen);
602 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost94_cp_sign);
603 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost94_cp_verify);
604 EVP_PKEY_meth_set_encrypt(*pmeth,
605 pkey_gost_encrypt_init, pkey_GOST94cp_encrypt);
606 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST94cp_decrypt);
607 EVP_PKEY_meth_set_derive(*pmeth,
608 pkey_gost_derive_init, pkey_gost94_derive);
609 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost94_paramgen);
611 case NID_id_GostR3410_2001:
612 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_ctrl, pkey_gost_ctrl01_str);
613 EVP_PKEY_meth_set_sign(*pmeth, NULL, pkey_gost01_cp_sign);
614 EVP_PKEY_meth_set_verify(*pmeth, NULL, pkey_gost01_cp_verify);
616 EVP_PKEY_meth_set_keygen(*pmeth, NULL, pkey_gost01cp_keygen);
618 EVP_PKEY_meth_set_encrypt(*pmeth,
619 pkey_gost_encrypt_init, pkey_GOST01cp_encrypt);
620 EVP_PKEY_meth_set_decrypt(*pmeth, NULL, pkey_GOST01cp_decrypt);
621 EVP_PKEY_meth_set_derive(*pmeth,
622 pkey_gost_derive_init, pkey_gost2001_derive);
623 EVP_PKEY_meth_set_paramgen(*pmeth, pkey_gost_paramgen_init,pkey_gost01_paramgen);
625 case NID_id_Gost28147_89_MAC:
626 EVP_PKEY_meth_set_ctrl(*pmeth,pkey_gost_mac_ctrl, pkey_gost_mac_ctrl_str);
627 EVP_PKEY_meth_set_signctx(*pmeth,pkey_gost_mac_signctx_init, pkey_gost_mac_signctx);
628 EVP_PKEY_meth_set_keygen(*pmeth,NULL, pkey_gost_mac_keygen);
629 EVP_PKEY_meth_set_init(*pmeth,pkey_gost_mac_init);
630 EVP_PKEY_meth_set_cleanup(*pmeth,pkey_gost_mac_cleanup);
631 EVP_PKEY_meth_set_copy(*pmeth,pkey_gost_mac_copy);
633 default: /*Unsupported method*/
636 EVP_PKEY_meth_set_init(*pmeth, pkey_gost_init);
637 EVP_PKEY_meth_set_cleanup(*pmeth, pkey_gost_cleanup);
639 EVP_PKEY_meth_set_copy(*pmeth, pkey_gost_copy);
640 /*FIXME derive etc...*/