rsa_lib.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <openssl/crypto.h>
  11. #include "internal/cryptlib.h"
  12. #include "internal/refcount.h"
  13. #include "internal/bn_int.h"
  14. #include <openssl/engine.h>
  15. #include <openssl/evp.h>
  16. #include "internal/evp_int.h"
  17. #include "rsa_locl.h"
  18. RSA *RSA_new(void)
  19. {
  20. return RSA_new_method(NULL);
  21. }
  22. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  23. {
  24. return rsa->meth;
  25. }
  26. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  27. {
  28. /*
  29. * NB: The caller is specifically setting a method, so it's not up to us
  30. * to deal with which ENGINE it comes from.
  31. */
  32. const RSA_METHOD *mtmp;
  33. mtmp = rsa->meth;
  34. if (mtmp->finish)
  35. mtmp->finish(rsa);
  36. #ifndef OPENSSL_NO_ENGINE
  37. ENGINE_finish(rsa->engine);
  38. rsa->engine = NULL;
  39. #endif
  40. rsa->meth = meth;
  41. if (meth->init)
  42. meth->init(rsa);
  43. return 1;
  44. }
  45. RSA *RSA_new_method(ENGINE *engine)
  46. {
  47. RSA *ret = OPENSSL_zalloc(sizeof(*ret));
  48. if (ret == NULL) {
  49. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  50. return NULL;
  51. }
  52. ret->references = 1;
  53. ret->lock = CRYPTO_THREAD_lock_new();
  54. if (ret->lock == NULL) {
  55. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  56. OPENSSL_free(ret);
  57. return NULL;
  58. }
  59. ret->meth = RSA_get_default_method();
  60. #ifndef OPENSSL_NO_ENGINE
  61. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  62. if (engine) {
  63. if (!ENGINE_init(engine)) {
  64. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  65. goto err;
  66. }
  67. ret->engine = engine;
  68. } else {
  69. ret->engine = ENGINE_get_default_RSA();
  70. }
  71. if (ret->engine) {
  72. ret->meth = ENGINE_get_RSA(ret->engine);
  73. if (ret->meth == NULL) {
  74. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  75. goto err;
  76. }
  77. }
  78. #endif
  79. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  80. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
  81. goto err;
  82. }
  83. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  84. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
  85. goto err;
  86. }
  87. return ret;
  88. err:
  89. RSA_free(ret);
  90. return NULL;
  91. }
  92. void RSA_free(RSA *r)
  93. {
  94. int i;
  95. if (r == NULL)
  96. return;
  97. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  98. REF_PRINT_COUNT("RSA", r);
  99. if (i > 0)
  100. return;
  101. REF_ASSERT_ISNT(i < 0);
  102. if (r->meth != NULL && r->meth->finish != NULL)
  103. r->meth->finish(r);
  104. #ifndef OPENSSL_NO_ENGINE
  105. ENGINE_finish(r->engine);
  106. #endif
  107. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  108. CRYPTO_THREAD_lock_free(r->lock);
  109. BN_free(r->n);
  110. BN_free(r->e);
  111. BN_clear_free(r->d);
  112. BN_clear_free(r->p);
  113. BN_clear_free(r->q);
  114. BN_clear_free(r->dmp1);
  115. BN_clear_free(r->dmq1);
  116. BN_clear_free(r->iqmp);
  117. RSA_PSS_PARAMS_free(r->pss);
  118. sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
  119. BN_BLINDING_free(r->blinding);
  120. BN_BLINDING_free(r->mt_blinding);
  121. OPENSSL_free(r->bignum_data);
  122. OPENSSL_free(r);
  123. }
  124. int RSA_up_ref(RSA *r)
  125. {
  126. int i;
  127. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  128. return 0;
  129. REF_PRINT_COUNT("RSA", r);
  130. REF_ASSERT_ISNT(i < 2);
  131. return i > 1 ? 1 : 0;
  132. }
  133. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  134. {
  135. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  136. }
  137. void *RSA_get_ex_data(const RSA *r, int idx)
  138. {
  139. return CRYPTO_get_ex_data(&r->ex_data, idx);
  140. }
  141. /*
  142. * Define a scaling constant for our fixed point arithmetic.
  143. * This value must be a power of two because the base two logarithm code
  144. * makes this assumption. The exponent must also be a multiple of three so
  145. * that the scale factor has an exact cube root. Finally, the scale factor
  146. * should not be so large that a multiplication of two scaled numbers
  147. * overflows a 64 bit unsigned integer.
  148. */
  149. static const unsigned int scale = 1 << 18;
  150. static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
  151. /* Define some constants, none exceed 32 bits */
  152. static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
  153. static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
  154. static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
  155. static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
  156. /*
  157. * Multiply two scale integers together and rescale the result.
  158. */
  159. static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
  160. {
  161. return a * b / scale;
  162. }
  163. /*
  164. * Calculate the cube root of a 64 bit scaled integer.
  165. * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
  166. * integer, this is not guaranteed after scaling, so this function has a
  167. * 64 bit return. This uses the shifting nth root algorithm with some
  168. * algebraic simplifications.
  169. */
  170. static uint64_t icbrt64(uint64_t x)
  171. {
  172. uint64_t r = 0;
  173. uint64_t b;
  174. int s;
  175. for (s = 63; s >= 0; s -= 3) {
  176. r <<= 1;
  177. b = 3 * r * (r + 1) + 1;
  178. if ((x >> s) >= b) {
  179. x -= b << s;
  180. r++;
  181. }
  182. }
  183. return r * cbrt_scale;
  184. }
  185. /*
  186. * Calculate the natural logarithm of a 64 bit scaled integer.
  187. * This is done by calculating a base two logarithm and scaling.
  188. * The maximum logarithm (base 2) is 64 and this reduces base e, so
  189. * a 32 bit result should not overflow. The argument passed must be
  190. * greater than unity so we don't need to handle negative results.
  191. */
  192. static uint32_t ilog_e(uint64_t v)
  193. {
  194. uint32_t i, r = 0;
  195. /*
  196. * Scale down the value into the range 1 .. 2.
  197. *
  198. * If fractional numbers need to be processed, another loop needs
  199. * to go here that checks v < scale and if so multiplies it by 2 and
  200. * reduces r by scale. This also means making r signed.
  201. */
  202. while (v >= 2 * scale) {
  203. v >>= 1;
  204. r += scale;
  205. }
  206. for (i = scale / 2; i != 0; i /= 2) {
  207. v = mul2(v, v);
  208. if (v >= 2 * scale) {
  209. v >>= 1;
  210. r += i;
  211. }
  212. }
  213. r = (r * (uint64_t)scale) / log_e;
  214. return r;
  215. }
  216. /*
  217. * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
  218. * Modulus Lengths.
  219. *
  220. * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
  221. * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
  222. * The two cube roots are merged together here.
  223. */
  224. static uint16_t rsa_compute_security_bits(int n)
  225. {
  226. uint64_t x;
  227. uint32_t lx;
  228. uint16_t y;
  229. /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
  230. switch (n) {
  231. case 2048:
  232. return 112;
  233. case 3072:
  234. return 128;
  235. case 4096:
  236. return 152;
  237. case 6144:
  238. return 176;
  239. case 8192:
  240. return 200;
  241. }
  242. /*
  243. * The first incorrect result (i.e. not accurate or off by one low) occurs
  244. * for n = 699668. The true value here is 1200. Instead of using this n
  245. * as the check threshold, the smallest n such that the correct result is
  246. * 1200 is used instead.
  247. */
  248. if (n >= 687737)
  249. return 1200;
  250. if (n < 8)
  251. return 0;
  252. x = n * (uint64_t)log_2;
  253. lx = ilog_e(x);
  254. y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
  255. / log_2);
  256. return (y + 4) & ~7;
  257. }
  258. int RSA_security_bits(const RSA *rsa)
  259. {
  260. int bits = BN_num_bits(rsa->n);
  261. if (rsa->version == RSA_ASN1_VERSION_MULTI) {
  262. /* This ought to mean that we have private key at hand. */
  263. int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
  264. if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
  265. return 0;
  266. }
  267. return rsa_compute_security_bits(bits);
  268. }
  269. int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  270. {
  271. /* If the fields n and e in r are NULL, the corresponding input
  272. * parameters MUST be non-NULL for n and e. d may be
  273. * left NULL (in case only the public key is used).
  274. */
  275. if ((r->n == NULL && n == NULL)
  276. || (r->e == NULL && e == NULL))
  277. return 0;
  278. if (n != NULL) {
  279. BN_free(r->n);
  280. r->n = n;
  281. }
  282. if (e != NULL) {
  283. BN_free(r->e);
  284. r->e = e;
  285. }
  286. if (d != NULL) {
  287. BN_clear_free(r->d);
  288. r->d = d;
  289. }
  290. return 1;
  291. }
  292. int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  293. {
  294. /* If the fields p and q in r are NULL, the corresponding input
  295. * parameters MUST be non-NULL.
  296. */
  297. if ((r->p == NULL && p == NULL)
  298. || (r->q == NULL && q == NULL))
  299. return 0;
  300. if (p != NULL) {
  301. BN_clear_free(r->p);
  302. r->p = p;
  303. }
  304. if (q != NULL) {
  305. BN_clear_free(r->q);
  306. r->q = q;
  307. }
  308. return 1;
  309. }
  310. int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  311. {
  312. /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
  313. * parameters MUST be non-NULL.
  314. */
  315. if ((r->dmp1 == NULL && dmp1 == NULL)
  316. || (r->dmq1 == NULL && dmq1 == NULL)
  317. || (r->iqmp == NULL && iqmp == NULL))
  318. return 0;
  319. if (dmp1 != NULL) {
  320. BN_clear_free(r->dmp1);
  321. r->dmp1 = dmp1;
  322. }
  323. if (dmq1 != NULL) {
  324. BN_clear_free(r->dmq1);
  325. r->dmq1 = dmq1;
  326. }
  327. if (iqmp != NULL) {
  328. BN_clear_free(r->iqmp);
  329. r->iqmp = iqmp;
  330. }
  331. return 1;
  332. }
  333. /*
  334. * Is it better to export RSA_PRIME_INFO structure
  335. * and related functions to let user pass a triplet?
  336. */
  337. int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
  338. BIGNUM *coeffs[], int pnum)
  339. {
  340. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
  341. RSA_PRIME_INFO *pinfo;
  342. int i;
  343. if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
  344. return 0;
  345. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  346. if (prime_infos == NULL)
  347. return 0;
  348. if (r->prime_infos != NULL)
  349. old = r->prime_infos;
  350. for (i = 0; i < pnum; i++) {
  351. pinfo = rsa_multip_info_new();
  352. if (pinfo == NULL)
  353. goto err;
  354. if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
  355. BN_free(pinfo->r);
  356. BN_free(pinfo->d);
  357. BN_free(pinfo->t);
  358. pinfo->r = primes[i];
  359. pinfo->d = exps[i];
  360. pinfo->t = coeffs[i];
  361. } else {
  362. rsa_multip_info_free(pinfo);
  363. goto err;
  364. }
  365. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  366. }
  367. r->prime_infos = prime_infos;
  368. if (!rsa_multip_calc_product(r)) {
  369. r->prime_infos = old;
  370. goto err;
  371. }
  372. if (old != NULL) {
  373. /*
  374. * This is hard to deal with, since the old infos could
  375. * also be set by this function and r, d, t should not
  376. * be freed in that case. So currently, stay consistent
  377. * with other *set0* functions: just free it...
  378. */
  379. sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
  380. }
  381. r->version = RSA_ASN1_VERSION_MULTI;
  382. return 1;
  383. err:
  384. /* r, d, t should not be freed */
  385. sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
  386. return 0;
  387. }
  388. void RSA_get0_key(const RSA *r,
  389. const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  390. {
  391. if (n != NULL)
  392. *n = r->n;
  393. if (e != NULL)
  394. *e = r->e;
  395. if (d != NULL)
  396. *d = r->d;
  397. }
  398. void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  399. {
  400. if (p != NULL)
  401. *p = r->p;
  402. if (q != NULL)
  403. *q = r->q;
  404. }
  405. int RSA_get_multi_prime_extra_count(const RSA *r)
  406. {
  407. int pnum;
  408. pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
  409. if (pnum <= 0)
  410. pnum = 0;
  411. return pnum;
  412. }
  413. int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
  414. {
  415. int pnum, i;
  416. RSA_PRIME_INFO *pinfo;
  417. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  418. return 0;
  419. /*
  420. * return other primes
  421. * it's caller's responsibility to allocate oth_primes[pnum]
  422. */
  423. for (i = 0; i < pnum; i++) {
  424. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  425. primes[i] = pinfo->r;
  426. }
  427. return 1;
  428. }
  429. void RSA_get0_crt_params(const RSA *r,
  430. const BIGNUM **dmp1, const BIGNUM **dmq1,
  431. const BIGNUM **iqmp)
  432. {
  433. if (dmp1 != NULL)
  434. *dmp1 = r->dmp1;
  435. if (dmq1 != NULL)
  436. *dmq1 = r->dmq1;
  437. if (iqmp != NULL)
  438. *iqmp = r->iqmp;
  439. }
  440. int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
  441. const BIGNUM *coeffs[])
  442. {
  443. int pnum;
  444. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  445. return 0;
  446. /* return other primes */
  447. if (exps != NULL || coeffs != NULL) {
  448. RSA_PRIME_INFO *pinfo;
  449. int i;
  450. /* it's the user's job to guarantee the buffer length */
  451. for (i = 0; i < pnum; i++) {
  452. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  453. if (exps != NULL)
  454. exps[i] = pinfo->d;
  455. if (coeffs != NULL)
  456. coeffs[i] = pinfo->t;
  457. }
  458. }
  459. return 1;
  460. }
  461. const BIGNUM *RSA_get0_n(const RSA *r)
  462. {
  463. return r->n;
  464. }
  465. const BIGNUM *RSA_get0_e(const RSA *r)
  466. {
  467. return r->e;
  468. }
  469. const BIGNUM *RSA_get0_d(const RSA *r)
  470. {
  471. return r->d;
  472. }
  473. const BIGNUM *RSA_get0_p(const RSA *r)
  474. {
  475. return r->p;
  476. }
  477. const BIGNUM *RSA_get0_q(const RSA *r)
  478. {
  479. return r->q;
  480. }
  481. const BIGNUM *RSA_get0_dmp1(const RSA *r)
  482. {
  483. return r->dmp1;
  484. }
  485. const BIGNUM *RSA_get0_dmq1(const RSA *r)
  486. {
  487. return r->dmq1;
  488. }
  489. const BIGNUM *RSA_get0_iqmp(const RSA *r)
  490. {
  491. return r->iqmp;
  492. }
  493. void RSA_clear_flags(RSA *r, int flags)
  494. {
  495. r->flags &= ~flags;
  496. }
  497. int RSA_test_flags(const RSA *r, int flags)
  498. {
  499. return r->flags & flags;
  500. }
  501. void RSA_set_flags(RSA *r, int flags)
  502. {
  503. r->flags |= flags;
  504. }
  505. int RSA_get_version(RSA *r)
  506. {
  507. /* { two-prime(0), multi(1) } */
  508. return r->version;
  509. }
  510. ENGINE *RSA_get0_engine(const RSA *r)
  511. {
  512. return r->engine;
  513. }
  514. int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
  515. {
  516. /* If key type not RSA or RSA-PSS return error */
  517. if (ctx != NULL && ctx->pmeth != NULL
  518. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  519. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  520. return -1;
  521. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
  522. }