rsa_eay.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  1. /* crypto/rsa/rsa_eay.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 "cryptlib.h"
  60. #include <openssl/bn.h>
  61. #include <openssl/rsa.h>
  62. #include <openssl/rand.h>
  63. #include <openssl/engine.h>
  64. #ifndef RSA_NULL
  65. static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
  66. unsigned char *to, RSA *rsa,int padding);
  67. static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
  68. unsigned char *to, RSA *rsa,int padding);
  69. static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
  70. unsigned char *to, RSA *rsa,int padding);
  71. static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
  72. unsigned char *to, RSA *rsa,int padding);
  73. static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa);
  74. static int RSA_eay_init(RSA *rsa);
  75. static int RSA_eay_finish(RSA *rsa);
  76. static RSA_METHOD rsa_pkcs1_eay_meth={
  77. "Eric Young's PKCS#1 RSA",
  78. RSA_eay_public_encrypt,
  79. RSA_eay_public_decrypt, /* signature verification */
  80. RSA_eay_private_encrypt, /* signing */
  81. RSA_eay_private_decrypt,
  82. RSA_eay_mod_exp,
  83. BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
  84. RSA_eay_init,
  85. RSA_eay_finish,
  86. 0, /* flags */
  87. NULL,
  88. 0, /* rsa_sign */
  89. 0 /* rsa_verify */
  90. };
  91. const RSA_METHOD *RSA_PKCS1_SSLeay(void)
  92. {
  93. return(&rsa_pkcs1_eay_meth);
  94. }
  95. static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
  96. unsigned char *to, RSA *rsa, int padding)
  97. {
  98. BIGNUM f,ret;
  99. int i,j,k,num=0,r= -1;
  100. unsigned char *buf=NULL;
  101. BN_CTX *ctx=NULL;
  102. BN_init(&f);
  103. BN_init(&ret);
  104. if ((ctx=BN_CTX_new()) == NULL) goto err;
  105. num=BN_num_bytes(rsa->n);
  106. if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  107. {
  108. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
  109. goto err;
  110. }
  111. switch (padding)
  112. {
  113. case RSA_PKCS1_PADDING:
  114. i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
  115. break;
  116. #ifndef OPENSSL_NO_SHA
  117. case RSA_PKCS1_OAEP_PADDING:
  118. i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
  119. break;
  120. #endif
  121. case RSA_SSLV23_PADDING:
  122. i=RSA_padding_add_SSLv23(buf,num,from,flen);
  123. break;
  124. case RSA_NO_PADDING:
  125. i=RSA_padding_add_none(buf,num,from,flen);
  126. break;
  127. default:
  128. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  129. goto err;
  130. }
  131. if (i <= 0) goto err;
  132. if (BN_bin2bn(buf,num,&f) == NULL) goto err;
  133. if (BN_ucmp(&f, rsa->n) >= 0)
  134. {
  135. /* usually the padding functions would catch this */
  136. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  137. goto err;
  138. }
  139. if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
  140. {
  141. BN_MONT_CTX* bn_mont_ctx;
  142. if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
  143. goto err;
  144. if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
  145. {
  146. BN_MONT_CTX_free(bn_mont_ctx);
  147. goto err;
  148. }
  149. if (rsa->_method_mod_n == NULL) /* other thread may have finished first */
  150. {
  151. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  152. if (rsa->_method_mod_n == NULL)
  153. {
  154. rsa->_method_mod_n = bn_mont_ctx;
  155. bn_mont_ctx = NULL;
  156. }
  157. CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
  158. }
  159. if (bn_mont_ctx)
  160. BN_MONT_CTX_free(bn_mont_ctx);
  161. }
  162. if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
  163. rsa->_method_mod_n)) goto err;
  164. /* put in leading 0 bytes if the number is less than the
  165. * length of the modulus */
  166. j=BN_num_bytes(&ret);
  167. i=BN_bn2bin(&ret,&(to[num-j]));
  168. for (k=0; k<(num-i); k++)
  169. to[k]=0;
  170. r=num;
  171. err:
  172. if (ctx != NULL) BN_CTX_free(ctx);
  173. BN_clear_free(&f);
  174. BN_clear_free(&ret);
  175. if (buf != NULL)
  176. {
  177. OPENSSL_cleanse(buf,num);
  178. OPENSSL_free(buf);
  179. }
  180. return(r);
  181. }
  182. /* signing */
  183. static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
  184. unsigned char *to, RSA *rsa, int padding)
  185. {
  186. BIGNUM f,ret;
  187. int i,j,k,num=0,r= -1;
  188. unsigned char *buf=NULL;
  189. BN_CTX *ctx=NULL;
  190. BN_init(&f);
  191. BN_init(&ret);
  192. if ((ctx=BN_CTX_new()) == NULL) goto err;
  193. num=BN_num_bytes(rsa->n);
  194. if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  195. {
  196. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
  197. goto err;
  198. }
  199. switch (padding)
  200. {
  201. case RSA_PKCS1_PADDING:
  202. i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
  203. break;
  204. case RSA_NO_PADDING:
  205. i=RSA_padding_add_none(buf,num,from,flen);
  206. break;
  207. case RSA_SSLV23_PADDING:
  208. default:
  209. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  210. goto err;
  211. }
  212. if (i <= 0) goto err;
  213. if (BN_bin2bn(buf,num,&f) == NULL) goto err;
  214. if (BN_ucmp(&f, rsa->n) >= 0)
  215. {
  216. /* usually the padding functions would catch this */
  217. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  218. goto err;
  219. }
  220. if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
  221. RSA_blinding_on(rsa,ctx);
  222. if (rsa->flags & RSA_FLAG_BLINDING)
  223. if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
  224. if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
  225. ((rsa->p != NULL) &&
  226. (rsa->q != NULL) &&
  227. (rsa->dmp1 != NULL) &&
  228. (rsa->dmq1 != NULL) &&
  229. (rsa->iqmp != NULL)) )
  230. { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
  231. else
  232. {
  233. if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL)) goto err;
  234. }
  235. if (rsa->flags & RSA_FLAG_BLINDING)
  236. if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
  237. /* put in leading 0 bytes if the number is less than the
  238. * length of the modulus */
  239. j=BN_num_bytes(&ret);
  240. i=BN_bn2bin(&ret,&(to[num-j]));
  241. for (k=0; k<(num-i); k++)
  242. to[k]=0;
  243. r=num;
  244. err:
  245. if (ctx != NULL) BN_CTX_free(ctx);
  246. BN_clear_free(&ret);
  247. BN_clear_free(&f);
  248. if (buf != NULL)
  249. {
  250. OPENSSL_cleanse(buf,num);
  251. OPENSSL_free(buf);
  252. }
  253. return(r);
  254. }
  255. static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
  256. unsigned char *to, RSA *rsa, int padding)
  257. {
  258. BIGNUM f,ret;
  259. int j,num=0,r= -1;
  260. unsigned char *p;
  261. unsigned char *buf=NULL;
  262. BN_CTX *ctx=NULL;
  263. BN_init(&f);
  264. BN_init(&ret);
  265. ctx=BN_CTX_new();
  266. if (ctx == NULL) goto err;
  267. num=BN_num_bytes(rsa->n);
  268. if ((buf=(unsigned char *)OPENSSL_malloc(num)) == NULL)
  269. {
  270. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
  271. goto err;
  272. }
  273. /* This check was for equality but PGP does evil things
  274. * and chops off the top '0' bytes */
  275. if (flen > num)
  276. {
  277. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
  278. goto err;
  279. }
  280. /* make data into a big number */
  281. if (BN_bin2bn(from,(int)flen,&f) == NULL) goto err;
  282. if (BN_ucmp(&f, rsa->n) >= 0)
  283. {
  284. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  285. goto err;
  286. }
  287. if ((rsa->flags & RSA_FLAG_BLINDING) && (rsa->blinding == NULL))
  288. RSA_blinding_on(rsa,ctx);
  289. if (rsa->flags & RSA_FLAG_BLINDING)
  290. if (!BN_BLINDING_convert(&f,rsa->blinding,ctx)) goto err;
  291. /* do the decrypt */
  292. if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
  293. ((rsa->p != NULL) &&
  294. (rsa->q != NULL) &&
  295. (rsa->dmp1 != NULL) &&
  296. (rsa->dmq1 != NULL) &&
  297. (rsa->iqmp != NULL)) )
  298. { if (!rsa->meth->rsa_mod_exp(&ret,&f,rsa)) goto err; }
  299. else
  300. {
  301. if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->d,rsa->n,ctx,NULL))
  302. goto err;
  303. }
  304. if (rsa->flags & RSA_FLAG_BLINDING)
  305. if (!BN_BLINDING_invert(&ret,rsa->blinding,ctx)) goto err;
  306. p=buf;
  307. j=BN_bn2bin(&ret,p); /* j is only used with no-padding mode */
  308. switch (padding)
  309. {
  310. case RSA_PKCS1_PADDING:
  311. r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
  312. break;
  313. #ifndef OPENSSL_NO_SHA
  314. case RSA_PKCS1_OAEP_PADDING:
  315. r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
  316. break;
  317. #endif
  318. case RSA_SSLV23_PADDING:
  319. r=RSA_padding_check_SSLv23(to,num,buf,j,num);
  320. break;
  321. case RSA_NO_PADDING:
  322. r=RSA_padding_check_none(to,num,buf,j,num);
  323. break;
  324. default:
  325. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  326. goto err;
  327. }
  328. if (r < 0)
  329. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
  330. err:
  331. if (ctx != NULL) BN_CTX_free(ctx);
  332. BN_clear_free(&f);
  333. BN_clear_free(&ret);
  334. if (buf != NULL)
  335. {
  336. OPENSSL_cleanse(buf,num);
  337. OPENSSL_free(buf);
  338. }
  339. return(r);
  340. }
  341. /* signature verification */
  342. static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
  343. unsigned char *to, RSA *rsa, int padding)
  344. {
  345. BIGNUM f,ret;
  346. int i,num=0,r= -1;
  347. unsigned char *p;
  348. unsigned char *buf=NULL;
  349. BN_CTX *ctx=NULL;
  350. BN_init(&f);
  351. BN_init(&ret);
  352. ctx=BN_CTX_new();
  353. if (ctx == NULL) goto err;
  354. num=BN_num_bytes(rsa->n);
  355. buf=(unsigned char *)OPENSSL_malloc(num);
  356. if (buf == NULL)
  357. {
  358. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
  359. goto err;
  360. }
  361. /* This check was for equality but PGP does evil things
  362. * and chops off the top '0' bytes */
  363. if (flen > num)
  364. {
  365. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
  366. goto err;
  367. }
  368. if (BN_bin2bn(from,flen,&f) == NULL) goto err;
  369. if (BN_ucmp(&f, rsa->n) >= 0)
  370. {
  371. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  372. goto err;
  373. }
  374. /* do the decrypt */
  375. if ((rsa->_method_mod_n == NULL) && (rsa->flags & RSA_FLAG_CACHE_PUBLIC))
  376. {
  377. BN_MONT_CTX* bn_mont_ctx;
  378. if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
  379. goto err;
  380. if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->n,ctx))
  381. {
  382. BN_MONT_CTX_free(bn_mont_ctx);
  383. goto err;
  384. }
  385. if (rsa->_method_mod_n == NULL) /* other thread may have finished first */
  386. {
  387. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  388. if (rsa->_method_mod_n == NULL)
  389. {
  390. rsa->_method_mod_n = bn_mont_ctx;
  391. bn_mont_ctx = NULL;
  392. }
  393. CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
  394. }
  395. if (bn_mont_ctx)
  396. BN_MONT_CTX_free(bn_mont_ctx);
  397. }
  398. if (!rsa->meth->bn_mod_exp(&ret,&f,rsa->e,rsa->n,ctx,
  399. rsa->_method_mod_n)) goto err;
  400. p=buf;
  401. i=BN_bn2bin(&ret,p);
  402. switch (padding)
  403. {
  404. case RSA_PKCS1_PADDING:
  405. r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
  406. break;
  407. case RSA_NO_PADDING:
  408. r=RSA_padding_check_none(to,num,buf,i,num);
  409. break;
  410. default:
  411. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  412. goto err;
  413. }
  414. if (r < 0)
  415. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
  416. err:
  417. if (ctx != NULL) BN_CTX_free(ctx);
  418. BN_clear_free(&f);
  419. BN_clear_free(&ret);
  420. if (buf != NULL)
  421. {
  422. OPENSSL_cleanse(buf,num);
  423. OPENSSL_free(buf);
  424. }
  425. return(r);
  426. }
  427. static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
  428. {
  429. BIGNUM r1,m1,vrfy;
  430. int ret=0;
  431. BN_CTX *ctx;
  432. BN_init(&m1);
  433. BN_init(&r1);
  434. BN_init(&vrfy);
  435. if ((ctx=BN_CTX_new()) == NULL) goto err;
  436. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
  437. {
  438. if (rsa->_method_mod_p == NULL)
  439. {
  440. BN_MONT_CTX* bn_mont_ctx;
  441. if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
  442. goto err;
  443. if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->p,ctx))
  444. {
  445. BN_MONT_CTX_free(bn_mont_ctx);
  446. goto err;
  447. }
  448. if (rsa->_method_mod_p == NULL) /* other thread may have finished first */
  449. {
  450. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  451. if (rsa->_method_mod_p == NULL)
  452. {
  453. rsa->_method_mod_p = bn_mont_ctx;
  454. bn_mont_ctx = NULL;
  455. }
  456. CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
  457. }
  458. if (bn_mont_ctx)
  459. BN_MONT_CTX_free(bn_mont_ctx);
  460. }
  461. if (rsa->_method_mod_q == NULL)
  462. {
  463. BN_MONT_CTX* bn_mont_ctx;
  464. if ((bn_mont_ctx=BN_MONT_CTX_new()) == NULL)
  465. goto err;
  466. if (!BN_MONT_CTX_set(bn_mont_ctx,rsa->q,ctx))
  467. {
  468. BN_MONT_CTX_free(bn_mont_ctx);
  469. goto err;
  470. }
  471. if (rsa->_method_mod_q == NULL) /* other thread may have finished first */
  472. {
  473. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  474. if (rsa->_method_mod_q == NULL)
  475. {
  476. rsa->_method_mod_q = bn_mont_ctx;
  477. bn_mont_ctx = NULL;
  478. }
  479. CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
  480. }
  481. if (bn_mont_ctx)
  482. BN_MONT_CTX_free(bn_mont_ctx);
  483. }
  484. }
  485. if (!BN_mod(&r1,I,rsa->q,ctx)) goto err;
  486. if (!rsa->meth->bn_mod_exp(&m1,&r1,rsa->dmq1,rsa->q,ctx,
  487. rsa->_method_mod_q)) goto err;
  488. if (!BN_mod(&r1,I,rsa->p,ctx)) goto err;
  489. if (!rsa->meth->bn_mod_exp(r0,&r1,rsa->dmp1,rsa->p,ctx,
  490. rsa->_method_mod_p)) goto err;
  491. if (!BN_sub(r0,r0,&m1)) goto err;
  492. /* This will help stop the size of r0 increasing, which does
  493. * affect the multiply if it optimised for a power of 2 size */
  494. if (BN_get_sign(r0))
  495. if (!BN_add(r0,r0,rsa->p)) goto err;
  496. if (!BN_mul(&r1,r0,rsa->iqmp,ctx)) goto err;
  497. if (!BN_mod(r0,&r1,rsa->p,ctx)) goto err;
  498. /* If p < q it is occasionally possible for the correction of
  499. * adding 'p' if r0 is negative above to leave the result still
  500. * negative. This can break the private key operations: the following
  501. * second correction should *always* correct this rare occurrence.
  502. * This will *never* happen with OpenSSL generated keys because
  503. * they ensure p > q [steve]
  504. */
  505. if (BN_get_sign(r0))
  506. if (!BN_add(r0,r0,rsa->p)) goto err;
  507. if (!BN_mul(&r1,r0,rsa->q,ctx)) goto err;
  508. if (!BN_add(r0,&r1,&m1)) goto err;
  509. if (rsa->e && rsa->n)
  510. {
  511. if (!rsa->meth->bn_mod_exp(&vrfy,r0,rsa->e,rsa->n,ctx,NULL)) goto err;
  512. /* If 'I' was greater than (or equal to) rsa->n, the operation
  513. * will be equivalent to using 'I mod n'. However, the result of
  514. * the verify will *always* be less than 'n' so we don't check
  515. * for absolute equality, just congruency. */
  516. if (!BN_sub(&vrfy, &vrfy, I)) goto err;
  517. if (!BN_mod(&vrfy, &vrfy, rsa->n, ctx)) goto err;
  518. if (BN_get_sign(&vrfy))
  519. if (!BN_add(&vrfy, &vrfy, rsa->n)) goto err;
  520. if (!BN_is_zero(&vrfy))
  521. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  522. * miscalculated CRT output, just do a raw (slower)
  523. * mod_exp and return that instead. */
  524. if (!rsa->meth->bn_mod_exp(r0,I,rsa->d,rsa->n,ctx,NULL)) goto err;
  525. }
  526. ret=1;
  527. err:
  528. BN_clear_free(&m1);
  529. BN_clear_free(&r1);
  530. BN_clear_free(&vrfy);
  531. BN_CTX_free(ctx);
  532. return(ret);
  533. }
  534. static int RSA_eay_init(RSA *rsa)
  535. {
  536. rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
  537. return(1);
  538. }
  539. static int RSA_eay_finish(RSA *rsa)
  540. {
  541. if (rsa->_method_mod_n != NULL)
  542. BN_MONT_CTX_free(rsa->_method_mod_n);
  543. if (rsa->_method_mod_p != NULL)
  544. BN_MONT_CTX_free(rsa->_method_mod_p);
  545. if (rsa->_method_mod_q != NULL)
  546. BN_MONT_CTX_free(rsa->_method_mod_q);
  547. return(1);
  548. }
  549. #endif