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.
 
 
 
 
 
 

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