rsa_eay.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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. /* ====================================================================
  59. * Copyright (c) 1998-2006 The OpenSSL Project. All rights reserved.
  60. *
  61. * Redistribution and use in source and binary forms, with or without
  62. * modification, are permitted provided that the following conditions
  63. * are met:
  64. *
  65. * 1. Redistributions of source code must retain the above copyright
  66. * notice, this list of conditions and the following disclaimer.
  67. *
  68. * 2. Redistributions in binary form must reproduce the above copyright
  69. * notice, this list of conditions and the following disclaimer in
  70. * the documentation and/or other materials provided with the
  71. * distribution.
  72. *
  73. * 3. All advertising materials mentioning features or use of this
  74. * software must display the following acknowledgment:
  75. * "This product includes software developed by the OpenSSL Project
  76. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  77. *
  78. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  79. * endorse or promote products derived from this software without
  80. * prior written permission. For written permission, please contact
  81. * openssl-core@openssl.org.
  82. *
  83. * 5. Products derived from this software may not be called "OpenSSL"
  84. * nor may "OpenSSL" appear in their names without prior written
  85. * permission of the OpenSSL Project.
  86. *
  87. * 6. Redistributions of any form whatsoever must retain the following
  88. * acknowledgment:
  89. * "This product includes software developed by the OpenSSL Project
  90. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  91. *
  92. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  93. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  94. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  95. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  96. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  97. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  98. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  99. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  100. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  101. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  102. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  103. * OF THE POSSIBILITY OF SUCH DAMAGE.
  104. * ====================================================================
  105. *
  106. * This product includes cryptographic software written by Eric Young
  107. * (eay@cryptsoft.com). This product includes software written by Tim
  108. * Hudson (tjh@cryptsoft.com).
  109. *
  110. */
  111. #define OPENSSL_FIPSAPI
  112. #include <stdio.h>
  113. #include "cryptlib.h"
  114. #include <openssl/bn.h>
  115. #include <openssl/rsa.h>
  116. #include <openssl/rand.h>
  117. #ifdef OPENSSL_FIPS
  118. #include <openssl/fips.h>
  119. #endif
  120. #ifndef RSA_NULL
  121. static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
  122. unsigned char *to, RSA *rsa,int padding);
  123. static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
  124. unsigned char *to, RSA *rsa,int padding);
  125. static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
  126. unsigned char *to, RSA *rsa,int padding);
  127. static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
  128. unsigned char *to, RSA *rsa,int padding);
  129. static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa, BN_CTX *ctx);
  130. static int RSA_eay_init(RSA *rsa);
  131. static int RSA_eay_finish(RSA *rsa);
  132. static RSA_METHOD rsa_pkcs1_eay_meth={
  133. "Eric Young's PKCS#1 RSA",
  134. RSA_eay_public_encrypt,
  135. RSA_eay_public_decrypt, /* signature verification */
  136. RSA_eay_private_encrypt, /* signing */
  137. RSA_eay_private_decrypt,
  138. RSA_eay_mod_exp,
  139. BN_mod_exp_mont, /* XXX probably we should not use Montgomery if e == 3 */
  140. RSA_eay_init,
  141. RSA_eay_finish,
  142. RSA_FLAG_FIPS_METHOD, /* flags */
  143. NULL,
  144. 0, /* rsa_sign */
  145. 0, /* rsa_verify */
  146. NULL /* rsa_keygen */
  147. };
  148. const RSA_METHOD *RSA_PKCS1_SSLeay(void)
  149. {
  150. return(&rsa_pkcs1_eay_meth);
  151. }
  152. static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
  153. unsigned char *to, RSA *rsa, int padding)
  154. {
  155. BIGNUM *f,*ret;
  156. int i,j,k,num=0,r= -1;
  157. unsigned char *buf=NULL;
  158. BN_CTX *ctx=NULL;
  159. #ifdef OPENSSL_FIPS
  160. if(FIPS_selftest_failed())
  161. {
  162. FIPSerr(FIPS_F_RSA_EAY_PUBLIC_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
  163. goto err;
  164. }
  165. if (FIPS_module_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
  166. && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
  167. {
  168. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
  169. return -1;
  170. }
  171. #endif
  172. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
  173. {
  174. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
  175. return -1;
  176. }
  177. if (BN_ucmp(rsa->n, rsa->e) <= 0)
  178. {
  179. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
  180. return -1;
  181. }
  182. /* for large moduli, enforce exponent limit */
  183. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
  184. {
  185. if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
  186. {
  187. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
  188. return -1;
  189. }
  190. }
  191. if ((ctx=BN_CTX_new()) == NULL) goto err;
  192. BN_CTX_start(ctx);
  193. f = BN_CTX_get(ctx);
  194. ret = BN_CTX_get(ctx);
  195. num=BN_num_bytes(rsa->n);
  196. buf = OPENSSL_malloc(num);
  197. if (!f || !ret || !buf)
  198. {
  199. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,ERR_R_MALLOC_FAILURE);
  200. goto err;
  201. }
  202. switch (padding)
  203. {
  204. case RSA_PKCS1_PADDING:
  205. i=RSA_padding_add_PKCS1_type_2(buf,num,from,flen);
  206. break;
  207. #ifndef OPENSSL_NO_SHA
  208. case RSA_PKCS1_OAEP_PADDING:
  209. i=RSA_padding_add_PKCS1_OAEP(buf,num,from,flen,NULL,0);
  210. break;
  211. #endif
  212. case RSA_SSLV23_PADDING:
  213. i=RSA_padding_add_SSLv23(buf,num,from,flen);
  214. break;
  215. case RSA_NO_PADDING:
  216. i=RSA_padding_add_none(buf,num,from,flen);
  217. break;
  218. default:
  219. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  220. goto err;
  221. }
  222. if (i <= 0) goto err;
  223. if (BN_bin2bn(buf,num,f) == NULL) goto err;
  224. if (BN_ucmp(f, rsa->n) >= 0)
  225. {
  226. /* usually the padding functions would catch this */
  227. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  228. goto err;
  229. }
  230. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  231. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  232. goto err;
  233. if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
  234. rsa->_method_mod_n)) goto err;
  235. /* put in leading 0 bytes if the number is less than the
  236. * length of the modulus */
  237. j=BN_num_bytes(ret);
  238. i=BN_bn2bin(ret,&(to[num-j]));
  239. for (k=0; k<(num-i); k++)
  240. to[k]=0;
  241. r=num;
  242. err:
  243. if (ctx != NULL)
  244. {
  245. BN_CTX_end(ctx);
  246. BN_CTX_free(ctx);
  247. }
  248. if (buf != NULL)
  249. {
  250. OPENSSL_cleanse(buf,num);
  251. OPENSSL_free(buf);
  252. }
  253. return(r);
  254. }
  255. static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
  256. {
  257. BN_BLINDING *ret;
  258. int got_write_lock = 0;
  259. CRYPTO_THREADID cur;
  260. CRYPTO_r_lock(CRYPTO_LOCK_RSA);
  261. if (rsa->blinding == NULL)
  262. {
  263. CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
  264. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  265. got_write_lock = 1;
  266. if (rsa->blinding == NULL)
  267. rsa->blinding = RSA_setup_blinding(rsa, ctx);
  268. }
  269. ret = rsa->blinding;
  270. if (ret == NULL)
  271. goto err;
  272. CRYPTO_THREADID_current(&cur);
  273. if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret)))
  274. {
  275. /* rsa->blinding is ours! */
  276. *local = 1;
  277. }
  278. else
  279. {
  280. /* resort to rsa->mt_blinding instead */
  281. *local = 0; /* instructs rsa_blinding_convert(), rsa_blinding_invert()
  282. * that the BN_BLINDING is shared, meaning that accesses
  283. * require locks, and that the blinding factor must be
  284. * stored outside the BN_BLINDING
  285. */
  286. if (rsa->mt_blinding == NULL)
  287. {
  288. if (!got_write_lock)
  289. {
  290. CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
  291. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  292. got_write_lock = 1;
  293. }
  294. if (rsa->mt_blinding == NULL)
  295. rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
  296. }
  297. ret = rsa->mt_blinding;
  298. }
  299. err:
  300. if (got_write_lock)
  301. CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
  302. else
  303. CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
  304. return ret;
  305. }
  306. static int rsa_blinding_convert(BN_BLINDING *b, int local, BIGNUM *f,
  307. BIGNUM *r, BN_CTX *ctx)
  308. {
  309. if (local)
  310. return BN_BLINDING_convert_ex(f, NULL, b, ctx);
  311. else
  312. {
  313. int ret;
  314. CRYPTO_r_lock(CRYPTO_LOCK_RSA_BLINDING);
  315. ret = BN_BLINDING_convert_ex(f, r, b, ctx);
  316. CRYPTO_r_unlock(CRYPTO_LOCK_RSA_BLINDING);
  317. return ret;
  318. }
  319. }
  320. static int rsa_blinding_invert(BN_BLINDING *b, int local, BIGNUM *f,
  321. BIGNUM *r, BN_CTX *ctx)
  322. {
  323. if (local)
  324. return BN_BLINDING_invert_ex(f, NULL, b, ctx);
  325. else
  326. {
  327. int ret;
  328. CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
  329. ret = BN_BLINDING_invert_ex(f, r, b, ctx);
  330. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  331. return ret;
  332. }
  333. }
  334. /* signing */
  335. static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
  336. unsigned char *to, RSA *rsa, int padding)
  337. {
  338. BIGNUM *f, *ret, *br, *res;
  339. int i,j,k,num=0,r= -1;
  340. unsigned char *buf=NULL;
  341. BN_CTX *ctx=NULL;
  342. int local_blinding = 0;
  343. BN_BLINDING *blinding = NULL;
  344. #ifdef OPENSSL_FIPS
  345. if(FIPS_selftest_failed())
  346. {
  347. FIPSerr(FIPS_F_RSA_EAY_PRIVATE_ENCRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
  348. goto err;
  349. }
  350. if (FIPS_module_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
  351. && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
  352. {
  353. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
  354. return -1;
  355. }
  356. #endif
  357. if ((ctx=BN_CTX_new()) == NULL) goto err;
  358. BN_CTX_start(ctx);
  359. f = BN_CTX_get(ctx);
  360. br = BN_CTX_get(ctx);
  361. ret = BN_CTX_get(ctx);
  362. num = BN_num_bytes(rsa->n);
  363. buf = OPENSSL_malloc(num);
  364. if(!f || !ret || !buf)
  365. {
  366. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,ERR_R_MALLOC_FAILURE);
  367. goto err;
  368. }
  369. switch (padding)
  370. {
  371. case RSA_PKCS1_PADDING:
  372. i=RSA_padding_add_PKCS1_type_1(buf,num,from,flen);
  373. break;
  374. case RSA_X931_PADDING:
  375. i=RSA_padding_add_X931(buf,num,from,flen);
  376. break;
  377. case RSA_NO_PADDING:
  378. i=RSA_padding_add_none(buf,num,from,flen);
  379. break;
  380. case RSA_SSLV23_PADDING:
  381. default:
  382. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  383. goto err;
  384. }
  385. if (i <= 0) goto err;
  386. if (BN_bin2bn(buf,num,f) == NULL) goto err;
  387. if (BN_ucmp(f, rsa->n) >= 0)
  388. {
  389. /* usually the padding functions would catch this */
  390. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  391. goto err;
  392. }
  393. if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
  394. {
  395. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  396. if (blinding == NULL)
  397. {
  398. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
  399. goto err;
  400. }
  401. }
  402. if (blinding != NULL)
  403. if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
  404. goto err;
  405. if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
  406. ((rsa->p != NULL) &&
  407. (rsa->q != NULL) &&
  408. (rsa->dmp1 != NULL) &&
  409. (rsa->dmq1 != NULL) &&
  410. (rsa->iqmp != NULL)) )
  411. {
  412. if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
  413. }
  414. else
  415. {
  416. BIGNUM local_d;
  417. BIGNUM *d = NULL;
  418. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  419. {
  420. BN_init(&local_d);
  421. d = &local_d;
  422. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  423. }
  424. else
  425. d= rsa->d;
  426. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  427. if(!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  428. goto err;
  429. if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
  430. rsa->_method_mod_n)) goto err;
  431. }
  432. if (blinding)
  433. if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
  434. goto err;
  435. if (padding == RSA_X931_PADDING)
  436. {
  437. BN_sub(f, rsa->n, ret);
  438. if (BN_cmp(ret, f))
  439. res = f;
  440. else
  441. res = ret;
  442. }
  443. else
  444. res = ret;
  445. /* put in leading 0 bytes if the number is less than the
  446. * length of the modulus */
  447. j=BN_num_bytes(res);
  448. i=BN_bn2bin(res,&(to[num-j]));
  449. for (k=0; k<(num-i); k++)
  450. to[k]=0;
  451. r=num;
  452. err:
  453. if (ctx != NULL)
  454. {
  455. BN_CTX_end(ctx);
  456. BN_CTX_free(ctx);
  457. }
  458. if (buf != NULL)
  459. {
  460. OPENSSL_cleanse(buf,num);
  461. OPENSSL_free(buf);
  462. }
  463. return(r);
  464. }
  465. static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
  466. unsigned char *to, RSA *rsa, int padding)
  467. {
  468. BIGNUM *f, *ret, *br;
  469. int j,num=0,r= -1;
  470. unsigned char *p;
  471. unsigned char *buf=NULL;
  472. BN_CTX *ctx=NULL;
  473. int local_blinding = 0;
  474. BN_BLINDING *blinding = NULL;
  475. #ifdef OPENSSL_FIPS
  476. if(FIPS_selftest_failed())
  477. {
  478. FIPSerr(FIPS_F_RSA_EAY_PRIVATE_DECRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
  479. goto err;
  480. }
  481. if (FIPS_module_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
  482. && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
  483. {
  484. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
  485. return -1;
  486. }
  487. #endif
  488. if((ctx = BN_CTX_new()) == NULL) goto err;
  489. BN_CTX_start(ctx);
  490. f = BN_CTX_get(ctx);
  491. br = BN_CTX_get(ctx);
  492. ret = BN_CTX_get(ctx);
  493. num = BN_num_bytes(rsa->n);
  494. buf = OPENSSL_malloc(num);
  495. if(!f || !ret || !buf)
  496. {
  497. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,ERR_R_MALLOC_FAILURE);
  498. goto err;
  499. }
  500. /* This check was for equality but PGP does evil things
  501. * and chops off the top '0' bytes */
  502. if (flen > num)
  503. {
  504. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
  505. goto err;
  506. }
  507. /* make data into a big number */
  508. if (BN_bin2bn(from,(int)flen,f) == NULL) goto err;
  509. if (BN_ucmp(f, rsa->n) >= 0)
  510. {
  511. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  512. goto err;
  513. }
  514. if (!(rsa->flags & RSA_FLAG_NO_BLINDING))
  515. {
  516. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  517. if (blinding == NULL)
  518. {
  519. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
  520. goto err;
  521. }
  522. }
  523. if (blinding != NULL)
  524. if (!rsa_blinding_convert(blinding, local_blinding, f, br, ctx))
  525. goto err;
  526. /* do the decrypt */
  527. if ( (rsa->flags & RSA_FLAG_EXT_PKEY) ||
  528. ((rsa->p != NULL) &&
  529. (rsa->q != NULL) &&
  530. (rsa->dmp1 != NULL) &&
  531. (rsa->dmq1 != NULL) &&
  532. (rsa->iqmp != NULL)) )
  533. {
  534. if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx)) goto err;
  535. }
  536. else
  537. {
  538. BIGNUM local_d;
  539. BIGNUM *d = NULL;
  540. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  541. {
  542. d = &local_d;
  543. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  544. }
  545. else
  546. d = rsa->d;
  547. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  548. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  549. goto err;
  550. if (!rsa->meth->bn_mod_exp(ret,f,d,rsa->n,ctx,
  551. rsa->_method_mod_n))
  552. goto err;
  553. }
  554. if (blinding)
  555. if (!rsa_blinding_invert(blinding, local_blinding, ret, br, ctx))
  556. goto err;
  557. p=buf;
  558. j=BN_bn2bin(ret,p); /* j is only used with no-padding mode */
  559. switch (padding)
  560. {
  561. case RSA_PKCS1_PADDING:
  562. r=RSA_padding_check_PKCS1_type_2(to,num,buf,j,num);
  563. break;
  564. #ifndef OPENSSL_NO_SHA
  565. case RSA_PKCS1_OAEP_PADDING:
  566. r=RSA_padding_check_PKCS1_OAEP(to,num,buf,j,num,NULL,0);
  567. break;
  568. #endif
  569. case RSA_SSLV23_PADDING:
  570. r=RSA_padding_check_SSLv23(to,num,buf,j,num);
  571. break;
  572. case RSA_NO_PADDING:
  573. r=RSA_padding_check_none(to,num,buf,j,num);
  574. break;
  575. default:
  576. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  577. goto err;
  578. }
  579. if (r < 0)
  580. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
  581. err:
  582. if (ctx != NULL)
  583. {
  584. BN_CTX_end(ctx);
  585. BN_CTX_free(ctx);
  586. }
  587. if (buf != NULL)
  588. {
  589. OPENSSL_cleanse(buf,num);
  590. OPENSSL_free(buf);
  591. }
  592. return(r);
  593. }
  594. /* signature verification */
  595. static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
  596. unsigned char *to, RSA *rsa, int padding)
  597. {
  598. BIGNUM *f,*ret;
  599. int i,num=0,r= -1;
  600. unsigned char *p;
  601. unsigned char *buf=NULL;
  602. BN_CTX *ctx=NULL;
  603. #ifdef OPENSSL_FIPS
  604. if(FIPS_selftest_failed())
  605. {
  606. FIPSerr(FIPS_F_RSA_EAY_PUBLIC_DECRYPT,FIPS_R_FIPS_SELFTEST_FAILED);
  607. goto err;
  608. }
  609. if (FIPS_module_mode() && !(rsa->flags & RSA_FLAG_NON_FIPS_ALLOW)
  610. && (BN_num_bits(rsa->n) < OPENSSL_RSA_FIPS_MIN_MODULUS_BITS))
  611. {
  612. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_KEY_SIZE_TOO_SMALL);
  613. return -1;
  614. }
  615. #endif
  616. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS)
  617. {
  618. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
  619. return -1;
  620. }
  621. if (BN_ucmp(rsa->n, rsa->e) <= 0)
  622. {
  623. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
  624. return -1;
  625. }
  626. /* for large moduli, enforce exponent limit */
  627. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS)
  628. {
  629. if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS)
  630. {
  631. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
  632. return -1;
  633. }
  634. }
  635. if((ctx = BN_CTX_new()) == NULL) goto err;
  636. BN_CTX_start(ctx);
  637. f = BN_CTX_get(ctx);
  638. ret = BN_CTX_get(ctx);
  639. num=BN_num_bytes(rsa->n);
  640. buf = OPENSSL_malloc(num);
  641. if(!f || !ret || !buf)
  642. {
  643. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,ERR_R_MALLOC_FAILURE);
  644. goto err;
  645. }
  646. /* This check was for equality but PGP does evil things
  647. * and chops off the top '0' bytes */
  648. if (flen > num)
  649. {
  650. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_GREATER_THAN_MOD_LEN);
  651. goto err;
  652. }
  653. if (BN_bin2bn(from,flen,f) == NULL) goto err;
  654. if (BN_ucmp(f, rsa->n) >= 0)
  655. {
  656. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  657. goto err;
  658. }
  659. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  660. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  661. goto err;
  662. if (!rsa->meth->bn_mod_exp(ret,f,rsa->e,rsa->n,ctx,
  663. rsa->_method_mod_n)) goto err;
  664. if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
  665. if (!BN_sub(ret, rsa->n, ret)) goto err;
  666. p=buf;
  667. i=BN_bn2bin(ret,p);
  668. switch (padding)
  669. {
  670. case RSA_PKCS1_PADDING:
  671. r=RSA_padding_check_PKCS1_type_1(to,num,buf,i,num);
  672. break;
  673. case RSA_X931_PADDING:
  674. r=RSA_padding_check_X931(to,num,buf,i,num);
  675. break;
  676. case RSA_NO_PADDING:
  677. r=RSA_padding_check_none(to,num,buf,i,num);
  678. break;
  679. default:
  680. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_UNKNOWN_PADDING_TYPE);
  681. goto err;
  682. }
  683. if (r < 0)
  684. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,RSA_R_PADDING_CHECK_FAILED);
  685. err:
  686. if (ctx != NULL)
  687. {
  688. BN_CTX_end(ctx);
  689. BN_CTX_free(ctx);
  690. }
  691. if (buf != NULL)
  692. {
  693. OPENSSL_cleanse(buf,num);
  694. OPENSSL_free(buf);
  695. }
  696. return(r);
  697. }
  698. static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  699. {
  700. BIGNUM *r1,*m1,*vrfy;
  701. BIGNUM local_dmp1,local_dmq1,local_c,local_r1;
  702. BIGNUM *dmp1,*dmq1,*c,*pr1;
  703. int ret=0;
  704. BN_CTX_start(ctx);
  705. r1 = BN_CTX_get(ctx);
  706. m1 = BN_CTX_get(ctx);
  707. vrfy = BN_CTX_get(ctx);
  708. {
  709. BIGNUM local_p, local_q;
  710. BIGNUM *p = NULL, *q = NULL;
  711. /* Make sure BN_mod_inverse in Montgomery intialization uses the
  712. * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
  713. */
  714. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  715. {
  716. BN_init(&local_p);
  717. p = &local_p;
  718. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  719. BN_init(&local_q);
  720. q = &local_q;
  721. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  722. }
  723. else
  724. {
  725. p = rsa->p;
  726. q = rsa->q;
  727. }
  728. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE)
  729. {
  730. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx))
  731. goto err;
  732. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx))
  733. goto err;
  734. }
  735. }
  736. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  737. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  738. goto err;
  739. /* compute I mod q */
  740. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  741. {
  742. c = &local_c;
  743. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  744. if (!BN_mod(r1,c,rsa->q,ctx)) goto err;
  745. }
  746. else
  747. {
  748. if (!BN_mod(r1,I,rsa->q,ctx)) goto err;
  749. }
  750. /* compute r1^dmq1 mod q */
  751. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  752. {
  753. dmq1 = &local_dmq1;
  754. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  755. }
  756. else
  757. dmq1 = rsa->dmq1;
  758. if (!rsa->meth->bn_mod_exp(m1,r1,dmq1,rsa->q,ctx,
  759. rsa->_method_mod_q)) goto err;
  760. /* compute I mod p */
  761. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  762. {
  763. c = &local_c;
  764. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  765. if (!BN_mod(r1,c,rsa->p,ctx)) goto err;
  766. }
  767. else
  768. {
  769. if (!BN_mod(r1,I,rsa->p,ctx)) goto err;
  770. }
  771. /* compute r1^dmp1 mod p */
  772. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  773. {
  774. dmp1 = &local_dmp1;
  775. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  776. }
  777. else
  778. dmp1 = rsa->dmp1;
  779. if (!rsa->meth->bn_mod_exp(r0,r1,dmp1,rsa->p,ctx,
  780. rsa->_method_mod_p)) goto err;
  781. if (!BN_sub(r0,r0,m1)) goto err;
  782. /* This will help stop the size of r0 increasing, which does
  783. * affect the multiply if it optimised for a power of 2 size */
  784. if (BN_is_negative(r0))
  785. if (!BN_add(r0,r0,rsa->p)) goto err;
  786. if (!BN_mul(r1,r0,rsa->iqmp,ctx)) goto err;
  787. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  788. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  789. {
  790. pr1 = &local_r1;
  791. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  792. }
  793. else
  794. pr1 = r1;
  795. if (!BN_mod(r0,pr1,rsa->p,ctx)) goto err;
  796. /* If p < q it is occasionally possible for the correction of
  797. * adding 'p' if r0 is negative above to leave the result still
  798. * negative. This can break the private key operations: the following
  799. * second correction should *always* correct this rare occurrence.
  800. * This will *never* happen with OpenSSL generated keys because
  801. * they ensure p > q [steve]
  802. */
  803. if (BN_is_negative(r0))
  804. if (!BN_add(r0,r0,rsa->p)) goto err;
  805. if (!BN_mul(r1,r0,rsa->q,ctx)) goto err;
  806. if (!BN_add(r0,r1,m1)) goto err;
  807. if (rsa->e && rsa->n)
  808. {
  809. if (!rsa->meth->bn_mod_exp(vrfy,r0,rsa->e,rsa->n,ctx,rsa->_method_mod_n)) goto err;
  810. /* If 'I' was greater than (or equal to) rsa->n, the operation
  811. * will be equivalent to using 'I mod n'. However, the result of
  812. * the verify will *always* be less than 'n' so we don't check
  813. * for absolute equality, just congruency. */
  814. if (!BN_sub(vrfy, vrfy, I)) goto err;
  815. if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) goto err;
  816. if (BN_is_negative(vrfy))
  817. if (!BN_add(vrfy, vrfy, rsa->n)) goto err;
  818. if (!BN_is_zero(vrfy))
  819. {
  820. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  821. * miscalculated CRT output, just do a raw (slower)
  822. * mod_exp and return that instead. */
  823. BIGNUM local_d;
  824. BIGNUM *d = NULL;
  825. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME))
  826. {
  827. d = &local_d;
  828. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  829. }
  830. else
  831. d = rsa->d;
  832. if (!rsa->meth->bn_mod_exp(r0,I,d,rsa->n,ctx,
  833. rsa->_method_mod_n)) goto err;
  834. }
  835. }
  836. ret=1;
  837. err:
  838. BN_CTX_end(ctx);
  839. return(ret);
  840. }
  841. static int RSA_eay_init(RSA *rsa)
  842. {
  843. #ifdef OPENSSL_FIPS
  844. if(FIPS_selftest_failed())
  845. {
  846. FIPSerr(FIPS_F_RSA_EAY_INIT,FIPS_R_FIPS_SELFTEST_FAILED);
  847. return 0;
  848. }
  849. #endif
  850. rsa->flags|=RSA_FLAG_CACHE_PUBLIC|RSA_FLAG_CACHE_PRIVATE;
  851. return(1);
  852. }
  853. static int RSA_eay_finish(RSA *rsa)
  854. {
  855. if (rsa->_method_mod_n != NULL)
  856. BN_MONT_CTX_free(rsa->_method_mod_n);
  857. if (rsa->_method_mod_p != NULL)
  858. BN_MONT_CTX_free(rsa->_method_mod_p);
  859. if (rsa->_method_mod_q != NULL)
  860. BN_MONT_CTX_free(rsa->_method_mod_q);
  861. return(1);
  862. }
  863. #endif