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.
 
 
 
 
 
 

969 lines
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 <string.h>
  58. #include <openssl/bn.h>
  59. #include <openssl/err.h>
  60. #include <openssl/mem.h>
  61. #include <openssl/thread.h>
  62. #include "internal.h"
  63. #include "../internal.h"
  64. #define OPENSSL_RSA_MAX_MODULUS_BITS 16384
  65. #define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
  66. #define OPENSSL_RSA_MAX_PUBEXP_BITS \
  67. 64 /* exponent limit enforced for "large" modulus only */
  68. static int finish(RSA *rsa) {
  69. if (rsa->_method_mod_n != NULL) {
  70. BN_MONT_CTX_free(rsa->_method_mod_n);
  71. }
  72. if (rsa->_method_mod_p != NULL) {
  73. BN_MONT_CTX_free(rsa->_method_mod_p);
  74. }
  75. if (rsa->_method_mod_q != NULL) {
  76. BN_MONT_CTX_free(rsa->_method_mod_q);
  77. }
  78. return 1;
  79. }
  80. static size_t size(const RSA *rsa) {
  81. return BN_num_bytes(rsa->n);
  82. }
  83. static int encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  84. const uint8_t *in, size_t in_len, int padding) {
  85. const unsigned rsa_size = RSA_size(rsa);
  86. BIGNUM *f, *result;
  87. uint8_t *buf = NULL;
  88. BN_CTX *ctx = NULL;
  89. int i, ret = 0;
  90. if (rsa_size > OPENSSL_RSA_MAX_MODULUS_BITS) {
  91. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_MODULUS_TOO_LARGE);
  92. return 0;
  93. }
  94. if (max_out < rsa_size) {
  95. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  96. return 0;
  97. }
  98. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  99. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
  100. return 0;
  101. }
  102. /* for large moduli, enforce exponent limit */
  103. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  104. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  105. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_BAD_E_VALUE);
  106. return 0;
  107. }
  108. ctx = BN_CTX_new();
  109. if (ctx == NULL) {
  110. goto err;
  111. }
  112. BN_CTX_start(ctx);
  113. f = BN_CTX_get(ctx);
  114. result = BN_CTX_get(ctx);
  115. buf = OPENSSL_malloc(rsa_size);
  116. if (!f || !result || !buf) {
  117. OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_MALLOC_FAILURE);
  118. goto err;
  119. }
  120. switch (padding) {
  121. case RSA_PKCS1_PADDING:
  122. i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
  123. break;
  124. case RSA_PKCS1_OAEP_PADDING:
  125. /* Use the default parameters: SHA-1 for both hashes and no label. */
  126. i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
  127. NULL, 0, NULL, NULL);
  128. break;
  129. case RSA_NO_PADDING:
  130. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  131. break;
  132. default:
  133. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_UNKNOWN_PADDING_TYPE);
  134. goto err;
  135. }
  136. if (i <= 0) {
  137. goto err;
  138. }
  139. if (BN_bin2bn(buf, rsa_size, f) == NULL) {
  140. goto err;
  141. }
  142. if (BN_ucmp(f, rsa->n) >= 0) {
  143. /* usually the padding functions would catch this */
  144. OPENSSL_PUT_ERROR(RSA, encrypt, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  145. goto err;
  146. }
  147. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  148. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
  149. NULL) {
  150. goto err;
  151. }
  152. }
  153. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
  154. rsa->_method_mod_n)) {
  155. goto err;
  156. }
  157. /* put in leading 0 bytes if the number is less than the length of the
  158. * modulus */
  159. if (!BN_bn2bin_padded(out, rsa_size, result)) {
  160. OPENSSL_PUT_ERROR(RSA, encrypt, ERR_R_INTERNAL_ERROR);
  161. goto err;
  162. }
  163. *out_len = rsa_size;
  164. ret = 1;
  165. err:
  166. if (ctx != NULL) {
  167. BN_CTX_end(ctx);
  168. BN_CTX_free(ctx);
  169. }
  170. if (buf != NULL) {
  171. OPENSSL_cleanse(buf, rsa_size);
  172. OPENSSL_free(buf);
  173. }
  174. return ret;
  175. }
  176. /* MAX_BLINDINGS_PER_RSA defines the maximum number of cached BN_BLINDINGs per
  177. * RSA*. Then this limit is exceeded, BN_BLINDING objects will be created and
  178. * destroyed as needed. */
  179. #define MAX_BLINDINGS_PER_RSA 1024
  180. /* rsa_blinding_get returns a BN_BLINDING to use with |rsa|. It does this by
  181. * allocating one of the cached BN_BLINDING objects in |rsa->blindings|. If
  182. * none are free, the cache will be extended by a extra element and the new
  183. * BN_BLINDING is returned.
  184. *
  185. * On success, the index of the assigned BN_BLINDING is written to
  186. * |*index_used| and must be passed to |rsa_blinding_release| when finished. */
  187. static BN_BLINDING *rsa_blinding_get(RSA *rsa, unsigned *index_used,
  188. BN_CTX *ctx) {
  189. BN_BLINDING *ret = NULL;
  190. BN_BLINDING **new_blindings;
  191. uint8_t *new_blindings_inuse;
  192. char overflow = 0;
  193. CRYPTO_MUTEX_lock_write(&rsa->lock);
  194. unsigned i;
  195. for (i = 0; i < rsa->num_blindings; i++) {
  196. if (rsa->blindings_inuse[i] == 0) {
  197. rsa->blindings_inuse[i] = 1;
  198. ret = rsa->blindings[i];
  199. *index_used = i;
  200. break;
  201. }
  202. }
  203. if (ret != NULL) {
  204. CRYPTO_MUTEX_unlock(&rsa->lock);
  205. return ret;
  206. }
  207. overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
  208. /* We didn't find a free BN_BLINDING to use so increase the length of
  209. * the arrays by one and use the newly created element. */
  210. CRYPTO_MUTEX_unlock(&rsa->lock);
  211. ret = rsa_setup_blinding(rsa, ctx);
  212. if (ret == NULL) {
  213. return NULL;
  214. }
  215. if (overflow) {
  216. /* We cannot add any more cached BN_BLINDINGs so we use |ret|
  217. * and mark it for destruction in |rsa_blinding_release|. */
  218. *index_used = MAX_BLINDINGS_PER_RSA;
  219. return ret;
  220. }
  221. CRYPTO_MUTEX_lock_write(&rsa->lock);
  222. new_blindings =
  223. OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
  224. if (new_blindings == NULL) {
  225. goto err1;
  226. }
  227. memcpy(new_blindings, rsa->blindings,
  228. sizeof(BN_BLINDING *) * rsa->num_blindings);
  229. new_blindings[rsa->num_blindings] = ret;
  230. new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
  231. if (new_blindings_inuse == NULL) {
  232. goto err2;
  233. }
  234. memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
  235. new_blindings_inuse[rsa->num_blindings] = 1;
  236. *index_used = rsa->num_blindings;
  237. if (rsa->blindings != NULL) {
  238. OPENSSL_free(rsa->blindings);
  239. }
  240. rsa->blindings = new_blindings;
  241. if (rsa->blindings_inuse != NULL) {
  242. OPENSSL_free(rsa->blindings_inuse);
  243. }
  244. rsa->blindings_inuse = new_blindings_inuse;
  245. rsa->num_blindings++;
  246. CRYPTO_MUTEX_unlock(&rsa->lock);
  247. return ret;
  248. err2:
  249. OPENSSL_free(new_blindings);
  250. err1:
  251. CRYPTO_MUTEX_unlock(&rsa->lock);
  252. BN_BLINDING_free(ret);
  253. return NULL;
  254. }
  255. /* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
  256. * for other threads to use. */
  257. static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
  258. unsigned blinding_index) {
  259. if (blinding_index == MAX_BLINDINGS_PER_RSA) {
  260. /* This blinding wasn't cached. */
  261. BN_BLINDING_free(blinding);
  262. return;
  263. }
  264. CRYPTO_MUTEX_lock_write(&rsa->lock);
  265. rsa->blindings_inuse[blinding_index] = 0;
  266. CRYPTO_MUTEX_unlock(&rsa->lock);
  267. }
  268. /* signing */
  269. static int sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  270. const uint8_t *in, size_t in_len, int padding) {
  271. const unsigned rsa_size = RSA_size(rsa);
  272. uint8_t *buf = NULL;
  273. int i, ret = 0;
  274. if (max_out < rsa_size) {
  275. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  276. return 0;
  277. }
  278. buf = OPENSSL_malloc(rsa_size);
  279. if (buf == NULL) {
  280. OPENSSL_PUT_ERROR(RSA, sign_raw, ERR_R_MALLOC_FAILURE);
  281. goto err;
  282. }
  283. switch (padding) {
  284. case RSA_PKCS1_PADDING:
  285. i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
  286. break;
  287. case RSA_NO_PADDING:
  288. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  289. break;
  290. default:
  291. OPENSSL_PUT_ERROR(RSA, sign_raw, RSA_R_UNKNOWN_PADDING_TYPE);
  292. goto err;
  293. }
  294. if (i <= 0) {
  295. goto err;
  296. }
  297. if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
  298. goto err;
  299. }
  300. *out_len = rsa_size;
  301. ret = 1;
  302. err:
  303. if (buf != NULL) {
  304. OPENSSL_cleanse(buf, rsa_size);
  305. OPENSSL_free(buf);
  306. }
  307. return ret;
  308. }
  309. static int decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  310. const uint8_t *in, size_t in_len, int padding) {
  311. const unsigned rsa_size = RSA_size(rsa);
  312. int r = -1;
  313. uint8_t *buf = NULL;
  314. int ret = 0;
  315. if (max_out < rsa_size) {
  316. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  317. return 0;
  318. }
  319. buf = OPENSSL_malloc(rsa_size);
  320. if (buf == NULL) {
  321. OPENSSL_PUT_ERROR(RSA, decrypt, ERR_R_MALLOC_FAILURE);
  322. goto err;
  323. }
  324. if (in_len != rsa_size) {
  325. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  326. goto err;
  327. }
  328. if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
  329. goto err;
  330. }
  331. switch (padding) {
  332. case RSA_PKCS1_PADDING:
  333. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  334. break;
  335. case RSA_PKCS1_OAEP_PADDING:
  336. /* Use the default parameters: SHA-1 for both hashes and no label. */
  337. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  338. NULL, 0, NULL, NULL);
  339. break;
  340. case RSA_NO_PADDING:
  341. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  342. break;
  343. default:
  344. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_UNKNOWN_PADDING_TYPE);
  345. goto err;
  346. }
  347. if (r < 0) {
  348. OPENSSL_PUT_ERROR(RSA, decrypt, RSA_R_PADDING_CHECK_FAILED);
  349. } else {
  350. *out_len = r;
  351. ret = 1;
  352. }
  353. err:
  354. if (buf != NULL) {
  355. OPENSSL_cleanse(buf, rsa_size);
  356. OPENSSL_free(buf);
  357. }
  358. return ret;
  359. }
  360. static int verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  361. const uint8_t *in, size_t in_len, int padding) {
  362. const unsigned rsa_size = RSA_size(rsa);
  363. BIGNUM *f, *result;
  364. int ret = 0;
  365. int r = -1;
  366. uint8_t *buf = NULL;
  367. BN_CTX *ctx = NULL;
  368. if (BN_num_bits(rsa->n) > OPENSSL_RSA_MAX_MODULUS_BITS) {
  369. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_MODULUS_TOO_LARGE);
  370. return 0;
  371. }
  372. if (BN_ucmp(rsa->n, rsa->e) <= 0) {
  373. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
  374. return 0;
  375. }
  376. if (max_out < rsa_size) {
  377. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  378. return 0;
  379. }
  380. /* for large moduli, enforce exponent limit */
  381. if (BN_num_bits(rsa->n) > OPENSSL_RSA_SMALL_MODULUS_BITS &&
  382. BN_num_bits(rsa->e) > OPENSSL_RSA_MAX_PUBEXP_BITS) {
  383. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_BAD_E_VALUE);
  384. return 0;
  385. }
  386. ctx = BN_CTX_new();
  387. if (ctx == NULL) {
  388. goto err;
  389. }
  390. BN_CTX_start(ctx);
  391. f = BN_CTX_get(ctx);
  392. result = BN_CTX_get(ctx);
  393. buf = OPENSSL_malloc(rsa_size);
  394. if (!f || !result || !buf) {
  395. OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_MALLOC_FAILURE);
  396. goto err;
  397. }
  398. if (in_len != rsa_size) {
  399. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  400. goto err;
  401. }
  402. if (BN_bin2bn(in, in_len, f) == NULL) {
  403. goto err;
  404. }
  405. if (BN_ucmp(f, rsa->n) >= 0) {
  406. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  407. goto err;
  408. }
  409. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  410. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
  411. NULL) {
  412. goto err;
  413. }
  414. }
  415. if (!rsa->meth->bn_mod_exp(result, f, rsa->e, rsa->n, ctx,
  416. rsa->_method_mod_n)) {
  417. goto err;
  418. }
  419. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  420. OPENSSL_PUT_ERROR(RSA, verify_raw, ERR_R_INTERNAL_ERROR);
  421. goto err;
  422. }
  423. switch (padding) {
  424. case RSA_PKCS1_PADDING:
  425. r = RSA_padding_check_PKCS1_type_1(out, rsa_size, buf, rsa_size);
  426. break;
  427. case RSA_NO_PADDING:
  428. r = RSA_padding_check_none(out, rsa_size, buf, rsa_size);
  429. break;
  430. default:
  431. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_UNKNOWN_PADDING_TYPE);
  432. goto err;
  433. }
  434. if (r < 0) {
  435. OPENSSL_PUT_ERROR(RSA, verify_raw, RSA_R_PADDING_CHECK_FAILED);
  436. } else {
  437. *out_len = r;
  438. ret = 1;
  439. }
  440. err:
  441. if (ctx != NULL) {
  442. BN_CTX_end(ctx);
  443. BN_CTX_free(ctx);
  444. }
  445. if (buf != NULL) {
  446. OPENSSL_cleanse(buf, rsa_size);
  447. OPENSSL_free(buf);
  448. }
  449. return ret;
  450. }
  451. static int private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  452. size_t len) {
  453. BIGNUM *f, *result;
  454. BN_CTX *ctx = NULL;
  455. unsigned blinding_index = 0;
  456. BN_BLINDING *blinding = NULL;
  457. int ret = 0;
  458. ctx = BN_CTX_new();
  459. if (ctx == NULL) {
  460. goto err;
  461. }
  462. BN_CTX_start(ctx);
  463. f = BN_CTX_get(ctx);
  464. result = BN_CTX_get(ctx);
  465. if (f == NULL || result == NULL) {
  466. OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_MALLOC_FAILURE);
  467. goto err;
  468. }
  469. if (BN_bin2bn(in, len, f) == NULL) {
  470. goto err;
  471. }
  472. if (BN_ucmp(f, rsa->n) >= 0) {
  473. /* Usually the padding functions would catch this. */
  474. OPENSSL_PUT_ERROR(RSA, private_transform, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  475. goto err;
  476. }
  477. if (!(rsa->flags & RSA_FLAG_NO_BLINDING)) {
  478. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  479. if (blinding == NULL) {
  480. OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_INTERNAL_ERROR);
  481. goto err;
  482. }
  483. if (!BN_BLINDING_convert_ex(f, NULL, blinding, ctx)) {
  484. goto err;
  485. }
  486. }
  487. if ((rsa->flags & RSA_FLAG_EXT_PKEY) ||
  488. ((rsa->p != NULL) && (rsa->q != NULL) && (rsa->dmp1 != NULL) &&
  489. (rsa->dmq1 != NULL) && (rsa->iqmp != NULL))) {
  490. if (!rsa->meth->mod_exp(result, f, rsa, ctx)) {
  491. goto err;
  492. }
  493. } else {
  494. BIGNUM local_d;
  495. BIGNUM *d = NULL;
  496. BN_init(&local_d);
  497. d = &local_d;
  498. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  499. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  500. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n,
  501. ctx) == NULL) {
  502. goto err;
  503. }
  504. }
  505. if (!rsa->meth->bn_mod_exp(result, f, d, rsa->n, ctx, rsa->_method_mod_n)) {
  506. goto err;
  507. }
  508. }
  509. if (blinding) {
  510. if (!BN_BLINDING_invert_ex(result, NULL, blinding, ctx)) {
  511. goto err;
  512. }
  513. }
  514. if (!BN_bn2bin_padded(out, len, result)) {
  515. OPENSSL_PUT_ERROR(RSA, private_transform, ERR_R_INTERNAL_ERROR);
  516. goto err;
  517. }
  518. ret = 1;
  519. err:
  520. if (ctx != NULL) {
  521. BN_CTX_end(ctx);
  522. BN_CTX_free(ctx);
  523. }
  524. if (blinding != NULL) {
  525. rsa_blinding_release(rsa, blinding, blinding_index);
  526. }
  527. return ret;
  528. }
  529. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  530. BIGNUM *r1, *m1, *vrfy;
  531. BIGNUM local_dmp1, local_dmq1, local_c, local_r1;
  532. BIGNUM *dmp1, *dmq1, *c, *pr1;
  533. int ret = 0;
  534. BN_CTX_start(ctx);
  535. r1 = BN_CTX_get(ctx);
  536. m1 = BN_CTX_get(ctx);
  537. vrfy = BN_CTX_get(ctx);
  538. {
  539. BIGNUM local_p, local_q;
  540. BIGNUM *p = NULL, *q = NULL;
  541. /* Make sure BN_mod_inverse in Montgomery intialization uses the
  542. * BN_FLG_CONSTTIME flag (unless RSA_FLAG_NO_CONSTTIME is set) */
  543. BN_init(&local_p);
  544. p = &local_p;
  545. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  546. BN_init(&local_q);
  547. q = &local_q;
  548. BN_with_flags(q, rsa->q, BN_FLG_CONSTTIME);
  549. if (rsa->flags & RSA_FLAG_CACHE_PRIVATE) {
  550. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_p, &rsa->lock, p, ctx) ==
  551. NULL) {
  552. goto err;
  553. }
  554. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_q, &rsa->lock, q, ctx) ==
  555. NULL) {
  556. goto err;
  557. }
  558. }
  559. }
  560. if (rsa->flags & RSA_FLAG_CACHE_PUBLIC) {
  561. if (BN_MONT_CTX_set_locked(&rsa->_method_mod_n, &rsa->lock, rsa->n, ctx) ==
  562. NULL) {
  563. goto err;
  564. }
  565. }
  566. /* compute I mod q */
  567. c = &local_c;
  568. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  569. if (!BN_mod(r1, c, rsa->q, ctx)) {
  570. goto err;
  571. }
  572. /* compute r1^dmq1 mod q */
  573. dmq1 = &local_dmq1;
  574. BN_with_flags(dmq1, rsa->dmq1, BN_FLG_CONSTTIME);
  575. if (!rsa->meth->bn_mod_exp(m1, r1, dmq1, rsa->q, ctx, rsa->_method_mod_q)) {
  576. goto err;
  577. }
  578. /* compute I mod p */
  579. c = &local_c;
  580. BN_with_flags(c, I, BN_FLG_CONSTTIME);
  581. if (!BN_mod(r1, c, rsa->p, ctx)) {
  582. goto err;
  583. }
  584. /* compute r1^dmp1 mod p */
  585. dmp1 = &local_dmp1;
  586. BN_with_flags(dmp1, rsa->dmp1, BN_FLG_CONSTTIME);
  587. if (!rsa->meth->bn_mod_exp(r0, r1, dmp1, rsa->p, ctx, rsa->_method_mod_p)) {
  588. goto err;
  589. }
  590. if (!BN_sub(r0, r0, m1)) {
  591. goto err;
  592. }
  593. /* This will help stop the size of r0 increasing, which does
  594. * affect the multiply if it optimised for a power of 2 size */
  595. if (BN_is_negative(r0)) {
  596. if (!BN_add(r0, r0, rsa->p)) {
  597. goto err;
  598. }
  599. }
  600. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  601. goto err;
  602. }
  603. /* Turn BN_FLG_CONSTTIME flag on before division operation */
  604. pr1 = &local_r1;
  605. BN_with_flags(pr1, r1, BN_FLG_CONSTTIME);
  606. if (!BN_mod(r0, pr1, rsa->p, ctx)) {
  607. goto err;
  608. }
  609. /* If p < q it is occasionally possible for the correction of
  610. * adding 'p' if r0 is negative above to leave the result still
  611. * negative. This can break the private key operations: the following
  612. * second correction should *always* correct this rare occurrence.
  613. * This will *never* happen with OpenSSL generated keys because
  614. * they ensure p > q [steve] */
  615. if (BN_is_negative(r0)) {
  616. if (!BN_add(r0, r0, rsa->p)) {
  617. goto err;
  618. }
  619. }
  620. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  621. goto err;
  622. }
  623. if (!BN_add(r0, r1, m1)) {
  624. goto err;
  625. }
  626. if (rsa->e && rsa->n) {
  627. if (!rsa->meth->bn_mod_exp(vrfy, r0, rsa->e, rsa->n, ctx,
  628. rsa->_method_mod_n)) {
  629. goto err;
  630. }
  631. /* If 'I' was greater than (or equal to) rsa->n, the operation
  632. * will be equivalent to using 'I mod n'. However, the result of
  633. * the verify will *always* be less than 'n' so we don't check
  634. * for absolute equality, just congruency. */
  635. if (!BN_sub(vrfy, vrfy, I)) {
  636. goto err;
  637. }
  638. if (!BN_mod(vrfy, vrfy, rsa->n, ctx)) {
  639. goto err;
  640. }
  641. if (BN_is_negative(vrfy)) {
  642. if (!BN_add(vrfy, vrfy, rsa->n)) {
  643. goto err;
  644. }
  645. }
  646. if (!BN_is_zero(vrfy)) {
  647. /* 'I' and 'vrfy' aren't congruent mod n. Don't leak
  648. * miscalculated CRT output, just do a raw (slower)
  649. * mod_exp and return that instead. */
  650. BIGNUM local_d;
  651. BIGNUM *d = NULL;
  652. d = &local_d;
  653. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  654. if (!rsa->meth->bn_mod_exp(r0, I, d, rsa->n, ctx, rsa->_method_mod_n)) {
  655. goto err;
  656. }
  657. }
  658. }
  659. ret = 1;
  660. err:
  661. BN_CTX_end(ctx);
  662. return ret;
  663. }
  664. static int keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  665. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  666. BIGNUM local_r0, local_d, local_p;
  667. BIGNUM *pr0, *d, *p;
  668. int bitsp, bitsq, ok = -1, n = 0;
  669. BN_CTX *ctx = NULL;
  670. ctx = BN_CTX_new();
  671. if (ctx == NULL) {
  672. goto err;
  673. }
  674. BN_CTX_start(ctx);
  675. r0 = BN_CTX_get(ctx);
  676. r1 = BN_CTX_get(ctx);
  677. r2 = BN_CTX_get(ctx);
  678. r3 = BN_CTX_get(ctx);
  679. if (r3 == NULL) {
  680. goto err;
  681. }
  682. bitsp = (bits + 1) / 2;
  683. bitsq = bits - bitsp;
  684. /* We need the RSA components non-NULL */
  685. if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
  686. goto err;
  687. }
  688. if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
  689. goto err;
  690. }
  691. if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
  692. goto err;
  693. }
  694. if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
  695. goto err;
  696. }
  697. if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
  698. goto err;
  699. }
  700. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
  701. goto err;
  702. }
  703. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
  704. goto err;
  705. }
  706. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
  707. goto err;
  708. }
  709. BN_copy(rsa->e, e_value);
  710. /* generate p and q */
  711. for (;;) {
  712. if (!BN_generate_prime_ex(rsa->p, bitsp, 0, NULL, NULL, cb) ||
  713. !BN_sub(r2, rsa->p, BN_value_one()) ||
  714. !BN_gcd(r1, r2, rsa->e, ctx)) {
  715. goto err;
  716. }
  717. if (BN_is_one(r1)) {
  718. break;
  719. }
  720. if (!BN_GENCB_call(cb, 2, n++)) {
  721. goto err;
  722. }
  723. }
  724. if (!BN_GENCB_call(cb, 3, 0)) {
  725. goto err;
  726. }
  727. for (;;) {
  728. /* When generating ridiculously small keys, we can get stuck
  729. * continually regenerating the same prime values. Check for
  730. * this and bail if it happens 3 times. */
  731. unsigned int degenerate = 0;
  732. do {
  733. if (!BN_generate_prime_ex(rsa->q, bitsq, 0, NULL, NULL, cb)) {
  734. goto err;
  735. }
  736. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  737. if (degenerate == 3) {
  738. ok = 0; /* we set our own err */
  739. OPENSSL_PUT_ERROR(RSA, keygen, RSA_R_KEY_SIZE_TOO_SMALL);
  740. goto err;
  741. }
  742. if (!BN_sub(r2, rsa->q, BN_value_one()) ||
  743. !BN_gcd(r1, r2, rsa->e, ctx)) {
  744. goto err;
  745. }
  746. if (BN_is_one(r1)) {
  747. break;
  748. }
  749. if (!BN_GENCB_call(cb, 2, n++)) {
  750. goto err;
  751. }
  752. }
  753. if (!BN_GENCB_call(cb, 3, 1)) {
  754. goto err;
  755. }
  756. if (BN_cmp(rsa->p, rsa->q) < 0) {
  757. tmp = rsa->p;
  758. rsa->p = rsa->q;
  759. rsa->q = tmp;
  760. }
  761. /* calculate n */
  762. if (!BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
  763. goto err;
  764. }
  765. /* calculate d */
  766. if (!BN_sub(r1, rsa->p, BN_value_one())) {
  767. goto err; /* p-1 */
  768. }
  769. if (!BN_sub(r2, rsa->q, BN_value_one())) {
  770. goto err; /* q-1 */
  771. }
  772. if (!BN_mul(r0, r1, r2, ctx)) {
  773. goto err; /* (p-1)(q-1) */
  774. }
  775. pr0 = &local_r0;
  776. BN_with_flags(pr0, r0, BN_FLG_CONSTTIME);
  777. if (!BN_mod_inverse(rsa->d, rsa->e, pr0, ctx)) {
  778. goto err; /* d */
  779. }
  780. /* set up d for correct BN_FLG_CONSTTIME flag */
  781. d = &local_d;
  782. BN_with_flags(d, rsa->d, BN_FLG_CONSTTIME);
  783. /* calculate d mod (p-1) */
  784. if (!BN_mod(rsa->dmp1, d, r1, ctx)) {
  785. goto err;
  786. }
  787. /* calculate d mod (q-1) */
  788. if (!BN_mod(rsa->dmq1, d, r2, ctx)) {
  789. goto err;
  790. }
  791. /* calculate inverse of q mod p */
  792. p = &local_p;
  793. BN_with_flags(p, rsa->p, BN_FLG_CONSTTIME);
  794. if (!BN_mod_inverse(rsa->iqmp, rsa->q, p, ctx)) {
  795. goto err;
  796. }
  797. ok = 1;
  798. err:
  799. if (ok == -1) {
  800. OPENSSL_PUT_ERROR(RSA, keygen, ERR_LIB_BN);
  801. ok = 0;
  802. }
  803. if (ctx != NULL) {
  804. BN_CTX_end(ctx);
  805. BN_CTX_free(ctx);
  806. }
  807. return ok;
  808. }
  809. const struct rsa_meth_st RSA_default_method = {
  810. {
  811. 0 /* references */,
  812. 1 /* is_static */,
  813. },
  814. NULL /* app_data */,
  815. NULL /* init */,
  816. finish,
  817. size,
  818. NULL /* sign */,
  819. NULL /* verify */,
  820. encrypt,
  821. sign_raw,
  822. decrypt,
  823. verify_raw,
  824. private_transform,
  825. mod_exp /* mod_exp */,
  826. BN_mod_exp_mont /* bn_mod_exp */,
  827. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  828. keygen,
  829. };