Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 
 

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