e_gmp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /* crypto/engine/e_gmp.c */
  2. /*
  3. * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
  4. * 2003.
  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. /*
  60. * This engine is not (currently) compiled in by default. Do enable it,
  61. * reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and headers
  62. * must reside in one of the paths searched by the compiler/linker, otherwise
  63. * paths must be specified - eg. try configuring with "enable-gmp
  64. * -I<includepath> -L<libpath> -lgmp". YMMV.
  65. */
  66. /*-
  67. * As for what this does - it's a largely unoptimised implementation of an
  68. * ENGINE that uses the GMP library to perform RSA private key operations. To
  69. * obtain more information about what "unoptimised" means, see my original mail
  70. * on the subject (though ignore the build instructions which have since
  71. * changed);
  72. *
  73. * http://www.mail-archive.com/openssl-dev@openssl.org/msg12227.html
  74. *
  75. * On my athlon system at least, it appears the builtin OpenSSL code is now
  76. * slightly faster, which is to say that the RSA-related MPI performance
  77. * between OpenSSL's BIGNUM and GMP's mpz implementations is probably pretty
  78. * balanced for this chip, and so the performance degradation in this ENGINE by
  79. * having to convert to/from GMP formats (and not being able to cache
  80. * montgomery forms) is probably the difference. However, if some unconfirmed
  81. * reports from users is anything to go by, the situation on some other
  82. * chipsets might be a good deal more favourable to the GMP version (eg. PPC).
  83. * Feedback welcome. */
  84. #include <stdio.h>
  85. #include <string.h>
  86. #include <openssl/crypto.h>
  87. #include <openssl/buffer.h>
  88. #include <openssl/engine.h>
  89. #ifndef OPENSSL_NO_RSA
  90. # include <openssl/rsa.h>
  91. #endif
  92. #include <openssl/bn.h>
  93. #ifndef OPENSSL_NO_HW
  94. # ifndef OPENSSL_NO_GMP
  95. # include <gmp.h>
  96. # define E_GMP_LIB_NAME "gmp engine"
  97. # include "e_gmp_err.c"
  98. static int e_gmp_destroy(ENGINE *e);
  99. static int e_gmp_init(ENGINE *e);
  100. static int e_gmp_finish(ENGINE *e);
  101. static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
  102. # ifndef OPENSSL_NO_RSA
  103. /* RSA stuff */
  104. static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
  105. BN_CTX *ctx);
  106. static int e_gmp_rsa_finish(RSA *r);
  107. # endif
  108. /* The definitions for control commands specific to this engine */
  109. /* #define E_GMP_CMD_SO_PATH ENGINE_CMD_BASE */
  110. static const ENGINE_CMD_DEFN e_gmp_cmd_defns[] = {
  111. # if 0
  112. {E_GMP_CMD_SO_PATH,
  113. "SO_PATH",
  114. "Specifies the path to the 'e_gmp' shared library",
  115. ENGINE_CMD_FLAG_STRING},
  116. # endif
  117. {0, NULL, NULL, 0}
  118. };
  119. # ifndef OPENSSL_NO_RSA
  120. /* Our internal RSA_METHOD that we provide pointers to */
  121. static RSA_METHOD e_gmp_rsa = {
  122. "GMP RSA method",
  123. NULL,
  124. NULL,
  125. NULL,
  126. NULL,
  127. e_gmp_rsa_mod_exp,
  128. NULL,
  129. NULL,
  130. e_gmp_rsa_finish,
  131. /*
  132. * These flags initialise montgomery crud that GMP ignores, however it
  133. * makes sure the public key ops (which are done in openssl) don't seem
  134. * *slower* than usual :-)
  135. */
  136. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  137. NULL,
  138. NULL,
  139. NULL
  140. };
  141. # endif
  142. /* Constants used when creating the ENGINE */
  143. static const char *engine_e_gmp_id = "gmp";
  144. static const char *engine_e_gmp_name = "GMP engine support";
  145. /*
  146. * This internal function is used by ENGINE_gmp() and possibly by the
  147. * "dynamic" ENGINE support too
  148. */
  149. static int bind_helper(ENGINE *e)
  150. {
  151. # ifndef OPENSSL_NO_RSA
  152. const RSA_METHOD *meth1;
  153. # endif
  154. if (!ENGINE_set_id(e, engine_e_gmp_id) ||
  155. !ENGINE_set_name(e, engine_e_gmp_name) ||
  156. # ifndef OPENSSL_NO_RSA
  157. !ENGINE_set_RSA(e, &e_gmp_rsa) ||
  158. # endif
  159. !ENGINE_set_destroy_function(e, e_gmp_destroy) ||
  160. !ENGINE_set_init_function(e, e_gmp_init) ||
  161. !ENGINE_set_finish_function(e, e_gmp_finish) ||
  162. !ENGINE_set_ctrl_function(e, e_gmp_ctrl) ||
  163. !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns))
  164. return 0;
  165. # ifndef OPENSSL_NO_RSA
  166. meth1 = RSA_PKCS1_SSLeay();
  167. e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  168. e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  169. e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  170. e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  171. e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp;
  172. # endif
  173. /* Ensure the e_gmp error handling is set up */
  174. ERR_load_GMP_strings();
  175. return 1;
  176. }
  177. static ENGINE *engine_gmp(void)
  178. {
  179. ENGINE *ret = ENGINE_new();
  180. if (!ret)
  181. return NULL;
  182. if (!bind_helper(ret)) {
  183. ENGINE_free(ret);
  184. return NULL;
  185. }
  186. return ret;
  187. }
  188. void ENGINE_load_gmp(void)
  189. {
  190. /* Copied from eng_[openssl|dyn].c */
  191. ENGINE *toadd = engine_gmp();
  192. if (!toadd)
  193. return;
  194. ENGINE_add(toadd);
  195. ENGINE_free(toadd);
  196. ERR_clear_error();
  197. }
  198. # ifndef OPENSSL_NO_RSA
  199. /* Used to attach our own key-data to an RSA structure */
  200. static int hndidx_rsa = -1;
  201. # endif
  202. static int e_gmp_destroy(ENGINE *e)
  203. {
  204. ERR_unload_GMP_strings();
  205. return 1;
  206. }
  207. /* (de)initialisation functions. */
  208. static int e_gmp_init(ENGINE *e)
  209. {
  210. # ifndef OPENSSL_NO_RSA
  211. if (hndidx_rsa == -1)
  212. hndidx_rsa = RSA_get_ex_new_index(0,
  213. "GMP-based RSA key handle",
  214. NULL, NULL, NULL);
  215. # endif
  216. if (hndidx_rsa == -1)
  217. return 0;
  218. return 1;
  219. }
  220. static int e_gmp_finish(ENGINE *e)
  221. {
  222. return 1;
  223. }
  224. static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
  225. {
  226. int to_return = 1;
  227. switch (cmd) {
  228. # if 0
  229. case E_GMP_CMD_SO_PATH:
  230. /* ... */
  231. # endif
  232. /* The command isn't understood by this engine */
  233. default:
  234. GMPerr(GMP_F_E_GMP_CTRL, GMP_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  235. to_return = 0;
  236. break;
  237. }
  238. return to_return;
  239. }
  240. /*
  241. * Most often limb sizes will be the same. If not, we use hex conversion
  242. * which is neat, but extremely inefficient.
  243. */
  244. static int bn2gmp(const BIGNUM *bn, mpz_t g)
  245. {
  246. bn_check_top(bn);
  247. if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
  248. (BN_BITS2 == GMP_NUMB_BITS)) {
  249. /* The common case */
  250. if (!_mpz_realloc(g, bn->top))
  251. return 0;
  252. memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
  253. g->_mp_size = bn->top;
  254. if (bn->neg)
  255. g->_mp_size = -g->_mp_size;
  256. return 1;
  257. } else {
  258. int toret;
  259. char *tmpchar = BN_bn2hex(bn);
  260. if (!tmpchar)
  261. return 0;
  262. toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
  263. OPENSSL_free(tmpchar);
  264. return toret;
  265. }
  266. }
  267. static int gmp2bn(mpz_t g, BIGNUM *bn)
  268. {
  269. if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
  270. (BN_BITS2 == GMP_NUMB_BITS)) {
  271. /* The common case */
  272. int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
  273. BN_zero(bn);
  274. if (bn_expand2(bn, s) == NULL)
  275. return 0;
  276. bn->top = s;
  277. memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
  278. bn_correct_top(bn);
  279. bn->neg = g->_mp_size >= 0 ? 0 : 1;
  280. return 1;
  281. } else {
  282. int toret;
  283. char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
  284. if (!tmpchar)
  285. return 0;
  286. mpz_get_str(tmpchar, 16, g);
  287. toret = BN_hex2bn(&bn, tmpchar);
  288. OPENSSL_free(tmpchar);
  289. return toret;
  290. }
  291. }
  292. # ifndef OPENSSL_NO_RSA
  293. typedef struct st_e_gmp_rsa_ctx {
  294. int public_only;
  295. mpz_t n;
  296. mpz_t d;
  297. mpz_t e;
  298. mpz_t p;
  299. mpz_t q;
  300. mpz_t dmp1;
  301. mpz_t dmq1;
  302. mpz_t iqmp;
  303. mpz_t r0, r1, I0, m1;
  304. } E_GMP_RSA_CTX;
  305. static E_GMP_RSA_CTX *e_gmp_get_rsa(RSA *rsa)
  306. {
  307. E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
  308. if (hptr)
  309. return hptr;
  310. hptr = OPENSSL_malloc(sizeof(E_GMP_RSA_CTX));
  311. if (!hptr)
  312. return NULL;
  313. /*
  314. * These inits could probably be replaced by more intelligent mpz_init2()
  315. * versions, to reduce malloc-thrashing.
  316. */
  317. mpz_init(hptr->n);
  318. mpz_init(hptr->d);
  319. mpz_init(hptr->e);
  320. mpz_init(hptr->p);
  321. mpz_init(hptr->q);
  322. mpz_init(hptr->dmp1);
  323. mpz_init(hptr->dmq1);
  324. mpz_init(hptr->iqmp);
  325. mpz_init(hptr->r0);
  326. mpz_init(hptr->r1);
  327. mpz_init(hptr->I0);
  328. mpz_init(hptr->m1);
  329. if (!bn2gmp(rsa->n, hptr->n) || !bn2gmp(rsa->e, hptr->e))
  330. goto err;
  331. if (!rsa->p || !rsa->q || !rsa->d || !rsa->dmp1 || !rsa->dmq1
  332. || !rsa->iqmp) {
  333. hptr->public_only = 1;
  334. return hptr;
  335. }
  336. if (!bn2gmp(rsa->d, hptr->d) || !bn2gmp(rsa->p, hptr->p) ||
  337. !bn2gmp(rsa->q, hptr->q) || !bn2gmp(rsa->dmp1, hptr->dmp1) ||
  338. !bn2gmp(rsa->dmq1, hptr->dmq1) || !bn2gmp(rsa->iqmp, hptr->iqmp))
  339. goto err;
  340. hptr->public_only = 0;
  341. RSA_set_ex_data(rsa, hndidx_rsa, hptr);
  342. return hptr;
  343. err:
  344. mpz_clear(hptr->n);
  345. mpz_clear(hptr->d);
  346. mpz_clear(hptr->e);
  347. mpz_clear(hptr->p);
  348. mpz_clear(hptr->q);
  349. mpz_clear(hptr->dmp1);
  350. mpz_clear(hptr->dmq1);
  351. mpz_clear(hptr->iqmp);
  352. mpz_clear(hptr->r0);
  353. mpz_clear(hptr->r1);
  354. mpz_clear(hptr->I0);
  355. mpz_clear(hptr->m1);
  356. OPENSSL_free(hptr);
  357. return NULL;
  358. }
  359. static int e_gmp_rsa_finish(RSA *rsa)
  360. {
  361. E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
  362. if (!hptr)
  363. return 0;
  364. mpz_clear(hptr->n);
  365. mpz_clear(hptr->d);
  366. mpz_clear(hptr->e);
  367. mpz_clear(hptr->p);
  368. mpz_clear(hptr->q);
  369. mpz_clear(hptr->dmp1);
  370. mpz_clear(hptr->dmq1);
  371. mpz_clear(hptr->iqmp);
  372. mpz_clear(hptr->r0);
  373. mpz_clear(hptr->r1);
  374. mpz_clear(hptr->I0);
  375. mpz_clear(hptr->m1);
  376. OPENSSL_free(hptr);
  377. RSA_set_ex_data(rsa, hndidx_rsa, NULL);
  378. return 1;
  379. }
  380. static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa,
  381. BN_CTX *ctx)
  382. {
  383. E_GMP_RSA_CTX *hptr;
  384. int to_return = 0;
  385. hptr = e_gmp_get_rsa(rsa);
  386. if (!hptr) {
  387. GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_KEY_CONTEXT_ERROR);
  388. return 0;
  389. }
  390. if (hptr->public_only) {
  391. GMPerr(GMP_F_E_GMP_RSA_MOD_EXP, GMP_R_MISSING_KEY_COMPONENTS);
  392. return 0;
  393. }
  394. /* ugh!!! */
  395. if (!bn2gmp(I, hptr->I0))
  396. return 0;
  397. /*
  398. * This is basically the CRT logic in crypto/rsa/rsa_eay.c reworded into
  399. * GMP-speak. It may be that GMP's API facilitates cleaner formulations
  400. * of this stuff, eg. better handling of negatives, or functions that
  401. * combine operations.
  402. */
  403. mpz_mod(hptr->r1, hptr->I0, hptr->q);
  404. mpz_powm(hptr->m1, hptr->r1, hptr->dmq1, hptr->q);
  405. mpz_mod(hptr->r1, hptr->I0, hptr->p);
  406. mpz_powm(hptr->r0, hptr->r1, hptr->dmp1, hptr->p);
  407. mpz_sub(hptr->r0, hptr->r0, hptr->m1);
  408. if (mpz_sgn(hptr->r0) < 0)
  409. mpz_add(hptr->r0, hptr->r0, hptr->p);
  410. mpz_mul(hptr->r1, hptr->r0, hptr->iqmp);
  411. mpz_mod(hptr->r0, hptr->r1, hptr->p);
  412. if (mpz_sgn(hptr->r0) < 0)
  413. mpz_add(hptr->r0, hptr->r0, hptr->p);
  414. mpz_mul(hptr->r1, hptr->r0, hptr->q);
  415. mpz_add(hptr->r0, hptr->r1, hptr->m1);
  416. /* ugh!!! */
  417. if (gmp2bn(hptr->r0, r))
  418. to_return = 1;
  419. return 1;
  420. }
  421. # endif
  422. # endif /* !OPENSSL_NO_GMP */
  423. /*
  424. * This stuff is needed if this ENGINE is being compiled into a
  425. * self-contained shared-library.
  426. */
  427. # ifndef OPENSSL_NO_DYNAMIC_ENGINE
  428. IMPLEMENT_DYNAMIC_CHECK_FN()
  429. # ifndef OPENSSL_NO_GMP
  430. static int bind_fn(ENGINE *e, const char *id)
  431. {
  432. if (id && (strcmp(id, engine_e_gmp_id) != 0))
  433. return 0;
  434. if (!bind_helper(e))
  435. return 0;
  436. return 1;
  437. }
  438. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  439. # else
  440. OPENSSL_EXPORT
  441. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns);
  442. OPENSSL_EXPORT
  443. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns)
  444. {
  445. return 0;
  446. }
  447. # endif
  448. # endif /* !OPENSSL_NO_DYNAMIC_ENGINE */
  449. #endif /* !OPENSSL_NO_HW */