e_gmp.c 14 KB

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