2
0

rsa_lib.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. default_RSA_meth = meth;
  78. }
  79. const RSA_METHOD *RSA_get_default_method(void)
  80. {
  81. if (default_RSA_meth == NULL)
  82. {
  83. #ifdef RSA_NULL
  84. default_RSA_meth=RSA_null_method();
  85. #else
  86. #if 0 /* was: #ifdef RSAref */
  87. default_RSA_meth=RSA_PKCS1_RSAref();
  88. #else
  89. default_RSA_meth=RSA_PKCS1_SSLeay();
  90. #endif
  91. #endif
  92. }
  93. return default_RSA_meth;
  94. }
  95. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  96. {
  97. return rsa->meth;
  98. }
  99. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  100. {
  101. /* NB: The caller is specifically setting a method, so it's not up to us
  102. * to deal with which ENGINE it comes from. */
  103. const RSA_METHOD *mtmp;
  104. mtmp = rsa->meth;
  105. if (mtmp->finish) mtmp->finish(rsa);
  106. #ifndef OPENSSL_NO_ENGINE
  107. if (rsa->engine)
  108. {
  109. ENGINE_finish(rsa->engine);
  110. rsa->engine = NULL;
  111. }
  112. #endif
  113. rsa->meth = meth;
  114. if (meth->init) meth->init(rsa);
  115. return 1;
  116. }
  117. RSA *RSA_new_method(ENGINE *engine)
  118. {
  119. RSA *ret;
  120. ret=(RSA *)OPENSSL_malloc(sizeof(RSA));
  121. if (ret == NULL)
  122. {
  123. RSAerr(RSA_F_RSA_NEW_METHOD,ERR_R_MALLOC_FAILURE);
  124. return NULL;
  125. }
  126. ret->meth = RSA_get_default_method();
  127. #ifndef OPENSSL_NO_ENGINE
  128. if (engine)
  129. {
  130. if (!ENGINE_init(engine))
  131. {
  132. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  133. OPENSSL_free(ret);
  134. return NULL;
  135. }
  136. ret->engine = engine;
  137. }
  138. else
  139. ret->engine = ENGINE_get_default_RSA();
  140. if(ret->engine)
  141. {
  142. ret->meth = ENGINE_get_RSA(ret->engine);
  143. if(!ret->meth)
  144. {
  145. RSAerr(RSA_F_RSA_NEW_METHOD,
  146. ERR_R_ENGINE_LIB);
  147. ENGINE_finish(ret->engine);
  148. OPENSSL_free(ret);
  149. return NULL;
  150. }
  151. }
  152. #endif
  153. ret->pad=0;
  154. ret->version=0;
  155. ret->n=NULL;
  156. ret->e=NULL;
  157. ret->d=NULL;
  158. ret->p=NULL;
  159. ret->q=NULL;
  160. ret->dmp1=NULL;
  161. ret->dmq1=NULL;
  162. ret->iqmp=NULL;
  163. ret->references=1;
  164. ret->_method_mod_n=NULL;
  165. ret->_method_mod_p=NULL;
  166. ret->_method_mod_q=NULL;
  167. ret->blinding=NULL;
  168. ret->mt_blinding=NULL;
  169. ret->bignum_data=NULL;
  170. ret->flags=ret->meth->flags;
  171. CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
  172. if ((ret->meth->init != NULL) && !ret->meth->init(ret))
  173. {
  174. #ifndef OPENSSL_NO_ENGINE
  175. if (ret->engine)
  176. ENGINE_finish(ret->engine);
  177. #endif
  178. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data);
  179. OPENSSL_free(ret);
  180. ret=NULL;
  181. }
  182. return(ret);
  183. }
  184. void RSA_free(RSA *r)
  185. {
  186. int i;
  187. if (r == NULL) return;
  188. i=CRYPTO_add(&r->references,-1,CRYPTO_LOCK_RSA);
  189. #ifdef REF_PRINT
  190. REF_PRINT("RSA",r);
  191. #endif
  192. if (i > 0) return;
  193. #ifdef REF_CHECK
  194. if (i < 0)
  195. {
  196. fprintf(stderr,"RSA_free, bad reference count\n");
  197. abort();
  198. }
  199. #endif
  200. if (r->meth->finish)
  201. r->meth->finish(r);
  202. #ifndef OPENSSL_NO_ENGINE
  203. if (r->engine)
  204. ENGINE_finish(r->engine);
  205. #endif
  206. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  207. if (r->n != NULL) BN_clear_free(r->n);
  208. if (r->e != NULL) BN_clear_free(r->e);
  209. if (r->d != NULL) BN_clear_free(r->d);
  210. if (r->p != NULL) BN_clear_free(r->p);
  211. if (r->q != NULL) BN_clear_free(r->q);
  212. if (r->dmp1 != NULL) BN_clear_free(r->dmp1);
  213. if (r->dmq1 != NULL) BN_clear_free(r->dmq1);
  214. if (r->iqmp != NULL) BN_clear_free(r->iqmp);
  215. if (r->blinding != NULL) BN_BLINDING_free(r->blinding);
  216. if (r->mt_blinding != NULL) BN_BLINDING_free(r->mt_blinding);
  217. if (r->bignum_data != NULL) OPENSSL_free_locked(r->bignum_data);
  218. OPENSSL_free(r);
  219. }
  220. int RSA_up_ref(RSA *r)
  221. {
  222. int i = CRYPTO_add(&r->references, 1, CRYPTO_LOCK_RSA);
  223. #ifdef REF_PRINT
  224. REF_PRINT("RSA",r);
  225. #endif
  226. #ifdef REF_CHECK
  227. if (i < 2)
  228. {
  229. fprintf(stderr, "RSA_up_ref, bad reference count\n");
  230. abort();
  231. }
  232. #endif
  233. return ((i > 1) ? 1 : 0);
  234. }
  235. int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_new *new_func,
  236. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func)
  237. {
  238. return CRYPTO_get_ex_new_index(CRYPTO_EX_INDEX_RSA, argl, argp,
  239. new_func, dup_func, free_func);
  240. }
  241. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  242. {
  243. return(CRYPTO_set_ex_data(&r->ex_data,idx,arg));
  244. }
  245. void *RSA_get_ex_data(const RSA *r, int idx)
  246. {
  247. return(CRYPTO_get_ex_data(&r->ex_data,idx));
  248. }
  249. int RSA_size(const RSA *r)
  250. {
  251. return(BN_num_bytes(r->n));
  252. }
  253. int RSA_public_encrypt(int flen, const unsigned char *from, unsigned char *to,
  254. RSA *rsa, int padding)
  255. {
  256. return(rsa->meth->rsa_pub_enc(flen, from, to, rsa, padding));
  257. }
  258. int RSA_private_encrypt(int flen, const unsigned char *from, unsigned char *to,
  259. RSA *rsa, int padding)
  260. {
  261. return(rsa->meth->rsa_priv_enc(flen, from, to, rsa, padding));
  262. }
  263. int RSA_private_decrypt(int flen, const unsigned char *from, unsigned char *to,
  264. RSA *rsa, int padding)
  265. {
  266. return(rsa->meth->rsa_priv_dec(flen, from, to, rsa, padding));
  267. }
  268. int RSA_public_decrypt(int flen, const unsigned char *from, unsigned char *to,
  269. RSA *rsa, int padding)
  270. {
  271. return(rsa->meth->rsa_pub_dec(flen, from, to, rsa, padding));
  272. }
  273. int RSA_flags(const RSA *r)
  274. {
  275. return((r == NULL)?0:r->meth->flags);
  276. }
  277. void RSA_blinding_off(RSA *rsa)
  278. {
  279. if (rsa->blinding != NULL)
  280. {
  281. BN_BLINDING_free(rsa->blinding);
  282. rsa->blinding=NULL;
  283. }
  284. rsa->flags &= ~RSA_FLAG_BLINDING;
  285. rsa->flags |= RSA_FLAG_NO_BLINDING;
  286. }
  287. int RSA_blinding_on(RSA *rsa, BN_CTX *ctx)
  288. {
  289. int ret=0;
  290. if (rsa->blinding != NULL)
  291. RSA_blinding_off(rsa);
  292. rsa->blinding = RSA_setup_blinding(rsa, ctx);
  293. if (rsa->blinding == NULL)
  294. goto err;
  295. rsa->flags |= RSA_FLAG_BLINDING;
  296. rsa->flags &= ~RSA_FLAG_NO_BLINDING;
  297. ret=1;
  298. err:
  299. return(ret);
  300. }
  301. static BIGNUM *rsa_get_public_exp(const BIGNUM *d, const BIGNUM *p,
  302. const BIGNUM *q, BN_CTX *ctx)
  303. {
  304. BIGNUM *ret = NULL, *r0, *r1, *r2;
  305. if (d == NULL || p == NULL || q == NULL)
  306. return NULL;
  307. BN_CTX_start(ctx);
  308. r0 = BN_CTX_get(ctx);
  309. r1 = BN_CTX_get(ctx);
  310. r2 = BN_CTX_get(ctx);
  311. if (r2 == NULL)
  312. goto err;
  313. if (!BN_sub(r1, p, BN_value_one())) goto err;
  314. if (!BN_sub(r2, q, BN_value_one())) goto err;
  315. if (!BN_mul(r0, r1, r2, ctx)) goto err;
  316. ret = BN_mod_inverse(NULL, d, r0, ctx);
  317. err:
  318. BN_CTX_end(ctx);
  319. return ret;
  320. }
  321. BN_BLINDING *RSA_setup_blinding(RSA *rsa, BN_CTX *in_ctx)
  322. {
  323. BIGNUM *e;
  324. BN_CTX *ctx;
  325. BN_BLINDING *ret = NULL;
  326. if (in_ctx == NULL)
  327. {
  328. if ((ctx = BN_CTX_new()) == NULL) return 0;
  329. }
  330. else
  331. ctx = in_ctx;
  332. BN_CTX_start(ctx);
  333. e = BN_CTX_get(ctx);
  334. if (e == NULL)
  335. {
  336. RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_MALLOC_FAILURE);
  337. goto err;
  338. }
  339. if (rsa->e == NULL)
  340. {
  341. e = rsa_get_public_exp(rsa->d, rsa->p, rsa->q, ctx);
  342. if (e == NULL)
  343. {
  344. RSAerr(RSA_F_RSA_SETUP_BLINDING, RSA_R_NO_PUBLIC_EXPONENT);
  345. goto err;
  346. }
  347. }
  348. else
  349. e = rsa->e;
  350. if ((RAND_status() == 0) && rsa->d != NULL && rsa->d->d != NULL)
  351. {
  352. /* if PRNG is not properly seeded, resort to secret
  353. * exponent as unpredictable seed */
  354. RAND_add(rsa->d->d, rsa->d->dmax * sizeof rsa->d->d[0], 0.0);
  355. }
  356. ret = BN_BLINDING_create_param(NULL, e, rsa->n, ctx,
  357. rsa->meth->bn_mod_exp, rsa->_method_mod_n);
  358. if (ret == NULL)
  359. {
  360. RSAerr(RSA_F_RSA_SETUP_BLINDING, ERR_R_BN_LIB);
  361. goto err;
  362. }
  363. BN_BLINDING_set_thread_id(ret, CRYPTO_thread_id());
  364. err:
  365. BN_CTX_end(ctx);
  366. if (in_ctx == NULL)
  367. BN_CTX_free(ctx);
  368. return ret;
  369. }
  370. int RSA_memory_lock(RSA *r)
  371. {
  372. int i,j,k,off;
  373. char *p;
  374. BIGNUM *bn,**t[6],*b;
  375. BN_ULONG *ul;
  376. if (r->d == NULL) return(1);
  377. t[0]= &r->d;
  378. t[1]= &r->p;
  379. t[2]= &r->q;
  380. t[3]= &r->dmp1;
  381. t[4]= &r->dmq1;
  382. t[5]= &r->iqmp;
  383. k=sizeof(BIGNUM)*6;
  384. off=k/sizeof(BN_ULONG)+1;
  385. j=1;
  386. for (i=0; i<6; i++)
  387. j+= (*t[i])->top;
  388. if ((p=OPENSSL_malloc_locked((off+j)*sizeof(BN_ULONG))) == NULL)
  389. {
  390. RSAerr(RSA_F_RSA_MEMORY_LOCK,ERR_R_MALLOC_FAILURE);
  391. return(0);
  392. }
  393. bn=(BIGNUM *)p;
  394. ul=(BN_ULONG *)&(p[off]);
  395. for (i=0; i<6; i++)
  396. {
  397. b= *(t[i]);
  398. *(t[i])= &(bn[i]);
  399. memcpy((char *)&(bn[i]),(char *)b,sizeof(BIGNUM));
  400. bn[i].flags=BN_FLG_STATIC_DATA;
  401. bn[i].d=ul;
  402. memcpy((char *)ul,b->d,sizeof(BN_ULONG)*b->top);
  403. ul+=b->top;
  404. BN_clear_free(b);
  405. }
  406. /* I should fix this so it can still be done */
  407. r->flags&= ~(RSA_FLAG_CACHE_PRIVATE|RSA_FLAG_CACHE_PUBLIC);
  408. r->bignum_data=p;
  409. return(1);
  410. }