rsa_eay.c 31 KB

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