You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1010 lines
27 KiB

  1. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  2. * All rights reserved.
  3. *
  4. * This package is an SSL implementation written
  5. * by Eric Young (eay@cryptsoft.com).
  6. * The implementation was written so as to conform with Netscapes SSL.
  7. *
  8. * This library is free for commercial and non-commercial use as long as
  9. * the following conditions are aheared to. The following conditions
  10. * apply to all code found in this distribution, be it the RC4, RSA,
  11. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  12. * included with this distribution is covered by the same copyright terms
  13. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  14. *
  15. * Copyright remains Eric Young's, and as such any Copyright notices in
  16. * the code are not to be removed.
  17. * If this package is used in a product, Eric Young should be given attribution
  18. * as the author of the parts of the library used.
  19. * This can be in the form of a textual message at program startup or
  20. * in documentation (online or textual) provided with the package.
  21. *
  22. * Redistribution and use in source and binary forms, with or without
  23. * modification, are permitted provided that the following conditions
  24. * are met:
  25. * 1. Redistributions of source code must retain the copyright
  26. * notice, this list of conditions and the following disclaimer.
  27. * 2. Redistributions in binary form must reproduce the above copyright
  28. * notice, this list of conditions and the following disclaimer in the
  29. * documentation and/or other materials provided with the distribution.
  30. * 3. All advertising materials mentioning features or use of this software
  31. * must display the following acknowledgement:
  32. * "This product includes cryptographic software written by
  33. * Eric Young (eay@cryptsoft.com)"
  34. * The word 'cryptographic' can be left out if the rouines from the library
  35. * being used are not cryptographic related :-).
  36. * 4. If you include any Windows specific code (or a derivative thereof) from
  37. * the apps directory (application code) you must include an acknowledgement:
  38. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  39. *
  40. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  41. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  42. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  43. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  44. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  45. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  46. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  48. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  49. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  50. * SUCH DAMAGE.
  51. *
  52. * The licence and distribution terms for any publically available version or
  53. * derivative of this code cannot be changed. i.e. this code cannot simply be
  54. * copied and put under another distribution licence
  55. * [including the GNU Public Licence.] */
  56. #include <openssl/rsa.h>
  57. #include <openssl/bn.h>
  58. #include <openssl/err.h>
  59. #include <openssl/mem.h>
  60. #include "internal.h"
  61. #define OPENSSL_RSA_MAX_MODULUS_BITS 16384
  62. #define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
  63. #define OPENSSL_RSA_MAX_PUBEXP_BITS \
  64. 64 /* exponent limit enforced for "large" modulus only */
  65. static int finish(RSA *rsa) {
  66. if (rsa->_method_mod_n != NULL) {
  67. BN_MONT_CTX_free(rsa->_method_mod_n);
  68. }
  69. if (rsa->_method_mod_p != NULL) {
  70. BN_MONT_CTX_free(rsa->_method_mod_p);
  71. }
  72. if (rsa->_method_mod_q != NULL) {
  73. BN_MONT_CTX_free(rsa->_method_mod_q);
  74. }
  75. return 1;
  76. }
  77. static int encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  78. const uint8_t *in, size_t in_len, int padding) {
  79. const unsigned rsa_size = RSA_size(rsa);
  80. BIGNUM *f, *result;
  81. uint8_t *buf = NULL;
  82. BN_CTX *ctx = NULL;
  83. int i, ret = 0;
  84. if (rsa_size > OPENSSL_RSA_MAX_MODULUS_BITS) {
  85. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_MODULUS_TOO_LARGE);
  86. return 0;
  87. }
  88. if (max_out < rsa_size) {
  89. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  90. return 0;
  91. }
  92. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  93. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
  94. return 0;
  95. }
  96. /* for large moduli, enforce exponent limit */
  97. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  98. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  99. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
  100. return 0;
  101. }
  102. ctx = BN_CTX_new();
  103. if (ctx == NULL) {
  104. goto err;
  105. }
  106. BN_CTX_start(ctx);
  107. f = BN_CTX_get(ctx);
  108. result = BN_CTX_get(ctx);
  109. buf = OPENSSL_malloc(rsa_size);
  110. if (!f || !result || !buf) {
  111. OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_MALLOC_FAILURE);
  112. goto err;
  113. }
  114. switch (padding) {
  115. case RSA_PKCS1_PADDING:
  116. i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
  117. break;
  118. case RSA_PKCS1_OAEP_PADDING:
  119. /* Use the default parameters: SHA-1 for both hashes and no label. */
  120. i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
  121. NULL, 0, NULL, NULL);
  122. break;
  123. case RSA_SSLV23_PADDING:
  124. i = RSA_padding_add_SSLv23(buf, rsa_size, in, in_len);
  125. break;
  126. case RSA_NO_PADDING:
  127. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  128. break;
  129. default:
  130. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_UNKNOWN_PADDING_TYPE);
  131. goto err;
  132. }
  133. if (i <= 0) {
  134. goto err;
  135. }
  136. if (BN_bin2bn(buf, rsa_size, f) == NULL) {
  137. goto err;
  138. }
  139. if (BN_ucmp(f, rsa->n) >= 0) {
  140. /* usually the padding functions would catch this */
  141. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  142. goto err;
  143. }
  144. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  145. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  146. ctx)) {
  147. goto err;
  148. }
  149. }
  150. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx, rsa->_method_mod_n)) {
  151. goto err;
  152. }
  153. /* put in leading 0 bytes if the number is less than the length of the
  154. * modulus */
  155. if (!BN_bn2bin_padded(out, rsa_size, result)) {
  156. OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_INTERNAL_ERROR);
  157. goto err;
  158. }
  159. *out_len = rsa_size;
  160. ret = 1;
  161. err:
  162. if (ctx != NULL) {
  163. BN_CTX_end(ctx);
  164. BN_CTX_free(ctx);
  165. }
  166. if (buf != NULL) {
  167. OPENSSL_cleanse(buf, rsa_size);
  168. OPENSSL_free(buf);
  169. }
  170. return ret;
  171. }
  172. /* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
  173. * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
  174. * destroyed as needed. */
  175. #define MAX_BLINDINGS_PER_RSA 1024
  176. /* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
  177. * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
  178. * none are free, the cache will be extended by a extra element and the new
  179. * BN_BLINDING is returned.
  180. *
  181. * On success, the index of the assigned BN_BLINDING is written to
  182. * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
  183. static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
  184. BN_CTX *ctx) {
  185. BN_BLINDING *ret = NULL;
  186. BN_BLINDING **new_blindings;
  187. uint8_t *new_blindings_inuse;
  188. char overflow = 0;
  189. CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
  190. if (rsa->num_blindings > 0) {
  191. unsigned i, starting_index;
  192. CRYPTO_THREADID threadid;
  193. /* We start searching the array at a value based on the
  194. * threadid in order to try avoid bouncing the BN_BLINDING
  195. * values around different threads. It's harmless if
  196. * threadid.val is always set to zero. */
  197. CRYPTO_THREADID_current(&threadid);
  198. starting_index = threadid.val % rsa->num_blindings;
  199. for (i = starting_index;;) {
  200. if (rsa->blindings_inuse[i] == 0) {
  201. rsa->blindings_inuse[i] = 1;
  202. ret = rsa->blindings[i];
  203. *index_used = i;
  204. break;
  205. }
  206. i++;
  207. if (i == rsa->num_blindings) {
  208. i = 0;
  209. }
  210. if (i == starting_index) {
  211. break;
  212. }
  213. }
  214. }
  215. if (ret != NULL) {
  216. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  217. return ret;
  218. }
  219. overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
  220. /* We didn't find a free BN_BLINDING to use so increase the length of
  221. * the arrays by one and use the newly created element. */
  222. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  223. ret = rsa_setup_blinding(rsa, ctx);
  224. if (ret == NULL) {
  225. return NULL;
  226. }
  227. if (overflow) {
  228. /* We cannot add any more cached BN_BLINDINGs so we use |ret|
  229. * and mark it for destruction in |rsa_blinding_release|. */
  230. *index_used = MAX_BLINDINGS_PER_RSA;
  231. return ret;
  232. }
  233. CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
  234. new_blindings =
  235. OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
  236. if (new_blindings == NULL) {
  237. goto err1;
  238. }
  239. memcpy(new_blindings, rsa->blindings,
  240. sizeof(BN_BLINDING *) * rsa->num_blindings);
  241. new_blindings[rsa->num_blindings] = ret;
  242. new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
  243. if (new_blindings_inuse == NULL) {
  244. goto err2;
  245. }
  246. memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
  247. new_blindings_inuse[rsa->num_blindings] = 1;
  248. *index_used = rsa->num_blindings;
  249. if (rsa->blindings != NULL) {
  250. OPENSSL_free(rsa->blindings);
  251. }
  252. rsa->blindings = new_blindings;
  253. if (rsa->blindings_inuse != NULL) {
  254. OPENSSL_free(rsa->blindings_inuse);
  255. }
  256. rsa->blindings_inuse = new_blindings_inuse;
  257. rsa->num_blindings++;
  258. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  259. return ret;
  260. err2:
  261. OPENSSL_free(new_blindings);
  262. err1:
  263. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  264. BN_BLINDING_free(ret);
  265. return NULL;
  266. }
  267. /* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
  268. * for other threads to use. */
  269. static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
  270. unsigned blinding_index) {
  271. if (blinding_index == MAX_BLINDINGS_PER_RSA) {
  272. /* This blinding wasn't cached. */
  273. BN_BLINDING_free(blinding);
  274. return;
  275. }
  276. CRYPTO_w_lock(CRYPTO_LOCK_RSA_BLINDING);
  277. rsa->blindings_inuse[blinding_index] = 0;
  278. CRYPTO_w_unlock(CRYPTO_LOCK_RSA_BLINDING);
  279. }
  280. /* signing */
  281. static int sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  282. const uint8_t *in, size_t in_len, int padding) {
  283. const unsigned rsa_size = RSA_size(rsa);
  284. BIGNUM *f, *result;
  285. uint8_t *buf = NULL;
  286. BN_CTX *ctx = NULL;
  287. unsigned blinding_index = 0;
  288. BN_BLINDING *blinding = NULL;
  289. int i, ret = 0;
  290. if (max_out < rsa_size) {
  291. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  292. return 0;
  293. }
  294. ctx = BN_CTX_new();
  295. if (ctx == NULL) {
  296. goto err;
  297. }
  298. BN_CTX_start(ctx);
  299. f = BN_CTX_get(ctx);
  300. result = BN_CTX_get(ctx);
  301. buf = OPENSSL_malloc(rsa_size);
  302. if (!f || !result || !buf) {
  303. OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_MALLOC_FAILURE);
  304. goto err;
  305. }
  306. switch (padding) {
  307. case RSA_PKCS1_PADDING:
  308. i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
  309. break;
  310. case RSA_NO_PADDING:
  311. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  312. break;
  313. default:
  314. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE);
  315. goto err;
  316. }
  317. if (i <= 0) {
  318. goto err;
  319. }
  320. if (BN_bin2bn(buf, rsa_size, f) == NULL) {
  321. goto err;
  322. }
  323. if (BN_ucmp(f, rsa->n) >= 0) {
  324. /* usually the padding functions would catch this */
  325. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  326. goto err;
  327. }
  328. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  329. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  330. if (blinding == NULL) {
  331. OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
  332. goto err;
  333. }
  334. if (!BN_BLINDING_convert_ex(f, NULL, blinding, ctx)) {
  335. goto err;
  336. }
  337. }
  338. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  339. ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
  340. (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  341. if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
  342. goto err;
  343. }
  344. } else {
  345. BIGNUM local_d;
  346. BIGNUM *d = NULL;
  347. BN_init(&local_d);
  348. d = &local_d;
  349. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  350. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  351. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  352. ctx)) {
  353. goto err;
  354. }
  355. }
  356. if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->_method_mod_n)) {
  357. goto err;
  358. }
  359. }
  360. if (blinding) {
  361. if (!BN_BLINDING_invert_ex(result, NULL, blinding, ctx)) {
  362. goto err;
  363. }
  364. }
  365. if (!BN_bn2bin_padded(out, rsa_size, result)) {
  366. OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
  367. goto err;
  368. }
  369. *out_len = rsa_size;
  370. ret = 1;
  371. err:
  372. if (ctx != NULL) {
  373. BN_CTX_end(ctx);
  374. BN_CTX_free(ctx);
  375. }
  376. if (buf != NULL) {
  377. OPENSSL_cleanse(buf, rsa_size);
  378. OPENSSL_free(buf);
  379. }
  380. if (blinding != NULL) {
  381. rsa_blinding_release(rsa, blinding, blinding_index);
  382. }
  383. return ret;
  384. }
  385. static int decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  386. const uint8_t *in, size_t in_len, int padding) {
  387. const unsigned rsa_size = RSA_size(rsa);
  388. BIGNUM *f, *result;
  389. int r = -1;
  390. uint8_t *buf = NULL;
  391. BN_CTX *ctx = NULL;
  392. unsigned blinding_index;
  393. BN_BLINDING *blinding = NULL;
  394. int ret = 0;
  395. if (max_out < rsa_size) {
  396. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  397. return 0;
  398. }
  399. ctx = BN_CTX_new();
  400. if (ctx == NULL) {
  401. goto err;
  402. }
  403. BN_CTX_start(ctx);
  404. f = BN_CTX_get(ctx);
  405. result = BN_CTX_get(ctx);
  406. buf = OPENSSL_malloc(rsa_size);
  407. if (!f || !result || !buf) {
  408. OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_MALLOC_FAILURE);
  409. goto err;
  410. }
  411. /* This check was for equality but PGP does evil things
  412. * and chops off the top '0' bytes.
  413. * TODO(fork): investigate this. */
  414. if (in_len > rsa_size) {
  415. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_DATA_GREATER_THAN_MOD_LEN);
  416. goto err;
  417. }
  418. /* make data into a big number */
  419. if (BN_bin2bn(in, (int)in_len, f) == NULL) {
  420. goto err;
  421. }
  422. if (BN_ucmp(f, rsa->n) >= 0) {
  423. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  424. goto err;
  425. }
  426. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  427. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  428. if (blinding == NULL) {
  429. OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_INTERNAL_ERROR);
  430. goto err;
  431. }
  432. if (!BN_BLINDING_convert_ex(f, NULL, blinding, ctx)) {
  433. goto err;
  434. }
  435. }
  436. /* do the decrypt */
  437. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  438. ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
  439. (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  440. if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
  441. goto err;
  442. }
  443. } else {
  444. BIGNUM local_d;
  445. BIGNUM *d = NULL;
  446. d = &local_d;
  447. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  448. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  449. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  450. ctx)) {
  451. goto err;
  452. }
  453. }
  454. if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->_method_mod_n)) {
  455. goto err;
  456. }
  457. }
  458. if (blinding) {
  459. if (!BN_BLINDING_invert_ex(result, NULL, blinding, ctx)) {
  460. goto err;
  461. }
  462. }
  463. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  464. OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_INTERNAL_ERROR);
  465. goto err;
  466. }
  467. switch (padding) {
  468. case RSA_PKCS1_PADDING:
  469. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  470. break;
  471. case RSA_PKCS1_OAEP_PADDING:
  472. /* Use the default parameters: SHA-1 for both hashes and no label. */
  473. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  474. NULL, 0, NULL, NULL);
  475. break;
  476. case RSA_SSLV23_PADDING:
  477. r = RSA_padding_check_SSLv23(out, rsa_size, buf, rsa_size);
  478. break;
  479. case RSA_NO_PADDING:
  480. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  481. break;
  482. default:
  483. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_UNKNOWN_PADDING_TYPE);
  484. goto err;
  485. }
  486. if (r < 0) {
  487. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_PADDING_CHECK_FAILED);
  488. } else {
  489. *out_len = r;
  490. ret = 1;
  491. }
  492. err:
  493. if (ctx != NULL) {
  494. BN_CTX_end(ctx);
  495. BN_CTX_free(ctx);
  496. }
  497. if (buf != NULL) {
  498. OPENSSL_cleanse(buf, rsa_size);
  499. OPENSSL_free(buf);
  500. }
  501. if (blinding != NULL) {
  502. rsa_blinding_release(rsa, blinding, blinding_index);
  503. }
  504. return ret;
  505. }
  506. static int verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  507. const uint8_t *in, size_t in_len, int padding) {
  508. const unsigned rsa_size = RSA_size(rsa);
  509. BIGNUM *f, *result;
  510. int ret = 0;
  511. int r = -1;
  512. uint8_t *buf = NULL;
  513. BN_CTX *ctx = NULL;
  514. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  515. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_MODULUS_TOO_LARGE);
  516. return 0;
  517. }
  518. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  519. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
  520. return 0;
  521. }
  522. if (max_out < rsa_size) {
  523. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  524. return 0;
  525. }
  526. /* for large moduli, enforce exponent limit */
  527. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  528. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  529. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
  530. return 0;
  531. }
  532. ctx = BN_CTX_new();
  533. if (ctx == NULL) {
  534. goto err;
  535. }
  536. BN_CTX_start(ctx);
  537. f = BN_CTX_get(ctx);
  538. result = BN_CTX_get(ctx);
  539. buf = OPENSSL_malloc(rsa_size);
  540. if (!f || !result || !buf) {
  541. OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_MALLOC_FAILURE);
  542. goto err;
  543. }
  544. /* This check was for equality but PGP does evil things
  545. * and chops off the top '0' bytes.
  546. * TODO(fork): investigate */
  547. if (in_len > rsa_size) {
  548. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_GREATER_THAN_MOD_LEN);
  549. goto err;
  550. }
  551. if (BN_bin2bn(in, in_len, f) == NULL) {
  552. goto err;
  553. }
  554. if (BN_ucmp(f, rsa->n) >= 0) {
  555. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  556. goto err;
  557. }
  558. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  559. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  560. ctx)) {
  561. goto err;
  562. }
  563. }
  564. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
  565. rsa->_method_mod_n)) {
  566. goto err;
  567. }
  568. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  569. OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_INTERNAL_ERROR);
  570. goto err;
  571. }
  572. switch (padding) {
  573. case RSA_PKCS1_PADDING:
  574. r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
  575. break;
  576. case RSA_NO_PADDING:
  577. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  578. break;
  579. default:
  580. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_UNKNOWN_PADDING_TYPE);
  581. goto err;
  582. }
  583. if (r < 0) {
  584. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_PADDING_CHECK_FAILED);
  585. } else {
  586. *out_len = r;
  587. ret = 1;
  588. }
  589. err:
  590. if (ctx != NULL) {
  591. BN_CTX_end(ctx);
  592. BN_CTX_free(ctx);
  593. }
  594. if (buf != NULL) {
  595. OPENSSL_cleanse(buf, rsa_size);
  596. OPENSSL_free(buf);
  597. }
  598. return ret;
  599. }
  600. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  601. BIGNUM *r1, *m1, *vrfy;
  602. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  603. BIGNUM *dmp1, *dmq1, *c, *pr1;
  604. int ret = 0;
  605. BN_CTX_start(ctx);
  606. r1 = BN_CTX_get(ctx);
  607. m1 = BN_CTX_get(ctx);
  608. vrfy = BN_CTX_get(ctx);
  609. {
  610. BIGNUM local_p, local_q;
  611. BIGNUM *p = NULL, *q = NULL;
  612. /* Make sure BN_mod_inverse in Montgomery intialization uses the
  613. * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) */
  614. BN_init(&local_p);
  615. p = &local_p;
  616. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  617. BN_init(&local_q);
  618. q = &local_q;
  619. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  620. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  621. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)) {
  622. goto err;
  623. }
  624. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx)) {
  625. goto err;
  626. }
  627. }
  628. }
  629. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  630. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  631. ctx)) {
  632. goto err;
  633. }
  634. }
  635. /* compute I mod q */
  636. c = &local_c;
  637. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  638. if (!BN_mod(r1, c, rsa->q, ctx)) {
  639. goto err;
  640. }
  641. /* compute r1^dmq1 mod q */
  642. dmq1 = &local_dmq1;
  643. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  644. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q)) {
  645. goto err;
  646. }
  647. /* compute I mod p */
  648. c = &local_c;
  649. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  650. if (!BN_mod(r1, c, rsa->p, ctx)) {
  651. goto err;
  652. }
  653. /* compute r1^dmp1 mod p */
  654. dmp1 = &local_dmp1;
  655. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  656. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p)) {
  657. goto err;
  658. }
  659. if (!BN_sub(r0, r0, m1)) {
  660. goto err;
  661. }
  662. /* This will help stop the size of r0 increasing, which does
  663. * affect the multiply if it optimised for a power of 2 size */
  664. if (BN_is_negative(r0)) {
  665. if (!BN_add(r0, r0, rsa->p)) {
  666. goto err;
  667. }
  668. }
  669. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  670. goto err;
  671. }
  672. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  673. pr1 = &local_r1;
  674. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  675. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  676. goto err;
  677. }
  678. /* If p < q it is occasionally possible for the correction of
  679. * adding 'p' if r0 is negative above to leave the result still
  680. * negative. This can break the private key operations: the following
  681. * second correction should *always* correct this rare occurrence.
  682. * This will *never* happen with OpenSSL generated keys because
  683. * they ensure p > q [steve] */
  684. if (BN_is_negative(r0)) {
  685. if (!BN_add(r0, r0, rsa->p)) {
  686. goto err;
  687. }
  688. }
  689. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  690. goto err;
  691. }
  692. if (!BN_add(r0, r1, m1)) {
  693. goto err;
  694. }
  695. if (rsa->e && rsa->n) {
  696. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  697. rsa->_method_mod_n)) {
  698. goto err;
  699. }
  700. /* If 'I' was greater than (or equal to) rsa->n, the operation
  701. * will be equivalent to using 'I mod n'. However, the result of
  702. * the verify will *always* be less than 'n' so we don't check
  703. * for absolute equality, just congruency. */
  704. if (!BN_sub(vrfy, vrfy, I)) {
  705. goto err;
  706. }
  707. if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
  708. goto err;
  709. }
  710. if (BN_is_negative(vrfy)) {
  711. if (!BN_add(vrfy, vrfy, rsa->n)) {
  712. goto err;
  713. }
  714. }
  715. if (!BN_is_zero(vrfy)) {
  716. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  717. * miscalculated CRT output, just do a raw (slower)
  718. * mod_exp and return that instead. */
  719. BIGNUM local_d;
  720. BIGNUM *d = NULL;
  721. d = &local_d;
  722. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  723. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, rsa->_method_mod_n)) {
  724. goto err;
  725. }
  726. }
  727. }
  728. ret = 1;
  729. err:
  730. BN_CTX_end(ctx);
  731. return ret;
  732. }
  733. static int keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  734. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  735. BIGNUM local_r0, local_d, local_p;
  736. BIGNUM *pr0, *d, *p;
  737. int bitsp, bitsq, ok = -1, n = 0;
  738. BN_CTX *ctx = NULL;
  739. ctx = BN_CTX_new();
  740. if (ctx == NULL) {
  741. goto err;
  742. }
  743. BN_CTX_start(ctx);
  744. r0 = BN_CTX_get(ctx);
  745. r1 = BN_CTX_get(ctx);
  746. r2 = BN_CTX_get(ctx);
  747. r3 = BN_CTX_get(ctx);
  748. if (r3 == NULL) {
  749. goto err;
  750. }
  751. bitsp = (bits + 1) / 2;
  752. bitsq = bits - bitsp;
  753. /* We need the RSA components non-NULL */
  754. if (!rsa->n && ((rsa->n = BN_new()) == NULL))
  755. goto err;
  756. if (!rsa->d && ((rsa->d = BN_new()) == NULL))
  757. goto err;
  758. if (!rsa->e && ((rsa->e = BN_new()) == NULL))
  759. goto err;
  760. if (!rsa->p && ((rsa->p = BN_new()) == NULL))
  761. goto err;
  762. if (!rsa->q && ((rsa->q = BN_new()) == NULL))
  763. goto err;
  764. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL))
  765. goto err;
  766. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL))
  767. goto err;
  768. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL))
  769. goto err;
  770. BN_copy(rsa->e, e_value);
  771. /* generate p and q */
  772. for (;;) {
  773. if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb))
  774. goto err;
  775. if (!BN_sub(r2, rsa->p, BN_value_one()))
  776. goto err;
  777. if (!BN_gcd(r1, r2, rsa->e, ctx))
  778. goto err;
  779. if (BN_is_one(r1))
  780. break;
  781. if (!BN_GENCB_call(cb, 2, n++))
  782. goto err;
  783. }
  784. if (!BN_GENCB_call(cb, 3, 0))
  785. goto err;
  786. for (;;) {
  787. /* When generating ridiculously small keys, we can get stuck
  788. * continually regenerating the same prime values. Check for
  789. * this and bail if it happens 3 times. */
  790. unsigned int degenerate = 0;
  791. do {
  792. if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
  793. goto err;
  794. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  795. if (degenerate == 3) {
  796. ok = 0; /* we set our own err */
  797. OPENSSL_PUT_ERROR(RSA, keygen, RSA_R_KEY_SIZE_TOO_SMALL);
  798. goto err;
  799. }
  800. if (!BN_sub(r2, rsa->q, BN_value_one()))
  801. goto err;
  802. if (!BN_gcd(r1, r2, rsa->e, ctx))
  803. goto err;
  804. if (BN_is_one(r1))
  805. break;
  806. if (!BN_GENCB_call(cb, 2, n++))
  807. goto err;
  808. }
  809. if (!BN_GENCB_call(cb, 3, 1))
  810. goto err;
  811. if (BN_cmp(rsa->p, rsa->q) < 0) {
  812. tmp = rsa->p;
  813. rsa->p = rsa->q;
  814. rsa->q = tmp;
  815. }
  816. /* calculate n */
  817. if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))
  818. goto err;
  819. /* calculate d */
  820. if (!BN_sub(r1, rsa->p, BN_value_one()))
  821. goto err; /* p-1 */
  822. if (!BN_sub(r2, rsa->q, BN_value_one()))
  823. goto err; /* q-1 */
  824. if (!BN_mul(r0, r1, r2, ctx))
  825. goto err; /* (p-1)(q-1) */
  826. pr0 = &local_r0;
  827. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  828. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx))
  829. goto err; /* d */
  830. /* set up d for correct BN_FLG_CONSTTIME flag */
  831. d = &local_d;
  832. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  833. /* calculate d mod (p-1) */
  834. if (!BN_mod(rsa->dmp1, d, r1, ctx))
  835. goto err;
  836. /* calculate d mod (q-1) */
  837. if (!BN_mod(rsa->dmq1, d, r2, ctx))
  838. goto err;
  839. /* calculate inverse of q mod p */
  840. p = &local_p;
  841. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  842. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx))
  843. goto err;
  844. ok = 1;
  845. err:
  846. if (ok == -1) {
  847. OPENSSL_PUT_ERROR(RSA, keygen, ERR_LIB_BN);
  848. ok = 0;
  849. }
  850. if (ctx != NULL) {
  851. BN_CTX_end(ctx);
  852. BN_CTX_free(ctx);
  853. }
  854. return ok;
  855. }
  856. const struct rsa_meth_st RSA_default_method = {
  857. {
  858. 0 /* references */,
  859. 1 /* is_static */,
  860. },
  861. NULL /* app_data */,
  862. NULL /* init */,
  863. finish,
  864. NULL /* sign */,
  865. NULL /* verify */,
  866. encrypt,
  867. sign_raw,
  868. decrypt,
  869. verify_raw,
  870. mod_exp /* mod_exp */,
  871. BN_mod_exp_mont /* bn_mod_exp */,
  872. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  873. keygen,
  874. };