rsa_lib.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298
  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", 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", 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. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  864. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md));
  865. }
  866. int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  867. const char *mdprops)
  868. {
  869. return
  870. int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  871. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname,
  872. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops);
  873. }
  874. int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
  875. size_t namesize)
  876. {
  877. return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  878. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  879. name, namesize);
  880. }
  881. /*
  882. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  883. * simply because that's easier.
  884. */
  885. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  886. {
  887. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  888. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
  889. }
  890. /*
  891. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  892. * simply because that's easier.
  893. */
  894. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  895. {
  896. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  897. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
  898. }
  899. int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  900. const char *mdprops)
  901. {
  902. return int_set_rsa_md_name(ctx, -1,
  903. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  904. OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
  905. OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops);
  906. }
  907. int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
  908. size_t namesize)
  909. {
  910. return int_get_rsa_md_name(ctx, -1,
  911. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  912. OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize);
  913. }
  914. /*
  915. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  916. * simply because that's easier.
  917. */
  918. int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  919. {
  920. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  921. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
  922. }
  923. int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
  924. const char *mdname)
  925. {
  926. return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  927. OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
  928. NULL, NULL);
  929. }
  930. /*
  931. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  932. * simply because that's easier.
  933. */
  934. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  935. {
  936. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  937. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md));
  938. }
  939. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
  940. {
  941. OSSL_PARAM rsa_params[2], *p = rsa_params;
  942. const char *empty = "";
  943. /*
  944. * Needed as we swap label with empty if it is NULL, and label is
  945. * freed at the end of this function.
  946. */
  947. void *plabel = label;
  948. int ret;
  949. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  950. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  951. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  952. return -2;
  953. }
  954. /* If key type not RSA return error */
  955. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  956. return -1;
  957. /* Accept NULL for backward compatibility */
  958. if (label == NULL && llen == 0)
  959. plabel = (void *)empty;
  960. /* Cast away the const. This is read only so should be safe */
  961. *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  962. (void *)plabel, (size_t)llen);
  963. *p++ = OSSL_PARAM_construct_end();
  964. ret = evp_pkey_ctx_set_params_strict(ctx, rsa_params);
  965. if (ret <= 0)
  966. return ret;
  967. /* Ownership is supposed to be transferred to the callee. */
  968. OPENSSL_free(label);
  969. return 1;
  970. }
  971. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
  972. {
  973. OSSL_PARAM rsa_params[2], *p = rsa_params;
  974. size_t labellen;
  975. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  976. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  977. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  978. return -2;
  979. }
  980. /* If key type not RSA return error */
  981. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  982. return -1;
  983. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  984. (void **)label, 0);
  985. *p++ = OSSL_PARAM_construct_end();
  986. if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
  987. return -1;
  988. labellen = rsa_params[0].return_size;
  989. if (labellen > INT_MAX)
  990. return -1;
  991. return (int)labellen;
  992. }
  993. /*
  994. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  995. * simply because that's easier.
  996. */
  997. int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
  998. {
  999. /*
  1000. * For some reason, the optype was set to this:
  1001. *
  1002. * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
  1003. *
  1004. * However, we do use RSA-PSS with the whole gamut of diverse signature
  1005. * and verification operations, so the optype gets upgraded to this:
  1006. *
  1007. * EVP_PKEY_OP_TYPE_SIG
  1008. */
  1009. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
  1010. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL);
  1011. }
  1012. /*
  1013. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  1014. * simply because that's easier.
  1015. */
  1016. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
  1017. {
  1018. /*
  1019. * Because of circumstances, the optype is updated from:
  1020. *
  1021. * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
  1022. *
  1023. * to:
  1024. *
  1025. * EVP_PKEY_OP_TYPE_SIG
  1026. */
  1027. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
  1028. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen);
  1029. }
  1030. int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
  1031. {
  1032. OSSL_PARAM pad_params[2], *p = pad_params;
  1033. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1034. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1035. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1036. return -2;
  1037. }
  1038. if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1039. return -1;
  1040. *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
  1041. &saltlen);
  1042. *p++ = OSSL_PARAM_construct_end();
  1043. return evp_pkey_ctx_set_params_strict(ctx, pad_params);
  1044. }
  1045. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
  1046. {
  1047. OSSL_PARAM params[2], *p = params;
  1048. size_t bits2 = bits;
  1049. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1050. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1051. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1052. return -2;
  1053. }
  1054. /* If key type not RSA return error */
  1055. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  1056. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1057. return -1;
  1058. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
  1059. *p++ = OSSL_PARAM_construct_end();
  1060. return evp_pkey_ctx_set_params_strict(ctx, params);
  1061. }
  1062. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
  1063. {
  1064. int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN,
  1065. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
  1066. /*
  1067. * Satisfy memory semantics for pre-3.0 callers of
  1068. * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
  1069. * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
  1070. */
  1071. if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) {
  1072. BN_free(ctx->rsa_pubexp);
  1073. ctx->rsa_pubexp = pubexp;
  1074. }
  1075. return ret;
  1076. }
  1077. int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
  1078. {
  1079. int ret = 0;
  1080. /*
  1081. * When we're dealing with a provider, there's no need to duplicate
  1082. * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
  1083. */
  1084. if (evp_pkey_ctx_is_legacy(ctx)) {
  1085. pubexp = BN_dup(pubexp);
  1086. if (pubexp == NULL)
  1087. return 0;
  1088. }
  1089. ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  1090. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
  1091. if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0)
  1092. BN_free(pubexp);
  1093. return ret;
  1094. }
  1095. int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
  1096. {
  1097. OSSL_PARAM params[2], *p = params;
  1098. size_t primes2 = primes;
  1099. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1100. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1101. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1102. return -2;
  1103. }
  1104. /* If key type not RSA return error */
  1105. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  1106. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1107. return -1;
  1108. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
  1109. *p++ = OSSL_PARAM_construct_end();
  1110. return evp_pkey_ctx_set_params_strict(ctx, params);
  1111. }
  1112. #endif