e_nuron.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. /* crypto/engine/hw_nuron.c */
  2. /*
  3. * Written by Ben Laurie for the OpenSSL Project, leaning heavily on Geoff
  4. * Thorpe's Atalla implementation.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2000-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_NURON
  77. # define NURON_LIB_NAME "nuron engine"
  78. # include "e_nuron_err.c"
  79. static const char *NURON_LIBNAME = NULL;
  80. static const char *get_NURON_LIBNAME(void)
  81. {
  82. if (NURON_LIBNAME)
  83. return NURON_LIBNAME;
  84. return "nuronssl";
  85. }
  86. static void free_NURON_LIBNAME(void)
  87. {
  88. if (NURON_LIBNAME)
  89. OPENSSL_free((void *)NURON_LIBNAME);
  90. NURON_LIBNAME = NULL;
  91. }
  92. static long set_NURON_LIBNAME(const char *name)
  93. {
  94. free_NURON_LIBNAME();
  95. return (((NURON_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
  96. }
  97. static const char *NURON_F1 = "nuron_mod_exp";
  98. /* The definitions for control commands specific to this engine */
  99. # define NURON_CMD_SO_PATH ENGINE_CMD_BASE
  100. static const ENGINE_CMD_DEFN nuron_cmd_defns[] = {
  101. {NURON_CMD_SO_PATH,
  102. "SO_PATH",
  103. "Specifies the path to the 'nuronssl' shared library",
  104. ENGINE_CMD_FLAG_STRING},
  105. {0, NULL, NULL, 0}
  106. };
  107. typedef int tfnModExp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  108. const BIGNUM *m);
  109. static tfnModExp *pfnModExp = NULL;
  110. static DSO *pvDSOHandle = NULL;
  111. static int nuron_destroy(ENGINE *e)
  112. {
  113. free_NURON_LIBNAME();
  114. ERR_unload_NURON_strings();
  115. return 1;
  116. }
  117. static int nuron_init(ENGINE *e)
  118. {
  119. if (pvDSOHandle != NULL) {
  120. NURONerr(NURON_F_NURON_INIT, NURON_R_ALREADY_LOADED);
  121. return 0;
  122. }
  123. pvDSOHandle = DSO_load(NULL, get_NURON_LIBNAME(), NULL,
  124. DSO_FLAG_NAME_TRANSLATION_EXT_ONLY);
  125. if (!pvDSOHandle) {
  126. NURONerr(NURON_F_NURON_INIT, NURON_R_DSO_NOT_FOUND);
  127. return 0;
  128. }
  129. pfnModExp = (tfnModExp *) DSO_bind_func(pvDSOHandle, NURON_F1);
  130. if (!pfnModExp) {
  131. NURONerr(NURON_F_NURON_INIT, NURON_R_DSO_FUNCTION_NOT_FOUND);
  132. return 0;
  133. }
  134. return 1;
  135. }
  136. static int nuron_finish(ENGINE *e)
  137. {
  138. free_NURON_LIBNAME();
  139. if (pvDSOHandle == NULL) {
  140. NURONerr(NURON_F_NURON_FINISH, NURON_R_NOT_LOADED);
  141. return 0;
  142. }
  143. if (!DSO_free(pvDSOHandle)) {
  144. NURONerr(NURON_F_NURON_FINISH, NURON_R_DSO_FAILURE);
  145. return 0;
  146. }
  147. pvDSOHandle = NULL;
  148. pfnModExp = NULL;
  149. return 1;
  150. }
  151. static int nuron_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  152. {
  153. int initialised = ((pvDSOHandle == NULL) ? 0 : 1);
  154. switch (cmd) {
  155. case NURON_CMD_SO_PATH:
  156. if (p == NULL) {
  157. NURONerr(NURON_F_NURON_CTRL, ERR_R_PASSED_NULL_PARAMETER);
  158. return 0;
  159. }
  160. if (initialised) {
  161. NURONerr(NURON_F_NURON_CTRL, NURON_R_ALREADY_LOADED);
  162. return 0;
  163. }
  164. return set_NURON_LIBNAME((const char *)p);
  165. default:
  166. break;
  167. }
  168. NURONerr(NURON_F_NURON_CTRL, NURON_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  169. return 0;
  170. }
  171. static int nuron_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  172. const BIGNUM *m, BN_CTX *ctx)
  173. {
  174. if (!pvDSOHandle) {
  175. NURONerr(NURON_F_NURON_MOD_EXP, NURON_R_NOT_LOADED);
  176. return 0;
  177. }
  178. return pfnModExp(r, a, p, m);
  179. }
  180. # ifndef OPENSSL_NO_RSA
  181. static int nuron_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
  182. BN_CTX *ctx)
  183. {
  184. return nuron_mod_exp(r0, I, rsa->d, rsa->n, ctx);
  185. }
  186. # endif
  187. # ifndef OPENSSL_NO_DSA
  188. /*
  189. * This code was liberated and adapted from the commented-out code in
  190. * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration (it
  191. * doesn't have a CRT form for RSA), this function means that an Atalla
  192. * system running with a DSA server certificate can handshake around 5 or 6
  193. * times faster/more than an equivalent system running with RSA. Just check
  194. * out the "signs" statistics from the RSA and DSA parts of "openssl speed
  195. * -engine atalla dsa1024 rsa1024".
  196. */
  197. static int nuron_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
  198. BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
  199. BN_CTX *ctx, BN_MONT_CTX *in_mont)
  200. {
  201. BIGNUM t;
  202. int to_return = 0;
  203. BN_init(&t);
  204. /* let rr = a1 ^ p1 mod m */
  205. if (!nuron_mod_exp(rr, a1, p1, m, ctx))
  206. goto end;
  207. /* let t = a2 ^ p2 mod m */
  208. if (!nuron_mod_exp(&t, a2, p2, m, ctx))
  209. goto end;
  210. /* let rr = rr * t mod m */
  211. if (!BN_mod_mul(rr, rr, &t, m, ctx))
  212. goto end;
  213. to_return = 1;
  214. end:
  215. BN_free(&t);
  216. return to_return;
  217. }
  218. static int nuron_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
  219. const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
  220. BN_MONT_CTX *m_ctx)
  221. {
  222. return nuron_mod_exp(r, a, p, m, ctx);
  223. }
  224. # endif
  225. /* This function is aliased to mod_exp (with the mont stuff dropped). */
  226. # ifndef OPENSSL_NO_RSA
  227. static int nuron_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
  228. const BIGNUM *m, BN_CTX *ctx,
  229. BN_MONT_CTX *m_ctx)
  230. {
  231. return nuron_mod_exp(r, a, p, m, ctx);
  232. }
  233. # endif
  234. # ifndef OPENSSL_NO_DH
  235. /* This function is aliased to mod_exp (with the dh and mont dropped). */
  236. static int nuron_mod_exp_dh(const DH *dh, BIGNUM *r,
  237. const BIGNUM *a, const BIGNUM *p,
  238. const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
  239. {
  240. return nuron_mod_exp(r, a, p, m, ctx);
  241. }
  242. # endif
  243. # ifndef OPENSSL_NO_RSA
  244. static RSA_METHOD nuron_rsa = {
  245. "Nuron RSA method",
  246. NULL,
  247. NULL,
  248. NULL,
  249. NULL,
  250. nuron_rsa_mod_exp,
  251. nuron_mod_exp_mont,
  252. NULL,
  253. NULL,
  254. 0,
  255. NULL,
  256. NULL,
  257. NULL,
  258. NULL
  259. };
  260. # endif
  261. # ifndef OPENSSL_NO_DSA
  262. static DSA_METHOD nuron_dsa = {
  263. "Nuron DSA method",
  264. NULL, /* dsa_do_sign */
  265. NULL, /* dsa_sign_setup */
  266. NULL, /* dsa_do_verify */
  267. nuron_dsa_mod_exp, /* dsa_mod_exp */
  268. nuron_mod_exp_dsa, /* bn_mod_exp */
  269. NULL, /* init */
  270. NULL, /* finish */
  271. 0, /* flags */
  272. NULL, /* app_data */
  273. NULL, /* dsa_paramgen */
  274. NULL /* dsa_keygen */
  275. };
  276. # endif
  277. # ifndef OPENSSL_NO_DH
  278. static DH_METHOD nuron_dh = {
  279. "Nuron DH method",
  280. NULL,
  281. NULL,
  282. nuron_mod_exp_dh,
  283. NULL,
  284. NULL,
  285. 0,
  286. NULL,
  287. NULL
  288. };
  289. # endif
  290. /* Constants used when creating the ENGINE */
  291. static const char *engine_nuron_id = "nuron";
  292. static const char *engine_nuron_name = "Nuron hardware engine support";
  293. /*
  294. * This internal function is used by ENGINE_nuron() and possibly by the
  295. * "dynamic" ENGINE support too
  296. */
  297. static int bind_helper(ENGINE *e)
  298. {
  299. # ifndef OPENSSL_NO_RSA
  300. const RSA_METHOD *meth1;
  301. # endif
  302. # ifndef OPENSSL_NO_DSA
  303. const DSA_METHOD *meth2;
  304. # endif
  305. # ifndef OPENSSL_NO_DH
  306. const DH_METHOD *meth3;
  307. # endif
  308. if (!ENGINE_set_id(e, engine_nuron_id) ||
  309. !ENGINE_set_name(e, engine_nuron_name) ||
  310. # ifndef OPENSSL_NO_RSA
  311. !ENGINE_set_RSA(e, &nuron_rsa) ||
  312. # endif
  313. # ifndef OPENSSL_NO_DSA
  314. !ENGINE_set_DSA(e, &nuron_dsa) ||
  315. # endif
  316. # ifndef OPENSSL_NO_DH
  317. !ENGINE_set_DH(e, &nuron_dh) ||
  318. # endif
  319. !ENGINE_set_destroy_function(e, nuron_destroy) ||
  320. !ENGINE_set_init_function(e, nuron_init) ||
  321. !ENGINE_set_finish_function(e, nuron_finish) ||
  322. !ENGINE_set_ctrl_function(e, nuron_ctrl) ||
  323. !ENGINE_set_cmd_defns(e, nuron_cmd_defns))
  324. return 0;
  325. # ifndef OPENSSL_NO_RSA
  326. /*
  327. * We know that the "PKCS1_SSLeay()" functions hook properly to the
  328. * nuron-specific mod_exp and mod_exp_crt so we use those functions. NB:
  329. * We don't use ENGINE_openssl() or anything "more generic" because
  330. * something like the RSAref code may not hook properly, and if you own
  331. * one of these cards then you have the right to do RSA operations on it
  332. * anyway!
  333. */
  334. meth1 = RSA_PKCS1_SSLeay();
  335. nuron_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  336. nuron_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  337. nuron_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  338. nuron_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  339. # endif
  340. # ifndef OPENSSL_NO_DSA
  341. /*
  342. * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
  343. */
  344. meth2 = DSA_OpenSSL();
  345. nuron_dsa.dsa_do_sign = meth2->dsa_do_sign;
  346. nuron_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
  347. nuron_dsa.dsa_do_verify = meth2->dsa_do_verify;
  348. # endif
  349. # ifndef OPENSSL_NO_DH
  350. /* Much the same for Diffie-Hellman */
  351. meth3 = DH_OpenSSL();
  352. nuron_dh.generate_key = meth3->generate_key;
  353. nuron_dh.compute_key = meth3->compute_key;
  354. # endif
  355. /* Ensure the nuron error handling is set up */
  356. ERR_load_NURON_strings();
  357. return 1;
  358. }
  359. # ifdef OPENSSL_NO_DYNAMIC_ENGINE
  360. static ENGINE *engine_nuron(void)
  361. {
  362. ENGINE *ret = ENGINE_new();
  363. if (!ret)
  364. return NULL;
  365. if (!bind_helper(ret)) {
  366. ENGINE_free(ret);
  367. return NULL;
  368. }
  369. return ret;
  370. }
  371. void ENGINE_load_nuron(void)
  372. {
  373. /* Copied from eng_[openssl|dyn].c */
  374. ENGINE *toadd = engine_nuron();
  375. if (!toadd)
  376. return;
  377. ENGINE_add(toadd);
  378. ENGINE_free(toadd);
  379. ERR_clear_error();
  380. }
  381. # endif
  382. /*
  383. * This stuff is needed if this ENGINE is being compiled into a
  384. * self-contained shared-library.
  385. */
  386. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  387. static int bind_fn(ENGINE *e, const char *id)
  388. {
  389. if (id && (strcmp(id, engine_nuron_id) != 0))
  390. return 0;
  391. if (!bind_helper(e))
  392. return 0;
  393. return 1;
  394. }
  395. IMPLEMENT_DYNAMIC_CHECK_FN()
  396. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  397. # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
  398. # endif /* !OPENSSL_NO_HW_NURON */
  399. #endif /* !OPENSSL_NO_HW */