e_ubsec.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071
  1. /* crypto/engine/hw_ubsec.c */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
  3. * project 2000.
  4. *
  5. * Cloned shamelessly by Joe Tardo.
  6. */
  7. /* ====================================================================
  8. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. All advertising materials mentioning features or use of this
  23. * software must display the following acknowledgment:
  24. * "This product includes software developed by the OpenSSL Project
  25. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  26. *
  27. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  28. * endorse or promote products derived from this software without
  29. * prior written permission. For written permission, please contact
  30. * licensing@OpenSSL.org.
  31. *
  32. * 5. Products derived from this software may not be called "OpenSSL"
  33. * nor may "OpenSSL" appear in their names without prior written
  34. * permission of the OpenSSL Project.
  35. *
  36. * 6. Redistributions of any form whatsoever must retain the following
  37. * acknowledgment:
  38. * "This product includes software developed by the OpenSSL Project
  39. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  42. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  44. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  47. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  48. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  50. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  52. * OF THE POSSIBILITY OF SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This product includes cryptographic software written by Eric Young
  56. * (eay@cryptsoft.com). This product includes software written by Tim
  57. * Hudson (tjh@cryptsoft.com).
  58. *
  59. */
  60. #include <stdio.h>
  61. #include <string.h>
  62. #include <openssl/crypto.h>
  63. #include <openssl/buffer.h>
  64. #include <openssl/dso.h>
  65. #include <openssl/engine.h>
  66. #ifndef OPENSSL_NO_RSA
  67. #include <openssl/rsa.h>
  68. #endif
  69. #ifndef OPENSSL_NO_DSA
  70. #include <openssl/dsa.h>
  71. #endif
  72. #ifndef OPENSSL_NO_DH
  73. #include <openssl/dh.h>
  74. #endif
  75. #include <openssl/bn.h>
  76. #ifndef OPENSSL_NO_HW
  77. #ifndef OPENSSL_NO_HW_UBSEC
  78. #ifdef FLAT_INC
  79. #include "hw_ubsec.h"
  80. #else
  81. #include "vendor_defns/hw_ubsec.h"
  82. #endif
  83. #define UBSEC_LIB_NAME "ubsec engine"
  84. #include "e_ubsec_err.c"
  85. #define FAIL_TO_SOFTWARE -15
  86. static int ubsec_destroy(ENGINE *e);
  87. static int ubsec_init(ENGINE *e);
  88. static int ubsec_finish(ENGINE *e);
  89. static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
  90. static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  91. const BIGNUM *m, BN_CTX *ctx);
  92. #ifndef OPENSSL_NO_RSA
  93. static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  94. const BIGNUM *q, const BIGNUM *dp,
  95. const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
  96. static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  97. static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  98. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
  99. #endif
  100. #ifndef OPENSSL_NO_DSA
  101. #ifdef NOT_USED
  102. static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  103. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  104. BN_CTX *ctx, BN_MONT_CTX *in_mont);
  105. static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  106. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  107. BN_MONT_CTX *m_ctx);
  108. #endif
  109. static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
  110. static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
  111. DSA_SIG *sig, DSA *dsa);
  112. #endif
  113. #ifndef OPENSSL_NO_DH
  114. static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  115. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  116. BN_MONT_CTX *m_ctx);
  117. static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
  118. static int ubsec_dh_generate_key(DH *dh);
  119. #endif
  120. #ifdef NOT_USED
  121. static int ubsec_rand_bytes(unsigned char *buf, int num);
  122. static int ubsec_rand_status(void);
  123. #endif
  124. #define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
  125. static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
  126. {UBSEC_CMD_SO_PATH,
  127. "SO_PATH",
  128. "Specifies the path to the 'ubsec' shared library",
  129. ENGINE_CMD_FLAG_STRING},
  130. {0, NULL, NULL, 0}
  131. };
  132. #ifndef OPENSSL_NO_RSA
  133. /* Our internal RSA_METHOD that we provide pointers to */
  134. static RSA_METHOD ubsec_rsa =
  135. {
  136. "UBSEC RSA method",
  137. NULL,
  138. NULL,
  139. NULL,
  140. NULL,
  141. ubsec_rsa_mod_exp,
  142. ubsec_mod_exp_mont,
  143. NULL,
  144. NULL,
  145. 0,
  146. NULL,
  147. NULL,
  148. NULL,
  149. NULL
  150. };
  151. #endif
  152. #ifndef OPENSSL_NO_DSA
  153. /* Our internal DSA_METHOD that we provide pointers to */
  154. static DSA_METHOD ubsec_dsa =
  155. {
  156. "UBSEC DSA method",
  157. ubsec_dsa_do_sign, /* dsa_do_sign */
  158. NULL, /* dsa_sign_setup */
  159. ubsec_dsa_verify, /* dsa_do_verify */
  160. NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
  161. NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
  162. NULL, /* init */
  163. NULL, /* finish */
  164. 0, /* flags */
  165. NULL, /* app_data */
  166. NULL, /* dsa_paramgen */
  167. NULL /* dsa_keygen */
  168. };
  169. #endif
  170. #ifndef OPENSSL_NO_DH
  171. /* Our internal DH_METHOD that we provide pointers to */
  172. static DH_METHOD ubsec_dh =
  173. {
  174. "UBSEC DH method",
  175. ubsec_dh_generate_key,
  176. ubsec_dh_compute_key,
  177. ubsec_mod_exp_dh,
  178. NULL,
  179. NULL,
  180. 0,
  181. NULL,
  182. NULL
  183. };
  184. #endif
  185. /* Constants used when creating the ENGINE */
  186. static const char *engine_ubsec_id = "ubsec";
  187. static const char *engine_ubsec_name = "UBSEC hardware engine support";
  188. /* This internal function is used by ENGINE_ubsec() and possibly by the
  189. * "dynamic" ENGINE support too */
  190. static int bind_helper(ENGINE *e)
  191. {
  192. #ifndef OPENSSL_NO_RSA
  193. const RSA_METHOD *meth1;
  194. #endif
  195. #ifndef OPENSSL_NO_DH
  196. #ifndef HAVE_UBSEC_DH
  197. const DH_METHOD *meth3;
  198. #endif /* HAVE_UBSEC_DH */
  199. #endif
  200. if(!ENGINE_set_id(e, engine_ubsec_id) ||
  201. !ENGINE_set_name(e, engine_ubsec_name) ||
  202. #ifndef OPENSSL_NO_RSA
  203. !ENGINE_set_RSA(e, &ubsec_rsa) ||
  204. #endif
  205. #ifndef OPENSSL_NO_DSA
  206. !ENGINE_set_DSA(e, &ubsec_dsa) ||
  207. #endif
  208. #ifndef OPENSSL_NO_DH
  209. !ENGINE_set_DH(e, &ubsec_dh) ||
  210. #endif
  211. !ENGINE_set_destroy_function(e, ubsec_destroy) ||
  212. !ENGINE_set_init_function(e, ubsec_init) ||
  213. !ENGINE_set_finish_function(e, ubsec_finish) ||
  214. !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
  215. !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
  216. return 0;
  217. #ifndef OPENSSL_NO_RSA
  218. /* We know that the "PKCS1_SSLeay()" functions hook properly
  219. * to the Broadcom-specific mod_exp and mod_exp_crt so we use
  220. * those functions. NB: We don't use ENGINE_openssl() or
  221. * anything "more generic" because something like the RSAref
  222. * code may not hook properly, and if you own one of these
  223. * cards then you have the right to do RSA operations on it
  224. * anyway! */
  225. meth1 = RSA_PKCS1_SSLeay();
  226. ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  227. ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  228. ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  229. ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  230. #endif
  231. #ifndef OPENSSL_NO_DH
  232. #ifndef HAVE_UBSEC_DH
  233. /* Much the same for Diffie-Hellman */
  234. meth3 = DH_OpenSSL();
  235. ubsec_dh.generate_key = meth3->generate_key;
  236. ubsec_dh.compute_key = meth3->compute_key;
  237. #endif /* HAVE_UBSEC_DH */
  238. #endif
  239. /* Ensure the ubsec error handling is set up */
  240. ERR_load_UBSEC_strings();
  241. return 1;
  242. }
  243. #ifdef OPENSSL_NO_DYNAMIC_ENGINE
  244. static ENGINE *engine_ubsec(void)
  245. {
  246. ENGINE *ret = ENGINE_new();
  247. if(!ret)
  248. return NULL;
  249. if(!bind_helper(ret))
  250. {
  251. ENGINE_free(ret);
  252. return NULL;
  253. }
  254. return ret;
  255. }
  256. void ENGINE_load_ubsec(void)
  257. {
  258. /* Copied from eng_[openssl|dyn].c */
  259. ENGINE *toadd = engine_ubsec();
  260. if(!toadd) return;
  261. ENGINE_add(toadd);
  262. ENGINE_free(toadd);
  263. ERR_clear_error();
  264. }
  265. #endif
  266. /* This is a process-global DSO handle used for loading and unloading
  267. * the UBSEC library. NB: This is only set (or unset) during an
  268. * init() or finish() call (reference counts permitting) and they're
  269. * operating with global locks, so this should be thread-safe
  270. * implicitly. */
  271. static DSO *ubsec_dso = NULL;
  272. /* These are the function pointers that are (un)set when the library has
  273. * successfully (un)loaded. */
  274. static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
  275. static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
  276. static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
  277. static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
  278. #ifndef OPENSSL_NO_DH
  279. static t_UBSEC_diffie_hellman_generate_ioctl
  280. *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
  281. static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
  282. #endif
  283. static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
  284. #ifndef OPENSSL_NO_RSA
  285. static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
  286. #endif
  287. #ifndef OPENSSL_NO_DSA
  288. static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
  289. static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
  290. #endif
  291. static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
  292. static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
  293. static t_UBSEC_max_key_len_ioctl *p_UBSEC_max_key_len_ioctl = NULL;
  294. static int max_key_len = 1024; /* ??? */
  295. /*
  296. * These are the static string constants for the DSO file name and the function
  297. * symbol names to bind to.
  298. */
  299. static const char *UBSEC_LIBNAME = NULL;
  300. static const char *get_UBSEC_LIBNAME(void)
  301. {
  302. if(UBSEC_LIBNAME)
  303. return UBSEC_LIBNAME;
  304. return "ubsec";
  305. }
  306. static void free_UBSEC_LIBNAME(void)
  307. {
  308. if(UBSEC_LIBNAME)
  309. OPENSSL_free((void*)UBSEC_LIBNAME);
  310. UBSEC_LIBNAME = NULL;
  311. }
  312. static long set_UBSEC_LIBNAME(const char *name)
  313. {
  314. free_UBSEC_LIBNAME();
  315. return (((UBSEC_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
  316. }
  317. static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
  318. static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
  319. static const char *UBSEC_F3 = "ubsec_open";
  320. static const char *UBSEC_F4 = "ubsec_close";
  321. #ifndef OPENSSL_NO_DH
  322. static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
  323. static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
  324. #endif
  325. /* #ifndef OPENSSL_NO_RSA */
  326. static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
  327. static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
  328. /* #endif */
  329. #ifndef OPENSSL_NO_DSA
  330. static const char *UBSEC_F9 = "dsa_sign_ioctl";
  331. static const char *UBSEC_F10 = "dsa_verify_ioctl";
  332. #endif
  333. static const char *UBSEC_F11 = "math_accelerate_ioctl";
  334. static const char *UBSEC_F12 = "rng_ioctl";
  335. static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl";
  336. /* Destructor (complements the "ENGINE_ubsec()" constructor) */
  337. static int ubsec_destroy(ENGINE *e)
  338. {
  339. free_UBSEC_LIBNAME();
  340. ERR_unload_UBSEC_strings();
  341. return 1;
  342. }
  343. /* (de)initialisation functions. */
  344. static int ubsec_init(ENGINE *e)
  345. {
  346. t_UBSEC_ubsec_bytes_to_bits *p1;
  347. t_UBSEC_ubsec_bits_to_bytes *p2;
  348. t_UBSEC_ubsec_open *p3;
  349. t_UBSEC_ubsec_close *p4;
  350. #ifndef OPENSSL_NO_DH
  351. t_UBSEC_diffie_hellman_generate_ioctl *p5;
  352. t_UBSEC_diffie_hellman_agree_ioctl *p6;
  353. #endif
  354. /* #ifndef OPENSSL_NO_RSA */
  355. t_UBSEC_rsa_mod_exp_ioctl *p7;
  356. t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
  357. /* #endif */
  358. #ifndef OPENSSL_NO_DSA
  359. t_UBSEC_dsa_sign_ioctl *p9;
  360. t_UBSEC_dsa_verify_ioctl *p10;
  361. #endif
  362. t_UBSEC_math_accelerate_ioctl *p11;
  363. t_UBSEC_rng_ioctl *p12;
  364. t_UBSEC_max_key_len_ioctl *p13;
  365. int fd = 0;
  366. if(ubsec_dso != NULL)
  367. {
  368. UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
  369. goto err;
  370. }
  371. /*
  372. * Attempt to load libubsec.so/ubsec.dll/whatever.
  373. */
  374. ubsec_dso = DSO_load(NULL, get_UBSEC_LIBNAME(), NULL, 0);
  375. if(ubsec_dso == NULL)
  376. {
  377. UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
  378. goto err;
  379. }
  380. if (
  381. !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
  382. !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
  383. !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
  384. !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
  385. #ifndef OPENSSL_NO_DH
  386. !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *)
  387. DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
  388. !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *)
  389. DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
  390. #endif
  391. /* #ifndef OPENSSL_NO_RSA */
  392. !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
  393. !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
  394. /* #endif */
  395. #ifndef OPENSSL_NO_DSA
  396. !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
  397. !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
  398. #endif
  399. !(p11 = (t_UBSEC_math_accelerate_ioctl *)
  400. DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
  401. !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) ||
  402. !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13)))
  403. {
  404. UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
  405. goto err;
  406. }
  407. /* Copy the pointers */
  408. p_UBSEC_ubsec_bytes_to_bits = p1;
  409. p_UBSEC_ubsec_bits_to_bytes = p2;
  410. p_UBSEC_ubsec_open = p3;
  411. p_UBSEC_ubsec_close = p4;
  412. #ifndef OPENSSL_NO_DH
  413. p_UBSEC_diffie_hellman_generate_ioctl = p5;
  414. p_UBSEC_diffie_hellman_agree_ioctl = p6;
  415. #endif
  416. #ifndef OPENSSL_NO_RSA
  417. p_UBSEC_rsa_mod_exp_ioctl = p7;
  418. p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
  419. #endif
  420. #ifndef OPENSSL_NO_DSA
  421. p_UBSEC_dsa_sign_ioctl = p9;
  422. p_UBSEC_dsa_verify_ioctl = p10;
  423. #endif
  424. p_UBSEC_math_accelerate_ioctl = p11;
  425. p_UBSEC_rng_ioctl = p12;
  426. p_UBSEC_max_key_len_ioctl = p13;
  427. /* Perform an open to see if there's actually any unit running. */
  428. if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
  429. {
  430. p_UBSEC_ubsec_close(fd);
  431. return 1;
  432. }
  433. else
  434. {
  435. UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
  436. }
  437. err:
  438. if(ubsec_dso)
  439. DSO_free(ubsec_dso);
  440. ubsec_dso = NULL;
  441. p_UBSEC_ubsec_bytes_to_bits = NULL;
  442. p_UBSEC_ubsec_bits_to_bytes = NULL;
  443. p_UBSEC_ubsec_open = NULL;
  444. p_UBSEC_ubsec_close = NULL;
  445. #ifndef OPENSSL_NO_DH
  446. p_UBSEC_diffie_hellman_generate_ioctl = NULL;
  447. p_UBSEC_diffie_hellman_agree_ioctl = NULL;
  448. #endif
  449. #ifndef OPENSSL_NO_RSA
  450. p_UBSEC_rsa_mod_exp_ioctl = NULL;
  451. p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
  452. #endif
  453. #ifndef OPENSSL_NO_DSA
  454. p_UBSEC_dsa_sign_ioctl = NULL;
  455. p_UBSEC_dsa_verify_ioctl = NULL;
  456. #endif
  457. p_UBSEC_math_accelerate_ioctl = NULL;
  458. p_UBSEC_rng_ioctl = NULL;
  459. p_UBSEC_max_key_len_ioctl = NULL;
  460. return 0;
  461. }
  462. static int ubsec_finish(ENGINE *e)
  463. {
  464. free_UBSEC_LIBNAME();
  465. if(ubsec_dso == NULL)
  466. {
  467. UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
  468. return 0;
  469. }
  470. if(!DSO_free(ubsec_dso))
  471. {
  472. UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
  473. return 0;
  474. }
  475. ubsec_dso = NULL;
  476. p_UBSEC_ubsec_bytes_to_bits = NULL;
  477. p_UBSEC_ubsec_bits_to_bytes = NULL;
  478. p_UBSEC_ubsec_open = NULL;
  479. p_UBSEC_ubsec_close = NULL;
  480. #ifndef OPENSSL_NO_DH
  481. p_UBSEC_diffie_hellman_generate_ioctl = NULL;
  482. p_UBSEC_diffie_hellman_agree_ioctl = NULL;
  483. #endif
  484. #ifndef OPENSSL_NO_RSA
  485. p_UBSEC_rsa_mod_exp_ioctl = NULL;
  486. p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
  487. #endif
  488. #ifndef OPENSSL_NO_DSA
  489. p_UBSEC_dsa_sign_ioctl = NULL;
  490. p_UBSEC_dsa_verify_ioctl = NULL;
  491. #endif
  492. p_UBSEC_math_accelerate_ioctl = NULL;
  493. p_UBSEC_rng_ioctl = NULL;
  494. p_UBSEC_max_key_len_ioctl = NULL;
  495. return 1;
  496. }
  497. static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  498. {
  499. int initialised = ((ubsec_dso == NULL) ? 0 : 1);
  500. switch(cmd)
  501. {
  502. case UBSEC_CMD_SO_PATH:
  503. if(p == NULL)
  504. {
  505. UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
  506. return 0;
  507. }
  508. if(initialised)
  509. {
  510. UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
  511. return 0;
  512. }
  513. return set_UBSEC_LIBNAME((const char *)p);
  514. default:
  515. break;
  516. }
  517. UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  518. return 0;
  519. }
  520. static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  521. const BIGNUM *m, BN_CTX *ctx)
  522. {
  523. int y_len = 0;
  524. int fd;
  525. if(ubsec_dso == NULL)
  526. {
  527. UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
  528. return 0;
  529. }
  530. /* Check if hardware can't handle this argument. */
  531. y_len = BN_num_bits(m);
  532. if (y_len > max_key_len) {
  533. UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  534. return BN_mod_exp(r, a, p, m, ctx);
  535. }
  536. if(!bn_wexpand(r, m->top))
  537. {
  538. UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
  539. return 0;
  540. }
  541. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
  542. fd = 0;
  543. UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_UNIT_FAILURE);
  544. return BN_mod_exp(r, a, p, m, ctx);
  545. }
  546. if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
  547. (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d,
  548. BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
  549. {
  550. UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
  551. p_UBSEC_ubsec_close(fd);
  552. return BN_mod_exp(r, a, p, m, ctx);
  553. }
  554. p_UBSEC_ubsec_close(fd);
  555. r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
  556. return 1;
  557. }
  558. #ifndef OPENSSL_NO_RSA
  559. static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  560. {
  561. int to_return = 0;
  562. if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
  563. {
  564. UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
  565. goto err;
  566. }
  567. to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
  568. rsa->dmq1, rsa->iqmp, ctx);
  569. if (to_return == FAIL_TO_SOFTWARE)
  570. {
  571. /*
  572. * Do in software as hardware failed.
  573. */
  574. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  575. to_return = (*meth->rsa_mod_exp)(r0, I, rsa, ctx);
  576. }
  577. err:
  578. return to_return;
  579. }
  580. static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  581. const BIGNUM *q, const BIGNUM *dp,
  582. const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
  583. {
  584. int y_len,
  585. m_len,
  586. fd;
  587. m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
  588. y_len = BN_num_bits(p) + BN_num_bits(q);
  589. /* Check if hardware can't handle this argument. */
  590. if (y_len > max_key_len) {
  591. UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  592. return FAIL_TO_SOFTWARE;
  593. }
  594. if (!bn_wexpand(r, p->top + q->top + 1)) {
  595. UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
  596. return 0;
  597. }
  598. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
  599. fd = 0;
  600. UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_UNIT_FAILURE);
  601. return FAIL_TO_SOFTWARE;
  602. }
  603. if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
  604. (unsigned char *)a->d, BN_num_bits(a),
  605. (unsigned char *)qinv->d, BN_num_bits(qinv),
  606. (unsigned char *)dp->d, BN_num_bits(dp),
  607. (unsigned char *)p->d, BN_num_bits(p),
  608. (unsigned char *)dq->d, BN_num_bits(dq),
  609. (unsigned char *)q->d, BN_num_bits(q),
  610. (unsigned char *)r->d, &y_len) != 0) {
  611. UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT, UBSEC_R_REQUEST_FAILED);
  612. p_UBSEC_ubsec_close(fd);
  613. return FAIL_TO_SOFTWARE;
  614. }
  615. p_UBSEC_ubsec_close(fd);
  616. r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
  617. return 1;
  618. }
  619. #endif
  620. #ifndef OPENSSL_NO_DSA
  621. #ifdef NOT_USED
  622. static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  623. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  624. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  625. {
  626. BIGNUM t;
  627. int to_return = 0;
  628. BN_init(&t);
  629. /* let rr = a1 ^ p1 mod m */
  630. if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
  631. /* let t = a2 ^ p2 mod m */
  632. if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
  633. /* let rr = rr * t mod m */
  634. if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
  635. to_return = 1;
  636. end:
  637. BN_free(&t);
  638. return to_return;
  639. }
  640. static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  641. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  642. BN_MONT_CTX *m_ctx)
  643. {
  644. return ubsec_mod_exp(r, a, p, m, ctx);
  645. }
  646. #endif
  647. #endif
  648. #ifndef OPENSSL_NO_RSA
  649. /*
  650. * This function is aliased to mod_exp (with the mont stuff dropped).
  651. */
  652. static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  653. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  654. {
  655. int ret = 0;
  656. /* Do in software if the key is too large for the hardware. */
  657. if (BN_num_bits(m) > max_key_len)
  658. {
  659. const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
  660. ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
  661. }
  662. else
  663. {
  664. ret = ubsec_mod_exp(r, a, p, m, ctx);
  665. }
  666. return ret;
  667. }
  668. #endif
  669. #ifndef OPENSSL_NO_DH
  670. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  671. static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  672. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  673. BN_MONT_CTX *m_ctx)
  674. {
  675. return ubsec_mod_exp(r, a, p, m, ctx);
  676. }
  677. #endif
  678. #ifndef OPENSSL_NO_DSA
  679. static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
  680. {
  681. DSA_SIG *to_return = NULL;
  682. int s_len = 160, r_len = 160, d_len, fd;
  683. BIGNUM m, *r=NULL, *s=NULL;
  684. BN_init(&m);
  685. s = BN_new();
  686. r = BN_new();
  687. if ((s == NULL) || (r==NULL))
  688. goto err;
  689. d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
  690. if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
  691. (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
  692. UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_BN_EXPAND_FAIL);
  693. goto err;
  694. }
  695. if (BN_bin2bn(dgst,dlen,&m) == NULL) {
  696. UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_BN_EXPAND_FAIL);
  697. goto err;
  698. }
  699. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
  700. const DSA_METHOD *meth;
  701. fd = 0;
  702. UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_UNIT_FAILURE);
  703. meth = DSA_OpenSSL();
  704. to_return = meth->dsa_do_sign(dgst, dlen, dsa);
  705. goto err;
  706. }
  707. if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
  708. (unsigned char *)dgst, d_len,
  709. NULL, 0, /* compute random value */
  710. (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
  711. (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
  712. (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
  713. (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
  714. (unsigned char *)r->d, &r_len,
  715. (unsigned char *)s->d, &s_len ) != 0) {
  716. const DSA_METHOD *meth;
  717. UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_REQUEST_FAILED);
  718. p_UBSEC_ubsec_close(fd);
  719. meth = DSA_OpenSSL();
  720. to_return = meth->dsa_do_sign(dgst, dlen, dsa);
  721. goto err;
  722. }
  723. p_UBSEC_ubsec_close(fd);
  724. r->top = (160+BN_BITS2-1)/BN_BITS2;
  725. s->top = (160+BN_BITS2-1)/BN_BITS2;
  726. to_return = DSA_SIG_new();
  727. if(to_return == NULL) {
  728. UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN, UBSEC_R_BN_EXPAND_FAIL);
  729. goto err;
  730. }
  731. to_return->r = r;
  732. to_return->s = s;
  733. err:
  734. if (!to_return) {
  735. if (r) BN_free(r);
  736. if (s) BN_free(s);
  737. }
  738. BN_clear_free(&m);
  739. return to_return;
  740. }
  741. static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
  742. DSA_SIG *sig, DSA *dsa)
  743. {
  744. int v_len, d_len;
  745. int to_return = 0;
  746. int fd;
  747. BIGNUM v, *pv = &v;
  748. BN_init(&v);
  749. if(!bn_wexpand(pv, dsa->p->top)) {
  750. UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY, UBSEC_R_BN_EXPAND_FAIL);
  751. goto err;
  752. }
  753. v_len = BN_num_bits(dsa->p);
  754. d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
  755. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
  756. const DSA_METHOD *meth;
  757. fd = 0;
  758. UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY, UBSEC_R_UNIT_FAILURE);
  759. meth = DSA_OpenSSL();
  760. to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
  761. goto err;
  762. }
  763. if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
  764. (unsigned char *)dgst, d_len,
  765. (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
  766. (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
  767. (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
  768. (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
  769. (unsigned char *)sig->r->d, BN_num_bits(sig->r),
  770. (unsigned char *)sig->s->d, BN_num_bits(sig->s),
  771. (unsigned char *)v.d, &v_len) != 0) {
  772. const DSA_METHOD *meth;
  773. UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY, UBSEC_R_REQUEST_FAILED);
  774. p_UBSEC_ubsec_close(fd);
  775. meth = DSA_OpenSSL();
  776. to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
  777. goto err;
  778. }
  779. p_UBSEC_ubsec_close(fd);
  780. to_return = 1;
  781. err:
  782. BN_clear_free(&v);
  783. return to_return;
  784. }
  785. #endif
  786. #ifndef OPENSSL_NO_DH
  787. static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh)
  788. {
  789. int ret = -1,
  790. k_len,
  791. fd;
  792. k_len = BN_num_bits(dh->p);
  793. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
  794. {
  795. const DH_METHOD *meth;
  796. UBSECerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_UNIT_FAILURE);
  797. meth = DH_OpenSSL();
  798. ret = meth->compute_key(key, pub_key, dh);
  799. goto err;
  800. }
  801. if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
  802. (unsigned char *)dh->priv_key->d, BN_num_bits(dh->priv_key),
  803. (unsigned char *)pub_key->d, BN_num_bits(pub_key),
  804. (unsigned char *)dh->p->d, BN_num_bits(dh->p),
  805. key, &k_len) != 0)
  806. {
  807. /* Hardware's a no go, failover to software */
  808. const DH_METHOD *meth;
  809. UBSECerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
  810. p_UBSEC_ubsec_close(fd);
  811. meth = DH_OpenSSL();
  812. ret = meth->compute_key(key, pub_key, dh);
  813. goto err;
  814. }
  815. p_UBSEC_ubsec_close(fd);
  816. ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
  817. err:
  818. return ret;
  819. }
  820. static int ubsec_dh_generate_key(DH *dh)
  821. {
  822. int ret = 0,
  823. random_bits = 0,
  824. pub_key_len = 0,
  825. priv_key_len = 0,
  826. fd;
  827. BIGNUM *pub_key = NULL;
  828. BIGNUM *priv_key = NULL;
  829. /*
  830. * How many bits should Random x be? dh_key.c
  831. * sets the range from 0 to num_bits(modulus) ???
  832. */
  833. if (dh->priv_key == NULL)
  834. {
  835. priv_key = BN_new();
  836. if (priv_key == NULL) goto err;
  837. priv_key_len = BN_num_bits(dh->p);
  838. bn_wexpand(priv_key, dh->p->top);
  839. do
  840. if (!BN_rand_range(priv_key, dh->p)) goto err;
  841. while (BN_is_zero(priv_key));
  842. random_bits = BN_num_bits(priv_key);
  843. }
  844. else
  845. {
  846. priv_key = dh->priv_key;
  847. }
  848. if (dh->pub_key == NULL)
  849. {
  850. pub_key = BN_new();
  851. pub_key_len = BN_num_bits(dh->p);
  852. bn_wexpand(pub_key, dh->p->top);
  853. if(pub_key == NULL) goto err;
  854. }
  855. else
  856. {
  857. pub_key = dh->pub_key;
  858. }
  859. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
  860. {
  861. const DH_METHOD *meth;
  862. UBSECerr(UBSEC_F_UBSEC_DH_GENERATE_KEY, UBSEC_R_UNIT_FAILURE);
  863. meth = DH_OpenSSL();
  864. ret = meth->generate_key(dh);
  865. goto err;
  866. }
  867. if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
  868. (unsigned char *)priv_key->d, &priv_key_len,
  869. (unsigned char *)pub_key->d, &pub_key_len,
  870. (unsigned char *)dh->g->d, BN_num_bits(dh->g),
  871. (unsigned char *)dh->p->d, BN_num_bits(dh->p),
  872. 0, 0, random_bits) != 0)
  873. {
  874. /* Hardware's a no go, failover to software */
  875. const DH_METHOD *meth;
  876. UBSECerr(UBSEC_F_UBSEC_DH_GENERATE_KEY, UBSEC_R_REQUEST_FAILED);
  877. p_UBSEC_ubsec_close(fd);
  878. meth = DH_OpenSSL();
  879. ret = meth->generate_key(dh);
  880. goto err;
  881. }
  882. p_UBSEC_ubsec_close(fd);
  883. dh->pub_key = pub_key;
  884. dh->pub_key->top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
  885. dh->priv_key = priv_key;
  886. dh->priv_key->top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
  887. ret = 1;
  888. err:
  889. return ret;
  890. }
  891. #endif
  892. #ifdef NOT_USED
  893. static int ubsec_rand_bytes(unsigned char * buf,
  894. int num)
  895. {
  896. int ret = 0,
  897. fd;
  898. if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
  899. {
  900. const RAND_METHOD *meth;
  901. UBSECerr(UBSEC_F_UBSEC_RAND_BYTES, UBSEC_R_UNIT_FAILURE);
  902. num = p_UBSEC_ubsec_bits_to_bytes(num);
  903. meth = RAND_SSLeay();
  904. meth->seed(buf, num);
  905. ret = meth->bytes(buf, num);
  906. goto err;
  907. }
  908. num *= 8; /* bytes to bits */
  909. if (p_UBSEC_rng_ioctl(fd,
  910. UBSEC_RNG_DIRECT,
  911. buf,
  912. &num) != 0)
  913. {
  914. /* Hardware's a no go, failover to software */
  915. const RAND_METHOD *meth;
  916. UBSECerr(UBSEC_F_UBSEC_RAND_BYTES, UBSEC_R_REQUEST_FAILED);
  917. p_UBSEC_ubsec_close(fd);
  918. num = p_UBSEC_ubsec_bits_to_bytes(num);
  919. meth = RAND_SSLeay();
  920. meth->seed(buf, num);
  921. ret = meth->bytes(buf, num);
  922. goto err;
  923. }
  924. p_UBSEC_ubsec_close(fd);
  925. ret = 1;
  926. err:
  927. return(ret);
  928. }
  929. static int ubsec_rand_status(void)
  930. {
  931. return 0;
  932. }
  933. #endif
  934. /* This stuff is needed if this ENGINE is being compiled into a self-contained
  935. * shared-library. */
  936. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  937. static int bind_fn(ENGINE *e, const char *id)
  938. {
  939. if(id && (strcmp(id, engine_ubsec_id) != 0))
  940. return 0;
  941. if(!bind_helper(e))
  942. return 0;
  943. return 1;
  944. }
  945. IMPLEMENT_DYNAMIC_CHECK_FN()
  946. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  947. #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  948. #endif /* !OPENSSL_NO_HW_UBSEC */
  949. #endif /* !OPENSSL_NO_HW */