25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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