rsa_eay.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904
  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. #include <stdio.h>
  112. #include "cryptlib.h"
  113. #include <openssl/bn.h>
  114. #include <openssl/rsa.h>
  115. #include <openssl/rand.h>
  116. #ifndef RSA_NULL
  117. static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
  118. unsigned char *to, RSA *rsa, int padding);
  119. static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
  120. unsigned char *to, RSA *rsa, int padding);
  121. static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
  122. unsigned char *to, RSA *rsa, int padding);
  123. static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
  124. unsigned char *to, RSA *rsa, int padding);
  125. static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *i, RSA *rsa,
  126. BN_CTX *ctx);
  127. static int RSA_eay_init(RSA *rsa);
  128. static int RSA_eay_finish(RSA *rsa);
  129. static RSA_METHOD rsa_pkcs1_eay_meth = {
  130. "Eric Young's PKCS#1 RSA",
  131. RSA_eay_public_encrypt,
  132. RSA_eay_public_decrypt, /* signature verification */
  133. RSA_eay_private_encrypt, /* signing */
  134. RSA_eay_private_decrypt,
  135. RSA_eay_mod_exp,
  136. BN_mod_exp_mont, /* XXX probably we should not use Montgomery
  137. * if e == 3 */
  138. RSA_eay_init,
  139. RSA_eay_finish,
  140. 0, /* flags */
  141. NULL,
  142. 0, /* rsa_sign */
  143. 0, /* rsa_verify */
  144. NULL /* rsa_keygen */
  145. };
  146. const RSA_METHOD *RSA_PKCS1_SSLeay(void)
  147. {
  148. return (&rsa_pkcs1_eay_meth);
  149. }
  150. static int RSA_eay_public_encrypt(int flen, const unsigned char *from,
  151. unsigned char *to, RSA *rsa, int padding)
  152. {
  153. BIGNUM *f, *ret;
  154. int i, j, k, num = 0, r = -1;
  155. unsigned char *buf = NULL;
  156. BN_CTX *ctx = NULL;
  157. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  158. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_MODULUS_TOO_LARGE);
  159. return -1;
  160. }
  161. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  162. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
  163. return -1;
  164. }
  165. /* for large moduli, enforce exponent limit */
  166. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
  167. if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  168. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_BAD_E_VALUE);
  169. return -1;
  170. }
  171. }
  172. if ((ctx = BN_CTX_new()) == NULL)
  173. goto err;
  174. BN_CTX_start(ctx);
  175. f = BN_CTX_get(ctx);
  176. ret = BN_CTX_get(ctx);
  177. num = BN_num_bytes(rsa->n);
  178. buf = OPENSSL_malloc(num);
  179. if (!f || !ret || !buf) {
  180. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, ERR_R_MALLOC_FAILURE);
  181. goto err;
  182. }
  183. switch (padding) {
  184. case RSA_PKCS1_PADDING:
  185. i = RSA_padding_add_PKCS1_type_2(buf, num, from, flen);
  186. break;
  187. # ifndef OPENSSL_NO_SHA
  188. case RSA_PKCS1_OAEP_PADDING:
  189. i = RSA_padding_add_PKCS1_OAEP(buf, num, from, flen, NULL, 0);
  190. break;
  191. # endif
  192. case RSA_SSLV23_PADDING:
  193. i = RSA_padding_add_SSLv23(buf, num, from, flen);
  194. break;
  195. case RSA_NO_PADDING:
  196. i = RSA_padding_add_none(buf, num, from, flen);
  197. break;
  198. default:
  199. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  200. goto err;
  201. }
  202. if (i <= 0)
  203. goto err;
  204. if (BN_bin2bn(buf, num, f) == NULL)
  205. goto err;
  206. if (BN_ucmp(f, rsa->n) >= 0) {
  207. /* usually the padding functions would catch this */
  208. RSAerr(RSA_F_RSA_EAY_PUBLIC_ENCRYPT,
  209. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  210. goto err;
  211. }
  212. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  213. if (!BN_MONT_CTX_set_locked
  214. (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  215. goto err;
  216. if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
  217. rsa->_method_mod_n))
  218. goto err;
  219. /*
  220. * put in leading 0 bytes if the number is less than the length of the
  221. * modulus
  222. */
  223. j = BN_num_bytes(ret);
  224. i = BN_bn2bin(ret, &(to[num - j]));
  225. for (k = 0; k < (num - i); k++)
  226. to[k] = 0;
  227. r = num;
  228. err:
  229. if (ctx != NULL) {
  230. BN_CTX_end(ctx);
  231. BN_CTX_free(ctx);
  232. }
  233. if (buf != NULL) {
  234. OPENSSL_cleanse(buf, num);
  235. OPENSSL_free(buf);
  236. }
  237. return (r);
  238. }
  239. static BN_BLINDING *rsa_get_blinding(RSA *rsa, int *local, BN_CTX *ctx)
  240. {
  241. BN_BLINDING *ret;
  242. int got_write_lock = 0;
  243. CRYPTO_THREADID cur;
  244. CRYPTO_r_lock(CRYPTO_LOCK_RSA);
  245. if (rsa->blinding == NULL) {
  246. CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
  247. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  248. got_write_lock = 1;
  249. if (rsa->blinding == NULL)
  250. rsa->blinding = RSA_setup_blinding(rsa, ctx);
  251. }
  252. ret = rsa->blinding;
  253. if (ret == NULL)
  254. goto err;
  255. CRYPTO_THREADID_current(&cur);
  256. if (!CRYPTO_THREADID_cmp(&cur, BN_BLINDING_thread_id(ret))) {
  257. /* rsa->blinding is ours! */
  258. *local = 1;
  259. } else {
  260. /* resort to rsa->mt_blinding instead */
  261. /*
  262. * instructs rsa_blinding_convert(), rsa_blinding_invert() that the
  263. * BN_BLINDING is shared, meaning that accesses require locks, and
  264. * that the blinding factor must be stored outside the BN_BLINDING
  265. */
  266. *local = 0;
  267. if (rsa->mt_blinding == NULL) {
  268. if (!got_write_lock) {
  269. CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
  270. CRYPTO_w_lock(CRYPTO_LOCK_RSA);
  271. got_write_lock = 1;
  272. }
  273. if (rsa->mt_blinding == NULL)
  274. rsa->mt_blinding = RSA_setup_blinding(rsa, ctx);
  275. }
  276. ret = rsa->mt_blinding;
  277. }
  278. err:
  279. if (got_write_lock)
  280. CRYPTO_w_unlock(CRYPTO_LOCK_RSA);
  281. else
  282. CRYPTO_r_unlock(CRYPTO_LOCK_RSA);
  283. return ret;
  284. }
  285. static int rsa_blinding_convert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
  286. BN_CTX *ctx)
  287. {
  288. if (unblind == NULL)
  289. /*
  290. * Local blinding: store the unblinding factor in BN_BLINDING.
  291. */
  292. return BN_BLINDING_convert_ex(f, NULL, b, ctx);
  293. else {
  294. /*
  295. * Shared blinding: store the unblinding factor outside BN_BLINDING.
  296. */
  297. int ret;
  298. CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
  299. ret = BN_BLINDING_convert_ex(f, unblind, b, ctx);
  300. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  301. return ret;
  302. }
  303. }
  304. static int rsa_blinding_invert(BN_BLINDING *b, BIGNUM *f, BIGNUM *unblind,
  305. BN_CTX *ctx)
  306. {
  307. /*
  308. * For local blinding, unblind is set to NULL, and BN_BLINDING_invert_ex
  309. * will use the unblinding factor stored in BN_BLINDING. If BN_BLINDING
  310. * is shared between threads, unblind must be non-null:
  311. * BN_BLINDING_invert_ex will then use the local unblinding factor, and
  312. * will only read the modulus from BN_BLINDING. In both cases it's safe
  313. * to access the blinding without a lock.
  314. */
  315. return BN_BLINDING_invert_ex(f, unblind, b, ctx);
  316. }
  317. /* signing */
  318. static int RSA_eay_private_encrypt(int flen, const unsigned char *from,
  319. unsigned char *to, RSA *rsa, int padding)
  320. {
  321. BIGNUM *f, *ret, *res;
  322. int i, j, k, num = 0, r = -1;
  323. unsigned char *buf = NULL;
  324. BN_CTX *ctx = NULL;
  325. int local_blinding = 0;
  326. /*
  327. * Used only if the blinding structure is shared. A non-NULL unblind
  328. * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
  329. * the unblinding factor outside the blinding structure.
  330. */
  331. BIGNUM *unblind = NULL;
  332. BN_BLINDING *blinding = NULL;
  333. if ((ctx = BN_CTX_new()) == NULL)
  334. goto err;
  335. BN_CTX_start(ctx);
  336. f = BN_CTX_get(ctx);
  337. ret = BN_CTX_get(ctx);
  338. num = BN_num_bytes(rsa->n);
  339. buf = OPENSSL_malloc(num);
  340. if (!f || !ret || !buf) {
  341. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  342. goto err;
  343. }
  344. switch (padding) {
  345. case RSA_PKCS1_PADDING:
  346. i = RSA_padding_add_PKCS1_type_1(buf, num, from, flen);
  347. break;
  348. case RSA_X931_PADDING:
  349. i = RSA_padding_add_X931(buf, num, from, flen);
  350. break;
  351. case RSA_NO_PADDING:
  352. i = RSA_padding_add_none(buf, num, from, flen);
  353. break;
  354. case RSA_SSLV23_PADDING:
  355. default:
  356. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  357. goto err;
  358. }
  359. if (i <= 0)
  360. goto err;
  361. if (BN_bin2bn(buf, num, f) == NULL)
  362. goto err;
  363. if (BN_ucmp(f, rsa->n) >= 0) {
  364. /* usually the padding functions would catch this */
  365. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT,
  366. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  367. goto err;
  368. }
  369. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  370. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  371. if (blinding == NULL) {
  372. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_INTERNAL_ERROR);
  373. goto err;
  374. }
  375. }
  376. if (blinding != NULL) {
  377. if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
  378. RSAerr(RSA_F_RSA_EAY_PRIVATE_ENCRYPT, ERR_R_MALLOC_FAILURE);
  379. goto err;
  380. }
  381. if (!rsa_blinding_convert(blinding, f, unblind, ctx))
  382. goto err;
  383. }
  384. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  385. ((rsa->p != NULL) &&
  386. (rsa->q != NULL) &&
  387. (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  388. if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
  389. goto err;
  390. } else {
  391. BIGNUM local_d;
  392. BIGNUM *d = NULL;
  393. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  394. BN_init(&local_d);
  395. d = &local_d;
  396. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  397. } else
  398. d = rsa->d;
  399. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  400. if (!BN_MONT_CTX_set_locked
  401. (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  402. goto err;
  403. if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
  404. rsa->_method_mod_n))
  405. goto err;
  406. }
  407. if (blinding)
  408. if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
  409. goto err;
  410. if (padding == RSA_X931_PADDING) {
  411. BN_sub(f, rsa->n, ret);
  412. if (BN_cmp(ret, f) > 0)
  413. res = f;
  414. else
  415. res = ret;
  416. } else
  417. res = ret;
  418. /*
  419. * put in leading 0 bytes if the number is less than the length of the
  420. * modulus
  421. */
  422. j = BN_num_bytes(res);
  423. i = BN_bn2bin(res, &(to[num - j]));
  424. for (k = 0; k < (num - i); k++)
  425. to[k] = 0;
  426. r = num;
  427. err:
  428. if (ctx != NULL) {
  429. BN_CTX_end(ctx);
  430. BN_CTX_free(ctx);
  431. }
  432. if (buf != NULL) {
  433. OPENSSL_cleanse(buf, num);
  434. OPENSSL_free(buf);
  435. }
  436. return (r);
  437. }
  438. static int RSA_eay_private_decrypt(int flen, const unsigned char *from,
  439. unsigned char *to, RSA *rsa, int padding)
  440. {
  441. BIGNUM *f, *ret;
  442. int j, num = 0, r = -1;
  443. unsigned char *p;
  444. unsigned char *buf = NULL;
  445. BN_CTX *ctx = NULL;
  446. int local_blinding = 0;
  447. /*
  448. * Used only if the blinding structure is shared. A non-NULL unblind
  449. * instructs rsa_blinding_convert() and rsa_blinding_invert() to store
  450. * the unblinding factor outside the blinding structure.
  451. */
  452. BIGNUM *unblind = NULL;
  453. BN_BLINDING *blinding = NULL;
  454. if ((ctx = BN_CTX_new()) == NULL)
  455. goto err;
  456. BN_CTX_start(ctx);
  457. f = BN_CTX_get(ctx);
  458. ret = BN_CTX_get(ctx);
  459. num = BN_num_bytes(rsa->n);
  460. buf = OPENSSL_malloc(num);
  461. if (!f || !ret || !buf) {
  462. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
  463. goto err;
  464. }
  465. /*
  466. * This check was for equality but PGP does evil things and chops off the
  467. * top '0' bytes
  468. */
  469. if (flen > num) {
  470. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
  471. RSA_R_DATA_GREATER_THAN_MOD_LEN);
  472. goto err;
  473. }
  474. /* make data into a big number */
  475. if (BN_bin2bn(from, (int)flen, f) == NULL)
  476. goto err;
  477. if (BN_ucmp(f, rsa->n) >= 0) {
  478. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT,
  479. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  480. goto err;
  481. }
  482. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  483. blinding = rsa_get_blinding(rsa, &local_blinding, ctx);
  484. if (blinding == NULL) {
  485. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_INTERNAL_ERROR);
  486. goto err;
  487. }
  488. }
  489. if (blinding != NULL) {
  490. if (!local_blinding && ((unblind = BN_CTX_get(ctx)) == NULL)) {
  491. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, ERR_R_MALLOC_FAILURE);
  492. goto err;
  493. }
  494. if (!rsa_blinding_convert(blinding, f, unblind, ctx))
  495. goto err;
  496. }
  497. /* do the decrypt */
  498. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  499. ((rsa->p != NULL) &&
  500. (rsa->q != NULL) &&
  501. (rsa->dmp1 != NULL) && (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  502. if (!rsa->meth->rsa_mod_exp(ret, f, rsa, ctx))
  503. goto err;
  504. } else {
  505. BIGNUM local_d;
  506. BIGNUM *d = NULL;
  507. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  508. d = &local_d;
  509. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  510. } else
  511. d = rsa->d;
  512. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  513. if (!BN_MONT_CTX_set_locked
  514. (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  515. goto err;
  516. if (!rsa->meth->bn_mod_exp(ret, f, d, rsa->n, ctx,
  517. rsa->_method_mod_n))
  518. goto err;
  519. }
  520. if (blinding)
  521. if (!rsa_blinding_invert(blinding, ret, unblind, ctx))
  522. goto err;
  523. p = buf;
  524. j = BN_bn2bin(ret, p); /* j is only used with no-padding mode */
  525. switch (padding) {
  526. case RSA_PKCS1_PADDING:
  527. r = RSA_padding_check_PKCS1_type_2(to, num, buf, j, num);
  528. break;
  529. # ifndef OPENSSL_NO_SHA
  530. case RSA_PKCS1_OAEP_PADDING:
  531. r = RSA_padding_check_PKCS1_OAEP(to, num, buf, j, num, NULL, 0);
  532. break;
  533. # endif
  534. case RSA_SSLV23_PADDING:
  535. r = RSA_padding_check_SSLv23(to, num, buf, j, num);
  536. break;
  537. case RSA_NO_PADDING:
  538. r = RSA_padding_check_none(to, num, buf, j, num);
  539. break;
  540. default:
  541. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  542. goto err;
  543. }
  544. if (r < 0)
  545. RSAerr(RSA_F_RSA_EAY_PRIVATE_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
  546. err:
  547. if (ctx != NULL) {
  548. BN_CTX_end(ctx);
  549. BN_CTX_free(ctx);
  550. }
  551. if (buf != NULL) {
  552. OPENSSL_cleanse(buf, num);
  553. OPENSSL_free(buf);
  554. }
  555. return (r);
  556. }
  557. /* signature verification */
  558. static int RSA_eay_public_decrypt(int flen, const unsigned char *from,
  559. unsigned char *to, RSA *rsa, int padding)
  560. {
  561. BIGNUM *f, *ret;
  562. int i, num = 0, r = -1;
  563. unsigned char *p;
  564. unsigned char *buf = NULL;
  565. BN_CTX *ctx = NULL;
  566. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  567. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_MODULUS_TOO_LARGE);
  568. return -1;
  569. }
  570. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  571. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
  572. return -1;
  573. }
  574. /* for large moduli, enforce exponent limit */
  575. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS) {
  576. if (BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  577. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_BAD_E_VALUE);
  578. return -1;
  579. }
  580. }
  581. if ((ctx = BN_CTX_new()) == NULL)
  582. goto err;
  583. BN_CTX_start(ctx);
  584. f = BN_CTX_get(ctx);
  585. ret = BN_CTX_get(ctx);
  586. num = BN_num_bytes(rsa->n);
  587. buf = OPENSSL_malloc(num);
  588. if (!f || !ret || !buf) {
  589. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, ERR_R_MALLOC_FAILURE);
  590. goto err;
  591. }
  592. /*
  593. * This check was for equality but PGP does evil things and chops off the
  594. * top '0' bytes
  595. */
  596. if (flen > num) {
  597. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_DATA_GREATER_THAN_MOD_LEN);
  598. goto err;
  599. }
  600. if (BN_bin2bn(from, flen, f) == NULL)
  601. goto err;
  602. if (BN_ucmp(f, rsa->n) >= 0) {
  603. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT,
  604. RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  605. goto err;
  606. }
  607. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  608. if (!BN_MONT_CTX_set_locked
  609. (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  610. goto err;
  611. if (!rsa->meth->bn_mod_exp(ret, f, rsa->e, rsa->n, ctx,
  612. rsa->_method_mod_n))
  613. goto err;
  614. if ((padding == RSA_X931_PADDING) && ((ret->d[0] & 0xf) != 12))
  615. if (!BN_sub(ret, rsa->n, ret))
  616. goto err;
  617. p = buf;
  618. i = BN_bn2bin(ret, p);
  619. switch (padding) {
  620. case RSA_PKCS1_PADDING:
  621. r = RSA_padding_check_PKCS1_type_1(to, num, buf, i, num);
  622. break;
  623. case RSA_X931_PADDING:
  624. r = RSA_padding_check_X931(to, num, buf, i, num);
  625. break;
  626. case RSA_NO_PADDING:
  627. r = RSA_padding_check_none(to, num, buf, i, num);
  628. break;
  629. default:
  630. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_UNKNOWN_PADDING_TYPE);
  631. goto err;
  632. }
  633. if (r < 0)
  634. RSAerr(RSA_F_RSA_EAY_PUBLIC_DECRYPT, RSA_R_PADDING_CHECK_FAILED);
  635. err:
  636. if (ctx != NULL) {
  637. BN_CTX_end(ctx);
  638. BN_CTX_free(ctx);
  639. }
  640. if (buf != NULL) {
  641. OPENSSL_cleanse(buf, num);
  642. OPENSSL_free(buf);
  643. }
  644. return (r);
  645. }
  646. static int RSA_eay_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
  647. {
  648. BIGNUM *r1, *m1, *vrfy;
  649. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  650. BIGNUM *dmp1, *dmq1, *c, *pr1;
  651. int ret = 0;
  652. BN_CTX_start(ctx);
  653. r1 = BN_CTX_get(ctx);
  654. m1 = BN_CTX_get(ctx);
  655. vrfy = BN_CTX_get(ctx);
  656. {
  657. BIGNUM local_p, local_q;
  658. BIGNUM *p = NULL, *q = NULL;
  659. /*
  660. * Make sure BN_mod_inverse in Montgomery intialization uses the
  661. * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set)
  662. */
  663. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  664. BN_init(&local_p);
  665. p = &local_p;
  666. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  667. BN_init(&local_q);
  668. q = &local_q;
  669. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  670. } else {
  671. p = rsa->p;
  672. q = rsa->q;
  673. }
  674. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  675. if (!BN_MONT_CTX_set_locked
  676. (&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx))
  677. goto err;
  678. if (!BN_MONT_CTX_set_locked
  679. (&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx))
  680. goto err;
  681. }
  682. }
  683. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC)
  684. if (!BN_MONT_CTX_set_locked
  685. (&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n, ctx))
  686. goto err;
  687. /* compute I mod q */
  688. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  689. c = &local_c;
  690. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  691. if (!BN_mod(r1, c, rsa->q, ctx))
  692. goto err;
  693. } else {
  694. if (!BN_mod(r1, I, rsa->q, ctx))
  695. goto err;
  696. }
  697. /* compute r1^dmq1 mod q */
  698. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  699. dmq1 = &local_dmq1;
  700. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  701. } else
  702. dmq1 = rsa->dmq1;
  703. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q))
  704. goto err;
  705. /* compute I mod p */
  706. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  707. c = &local_c;
  708. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  709. if (!BN_mod(r1, c, rsa->p, ctx))
  710. goto err;
  711. } else {
  712. if (!BN_mod(r1, I, rsa->p, ctx))
  713. goto err;
  714. }
  715. /* compute r1^dmp1 mod p */
  716. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  717. dmp1 = &local_dmp1;
  718. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  719. } else
  720. dmp1 = rsa->dmp1;
  721. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p))
  722. goto err;
  723. if (!BN_sub(r0, r0, m1))
  724. goto err;
  725. /*
  726. * This will help stop the size of r0 increasing, which does affect the
  727. * multiply if it optimised for a power of 2 size
  728. */
  729. if (BN_is_negative(r0))
  730. if (!BN_add(r0, r0, rsa->p))
  731. goto err;
  732. if (!BN_mul(r1, r0, rsa->iqmp, ctx))
  733. goto err;
  734. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  735. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  736. pr1 = &local_r1;
  737. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  738. } else
  739. pr1 = r1;
  740. if (!BN_mod(r0, pr1, rsa->p, ctx))
  741. goto err;
  742. /*
  743. * If p < q it is occasionally possible for the correction of adding 'p'
  744. * if r0 is negative above to leave the result still negative. This can
  745. * break the private key operations: the following second correction
  746. * should *always* correct this rare occurrence. This will *never* happen
  747. * with OpenSSL generated keys because they ensure p > q [steve]
  748. */
  749. if (BN_is_negative(r0))
  750. if (!BN_add(r0, r0, rsa->p))
  751. goto err;
  752. if (!BN_mul(r1, r0, rsa->q, ctx))
  753. goto err;
  754. if (!BN_add(r0, r1, m1))
  755. goto err;
  756. if (rsa->e && rsa->n) {
  757. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  758. rsa->_method_mod_n))
  759. goto err;
  760. /*
  761. * If 'I' was greater than (or equal to) rsa->n, the operation will
  762. * be equivalent to using 'I mod n'. However, the result of the
  763. * verify will *always* be less than 'n' so we don't check for
  764. * absolute equality, just congruency.
  765. */
  766. if (!BN_sub(vrfy, vrfy, I))
  767. goto err;
  768. if (!BN_mod(vrfy, vrfy, rsa->n, ctx))
  769. goto err;
  770. if (BN_is_negative(vrfy))
  771. if (!BN_add(vrfy, vrfy, rsa->n))
  772. goto err;
  773. if (!BN_is_zero(vrfy)) {
  774. /*
  775. * 'I' and 'vrfy' aren't congruent mod n. Don't leak
  776. * miscalculated CRT output, just do a raw (slower) mod_exp and
  777. * return that instead.
  778. */
  779. BIGNUM local_d;
  780. BIGNUM *d = NULL;
  781. if (!(rsa->flags & RSA_FLAG_NO_CONSTTIME)) {
  782. d = &local_d;
  783. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  784. } else
  785. d = rsa->d;
  786. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx,
  787. rsa->_method_mod_n))
  788. goto err;
  789. }
  790. }
  791. ret = 1;
  792. err:
  793. BN_CTX_end(ctx);
  794. return (ret);
  795. }
  796. static int RSA_eay_init(RSA *rsa)
  797. {
  798. rsa->flags |= RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE;
  799. return (1);
  800. }
  801. static int RSA_eay_finish(RSA *rsa)
  802. {
  803. if (rsa->_method_mod_n != NULL)
  804. BN_MONT_CTX_free(rsa->_method_mod_n);
  805. if (rsa->_method_mod_p != NULL)
  806. BN_MONT_CTX_free(rsa->_method_mod_p);
  807. if (rsa->_method_mod_q != NULL)
  808. BN_MONT_CTX_free(rsa->_method_mod_q);
  809. return (1);
  810. }
  811. #endif