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.
 
 
 
 
 
 

1101 regels
30 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 <assert.h>
  58. #include <string.h>
  59. #include <openssl/bn.h>
  60. #include <openssl/err.h>
  61. #include <openssl/mem.h>
  62. #include <openssl/thread.h>
  63. #include "internal.h"
  64. #include "../bn/internal.h"
  65. #include "../internal.h"
  66. static int check_modulus_and_exponent_sizes(const RSA *rsa) {
  67. unsigned rsa_bits = BN_num_bits(rsa->n);
  68. if (rsa_bits > 16 * 1024) {
  69. OPENSSL_PUT_ERROR(RSA, RSA_R_MODULUS_TOO_LARGE);
  70. return 0;
  71. }
  72. /* Mitigate DoS attacks by limiting the exponent size. 33 bits was chosen as
  73. * the limit based on the recommendations in [1] and [2]. Windows CryptoAPI
  74. * doesn't support values larger than 32 bits [3], so it is unlikely that
  75. * exponents larger than 32 bits are being used for anything Windows commonly
  76. * does.
  77. *
  78. * [1] https://www.imperialviolet.org/2012/03/16/rsae.html
  79. * [2] https://www.imperialviolet.org/2012/03/17/rsados.html
  80. * [3] https://msdn.microsoft.com/en-us/library/aa387685(VS.85).aspx */
  81. static const unsigned kMaxExponentBits = 33;
  82. if (BN_num_bits(rsa->e) > kMaxExponentBits) {
  83. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_E_VALUE);
  84. return 0;
  85. }
  86. /* Verify |n > e|. Comparing |rsa_bits| to |kMaxExponentBits| is a small
  87. * shortcut to comparing |n| and |e| directly. In reality, |kMaxExponentBits|
  88. * is much smaller than the minimum RSA key size that any application should
  89. * accept. */
  90. if (rsa_bits <= kMaxExponentBits) {
  91. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  92. return 0;
  93. }
  94. assert(BN_ucmp(rsa->n, rsa->e) > 0);
  95. return 1;
  96. }
  97. size_t rsa_default_size(const RSA *rsa) {
  98. return BN_num_bytes(rsa->n);
  99. }
  100. int rsa_default_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  101. const uint8_t *in, size_t in_len, int padding) {
  102. const unsigned rsa_size = RSA_size(rsa);
  103. BIGNUM *f, *result;
  104. uint8_t *buf = NULL;
  105. BN_CTX *ctx = NULL;
  106. int i, ret = 0;
  107. if (max_out < rsa_size) {
  108. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  109. return 0;
  110. }
  111. if (!check_modulus_and_exponent_sizes(rsa)) {
  112. return 0;
  113. }
  114. ctx = BN_CTX_new();
  115. if (ctx == NULL) {
  116. goto err;
  117. }
  118. BN_CTX_start(ctx);
  119. f = BN_CTX_get(ctx);
  120. result = BN_CTX_get(ctx);
  121. buf = OPENSSL_malloc(rsa_size);
  122. if (!f || !result || !buf) {
  123. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  124. goto err;
  125. }
  126. switch (padding) {
  127. case RSA_PKCS1_PADDING:
  128. i = RSA_padding_add_PKCS1_type_2(buf, rsa_size, in, in_len);
  129. break;
  130. case RSA_PKCS1_OAEP_PADDING:
  131. /* Use the default parameters: SHA-1 for both hashes and no label. */
  132. i = RSA_padding_add_PKCS1_OAEP_mgf1(buf, rsa_size, in, in_len,
  133. NULL, 0, NULL, NULL);
  134. break;
  135. case RSA_NO_PADDING:
  136. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  137. break;
  138. default:
  139. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  140. goto err;
  141. }
  142. if (i <= 0) {
  143. goto err;
  144. }
  145. if (BN_bin2bn(buf, rsa_size, f) == NULL) {
  146. goto err;
  147. }
  148. if (BN_ucmp(f, rsa->n) >= 0) {
  149. /* usually the padding functions would catch this */
  150. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  151. goto err;
  152. }
  153. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
  154. !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_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, 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. assert(ctx != NULL);
  190. assert(rsa->mont_n != NULL);
  191. BN_BLINDING *ret = NULL;
  192. BN_BLINDING **new_blindings;
  193. uint8_t *new_blindings_inuse;
  194. char overflow = 0;
  195. CRYPTO_MUTEX_lock_write(&rsa->lock);
  196. unsigned i;
  197. for (i = 0; i < rsa->num_blindings; i++) {
  198. if (rsa->blindings_inuse[i] == 0) {
  199. rsa->blindings_inuse[i] = 1;
  200. ret = rsa->blindings[i];
  201. *index_used = i;
  202. break;
  203. }
  204. }
  205. if (ret != NULL) {
  206. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  207. return ret;
  208. }
  209. overflow = rsa->num_blindings >= MAX_BLINDINGS_PER_RSA;
  210. /* We didn't find a free BN_BLINDING to use so increase the length of
  211. * the arrays by one and use the newly created element. */
  212. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  213. ret = BN_BLINDING_new();
  214. if (ret == NULL) {
  215. return NULL;
  216. }
  217. if (overflow) {
  218. /* We cannot add any more cached BN_BLINDINGs so we use |ret|
  219. * and mark it for destruction in |rsa_blinding_release|. */
  220. *index_used = MAX_BLINDINGS_PER_RSA;
  221. return ret;
  222. }
  223. CRYPTO_MUTEX_lock_write(&rsa->lock);
  224. new_blindings =
  225. OPENSSL_malloc(sizeof(BN_BLINDING *) * (rsa->num_blindings + 1));
  226. if (new_blindings == NULL) {
  227. goto err1;
  228. }
  229. OPENSSL_memcpy(new_blindings, rsa->blindings,
  230. sizeof(BN_BLINDING *) * rsa->num_blindings);
  231. new_blindings[rsa->num_blindings] = ret;
  232. new_blindings_inuse = OPENSSL_malloc(rsa->num_blindings + 1);
  233. if (new_blindings_inuse == NULL) {
  234. goto err2;
  235. }
  236. OPENSSL_memcpy(new_blindings_inuse, rsa->blindings_inuse, rsa->num_blindings);
  237. new_blindings_inuse[rsa->num_blindings] = 1;
  238. *index_used = rsa->num_blindings;
  239. OPENSSL_free(rsa->blindings);
  240. rsa->blindings = new_blindings;
  241. OPENSSL_free(rsa->blindings_inuse);
  242. rsa->blindings_inuse = new_blindings_inuse;
  243. rsa->num_blindings++;
  244. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  245. return ret;
  246. err2:
  247. OPENSSL_free(new_blindings);
  248. err1:
  249. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  250. BN_BLINDING_free(ret);
  251. return NULL;
  252. }
  253. /* rsa_blinding_release marks the cached BN_BLINDING at the given index as free
  254. * for other threads to use. */
  255. static void rsa_blinding_release(RSA *rsa, BN_BLINDING *blinding,
  256. unsigned blinding_index) {
  257. if (blinding_index == MAX_BLINDINGS_PER_RSA) {
  258. /* This blinding wasn't cached. */
  259. BN_BLINDING_free(blinding);
  260. return;
  261. }
  262. CRYPTO_MUTEX_lock_write(&rsa->lock);
  263. rsa->blindings_inuse[blinding_index] = 0;
  264. CRYPTO_MUTEX_unlock_write(&rsa->lock);
  265. }
  266. /* signing */
  267. int rsa_default_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out,
  268. size_t max_out, const uint8_t *in, size_t in_len,
  269. int padding) {
  270. const unsigned rsa_size = RSA_size(rsa);
  271. uint8_t *buf = NULL;
  272. int i, ret = 0;
  273. if (max_out < rsa_size) {
  274. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  275. return 0;
  276. }
  277. buf = OPENSSL_malloc(rsa_size);
  278. if (buf == NULL) {
  279. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  280. goto err;
  281. }
  282. switch (padding) {
  283. case RSA_PKCS1_PADDING:
  284. i = RSA_padding_add_PKCS1_type_1(buf, rsa_size, in, in_len);
  285. break;
  286. case RSA_NO_PADDING:
  287. i = RSA_padding_add_none(buf, rsa_size, in, in_len);
  288. break;
  289. default:
  290. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  291. goto err;
  292. }
  293. if (i <= 0) {
  294. goto err;
  295. }
  296. if (!RSA_private_transform(rsa, out, buf, rsa_size)) {
  297. goto err;
  298. }
  299. *out_len = rsa_size;
  300. ret = 1;
  301. err:
  302. if (buf != NULL) {
  303. OPENSSL_cleanse(buf, rsa_size);
  304. OPENSSL_free(buf);
  305. }
  306. return ret;
  307. }
  308. int rsa_default_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  309. const uint8_t *in, size_t in_len, int padding) {
  310. const unsigned rsa_size = RSA_size(rsa);
  311. int r = -1;
  312. uint8_t *buf = NULL;
  313. int ret = 0;
  314. if (max_out < rsa_size) {
  315. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  316. return 0;
  317. }
  318. if (padding == RSA_NO_PADDING) {
  319. buf = out;
  320. } else {
  321. /* Allocate a temporary buffer to hold the padded plaintext. */
  322. buf = OPENSSL_malloc(rsa_size);
  323. if (buf == NULL) {
  324. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  325. goto err;
  326. }
  327. }
  328. if (in_len != rsa_size) {
  329. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  330. goto err;
  331. }
  332. if (!RSA_private_transform(rsa, buf, in, rsa_size)) {
  333. goto err;
  334. }
  335. switch (padding) {
  336. case RSA_PKCS1_PADDING:
  337. r = RSA_padding_check_PKCS1_type_2(out, rsa_size, buf, rsa_size);
  338. break;
  339. case RSA_PKCS1_OAEP_PADDING:
  340. /* Use the default parameters: SHA-1 for both hashes and no label. */
  341. r = RSA_padding_check_PKCS1_OAEP_mgf1(out, rsa_size, buf, rsa_size,
  342. NULL, 0, NULL, NULL);
  343. break;
  344. case RSA_NO_PADDING:
  345. r = rsa_size;
  346. break;
  347. default:
  348. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  349. goto err;
  350. }
  351. if (r < 0) {
  352. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  353. } else {
  354. *out_len = r;
  355. ret = 1;
  356. }
  357. err:
  358. if (padding != RSA_NO_PADDING && buf != NULL) {
  359. OPENSSL_cleanse(buf, rsa_size);
  360. OPENSSL_free(buf);
  361. }
  362. return ret;
  363. }
  364. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
  365. int RSA_verify_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  366. const uint8_t *in, size_t in_len, int padding) {
  367. if (rsa->n == NULL || rsa->e == NULL) {
  368. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  369. return 0;
  370. }
  371. const unsigned rsa_size = RSA_size(rsa);
  372. BIGNUM *f, *result;
  373. int r = -1;
  374. if (max_out < rsa_size) {
  375. OPENSSL_PUT_ERROR(RSA, RSA_R_OUTPUT_BUFFER_TOO_SMALL);
  376. return 0;
  377. }
  378. if (in_len != rsa_size) {
  379. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_LEN_NOT_EQUAL_TO_MOD_LEN);
  380. return 0;
  381. }
  382. if (!check_modulus_and_exponent_sizes(rsa)) {
  383. return 0;
  384. }
  385. BN_CTX *ctx = BN_CTX_new();
  386. if (ctx == NULL) {
  387. return 0;
  388. }
  389. int ret = 0;
  390. uint8_t *buf = NULL;
  391. BN_CTX_start(ctx);
  392. f = BN_CTX_get(ctx);
  393. result = BN_CTX_get(ctx);
  394. if (f == NULL || result == NULL) {
  395. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  396. goto err;
  397. }
  398. if (padding == RSA_NO_PADDING) {
  399. buf = out;
  400. } else {
  401. /* Allocate a temporary buffer to hold the padded plaintext. */
  402. buf = OPENSSL_malloc(rsa_size);
  403. if (buf == NULL) {
  404. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  405. goto err;
  406. }
  407. }
  408. if (BN_bin2bn(in, in_len, f) == NULL) {
  409. goto err;
  410. }
  411. if (BN_ucmp(f, rsa->n) >= 0) {
  412. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  413. goto err;
  414. }
  415. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx) ||
  416. !BN_mod_exp_mont(result, f, rsa->e, rsa->n, ctx, rsa->mont_n)) {
  417. goto err;
  418. }
  419. if (!BN_bn2bin_padded(buf, rsa_size, result)) {
  420. OPENSSL_PUT_ERROR(RSA, 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_size;
  429. break;
  430. default:
  431. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_PADDING_TYPE);
  432. goto err;
  433. }
  434. if (r < 0) {
  435. OPENSSL_PUT_ERROR(RSA, RSA_R_PADDING_CHECK_FAILED);
  436. } else {
  437. *out_len = r;
  438. ret = 1;
  439. }
  440. err:
  441. BN_CTX_end(ctx);
  442. BN_CTX_free(ctx);
  443. if (buf != out) {
  444. OPENSSL_free(buf);
  445. }
  446. return ret;
  447. }
  448. int rsa_default_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  449. size_t len) {
  450. BIGNUM *f, *result;
  451. BN_CTX *ctx = NULL;
  452. unsigned blinding_index = 0;
  453. BN_BLINDING *blinding = NULL;
  454. int ret = 0;
  455. ctx = BN_CTX_new();
  456. if (ctx == NULL) {
  457. goto err;
  458. }
  459. BN_CTX_start(ctx);
  460. f = BN_CTX_get(ctx);
  461. result = BN_CTX_get(ctx);
  462. if (f == NULL || result == NULL) {
  463. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  464. goto err;
  465. }
  466. if (BN_bin2bn(in, len, f) == NULL) {
  467. goto err;
  468. }
  469. if (BN_ucmp(f, rsa->n) >= 0) {
  470. /* Usually the padding functions would catch this. */
  471. OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_MODULUS);
  472. goto err;
  473. }
  474. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
  475. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  476. goto err;
  477. }
  478. /* We cannot do blinding or verification without |e|, and continuing without
  479. * those countermeasures is dangerous. However, the Java/Android RSA API
  480. * requires support for keys where only |d| and |n| (and not |e|) are known.
  481. * The callers that require that bad behavior set |RSA_FLAG_NO_BLINDING|. */
  482. int disable_security = (rsa->flags & RSA_FLAG_NO_BLINDING) && rsa->e == NULL;
  483. if (!disable_security) {
  484. /* Keys without public exponents must have blinding explicitly disabled to
  485. * be used. */
  486. if (rsa->e == NULL) {
  487. OPENSSL_PUT_ERROR(RSA, RSA_R_NO_PUBLIC_EXPONENT);
  488. goto err;
  489. }
  490. blinding = rsa_blinding_get(rsa, &blinding_index, ctx);
  491. if (blinding == NULL) {
  492. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  493. goto err;
  494. }
  495. if (!BN_BLINDING_convert(f, blinding, rsa->e, rsa->mont_n, ctx)) {
  496. goto err;
  497. }
  498. }
  499. if (rsa->p != NULL && rsa->q != NULL && rsa->e != NULL && rsa->dmp1 != NULL &&
  500. rsa->dmq1 != NULL && rsa->iqmp != NULL) {
  501. if (!mod_exp(result, f, rsa, ctx)) {
  502. goto err;
  503. }
  504. } else if (!BN_mod_exp_mont_consttime(result, f, rsa->d, rsa->n, ctx,
  505. rsa->mont_n)) {
  506. goto err;
  507. }
  508. /* Verify the result to protect against fault attacks as described in the
  509. * 1997 paper "On the Importance of Checking Cryptographic Protocols for
  510. * Faults" by Dan Boneh, Richard A. DeMillo, and Richard J. Lipton. Some
  511. * implementations do this only when the CRT is used, but we do it in all
  512. * cases. Section 6 of the aforementioned paper describes an attack that
  513. * works when the CRT isn't used. That attack is much less likely to succeed
  514. * than the CRT attack, but there have likely been improvements since 1997.
  515. *
  516. * This check is cheap assuming |e| is small; it almost always is. */
  517. if (!disable_security) {
  518. BIGNUM *vrfy = BN_CTX_get(ctx);
  519. if (vrfy == NULL ||
  520. !BN_mod_exp_mont(vrfy, result, rsa->e, rsa->n, ctx, rsa->mont_n) ||
  521. !BN_equal_consttime(vrfy, f)) {
  522. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  523. goto err;
  524. }
  525. if (!BN_BLINDING_invert(result, blinding, rsa->mont_n, ctx)) {
  526. goto err;
  527. }
  528. }
  529. if (!BN_bn2bin_padded(out, len, result)) {
  530. OPENSSL_PUT_ERROR(RSA, ERR_R_INTERNAL_ERROR);
  531. goto err;
  532. }
  533. ret = 1;
  534. err:
  535. if (ctx != NULL) {
  536. BN_CTX_end(ctx);
  537. BN_CTX_free(ctx);
  538. }
  539. if (blinding != NULL) {
  540. rsa_blinding_release(rsa, blinding, blinding_index);
  541. }
  542. return ret;
  543. }
  544. static int mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx) {
  545. assert(ctx != NULL);
  546. assert(rsa->n != NULL);
  547. assert(rsa->e != NULL);
  548. assert(rsa->d != NULL);
  549. assert(rsa->p != NULL);
  550. assert(rsa->q != NULL);
  551. assert(rsa->dmp1 != NULL);
  552. assert(rsa->dmq1 != NULL);
  553. assert(rsa->iqmp != NULL);
  554. BIGNUM *r1, *m1, *vrfy;
  555. int ret = 0;
  556. size_t i, num_additional_primes = 0;
  557. if (rsa->additional_primes != NULL) {
  558. num_additional_primes = sk_RSA_additional_prime_num(rsa->additional_primes);
  559. }
  560. BN_CTX_start(ctx);
  561. r1 = BN_CTX_get(ctx);
  562. m1 = BN_CTX_get(ctx);
  563. vrfy = BN_CTX_get(ctx);
  564. if (r1 == NULL ||
  565. m1 == NULL ||
  566. vrfy == NULL) {
  567. goto err;
  568. }
  569. if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
  570. !BN_MONT_CTX_set_locked(&rsa->mont_q, &rsa->lock, rsa->q, ctx)) {
  571. goto err;
  572. }
  573. if (!BN_MONT_CTX_set_locked(&rsa->mont_n, &rsa->lock, rsa->n, ctx)) {
  574. goto err;
  575. }
  576. /* compute I mod q */
  577. if (!BN_mod(r1, I, rsa->q, ctx)) {
  578. goto err;
  579. }
  580. /* compute r1^dmq1 mod q */
  581. if (!BN_mod_exp_mont_consttime(m1, r1, rsa->dmq1, rsa->q, ctx, rsa->mont_q)) {
  582. goto err;
  583. }
  584. /* compute I mod p */
  585. if (!BN_mod(r1, I, rsa->p, ctx)) {
  586. goto err;
  587. }
  588. /* compute r1^dmp1 mod p */
  589. if (!BN_mod_exp_mont_consttime(r0, r1, rsa->dmp1, rsa->p, ctx, rsa->mont_p)) {
  590. goto err;
  591. }
  592. if (!BN_sub(r0, r0, m1)) {
  593. goto err;
  594. }
  595. /* This will help stop the size of r0 increasing, which does
  596. * affect the multiply if it optimised for a power of 2 size */
  597. if (BN_is_negative(r0)) {
  598. if (!BN_add(r0, r0, rsa->p)) {
  599. goto err;
  600. }
  601. }
  602. if (!BN_mul(r1, r0, rsa->iqmp, ctx)) {
  603. goto err;
  604. }
  605. if (!BN_mod(r0, r1, rsa->p, ctx)) {
  606. goto err;
  607. }
  608. /* If p < q it is occasionally possible for the correction of
  609. * adding 'p' if r0 is negative above to leave the result still
  610. * negative. This can break the private key operations: the following
  611. * second correction should *always* correct this rare occurrence.
  612. * This will *never* happen with OpenSSL generated keys because
  613. * they ensure p > q [steve] */
  614. if (BN_is_negative(r0)) {
  615. if (!BN_add(r0, r0, rsa->p)) {
  616. goto err;
  617. }
  618. }
  619. if (!BN_mul(r1, r0, rsa->q, ctx)) {
  620. goto err;
  621. }
  622. if (!BN_add(r0, r1, m1)) {
  623. goto err;
  624. }
  625. for (i = 0; i < num_additional_primes; i++) {
  626. /* multi-prime RSA. */
  627. RSA_additional_prime *ap =
  628. sk_RSA_additional_prime_value(rsa->additional_primes, i);
  629. /* c will already point to a BIGNUM with the correct flags. */
  630. if (!BN_mod(r1, I, ap->prime, ctx)) {
  631. goto err;
  632. }
  633. if (!BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, ap->prime, ctx) ||
  634. !BN_mod_exp_mont_consttime(m1, r1, ap->exp, ap->prime, ctx, ap->mont)) {
  635. goto err;
  636. }
  637. if (!BN_sub(m1, m1, r0) ||
  638. !BN_mul(m1, m1, ap->coeff, ctx) ||
  639. !BN_mod(m1, m1, ap->prime, ctx) ||
  640. (BN_is_negative(m1) && !BN_add(m1, m1, ap->prime)) ||
  641. !BN_mul(m1, m1, ap->r, ctx) ||
  642. !BN_add(r0, r0, m1)) {
  643. goto err;
  644. }
  645. }
  646. ret = 1;
  647. err:
  648. BN_CTX_end(ctx);
  649. return ret;
  650. }
  651. int rsa_default_multi_prime_keygen(RSA *rsa, int bits, int num_primes,
  652. BIGNUM *e_value, BN_GENCB *cb) {
  653. BIGNUM *r0 = NULL, *r1 = NULL, *r2 = NULL, *r3 = NULL, *tmp;
  654. int prime_bits, ok = -1, n = 0, i, j;
  655. BN_CTX *ctx = NULL;
  656. STACK_OF(RSA_additional_prime) *additional_primes = NULL;
  657. if (num_primes < 2) {
  658. ok = 0; /* we set our own err */
  659. OPENSSL_PUT_ERROR(RSA, RSA_R_MUST_HAVE_AT_LEAST_TWO_PRIMES);
  660. goto err;
  661. }
  662. ctx = BN_CTX_new();
  663. if (ctx == NULL) {
  664. goto err;
  665. }
  666. BN_CTX_start(ctx);
  667. r0 = BN_CTX_get(ctx);
  668. r1 = BN_CTX_get(ctx);
  669. r2 = BN_CTX_get(ctx);
  670. r3 = BN_CTX_get(ctx);
  671. if (r0 == NULL || r1 == NULL || r2 == NULL || r3 == NULL) {
  672. goto err;
  673. }
  674. if (num_primes > 2) {
  675. additional_primes = sk_RSA_additional_prime_new_null();
  676. if (additional_primes == NULL) {
  677. goto err;
  678. }
  679. }
  680. for (i = 2; i < num_primes; i++) {
  681. RSA_additional_prime *ap = OPENSSL_malloc(sizeof(RSA_additional_prime));
  682. if (ap == NULL) {
  683. goto err;
  684. }
  685. OPENSSL_memset(ap, 0, sizeof(RSA_additional_prime));
  686. ap->prime = BN_new();
  687. ap->exp = BN_new();
  688. ap->coeff = BN_new();
  689. ap->r = BN_new();
  690. if (ap->prime == NULL ||
  691. ap->exp == NULL ||
  692. ap->coeff == NULL ||
  693. ap->r == NULL ||
  694. !sk_RSA_additional_prime_push(additional_primes, ap)) {
  695. RSA_additional_prime_free(ap);
  696. goto err;
  697. }
  698. }
  699. /* We need the RSA components non-NULL */
  700. if (!rsa->n && ((rsa->n = BN_new()) == NULL)) {
  701. goto err;
  702. }
  703. if (!rsa->d && ((rsa->d = BN_new()) == NULL)) {
  704. goto err;
  705. }
  706. if (!rsa->e && ((rsa->e = BN_new()) == NULL)) {
  707. goto err;
  708. }
  709. if (!rsa->p && ((rsa->p = BN_new()) == NULL)) {
  710. goto err;
  711. }
  712. if (!rsa->q && ((rsa->q = BN_new()) == NULL)) {
  713. goto err;
  714. }
  715. if (!rsa->dmp1 && ((rsa->dmp1 = BN_new()) == NULL)) {
  716. goto err;
  717. }
  718. if (!rsa->dmq1 && ((rsa->dmq1 = BN_new()) == NULL)) {
  719. goto err;
  720. }
  721. if (!rsa->iqmp && ((rsa->iqmp = BN_new()) == NULL)) {
  722. goto err;
  723. }
  724. if (!BN_copy(rsa->e, e_value)) {
  725. goto err;
  726. }
  727. /* generate p and q */
  728. prime_bits = (bits + (num_primes - 1)) / num_primes;
  729. for (;;) {
  730. if (!BN_generate_prime_ex(rsa->p, prime_bits, 0, NULL, NULL, cb) ||
  731. !BN_sub(r2, rsa->p, BN_value_one()) ||
  732. !BN_gcd(r1, r2, rsa->e, ctx)) {
  733. goto err;
  734. }
  735. if (BN_is_one(r1)) {
  736. break;
  737. }
  738. if (!BN_GENCB_call(cb, 2, n++)) {
  739. goto err;
  740. }
  741. }
  742. if (!BN_GENCB_call(cb, 3, 0)) {
  743. goto err;
  744. }
  745. prime_bits = ((bits - prime_bits) + (num_primes - 2)) / (num_primes - 1);
  746. for (;;) {
  747. /* When generating ridiculously small keys, we can get stuck
  748. * continually regenerating the same prime values. Check for
  749. * this and bail if it happens 3 times. */
  750. unsigned int degenerate = 0;
  751. do {
  752. if (!BN_generate_prime_ex(rsa->q, prime_bits, 0, NULL, NULL, cb)) {
  753. goto err;
  754. }
  755. } while ((BN_cmp(rsa->p, rsa->q) == 0) && (++degenerate < 3));
  756. if (degenerate == 3) {
  757. ok = 0; /* we set our own err */
  758. OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
  759. goto err;
  760. }
  761. if (!BN_sub(r2, rsa->q, BN_value_one()) ||
  762. !BN_gcd(r1, r2, rsa->e, ctx)) {
  763. goto err;
  764. }
  765. if (BN_is_one(r1)) {
  766. break;
  767. }
  768. if (!BN_GENCB_call(cb, 2, n++)) {
  769. goto err;
  770. }
  771. }
  772. if (!BN_GENCB_call(cb, 3, 1) ||
  773. !BN_mul(rsa->n, rsa->p, rsa->q, ctx)) {
  774. goto err;
  775. }
  776. for (i = 2; i < num_primes; i++) {
  777. RSA_additional_prime *ap =
  778. sk_RSA_additional_prime_value(additional_primes, i - 2);
  779. prime_bits = ((bits - BN_num_bits(rsa->n)) + (num_primes - (i + 1))) /
  780. (num_primes - i);
  781. for (;;) {
  782. if (!BN_generate_prime_ex(ap->prime, prime_bits, 0, NULL, NULL, cb)) {
  783. goto err;
  784. }
  785. if (BN_cmp(rsa->p, ap->prime) == 0 ||
  786. BN_cmp(rsa->q, ap->prime) == 0) {
  787. continue;
  788. }
  789. for (j = 0; j < i - 2; j++) {
  790. if (BN_cmp(sk_RSA_additional_prime_value(additional_primes, j)->prime,
  791. ap->prime) == 0) {
  792. break;
  793. }
  794. }
  795. if (j != i - 2) {
  796. continue;
  797. }
  798. if (!BN_sub(r2, ap->prime, BN_value_one()) ||
  799. !BN_gcd(r1, r2, rsa->e, ctx)) {
  800. goto err;
  801. }
  802. if (!BN_is_one(r1)) {
  803. continue;
  804. }
  805. if (i != num_primes - 1) {
  806. break;
  807. }
  808. /* For the last prime we'll check that it makes n large enough. In the
  809. * two prime case this isn't a problem because we generate primes with
  810. * the top two bits set and so the product is always of the expected
  811. * size. In the multi prime case, this doesn't follow. */
  812. if (!BN_mul(r1, rsa->n, ap->prime, ctx)) {
  813. goto err;
  814. }
  815. if (BN_num_bits(r1) == (unsigned) bits) {
  816. break;
  817. }
  818. if (!BN_GENCB_call(cb, 2, n++)) {
  819. goto err;
  820. }
  821. }
  822. /* ap->r is is the product of all the primes prior to the current one
  823. * (including p and q). */
  824. if (!BN_copy(ap->r, rsa->n)) {
  825. goto err;
  826. }
  827. if (i == num_primes - 1) {
  828. /* In the case of the last prime, we calculated n as |r1| in the loop
  829. * above. */
  830. if (!BN_copy(rsa->n, r1)) {
  831. goto err;
  832. }
  833. } else if (!BN_mul(rsa->n, rsa->n, ap->prime, ctx)) {
  834. goto err;
  835. }
  836. if (!BN_GENCB_call(cb, 3, 1)) {
  837. goto err;
  838. }
  839. }
  840. if (BN_cmp(rsa->p, rsa->q) < 0) {
  841. tmp = rsa->p;
  842. rsa->p = rsa->q;
  843. rsa->q = tmp;
  844. }
  845. /* calculate d */
  846. if (!BN_sub(r1, rsa->p, BN_value_one())) {
  847. goto err; /* p-1 */
  848. }
  849. if (!BN_sub(r2, rsa->q, BN_value_one())) {
  850. goto err; /* q-1 */
  851. }
  852. if (!BN_mul(r0, r1, r2, ctx)) {
  853. goto err; /* (p-1)(q-1) */
  854. }
  855. for (i = 2; i < num_primes; i++) {
  856. RSA_additional_prime *ap =
  857. sk_RSA_additional_prime_value(additional_primes, i - 2);
  858. if (!BN_sub(r3, ap->prime, BN_value_one()) ||
  859. !BN_mul(r0, r0, r3, ctx)) {
  860. goto err;
  861. }
  862. }
  863. if (!BN_mod_inverse(rsa->d, rsa->e, r0, ctx)) {
  864. goto err; /* d */
  865. }
  866. /* calculate d mod (p-1) */
  867. if (!BN_mod(rsa->dmp1, rsa->d, r1, ctx)) {
  868. goto err;
  869. }
  870. /* calculate d mod (q-1) */
  871. if (!BN_mod(rsa->dmq1, rsa->d, r2, ctx)) {
  872. goto err;
  873. }
  874. /* Calculate inverse of q mod p. Note that although RSA key generation is far
  875. * from constant-time, |bn_mod_inverse_secret_prime| uses the same modular
  876. * exponentation logic as in RSA private key operations and, if the RSAZ-1024
  877. * code is enabled, will be optimized for common RSA prime sizes. */
  878. if (!BN_MONT_CTX_set_locked(&rsa->mont_p, &rsa->lock, rsa->p, ctx) ||
  879. !bn_mod_inverse_secret_prime(rsa->iqmp, rsa->q, rsa->p, ctx,
  880. rsa->mont_p)) {
  881. goto err;
  882. }
  883. for (i = 2; i < num_primes; i++) {
  884. RSA_additional_prime *ap =
  885. sk_RSA_additional_prime_value(additional_primes, i - 2);
  886. if (!BN_sub(ap->exp, ap->prime, BN_value_one()) ||
  887. !BN_mod(ap->exp, rsa->d, ap->exp, ctx) ||
  888. !BN_MONT_CTX_set_locked(&ap->mont, &rsa->lock, ap->prime, ctx) ||
  889. !bn_mod_inverse_secret_prime(ap->coeff, ap->r, ap->prime, ctx,
  890. ap->mont)) {
  891. goto err;
  892. }
  893. }
  894. rsa->additional_primes = additional_primes;
  895. additional_primes = NULL;
  896. /* The key generation process is complex and thus error-prone. It could be
  897. * disastrous to generate and then use a bad key so double-check that the key
  898. * makes sense. */
  899. ok = RSA_check_key(rsa);
  900. if (!ok) {
  901. OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR);
  902. }
  903. err:
  904. if (ok == -1) {
  905. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  906. ok = 0;
  907. }
  908. if (ctx != NULL) {
  909. BN_CTX_end(ctx);
  910. BN_CTX_free(ctx);
  911. }
  912. sk_RSA_additional_prime_pop_free(additional_primes,
  913. RSA_additional_prime_free);
  914. return ok;
  915. }
  916. int rsa_default_keygen(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  917. return rsa_default_multi_prime_keygen(rsa, bits, 2 /* num primes */, e_value,
  918. cb);
  919. }
  920. /* All of the methods are NULL to make it easier for the compiler/linker to drop
  921. * unused functions. The wrapper functions will select the appropriate
  922. * |rsa_default_*| implementation. */
  923. const RSA_METHOD RSA_default_method = {
  924. {
  925. 0 /* references */,
  926. 1 /* is_static */,
  927. },
  928. NULL /* app_data */,
  929. NULL /* init */,
  930. NULL /* finish (defaults to rsa_default_finish) */,
  931. NULL /* size (defaults to rsa_default_size) */,
  932. NULL /* sign */,
  933. NULL /* verify */,
  934. NULL /* encrypt (defaults to rsa_default_encrypt) */,
  935. NULL /* sign_raw (defaults to rsa_default_sign_raw) */,
  936. NULL /* decrypt (defaults to rsa_default_decrypt) */,
  937. NULL /* verify_raw (defaults to rsa_default_verify_raw) */,
  938. NULL /* private_transform (defaults to rsa_default_private_transform) */,
  939. NULL /* mod_exp (ignored) */,
  940. NULL /* bn_mod_exp (ignored) */,
  941. RSA_FLAG_CACHE_PUBLIC | RSA_FLAG_CACHE_PRIVATE,
  942. NULL /* keygen (defaults to rsa_default_keygen) */,
  943. NULL /* multi_prime_keygen (defaults to rsa_default_multi_prime_keygen) */,
  944. NULL /* supports_digest */,
  945. };