2
0

rsa_lib.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. /*
  2. * Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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 <openssl/core_names.h>
  12. #include <openssl/engine.h>
  13. #include <openssl/evp.h>
  14. #include "internal/cryptlib.h"
  15. #include "internal/refcount.h"
  16. #include "crypto/bn.h"
  17. #include "crypto/evp.h"
  18. #include "crypto/rsa.h"
  19. #include "rsa_local.h"
  20. RSA *RSA_new(void)
  21. {
  22. return RSA_new_method(NULL);
  23. }
  24. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  25. {
  26. return rsa->meth;
  27. }
  28. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  29. {
  30. /*
  31. * NB: The caller is specifically setting a method, so it's not up to us
  32. * to deal with which ENGINE it comes from.
  33. */
  34. const RSA_METHOD *mtmp;
  35. mtmp = rsa->meth;
  36. if (mtmp->finish)
  37. mtmp->finish(rsa);
  38. #ifndef OPENSSL_NO_ENGINE
  39. ENGINE_finish(rsa->engine);
  40. rsa->engine = NULL;
  41. #endif
  42. rsa->meth = meth;
  43. if (meth->init)
  44. meth->init(rsa);
  45. return 1;
  46. }
  47. RSA *RSA_new_method(ENGINE *engine)
  48. {
  49. RSA *ret = OPENSSL_zalloc(sizeof(*ret));
  50. if (ret == NULL) {
  51. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  52. return NULL;
  53. }
  54. ret->references = 1;
  55. ret->lock = CRYPTO_THREAD_lock_new();
  56. if (ret->lock == NULL) {
  57. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_MALLOC_FAILURE);
  58. OPENSSL_free(ret);
  59. return NULL;
  60. }
  61. ret->meth = RSA_get_default_method();
  62. #ifndef OPENSSL_NO_ENGINE
  63. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  64. if (engine) {
  65. if (!ENGINE_init(engine)) {
  66. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  67. goto err;
  68. }
  69. ret->engine = engine;
  70. } else {
  71. ret->engine = ENGINE_get_default_RSA();
  72. }
  73. if (ret->engine) {
  74. ret->meth = ENGINE_get_RSA(ret->engine);
  75. if (ret->meth == NULL) {
  76. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_ENGINE_LIB);
  77. goto err;
  78. }
  79. }
  80. #endif
  81. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  82. #ifndef FIPS_MODE
  83. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
  84. goto err;
  85. }
  86. #endif
  87. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  88. RSAerr(RSA_F_RSA_NEW_METHOD, ERR_R_INIT_FAIL);
  89. goto err;
  90. }
  91. return ret;
  92. err:
  93. RSA_free(ret);
  94. return NULL;
  95. }
  96. void RSA_free(RSA *r)
  97. {
  98. int i;
  99. if (r == NULL)
  100. return;
  101. CRYPTO_DOWN_REF(&r->references, &i, r->lock);
  102. REF_PRINT_COUNT("RSA", r);
  103. if (i > 0)
  104. return;
  105. REF_ASSERT_ISNT(i < 0);
  106. if (r->meth != NULL && r->meth->finish != NULL)
  107. r->meth->finish(r);
  108. #ifndef OPENSSL_NO_ENGINE
  109. ENGINE_finish(r->engine);
  110. #endif
  111. #ifndef FIPS_MODE
  112. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  113. #endif
  114. CRYPTO_THREAD_lock_free(r->lock);
  115. BN_free(r->n);
  116. BN_free(r->e);
  117. BN_clear_free(r->d);
  118. BN_clear_free(r->p);
  119. BN_clear_free(r->q);
  120. BN_clear_free(r->dmp1);
  121. BN_clear_free(r->dmq1);
  122. BN_clear_free(r->iqmp);
  123. RSA_PSS_PARAMS_free(r->pss);
  124. sk_RSA_PRIME_INFO_pop_free(r->prime_infos, rsa_multip_info_free);
  125. BN_BLINDING_free(r->blinding);
  126. BN_BLINDING_free(r->mt_blinding);
  127. OPENSSL_free(r->bignum_data);
  128. OPENSSL_free(r);
  129. }
  130. int RSA_up_ref(RSA *r)
  131. {
  132. int i;
  133. if (CRYPTO_UP_REF(&r->references, &i, r->lock) <= 0)
  134. return 0;
  135. REF_PRINT_COUNT("RSA", r);
  136. REF_ASSERT_ISNT(i < 2);
  137. return i > 1 ? 1 : 0;
  138. }
  139. #ifndef FIPS_MODE
  140. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  141. {
  142. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  143. }
  144. void *RSA_get_ex_data(const RSA *r, int idx)
  145. {
  146. return CRYPTO_get_ex_data(&r->ex_data, idx);
  147. }
  148. #endif
  149. /*
  150. * Define a scaling constant for our fixed point arithmetic.
  151. * This value must be a power of two because the base two logarithm code
  152. * makes this assumption. The exponent must also be a multiple of three so
  153. * that the scale factor has an exact cube root. Finally, the scale factor
  154. * should not be so large that a multiplication of two scaled numbers
  155. * overflows a 64 bit unsigned integer.
  156. */
  157. static const unsigned int scale = 1 << 18;
  158. static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
  159. /* Define some constants, none exceed 32 bits */
  160. static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
  161. static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
  162. static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
  163. static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
  164. /*
  165. * Multiply two scaled integers together and rescale the result.
  166. */
  167. static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
  168. {
  169. return a * b / scale;
  170. }
  171. /*
  172. * Calculate the cube root of a 64 bit scaled integer.
  173. * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
  174. * integer, this is not guaranteed after scaling, so this function has a
  175. * 64 bit return. This uses the shifting nth root algorithm with some
  176. * algebraic simplifications.
  177. */
  178. static uint64_t icbrt64(uint64_t x)
  179. {
  180. uint64_t r = 0;
  181. uint64_t b;
  182. int s;
  183. for (s = 63; s >= 0; s -= 3) {
  184. r <<= 1;
  185. b = 3 * r * (r + 1) + 1;
  186. if ((x >> s) >= b) {
  187. x -= b << s;
  188. r++;
  189. }
  190. }
  191. return r * cbrt_scale;
  192. }
  193. /*
  194. * Calculate the natural logarithm of a 64 bit scaled integer.
  195. * This is done by calculating a base two logarithm and scaling.
  196. * The maximum logarithm (base 2) is 64 and this reduces base e, so
  197. * a 32 bit result should not overflow. The argument passed must be
  198. * greater than unity so we don't need to handle negative results.
  199. */
  200. static uint32_t ilog_e(uint64_t v)
  201. {
  202. uint32_t i, r = 0;
  203. /*
  204. * Scale down the value into the range 1 .. 2.
  205. *
  206. * If fractional numbers need to be processed, another loop needs
  207. * to go here that checks v < scale and if so multiplies it by 2 and
  208. * reduces r by scale. This also means making r signed.
  209. */
  210. while (v >= 2 * scale) {
  211. v >>= 1;
  212. r += scale;
  213. }
  214. for (i = scale / 2; i != 0; i /= 2) {
  215. v = mul2(v, v);
  216. if (v >= 2 * scale) {
  217. v >>= 1;
  218. r += i;
  219. }
  220. }
  221. r = (r * (uint64_t)scale) / log_e;
  222. return r;
  223. }
  224. /*
  225. * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
  226. * Modulus Lengths.
  227. *
  228. * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
  229. * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
  230. * The two cube roots are merged together here.
  231. */
  232. uint16_t rsa_compute_security_bits(int n)
  233. {
  234. uint64_t x;
  235. uint32_t lx;
  236. uint16_t y;
  237. /* Look for common values as listed in SP 800-56B rev 2 Appendix D */
  238. switch (n) {
  239. case 2048:
  240. return 112;
  241. case 3072:
  242. return 128;
  243. case 4096:
  244. return 152;
  245. case 6144:
  246. return 176;
  247. case 8192:
  248. return 200;
  249. }
  250. /*
  251. * The first incorrect result (i.e. not accurate or off by one low) occurs
  252. * for n = 699668. The true value here is 1200. Instead of using this n
  253. * as the check threshold, the smallest n such that the correct result is
  254. * 1200 is used instead.
  255. */
  256. if (n >= 687737)
  257. return 1200;
  258. if (n < 8)
  259. return 0;
  260. x = n * (uint64_t)log_2;
  261. lx = ilog_e(x);
  262. y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
  263. / log_2);
  264. return (y + 4) & ~7;
  265. }
  266. int RSA_security_bits(const RSA *rsa)
  267. {
  268. int bits = BN_num_bits(rsa->n);
  269. if (rsa->version == RSA_ASN1_VERSION_MULTI) {
  270. /* This ought to mean that we have private key at hand. */
  271. int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
  272. if (ex_primes <= 0 || (ex_primes + 2) > rsa_multip_cap(bits))
  273. return 0;
  274. }
  275. return rsa_compute_security_bits(bits);
  276. }
  277. int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  278. {
  279. /* If the fields n and e in r are NULL, the corresponding input
  280. * parameters MUST be non-NULL for n and e. d may be
  281. * left NULL (in case only the public key is used).
  282. */
  283. if ((r->n == NULL && n == NULL)
  284. || (r->e == NULL && e == NULL))
  285. return 0;
  286. if (n != NULL) {
  287. BN_free(r->n);
  288. r->n = n;
  289. }
  290. if (e != NULL) {
  291. BN_free(r->e);
  292. r->e = e;
  293. }
  294. if (d != NULL) {
  295. BN_clear_free(r->d);
  296. r->d = d;
  297. BN_set_flags(r->d, BN_FLG_CONSTTIME);
  298. }
  299. r->dirty_cnt++;
  300. return 1;
  301. }
  302. int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  303. {
  304. /* If the fields p and q in r are NULL, the corresponding input
  305. * parameters MUST be non-NULL.
  306. */
  307. if ((r->p == NULL && p == NULL)
  308. || (r->q == NULL && q == NULL))
  309. return 0;
  310. if (p != NULL) {
  311. BN_clear_free(r->p);
  312. r->p = p;
  313. BN_set_flags(r->p, BN_FLG_CONSTTIME);
  314. }
  315. if (q != NULL) {
  316. BN_clear_free(r->q);
  317. r->q = q;
  318. BN_set_flags(r->q, BN_FLG_CONSTTIME);
  319. }
  320. r->dirty_cnt++;
  321. return 1;
  322. }
  323. int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  324. {
  325. /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
  326. * parameters MUST be non-NULL.
  327. */
  328. if ((r->dmp1 == NULL && dmp1 == NULL)
  329. || (r->dmq1 == NULL && dmq1 == NULL)
  330. || (r->iqmp == NULL && iqmp == NULL))
  331. return 0;
  332. if (dmp1 != NULL) {
  333. BN_clear_free(r->dmp1);
  334. r->dmp1 = dmp1;
  335. BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
  336. }
  337. if (dmq1 != NULL) {
  338. BN_clear_free(r->dmq1);
  339. r->dmq1 = dmq1;
  340. BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
  341. }
  342. if (iqmp != NULL) {
  343. BN_clear_free(r->iqmp);
  344. r->iqmp = iqmp;
  345. BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
  346. }
  347. r->dirty_cnt++;
  348. return 1;
  349. }
  350. /*
  351. * Is it better to export RSA_PRIME_INFO structure
  352. * and related functions to let user pass a triplet?
  353. */
  354. int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
  355. BIGNUM *coeffs[], int pnum)
  356. {
  357. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
  358. RSA_PRIME_INFO *pinfo;
  359. int i;
  360. if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
  361. return 0;
  362. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  363. if (prime_infos == NULL)
  364. return 0;
  365. if (r->prime_infos != NULL)
  366. old = r->prime_infos;
  367. for (i = 0; i < pnum; i++) {
  368. pinfo = rsa_multip_info_new();
  369. if (pinfo == NULL)
  370. goto err;
  371. if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
  372. BN_clear_free(pinfo->r);
  373. BN_clear_free(pinfo->d);
  374. BN_clear_free(pinfo->t);
  375. pinfo->r = primes[i];
  376. pinfo->d = exps[i];
  377. pinfo->t = coeffs[i];
  378. BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
  379. BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
  380. BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
  381. } else {
  382. rsa_multip_info_free(pinfo);
  383. goto err;
  384. }
  385. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  386. }
  387. r->prime_infos = prime_infos;
  388. if (!rsa_multip_calc_product(r)) {
  389. r->prime_infos = old;
  390. goto err;
  391. }
  392. if (old != NULL) {
  393. /*
  394. * This is hard to deal with, since the old infos could
  395. * also be set by this function and r, d, t should not
  396. * be freed in that case. So currently, stay consistent
  397. * with other *set0* functions: just free it...
  398. */
  399. sk_RSA_PRIME_INFO_pop_free(old, rsa_multip_info_free);
  400. }
  401. r->version = RSA_ASN1_VERSION_MULTI;
  402. r->dirty_cnt++;
  403. return 1;
  404. err:
  405. /* r, d, t should not be freed */
  406. sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
  407. return 0;
  408. }
  409. void RSA_get0_key(const RSA *r,
  410. const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  411. {
  412. if (n != NULL)
  413. *n = r->n;
  414. if (e != NULL)
  415. *e = r->e;
  416. if (d != NULL)
  417. *d = r->d;
  418. }
  419. void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  420. {
  421. if (p != NULL)
  422. *p = r->p;
  423. if (q != NULL)
  424. *q = r->q;
  425. }
  426. int RSA_get_multi_prime_extra_count(const RSA *r)
  427. {
  428. int pnum;
  429. pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
  430. if (pnum <= 0)
  431. pnum = 0;
  432. return pnum;
  433. }
  434. int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
  435. {
  436. int pnum, i;
  437. RSA_PRIME_INFO *pinfo;
  438. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  439. return 0;
  440. /*
  441. * return other primes
  442. * it's caller's responsibility to allocate oth_primes[pnum]
  443. */
  444. for (i = 0; i < pnum; i++) {
  445. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  446. primes[i] = pinfo->r;
  447. }
  448. return 1;
  449. }
  450. void RSA_get0_crt_params(const RSA *r,
  451. const BIGNUM **dmp1, const BIGNUM **dmq1,
  452. const BIGNUM **iqmp)
  453. {
  454. if (dmp1 != NULL)
  455. *dmp1 = r->dmp1;
  456. if (dmq1 != NULL)
  457. *dmq1 = r->dmq1;
  458. if (iqmp != NULL)
  459. *iqmp = r->iqmp;
  460. }
  461. int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
  462. const BIGNUM *coeffs[])
  463. {
  464. int pnum;
  465. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  466. return 0;
  467. /* return other primes */
  468. if (exps != NULL || coeffs != NULL) {
  469. RSA_PRIME_INFO *pinfo;
  470. int i;
  471. /* it's the user's job to guarantee the buffer length */
  472. for (i = 0; i < pnum; i++) {
  473. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  474. if (exps != NULL)
  475. exps[i] = pinfo->d;
  476. if (coeffs != NULL)
  477. coeffs[i] = pinfo->t;
  478. }
  479. }
  480. return 1;
  481. }
  482. const BIGNUM *RSA_get0_n(const RSA *r)
  483. {
  484. return r->n;
  485. }
  486. const BIGNUM *RSA_get0_e(const RSA *r)
  487. {
  488. return r->e;
  489. }
  490. const BIGNUM *RSA_get0_d(const RSA *r)
  491. {
  492. return r->d;
  493. }
  494. const BIGNUM *RSA_get0_p(const RSA *r)
  495. {
  496. return r->p;
  497. }
  498. const BIGNUM *RSA_get0_q(const RSA *r)
  499. {
  500. return r->q;
  501. }
  502. const BIGNUM *RSA_get0_dmp1(const RSA *r)
  503. {
  504. return r->dmp1;
  505. }
  506. const BIGNUM *RSA_get0_dmq1(const RSA *r)
  507. {
  508. return r->dmq1;
  509. }
  510. const BIGNUM *RSA_get0_iqmp(const RSA *r)
  511. {
  512. return r->iqmp;
  513. }
  514. const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
  515. {
  516. return r->pss;
  517. }
  518. void RSA_clear_flags(RSA *r, int flags)
  519. {
  520. r->flags &= ~flags;
  521. }
  522. int RSA_test_flags(const RSA *r, int flags)
  523. {
  524. return r->flags & flags;
  525. }
  526. void RSA_set_flags(RSA *r, int flags)
  527. {
  528. r->flags |= flags;
  529. }
  530. int RSA_get_version(RSA *r)
  531. {
  532. /* { two-prime(0), multi(1) } */
  533. return r->version;
  534. }
  535. ENGINE *RSA_get0_engine(const RSA *r)
  536. {
  537. return r->engine;
  538. }
  539. int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
  540. {
  541. /* If key type not RSA or RSA-PSS return error */
  542. if (ctx != NULL && ctx->pmeth != NULL
  543. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  544. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  545. return -1;
  546. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
  547. }
  548. DEFINE_STACK_OF(BIGNUM)
  549. int rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
  550. const STACK_OF(BIGNUM) *exps,
  551. const STACK_OF(BIGNUM) *coeffs)
  552. {
  553. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
  554. int pnum;
  555. if (primes == NULL || exps == NULL || coeffs == NULL)
  556. return 0;
  557. pnum = sk_BIGNUM_num(primes);
  558. if (pnum < 2
  559. || pnum != sk_BIGNUM_num(exps)
  560. || pnum != sk_BIGNUM_num(coeffs) + 1)
  561. return 0;
  562. if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
  563. sk_BIGNUM_value(primes, 1))
  564. || !RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
  565. sk_BIGNUM_value(exps, 1),
  566. sk_BIGNUM_value(coeffs, 0)))
  567. return 0;
  568. old_infos = r->prime_infos;
  569. if (pnum > 2) {
  570. int i;
  571. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  572. if (prime_infos == NULL)
  573. return 0;
  574. for (i = 2; i < pnum; i++) {
  575. BIGNUM *prime = sk_BIGNUM_value(primes, i);
  576. BIGNUM *exp = sk_BIGNUM_value(exps, i);
  577. BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
  578. RSA_PRIME_INFO *pinfo = NULL;
  579. if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
  580. goto err;
  581. /* Using rsa_multip_info_new() is wasteful, so allocate directly */
  582. if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) {
  583. ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
  584. goto err;
  585. }
  586. pinfo->r = prime;
  587. pinfo->d = exp;
  588. pinfo->t = coeff;
  589. BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
  590. BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
  591. BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
  592. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  593. }
  594. r->prime_infos = prime_infos;
  595. if (!rsa_multip_calc_product(r)) {
  596. r->prime_infos = old_infos;
  597. goto err;
  598. }
  599. }
  600. if (old_infos != NULL) {
  601. /*
  602. * This is hard to deal with, since the old infos could
  603. * also be set by this function and r, d, t should not
  604. * be freed in that case. So currently, stay consistent
  605. * with other *set0* functions: just free it...
  606. */
  607. sk_RSA_PRIME_INFO_pop_free(old_infos, rsa_multip_info_free);
  608. }
  609. r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
  610. r->dirty_cnt++;
  611. return 1;
  612. err:
  613. /* r, d, t should not be freed */
  614. sk_RSA_PRIME_INFO_pop_free(prime_infos, rsa_multip_info_free_ex);
  615. return 0;
  616. }
  617. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  618. int rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
  619. STACK_OF(BIGNUM_const) *exps,
  620. STACK_OF(BIGNUM_const) *coeffs)
  621. {
  622. RSA_PRIME_INFO *pinfo;
  623. int i, pnum;
  624. if (r == NULL)
  625. return 0;
  626. pnum = RSA_get_multi_prime_extra_count(r);
  627. sk_BIGNUM_const_push(primes, RSA_get0_p(r));
  628. sk_BIGNUM_const_push(primes, RSA_get0_q(r));
  629. sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
  630. sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
  631. sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
  632. for (i = 0; i < pnum; i++) {
  633. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  634. sk_BIGNUM_const_push(primes, pinfo->r);
  635. sk_BIGNUM_const_push(exps, pinfo->d);
  636. sk_BIGNUM_const_push(coeffs, pinfo->t);
  637. }
  638. return 1;
  639. }
  640. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
  641. {
  642. OSSL_PARAM pad_params[2], *p = pad_params;
  643. if (ctx == NULL) {
  644. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  645. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  646. return -2;
  647. }
  648. /* If key type not RSA or RSA-PSS return error */
  649. if (ctx->pmeth != NULL
  650. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  651. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  652. return -1;
  653. /* TODO(3.0): Remove this eventually when no more legacy */
  654. if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  655. || ctx->op.ciph.ciphprovctx == NULL)
  656. return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_RSA_PADDING,
  657. pad_mode, NULL);
  658. *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, &pad_mode);
  659. *p++ = OSSL_PARAM_construct_end();
  660. return EVP_PKEY_CTX_set_params(ctx, pad_params);
  661. }
  662. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
  663. {
  664. OSSL_PARAM pad_params[2], *p = pad_params;
  665. if (ctx == NULL || pad_mode == NULL) {
  666. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  667. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  668. return -2;
  669. }
  670. /* If key type not RSA or RSA-PSS return error */
  671. if (ctx->pmeth != NULL
  672. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  673. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  674. return -1;
  675. /* TODO(3.0): Remove this eventually when no more legacy */
  676. if (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  677. || ctx->op.ciph.ciphprovctx == NULL)
  678. return EVP_PKEY_CTX_ctrl(ctx, -1, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, 0,
  679. pad_mode);
  680. *p++ = OSSL_PARAM_construct_int(OSSL_ASYM_CIPHER_PARAM_PAD_MODE, pad_mode);
  681. *p++ = OSSL_PARAM_construct_end();
  682. if (!EVP_PKEY_CTX_get_params(ctx, pad_params))
  683. return 0;
  684. return 1;
  685. }
  686. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  687. {
  688. const char *name;
  689. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  690. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  691. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  692. return -2;
  693. }
  694. /* If key type not RSA return error */
  695. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
  696. return -1;
  697. /* TODO(3.0): Remove this eventually when no more legacy */
  698. if (ctx->op.ciph.ciphprovctx == NULL)
  699. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  700. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)md);
  701. name = (md == NULL) ? "" : EVP_MD_name(md);
  702. return EVP_PKEY_CTX_set_rsa_oaep_md_name(ctx, name, NULL);
  703. }
  704. int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  705. const char *mdprops)
  706. {
  707. OSSL_PARAM rsa_params[3], *p = rsa_params;
  708. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  709. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  710. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  711. return -2;
  712. }
  713. /* If key type not RSA return error */
  714. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
  715. return -1;
  716. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  717. /*
  718. * Cast away the const. This is read
  719. * only so should be safe
  720. */
  721. (char *)mdname, 0);
  722. if (mdprops != NULL) {
  723. *p++ = OSSL_PARAM_construct_utf8_string(
  724. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS,
  725. /*
  726. * Cast away the const. This is read
  727. * only so should be safe
  728. */
  729. (char *)mdprops, 0);
  730. }
  731. *p++ = OSSL_PARAM_construct_end();
  732. return EVP_PKEY_CTX_set_params(ctx, rsa_params);
  733. }
  734. int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
  735. size_t namelen)
  736. {
  737. OSSL_PARAM rsa_params[2], *p = rsa_params;
  738. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  739. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  740. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  741. return -2;
  742. }
  743. /* If key type not RSA return error */
  744. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
  745. return -1;
  746. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  747. name, namelen);
  748. *p++ = OSSL_PARAM_construct_end();
  749. if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
  750. return -1;
  751. return 1;
  752. }
  753. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  754. {
  755. /* 80 should be big enough */
  756. char name[80] = "";
  757. if (ctx == NULL || md == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  758. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  759. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  760. return -2;
  761. }
  762. /* If key type not RSA return error */
  763. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
  764. return -1;
  765. /* TODO(3.0): Remove this eventually when no more legacy */
  766. if (ctx->op.ciph.ciphprovctx == NULL)
  767. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  768. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
  769. if (EVP_PKEY_CTX_get_rsa_oaep_md_name(ctx, name, sizeof(name)) <= 0)
  770. return -1;
  771. /* May be NULL meaning "unknown" */
  772. *md = EVP_get_digestbyname(name);
  773. return 1;
  774. }
  775. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  776. {
  777. const char *name;
  778. if (ctx == NULL
  779. || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  780. && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
  781. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  782. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  783. return -2;
  784. }
  785. /* If key type not RSA return error */
  786. if (ctx->pmeth != NULL
  787. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  788. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  789. return -1;
  790. /* TODO(3.0): Remove this eventually when no more legacy */
  791. if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  792. && ctx->op.ciph.ciphprovctx == NULL)
  793. || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  794. && ctx->op.sig.sigprovctx == NULL))
  795. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA,
  796. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  797. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)md);
  798. name = (md == NULL) ? "" : EVP_MD_name(md);
  799. return EVP_PKEY_CTX_set_rsa_mgf1_md_name(ctx, name, NULL);
  800. }
  801. int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  802. const char *mdprops)
  803. {
  804. OSSL_PARAM rsa_params[3], *p = rsa_params;
  805. if (ctx == NULL
  806. || mdname == NULL
  807. || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  808. && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
  809. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  810. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  811. return -2;
  812. }
  813. /* If key type not RSA return error */
  814. if (ctx->pmeth != NULL
  815. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  816. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  817. return -1;
  818. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
  819. /*
  820. * Cast away the const. This is read
  821. * only so should be safe
  822. */
  823. (char *)mdname, 0);
  824. if (mdprops != NULL) {
  825. *p++ = OSSL_PARAM_construct_utf8_string(
  826. OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST_PROPS,
  827. /*
  828. * Cast away the const. This is read
  829. * only so should be safe
  830. */
  831. (char *)mdprops, 0);
  832. }
  833. *p++ = OSSL_PARAM_construct_end();
  834. return EVP_PKEY_CTX_set_params(ctx, rsa_params);
  835. }
  836. int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
  837. size_t namelen)
  838. {
  839. OSSL_PARAM rsa_params[2], *p = rsa_params;
  840. if (ctx == NULL
  841. || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  842. && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
  843. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  844. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  845. return -2;
  846. }
  847. /* If key type not RSA or RSA-PSS return error */
  848. if (ctx->pmeth != NULL
  849. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  850. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  851. return -1;
  852. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_ASYM_CIPHER_PARAM_MGF1_DIGEST,
  853. name, namelen);
  854. *p++ = OSSL_PARAM_construct_end();
  855. if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
  856. return -1;
  857. return 1;
  858. }
  859. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  860. {
  861. /* 80 should be big enough */
  862. char name[80] = "";
  863. if (ctx == NULL
  864. || (!EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  865. && !EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx))) {
  866. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  867. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  868. return -2;
  869. }
  870. /* If key type not RSA or RSA-PSS return error */
  871. if (ctx->pmeth != NULL
  872. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  873. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  874. return -1;
  875. /* TODO(3.0): Remove this eventually when no more legacy */
  876. if ((EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)
  877. && ctx->op.ciph.ciphprovctx == NULL)
  878. || (EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx)
  879. && ctx->op.sig.sigprovctx == NULL))
  880. return EVP_PKEY_CTX_ctrl(ctx, -1,
  881. EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  882. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)md);
  883. if (EVP_PKEY_CTX_get_rsa_mgf1_md_name(ctx, name, sizeof(name)) <= 0)
  884. return -1;
  885. /* May be NULL meaning "unknown" */
  886. *md = EVP_get_digestbyname(name);
  887. return 1;
  888. }
  889. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
  890. {
  891. OSSL_PARAM rsa_params[2], *p = rsa_params;
  892. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  893. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  894. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  895. return -2;
  896. }
  897. /* If key type not RSA return error */
  898. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
  899. return -1;
  900. /* TODO(3.0): Remove this eventually when no more legacy */
  901. if (ctx->op.ciph.ciphprovctx == NULL)
  902. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  903. EVP_PKEY_CTRL_RSA_OAEP_LABEL, llen,
  904. (void *)label);
  905. *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  906. /*
  907. * Cast away the const. This is read
  908. * only so should be safe
  909. */
  910. (void *)label,
  911. (size_t)llen);
  912. *p++ = OSSL_PARAM_construct_end();
  913. if (!EVP_PKEY_CTX_set_params(ctx, rsa_params))
  914. return 0;
  915. OPENSSL_free(label);
  916. return 1;
  917. }
  918. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
  919. {
  920. OSSL_PARAM rsa_params[3], *p = rsa_params;
  921. size_t labellen;
  922. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  923. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  924. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  925. return -2;
  926. }
  927. /* If key type not RSA return error */
  928. if (ctx->pmeth != NULL && ctx->pmeth->pkey_id != EVP_PKEY_RSA)
  929. return -1;
  930. /* TODO(3.0): Remove this eventually when no more legacy */
  931. if (ctx->op.ciph.ciphprovctx == NULL)
  932. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  933. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, 0,
  934. (void *)label);
  935. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  936. (void **)label, 0);
  937. *p++ = OSSL_PARAM_construct_size_t(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL_LEN,
  938. &labellen);
  939. *p++ = OSSL_PARAM_construct_end();
  940. if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
  941. return -1;
  942. if (labellen > INT_MAX)
  943. return -1;
  944. return (int)labellen;
  945. }