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.
 
 
 
 
 
 

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