e_sureware.c
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:31k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /* Written by Corinne Dive-Reclus(cdive@baltimore.com)
  2. *
  3. * Redistribution and use in source and binary forms, with or without
  4. * modification, are permitted provided that the following conditions
  5. * are met:
  6. *
  7. * 1. Redistributions of source code must retain the above copyright
  8. *    notice, this list of conditions and the following disclaimer. 
  9. *
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. *    notice, this list of conditions and the following disclaimer in
  12. *    the documentation and/or other materials provided with the
  13. *    distribution.
  14. *
  15. * 3. All advertising materials mentioning features or use of this
  16. *    software must display the following acknowledgment:
  17. *    "This product includes software developed by the OpenSSL Project
  18. *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  19. *
  20. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  21. *    endorse or promote products derived from this software without
  22. *    prior written permission. For written permission, please contact
  23. *    licensing@OpenSSL.org.
  24. *
  25. * 5. Products derived from this software may not be called "OpenSSL"
  26. *    nor may "OpenSSL" appear in their names without prior written
  27. *    permission of the OpenSSL Project.
  28. *
  29. * 6. Redistributions of any form whatsoever must retain the following
  30. *    acknowledgment:
  31. *    "This product includes software developed by the OpenSSL Project
  32. *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  33. *
  34. * Written by Corinne Dive-Reclus(cdive@baltimore.com)
  35. *
  36. * Copyright@2001 Baltimore Technologies Ltd.
  37. * All right Reserved.
  38. * *
  39. * THIS FILE IS PROVIDED BY BALTIMORE TECHNOLOGIES ``AS IS'' AND *
  40. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * 
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
  42. * ARE DISCLAIMED.  IN NO EVENT SHALL BALTIMORE TECHNOLOGIES BE LIABLE *
  43. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
  44. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS *
  45. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) *
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT *
  47. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY *
  48. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF *
  49. * SUCH DAMAGE. *
  50. ====================================================================*/
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <openssl/crypto.h>
  54. #include <openssl/pem.h>
  55. #include <openssl/dso.h>
  56. #include <openssl/engine.h>
  57. #include <openssl/rand.h>
  58. #ifndef OPENSSL_NO_RSA
  59. #include <openssl/rsa.h>
  60. #endif
  61. #ifndef OPENSSL_NO_DSA
  62. #include <openssl/dsa.h>
  63. #endif
  64. #ifndef OPENSSL_NO_DH
  65. #include <openssl/dh.h>
  66. #endif
  67. #include <openssl/bn.h>
  68. #ifndef OPENSSL_NO_HW
  69. #ifndef OPENSSL_NO_HW_SUREWARE
  70. #ifdef FLAT_INC
  71. #include "sureware.h"
  72. #else
  73. #include "vendor_defns/sureware.h"
  74. #endif
  75. #define SUREWARE_LIB_NAME "sureware engine"
  76. #include "e_sureware_err.c"
  77. static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
  78. static int surewarehk_destroy(ENGINE *e);
  79. static int surewarehk_init(ENGINE *e);
  80. static int surewarehk_finish(ENGINE *e);
  81. static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  82. const BIGNUM *m, BN_CTX *ctx);
  83. /* RSA stuff */
  84. #ifndef OPENSSL_NO_RSA
  85. static int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to,
  86. RSA *rsa,int padding);
  87. static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to,
  88.     RSA *rsa,int padding);
  89. #endif
  90. /* RAND stuff */
  91. static int surewarehk_rand_bytes(unsigned char *buf, int num);
  92. static void surewarehk_rand_seed(const void *buf, int num);
  93. static void surewarehk_rand_add(const void *buf, int num, double entropy);
  94. /* KM stuff */
  95. static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
  96. UI_METHOD *ui_method, void *callback_data);
  97. static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
  98. UI_METHOD *ui_method, void *callback_data);
  99. static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  100. int idx,long argl, void *argp);
  101. #if 0
  102. static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  103. int idx,long argl, void *argp);
  104. #endif
  105. #ifndef OPENSSL_NO_RSA
  106. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  107. static int surewarehk_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  108. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  109. {
  110. return surewarehk_modexp(r, a, p, m, ctx);
  111. }
  112. /* Our internal RSA_METHOD that we provide pointers to */
  113. static RSA_METHOD surewarehk_rsa =
  114. {
  115. "SureWare RSA method",
  116. NULL, /* pub_enc*/
  117. NULL, /* pub_dec*/
  118. surewarehk_rsa_sign, /* our rsa_sign is OpenSSL priv_enc*/
  119. surewarehk_rsa_priv_dec, /* priv_dec*/
  120. NULL, /*mod_exp*/
  121. surewarehk_mod_exp_mont, /*mod_exp_mongomery*/
  122. NULL, /* init*/
  123. NULL, /* finish*/
  124. 0, /* RSA flag*/
  125. NULL, 
  126. NULL, /* OpenSSL sign*/
  127. NULL, /* OpenSSL verify*/
  128. NULL  /* keygen */
  129. };
  130. #endif
  131. #ifndef OPENSSL_NO_DH
  132. /* Our internal DH_METHOD that we provide pointers to */
  133. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  134. static int surewarehk_modexp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
  135. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  136. {
  137. return surewarehk_modexp(r, a, p, m, ctx);
  138. }
  139. static DH_METHOD surewarehk_dh =
  140. {
  141. "SureWare DH method",
  142. NULL,/*gen_key*/
  143. NULL,/*agree,*/
  144. surewarehk_modexp_dh, /*dh mod exp*/
  145. NULL, /* init*/
  146. NULL, /* finish*/
  147. 0,    /* flags*/
  148. NULL,
  149. NULL
  150. };
  151. #endif
  152. static RAND_METHOD surewarehk_rand =
  153. {
  154. /* "SureWare RAND method", */
  155. surewarehk_rand_seed,
  156. surewarehk_rand_bytes,
  157. NULL,/*cleanup*/
  158. surewarehk_rand_add,
  159. surewarehk_rand_bytes,
  160. NULL,/*rand_status*/
  161. };
  162. #ifndef OPENSSL_NO_DSA
  163. /* DSA stuff */
  164. static DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
  165. static int surewarehk_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  166. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  167. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  168. {
  169. BIGNUM t;
  170. int to_return = 0;
  171. BN_init(&t);
  172. /* let rr = a1 ^ p1 mod m */
  173. if (!surewarehk_modexp(rr,a1,p1,m,ctx)) goto end;
  174. /* let t = a2 ^ p2 mod m */
  175. if (!surewarehk_modexp(&t,a2,p2,m,ctx)) goto end;
  176. /* let rr = rr * t mod m */
  177. if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
  178. to_return = 1;
  179. end:
  180. BN_free(&t);
  181. return to_return;
  182. }
  183. static DSA_METHOD surewarehk_dsa =
  184. {
  185.  "SureWare DSA method", 
  186. surewarehk_dsa_do_sign,
  187. NULL,/*sign setup*/
  188. NULL,/*verify,*/
  189. surewarehk_dsa_mod_exp,/*mod exp*/
  190. NULL,/*bn mod exp*/
  191. NULL, /*init*/
  192. NULL,/*finish*/
  193. 0,
  194. NULL,
  195. NULL,
  196. NULL
  197. };
  198. #endif
  199. static const char *engine_sureware_id = "sureware";
  200. static const char *engine_sureware_name = "SureWare hardware engine support";
  201. /* Now, to our own code */
  202. /* As this is only ever called once, there's no need for locking
  203.  * (indeed - the lock will already be held by our caller!!!) */
  204. static int bind_sureware(ENGINE *e)
  205. {
  206. #ifndef OPENSSL_NO_RSA
  207. const RSA_METHOD *meth1;
  208. #endif
  209. #ifndef OPENSSL_NO_DSA
  210. const DSA_METHOD *meth2;
  211. #endif
  212. #ifndef OPENSSL_NO_DH
  213. const DH_METHOD *meth3;
  214. #endif
  215. if(!ENGINE_set_id(e, engine_sureware_id) ||
  216.    !ENGINE_set_name(e, engine_sureware_name) ||
  217. #ifndef OPENSSL_NO_RSA
  218.    !ENGINE_set_RSA(e, &surewarehk_rsa) ||
  219. #endif
  220. #ifndef OPENSSL_NO_DSA
  221.    !ENGINE_set_DSA(e, &surewarehk_dsa) ||
  222. #endif
  223. #ifndef OPENSSL_NO_DH
  224.    !ENGINE_set_DH(e, &surewarehk_dh) ||
  225. #endif
  226.    !ENGINE_set_RAND(e, &surewarehk_rand) ||
  227.    !ENGINE_set_destroy_function(e, surewarehk_destroy) ||
  228.    !ENGINE_set_init_function(e, surewarehk_init) ||
  229.    !ENGINE_set_finish_function(e, surewarehk_finish) ||
  230.    !ENGINE_set_ctrl_function(e, surewarehk_ctrl) ||
  231.    !ENGINE_set_load_privkey_function(e, surewarehk_load_privkey) ||
  232.    !ENGINE_set_load_pubkey_function(e, surewarehk_load_pubkey))
  233.   return 0;
  234. #ifndef OPENSSL_NO_RSA
  235. /* We know that the "PKCS1_SSLeay()" functions hook properly
  236.  * to the cswift-specific mod_exp and mod_exp_crt so we use
  237.  * those functions. NB: We don't use ENGINE_openssl() or
  238.  * anything "more generic" because something like the RSAref
  239.  * code may not hook properly, and if you own one of these
  240.  * cards then you have the right to do RSA operations on it
  241.  * anyway! */ 
  242. meth1 = RSA_PKCS1_SSLeay();
  243. if (meth1)
  244. {
  245. surewarehk_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  246. surewarehk_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  247. }
  248. #endif
  249. #ifndef OPENSSL_NO_DSA
  250. /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
  251.  * bits. */
  252. meth2 = DSA_OpenSSL();
  253. if (meth2)
  254. {
  255. surewarehk_dsa.dsa_do_verify = meth2->dsa_do_verify;
  256. }
  257. #endif
  258. #ifndef OPENSSL_NO_DH
  259. /* Much the same for Diffie-Hellman */
  260. meth3 = DH_OpenSSL();
  261. if (meth3)
  262. {
  263. surewarehk_dh.generate_key = meth3->generate_key;
  264. surewarehk_dh.compute_key = meth3->compute_key;
  265. }
  266. #endif
  267. /* Ensure the sureware error handling is set up */
  268. ERR_load_SUREWARE_strings();
  269. return 1;
  270. }
  271. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  272. static int bind_helper(ENGINE *e, const char *id)
  273. {
  274. if(id && (strcmp(id, engine_sureware_id) != 0))
  275. return 0;
  276. if(!bind_sureware(e))
  277. return 0;
  278. return 1;
  279. }       
  280. IMPLEMENT_DYNAMIC_CHECK_FN()
  281. IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
  282. #else
  283. static ENGINE *engine_sureware(void)
  284. {
  285. ENGINE *ret = ENGINE_new();
  286. if(!ret)
  287. return NULL;
  288. if(!bind_sureware(ret))
  289. {
  290. ENGINE_free(ret);
  291. return NULL;
  292. }
  293. return ret;
  294. }
  295. void ENGINE_load_sureware(void)
  296. {
  297. /* Copied from eng_[openssl|dyn].c */
  298. ENGINE *toadd = engine_sureware();
  299. if(!toadd) return;
  300. ENGINE_add(toadd);
  301. ENGINE_free(toadd);
  302. ERR_clear_error();
  303. }
  304. #endif
  305. /* This is a process-global DSO handle used for loading and unloading
  306.  * the SureWareHook library. NB: This is only set (or unset) during an
  307.  * init() or finish() call (reference counts permitting) and they're
  308.  * operating with global locks, so this should be thread-safe
  309.  * implicitly. */
  310. static DSO *surewarehk_dso = NULL;
  311. #ifndef OPENSSL_NO_RSA
  312. static int rsaHndidx = -1; /* Index for KM handle.  Not really used yet. */
  313. #endif
  314. #ifndef OPENSSL_NO_DSA
  315. static int dsaHndidx = -1; /* Index for KM handle.  Not really used yet. */
  316. #endif
  317. /* These are the function pointers that are (un)set when the library has
  318.  * successfully (un)loaded. */
  319. static SureWareHook_Init_t *p_surewarehk_Init = NULL;
  320. static SureWareHook_Finish_t *p_surewarehk_Finish = NULL;
  321. static SureWareHook_Rand_Bytes_t *p_surewarehk_Rand_Bytes = NULL;
  322. static SureWareHook_Rand_Seed_t *p_surewarehk_Rand_Seed = NULL;
  323. static SureWareHook_Load_Privkey_t *p_surewarehk_Load_Privkey = NULL;
  324. static SureWareHook_Info_Pubkey_t *p_surewarehk_Info_Pubkey = NULL;
  325. static SureWareHook_Load_Rsa_Pubkey_t *p_surewarehk_Load_Rsa_Pubkey = NULL;
  326. static SureWareHook_Load_Dsa_Pubkey_t *p_surewarehk_Load_Dsa_Pubkey = NULL;
  327. static SureWareHook_Free_t *p_surewarehk_Free=NULL;
  328. static SureWareHook_Rsa_Priv_Dec_t *p_surewarehk_Rsa_Priv_Dec=NULL;
  329. static SureWareHook_Rsa_Sign_t *p_surewarehk_Rsa_Sign=NULL;
  330. static SureWareHook_Dsa_Sign_t *p_surewarehk_Dsa_Sign=NULL;
  331. static SureWareHook_Mod_Exp_t *p_surewarehk_Mod_Exp=NULL;
  332. /* Used in the DSO operations. */
  333. static const char *surewarehk_LIBNAME = "SureWareHook";
  334. static const char *n_surewarehk_Init = "SureWareHook_Init";
  335. static const char *n_surewarehk_Finish = "SureWareHook_Finish";
  336. static const char *n_surewarehk_Rand_Bytes="SureWareHook_Rand_Bytes";
  337. static const char *n_surewarehk_Rand_Seed="SureWareHook_Rand_Seed";
  338. static const char *n_surewarehk_Load_Privkey="SureWareHook_Load_Privkey";
  339. static const char *n_surewarehk_Info_Pubkey="SureWareHook_Info_Pubkey";
  340. static const char *n_surewarehk_Load_Rsa_Pubkey="SureWareHook_Load_Rsa_Pubkey";
  341. static const char *n_surewarehk_Load_Dsa_Pubkey="SureWareHook_Load_Dsa_Pubkey";
  342. static const char *n_surewarehk_Free="SureWareHook_Free";
  343. static const char *n_surewarehk_Rsa_Priv_Dec="SureWareHook_Rsa_Priv_Dec";
  344. static const char *n_surewarehk_Rsa_Sign="SureWareHook_Rsa_Sign";
  345. static const char *n_surewarehk_Dsa_Sign="SureWareHook_Dsa_Sign";
  346. static const char *n_surewarehk_Mod_Exp="SureWareHook_Mod_Exp";
  347. static BIO *logstream = NULL;
  348. /* SureWareHook library functions and mechanics - these are used by the
  349.  * higher-level functions further down. NB: As and where there's no
  350.  * error checking, take a look lower down where these functions are
  351.  * called, the checking and error handling is probably down there. 
  352. */
  353. static int threadsafe=1;
  354. static int surewarehk_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  355. {
  356. int to_return = 1;
  357. switch(cmd)
  358. {
  359. case ENGINE_CTRL_SET_LOGSTREAM:
  360. {
  361. BIO *bio = (BIO *)p;
  362. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  363. if (logstream)
  364. {
  365. BIO_free(logstream);
  366. logstream = NULL;
  367. }
  368. if (CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO) > 1)
  369. logstream = bio;
  370. else
  371. SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,SUREWARE_R_BIO_WAS_FREED);
  372. }
  373. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  374. break;
  375. /* This will prevent the initialisation function from "installing"
  376.  * the mutex-handling callbacks, even if they are available from
  377.  * within the library (or were provided to the library from the
  378.  * calling application). This is to remove any baggage for
  379.  * applications not using multithreading. */
  380. case ENGINE_CTRL_CHIL_NO_LOCKING:
  381. CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
  382. threadsafe = 0;
  383. CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
  384. break;
  385. /* The command isn't understood by this engine */
  386. default:
  387. SUREWAREerr(SUREWARE_F_SUREWAREHK_CTRL,
  388. ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  389. to_return = 0;
  390. break;
  391. }
  392. return to_return;
  393. }
  394. /* Destructor (complements the "ENGINE_surewarehk()" constructor) */
  395. static int surewarehk_destroy(ENGINE *e)
  396. {
  397. ERR_unload_SUREWARE_strings();
  398. return 1;
  399. }
  400. /* (de)initialisation functions. */
  401. static int surewarehk_init(ENGINE *e)
  402. {
  403. char msg[64]="ENGINE_init";
  404. SureWareHook_Init_t *p1=NULL;
  405. SureWareHook_Finish_t *p2=NULL;
  406. SureWareHook_Rand_Bytes_t *p3=NULL;
  407. SureWareHook_Rand_Seed_t *p4=NULL;
  408. SureWareHook_Load_Privkey_t *p5=NULL;
  409. SureWareHook_Load_Rsa_Pubkey_t *p6=NULL;
  410. SureWareHook_Free_t *p7=NULL;
  411. SureWareHook_Rsa_Priv_Dec_t *p8=NULL;
  412. SureWareHook_Rsa_Sign_t *p9=NULL;
  413. SureWareHook_Dsa_Sign_t *p12=NULL;
  414. SureWareHook_Info_Pubkey_t *p13=NULL;
  415. SureWareHook_Load_Dsa_Pubkey_t *p14=NULL;
  416. SureWareHook_Mod_Exp_t *p15=NULL;
  417. if(surewarehk_dso != NULL)
  418. {
  419. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_ALREADY_LOADED);
  420. goto err;
  421. }
  422. /* Attempt to load libsurewarehk.so/surewarehk.dll/whatever. */
  423. surewarehk_dso = DSO_load(NULL, surewarehk_LIBNAME, NULL, 0);
  424. if(surewarehk_dso == NULL)
  425. {
  426. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE);
  427. goto err;
  428. }
  429. if(!(p1=(SureWareHook_Init_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Init)) ||
  430.    !(p2=(SureWareHook_Finish_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Finish)) ||
  431.    !(p3=(SureWareHook_Rand_Bytes_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Bytes)) ||
  432.    !(p4=(SureWareHook_Rand_Seed_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rand_Seed)) ||
  433.    !(p5=(SureWareHook_Load_Privkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Privkey)) ||
  434.    !(p6=(SureWareHook_Load_Rsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Rsa_Pubkey)) ||
  435.    !(p7=(SureWareHook_Free_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Free)) ||
  436.    !(p8=(SureWareHook_Rsa_Priv_Dec_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Priv_Dec)) ||
  437.    !(p9=(SureWareHook_Rsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Rsa_Sign)) ||
  438.    !(p12=(SureWareHook_Dsa_Sign_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Dsa_Sign)) ||
  439.    !(p13=(SureWareHook_Info_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Info_Pubkey)) ||
  440.    !(p14=(SureWareHook_Load_Dsa_Pubkey_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Load_Dsa_Pubkey)) ||
  441.    !(p15=(SureWareHook_Mod_Exp_t*)DSO_bind_func(surewarehk_dso, n_surewarehk_Mod_Exp)))
  442. {
  443. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,ENGINE_R_DSO_FAILURE);
  444. goto err;
  445. }
  446. /* Copy the pointers */
  447. p_surewarehk_Init = p1;
  448. p_surewarehk_Finish = p2;
  449. p_surewarehk_Rand_Bytes = p3;
  450. p_surewarehk_Rand_Seed = p4;
  451. p_surewarehk_Load_Privkey = p5;
  452. p_surewarehk_Load_Rsa_Pubkey = p6;
  453. p_surewarehk_Free = p7;
  454. p_surewarehk_Rsa_Priv_Dec = p8;
  455. p_surewarehk_Rsa_Sign = p9;
  456. p_surewarehk_Dsa_Sign = p12;
  457. p_surewarehk_Info_Pubkey = p13;
  458. p_surewarehk_Load_Dsa_Pubkey = p14;
  459. p_surewarehk_Mod_Exp = p15;
  460. /* Contact the hardware and initialises it. */
  461. if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE)
  462. {
  463. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE);
  464. goto err;
  465. }
  466. if(p_surewarehk_Init(msg,threadsafe)==SUREWAREHOOK_ERROR_UNIT_FAILURE)
  467. {
  468. SUREWAREerr(SUREWARE_F_SUREWAREHK_INIT,SUREWARE_R_UNIT_FAILURE);
  469. goto err;
  470. }
  471. /* try to load the default private key, if failed does not return a failure but
  472.            wait for an explicit ENGINE_load_privakey */
  473. surewarehk_load_privkey(e,NULL,NULL,NULL);
  474. /* Everything's fine. */
  475. #ifndef OPENSSL_NO_RSA
  476. if (rsaHndidx == -1)
  477. rsaHndidx = RSA_get_ex_new_index(0,
  478. "SureWareHook RSA key handle",
  479. NULL, NULL, surewarehk_ex_free);
  480. #endif
  481. #ifndef OPENSSL_NO_DSA
  482. if (dsaHndidx == -1)
  483. dsaHndidx = DSA_get_ex_new_index(0,
  484. "SureWareHook DSA key handle",
  485. NULL, NULL, surewarehk_ex_free);
  486. #endif
  487. return 1;
  488. err:
  489. if(surewarehk_dso)
  490. DSO_free(surewarehk_dso);
  491. surewarehk_dso = NULL;
  492. p_surewarehk_Init = NULL;
  493. p_surewarehk_Finish = NULL;
  494. p_surewarehk_Rand_Bytes = NULL;
  495. p_surewarehk_Rand_Seed = NULL;
  496. p_surewarehk_Load_Privkey = NULL;
  497. p_surewarehk_Load_Rsa_Pubkey = NULL;
  498. p_surewarehk_Free = NULL;
  499. p_surewarehk_Rsa_Priv_Dec = NULL;
  500. p_surewarehk_Rsa_Sign = NULL;
  501. p_surewarehk_Dsa_Sign = NULL;
  502. p_surewarehk_Info_Pubkey = NULL;
  503. p_surewarehk_Load_Dsa_Pubkey = NULL;
  504. p_surewarehk_Mod_Exp = NULL;
  505. return 0;
  506. }
  507. static int surewarehk_finish(ENGINE *e)
  508. {
  509. int to_return = 1;
  510. if(surewarehk_dso == NULL)
  511. {
  512. SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH,ENGINE_R_NOT_LOADED);
  513. to_return = 0;
  514. goto err;
  515. }
  516. p_surewarehk_Finish();
  517. if(!DSO_free(surewarehk_dso))
  518. {
  519. SUREWAREerr(SUREWARE_F_SUREWAREHK_FINISH,ENGINE_R_DSO_FAILURE);
  520. to_return = 0;
  521. goto err;
  522. }
  523.  err:
  524. if (logstream)
  525. BIO_free(logstream);
  526. surewarehk_dso = NULL;
  527. p_surewarehk_Init = NULL;
  528. p_surewarehk_Finish = NULL;
  529. p_surewarehk_Rand_Bytes = NULL;
  530. p_surewarehk_Rand_Seed = NULL;
  531. p_surewarehk_Load_Privkey = NULL;
  532. p_surewarehk_Load_Rsa_Pubkey = NULL;
  533. p_surewarehk_Free = NULL;
  534. p_surewarehk_Rsa_Priv_Dec = NULL;
  535. p_surewarehk_Rsa_Sign = NULL;
  536. p_surewarehk_Dsa_Sign = NULL;
  537. p_surewarehk_Info_Pubkey = NULL;
  538. p_surewarehk_Load_Dsa_Pubkey = NULL;
  539. p_surewarehk_Mod_Exp = NULL;
  540. return to_return;
  541. }
  542. static void surewarehk_error_handling(char *const msg,int func,int ret)
  543. {
  544. switch (ret)
  545. {
  546. case SUREWAREHOOK_ERROR_UNIT_FAILURE:
  547. ENGINEerr(func,SUREWARE_R_UNIT_FAILURE);
  548. break;
  549. case SUREWAREHOOK_ERROR_FALLBACK:
  550. ENGINEerr(func,SUREWARE_R_REQUEST_FALLBACK);
  551. break;
  552. case SUREWAREHOOK_ERROR_DATA_SIZE:
  553. ENGINEerr(func,SUREWARE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
  554. break;
  555. case SUREWAREHOOK_ERROR_INVALID_PAD:
  556. ENGINEerr(func,SUREWARE_R_PADDING_CHECK_FAILED);
  557. break;
  558. default:
  559. ENGINEerr(func,SUREWARE_R_REQUEST_FAILED);
  560. break;
  561. case 1:/*nothing*/
  562. msg[0]='';
  563. }
  564. if (*msg)
  565. {
  566. ERR_add_error_data(1,msg);
  567. if (logstream)
  568. {
  569. CRYPTO_w_lock(CRYPTO_LOCK_BIO);
  570. BIO_write(logstream, msg, strlen(msg));
  571. CRYPTO_w_unlock(CRYPTO_LOCK_BIO);
  572. }
  573. }
  574. }
  575. static int surewarehk_rand_bytes(unsigned char *buf, int num)
  576. {
  577. int ret=0;
  578. char msg[64]="ENGINE_rand_bytes";
  579. if(!p_surewarehk_Rand_Bytes)
  580. {
  581. SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_BYTES,ENGINE_R_NOT_INITIALISED);
  582. }
  583. else
  584. {
  585. ret = p_surewarehk_Rand_Bytes(msg,buf, num);
  586. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RAND_BYTES,ret);
  587. }
  588. return ret==1 ? 1 : 0;
  589. }
  590. static void surewarehk_rand_seed(const void *buf, int num)
  591. {
  592. int ret=0;
  593. char msg[64]="ENGINE_rand_seed";
  594. if(!p_surewarehk_Rand_Seed)
  595. {
  596. SUREWAREerr(SUREWARE_F_SUREWAREHK_RAND_SEED,ENGINE_R_NOT_INITIALISED);
  597. }
  598. else
  599. {
  600. ret = p_surewarehk_Rand_Seed(msg,buf, num);
  601. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RAND_SEED,ret);
  602. }
  603. }
  604. static void surewarehk_rand_add(const void *buf, int num, double entropy)
  605. {
  606. surewarehk_rand_seed(buf,num);
  607. }
  608. static EVP_PKEY* sureware_load_public(ENGINE *e,const char *key_id,char *hptr,unsigned long el,char keytype)
  609. {
  610. EVP_PKEY *res = NULL;
  611. #ifndef OPENSSL_NO_RSA
  612. RSA *rsatmp = NULL;
  613. #endif
  614. #ifndef OPENSSL_NO_DSA
  615. DSA *dsatmp=NULL;
  616. #endif
  617. char msg[64]="sureware_load_public";
  618. int ret=0;
  619. if(!p_surewarehk_Load_Rsa_Pubkey || !p_surewarehk_Load_Dsa_Pubkey)
  620. {
  621. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_NOT_INITIALISED);
  622. goto err;
  623. }
  624. switch (keytype)
  625. {
  626. #ifndef OPENSSL_NO_RSA
  627. case 1: /*RSA*/
  628. /* set private external reference */
  629. rsatmp = RSA_new_method(e);
  630. RSA_set_ex_data(rsatmp,rsaHndidx,hptr);
  631. rsatmp->flags |= RSA_FLAG_EXT_PKEY;
  632. /* set public big nums*/
  633. rsatmp->e = BN_new();
  634. rsatmp->n = BN_new();
  635. bn_expand2(rsatmp->e, el/sizeof(BN_ULONG));
  636. bn_expand2(rsatmp->n, el/sizeof(BN_ULONG));
  637. if (!rsatmp->e || rsatmp->e->dmax!=(int)(el/sizeof(BN_ULONG))|| 
  638. !rsatmp->n || rsatmp->n->dmax!=(int)(el/sizeof(BN_ULONG)))
  639. goto err;
  640. ret=p_surewarehk_Load_Rsa_Pubkey(msg,key_id,el,
  641.  (unsigned long *)rsatmp->n->d,
  642.  (unsigned long *)rsatmp->e->d);
  643. surewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);
  644. if (ret!=1)
  645. {
  646. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  647. goto err;
  648. }
  649. /* normalise pub e and pub n */
  650. rsatmp->e->top=el/sizeof(BN_ULONG);
  651. bn_fix_top(rsatmp->e);
  652. rsatmp->n->top=el/sizeof(BN_ULONG);
  653. bn_fix_top(rsatmp->n);
  654. /* create an EVP object: engine + rsa key */
  655. res = EVP_PKEY_new();
  656. EVP_PKEY_assign_RSA(res, rsatmp);
  657. break;
  658. #endif
  659. #ifndef OPENSSL_NO_DSA
  660. case 2:/*DSA*/
  661. /* set private/public external reference */
  662. dsatmp = DSA_new_method(e);
  663. DSA_set_ex_data(dsatmp,dsaHndidx,hptr);
  664. /*dsatmp->flags |= DSA_FLAG_EXT_PKEY;*/
  665. /* set public key*/
  666. dsatmp->pub_key = BN_new();
  667. dsatmp->p = BN_new();
  668. dsatmp->q = BN_new();
  669. dsatmp->g = BN_new();
  670. bn_expand2(dsatmp->pub_key, el/sizeof(BN_ULONG));
  671. bn_expand2(dsatmp->p, el/sizeof(BN_ULONG));
  672. bn_expand2(dsatmp->q, 20/sizeof(BN_ULONG));
  673. bn_expand2(dsatmp->g, el/sizeof(BN_ULONG));
  674. if (!dsatmp->pub_key || dsatmp->pub_key->dmax!=(int)(el/sizeof(BN_ULONG))|| 
  675. !dsatmp->p || dsatmp->p->dmax!=(int)(el/sizeof(BN_ULONG)) ||
  676. !dsatmp->q || dsatmp->q->dmax!=20/sizeof(BN_ULONG) ||
  677. !dsatmp->g || dsatmp->g->dmax!=(int)(el/sizeof(BN_ULONG)))
  678. goto err;
  679. ret=p_surewarehk_Load_Dsa_Pubkey(msg,key_id,el,
  680.  (unsigned long *)dsatmp->pub_key->d, 
  681.  (unsigned long *)dsatmp->p->d,
  682.  (unsigned long *)dsatmp->q->d,
  683.  (unsigned long *)dsatmp->g->d);
  684. surewarehk_error_handling(msg,SUREWARE_F_SUREWARE_LOAD_PUBLIC,ret);
  685. if (ret!=1)
  686. {
  687. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  688. goto err;
  689. }
  690. /* set parameters */
  691. /* normalise pubkey and parameters in case of */
  692. dsatmp->pub_key->top=el/sizeof(BN_ULONG);
  693. bn_fix_top(dsatmp->pub_key);
  694. dsatmp->p->top=el/sizeof(BN_ULONG);
  695. bn_fix_top(dsatmp->p);
  696. dsatmp->q->top=20/sizeof(BN_ULONG);
  697. bn_fix_top(dsatmp->q);
  698. dsatmp->g->top=el/sizeof(BN_ULONG);
  699. bn_fix_top(dsatmp->g);
  700. /* create an EVP object: engine + rsa key */
  701. res = EVP_PKEY_new();
  702. EVP_PKEY_assign_DSA(res, dsatmp);
  703. break;
  704. #endif
  705. default:
  706. SUREWAREerr(SUREWARE_F_SUREWARE_LOAD_PUBLIC,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  707. goto err;
  708. }
  709. return res;
  710.  err:
  711. if (res)
  712. EVP_PKEY_free(res);
  713. #ifndef OPENSSL_NO_RSA
  714. if (rsatmp)
  715. RSA_free(rsatmp);
  716. #endif
  717. #ifndef OPENSSL_NO_DSA
  718. if (dsatmp)
  719. DSA_free(dsatmp);
  720. #endif
  721. return NULL;
  722. }
  723. static EVP_PKEY *surewarehk_load_privkey(ENGINE *e, const char *key_id,
  724.  UI_METHOD *ui_method, void *callback_data)
  725. {
  726. EVP_PKEY *res = NULL;
  727. int ret=0;
  728. unsigned long el=0;
  729. char *hptr=NULL;
  730. char keytype=0;
  731. char msg[64]="ENGINE_load_privkey";
  732. if(!p_surewarehk_Load_Privkey)
  733. {
  734. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,ENGINE_R_NOT_INITIALISED);
  735. }
  736. else
  737. {
  738. ret=p_surewarehk_Load_Privkey(msg,key_id,&hptr,&el,&keytype);
  739. if (ret!=1)
  740. {
  741. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PRIVKEY,ENGINE_R_FAILED_LOADING_PRIVATE_KEY);
  742. ERR_add_error_data(1,msg);
  743. }
  744. else
  745. res=sureware_load_public(e,key_id,hptr,el,keytype);
  746. }
  747. return res;
  748. }
  749. static EVP_PKEY *surewarehk_load_pubkey(ENGINE *e, const char *key_id,
  750.  UI_METHOD *ui_method, void *callback_data)
  751. {
  752. EVP_PKEY *res = NULL;
  753. int ret=0;
  754. unsigned long el=0;
  755. char *hptr=NULL;
  756. char keytype=0;
  757. char msg[64]="ENGINE_load_pubkey";
  758. if(!p_surewarehk_Info_Pubkey)
  759. {
  760. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,ENGINE_R_NOT_INITIALISED);
  761. }
  762. else
  763. {
  764. /* call once to identify if DSA or RSA */
  765. ret=p_surewarehk_Info_Pubkey(msg,key_id,&el,&keytype);
  766. if (ret!=1)
  767. {
  768. SUREWAREerr(SUREWARE_F_SUREWAREHK_LOAD_PUBKEY,ENGINE_R_FAILED_LOADING_PUBLIC_KEY);
  769. ERR_add_error_data(1,msg);
  770. }
  771. else
  772. res=sureware_load_public(e,key_id,hptr,el,keytype);
  773. }
  774. return res;
  775. }
  776. /* This cleans up an RSA/DSA KM key(do not destroy the key into the hardware)
  777. , called when ex_data is freed */
  778. static void surewarehk_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  779. int idx,long argl, void *argp)
  780. {
  781. if(!p_surewarehk_Free)
  782. {
  783. SUREWAREerr(SUREWARE_F_SUREWAREHK_EX_FREE,ENGINE_R_NOT_INITIALISED);
  784. }
  785. else
  786. p_surewarehk_Free((char *)item,0);
  787. }
  788. #if 0
  789. /* not currently used (bug?) */
  790. /* This cleans up an DH KM key (destroys the key into hardware), 
  791. called when ex_data is freed */
  792. static void surewarehk_dh_ex_free(void *obj, void *item, CRYPTO_EX_DATA *ad,
  793. int idx,long argl, void *argp)
  794. {
  795. if(!p_surewarehk_Free)
  796. {
  797. SUREWAREerr(SUREWARE_F_SUREWAREHK_DH_EX_FREE,ENGINE_R_NOT_INITIALISED);
  798. }
  799. else
  800. p_surewarehk_Free((char *)item,1);
  801. }
  802. #endif
  803. /*
  804. * return number of decrypted bytes
  805. */
  806. #ifndef OPENSSL_NO_RSA
  807. static int surewarehk_rsa_priv_dec(int flen,const unsigned char *from,unsigned char *to,
  808. RSA *rsa,int padding)
  809. {
  810. int ret=0,tlen;
  811. char *buf=NULL,*hptr=NULL;
  812. char msg[64]="ENGINE_rsa_priv_dec";
  813. if (!p_surewarehk_Rsa_Priv_Dec)
  814. {
  815. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ENGINE_R_NOT_INITIALISED);
  816. }
  817. /* extract ref to private key */
  818. else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx)))
  819. {
  820. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_MISSING_KEY_COMPONENTS);
  821. goto err;
  822. }
  823. /* analyse what padding we can do into the hardware */
  824. if (padding==RSA_PKCS1_PADDING)
  825. {
  826. /* do it one shot */
  827. ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD);
  828. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ret);
  829. if (ret!=1)
  830. goto err;
  831. ret=tlen;
  832. }
  833. else /* do with no padding into hardware */
  834. {
  835. ret=p_surewarehk_Rsa_Priv_Dec(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_NO_PAD);
  836. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ret);
  837. if (ret!=1)
  838. goto err;
  839. /* intermediate buffer for padding */
  840. if ((buf=OPENSSL_malloc(tlen)) == NULL)
  841. {
  842. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,ERR_R_MALLOC_FAILURE);
  843. goto err;
  844. }
  845. memcpy(buf,to,tlen);/* transfert to into buf */
  846. switch (padding) /* check padding in software */
  847. {
  848. #ifndef OPENSSL_NO_SHA
  849. case RSA_PKCS1_OAEP_PADDING:
  850. ret=RSA_padding_check_PKCS1_OAEP(to,tlen,(unsigned char *)buf,tlen,tlen,NULL,0);
  851. break;
  852. #endif
  853.   case RSA_SSLV23_PADDING:
  854. ret=RSA_padding_check_SSLv23(to,tlen,(unsigned char *)buf,flen,tlen);
  855. break;
  856. case RSA_NO_PADDING:
  857. ret=RSA_padding_check_none(to,tlen,(unsigned char *)buf,flen,tlen);
  858. break;
  859. default:
  860. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_UNKNOWN_PADDING_TYPE);
  861. goto err;
  862. }
  863. if (ret < 0)
  864. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_PRIV_DEC,SUREWARE_R_PADDING_CHECK_FAILED);
  865. }
  866. err:
  867. if (buf)
  868. {
  869. OPENSSL_cleanse(buf,tlen);
  870. OPENSSL_free(buf);
  871. }
  872. return ret;
  873. }
  874. /*
  875. * Does what OpenSSL rsa_priv_enc does.
  876. */
  877. static int surewarehk_rsa_sign(int flen,const unsigned char *from,unsigned char *to,
  878.     RSA *rsa,int padding)
  879. {
  880. int ret=0,tlen;
  881. char *hptr=NULL;
  882. char msg[64]="ENGINE_rsa_sign";
  883. if (!p_surewarehk_Rsa_Sign)
  884. {
  885. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,ENGINE_R_NOT_INITIALISED);
  886. }
  887. /* extract ref to private key */
  888. else if (!(hptr=RSA_get_ex_data(rsa, rsaHndidx)))
  889. {
  890. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,SUREWARE_R_MISSING_KEY_COMPONENTS);
  891. }
  892. else
  893. {
  894. switch (padding)
  895. {
  896. case RSA_PKCS1_PADDING: /* do it in one shot */
  897. ret=p_surewarehk_Rsa_Sign(msg,flen,(unsigned char *)from,&tlen,to,hptr,SUREWARE_PKCS1_PAD);
  898. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_RSA_SIGN,ret);
  899. break;
  900. case RSA_NO_PADDING:
  901. default:
  902. SUREWAREerr(SUREWARE_F_SUREWAREHK_RSA_SIGN,SUREWARE_R_UNKNOWN_PADDING_TYPE);
  903. }
  904. }
  905. return ret==1 ? tlen : ret;
  906. }
  907. #endif
  908. #ifndef OPENSSL_NO_DSA
  909. /* DSA sign and verify */
  910. static DSA_SIG * surewarehk_dsa_do_sign(const unsigned char *from, int flen, DSA *dsa)
  911. {
  912. int ret=0;
  913. char *hptr=NULL;
  914. DSA_SIG *psign=NULL;
  915. char msg[64]="ENGINE_dsa_do_sign";
  916. if (!p_surewarehk_Dsa_Sign)
  917. {
  918. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ENGINE_R_NOT_INITIALISED);
  919. goto err;
  920. }
  921. /* extract ref to private key */
  922. else if (!(hptr=DSA_get_ex_data(dsa, dsaHndidx)))
  923. {
  924. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,SUREWARE_R_MISSING_KEY_COMPONENTS);
  925. goto err;
  926. }
  927. else
  928. {
  929. if((psign = DSA_SIG_new()) == NULL)
  930. {
  931. SUREWAREerr(SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ERR_R_MALLOC_FAILURE);
  932. goto err;
  933. }
  934. psign->r=BN_new();
  935. psign->s=BN_new();
  936. bn_expand2(psign->r, 20/sizeof(BN_ULONG));
  937. bn_expand2(psign->s, 20/sizeof(BN_ULONG));
  938. if (!psign->r || psign->r->dmax!=20/sizeof(BN_ULONG) ||
  939. !psign->s || psign->s->dmax!=20/sizeof(BN_ULONG))
  940. goto err;
  941. ret=p_surewarehk_Dsa_Sign(msg,flen,from,
  942.   (unsigned long *)psign->r->d,
  943.   (unsigned long *)psign->s->d,
  944.   hptr);
  945. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_DSA_DO_SIGN,ret);
  946. }
  947. psign->r->top=20/sizeof(BN_ULONG);
  948. bn_fix_top(psign->r);
  949. psign->s->top=20/sizeof(BN_ULONG);
  950. bn_fix_top(psign->s);
  951. err:
  952. if (psign)
  953. {
  954. DSA_SIG_free(psign);
  955. psign=NULL;
  956. }
  957. return psign;
  958. }
  959. #endif
  960. static int surewarehk_modexp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  961.      const BIGNUM *m, BN_CTX *ctx)
  962. {
  963. int ret=0;
  964. char msg[64]="ENGINE_modexp";
  965. if (!p_surewarehk_Mod_Exp)
  966. {
  967. SUREWAREerr(SUREWARE_F_SUREWAREHK_MODEXP,ENGINE_R_NOT_INITIALISED);
  968. }
  969. else
  970. {
  971. bn_expand2(r,m->top);
  972. if (r && r->dmax==m->top)
  973. {
  974. /* do it*/
  975. ret=p_surewarehk_Mod_Exp(msg,
  976.  m->top*sizeof(BN_ULONG),
  977.  (unsigned long *)m->d,
  978.  p->top*sizeof(BN_ULONG),
  979.  (unsigned long *)p->d,
  980.  a->top*sizeof(BN_ULONG),
  981.  (unsigned long *)a->d,
  982.  (unsigned long *)r->d);
  983. surewarehk_error_handling(msg,SUREWARE_F_SUREWAREHK_MODEXP,ret);
  984. if (ret==1)
  985. {
  986. /* normalise result */
  987. r->top=m->top;
  988. bn_fix_top(r);
  989. }
  990. }
  991. }
  992. return ret;
  993. }
  994. #endif /* !OPENSSL_NO_HW_SureWare */
  995. #endif /* !OPENSSL_NO_HW */