60 #ifndef OPENSSL_NO_RSA
63 #ifndef OPENSSL_NO_DSA
72 #ifndef OPENSSL_NO_HW_SUREWARE
80 #define SUREWARE_LIB_NAME "sureware engine"
83 static int surewarehk_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)(
void));
84 static int surewarehk_destroy(
ENGINE *e);
85 static int surewarehk_init(
ENGINE *e);
86 static int surewarehk_finish(
ENGINE *e);
91 #ifndef OPENSSL_NO_RSA
92 static int surewarehk_rsa_priv_dec(
int flen,
const unsigned char *from,
unsigned char *to,
93 RSA *rsa,
int padding);
94 static int surewarehk_rsa_sign(
int flen,
const unsigned char *from,
unsigned char *to,
95 RSA *rsa,
int padding);
99 static int surewarehk_rand_bytes(
unsigned char *buf,
int num);
100 static void surewarehk_rand_seed(
const void *buf,
int num);
101 static void surewarehk_rand_add(
const void *buf,
int num,
double entropy);
104 static EVP_PKEY *surewarehk_load_privkey(
ENGINE *e,
const char *key_id,
105 UI_METHOD *ui_method,
void *callback_data);
106 static EVP_PKEY *surewarehk_load_pubkey(
ENGINE *e,
const char *key_id,
107 UI_METHOD *ui_method,
void *callback_data);
109 int idx,
long argl,
void *argp);
112 int idx,
long argl,
void *argp);
115 #ifndef OPENSSL_NO_RSA
120 return surewarehk_modexp(r, a, p, m, ctx);
126 "SureWare RSA method",
130 surewarehk_rsa_priv_dec,
132 surewarehk_mod_exp_mont,
143 #ifndef OPENSSL_NO_DH
146 static int surewarehk_modexp_dh(
const DH *dh,
BIGNUM *r,
const BIGNUM *
a,
149 return surewarehk_modexp(r, a, p, m, ctx);
154 "SureWare DH method",
157 surewarehk_modexp_dh,
169 surewarehk_rand_seed,
170 surewarehk_rand_bytes,
173 surewarehk_rand_bytes,
177 #ifndef OPENSSL_NO_DSA
179 static DSA_SIG * surewarehk_dsa_do_sign(
const unsigned char *dgst,
int dlen,
DSA *dsa);
188 if (!surewarehk_modexp(rr,a1,p1,m,ctx))
goto end;
190 if (!surewarehk_modexp(&t,a2,p2,m,ctx))
goto end;
201 "SureWare DSA method",
202 surewarehk_dsa_do_sign,
205 surewarehk_dsa_mod_exp,
216 static const char *engine_sureware_id =
"sureware";
217 static const char *engine_sureware_name =
"SureWare hardware engine support";
223 static int bind_sureware(
ENGINE *e)
225 #ifndef OPENSSL_NO_RSA
228 #ifndef OPENSSL_NO_DSA
231 #ifndef OPENSSL_NO_DH
237 #ifndef OPENSSL_NO_RSA
240 #ifndef OPENSSL_NO_DSA
243 #ifndef OPENSSL_NO_DH
255 #ifndef OPENSSL_NO_RSA
271 #ifndef OPENSSL_NO_DSA
281 #ifndef OPENSSL_NO_DH
292 ERR_load_SUREWARE_strings();
296 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
297 static int bind_helper(
ENGINE *e,
const char *
id)
299 if(
id && (strcmp(
id, engine_sureware_id) != 0))
301 if(!bind_sureware(e))
308 static ENGINE *engine_sureware(
void)
313 if(!bind_sureware(ret))
324 ENGINE *toadd = engine_sureware();
337 static DSO *surewarehk_dso = NULL;
338 #ifndef OPENSSL_NO_RSA
339 static int rsaHndidx = -1;
341 #ifndef OPENSSL_NO_DSA
342 static int dsaHndidx = -1;
362 static const char *surewarehk_LIBNAME =
"SureWareHook";
363 static const char *n_surewarehk_Init =
"SureWareHook_Init";
364 static const char *n_surewarehk_Finish =
"SureWareHook_Finish";
365 static const char *n_surewarehk_Rand_Bytes=
"SureWareHook_Rand_Bytes";
366 static const char *n_surewarehk_Rand_Seed=
"SureWareHook_Rand_Seed";
367 static const char *n_surewarehk_Load_Privkey=
"SureWareHook_Load_Privkey";
368 static const char *n_surewarehk_Info_Pubkey=
"SureWareHook_Info_Pubkey";
369 static const char *n_surewarehk_Load_Rsa_Pubkey=
"SureWareHook_Load_Rsa_Pubkey";
370 static const char *n_surewarehk_Load_Dsa_Pubkey=
"SureWareHook_Load_Dsa_Pubkey";
371 static const char *n_surewarehk_Free=
"SureWareHook_Free";
372 static const char *n_surewarehk_Rsa_Priv_Dec=
"SureWareHook_Rsa_Priv_Dec";
373 static const char *n_surewarehk_Rsa_Sign=
"SureWareHook_Rsa_Sign";
374 static const char *n_surewarehk_Dsa_Sign=
"SureWareHook_Dsa_Sign";
375 static const char *n_surewarehk_Mod_Exp=
"SureWareHook_Mod_Exp";
376 static BIO *logstream = NULL;
383 static int threadsafe=1;
384 static int surewarehk_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
429 static int surewarehk_destroy(
ENGINE *e)
431 ERR_unload_SUREWARE_strings();
436 static int surewarehk_init(
ENGINE *e)
438 char msg[64]=
"ENGINE_init";
453 if(surewarehk_dso != NULL)
459 surewarehk_dso =
DSO_load(NULL, surewarehk_LIBNAME, NULL, 0);
460 if(surewarehk_dso == NULL)
483 p_surewarehk_Init = p1;
484 p_surewarehk_Finish = p2;
485 p_surewarehk_Rand_Bytes = p3;
486 p_surewarehk_Rand_Seed = p4;
487 p_surewarehk_Load_Privkey = p5;
488 p_surewarehk_Load_Rsa_Pubkey = p6;
489 p_surewarehk_Free = p7;
490 p_surewarehk_Rsa_Priv_Dec = p8;
491 p_surewarehk_Rsa_Sign = p9;
492 p_surewarehk_Dsa_Sign = p12;
493 p_surewarehk_Info_Pubkey = p13;
494 p_surewarehk_Load_Dsa_Pubkey = p14;
495 p_surewarehk_Mod_Exp = p15;
509 surewarehk_load_privkey(e,NULL,NULL,NULL);
512 #ifndef OPENSSL_NO_RSA
515 "SureWareHook RSA key handle",
516 NULL, NULL, surewarehk_ex_free);
518 #ifndef OPENSSL_NO_DSA
521 "SureWareHook DSA key handle",
522 NULL, NULL, surewarehk_ex_free);
529 surewarehk_dso = NULL;
530 p_surewarehk_Init = NULL;
531 p_surewarehk_Finish = NULL;
532 p_surewarehk_Rand_Bytes = NULL;
533 p_surewarehk_Rand_Seed = NULL;
534 p_surewarehk_Load_Privkey = NULL;
535 p_surewarehk_Load_Rsa_Pubkey = NULL;
536 p_surewarehk_Free = NULL;
537 p_surewarehk_Rsa_Priv_Dec = NULL;
538 p_surewarehk_Rsa_Sign = NULL;
539 p_surewarehk_Dsa_Sign = NULL;
540 p_surewarehk_Info_Pubkey = NULL;
541 p_surewarehk_Load_Dsa_Pubkey = NULL;
542 p_surewarehk_Mod_Exp = NULL;
546 static int surewarehk_finish(
ENGINE *e)
549 if(surewarehk_dso == NULL)
555 p_surewarehk_Finish();
565 surewarehk_dso = NULL;
566 p_surewarehk_Init = NULL;
567 p_surewarehk_Finish = NULL;
568 p_surewarehk_Rand_Bytes = NULL;
569 p_surewarehk_Rand_Seed = NULL;
570 p_surewarehk_Load_Privkey = NULL;
571 p_surewarehk_Load_Rsa_Pubkey = NULL;
572 p_surewarehk_Free = NULL;
573 p_surewarehk_Rsa_Priv_Dec = NULL;
574 p_surewarehk_Rsa_Sign = NULL;
575 p_surewarehk_Dsa_Sign = NULL;
576 p_surewarehk_Info_Pubkey = NULL;
577 p_surewarehk_Load_Dsa_Pubkey = NULL;
578 p_surewarehk_Mod_Exp = NULL;
582 static void surewarehk_error_handling(
char *
const msg,
int func,
int ret)
616 static int surewarehk_rand_bytes(
unsigned char *buf,
int num)
619 char msg[64]=
"ENGINE_rand_bytes";
620 if(!p_surewarehk_Rand_Bytes)
626 ret = p_surewarehk_Rand_Bytes(msg,buf, num);
629 return ret==1 ? 1 : 0;
632 static void surewarehk_rand_seed(
const void *buf,
int num)
635 char msg[64]=
"ENGINE_rand_seed";
636 if(!p_surewarehk_Rand_Seed)
642 ret = p_surewarehk_Rand_Seed(msg,buf, num);
647 static void surewarehk_rand_add(
const void *buf,
int num,
double entropy)
649 surewarehk_rand_seed(buf,num);
652 static EVP_PKEY* sureware_load_public(
ENGINE *e,
const char *key_id,
char *hptr,
unsigned long el,
char keytype)
655 #ifndef OPENSSL_NO_RSA
658 #ifndef OPENSSL_NO_DSA
661 char msg[64]=
"sureware_load_public";
663 if(!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey)
670 #ifndef OPENSSL_NO_RSA
682 if (!rsatmp->
e || rsatmp->
e->
dmax!=(
int)(el/
sizeof(BN_ULONG))||
683 !rsatmp->
n || rsatmp->
n->
dmax!=(
int)(el/
sizeof(BN_ULONG)))
685 ret=p_surewarehk_Load_Rsa_Pubkey(msg,key_id,el,
686 (
unsigned long *)rsatmp->
n->
d,
687 (
unsigned long *)rsatmp->
e->
d);
695 rsatmp->
e->
top=el/
sizeof(BN_ULONG);
697 rsatmp->
n->
top=el/
sizeof(BN_ULONG);
705 #ifndef OPENSSL_NO_DSA
722 !dsatmp->
p || dsatmp->
p->
dmax!=(
int)(el/
sizeof(BN_ULONG)) ||
723 !dsatmp->
q || dsatmp->
q->
dmax!=20/
sizeof(BN_ULONG) ||
724 !dsatmp->
g || dsatmp->
g->
dmax!=(
int)(el/
sizeof(BN_ULONG)))
727 ret=p_surewarehk_Load_Dsa_Pubkey(msg,key_id,el,
728 (
unsigned long *)dsatmp->
pub_key->
d,
729 (
unsigned long *)dsatmp->
p->
d,
730 (
unsigned long *)dsatmp->
q->
d,
731 (
unsigned long *)dsatmp->
g->
d);
742 dsatmp->
p->
top=el/
sizeof(BN_ULONG);
744 dsatmp->
q->
top=20/
sizeof(BN_ULONG);
746 dsatmp->
g->
top=el/
sizeof(BN_ULONG);
761 #ifndef OPENSSL_NO_RSA
765 #ifndef OPENSSL_NO_DSA
772 static EVP_PKEY *surewarehk_load_privkey(
ENGINE *e,
const char *key_id,
773 UI_METHOD *ui_method,
void *callback_data)
780 char msg[64]=
"ENGINE_load_privkey";
782 if(!p_surewarehk_Load_Privkey)
788 ret=p_surewarehk_Load_Privkey(msg,key_id,&hptr,&el,&keytype);
795 res=sureware_load_public(e,key_id,hptr,el,keytype);
800 static EVP_PKEY *surewarehk_load_pubkey(
ENGINE *e,
const char *key_id,
801 UI_METHOD *ui_method,
void *callback_data)
808 char msg[64]=
"ENGINE_load_pubkey";
810 if(!p_surewarehk_Info_Pubkey)
817 ret=p_surewarehk_Info_Pubkey(msg,key_id,&el,&keytype);
824 res=sureware_load_public(e,key_id,hptr,el,keytype);
832 int idx,
long argl,
void *argp)
834 if(!p_surewarehk_Free)
839 p_surewarehk_Free((
char *)item,0);
846 static void surewarehk_dh_ex_free(
void *obj,
void *item,
CRYPTO_EX_DATA *ad,
847 int idx,
long argl,
void *argp)
849 if(!p_surewarehk_Free)
854 p_surewarehk_Free((
char *)item,1);
861 #ifndef OPENSSL_NO_RSA
862 static int surewarehk_rsa_priv_dec(
int flen,
const unsigned char *from,
unsigned char *to,
863 RSA *rsa,
int padding)
866 char *buf=NULL,*hptr=NULL;
867 char msg[64]=
"ENGINE_rsa_priv_dec";
868 if (!p_surewarehk_Rsa_Priv_Dec)
882 ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(
unsigned char *)from,&tlen,to,hptr,
SUREWARE_PKCS1_PAD);
890 ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(
unsigned char *)from,&tlen,to,hptr,
SUREWARE_NO_PAD);
903 #ifndef OPENSSL_NO_SHA
933 static int surewarehk_rsa_sign(
int flen,
const unsigned char *from,
unsigned char *to,
934 RSA *rsa,
int padding)
938 char msg[64]=
"ENGINE_rsa_sign";
939 if (!p_surewarehk_Rsa_Sign)
953 ret=p_surewarehk_Rsa_Sign(msg,flen,(
unsigned char *)from,&tlen,to,hptr,
SUREWARE_PKCS1_PAD);
961 return ret==1 ? tlen : ret;
966 #ifndef OPENSSL_NO_DSA
968 static DSA_SIG * surewarehk_dsa_do_sign(
const unsigned char *from,
int flen,
DSA *dsa)
973 char msg[64]=
"ENGINE_dsa_do_sign";
974 if (!p_surewarehk_Dsa_Sign)
996 if (!psign->
r || psign->
r->
dmax!=20/
sizeof(BN_ULONG) ||
997 !psign->
s || psign->
s->
dmax!=20/
sizeof(BN_ULONG))
999 ret=p_surewarehk_Dsa_Sign(msg,flen,from,
1000 (
unsigned long *)psign->
r->
d,
1001 (
unsigned long *)psign->
s->
d,
1005 psign->
r->
top=20/
sizeof(BN_ULONG);
1007 psign->
s->
top=20/
sizeof(BN_ULONG);
1024 char msg[64]=
"ENGINE_modexp";
1025 if (!p_surewarehk_Mod_Exp)
1035 ret=p_surewarehk_Mod_Exp(msg,
1036 m->
top*
sizeof(BN_ULONG),
1037 (
unsigned long *)m->
d,
1038 p->
top*
sizeof(BN_ULONG),
1039 (
unsigned long *)p->
d,
1040 a->
top*
sizeof(BN_ULONG),
1041 (
unsigned long *)a->
d,
1042 (
unsigned long *)r->
d);