67 #ifndef OPENSSL_NO_RSA
70 #ifndef OPENSSL_NO_DSA
79 #ifndef OPENSSL_NO_HW_UBSEC
87 #define UBSEC_LIB_NAME "ubsec engine"
90 #define FAIL_TO_SOFTWARE -15
92 static int ubsec_destroy(
ENGINE *
e);
93 static int ubsec_init(
ENGINE *
e);
94 static int ubsec_finish(
ENGINE *
e);
95 static int ubsec_ctrl(
ENGINE *
e,
int cmd,
long i,
void *
p,
void (*
f)(
void));
98 #ifndef OPENSSL_NO_RSA
106 #ifndef OPENSSL_NO_DSA
115 static DSA_SIG *ubsec_dsa_do_sign(
const unsigned char *dgst,
int dlen,
DSA *dsa);
116 static int ubsec_dsa_verify(
const unsigned char *dgst,
int dgst_len,
119 #ifndef OPENSSL_NO_DH
123 static int ubsec_dh_compute_key(
unsigned char *
key,
const BIGNUM *pub_key,
DH *dh);
124 static int ubsec_dh_generate_key(
DH *dh);
128 static int ubsec_rand_bytes(
unsigned char *buf,
int num);
129 static int ubsec_rand_status(
void);
132 #define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
136 "Specifies the path to the 'ubsec' shared library",
141 #ifndef OPENSSL_NO_RSA
162 #ifndef OPENSSL_NO_DSA
181 #ifndef OPENSSL_NO_DH
186 ubsec_dh_generate_key,
187 ubsec_dh_compute_key,
198 static const char *engine_ubsec_id =
"ubsec";
199 static const char *engine_ubsec_name =
"UBSEC hardware engine support";
203 static int bind_helper(
ENGINE *e)
205 #ifndef OPENSSL_NO_RSA
208 #ifndef OPENSSL_NO_DH
209 #ifndef HAVE_UBSEC_DH
215 #ifndef OPENSSL_NO_RSA
218 #ifndef OPENSSL_NO_DSA
221 #ifndef OPENSSL_NO_DH
231 #ifndef OPENSSL_NO_RSA
246 #ifndef OPENSSL_NO_DH
247 #ifndef HAVE_UBSEC_DH
256 ERR_load_UBSEC_strings();
260 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
261 static ENGINE *engine_ubsec(
void)
266 if(!bind_helper(ret))
277 ENGINE *toadd = engine_ubsec();
291 static DSO *ubsec_dso = NULL;
300 #ifndef OPENSSL_NO_DH
302 *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
305 #ifndef OPENSSL_NO_RSA
309 #ifndef OPENSSL_NO_DSA
317 static int max_key_len = 1024;
324 static const char *UBSEC_LIBNAME = NULL;
325 static const char *get_UBSEC_LIBNAME(
void)
328 return UBSEC_LIBNAME;
331 static void free_UBSEC_LIBNAME(
void)
335 UBSEC_LIBNAME = NULL;
337 static long set_UBSEC_LIBNAME(
const char *name)
339 free_UBSEC_LIBNAME();
340 return (((UBSEC_LIBNAME =
BUF_strdup(name)) != NULL) ? 1 : 0);
342 static const char *UBSEC_F1 =
"ubsec_bytes_to_bits";
343 static const char *UBSEC_F2 =
"ubsec_bits_to_bytes";
344 static const char *UBSEC_F3 =
"ubsec_open";
345 static const char *UBSEC_F4 =
"ubsec_close";
346 #ifndef OPENSSL_NO_DH
347 static const char *UBSEC_F5 =
"diffie_hellman_generate_ioctl";
348 static const char *UBSEC_F6 =
"diffie_hellman_agree_ioctl";
351 static const char *UBSEC_F7 =
"rsa_mod_exp_ioctl";
352 static const char *UBSEC_F8 =
"rsa_mod_exp_crt_ioctl";
354 #ifndef OPENSSL_NO_DSA
355 static const char *UBSEC_F9 =
"dsa_sign_ioctl";
356 static const char *UBSEC_F10 =
"dsa_verify_ioctl";
358 static const char *UBSEC_F11 =
"math_accelerate_ioctl";
359 static const char *UBSEC_F12 =
"rng_ioctl";
360 static const char *UBSEC_F13 =
"ubsec_max_key_len_ioctl";
363 static int ubsec_destroy(
ENGINE *e)
365 free_UBSEC_LIBNAME();
366 ERR_unload_UBSEC_strings();
371 static int ubsec_init(
ENGINE *e)
377 #ifndef OPENSSL_NO_DH
385 #ifndef OPENSSL_NO_DSA
394 if(ubsec_dso != NULL)
402 ubsec_dso =
DSO_load(NULL, get_UBSEC_LIBNAME(), NULL, 0);
403 if(ubsec_dso == NULL)
414 #ifndef OPENSSL_NO_DH
424 #ifndef OPENSSL_NO_DSA
438 p_UBSEC_ubsec_bytes_to_bits = p1;
439 p_UBSEC_ubsec_bits_to_bytes = p2;
440 p_UBSEC_ubsec_open = p3;
441 p_UBSEC_ubsec_close = p4;
442 #ifndef OPENSSL_NO_DH
443 p_UBSEC_diffie_hellman_generate_ioctl = p5;
444 p_UBSEC_diffie_hellman_agree_ioctl = p6;
446 #ifndef OPENSSL_NO_RSA
447 p_UBSEC_rsa_mod_exp_ioctl = p7;
448 p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
450 #ifndef OPENSSL_NO_DSA
451 p_UBSEC_dsa_sign_ioctl = p9;
452 p_UBSEC_dsa_verify_ioctl = p10;
454 p_UBSEC_math_accelerate_ioctl = p11;
455 p_UBSEC_rng_ioctl = p12;
456 p_UBSEC_max_key_len_ioctl = p13;
459 if (((fd = p_UBSEC_ubsec_open(
UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
461 p_UBSEC_ubsec_close(fd);
473 p_UBSEC_ubsec_bytes_to_bits = NULL;
474 p_UBSEC_ubsec_bits_to_bytes = NULL;
475 p_UBSEC_ubsec_open = NULL;
476 p_UBSEC_ubsec_close = NULL;
477 #ifndef OPENSSL_NO_DH
478 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
479 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
481 #ifndef OPENSSL_NO_RSA
482 p_UBSEC_rsa_mod_exp_ioctl = NULL;
483 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
485 #ifndef OPENSSL_NO_DSA
486 p_UBSEC_dsa_sign_ioctl = NULL;
487 p_UBSEC_dsa_verify_ioctl = NULL;
489 p_UBSEC_math_accelerate_ioctl = NULL;
490 p_UBSEC_rng_ioctl = NULL;
491 p_UBSEC_max_key_len_ioctl = NULL;
496 static int ubsec_finish(
ENGINE *e)
498 free_UBSEC_LIBNAME();
499 if(ubsec_dso == NULL)
510 p_UBSEC_ubsec_bytes_to_bits = NULL;
511 p_UBSEC_ubsec_bits_to_bytes = NULL;
512 p_UBSEC_ubsec_open = NULL;
513 p_UBSEC_ubsec_close = NULL;
514 #ifndef OPENSSL_NO_DH
515 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
516 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
518 #ifndef OPENSSL_NO_RSA
519 p_UBSEC_rsa_mod_exp_ioctl = NULL;
520 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
522 #ifndef OPENSSL_NO_DSA
523 p_UBSEC_dsa_sign_ioctl = NULL;
524 p_UBSEC_dsa_verify_ioctl = NULL;
526 p_UBSEC_math_accelerate_ioctl = NULL;
527 p_UBSEC_rng_ioctl = NULL;
528 p_UBSEC_max_key_len_ioctl = NULL;
532 static int ubsec_ctrl(
ENGINE *e,
int cmd,
long i,
void *p,
void (*
f)(
void))
534 int initialised = ((ubsec_dso == NULL) ? 0 : 1);
548 return set_UBSEC_LIBNAME((
const char *)p);
562 if(ubsec_dso == NULL)
570 if (y_len > max_key_len) {
587 if (p_UBSEC_rsa_mod_exp_ioctl(fd, (
unsigned char *)a->
d,
BN_num_bits(a),
588 (
unsigned char *)m->
d,
BN_num_bits(m), (
unsigned char *)p->
d,
592 p_UBSEC_ubsec_close(fd);
597 p_UBSEC_ubsec_close(fd);
603 #ifndef OPENSSL_NO_RSA
614 to_return = ubsec_mod_exp_crt(r0, I, rsa->
p, rsa->
q, rsa->
dmp1,
638 if (y_len > max_key_len) {
654 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
661 (
unsigned char *)r->
d, &y_len) != 0) {
663 p_UBSEC_ubsec_close(fd);
667 p_UBSEC_ubsec_close(fd);
674 #ifndef OPENSSL_NO_DSA
685 if (!ubsec_mod_exp(rr,a1,p1,m,ctx))
goto end;
687 if (!ubsec_mod_exp(&t,a2,p2,m,ctx))
goto end;
700 return ubsec_mod_exp(r, a, p, m, ctx);
705 #ifndef OPENSSL_NO_RSA
719 ret = (*meth->
bn_mod_exp)(r, a, p, m, ctx, m_ctx);
723 ret = ubsec_mod_exp(r, a, p, m, ctx);
730 #ifndef OPENSSL_NO_DH
732 static int ubsec_mod_exp_dh(
const DH *dh,
BIGNUM *r,
const BIGNUM *a,
736 return ubsec_mod_exp(r, a, p, m, ctx);
740 #ifndef OPENSSL_NO_DSA
741 static DSA_SIG *ubsec_dsa_do_sign(
const unsigned char *dgst,
int dlen,
DSA *dsa)
744 int s_len = 160, r_len = 160, d_len, fd;
745 BIGNUM m, *r=NULL, *s=NULL;
751 if ((s == NULL) || (r==NULL))
754 d_len = p_UBSEC_ubsec_bytes_to_bits((
unsigned char *)dgst, dlen);
756 if(!
bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
757 (!
bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
776 if (p_UBSEC_dsa_sign_ioctl(fd, 0,
777 (
unsigned char *)dgst, d_len,
783 (
unsigned char *)r->
d, &r_len,
784 (
unsigned char *)s->d, &s_len ) != 0) {
788 p_UBSEC_ubsec_close(fd);
795 p_UBSEC_ubsec_close(fd);
797 r->
top = (160+BN_BITS2-1)/BN_BITS2;
798 s->top = (160+BN_BITS2-1)/BN_BITS2;
801 if(to_return == NULL) {
818 static int ubsec_dsa_verify(
const unsigned char *dgst,
int dgst_len,
835 d_len = p_UBSEC_ubsec_bytes_to_bits((
unsigned char *)dgst, dgst_len);
846 if (p_UBSEC_dsa_verify_ioctl(fd, 0,
847 (
unsigned char *)dgst, d_len,
854 (
unsigned char *)v.
d, &v_len) != 0) {
857 p_UBSEC_ubsec_close(fd);
865 p_UBSEC_ubsec_close(fd);
874 #ifndef OPENSSL_NO_DH
875 static int ubsec_dh_compute_key(
unsigned char *
key,
const BIGNUM *pub_key,
DH *dh)
892 if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
901 p_UBSEC_ubsec_close(fd);
909 p_UBSEC_ubsec_close(fd);
911 ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
916 static int ubsec_dh_generate_key(
DH *dh)
934 if (priv_key == NULL)
goto err;
952 if(pub_key == NULL)
goto err;
968 if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
969 (
unsigned char *)priv_key->
d, &priv_key_len,
970 (
unsigned char *)pub_key->
d, &pub_key_len,
973 0, 0, random_bits) != 0)
979 p_UBSEC_ubsec_close(fd);
987 p_UBSEC_ubsec_close(fd);
990 dh->
pub_key->
top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
992 dh->
priv_key->
top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
1001 static int ubsec_rand_bytes(
unsigned char * buf,
1011 num = p_UBSEC_ubsec_bits_to_bytes(num);
1013 meth->
seed(buf, num);
1014 ret = meth->
bytes(buf, num);
1020 if (p_UBSEC_rng_ioctl(fd,
1029 p_UBSEC_ubsec_close(fd);
1031 num = p_UBSEC_ubsec_bits_to_bytes(num);
1033 meth->
seed(buf, num);
1034 ret = meth->
bytes(buf, num);
1039 p_UBSEC_ubsec_close(fd);
1047 static int ubsec_rand_status(
void)
1055 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
1056 static int bind_fn(
ENGINE *e,
const char *
id)
1058 if(
id && (strcmp(
id, engine_ubsec_id) != 0))