e_gmp.c 12 KB

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