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.
 
 
 
 
 
 

831 lines
23 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 <limits.h>
  58. #include <string.h>
  59. #include <openssl/bn.h>
  60. #include <openssl/engine.h>
  61. #include <openssl/err.h>
  62. #include <openssl/ex_data.h>
  63. #include <openssl/mem.h>
  64. #include <openssl/nid.h>
  65. #include <openssl/thread.h>
  66. #include "internal.h"
  67. #include "../internal.h"
  68. static CRYPTO_EX_DATA_CLASS g_ex_data_class = CRYPTO_EX_DATA_CLASS_INIT;
  69. RSA *RSA_new(void) { return RSA_new_method(NULL); }
  70. RSA *RSA_new_method(const ENGINE *engine) {
  71. RSA *rsa = OPENSSL_malloc(sizeof(RSA));
  72. if (rsa == NULL) {
  73. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  74. return NULL;
  75. }
  76. memset(rsa, 0, sizeof(RSA));
  77. if (engine) {
  78. rsa->meth = ENGINE_get_RSA_method(engine);
  79. }
  80. if (rsa->meth == NULL) {
  81. rsa->meth = (RSA_METHOD*) &RSA_default_method;
  82. }
  83. METHOD_ref(rsa->meth);
  84. rsa->references = 1;
  85. rsa->flags = rsa->meth->flags;
  86. CRYPTO_MUTEX_init(&rsa->lock);
  87. CRYPTO_new_ex_data(&rsa->ex_data);
  88. if (rsa->meth->init && !rsa->meth->init(rsa)) {
  89. CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
  90. CRYPTO_MUTEX_cleanup(&rsa->lock);
  91. METHOD_unref(rsa->meth);
  92. OPENSSL_free(rsa);
  93. return NULL;
  94. }
  95. return rsa;
  96. }
  97. void RSA_additional_prime_free(RSA_additional_prime *ap) {
  98. if (ap == NULL) {
  99. return;
  100. }
  101. BN_clear_free(ap->prime);
  102. BN_clear_free(ap->exp);
  103. BN_clear_free(ap->coeff);
  104. BN_clear_free(ap->r);
  105. BN_MONT_CTX_free(ap->mont);
  106. OPENSSL_free(ap);
  107. }
  108. void RSA_free(RSA *rsa) {
  109. unsigned u;
  110. if (rsa == NULL) {
  111. return;
  112. }
  113. if (!CRYPTO_refcount_dec_and_test_zero(&rsa->references)) {
  114. return;
  115. }
  116. if (rsa->meth->finish) {
  117. rsa->meth->finish(rsa);
  118. }
  119. METHOD_unref(rsa->meth);
  120. CRYPTO_free_ex_data(&g_ex_data_class, rsa, &rsa->ex_data);
  121. BN_clear_free(rsa->n);
  122. BN_clear_free(rsa->e);
  123. BN_clear_free(rsa->d);
  124. BN_clear_free(rsa->p);
  125. BN_clear_free(rsa->q);
  126. BN_clear_free(rsa->dmp1);
  127. BN_clear_free(rsa->dmq1);
  128. BN_clear_free(rsa->iqmp);
  129. BN_MONT_CTX_free(rsa->mont_n);
  130. BN_MONT_CTX_free(rsa->mont_p);
  131. BN_MONT_CTX_free(rsa->mont_q);
  132. for (u = 0; u < rsa->num_blindings; u++) {
  133. BN_BLINDING_free(rsa->blindings[u]);
  134. }
  135. OPENSSL_free(rsa->blindings);
  136. OPENSSL_free(rsa->blindings_inuse);
  137. if (rsa->additional_primes != NULL) {
  138. sk_RSA_additional_prime_pop_free(rsa->additional_primes,
  139. RSA_additional_prime_free);
  140. }
  141. CRYPTO_MUTEX_cleanup(&rsa->lock);
  142. OPENSSL_free(rsa);
  143. }
  144. int RSA_up_ref(RSA *rsa) {
  145. CRYPTO_refcount_inc(&rsa->references);
  146. return 1;
  147. }
  148. void RSA_get0_key(const RSA *rsa, const BIGNUM **out_n, const BIGNUM **out_e,
  149. const BIGNUM **out_d) {
  150. if (out_n != NULL) {
  151. *out_n = rsa->n;
  152. }
  153. if (out_e != NULL) {
  154. *out_e = rsa->e;
  155. }
  156. if (out_d != NULL) {
  157. *out_d = rsa->d;
  158. }
  159. }
  160. void RSA_get0_factors(const RSA *rsa, const BIGNUM **out_p,
  161. const BIGNUM **out_q) {
  162. if (out_p != NULL) {
  163. *out_p = rsa->p;
  164. }
  165. if (out_q != NULL) {
  166. *out_q = rsa->q;
  167. }
  168. }
  169. void RSA_get0_crt_params(const RSA *rsa, const BIGNUM **out_dmp1,
  170. const BIGNUM **out_dmq1, const BIGNUM **out_iqmp) {
  171. if (out_dmp1 != NULL) {
  172. *out_dmp1 = rsa->dmp1;
  173. }
  174. if (out_dmq1 != NULL) {
  175. *out_dmq1 = rsa->dmq1;
  176. }
  177. if (out_iqmp != NULL) {
  178. *out_iqmp = rsa->iqmp;
  179. }
  180. }
  181. int RSA_generate_key_ex(RSA *rsa, int bits, BIGNUM *e_value, BN_GENCB *cb) {
  182. if (rsa->meth->keygen) {
  183. return rsa->meth->keygen(rsa, bits, e_value, cb);
  184. }
  185. return rsa_default_keygen(rsa, bits, e_value, cb);
  186. }
  187. int RSA_generate_multi_prime_key(RSA *rsa, int bits, int num_primes,
  188. BIGNUM *e_value, BN_GENCB *cb) {
  189. if (rsa->meth->multi_prime_keygen) {
  190. return rsa->meth->multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
  191. }
  192. return rsa_default_multi_prime_keygen(rsa, bits, num_primes, e_value, cb);
  193. }
  194. int RSA_encrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  195. const uint8_t *in, size_t in_len, int padding) {
  196. if (rsa->meth->encrypt) {
  197. return rsa->meth->encrypt(rsa, out_len, out, max_out, in, in_len, padding);
  198. }
  199. return rsa_default_encrypt(rsa, out_len, out, max_out, in, in_len, padding);
  200. }
  201. int RSA_public_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  202. int padding) {
  203. size_t out_len;
  204. if (!RSA_encrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  205. return -1;
  206. }
  207. if (out_len > INT_MAX) {
  208. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  209. return -1;
  210. }
  211. return out_len;
  212. }
  213. int RSA_sign_raw(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  214. const uint8_t *in, size_t in_len, int padding) {
  215. if (rsa->meth->sign_raw) {
  216. return rsa->meth->sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
  217. }
  218. return rsa_default_sign_raw(rsa, out_len, out, max_out, in, in_len, padding);
  219. }
  220. int RSA_private_encrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  221. int padding) {
  222. size_t out_len;
  223. if (!RSA_sign_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  224. return -1;
  225. }
  226. if (out_len > INT_MAX) {
  227. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  228. return -1;
  229. }
  230. return out_len;
  231. }
  232. int RSA_decrypt(RSA *rsa, size_t *out_len, uint8_t *out, size_t max_out,
  233. const uint8_t *in, size_t in_len, int padding) {
  234. if (rsa->meth->decrypt) {
  235. return rsa->meth->decrypt(rsa, out_len, out, max_out, in, in_len, padding);
  236. }
  237. return rsa_default_decrypt(rsa, out_len, out, max_out, in, in_len, padding);
  238. }
  239. int RSA_private_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  240. int padding) {
  241. size_t out_len;
  242. if (!RSA_decrypt(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  243. return -1;
  244. }
  245. if (out_len > INT_MAX) {
  246. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  247. return -1;
  248. }
  249. return out_len;
  250. }
  251. int RSA_public_decrypt(size_t flen, const uint8_t *from, uint8_t *to, RSA *rsa,
  252. int padding) {
  253. size_t out_len;
  254. if (!RSA_verify_raw(rsa, &out_len, to, RSA_size(rsa), from, flen, padding)) {
  255. return -1;
  256. }
  257. if (out_len > INT_MAX) {
  258. OPENSSL_PUT_ERROR(RSA, ERR_R_OVERFLOW);
  259. return -1;
  260. }
  261. return out_len;
  262. }
  263. unsigned RSA_size(const RSA *rsa) {
  264. if (rsa->meth->size) {
  265. return rsa->meth->size(rsa);
  266. }
  267. return rsa_default_size(rsa);
  268. }
  269. int RSA_is_opaque(const RSA *rsa) {
  270. return rsa->meth && (rsa->meth->flags & RSA_FLAG_OPAQUE);
  271. }
  272. int RSA_supports_digest(const RSA *rsa, const EVP_MD *md) {
  273. if (rsa->meth && rsa->meth->supports_digest) {
  274. return rsa->meth->supports_digest(rsa, md);
  275. }
  276. return 1;
  277. }
  278. int RSA_get_ex_new_index(long argl, void *argp, CRYPTO_EX_unused *unused,
  279. CRYPTO_EX_dup *dup_func, CRYPTO_EX_free *free_func) {
  280. int index;
  281. if (!CRYPTO_get_ex_new_index(&g_ex_data_class, &index, argl, argp, dup_func,
  282. free_func)) {
  283. return -1;
  284. }
  285. return index;
  286. }
  287. int RSA_set_ex_data(RSA *d, int idx, void *arg) {
  288. return CRYPTO_set_ex_data(&d->ex_data, idx, arg);
  289. }
  290. void *RSA_get_ex_data(const RSA *d, int idx) {
  291. return CRYPTO_get_ex_data(&d->ex_data, idx);
  292. }
  293. /* SSL_SIG_LENGTH is the size of an SSL/TLS (prior to TLS 1.2) signature: it's
  294. * the length of an MD5 and SHA1 hash. */
  295. static const unsigned SSL_SIG_LENGTH = 36;
  296. /* pkcs1_sig_prefix contains the ASN.1, DER encoded prefix for a hash that is
  297. * to be signed with PKCS#1. */
  298. struct pkcs1_sig_prefix {
  299. /* nid identifies the hash function. */
  300. int nid;
  301. /* len is the number of bytes of |bytes| which are valid. */
  302. uint8_t len;
  303. /* bytes contains the DER bytes. */
  304. uint8_t bytes[19];
  305. };
  306. /* kPKCS1SigPrefixes contains the ASN.1 prefixes for PKCS#1 signatures with
  307. * different hash functions. */
  308. static const struct pkcs1_sig_prefix kPKCS1SigPrefixes[] = {
  309. {
  310. NID_md5,
  311. 18,
  312. {0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d,
  313. 0x02, 0x05, 0x05, 0x00, 0x04, 0x10},
  314. },
  315. {
  316. NID_sha1,
  317. 15,
  318. {0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
  319. 0x00, 0x04, 0x14},
  320. },
  321. {
  322. NID_sha224,
  323. 19,
  324. {0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  325. 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1c},
  326. },
  327. {
  328. NID_sha256,
  329. 19,
  330. {0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  331. 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20},
  332. },
  333. {
  334. NID_sha384,
  335. 19,
  336. {0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  337. 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30},
  338. },
  339. {
  340. NID_sha512,
  341. 19,
  342. {0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03,
  343. 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40},
  344. },
  345. {
  346. NID_undef, 0, {0},
  347. },
  348. };
  349. int RSA_add_pkcs1_prefix(uint8_t **out_msg, size_t *out_msg_len,
  350. int *is_alloced, int hash_nid, const uint8_t *msg,
  351. size_t msg_len) {
  352. unsigned i;
  353. if (hash_nid == NID_md5_sha1) {
  354. /* Special case: SSL signature, just check the length. */
  355. if (msg_len != SSL_SIG_LENGTH) {
  356. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  357. return 0;
  358. }
  359. *out_msg = (uint8_t*) msg;
  360. *out_msg_len = SSL_SIG_LENGTH;
  361. *is_alloced = 0;
  362. return 1;
  363. }
  364. for (i = 0; kPKCS1SigPrefixes[i].nid != NID_undef; i++) {
  365. const struct pkcs1_sig_prefix *sig_prefix = &kPKCS1SigPrefixes[i];
  366. if (sig_prefix->nid != hash_nid) {
  367. continue;
  368. }
  369. const uint8_t* prefix = sig_prefix->bytes;
  370. unsigned prefix_len = sig_prefix->len;
  371. unsigned signed_msg_len;
  372. uint8_t *signed_msg;
  373. signed_msg_len = prefix_len + msg_len;
  374. if (signed_msg_len < prefix_len) {
  375. OPENSSL_PUT_ERROR(RSA, RSA_R_TOO_LONG);
  376. return 0;
  377. }
  378. signed_msg = OPENSSL_malloc(signed_msg_len);
  379. if (!signed_msg) {
  380. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  381. return 0;
  382. }
  383. memcpy(signed_msg, prefix, prefix_len);
  384. memcpy(signed_msg + prefix_len, msg, msg_len);
  385. *out_msg = signed_msg;
  386. *out_msg_len = signed_msg_len;
  387. *is_alloced = 1;
  388. return 1;
  389. }
  390. OPENSSL_PUT_ERROR(RSA, RSA_R_UNKNOWN_ALGORITHM_TYPE);
  391. return 0;
  392. }
  393. int RSA_sign(int hash_nid, const uint8_t *in, unsigned in_len, uint8_t *out,
  394. unsigned *out_len, RSA *rsa) {
  395. const unsigned rsa_size = RSA_size(rsa);
  396. int ret = 0;
  397. uint8_t *signed_msg;
  398. size_t signed_msg_len;
  399. int signed_msg_is_alloced = 0;
  400. size_t size_t_out_len;
  401. if (rsa->meth->sign) {
  402. return rsa->meth->sign(hash_nid, in, in_len, out, out_len, rsa);
  403. }
  404. if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
  405. &signed_msg_is_alloced, hash_nid, in, in_len)) {
  406. return 0;
  407. }
  408. if (rsa_size < RSA_PKCS1_PADDING_SIZE ||
  409. signed_msg_len > rsa_size - RSA_PKCS1_PADDING_SIZE) {
  410. OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
  411. goto finish;
  412. }
  413. if (RSA_sign_raw(rsa, &size_t_out_len, out, rsa_size, signed_msg,
  414. signed_msg_len, RSA_PKCS1_PADDING)) {
  415. *out_len = size_t_out_len;
  416. ret = 1;
  417. }
  418. finish:
  419. if (signed_msg_is_alloced) {
  420. OPENSSL_free(signed_msg);
  421. }
  422. return ret;
  423. }
  424. int RSA_verify(int hash_nid, const uint8_t *msg, size_t msg_len,
  425. const uint8_t *sig, size_t sig_len, RSA *rsa) {
  426. if (rsa->n == NULL || rsa->e == NULL) {
  427. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  428. return 0;
  429. }
  430. const size_t rsa_size = RSA_size(rsa);
  431. uint8_t *buf = NULL;
  432. int ret = 0;
  433. uint8_t *signed_msg = NULL;
  434. size_t signed_msg_len, len;
  435. int signed_msg_is_alloced = 0;
  436. if (hash_nid == NID_md5_sha1 && msg_len != SSL_SIG_LENGTH) {
  437. OPENSSL_PUT_ERROR(RSA, RSA_R_INVALID_MESSAGE_LENGTH);
  438. return 0;
  439. }
  440. buf = OPENSSL_malloc(rsa_size);
  441. if (!buf) {
  442. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  443. return 0;
  444. }
  445. if (!RSA_verify_raw(rsa, &len, buf, rsa_size, sig, sig_len,
  446. RSA_PKCS1_PADDING)) {
  447. goto out;
  448. }
  449. if (!RSA_add_pkcs1_prefix(&signed_msg, &signed_msg_len,
  450. &signed_msg_is_alloced, hash_nid, msg, msg_len)) {
  451. goto out;
  452. }
  453. if (len != signed_msg_len || memcmp(buf, signed_msg, len) != 0) {
  454. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
  455. goto out;
  456. }
  457. ret = 1;
  458. out:
  459. OPENSSL_free(buf);
  460. if (signed_msg_is_alloced) {
  461. OPENSSL_free(signed_msg);
  462. }
  463. return ret;
  464. }
  465. static void bn_free_and_null(BIGNUM **bn) {
  466. BN_free(*bn);
  467. *bn = NULL;
  468. }
  469. int RSA_check_key(const RSA *key) {
  470. BIGNUM n, pm1, qm1, lcm, gcd, de, dmp1, dmq1, iqmp_times_q;
  471. BN_CTX *ctx;
  472. int ok = 0, has_crt_values;
  473. if (RSA_is_opaque(key)) {
  474. /* Opaque keys can't be checked. */
  475. return 1;
  476. }
  477. if ((key->p != NULL) != (key->q != NULL)) {
  478. OPENSSL_PUT_ERROR(RSA, RSA_R_ONLY_ONE_OF_P_Q_GIVEN);
  479. return 0;
  480. }
  481. if (!key->n || !key->e) {
  482. OPENSSL_PUT_ERROR(RSA, RSA_R_VALUE_MISSING);
  483. return 0;
  484. }
  485. if (!key->d || !key->p) {
  486. /* For a public key, or without p and q, there's nothing that can be
  487. * checked. */
  488. return 1;
  489. }
  490. ctx = BN_CTX_new();
  491. if (ctx == NULL) {
  492. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  493. return 0;
  494. }
  495. BN_init(&n);
  496. BN_init(&pm1);
  497. BN_init(&qm1);
  498. BN_init(&lcm);
  499. BN_init(&gcd);
  500. BN_init(&de);
  501. BN_init(&dmp1);
  502. BN_init(&dmq1);
  503. BN_init(&iqmp_times_q);
  504. if (!BN_mul(&n, key->p, key->q, ctx) ||
  505. /* lcm = lcm(prime-1, for all primes) */
  506. !BN_sub(&pm1, key->p, BN_value_one()) ||
  507. !BN_sub(&qm1, key->q, BN_value_one()) ||
  508. !BN_mul(&lcm, &pm1, &qm1, ctx) ||
  509. !BN_gcd(&gcd, &pm1, &qm1, ctx)) {
  510. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  511. goto out;
  512. }
  513. size_t num_additional_primes = 0;
  514. if (key->additional_primes != NULL) {
  515. num_additional_primes = sk_RSA_additional_prime_num(key->additional_primes);
  516. }
  517. for (size_t i = 0; i < num_additional_primes; i++) {
  518. const RSA_additional_prime *ap =
  519. sk_RSA_additional_prime_value(key->additional_primes, i);
  520. if (!BN_mul(&n, &n, ap->prime, ctx) ||
  521. !BN_sub(&pm1, ap->prime, BN_value_one()) ||
  522. !BN_mul(&lcm, &lcm, &pm1, ctx) ||
  523. !BN_gcd(&gcd, &gcd, &pm1, ctx)) {
  524. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  525. goto out;
  526. }
  527. }
  528. if (!BN_div(&lcm, NULL, &lcm, &gcd, ctx) ||
  529. !BN_gcd(&gcd, &pm1, &qm1, ctx) ||
  530. /* de = d*e mod lcm(prime-1, for all primes). */
  531. !BN_mod_mul(&de, key->d, key->e, &lcm, ctx)) {
  532. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  533. goto out;
  534. }
  535. if (BN_cmp(&n, key->n) != 0) {
  536. OPENSSL_PUT_ERROR(RSA, RSA_R_N_NOT_EQUAL_P_Q);
  537. goto out;
  538. }
  539. if (!BN_is_one(&de)) {
  540. OPENSSL_PUT_ERROR(RSA, RSA_R_D_E_NOT_CONGRUENT_TO_1);
  541. goto out;
  542. }
  543. has_crt_values = key->dmp1 != NULL;
  544. if (has_crt_values != (key->dmq1 != NULL) ||
  545. has_crt_values != (key->iqmp != NULL)) {
  546. OPENSSL_PUT_ERROR(RSA, RSA_R_INCONSISTENT_SET_OF_CRT_VALUES);
  547. goto out;
  548. }
  549. if (has_crt_values && num_additional_primes == 0) {
  550. if (/* dmp1 = d mod (p-1) */
  551. !BN_mod(&dmp1, key->d, &pm1, ctx) ||
  552. /* dmq1 = d mod (q-1) */
  553. !BN_mod(&dmq1, key->d, &qm1, ctx) ||
  554. /* iqmp = q^-1 mod p */
  555. !BN_mod_mul(&iqmp_times_q, key->iqmp, key->q, key->p, ctx)) {
  556. OPENSSL_PUT_ERROR(RSA, ERR_LIB_BN);
  557. goto out;
  558. }
  559. if (BN_cmp(&dmp1, key->dmp1) != 0 ||
  560. BN_cmp(&dmq1, key->dmq1) != 0 ||
  561. BN_cmp(key->iqmp, key->p) >= 0 ||
  562. !BN_is_one(&iqmp_times_q)) {
  563. OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_VALUES_INCORRECT);
  564. goto out;
  565. }
  566. }
  567. ok = 1;
  568. out:
  569. BN_free(&n);
  570. BN_free(&pm1);
  571. BN_free(&qm1);
  572. BN_free(&lcm);
  573. BN_free(&gcd);
  574. BN_free(&de);
  575. BN_free(&dmp1);
  576. BN_free(&dmq1);
  577. BN_free(&iqmp_times_q);
  578. BN_CTX_free(ctx);
  579. return ok;
  580. }
  581. int RSA_recover_crt_params(RSA *rsa) {
  582. BN_CTX *ctx;
  583. BIGNUM *totient, *rem, *multiple, *p_plus_q, *p_minus_q;
  584. int ok = 0;
  585. if (rsa->n == NULL || rsa->e == NULL || rsa->d == NULL) {
  586. OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
  587. return 0;
  588. }
  589. if (rsa->p || rsa->q || rsa->dmp1 || rsa->dmq1 || rsa->iqmp) {
  590. OPENSSL_PUT_ERROR(RSA, RSA_R_CRT_PARAMS_ALREADY_GIVEN);
  591. return 0;
  592. }
  593. if (rsa->additional_primes != NULL) {
  594. OPENSSL_PUT_ERROR(RSA, RSA_R_CANNOT_RECOVER_MULTI_PRIME_KEY);
  595. return 0;
  596. }
  597. /* This uses the algorithm from section 9B of the RSA paper:
  598. * http://people.csail.mit.edu/rivest/Rsapaper.pdf */
  599. ctx = BN_CTX_new();
  600. if (ctx == NULL) {
  601. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  602. return 0;
  603. }
  604. BN_CTX_start(ctx);
  605. totient = BN_CTX_get(ctx);
  606. rem = BN_CTX_get(ctx);
  607. multiple = BN_CTX_get(ctx);
  608. p_plus_q = BN_CTX_get(ctx);
  609. p_minus_q = BN_CTX_get(ctx);
  610. if (totient == NULL || rem == NULL || multiple == NULL || p_plus_q == NULL ||
  611. p_minus_q == NULL) {
  612. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  613. goto err;
  614. }
  615. /* ed-1 is a small multiple of φ(n). */
  616. if (!BN_mul(totient, rsa->e, rsa->d, ctx) ||
  617. !BN_sub_word(totient, 1) ||
  618. /* φ(n) =
  619. * pq - p - q + 1 =
  620. * n - (p + q) + 1
  621. *
  622. * Thus n is a reasonable estimate for φ(n). So, (ed-1)/n will be very
  623. * close. But, when we calculate the quotient, we'll be truncating it
  624. * because we discard the remainder. Thus (ed-1)/multiple will be >= n,
  625. * which the totient cannot be. So we add one to the estimate.
  626. *
  627. * Consider ed-1 as:
  628. *
  629. * multiple * (n - (p+q) + 1) =
  630. * multiple*n - multiple*(p+q) + multiple
  631. *
  632. * When we divide by n, the first term becomes multiple and, since
  633. * multiple and p+q is tiny compared to n, the second and third terms can
  634. * be ignored. Thus I claim that subtracting one from the estimate is
  635. * sufficient. */
  636. !BN_div(multiple, NULL, totient, rsa->n, ctx) ||
  637. !BN_add_word(multiple, 1) ||
  638. !BN_div(totient, rem, totient, multiple, ctx)) {
  639. OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
  640. goto err;
  641. }
  642. if (!BN_is_zero(rem)) {
  643. OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_RSA_PARAMETERS);
  644. goto err;
  645. }
  646. rsa->p = BN_new();
  647. rsa->q = BN_new();
  648. rsa->dmp1 = BN_new();
  649. rsa->dmq1 = BN_new();
  650. rsa->iqmp = BN_new();
  651. if (rsa->p == NULL || rsa->q == NULL || rsa->dmp1 == NULL || rsa->dmq1 ==
  652. NULL || rsa->iqmp == NULL) {
  653. OPENSSL_PUT_ERROR(RSA, ERR_R_MALLOC_FAILURE);
  654. goto err;
  655. }
  656. /* φ(n) = n - (p + q) + 1 =>
  657. * n - totient + 1 = p + q */
  658. if (!BN_sub(p_plus_q, rsa->n, totient) ||
  659. !BN_add_word(p_plus_q, 1) ||
  660. /* p - q = sqrt((p+q)^2 - 4n) */
  661. !BN_sqr(rem, p_plus_q, ctx) ||
  662. !BN_lshift(multiple, rsa->n, 2) ||
  663. !BN_sub(rem, rem, multiple) ||
  664. !BN_sqrt(p_minus_q, rem, ctx) ||
  665. /* q is 1/2 (p+q)-(p-q) */
  666. !BN_sub(rsa->q, p_plus_q, p_minus_q) ||
  667. !BN_rshift1(rsa->q, rsa->q) ||
  668. !BN_div(rsa->p, NULL, rsa->n, rsa->q, ctx) ||
  669. !BN_mul(multiple, rsa->p, rsa->q, ctx)) {
  670. OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
  671. goto err;
  672. }
  673. if (BN_cmp(multiple, rsa->n) != 0) {
  674. OPENSSL_PUT_ERROR(RSA, RSA_R_INTERNAL_ERROR);
  675. goto err;
  676. }
  677. if (!BN_sub(rem, rsa->p, BN_value_one()) ||
  678. !BN_mod(rsa->dmp1, rsa->d, rem, ctx) ||
  679. !BN_sub(rem, rsa->q, BN_value_one()) ||
  680. !BN_mod(rsa->dmq1, rsa->d, rem, ctx) ||
  681. !BN_mod_inverse(rsa->iqmp, rsa->q, rsa->p, ctx)) {
  682. OPENSSL_PUT_ERROR(RSA, ERR_R_BN_LIB);
  683. goto err;
  684. }
  685. ok = 1;
  686. err:
  687. BN_CTX_end(ctx);
  688. BN_CTX_free(ctx);
  689. if (!ok) {
  690. bn_free_and_null(&rsa->p);
  691. bn_free_and_null(&rsa->q);
  692. bn_free_and_null(&rsa->dmp1);
  693. bn_free_and_null(&rsa->dmq1);
  694. bn_free_and_null(&rsa->iqmp);
  695. }
  696. return ok;
  697. }
  698. int RSA_private_transform(RSA *rsa, uint8_t *out, const uint8_t *in,
  699. size_t len) {
  700. if (rsa->meth->private_transform) {
  701. return rsa->meth->private_transform(rsa, out, in, len);
  702. }
  703. return rsa_default_private_transform(rsa, out, in, len);
  704. }
  705. int RSA_blinding_on(RSA *rsa, BN_CTX *ctx) {
  706. return 1;
  707. }