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.
 
 
 
 
 
 

959 rivejä
25 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 size_t size(const RSA *rsa) {
  78. return BN_num_bytes(rsa->n);
  79. }
  80. static int encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  81. const uint8_t *in, size_t in_len, int padding) {
  82. const unsigned rsa_size = RSA_size(rsa);
  83. BIGNUM *f, *result;
  84. uint8_t *buf = NULL;
  85. BN_CTX *ctx = NULL;
  86. int i, ret = 0;
  87. if (rsa_size > OPENSSL_RSA_MAX_MODULUS_BITS) {
  88. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_MODULUS_TOO_LARGE);
  89. return 0;
  90. }
  91. if (max_out < rsa_size) {
  92. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  93. return 0;
  94. }
  95. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  96. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
  97. return 0;
  98. }
  99. /* for large moduli, enforce exponent limit */
  100. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  101. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  102. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
  103. return 0;
  104. }
  105. ctx = BN_CTX_new();
  106. if (ctx == NULL) {
  107. goto err;
  108. }
  109. BN_CTX_start(ctx);
  110. f = BN_CTX_get(ctx);
  111. result = BN_CTX_get(ctx);
  112. buf = OPENSSL_malloc(rsa_size);
  113. if (!f || !result || !buf) {
  114. OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_MALLOC_FAILURE);
  115. goto err;
  116. }
  117. switch (padding) {
  118. case RSA_PKCS1_PADDING:
  119. i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
  120. break;
  121. case RSA_PKCS1_OAEP_PADDING:
  122. /* Use the default parameters: SHA-1 for both hashes and no label. */
  123. i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
  124. NULL, 0, NULL, NULL);
  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. uint8_t *buf = NULL;
  285. int i, ret = 0;
  286. if (max_out < rsa_size) {
  287. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  288. return 0;
  289. }
  290. buf = OPENSSL_malloc(rsa_size);
  291. if (buf == NULL) {
  292. OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_MALLOC_FAILURE);
  293. goto err;
  294. }
  295. switch (padding) {
  296. case RSA_PKCS1_PADDING:
  297. i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
  298. break;
  299. case RSA_NO_PADDING:
  300. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  301. break;
  302. default:
  303. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE);
  304. goto err;
  305. }
  306. if (i <= 0) {
  307. goto err;
  308. }
  309. if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
  310. OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_INTERNAL_ERROR);
  311. goto err;
  312. }
  313. *out_len = rsa_size;
  314. ret = 1;
  315. err:
  316. if (buf != NULL) {
  317. OPENSSL_cleanse(buf, rsa_size);
  318. OPENSSL_free(buf);
  319. }
  320. return ret;
  321. }
  322. static int decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  323. const uint8_t *in, size_t in_len, int padding) {
  324. const unsigned rsa_size = RSA_size(rsa);
  325. int r = -1;
  326. uint8_t *buf = NULL;
  327. int ret = 0;
  328. if (max_out < rsa_size) {
  329. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  330. return 0;
  331. }
  332. buf = OPENSSL_malloc(rsa_size);
  333. if (buf == NULL) {
  334. OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_MALLOC_FAILURE);
  335. goto err;
  336. }
  337. if (in_len != rsa_size) {
  338. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  339. goto err;
  340. }
  341. if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
  342. OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_INTERNAL_ERROR);
  343. goto err;
  344. }
  345. switch (padding) {
  346. case RSA_PKCS1_PADDING:
  347. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  348. break;
  349. case RSA_PKCS1_OAEP_PADDING:
  350. /* Use the default parameters: SHA-1 for both hashes and no label. */
  351. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  352. NULL, 0, NULL, NULL);
  353. break;
  354. case RSA_NO_PADDING:
  355. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  356. break;
  357. default:
  358. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_UNKNOWN_PADDING_TYPE);
  359. goto err;
  360. }
  361. if (r < 0) {
  362. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_PADDING_CHECK_FAILED);
  363. } else {
  364. *out_len = r;
  365. ret = 1;
  366. }
  367. err:
  368. if (buf != NULL) {
  369. OPENSSL_cleanse(buf, rsa_size);
  370. OPENSSL_free(buf);
  371. }
  372. return ret;
  373. }
  374. static int verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  375. const uint8_t *in, size_t in_len, int padding) {
  376. const unsigned rsa_size = RSA_size(rsa);
  377. BIGNUM *f, *result;
  378. int ret = 0;
  379. int r = -1;
  380. uint8_t *buf = NULL;
  381. BN_CTX *ctx = NULL;
  382. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  383. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_MODULUS_TOO_LARGE);
  384. return 0;
  385. }
  386. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  387. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
  388. return 0;
  389. }
  390. if (max_out < rsa_size) {
  391. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  392. return 0;
  393. }
  394. /* for large moduli, enforce exponent limit */
  395. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  396. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  397. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
  398. return 0;
  399. }
  400. ctx = BN_CTX_new();
  401. if (ctx == NULL) {
  402. goto err;
  403. }
  404. BN_CTX_start(ctx);
  405. f = BN_CTX_get(ctx);
  406. result = BN_CTX_get(ctx);
  407. buf = OPENSSL_malloc(rsa_size);
  408. if (!f || !result || !buf) {
  409. OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_MALLOC_FAILURE);
  410. goto err;
  411. }
  412. if (in_len != rsa_size) {
  413. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  414. goto err;
  415. }
  416. if (BN_bin2bn(in, in_len, f) == NULL) {
  417. goto err;
  418. }
  419. if (BN_ucmp(f, rsa->n) >= 0) {
  420. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  421. goto err;
  422. }
  423. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  424. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  425. ctx)) {
  426. goto err;
  427. }
  428. }
  429. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
  430. rsa->_method_mod_n)) {
  431. goto err;
  432. }
  433. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  434. OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_INTERNAL_ERROR);
  435. goto err;
  436. }
  437. switch (padding) {
  438. case RSA_PKCS1_PADDING:
  439. r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
  440. break;
  441. case RSA_NO_PADDING:
  442. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  443. break;
  444. default:
  445. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_UNKNOWN_PADDING_TYPE);
  446. goto err;
  447. }
  448. if (r < 0) {
  449. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_PADDING_CHECK_FAILED);
  450. } else {
  451. *out_len = r;
  452. ret = 1;
  453. }
  454. err:
  455. if (ctx != NULL) {
  456. BN_CTX_end(ctx);
  457. BN_CTX_free(ctx);
  458. }
  459. if (buf != NULL) {
  460. OPENSSL_cleanse(buf, rsa_size);
  461. OPENSSL_free(buf);
  462. }
  463. return ret;
  464. }
  465. static int private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  466. size_t len) {
  467. BIGNUM *f, *result;
  468. BN_CTX *ctx = NULL;
  469. unsigned blinding_index = 0;
  470. BN_BLINDING *blinding = NULL;
  471. int ret = 0;
  472. ctx = BN_CTX_new();
  473. if (ctx == NULL) {
  474. goto err;
  475. }
  476. BN_CTX_start(ctx);
  477. f = BN_CTX_get(ctx);
  478. result = BN_CTX_get(ctx);
  479. if (f == NULL || result == NULL) {
  480. OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_MALLOC_FAILURE);
  481. goto err;
  482. }
  483. if (BN_bin2bn(in, len, f) == NULL) {
  484. goto err;
  485. }
  486. if (BN_ucmp(f, rsa->n) >= 0) {
  487. /* Usually the padding functions would catch this. */
  488. OPENSSL_PUT_ERROR(RSA, private_transform, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  489. goto err;
  490. }
  491. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  492. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  493. if (blinding == NULL) {
  494. OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_INTERNAL_ERROR);
  495. goto err;
  496. }
  497. if (!BN_BLINDING_convert_ex(f, NULL, blinding, ctx)) {
  498. goto err;
  499. }
  500. }
  501. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  502. ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
  503. (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  504. if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
  505. goto err;
  506. }
  507. } else {
  508. BIGNUM local_d;
  509. BIGNUM *d = NULL;
  510. BN_init(&local_d);
  511. d = &local_d;
  512. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  513. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  514. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  515. ctx)) {
  516. goto err;
  517. }
  518. }
  519. if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->_method_mod_n)) {
  520. goto err;
  521. }
  522. }
  523. if (blinding) {
  524. if (!BN_BLINDING_invert_ex(result, NULL, blinding, ctx)) {
  525. goto err;
  526. }
  527. }
  528. if (!BN_bn2bin_padded(out, len, result)) {
  529. OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_INTERNAL_ERROR);
  530. goto err;
  531. }
  532. ret = 1;
  533. err:
  534. if (ctx != NULL) {
  535. BN_CTX_end(ctx);
  536. BN_CTX_free(ctx);
  537. }
  538. if (blinding != NULL) {
  539. rsa_blinding_release(rsa, blinding, blinding_index);
  540. }
  541. return ret;
  542. }
  543. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  544. BIGNUM *r1, *m1, *vrfy;
  545. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  546. BIGNUM *dmp1, *dmq1, *c, *pr1;
  547. int ret = 0;
  548. BN_CTX_start(ctx);
  549. r1 = BN_CTX_get(ctx);
  550. m1 = BN_CTX_get(ctx);
  551. vrfy = BN_CTX_get(ctx);
  552. {
  553. BIGNUM local_p, local_q;
  554. BIGNUM *p = NULL, *q = NULL;
  555. /* Make sure BN_mod_inverse in Montgomery intialization uses the
  556. * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) */
  557. BN_init(&local_p);
  558. p = &local_p;
  559. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  560. BN_init(&local_q);
  561. q = &local_q;
  562. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  563. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  564. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_p, CRYPTO_LOCK_RSA, p, ctx)) {
  565. goto err;
  566. }
  567. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_q, CRYPTO_LOCK_RSA, q, ctx)) {
  568. goto err;
  569. }
  570. }
  571. }
  572. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  573. if (!BN_MONT_CTX_set_locked(&rsa->_method_mod_n, CRYPTO_LOCK_RSA, rsa->n,
  574. ctx)) {
  575. goto err;
  576. }
  577. }
  578. /* compute I mod q */
  579. c = &local_c;
  580. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  581. if (!BN_mod(r1, c, rsa->q, ctx)) {
  582. goto err;
  583. }
  584. /* compute r1^dmq1 mod q */
  585. dmq1 = &local_dmq1;
  586. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  587. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q)) {
  588. goto err;
  589. }
  590. /* compute I mod p */
  591. c = &local_c;
  592. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  593. if (!BN_mod(r1, c, rsa->p, ctx)) {
  594. goto err;
  595. }
  596. /* compute r1^dmp1 mod p */
  597. dmp1 = &local_dmp1;
  598. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  599. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p)) {
  600. goto err;
  601. }
  602. if (!BN_sub(r0, r0, m1)) {
  603. goto err;
  604. }
  605. /* This will help stop the size of r0 increasing, which does
  606. * affect the multiply if it optimised for a power of 2 size */
  607. if (BN_is_negative(r0)) {
  608. if (!BN_add(r0, r0, rsa->p)) {
  609. goto err;
  610. }
  611. }
  612. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  613. goto err;
  614. }
  615. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  616. pr1 = &local_r1;
  617. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  618. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  619. goto err;
  620. }
  621. /* If p < q it is occasionally possible for the correction of
  622. * adding 'p' if r0 is negative above to leave the result still
  623. * negative. This can break the private key operations: the following
  624. * second correction should *always* correct this rare occurrence.
  625. * This will *never* happen with OpenSSL generated keys because
  626. * they ensure p > q [steve] */
  627. if (BN_is_negative(r0)) {
  628. if (!BN_add(r0, r0, rsa->p)) {
  629. goto err;
  630. }
  631. }
  632. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  633. goto err;
  634. }
  635. if (!BN_add(r0, r1, m1)) {
  636. goto err;
  637. }
  638. if (rsa->e && rsa->n) {
  639. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  640. rsa->_method_mod_n)) {
  641. goto err;
  642. }
  643. /* If 'I' was greater than (or equal to) rsa->n, the operation
  644. * will be equivalent to using 'I mod n'. However, the result of
  645. * the verify will *always* be less than 'n' so we don't check
  646. * for absolute equality, just congruency. */
  647. if (!BN_sub(vrfy, vrfy, I)) {
  648. goto err;
  649. }
  650. if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
  651. goto err;
  652. }
  653. if (BN_is_negative(vrfy)) {
  654. if (!BN_add(vrfy, vrfy, rsa->n)) {
  655. goto err;
  656. }
  657. }
  658. if (!BN_is_zero(vrfy)) {
  659. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  660. * miscalculated CRT output, just do a raw (slower)
  661. * mod_exp and return that instead. */
  662. BIGNUM local_d;
  663. BIGNUM *d = NULL;
  664. d = &local_d;
  665. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  666. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, rsa->_method_mod_n)) {
  667. goto err;
  668. }
  669. }
  670. }
  671. ret = 1;
  672. err:
  673. BN_CTX_end(ctx);
  674. return ret;
  675. }
  676. static int keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  677. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  678. BIGNUM local_r0, local_d, local_p;
  679. BIGNUM *pr0, *d, *p;
  680. int bitsp, bitsq, ok = -1, n = 0;
  681. BN_CTX *ctx = NULL;
  682. ctx = BN_CTX_new();
  683. if (ctx == NULL) {
  684. goto err;
  685. }
  686. BN_CTX_start(ctx);
  687. r0 = BN_CTX_get(ctx);
  688. r1 = BN_CTX_get(ctx);
  689. r2 = BN_CTX_get(ctx);
  690. r3 = BN_CTX_get(ctx);
  691. if (r3 == NULL) {
  692. goto err;
  693. }
  694. bitsp = (bits + 1) / 2;
  695. bitsq = bits - bitsp;
  696. /* We need the RSA components non-NULL */
  697. if (!rsa->n && ((rsa->n = BN_new()) == NULL))
  698. goto err;
  699. if (!rsa->d && ((rsa->d = BN_new()) == NULL))
  700. goto err;
  701. if (!rsa->e && ((rsa->e = BN_new()) == NULL))
  702. goto err;
  703. if (!rsa->p && ((rsa->p = BN_new()) == NULL))
  704. goto err;
  705. if (!rsa->q && ((rsa->q = BN_new()) == NULL))
  706. goto err;
  707. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL))
  708. goto err;
  709. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL))
  710. goto err;
  711. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL))
  712. goto err;
  713. BN_copy(rsa->e, e_value);
  714. /* generate p and q */
  715. for (;;) {
  716. if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb))
  717. goto err;
  718. if (!BN_sub(r2, rsa->p, BN_value_one()))
  719. goto err;
  720. if (!BN_gcd(r1, r2, rsa->e, ctx))
  721. goto err;
  722. if (BN_is_one(r1))
  723. break;
  724. if (!BN_GENCB_call(cb, 2, n++))
  725. goto err;
  726. }
  727. if (!BN_GENCB_call(cb, 3, 0))
  728. goto err;
  729. for (;;) {
  730. /* When generating ridiculously small keys, we can get stuck
  731. * continually regenerating the same prime values. Check for
  732. * this and bail if it happens 3 times. */
  733. unsigned int degenerate = 0;
  734. do {
  735. if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb))
  736. goto err;
  737. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  738. if (degenerate == 3) {
  739. ok = 0; /* we set our own err */
  740. OPENSSL_PUT_ERROR(RSA, keygen, RSA_R_KEY_SIZE_TOO_SMALL);
  741. goto err;
  742. }
  743. if (!BN_sub(r2, rsa->q, BN_value_one()))
  744. goto err;
  745. if (!BN_gcd(r1, r2, rsa->e, ctx))
  746. goto err;
  747. if (BN_is_one(r1))
  748. break;
  749. if (!BN_GENCB_call(cb, 2, n++))
  750. goto err;
  751. }
  752. if (!BN_GENCB_call(cb, 3, 1))
  753. goto err;
  754. if (BN_cmp(rsa->p, rsa->q) < 0) {
  755. tmp = rsa->p;
  756. rsa->p = rsa->q;
  757. rsa->q = tmp;
  758. }
  759. /* calculate n */
  760. if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx))
  761. goto err;
  762. /* calculate d */
  763. if (!BN_sub(r1, rsa->p, BN_value_one()))
  764. goto err; /* p-1 */
  765. if (!BN_sub(r2, rsa->q, BN_value_one()))
  766. goto err; /* q-1 */
  767. if (!BN_mul(r0, r1, r2, ctx))
  768. goto err; /* (p-1)(q-1) */
  769. pr0 = &local_r0;
  770. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  771. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx))
  772. goto err; /* d */
  773. /* set up d for correct BN_FLG_CONSTTIME flag */
  774. d = &local_d;
  775. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  776. /* calculate d mod (p-1) */
  777. if (!BN_mod(rsa->dmp1, d, r1, ctx))
  778. goto err;
  779. /* calculate d mod (q-1) */
  780. if (!BN_mod(rsa->dmq1, d, r2, ctx))
  781. goto err;
  782. /* calculate inverse of q mod p */
  783. p = &local_p;
  784. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  785. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx))
  786. goto err;
  787. ok = 1;
  788. err:
  789. if (ok == -1) {
  790. OPENSSL_PUT_ERROR(RSA, keygen, ERR_LIB_BN);
  791. ok = 0;
  792. }
  793. if (ctx != NULL) {
  794. BN_CTX_end(ctx);
  795. BN_CTX_free(ctx);
  796. }
  797. return ok;
  798. }
  799. const struct rsa_meth_st RSA_default_method = {
  800. {
  801. 0 /* references */,
  802. 1 /* is_static */,
  803. },
  804. NULL /* app_data */,
  805. NULL /* init */,
  806. finish,
  807. size,
  808. NULL /* sign */,
  809. NULL /* verify */,
  810. encrypt,
  811. sign_raw,
  812. decrypt,
  813. verify_raw,
  814. private_transform,
  815. mod_exp /* mod_exp */,
  816. BN_mod_exp_mont /* bn_mod_exp */,
  817. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  818. keygen,
  819. };