e_gmp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* crypto/engine/e_gmp.c */
  2. /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
  3. * project 2003.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * licensing@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. /* This engine is not (currently) compiled in by default. Do enable it,
  59. * reconfigure OpenSSL with "enable-gmp -lgmp". The GMP libraries and
  60. * headers must reside in one of the paths searched by the compiler/linker,
  61. * otherwise paths must be specified - eg. try configuring with
  62. * "enable-gmp -I<includepath> -L<libpath> -lgmp". YMMV. */
  63. /* As for what this does - it's a largely unoptimised implementation of an
  64. * ENGINE that uses the GMP library to perform RSA private key operations. To
  65. * obtain more information about what "unoptimised" means, see my original mail
  66. * on the subject (though ignore the build instructions which have since
  67. * changed);
  68. *
  69. * http://www.mail-archive.com/openssl-dev@openssl.org/msg12227.html
  70. *
  71. * On my athlon system at least, it appears the builtin OpenSSL code is now
  72. * slightly faster, which is to say that the RSA-related MPI performance
  73. * between OpenSSL's BIGNUM and GMP's mpz implementations is probably pretty
  74. * balanced for this chip, and so the performance degradation in this ENGINE by
  75. * having to convert to/from GMP formats (and not being able to cache
  76. * montgomery forms) is probably the difference. However, if some unconfirmed
  77. * reports from users is anything to go by, the situation on some other
  78. * chipsets might be a good deal more favourable to the GMP version (eg. PPC).
  79. * Feedback welcome. */
  80. #include <stdio.h>
  81. #include <string.h>
  82. #include <openssl/crypto.h>
  83. #include <openssl/buffer.h>
  84. #include <openssl/engine.h>
  85. #ifndef OPENSSL_NO_RSA
  86. #include <openssl/rsa.h>
  87. #endif
  88. #include <openssl/bn.h>
  89. #ifndef OPENSSL_NO_HW
  90. #ifndef OPENSSL_NO_GMP
  91. #include <gmp.h>
  92. #define E_GMP_LIB_NAME "gmp engine"
  93. #include "e_gmp_err.c"
  94. static int e_gmp_destroy(ENGINE *e);
  95. static int e_gmp_init(ENGINE *e);
  96. static int e_gmp_finish(ENGINE *e);
  97. static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
  98. #ifndef OPENSSL_NO_RSA
  99. /* RSA stuff */
  100. static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  101. static int e_gmp_rsa_finish(RSA *r);
  102. #endif
  103. /* The definitions for control commands specific to this engine */
  104. /* #define E_GMP_CMD_SO_PATH ENGINE_CMD_BASE */
  105. static const ENGINE_CMD_DEFN e_gmp_cmd_defns[] = {
  106. #if 0
  107. {E_GMP_CMD_SO_PATH,
  108. "SO_PATH",
  109. "Specifies the path to the 'e_gmp' shared library",
  110. ENGINE_CMD_FLAG_STRING},
  111. #endif
  112. {0, NULL, NULL, 0}
  113. };
  114. #ifndef OPENSSL_NO_RSA
  115. /* Our internal RSA_METHOD that we provide pointers to */
  116. static RSA_METHOD e_gmp_rsa =
  117. {
  118. "GMP RSA method",
  119. NULL,
  120. NULL,
  121. NULL,
  122. NULL,
  123. e_gmp_rsa_mod_exp,
  124. NULL,
  125. NULL,
  126. e_gmp_rsa_finish,
  127. /* These flags initialise montgomery crud that GMP ignores, however it
  128. * makes sure the public key ops (which are done in openssl) don't seem
  129. * *slower* than usual :-) */
  130. RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE,
  131. NULL,
  132. NULL,
  133. NULL
  134. };
  135. #endif
  136. /* Constants used when creating the ENGINE */
  137. static const char *engine_e_gmp_id = "gmp";
  138. static const char *engine_e_gmp_name = "GMP engine support";
  139. /* This internal function is used by ENGINE_gmp() and possibly by the
  140. * "dynamic" ENGINE support too */
  141. static int bind_helper(ENGINE *e)
  142. {
  143. #ifndef OPENSSL_NO_RSA
  144. const RSA_METHOD *meth1;
  145. #endif
  146. if(!ENGINE_set_id(e, engine_e_gmp_id) ||
  147. !ENGINE_set_name(e, engine_e_gmp_name) ||
  148. #ifndef OPENSSL_NO_RSA
  149. !ENGINE_set_RSA(e, &e_gmp_rsa) ||
  150. #endif
  151. !ENGINE_set_destroy_function(e, e_gmp_destroy) ||
  152. !ENGINE_set_init_function(e, e_gmp_init) ||
  153. !ENGINE_set_finish_function(e, e_gmp_finish) ||
  154. !ENGINE_set_ctrl_function(e, e_gmp_ctrl) ||
  155. !ENGINE_set_cmd_defns(e, e_gmp_cmd_defns))
  156. return 0;
  157. #ifndef OPENSSL_NO_RSA
  158. meth1 = RSA_PKCS1_SSLeay();
  159. e_gmp_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
  160. e_gmp_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
  161. e_gmp_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
  162. e_gmp_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
  163. e_gmp_rsa.bn_mod_exp = meth1->bn_mod_exp;
  164. #endif
  165. /* Ensure the e_gmp error handling is set up */
  166. ERR_load_GMP_strings();
  167. return 1;
  168. }
  169. static ENGINE *engine_gmp(void)
  170. {
  171. ENGINE *ret = ENGINE_new();
  172. if(!ret)
  173. return NULL;
  174. if(!bind_helper(ret))
  175. {
  176. ENGINE_free(ret);
  177. return NULL;
  178. }
  179. return ret;
  180. }
  181. void ENGINE_load_gmp(void)
  182. {
  183. /* Copied from eng_[openssl|dyn].c */
  184. ENGINE *toadd = engine_gmp();
  185. if(!toadd) return;
  186. ENGINE_add(toadd);
  187. ENGINE_free(toadd);
  188. ERR_clear_error();
  189. }
  190. #ifndef OPENSSL_NO_RSA
  191. /* Used to attach our own key-data to an RSA structure */
  192. static int hndidx_rsa = -1;
  193. #endif
  194. static int e_gmp_destroy(ENGINE *e)
  195. {
  196. ERR_unload_GMP_strings();
  197. return 1;
  198. }
  199. /* (de)initialisation functions. */
  200. static int e_gmp_init(ENGINE *e)
  201. {
  202. #ifndef OPENSSL_NO_RSA
  203. if (hndidx_rsa == -1)
  204. hndidx_rsa = RSA_get_ex_new_index(0,
  205. "GMP-based RSA key handle",
  206. NULL, NULL, NULL);
  207. #endif
  208. if (hndidx_rsa == -1)
  209. return 0;
  210. return 1;
  211. }
  212. static int e_gmp_finish(ENGINE *e)
  213. {
  214. return 1;
  215. }
  216. static int e_gmp_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
  217. {
  218. int to_return = 1;
  219. switch(cmd)
  220. {
  221. #if 0
  222. case E_GMP_CMD_SO_PATH:
  223. /* ... */
  224. #endif
  225. /* The command isn't understood by this engine */
  226. default:
  227. GMPerr(GMP_F_E_GMP_CTRL,
  228. GMP_R_CTRL_COMMAND_NOT_IMPLEMENTED);
  229. to_return = 0;
  230. break;
  231. }
  232. return to_return;
  233. }
  234. /* Most often limb sizes will be the same. If not, we use hex conversion
  235. * which is neat, but extremely inefficient. */
  236. static int bn2gmp(const BIGNUM *bn, mpz_t g)
  237. {
  238. bn_check_top(bn);
  239. if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
  240. (BN_BITS2 == GMP_NUMB_BITS))
  241. {
  242. /* The common case */
  243. if(!_mpz_realloc (g, bn->top))
  244. return 0;
  245. memcpy(&g->_mp_d[0], &bn->d[0], bn->top * sizeof(bn->d[0]));
  246. g->_mp_size = bn->top;
  247. if(bn->neg)
  248. g->_mp_size = -g->_mp_size;
  249. return 1;
  250. }
  251. else
  252. {
  253. int toret;
  254. char *tmpchar = BN_bn2hex(bn);
  255. if(!tmpchar) return 0;
  256. toret = (mpz_set_str(g, tmpchar, 16) == 0 ? 1 : 0);
  257. OPENSSL_free(tmpchar);
  258. return toret;
  259. }
  260. }
  261. static int gmp2bn(mpz_t g, BIGNUM *bn)
  262. {
  263. if(((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
  264. (BN_BITS2 == GMP_NUMB_BITS))
  265. {
  266. /* The common case */
  267. int s = (g->_mp_size >= 0) ? g->_mp_size : -g->_mp_size;
  268. BN_zero(bn);
  269. if(bn_expand2 (bn, s) == NULL)
  270. return 0;
  271. bn->top = s;
  272. memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
  273. bn_correct_top(bn);
  274. bn->neg = g->_mp_size >= 0 ? 0 : 1;
  275. return 1;
  276. }
  277. else
  278. {
  279. int toret;
  280. char *tmpchar = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
  281. if(!tmpchar) return 0;
  282. mpz_get_str(tmpchar, 16, g);
  283. toret = BN_hex2bn(&bn, tmpchar);
  284. OPENSSL_free(tmpchar);
  285. return toret;
  286. }
  287. }
  288. #ifndef OPENSSL_NO_RSA
  289. typedef struct st_e_gmp_rsa_ctx
  290. {
  291. int public_only;
  292. mpz_t n;
  293. mpz_t d;
  294. mpz_t e;
  295. mpz_t p;
  296. mpz_t q;
  297. mpz_t dmp1;
  298. mpz_t dmq1;
  299. mpz_t iqmp;
  300. mpz_t r0, r1, I0, m1;
  301. } E_GMP_RSA_CTX;
  302. static E_GMP_RSA_CTX *e_gmp_get_rsa(RSA *rsa)
  303. {
  304. E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
  305. if(hptr) return hptr;
  306. hptr = OPENSSL_malloc(sizeof(E_GMP_RSA_CTX));
  307. if(!hptr) return NULL;
  308. /* These inits could probably be replaced by more intelligent
  309. * mpz_init2() versions, to reduce malloc-thrashing. */
  310. mpz_init(hptr->n);
  311. mpz_init(hptr->d);
  312. mpz_init(hptr->e);
  313. mpz_init(hptr->p);
  314. mpz_init(hptr->q);
  315. mpz_init(hptr->dmp1);
  316. mpz_init(hptr->dmq1);
  317. mpz_init(hptr->iqmp);
  318. mpz_init(hptr->r0);
  319. mpz_init(hptr->r1);
  320. mpz_init(hptr->I0);
  321. mpz_init(hptr->m1);
  322. if(!bn2gmp(rsa->n, hptr->n) || !bn2gmp(rsa->e, hptr->e))
  323. goto err;
  324. if(!rsa->p || !rsa->q || !rsa->d || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
  325. {
  326. hptr->public_only = 1;
  327. return hptr;
  328. }
  329. if(!bn2gmp(rsa->d, hptr->d) || !bn2gmp(rsa->p, hptr->p) ||
  330. !bn2gmp(rsa->q, hptr->q) || !bn2gmp(rsa->dmp1, hptr->dmp1) ||
  331. !bn2gmp(rsa->dmq1, hptr->dmq1) || !bn2gmp(rsa->iqmp, hptr->iqmp))
  332. goto err;
  333. hptr->public_only = 0;
  334. RSA_set_ex_data(rsa, hndidx_rsa, hptr);
  335. return hptr;
  336. err:
  337. mpz_clear(hptr->n);
  338. mpz_clear(hptr->d);
  339. mpz_clear(hptr->e);
  340. mpz_clear(hptr->p);
  341. mpz_clear(hptr->q);
  342. mpz_clear(hptr->dmp1);
  343. mpz_clear(hptr->dmq1);
  344. mpz_clear(hptr->iqmp);
  345. mpz_clear(hptr->r0);
  346. mpz_clear(hptr->r1);
  347. mpz_clear(hptr->I0);
  348. mpz_clear(hptr->m1);
  349. OPENSSL_free(hptr);
  350. return NULL;
  351. }
  352. static int e_gmp_rsa_finish(RSA *rsa)
  353. {
  354. E_GMP_RSA_CTX *hptr = RSA_get_ex_data(rsa, hndidx_rsa);
  355. if(!hptr) return 0;
  356. mpz_clear(hptr->n);
  357. mpz_clear(hptr->d);
  358. mpz_clear(hptr->e);
  359. mpz_clear(hptr->p);
  360. mpz_clear(hptr->q);
  361. mpz_clear(hptr->dmp1);
  362. mpz_clear(hptr->dmq1);
  363. mpz_clear(hptr->iqmp);
  364. mpz_clear(hptr->r0);
  365. mpz_clear(hptr->r1);
  366. mpz_clear(hptr->I0);
  367. mpz_clear(hptr->m1);
  368. OPENSSL_free(hptr);
  369. RSA_set_ex_data(rsa, hndidx_rsa, NULL);
  370. return 1;
  371. }
  372. static int e_gmp_rsa_mod_exp(BIGNUM *r, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  373. {
  374. E_GMP_RSA_CTX *hptr;
  375. int to_return = 0;
  376. hptr = e_gmp_get_rsa(rsa);
  377. if(!hptr)
  378. {
  379. GMPerr(GMP_F_E_GMP_RSA_MOD_EXP,
  380. GMP_R_KEY_CONTEXT_ERROR);
  381. return 0;
  382. }
  383. if(hptr->public_only)
  384. {
  385. GMPerr(GMP_F_E_GMP_RSA_MOD_EXP,
  386. GMP_R_MISSING_KEY_COMPONENTS);
  387. return 0;
  388. }
  389. /* ugh!!! */
  390. if(!bn2gmp(I, hptr->I0))
  391. return 0;
  392. /* This is basically the CRT logic in crypto/rsa/rsa_eay.c reworded into
  393. * GMP-speak. It may be that GMP's API facilitates cleaner formulations
  394. * of this stuff, eg. better handling of negatives, or functions that
  395. * combine operations. */
  396. mpz_mod(hptr->r1, hptr->I0, hptr->q);
  397. mpz_powm(hptr->m1, hptr->r1, hptr->dmq1, hptr->q);
  398. mpz_mod(hptr->r1, hptr->I0, hptr->p);
  399. mpz_powm(hptr->r0, hptr->r1, hptr->dmp1, hptr->p);
  400. mpz_sub(hptr->r0, hptr->r0, hptr->m1);
  401. if(mpz_sgn(hptr->r0) < 0)
  402. mpz_add(hptr->r0, hptr->r0, hptr->p);
  403. mpz_mul(hptr->r1, hptr->r0, hptr->iqmp);
  404. mpz_mod(hptr->r0, hptr->r1, hptr->p);
  405. if(mpz_sgn(hptr->r0) < 0)
  406. mpz_add(hptr->r0, hptr->r0, hptr->p);
  407. mpz_mul(hptr->r1, hptr->r0, hptr->q);
  408. mpz_add(hptr->r0, hptr->r1, hptr->m1);
  409. /* ugh!!! */
  410. if(gmp2bn(hptr->r0, r))
  411. to_return = 1;
  412. return 1;
  413. }
  414. #endif
  415. #endif /* !OPENSSL_NO_GMP */
  416. /* This stuff is needed if this ENGINE is being compiled into a self-contained
  417. * shared-library. */
  418. #ifndef OPENSSL_NO_DYNAMIC_ENGINE
  419. IMPLEMENT_DYNAMIC_CHECK_FN()
  420. #ifndef OPENSSL_NO_GMP
  421. static int bind_fn(ENGINE *e, const char *id)
  422. {
  423. if(id && (strcmp(id, engine_e_gmp_id) != 0))
  424. return 0;
  425. if(!bind_helper(e))
  426. return 0;
  427. return 1;
  428. }
  429. IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
  430. #else
  431. OPENSSL_EXPORT
  432. int bind_engine(ENGINE *e, const char *id, const dynamic_fns *fns) { return 0; }
  433. #endif
  434. #endif /* !OPENSSL_NO_DYNAMIC_ENGINE */
  435. #endif /* !OPENSSL_NO_HW */