rsa.c 22 KB

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