rsa_lib.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306
  1. /*
  2. * Copyright 1995-2023 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. /*
  10. * RSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/crypto.h>
  15. #include <openssl/core_names.h>
  16. #ifndef FIPS_MODULE
  17. # include <openssl/engine.h>
  18. #endif
  19. #include <openssl/evp.h>
  20. #include <openssl/param_build.h>
  21. #include "internal/cryptlib.h"
  22. #include "internal/refcount.h"
  23. #include "crypto/bn.h"
  24. #include "crypto/evp.h"
  25. #include "crypto/rsa.h"
  26. #include "crypto/security_bits.h"
  27. #include "rsa_local.h"
  28. static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx);
  29. #ifndef FIPS_MODULE
  30. RSA *RSA_new(void)
  31. {
  32. return rsa_new_intern(NULL, NULL);
  33. }
  34. const RSA_METHOD *RSA_get_method(const RSA *rsa)
  35. {
  36. return rsa->meth;
  37. }
  38. int RSA_set_method(RSA *rsa, const RSA_METHOD *meth)
  39. {
  40. /*
  41. * NB: The caller is specifically setting a method, so it's not up to us
  42. * to deal with which ENGINE it comes from.
  43. */
  44. const RSA_METHOD *mtmp;
  45. mtmp = rsa->meth;
  46. if (mtmp->finish)
  47. mtmp->finish(rsa);
  48. #ifndef OPENSSL_NO_ENGINE
  49. ENGINE_finish(rsa->engine);
  50. rsa->engine = NULL;
  51. #endif
  52. rsa->meth = meth;
  53. if (meth->init)
  54. meth->init(rsa);
  55. return 1;
  56. }
  57. RSA *RSA_new_method(ENGINE *engine)
  58. {
  59. return rsa_new_intern(engine, NULL);
  60. }
  61. #endif
  62. RSA *ossl_rsa_new_with_ctx(OSSL_LIB_CTX *libctx)
  63. {
  64. return rsa_new_intern(NULL, libctx);
  65. }
  66. static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx)
  67. {
  68. RSA *ret = OPENSSL_zalloc(sizeof(*ret));
  69. if (ret == NULL)
  70. return NULL;
  71. ret->lock = CRYPTO_THREAD_lock_new();
  72. if (ret->lock == NULL) {
  73. ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB);
  74. OPENSSL_free(ret);
  75. return NULL;
  76. }
  77. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  78. CRYPTO_THREAD_lock_free(ret->lock);
  79. OPENSSL_free(ret);
  80. return NULL;
  81. }
  82. ret->libctx = libctx;
  83. ret->meth = RSA_get_default_method();
  84. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  85. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  86. if (engine) {
  87. if (!ENGINE_init(engine)) {
  88. ERR_raise(ERR_LIB_RSA, ERR_R_ENGINE_LIB);
  89. goto err;
  90. }
  91. ret->engine = engine;
  92. } else {
  93. ret->engine = ENGINE_get_default_RSA();
  94. }
  95. if (ret->engine) {
  96. ret->meth = ENGINE_get_RSA(ret->engine);
  97. if (ret->meth == NULL) {
  98. ERR_raise(ERR_LIB_RSA, ERR_R_ENGINE_LIB);
  99. goto err;
  100. }
  101. }
  102. #endif
  103. ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW;
  104. #ifndef FIPS_MODULE
  105. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) {
  106. goto err;
  107. }
  108. #endif
  109. if ((ret->meth->init != NULL) && !ret->meth->init(ret)) {
  110. ERR_raise(ERR_LIB_RSA, ERR_R_INIT_FAIL);
  111. goto err;
  112. }
  113. return ret;
  114. err:
  115. RSA_free(ret);
  116. return NULL;
  117. }
  118. void RSA_free(RSA *r)
  119. {
  120. int i;
  121. if (r == NULL)
  122. return;
  123. CRYPTO_DOWN_REF(&r->references, &i);
  124. REF_PRINT_COUNT("RSA", i, r);
  125. if (i > 0)
  126. return;
  127. REF_ASSERT_ISNT(i < 0);
  128. if (r->meth != NULL && r->meth->finish != NULL)
  129. r->meth->finish(r);
  130. #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE)
  131. ENGINE_finish(r->engine);
  132. #endif
  133. #ifndef FIPS_MODULE
  134. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data);
  135. #endif
  136. CRYPTO_THREAD_lock_free(r->lock);
  137. CRYPTO_FREE_REF(&r->references);
  138. BN_free(r->n);
  139. BN_free(r->e);
  140. BN_clear_free(r->d);
  141. BN_clear_free(r->p);
  142. BN_clear_free(r->q);
  143. BN_clear_free(r->dmp1);
  144. BN_clear_free(r->dmq1);
  145. BN_clear_free(r->iqmp);
  146. #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS)
  147. ossl_rsa_acvp_test_free(r->acvp_test);
  148. #endif
  149. #ifndef FIPS_MODULE
  150. RSA_PSS_PARAMS_free(r->pss);
  151. sk_RSA_PRIME_INFO_pop_free(r->prime_infos, ossl_rsa_multip_info_free);
  152. #endif
  153. BN_BLINDING_free(r->blinding);
  154. BN_BLINDING_free(r->mt_blinding);
  155. OPENSSL_free(r);
  156. }
  157. int RSA_up_ref(RSA *r)
  158. {
  159. int i;
  160. if (CRYPTO_UP_REF(&r->references, &i) <= 0)
  161. return 0;
  162. REF_PRINT_COUNT("RSA", i, r);
  163. REF_ASSERT_ISNT(i < 2);
  164. return i > 1 ? 1 : 0;
  165. }
  166. OSSL_LIB_CTX *ossl_rsa_get0_libctx(RSA *r)
  167. {
  168. return r->libctx;
  169. }
  170. void ossl_rsa_set0_libctx(RSA *r, OSSL_LIB_CTX *libctx)
  171. {
  172. r->libctx = libctx;
  173. }
  174. #ifndef FIPS_MODULE
  175. int RSA_set_ex_data(RSA *r, int idx, void *arg)
  176. {
  177. return CRYPTO_set_ex_data(&r->ex_data, idx, arg);
  178. }
  179. void *RSA_get_ex_data(const RSA *r, int idx)
  180. {
  181. return CRYPTO_get_ex_data(&r->ex_data, idx);
  182. }
  183. #endif
  184. /*
  185. * Define a scaling constant for our fixed point arithmetic.
  186. * This value must be a power of two because the base two logarithm code
  187. * makes this assumption. The exponent must also be a multiple of three so
  188. * that the scale factor has an exact cube root. Finally, the scale factor
  189. * should not be so large that a multiplication of two scaled numbers
  190. * overflows a 64 bit unsigned integer.
  191. */
  192. static const unsigned int scale = 1 << 18;
  193. static const unsigned int cbrt_scale = 1 << (2 * 18 / 3);
  194. /* Define some constants, none exceed 32 bits */
  195. static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */
  196. static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */
  197. static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */
  198. static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */
  199. /*
  200. * Multiply two scaled integers together and rescale the result.
  201. */
  202. static ossl_inline uint64_t mul2(uint64_t a, uint64_t b)
  203. {
  204. return a * b / scale;
  205. }
  206. /*
  207. * Calculate the cube root of a 64 bit scaled integer.
  208. * Although the cube root of a 64 bit number does fit into a 32 bit unsigned
  209. * integer, this is not guaranteed after scaling, so this function has a
  210. * 64 bit return. This uses the shifting nth root algorithm with some
  211. * algebraic simplifications.
  212. */
  213. static uint64_t icbrt64(uint64_t x)
  214. {
  215. uint64_t r = 0;
  216. uint64_t b;
  217. int s;
  218. for (s = 63; s >= 0; s -= 3) {
  219. r <<= 1;
  220. b = 3 * r * (r + 1) + 1;
  221. if ((x >> s) >= b) {
  222. x -= b << s;
  223. r++;
  224. }
  225. }
  226. return r * cbrt_scale;
  227. }
  228. /*
  229. * Calculate the natural logarithm of a 64 bit scaled integer.
  230. * This is done by calculating a base two logarithm and scaling.
  231. * The maximum logarithm (base 2) is 64 and this reduces base e, so
  232. * a 32 bit result should not overflow. The argument passed must be
  233. * greater than unity so we don't need to handle negative results.
  234. */
  235. static uint32_t ilog_e(uint64_t v)
  236. {
  237. uint32_t i, r = 0;
  238. /*
  239. * Scale down the value into the range 1 .. 2.
  240. *
  241. * If fractional numbers need to be processed, another loop needs
  242. * to go here that checks v < scale and if so multiplies it by 2 and
  243. * reduces r by scale. This also means making r signed.
  244. */
  245. while (v >= 2 * scale) {
  246. v >>= 1;
  247. r += scale;
  248. }
  249. for (i = scale / 2; i != 0; i /= 2) {
  250. v = mul2(v, v);
  251. if (v >= 2 * scale) {
  252. v >>= 1;
  253. r += i;
  254. }
  255. }
  256. r = (r * (uint64_t)scale) / log_e;
  257. return r;
  258. }
  259. /*
  260. * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC
  261. * Modulus Lengths.
  262. *
  263. * Note that this formula is also referred to in SP800-56A rev3 Appendix D:
  264. * for FFC safe prime groups for modp and ffdhe.
  265. * After Table 25 and Table 26 it refers to
  266. * "The maximum security strength estimates were calculated using the formula in
  267. * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight
  268. * bits".
  269. *
  270. * The formula is:
  271. *
  272. * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)}
  273. * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)}
  274. * The two cube roots are merged together here.
  275. */
  276. uint16_t ossl_ifc_ffc_compute_security_bits(int n)
  277. {
  278. uint64_t x;
  279. uint32_t lx;
  280. uint16_t y, cap;
  281. /*
  282. * Look for common values as listed in standards.
  283. * These values are not exactly equal to the results from the formulae in
  284. * the standards but are defined to be canonical.
  285. */
  286. switch (n) {
  287. case 2048: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
  288. return 112;
  289. case 3072: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */
  290. return 128;
  291. case 4096: /* SP 800-56B rev 2 Appendix D */
  292. return 152;
  293. case 6144: /* SP 800-56B rev 2 Appendix D */
  294. return 176;
  295. case 7680: /* FIPS 140-2 IG 7.5 */
  296. return 192;
  297. case 8192: /* SP 800-56B rev 2 Appendix D */
  298. return 200;
  299. case 15360: /* FIPS 140-2 IG 7.5 */
  300. return 256;
  301. }
  302. /*
  303. * The first incorrect result (i.e. not accurate or off by one low) occurs
  304. * for n = 699668. The true value here is 1200. Instead of using this n
  305. * as the check threshold, the smallest n such that the correct result is
  306. * 1200 is used instead.
  307. */
  308. if (n >= 687737)
  309. return 1200;
  310. if (n < 8)
  311. return 0;
  312. /*
  313. * To ensure that the output is non-decreasing with respect to n,
  314. * a cap needs to be applied to the two values where the function over
  315. * estimates the strength (according to the above fast path).
  316. */
  317. if (n <= 7680)
  318. cap = 192;
  319. else if (n <= 15360)
  320. cap = 256;
  321. else
  322. cap = 1200;
  323. x = n * (uint64_t)log_2;
  324. lx = ilog_e(x);
  325. y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690)
  326. / log_2);
  327. y = (y + 4) & ~7;
  328. if (y > cap)
  329. y = cap;
  330. return y;
  331. }
  332. int RSA_security_bits(const RSA *rsa)
  333. {
  334. int bits = BN_num_bits(rsa->n);
  335. #ifndef FIPS_MODULE
  336. if (rsa->version == RSA_ASN1_VERSION_MULTI) {
  337. /* This ought to mean that we have private key at hand. */
  338. int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos);
  339. if (ex_primes <= 0 || (ex_primes + 2) > ossl_rsa_multip_cap(bits))
  340. return 0;
  341. }
  342. #endif
  343. return ossl_ifc_ffc_compute_security_bits(bits);
  344. }
  345. int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d)
  346. {
  347. /* If the fields n and e in r are NULL, the corresponding input
  348. * parameters MUST be non-NULL for n and e. d may be
  349. * left NULL (in case only the public key is used).
  350. */
  351. if ((r->n == NULL && n == NULL)
  352. || (r->e == NULL && e == NULL))
  353. return 0;
  354. if (n != NULL) {
  355. BN_free(r->n);
  356. r->n = n;
  357. }
  358. if (e != NULL) {
  359. BN_free(r->e);
  360. r->e = e;
  361. }
  362. if (d != NULL) {
  363. BN_clear_free(r->d);
  364. r->d = d;
  365. BN_set_flags(r->d, BN_FLG_CONSTTIME);
  366. }
  367. r->dirty_cnt++;
  368. return 1;
  369. }
  370. int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q)
  371. {
  372. /* If the fields p and q in r are NULL, the corresponding input
  373. * parameters MUST be non-NULL.
  374. */
  375. if ((r->p == NULL && p == NULL)
  376. || (r->q == NULL && q == NULL))
  377. return 0;
  378. if (p != NULL) {
  379. BN_clear_free(r->p);
  380. r->p = p;
  381. BN_set_flags(r->p, BN_FLG_CONSTTIME);
  382. }
  383. if (q != NULL) {
  384. BN_clear_free(r->q);
  385. r->q = q;
  386. BN_set_flags(r->q, BN_FLG_CONSTTIME);
  387. }
  388. r->dirty_cnt++;
  389. return 1;
  390. }
  391. int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp)
  392. {
  393. /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input
  394. * parameters MUST be non-NULL.
  395. */
  396. if ((r->dmp1 == NULL && dmp1 == NULL)
  397. || (r->dmq1 == NULL && dmq1 == NULL)
  398. || (r->iqmp == NULL && iqmp == NULL))
  399. return 0;
  400. if (dmp1 != NULL) {
  401. BN_clear_free(r->dmp1);
  402. r->dmp1 = dmp1;
  403. BN_set_flags(r->dmp1, BN_FLG_CONSTTIME);
  404. }
  405. if (dmq1 != NULL) {
  406. BN_clear_free(r->dmq1);
  407. r->dmq1 = dmq1;
  408. BN_set_flags(r->dmq1, BN_FLG_CONSTTIME);
  409. }
  410. if (iqmp != NULL) {
  411. BN_clear_free(r->iqmp);
  412. r->iqmp = iqmp;
  413. BN_set_flags(r->iqmp, BN_FLG_CONSTTIME);
  414. }
  415. r->dirty_cnt++;
  416. return 1;
  417. }
  418. #ifndef FIPS_MODULE
  419. /*
  420. * Is it better to export RSA_PRIME_INFO structure
  421. * and related functions to let user pass a triplet?
  422. */
  423. int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[],
  424. BIGNUM *coeffs[], int pnum)
  425. {
  426. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL;
  427. RSA_PRIME_INFO *pinfo;
  428. int i;
  429. if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0)
  430. return 0;
  431. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  432. if (prime_infos == NULL)
  433. return 0;
  434. if (r->prime_infos != NULL)
  435. old = r->prime_infos;
  436. for (i = 0; i < pnum; i++) {
  437. pinfo = ossl_rsa_multip_info_new();
  438. if (pinfo == NULL)
  439. goto err;
  440. if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) {
  441. BN_clear_free(pinfo->r);
  442. BN_clear_free(pinfo->d);
  443. BN_clear_free(pinfo->t);
  444. pinfo->r = primes[i];
  445. pinfo->d = exps[i];
  446. pinfo->t = coeffs[i];
  447. BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
  448. BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
  449. BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
  450. } else {
  451. ossl_rsa_multip_info_free(pinfo);
  452. goto err;
  453. }
  454. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  455. }
  456. r->prime_infos = prime_infos;
  457. if (!ossl_rsa_multip_calc_product(r)) {
  458. r->prime_infos = old;
  459. goto err;
  460. }
  461. if (old != NULL) {
  462. /*
  463. * This is hard to deal with, since the old infos could
  464. * also be set by this function and r, d, t should not
  465. * be freed in that case. So currently, stay consistent
  466. * with other *set0* functions: just free it...
  467. */
  468. sk_RSA_PRIME_INFO_pop_free(old, ossl_rsa_multip_info_free);
  469. }
  470. r->version = RSA_ASN1_VERSION_MULTI;
  471. r->dirty_cnt++;
  472. return 1;
  473. err:
  474. /* r, d, t should not be freed */
  475. sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex);
  476. return 0;
  477. }
  478. #endif
  479. void RSA_get0_key(const RSA *r,
  480. const BIGNUM **n, const BIGNUM **e, const BIGNUM **d)
  481. {
  482. if (n != NULL)
  483. *n = r->n;
  484. if (e != NULL)
  485. *e = r->e;
  486. if (d != NULL)
  487. *d = r->d;
  488. }
  489. void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q)
  490. {
  491. if (p != NULL)
  492. *p = r->p;
  493. if (q != NULL)
  494. *q = r->q;
  495. }
  496. #ifndef FIPS_MODULE
  497. int RSA_get_multi_prime_extra_count(const RSA *r)
  498. {
  499. int pnum;
  500. pnum = sk_RSA_PRIME_INFO_num(r->prime_infos);
  501. if (pnum <= 0)
  502. pnum = 0;
  503. return pnum;
  504. }
  505. int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[])
  506. {
  507. int pnum, i;
  508. RSA_PRIME_INFO *pinfo;
  509. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  510. return 0;
  511. /*
  512. * return other primes
  513. * it's caller's responsibility to allocate oth_primes[pnum]
  514. */
  515. for (i = 0; i < pnum; i++) {
  516. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  517. primes[i] = pinfo->r;
  518. }
  519. return 1;
  520. }
  521. #endif
  522. void RSA_get0_crt_params(const RSA *r,
  523. const BIGNUM **dmp1, const BIGNUM **dmq1,
  524. const BIGNUM **iqmp)
  525. {
  526. if (dmp1 != NULL)
  527. *dmp1 = r->dmp1;
  528. if (dmq1 != NULL)
  529. *dmq1 = r->dmq1;
  530. if (iqmp != NULL)
  531. *iqmp = r->iqmp;
  532. }
  533. #ifndef FIPS_MODULE
  534. int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[],
  535. const BIGNUM *coeffs[])
  536. {
  537. int pnum;
  538. if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0)
  539. return 0;
  540. /* return other primes */
  541. if (exps != NULL || coeffs != NULL) {
  542. RSA_PRIME_INFO *pinfo;
  543. int i;
  544. /* it's the user's job to guarantee the buffer length */
  545. for (i = 0; i < pnum; i++) {
  546. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  547. if (exps != NULL)
  548. exps[i] = pinfo->d;
  549. if (coeffs != NULL)
  550. coeffs[i] = pinfo->t;
  551. }
  552. }
  553. return 1;
  554. }
  555. #endif
  556. const BIGNUM *RSA_get0_n(const RSA *r)
  557. {
  558. return r->n;
  559. }
  560. const BIGNUM *RSA_get0_e(const RSA *r)
  561. {
  562. return r->e;
  563. }
  564. const BIGNUM *RSA_get0_d(const RSA *r)
  565. {
  566. return r->d;
  567. }
  568. const BIGNUM *RSA_get0_p(const RSA *r)
  569. {
  570. return r->p;
  571. }
  572. const BIGNUM *RSA_get0_q(const RSA *r)
  573. {
  574. return r->q;
  575. }
  576. const BIGNUM *RSA_get0_dmp1(const RSA *r)
  577. {
  578. return r->dmp1;
  579. }
  580. const BIGNUM *RSA_get0_dmq1(const RSA *r)
  581. {
  582. return r->dmq1;
  583. }
  584. const BIGNUM *RSA_get0_iqmp(const RSA *r)
  585. {
  586. return r->iqmp;
  587. }
  588. const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r)
  589. {
  590. #ifdef FIPS_MODULE
  591. return NULL;
  592. #else
  593. return r->pss;
  594. #endif
  595. }
  596. /* Internal */
  597. int ossl_rsa_set0_pss_params(RSA *r, RSA_PSS_PARAMS *pss)
  598. {
  599. #ifdef FIPS_MODULE
  600. return 0;
  601. #else
  602. RSA_PSS_PARAMS_free(r->pss);
  603. r->pss = pss;
  604. return 1;
  605. #endif
  606. }
  607. /* Internal */
  608. RSA_PSS_PARAMS_30 *ossl_rsa_get0_pss_params_30(RSA *r)
  609. {
  610. return &r->pss_params;
  611. }
  612. void RSA_clear_flags(RSA *r, int flags)
  613. {
  614. r->flags &= ~flags;
  615. }
  616. int RSA_test_flags(const RSA *r, int flags)
  617. {
  618. return r->flags & flags;
  619. }
  620. void RSA_set_flags(RSA *r, int flags)
  621. {
  622. r->flags |= flags;
  623. }
  624. int RSA_get_version(RSA *r)
  625. {
  626. /* { two-prime(0), multi(1) } */
  627. return r->version;
  628. }
  629. #ifndef FIPS_MODULE
  630. ENGINE *RSA_get0_engine(const RSA *r)
  631. {
  632. return r->engine;
  633. }
  634. int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2)
  635. {
  636. /* If key type not RSA or RSA-PSS return error */
  637. if (ctx != NULL && ctx->pmeth != NULL
  638. && ctx->pmeth->pkey_id != EVP_PKEY_RSA
  639. && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS)
  640. return -1;
  641. return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2);
  642. }
  643. #endif
  644. DEFINE_STACK_OF(BIGNUM)
  645. int ossl_rsa_set0_all_params(RSA *r, const STACK_OF(BIGNUM) *primes,
  646. const STACK_OF(BIGNUM) *exps,
  647. const STACK_OF(BIGNUM) *coeffs)
  648. {
  649. #ifndef FIPS_MODULE
  650. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
  651. #endif
  652. int pnum;
  653. if (primes == NULL || exps == NULL || coeffs == NULL)
  654. return 0;
  655. pnum = sk_BIGNUM_num(primes);
  656. if (pnum < 2)
  657. return 0;
  658. if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
  659. sk_BIGNUM_value(primes, 1)))
  660. return 0;
  661. if (pnum == sk_BIGNUM_num(exps)
  662. && pnum == sk_BIGNUM_num(coeffs) + 1) {
  663. if (!RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
  664. sk_BIGNUM_value(exps, 1),
  665. sk_BIGNUM_value(coeffs, 0)))
  666. return 0;
  667. }
  668. #ifndef FIPS_MODULE
  669. old_infos = r->prime_infos;
  670. #endif
  671. if (pnum > 2) {
  672. #ifndef FIPS_MODULE
  673. int i;
  674. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  675. if (prime_infos == NULL)
  676. return 0;
  677. for (i = 2; i < pnum; i++) {
  678. BIGNUM *prime = sk_BIGNUM_value(primes, i);
  679. BIGNUM *exp = sk_BIGNUM_value(exps, i);
  680. BIGNUM *coeff = sk_BIGNUM_value(coeffs, i - 1);
  681. RSA_PRIME_INFO *pinfo = NULL;
  682. if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
  683. goto err;
  684. /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */
  685. if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL)
  686. goto err;
  687. pinfo->r = prime;
  688. pinfo->d = exp;
  689. pinfo->t = coeff;
  690. BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
  691. BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
  692. BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
  693. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  694. }
  695. r->prime_infos = prime_infos;
  696. if (!ossl_rsa_multip_calc_product(r)) {
  697. r->prime_infos = old_infos;
  698. goto err;
  699. }
  700. #else
  701. return 0;
  702. #endif
  703. }
  704. #ifndef FIPS_MODULE
  705. if (old_infos != NULL) {
  706. /*
  707. * This is hard to deal with, since the old infos could
  708. * also be set by this function and r, d, t should not
  709. * be freed in that case. So currently, stay consistent
  710. * with other *set0* functions: just free it...
  711. */
  712. sk_RSA_PRIME_INFO_pop_free(old_infos, ossl_rsa_multip_info_free);
  713. }
  714. #endif
  715. r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
  716. r->dirty_cnt++;
  717. return 1;
  718. #ifndef FIPS_MODULE
  719. err:
  720. /* r, d, t should not be freed */
  721. sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex);
  722. return 0;
  723. #endif
  724. }
  725. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  726. int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
  727. STACK_OF(BIGNUM_const) *exps,
  728. STACK_OF(BIGNUM_const) *coeffs)
  729. {
  730. #ifndef FIPS_MODULE
  731. RSA_PRIME_INFO *pinfo;
  732. int i, pnum;
  733. #endif
  734. if (r == NULL)
  735. return 0;
  736. /* If |p| is NULL, there are no CRT parameters */
  737. if (RSA_get0_p(r) == NULL)
  738. return 1;
  739. sk_BIGNUM_const_push(primes, RSA_get0_p(r));
  740. sk_BIGNUM_const_push(primes, RSA_get0_q(r));
  741. sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
  742. sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
  743. sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
  744. #ifndef FIPS_MODULE
  745. pnum = RSA_get_multi_prime_extra_count(r);
  746. for (i = 0; i < pnum; i++) {
  747. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  748. sk_BIGNUM_const_push(primes, pinfo->r);
  749. sk_BIGNUM_const_push(exps, pinfo->d);
  750. sk_BIGNUM_const_push(coeffs, pinfo->t);
  751. }
  752. #endif
  753. return 1;
  754. }
  755. #ifndef FIPS_MODULE
  756. /* Helpers to set or get diverse hash algorithm names */
  757. static int int_set_rsa_md_name(EVP_PKEY_CTX *ctx,
  758. /* For checks */
  759. int keytype, int optype,
  760. /* For EVP_PKEY_CTX_set_params() */
  761. const char *mdkey, const char *mdname,
  762. const char *propkey, const char *mdprops)
  763. {
  764. OSSL_PARAM params[3], *p = params;
  765. if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
  766. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  767. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  768. return -2;
  769. }
  770. /* If key type not RSA return error */
  771. switch (keytype) {
  772. case -1:
  773. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  774. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  775. return -1;
  776. break;
  777. default:
  778. if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
  779. return -1;
  780. break;
  781. }
  782. /* Cast away the const. This is read only so should be safe */
  783. *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, 0);
  784. if (evp_pkey_ctx_is_provided(ctx) && mdprops != NULL) {
  785. /* Cast away the const. This is read only so should be safe */
  786. *p++ = OSSL_PARAM_construct_utf8_string(propkey, (char *)mdprops, 0);
  787. }
  788. *p++ = OSSL_PARAM_construct_end();
  789. return evp_pkey_ctx_set_params_strict(ctx, params);
  790. }
  791. /* Helpers to set or get diverse hash algorithm names */
  792. static int int_get_rsa_md_name(EVP_PKEY_CTX *ctx,
  793. /* For checks */
  794. int keytype, int optype,
  795. /* For EVP_PKEY_CTX_get_params() */
  796. const char *mdkey,
  797. char *mdname, size_t mdnamesize)
  798. {
  799. OSSL_PARAM params[2], *p = params;
  800. if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
  801. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  802. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  803. return -2;
  804. }
  805. /* If key type not RSA return error */
  806. switch (keytype) {
  807. case -1:
  808. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  809. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  810. return -1;
  811. break;
  812. default:
  813. if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
  814. return -1;
  815. break;
  816. }
  817. /* Cast away the const. This is read only so should be safe */
  818. *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, mdnamesize);
  819. *p++ = OSSL_PARAM_construct_end();
  820. return evp_pkey_ctx_get_params_strict(ctx, params);
  821. }
  822. /*
  823. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  824. * simply because that's easier.
  825. */
  826. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
  827. {
  828. return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING,
  829. pad_mode, NULL);
  830. }
  831. /*
  832. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  833. * simply because that's easier.
  834. */
  835. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
  836. {
  837. return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  838. 0, pad_mode);
  839. }
  840. /*
  841. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  842. * simply because that's easier.
  843. */
  844. int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  845. {
  846. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  847. EVP_PKEY_CTRL_MD, 0, (void *)(md));
  848. }
  849. int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX *ctx,
  850. const char *mdname,
  851. const char *mdprops)
  852. {
  853. return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  854. OSSL_PKEY_PARAM_RSA_DIGEST, mdname,
  855. OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, mdprops);
  856. }
  857. /*
  858. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  859. * simply because that's easier.
  860. */
  861. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  862. {
  863. /* If key type not RSA return error */
  864. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  865. return -1;
  866. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  867. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md));
  868. }
  869. int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  870. const char *mdprops)
  871. {
  872. return
  873. int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  874. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname,
  875. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops);
  876. }
  877. int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
  878. size_t namesize)
  879. {
  880. return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  881. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  882. name, namesize);
  883. }
  884. /*
  885. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  886. * simply because that's easier.
  887. */
  888. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  889. {
  890. /* If key type not RSA return error */
  891. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  892. return -1;
  893. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  894. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
  895. }
  896. /*
  897. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  898. * simply because that's easier.
  899. */
  900. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  901. {
  902. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  903. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
  904. }
  905. int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  906. const char *mdprops)
  907. {
  908. return int_set_rsa_md_name(ctx, -1,
  909. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  910. OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
  911. OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops);
  912. }
  913. int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
  914. size_t namesize)
  915. {
  916. return int_get_rsa_md_name(ctx, -1,
  917. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  918. OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize);
  919. }
  920. /*
  921. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  922. * simply because that's easier.
  923. */
  924. int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  925. {
  926. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  927. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
  928. }
  929. int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
  930. const char *mdname)
  931. {
  932. return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  933. OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
  934. NULL, NULL);
  935. }
  936. /*
  937. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  938. * simply because that's easier.
  939. */
  940. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  941. {
  942. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  943. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md));
  944. }
  945. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
  946. {
  947. OSSL_PARAM rsa_params[2], *p = rsa_params;
  948. const char *empty = "";
  949. /*
  950. * Needed as we swap label with empty if it is NULL, and label is
  951. * freed at the end of this function.
  952. */
  953. void *plabel = label;
  954. int ret;
  955. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  956. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  957. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  958. return -2;
  959. }
  960. /* If key type not RSA return error */
  961. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  962. return -1;
  963. /* Accept NULL for backward compatibility */
  964. if (label == NULL && llen == 0)
  965. plabel = (void *)empty;
  966. /* Cast away the const. This is read only so should be safe */
  967. *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  968. (void *)plabel, (size_t)llen);
  969. *p++ = OSSL_PARAM_construct_end();
  970. ret = evp_pkey_ctx_set_params_strict(ctx, rsa_params);
  971. if (ret <= 0)
  972. return ret;
  973. /* Ownership is supposed to be transferred to the callee. */
  974. OPENSSL_free(label);
  975. return 1;
  976. }
  977. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
  978. {
  979. OSSL_PARAM rsa_params[2], *p = rsa_params;
  980. size_t labellen;
  981. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  982. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  983. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  984. return -2;
  985. }
  986. /* If key type not RSA return error */
  987. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  988. return -1;
  989. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  990. (void **)label, 0);
  991. *p++ = OSSL_PARAM_construct_end();
  992. if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
  993. return -1;
  994. labellen = rsa_params[0].return_size;
  995. if (labellen > INT_MAX)
  996. return -1;
  997. return (int)labellen;
  998. }
  999. /*
  1000. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  1001. * simply because that's easier.
  1002. */
  1003. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
  1004. {
  1005. /*
  1006. * For some reason, the optype was set to this:
  1007. *
  1008. * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
  1009. *
  1010. * However, we do use RSA-PSS with the whole gamut of diverse signature
  1011. * and verification operations, so the optype gets upgraded to this:
  1012. *
  1013. * EVP_PKEY_OP_TYPE_SIG
  1014. */
  1015. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
  1016. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL);
  1017. }
  1018. /*
  1019. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  1020. * simply because that's easier.
  1021. */
  1022. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
  1023. {
  1024. /*
  1025. * Because of circumstances, the optype is updated from:
  1026. *
  1027. * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
  1028. *
  1029. * to:
  1030. *
  1031. * EVP_PKEY_OP_TYPE_SIG
  1032. */
  1033. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
  1034. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen);
  1035. }
  1036. int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
  1037. {
  1038. OSSL_PARAM pad_params[2], *p = pad_params;
  1039. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1040. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1041. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1042. return -2;
  1043. }
  1044. if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1045. return -1;
  1046. *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
  1047. &saltlen);
  1048. *p++ = OSSL_PARAM_construct_end();
  1049. return evp_pkey_ctx_set_params_strict(ctx, pad_params);
  1050. }
  1051. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
  1052. {
  1053. OSSL_PARAM params[2], *p = params;
  1054. size_t bits2 = bits;
  1055. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1056. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1057. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1058. return -2;
  1059. }
  1060. /* If key type not RSA return error */
  1061. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  1062. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1063. return -1;
  1064. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
  1065. *p++ = OSSL_PARAM_construct_end();
  1066. return evp_pkey_ctx_set_params_strict(ctx, params);
  1067. }
  1068. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
  1069. {
  1070. int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN,
  1071. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
  1072. /*
  1073. * Satisfy memory semantics for pre-3.0 callers of
  1074. * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
  1075. * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
  1076. */
  1077. if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) {
  1078. BN_free(ctx->rsa_pubexp);
  1079. ctx->rsa_pubexp = pubexp;
  1080. }
  1081. return ret;
  1082. }
  1083. int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
  1084. {
  1085. int ret = 0;
  1086. /*
  1087. * When we're dealing with a provider, there's no need to duplicate
  1088. * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
  1089. */
  1090. if (evp_pkey_ctx_is_legacy(ctx)) {
  1091. pubexp = BN_dup(pubexp);
  1092. if (pubexp == NULL)
  1093. return 0;
  1094. }
  1095. ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  1096. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
  1097. if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0)
  1098. BN_free(pubexp);
  1099. return ret;
  1100. }
  1101. int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
  1102. {
  1103. OSSL_PARAM params[2], *p = params;
  1104. size_t primes2 = primes;
  1105. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1106. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1107. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1108. return -2;
  1109. }
  1110. /* If key type not RSA return error */
  1111. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  1112. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1113. return -1;
  1114. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
  1115. *p++ = OSSL_PARAM_construct_end();
  1116. return evp_pkey_ctx_set_params_strict(ctx, params);
  1117. }
  1118. #endif