rsa_lib.c 8.3 KB

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