e_atalla.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. /* crypto/engine/hw_atalla.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2000.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. All advertising materials mentioning features or use of this
  22. * software must display the following acknowledgment:
  23. * "This product includes software developed by the OpenSSL Project
  24. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  25. *
  26. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  27. * endorse or promote products derived from this software without
  28. * prior written permission. For written permission, please contact
  29. * licensing@OpenSSL.org.
  30. *
  31. * 5. Products derived from this software may not be called "OpenSSL"
  32. * nor may "OpenSSL" appear in their names without prior written
  33. * permission of the OpenSSL Project.
  34. *
  35. * 6. Redistributions of any form whatsoever must retain the following
  36. * acknowledgment:
  37. * "This product includes software developed by the OpenSSL Project
  38. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  41. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  43. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  44. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  45. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  46. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  47. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  49. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  50. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  51. * OF THE POSSIBILITY OF SUCH DAMAGE.
  52. * ====================================================================
  53. *
  54. * This product includes cryptographic software written by Eric Young
  55. * (eay@cryptsoft.com). This product includes software written by Tim
  56. * Hudson (tjh@cryptsoft.com).
  57. *
  58. */
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/crypto.h>
  62. #include <openssl/buffer.h>
  63. #include <openssl/dso.h>
  64. #include <openssl/engine.h>
  65. #ifndef OPENSSL_NO_RSA
  66. # include <openssl/rsa.h>
  67. #endif
  68. #ifndef OPENSSL_NO_DSA
  69. # include <openssl/dsa.h>
  70. #endif
  71. #ifndef OPENSSL_NO_DH
  72. # include <openssl/dh.h>
  73. #endif
  74. #include <openssl/bn.h>
  75. #ifndef OPENSSL_NO_HW
  76. # ifndef OPENSSL_NO_HW_ATALLA
  77. # ifdef FLAT_INC
  78. # include "atalla.h"
  79. # else
  80. # include "vendor_defns/atalla.h"
  81. # endif
  82. # define ATALLA_LIB_NAME "atalla engine"
  83. # include "e_atalla_err.c"
  84. static int atalla_destroy(ENGINE *e);
  85. static int atalla_init(ENGINE *e);
  86. static int atalla_finish(ENGINE *e);
  87. static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
  88. /* BIGNUM stuff */
  89. static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  90. const BIGNUM *m, BN_CTX *ctx);
  91. # ifndef OPENSSL_NO_RSA
  92. /* RSA stuff */
  93. static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  94. BN_CTX *ctx);
  95. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  96. static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  97. const BIGNUM *m, BN_CTX *ctx,
  98. BN_MONT_CTX *m_ctx);
  99. # endif
  100. # ifndef OPENSSL_NO_DSA
  101. /* DSA stuff */
  102. static int atalla_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 atalla_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. # ifndef OPENSSL_NO_DH
  110. /* DH stuff */
  111. /* This function is alised to mod_exp (with the DH and mont dropped). */
  112. static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
  113. const BIGNUM *a, const BIGNUM *p,
  114. const BIGNUM *m, BN_CTX *ctx,
  115. BN_MONT_CTX *m_ctx);
  116. # endif
  117. /* The definitions for control commands specific to this engine */
  118. # define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE
  119. static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
  120. {ATALLA_CMD_SO_PATH,
  121. "SO_PATH",
  122. "Specifies the path to the 'atasi' shared library",
  123. ENGINE_CMD_FLAG_STRING},
  124. {0, NULL, NULL, 0}
  125. };
  126. # ifndef OPENSSL_NO_RSA
  127. /* Our internal RSA_METHOD that we provide pointers to */
  128. static RSA_METHOD atalla_rsa = {
  129. "Atalla RSA method",
  130. NULL,
  131. NULL,
  132. NULL,
  133. NULL,
  134. atalla_rsa_mod_exp,
  135. atalla_mod_exp_mont,
  136. NULL,
  137. NULL,
  138. 0,
  139. NULL,
  140. NULL,
  141. NULL,
  142. NULL
  143. };
  144. # endif
  145. # ifndef OPENSSL_NO_DSA
  146. /* Our internal DSA_METHOD that we provide pointers to */
  147. static DSA_METHOD atalla_dsa = {
  148. "Atalla DSA method",
  149. NULL, /* dsa_do_sign */
  150. NULL, /* dsa_sign_setup */
  151. NULL, /* dsa_do_verify */
  152. atalla_dsa_mod_exp, /* dsa_mod_exp */
  153. atalla_mod_exp_dsa, /* bn_mod_exp */
  154. NULL, /* init */
  155. NULL, /* finish */
  156. 0, /* flags */
  157. NULL, /* app_data */
  158. NULL, /* dsa_paramgen */
  159. NULL /* dsa_keygen */
  160. };
  161. # endif
  162. # ifndef OPENSSL_NO_DH
  163. /* Our internal DH_METHOD that we provide pointers to */
  164. static DH_METHOD atalla_dh = {
  165. "Atalla DH method",
  166. NULL,
  167. NULL,
  168. atalla_mod_exp_dh,
  169. NULL,
  170. NULL,
  171. 0,
  172. NULL,
  173. NULL
  174. };
  175. # endif
  176. /* Constants used when creating the ENGINE */
  177. static const char *engine_atalla_id = "atalla";
  178. static const char *engine_atalla_name = "Atalla hardware engine support";
  179. /*
  180. * This internal function is used by ENGINE_atalla() and possibly by the
  181. * "dynamic" ENGINE support too
  182. */
  183. static int bind_helper(ENGINE *e)
  184. {
  185. # ifndef OPENSSL_NO_RSA
  186. const RSA_METHOD *meth1;
  187. # endif
  188. # ifndef OPENSSL_NO_DSA
  189. const DSA_METHOD *meth2;
  190. # endif
  191. # ifndef OPENSSL_NO_DH
  192. const DH_METHOD *meth3;
  193. # endif
  194. if (!ENGINE_set_id(e, engine_atalla_id) ||
  195. !ENGINE_set_name(e, engine_atalla_name) ||
  196. # ifndef OPENSSL_NO_RSA
  197. !ENGINE_set_RSA(e, &atalla_rsa) ||
  198. # endif
  199. # ifndef OPENSSL_NO_DSA
  200. !ENGINE_set_DSA(e, &atalla_dsa) ||
  201. # endif
  202. # ifndef OPENSSL_NO_DH
  203. !ENGINE_set_DH(e, &atalla_dh) ||
  204. # endif
  205. !ENGINE_set_destroy_function(e, atalla_destroy) ||
  206. !ENGINE_set_init_function(e, atalla_init) ||
  207. !ENGINE_set_finish_function(e, atalla_finish) ||
  208. !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
  209. !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
  210. return 0;
  211. # ifndef OPENSSL_NO_RSA
  212. /*
  213. * We know that the "PKCS1_SSLeay()" functions hook properly to the
  214. * atalla-specific mod_exp and mod_exp_crt so we use those functions. NB:
  215. * We don't use ENGINE_openssl() or anything "more generic" because
  216. * something like the RSAref code may not hook properly, and if you own
  217. * one of these cards then you have the right to do RSA operations on it
  218. * anyway!
  219. */
  220. meth1 = RSA_PKCS1_SSLeay();
  221. atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  222. atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  223. atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  224. atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  225. # endif
  226. # ifndef OPENSSL_NO_DSA
  227. /*
  228. * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
  229. */
  230. meth2 = DSA_OpenSSL();
  231. atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
  232. atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
  233. atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
  234. # endif
  235. # ifndef OPENSSL_NO_DH
  236. /* Much the same for Diffie-Hellman */
  237. meth3 = DH_OpenSSL();
  238. atalla_dh.generate_key = meth3->generate_key;
  239. atalla_dh.compute_key = meth3->compute_key;
  240. # endif
  241. /* Ensure the atalla error handling is set up */
  242. ERR_load_ATALLA_strings();
  243. return 1;
  244. }
  245. # ifdef OPENSSL_NO_DYNAMIC_ENGINE
  246. static ENGINE *engine_atalla(void)
  247. {
  248. ENGINE *ret = ENGINE_new();
  249. if (!ret)
  250. return NULL;
  251. if (!bind_helper(ret)) {
  252. ENGINE_free(ret);
  253. return NULL;
  254. }
  255. return ret;
  256. }
  257. void ENGINE_load_atalla(void)
  258. {
  259. /* Copied from eng_[openssl|dyn].c */
  260. ENGINE *toadd = engine_atalla();
  261. if (!toadd)
  262. return;
  263. ENGINE_add(toadd);
  264. ENGINE_free(toadd);
  265. ERR_clear_error();
  266. }
  267. # endif
  268. /*
  269. * This is a process-global DSO handle used for loading and unloading the
  270. * Atalla library. NB: This is only set (or unset) during an init() or
  271. * finish() call (reference counts permitting) and they're operating with
  272. * global locks, so this should be thread-safe implicitly.
  273. */
  274. static DSO *atalla_dso = NULL;
  275. /*
  276. * These are the function pointers that are (un)set when the library has
  277. * successfully (un)loaded.
  278. */
  279. static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
  280. static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
  281. static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics =
  282. NULL;
  283. /*
  284. * These are the static string constants for the DSO file name and the
  285. * function symbol names to bind to. Regrettably, the DSO name on *nix
  286. * appears to be "atasi.so" rather than something more consistent like
  287. * "libatasi.so". At the time of writing, I'm not sure what the file name on
  288. * win32 is but clearly native name translation is not possible (eg
  289. * libatasi.so on *nix, and atasi.dll on win32). For the purposes of testing,
  290. * I have created a symbollic link called "libatasi.so" so that we can use
  291. * native name-translation - a better solution will be needed.
  292. */
  293. static const char *ATALLA_LIBNAME = NULL;
  294. static const char *get_ATALLA_LIBNAME(void)
  295. {
  296. if (ATALLA_LIBNAME)
  297. return ATALLA_LIBNAME;
  298. return "atasi";
  299. }
  300. static void free_ATALLA_LIBNAME(void)
  301. {
  302. if (ATALLA_LIBNAME)
  303. OPENSSL_free((void *)ATALLA_LIBNAME);
  304. ATALLA_LIBNAME = NULL;
  305. }
  306. static long set_ATALLA_LIBNAME(const char *name)
  307. {
  308. free_ATALLA_LIBNAME();
  309. return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
  310. }
  311. static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
  312. static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
  313. static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
  314. /* Destructor (complements the "ENGINE_atalla()" constructor) */
  315. static int atalla_destroy(ENGINE *e)
  316. {
  317. free_ATALLA_LIBNAME();
  318. /*
  319. * Unload the atalla error strings so any error state including our
  320. * functs or reasons won't lead to a segfault (they simply get displayed
  321. * without corresponding string data because none will be found).
  322. */
  323. ERR_unload_ATALLA_strings();
  324. return 1;
  325. }
  326. /* (de)initialisation functions. */
  327. static int atalla_init(ENGINE *e)
  328. {
  329. tfnASI_GetHardwareConfig *p1;
  330. tfnASI_RSAPrivateKeyOpFn *p2;
  331. tfnASI_GetPerformanceStatistics *p3;
  332. /*
  333. * Not sure of the origin of this magic value, but Ben's code had it and
  334. * it seemed to have been working for a few people. :-)
  335. */
  336. unsigned int config_buf[1024];
  337. if (atalla_dso != NULL) {
  338. ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_ALREADY_LOADED);
  339. goto err;
  340. }
  341. /*
  342. * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed
  343. * unfortunately because the Atalla drivers don't have standard library
  344. * names that can be platform-translated well.
  345. */
  346. /*
  347. * TODO: Work out how to actually map to the names the Atalla drivers
  348. * really use - for now a symbollic link needs to be created on the host
  349. * system from libatasi.so to atasi.so on unix variants.
  350. */
  351. atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0);
  352. if (atalla_dso == NULL) {
  353. ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED);
  354. goto err;
  355. }
  356. if (!
  357. (p1 =
  358. (tfnASI_GetHardwareConfig *) DSO_bind_func(atalla_dso, ATALLA_F1))
  359. || !(p2 = (tfnASI_RSAPrivateKeyOpFn *) DSO_bind_func(atalla_dso, ATALLA_F2))
  360. || !(p3 =
  361. (tfnASI_GetPerformanceStatistics *) DSO_bind_func(atalla_dso,
  362. ATALLA_F3))) {
  363. ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED);
  364. goto err;
  365. }
  366. /* Copy the pointers */
  367. p_Atalla_GetHardwareConfig = p1;
  368. p_Atalla_RSAPrivateKeyOpFn = p2;
  369. p_Atalla_GetPerformanceStatistics = p3;
  370. /*
  371. * Perform a basic test to see if there's actually any unit running.
  372. */
  373. if (p1(0L, config_buf) != 0) {
  374. ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_UNIT_FAILURE);
  375. goto err;
  376. }
  377. /* Everything's fine. */
  378. return 1;
  379. err:
  380. if (atalla_dso)
  381. DSO_free(atalla_dso);
  382. atalla_dso = NULL;
  383. p_Atalla_GetHardwareConfig = NULL;
  384. p_Atalla_RSAPrivateKeyOpFn = NULL;
  385. p_Atalla_GetPerformanceStatistics = NULL;
  386. return 0;
  387. }
  388. static int atalla_finish(ENGINE *e)
  389. {
  390. free_ATALLA_LIBNAME();
  391. if (atalla_dso == NULL) {
  392. ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_NOT_LOADED);
  393. return 0;
  394. }
  395. if (!DSO_free(atalla_dso)) {
  396. ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_UNIT_FAILURE);
  397. return 0;
  398. }
  399. atalla_dso = NULL;
  400. p_Atalla_GetHardwareConfig = NULL;
  401. p_Atalla_RSAPrivateKeyOpFn = NULL;
  402. p_Atalla_GetPerformanceStatistics = NULL;
  403. return 1;
  404. }
  405. static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  406. {
  407. int initialised = ((atalla_dso == NULL) ? 0 : 1);
  408. switch (cmd) {
  409. case ATALLA_CMD_SO_PATH:
  410. if (p == NULL) {
  411. ATALLAerr(ATALLA_F_ATALLA_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  412. return 0;
  413. }
  414. if (initialised) {
  415. ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_ALREADY_LOADED);
  416. return 0;
  417. }
  418. return set_ATALLA_LIBNAME((const char *)p);
  419. default:
  420. break;
  421. }
  422. ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  423. return 0;
  424. }
  425. static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  426. const BIGNUM *m, BN_CTX *ctx)
  427. {
  428. /*
  429. * I need somewhere to store temporary serialised values for use with the
  430. * Atalla API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
  431. * access their arrays directly as byte arrays <grin>. This way I don't
  432. * have to clean anything up.
  433. */
  434. BIGNUM *modulus;
  435. BIGNUM *exponent;
  436. BIGNUM *argument;
  437. BIGNUM *result;
  438. RSAPrivateKey keydata;
  439. int to_return, numbytes;
  440. modulus = exponent = argument = result = NULL;
  441. to_return = 0; /* expect failure */
  442. if (!atalla_dso) {
  443. ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_NOT_LOADED);
  444. goto err;
  445. }
  446. /* Prepare the params */
  447. BN_CTX_start(ctx);
  448. modulus = BN_CTX_get(ctx);
  449. exponent = BN_CTX_get(ctx);
  450. argument = BN_CTX_get(ctx);
  451. result = BN_CTX_get(ctx);
  452. if (!result) {
  453. ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_CTX_FULL);
  454. goto err;
  455. }
  456. if (!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
  457. !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top)) {
  458. ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_EXPAND_FAIL);
  459. goto err;
  460. }
  461. /* Prepare the key-data */
  462. memset(&keydata, 0, sizeof(keydata));
  463. numbytes = BN_num_bytes(m);
  464. memset(exponent->d, 0, numbytes);
  465. memset(modulus->d, 0, numbytes);
  466. BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
  467. BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
  468. keydata.privateExponent.data = (unsigned char *)exponent->d;
  469. keydata.privateExponent.len = numbytes;
  470. keydata.modulus.data = (unsigned char *)modulus->d;
  471. keydata.modulus.len = numbytes;
  472. /* Prepare the argument */
  473. memset(argument->d, 0, numbytes);
  474. memset(result->d, 0, numbytes);
  475. BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
  476. /* Perform the operation */
  477. if (p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
  478. (unsigned char *)argument->d,
  479. keydata.modulus.len) != 0) {
  480. ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_REQUEST_FAILED);
  481. goto err;
  482. }
  483. /* Convert the response */
  484. BN_bin2bn((unsigned char *)result->d, numbytes, r);
  485. to_return = 1;
  486. err:
  487. BN_CTX_end(ctx);
  488. return to_return;
  489. }
  490. # ifndef OPENSSL_NO_RSA
  491. static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  492. BN_CTX *ctx)
  493. {
  494. int to_return = 0;
  495. if (!atalla_dso) {
  496. ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP, ATALLA_R_NOT_LOADED);
  497. goto err;
  498. }
  499. if (!rsa->d || !rsa->n) {
  500. ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,
  501. ATALLA_R_MISSING_KEY_COMPONENTS);
  502. goto err;
  503. }
  504. to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
  505. err:
  506. return to_return;
  507. }
  508. # endif
  509. # ifndef OPENSSL_NO_DSA
  510. /*
  511. * This code was liberated and adapted from the commented-out code in
  512. * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration (it
  513. * doesn't have a CRT form for RSA), this function means that an Atalla
  514. * system running with a DSA server certificate can handshake around 5 or 6
  515. * times faster/more than an equivalent system running with RSA. Just check
  516. * out the "signs" statistics from the RSA and DSA parts of "openssl speed
  517. * -engine atalla dsa1024 rsa1024".
  518. */
  519. static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  520. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  521. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  522. {
  523. BIGNUM t;
  524. int to_return = 0;
  525. BN_init(&t);
  526. /* let rr = a1 ^ p1 mod m */
  527. if (!atalla_mod_exp(rr, a1, p1, m, ctx))
  528. goto end;
  529. /* let t = a2 ^ p2 mod m */
  530. if (!atalla_mod_exp(&t, a2, p2, m, ctx))
  531. goto end;
  532. /* let rr = rr * t mod m */
  533. if (!BN_mod_mul(rr, rr, &t, m, ctx))
  534. goto end;
  535. to_return = 1;
  536. end:
  537. BN_free(&t);
  538. return to_return;
  539. }
  540. static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  541. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  542. BN_MONT_CTX *m_ctx)
  543. {
  544. return atalla_mod_exp(r, a, p, m, ctx);
  545. }
  546. # endif
  547. # ifndef OPENSSL_NO_RSA
  548. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  549. static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  550. const BIGNUM *m, BN_CTX *ctx,
  551. BN_MONT_CTX *m_ctx)
  552. {
  553. return atalla_mod_exp(r, a, p, m, ctx);
  554. }
  555. # endif
  556. # ifndef OPENSSL_NO_DH
  557. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  558. static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
  559. const BIGNUM *a, const BIGNUM *p,
  560. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  561. {
  562. return atalla_mod_exp(r, a, p, m, ctx);
  563. }
  564. # endif
  565. /*
  566. * This stuff is needed if this ENGINE is being compiled into a
  567. * self-contained shared-library.
  568. */
  569. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  570. static int bind_fn(ENGINE *e, const char *id)
  571. {
  572. if (id && (strcmp(id, engine_atalla_id) != 0))
  573. return 0;
  574. if (!bind_helper(e))
  575. return 0;
  576. return 1;
  577. }
  578. IMPLEMENT_DYNAMIC_CHECK_FN()
  579. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  580. # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  581. # endif /* !OPENSSL_NO_HW_ATALLA */
  582. #endif /* !OPENSSL_NO_HW */