rsa_eng.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /* crypto/rsa/rsa_lib.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #include <stdio.h>
  59. #include <openssl/crypto.h>
  60. #include "cryptlib.h"
  61. #include <openssl/lhash.h>
  62. #include <openssl/bn.h>
  63. #include <openssl/rsa.h>
  64. #include <openssl/rand.h>
  65. #ifndef OPENSSL_NO_ENGINE
  66. # include <openssl/engine.h>
  67. #endif
  68. const char RSA_version[] = "RSA" OPENSSL_VERSION_PTEXT;
  69. static const RSA_METHOD *default_RSA_meth = NULL;
  70. RSA *RSA_new(void)
  71. {
  72. RSA *r = RSA_new_method(NULL);
  73. return r;
  74. }
  75. void RSA_set_default_method(const RSA_METHOD *meth)
  76. {
  77. #ifdef OPENSSL_FIPS
  78. if (FIPS_mode() && !(meth->flags & RSA_FLAG_FIPS_METHOD)) {
  79. RSAerr(RSA_F_RSA_SET_DEFAULT_METHOD, RSA_R_NON_FIPS_METHOD);
  80. return;
  81. }
  82. #endif
  83. default_RSA_meth = meth;
  84. }
  85. const RSA_METHOD *RSA_get_default_method(void)
  86. {
  87. if (default_RSA_meth == NULL) {
  88. #ifdef RSA_NULL
  89. default_RSA_meth = RSA_null_method();
  90. #else
  91. # if 0 /* was: #ifdef RSAref */
  92. default_RSA_meth = RSA_PKCS1_RSAref();
  93. # else
  94. default_RSA_meth = RSA_PKCS1_SSLeay();
  95. # endif
  96. #endif
  97. }
  98. return default_RSA_meth;
  99. }
  100. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  101. {
  102. return rsa->meth;
  103. }
  104. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  105. {
  106. /*
  107. * NB: The caller is specifically setting a method, so it's not up to us
  108. * to deal with which ENGINE it comes from.
  109. */
  110. const RSA_METHOD *mtmp;
  111. #ifdef OPENSSL_FIPS
  112. if (FIPS_mode() && !(meth->flags & RSA_FLAG_FIPS_METHOD)) {
  113. RSAerr(RSA_F_RSA_SET_METHOD, RSA_R_NON_FIPS_METHOD);
  114. return 0;
  115. }
  116. #endif
  117. mtmp = rsa->meth;
  118. if (mtmp->finish)
  119. mtmp->finish(rsa);
  120. #ifndef OPENSSL_NO_ENGINE
  121. if (rsa->engine) {
  122. ENGINE_finish(rsa->engine);
  123. rsa->engine = NULL;
  124. }
  125. #endif
  126. rsa->meth = meth;
  127. if (meth->init)
  128. meth->init(rsa);
  129. return 1;
  130. }
  131. RSA *RSA_new_method(ENGINE *engine)
  132. {
  133. RSA *ret;
  134. ret = (RSA *)OPENSSL_malloc(sizeof(RSA));
  135. if (ret == NULL) {
  136. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  137. return NULL;
  138. }
  139. ret->meth = RSA_get_default_method();
  140. #ifndef OPENSSL_NO_ENGINE
  141. if (engine) {
  142. if (!ENGINE_init(engine)) {
  143. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  144. OPENSSL_free(ret);
  145. return NULL;
  146. }
  147. ret->engine = engine;
  148. } else
  149. ret->engine = ENGINE_get_default_RSA();
  150. if (ret->engine) {
  151. ret->meth = ENGINE_get_RSA(ret->engine);
  152. if (!ret->meth) {
  153. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  154. ENGINE_finish(ret->engine);
  155. OPENSSL_free(ret);
  156. return NULL;
  157. }
  158. }
  159. #endif
  160. #ifdef OPENSSL_FIPS
  161. if (FIPS_mode() && !(ret->meth->flags & RSA_FLAG_FIPS_METHOD)) {
  162. RSAerr(RSA_F_RSA_NEW_METHOD, RSA_R_NON_FIPS_METHOD);
  163. # ifndef OPENSSL_NO_ENGINE
  164. if (ret->engine)
  165. ENGINE_finish(ret->engine);
  166. # endif
  167. OPENSSL_free(ret);
  168. return NULL;
  169. }
  170. #endif
  171. ret->pad = 0;
  172. ret->version = 0;
  173. ret->n = NULL;
  174. ret->e = NULL;
  175. ret->d = NULL;
  176. ret->p = NULL;
  177. ret->q = NULL;
  178. ret->dmp1 = NULL;
  179. ret->dmq1 = NULL;
  180. ret->iqmp = NULL;
  181. ret->references = 1;
  182. ret->_method_mod_n = NULL;
  183. ret->_method_mod_p = NULL;
  184. ret->_method_mod_q = NULL;
  185. ret->blinding = NULL;
  186. ret->mt_blinding = NULL;
  187. ret->bignum_data = NULL;
  188. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  189. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
  190. #ifndef OPENSSL_NO_ENGINE
  191. if (ret->engine)
  192. ENGINE_finish(ret->engine);
  193. #endif
  194. OPENSSL_free(ret);
  195. return (NULL);
  196. }
  197. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  198. #ifndef OPENSSL_NO_ENGINE
  199. if (ret->engine)
  200. ENGINE_finish(ret->engine);
  201. #endif
  202. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
  203. OPENSSL_free(ret);
  204. ret = NULL;
  205. }
  206. return (ret);
  207. }
  208. void RSA_free(RSA *r)
  209. {
  210. int i;
  211. if (r == NULL)
  212. return;
  213. i = CRYPTO_add(&r->references, -1, CRYPTO_LOCK_RSA);
  214. #ifdef REF_PRINT
  215. REF_PRINT("RSA", r);
  216. #endif
  217. if (i > 0)
  218. return;
  219. #ifdef REF_CHECK
  220. if (i < 0) {
  221. fprintf(stderr, "RSA_free, bad reference count\n");
  222. abort();
  223. }
  224. #endif
  225. if (r->meth->finish)
  226. r->meth->finish(r);
  227. #ifndef OPENSSL_NO_ENGINE
  228. if (r->engine)
  229. ENGINE_finish(r->engine);
  230. #endif
  231. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  232. if (r->n != NULL)
  233. BN_clear_free(r->n);
  234. if (r->e != NULL)
  235. BN_clear_free(r->e);
  236. if (r->d != NULL)
  237. BN_clear_free(r->d);
  238. if (r->p != NULL)
  239. BN_clear_free(r->p);
  240. if (r->q != NULL)
  241. BN_clear_free(r->q);
  242. if (r->dmp1 != NULL)
  243. BN_clear_free(r->dmp1);
  244. if (r->dmq1 != NULL)
  245. BN_clear_free(r->dmq1);
  246. if (r->iqmp != NULL)
  247. BN_clear_free(r->iqmp);
  248. if (r->blinding != NULL)
  249. BN_BLINDING_free(r->blinding);
  250. if (r->mt_blinding != NULL)
  251. BN_BLINDING_free(r->mt_blinding);
  252. if (r->bignum_data != NULL)
  253. OPENSSL_free_locked(r->bignum_data);
  254. OPENSSL_free(r);
  255. }
  256. int RSA_up_ref(RSA *r)
  257. {
  258. int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
  259. #ifdef REF_PRINT
  260. REF_PRINT("RSA", r);
  261. #endif
  262. #ifdef REF_CHECK
  263. if (i < 2) {
  264. fprintf(stderr, "RSA_up_ref, bad reference count\n");
  265. abort();
  266. }
  267. #endif
  268. return ((i > 1) ? 1 : 0);
  269. }
  270. int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  271. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  272. {
  273. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
  274. new_func, dup_func, free_func);
  275. }
  276. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  277. {
  278. return (CRYPTO_set_ex_data(&r->ex_data, idx, arg));
  279. }
  280. void *RSA_get_ex_data(const RSA *r, int idx)
  281. {
  282. return (CRYPTO_get_ex_data(&r->ex_data, idx));
  283. }
  284. int RSA_flags(const RSA *r)
  285. {
  286. return ((r == NULL) ? 0 : r->meth->flags);
  287. }
  288. int RSA_memory_lock(RSA *r)
  289. {
  290. int i, j, k, off;
  291. char *p;
  292. BIGNUM *bn, **t[6], *b;
  293. BN_ULONG *ul;
  294. if (r->d == NULL)
  295. return (1);
  296. t[0] = &r->d;
  297. t[1] = &r->p;
  298. t[2] = &r->q;
  299. t[3] = &r->dmp1;
  300. t[4] = &r->dmq1;
  301. t[5] = &r->iqmp;
  302. k = sizeof(BIGNUM) * 6;
  303. off = k / sizeof(BN_ULONG) + 1;
  304. j = 1;
  305. for (i = 0; i < 6; i++)
  306. j += (*t[i])->top;
  307. if ((p = OPENSSL_malloc_locked((off + j) * sizeof(BN_ULONG))) == NULL) {
  308. RSAerr(RSA_F_RSA_MEMORY_LOCK, ERR_R_MALLOC_FAILURE);
  309. return (0);
  310. }
  311. bn = (BIGNUM *)p;
  312. ul = (BN_ULONG *)&(p[off]);
  313. for (i = 0; i < 6; i++) {
  314. b = *(t[i]);
  315. *(t[i]) = &(bn[i]);
  316. memcpy((char *)&(bn[i]), (char *)b, sizeof(BIGNUM));
  317. bn[i].flags = BN_FLG_STATIC_DATA;
  318. bn[i].d = ul;
  319. memcpy((char *)ul, b->d, sizeof(BN_ULONG) * b->top);
  320. ul += b->top;
  321. BN_clear_free(b);
  322. }
  323. /* I should fix this so it can still be done */
  324. r->flags &= ~(RSA_FLAG_CACHE_PRIVATE | RSA_FLAG_CACHE_PUBLIC);
  325. r->bignum_data = p;
  326. return (1);
  327. }