rsa_lib.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  1. /*
  2. * Copyright 1995-2024 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. /*
  646. * Note: This function deletes values from the parameter
  647. * stack values as they are consumed and set in the RSA key.
  648. */
  649. int ossl_rsa_set0_all_params(RSA *r, STACK_OF(BIGNUM) *primes,
  650. STACK_OF(BIGNUM) *exps,
  651. STACK_OF(BIGNUM) *coeffs)
  652. {
  653. #ifndef FIPS_MODULE
  654. STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL;
  655. #endif
  656. int pnum;
  657. if (primes == NULL || exps == NULL || coeffs == NULL)
  658. return 0;
  659. pnum = sk_BIGNUM_num(primes);
  660. /* we need at least 2 primes */
  661. if (pnum < 2)
  662. return 0;
  663. if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0),
  664. sk_BIGNUM_value(primes, 1)))
  665. return 0;
  666. /*
  667. * if we managed to set everything above, remove those elements from the
  668. * stack
  669. * Note, we do this after the above all to ensure that we have taken
  670. * ownership of all the elements in the RSA key to avoid memory leaks
  671. * we also use delete 0 here as we are grabbing items from the end of the
  672. * stack rather than the start, otherwise we could use pop
  673. */
  674. sk_BIGNUM_delete(primes, 0);
  675. sk_BIGNUM_delete(primes, 0);
  676. if (pnum == sk_BIGNUM_num(exps)
  677. && pnum == sk_BIGNUM_num(coeffs) + 1) {
  678. if (!RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0),
  679. sk_BIGNUM_value(exps, 1),
  680. sk_BIGNUM_value(coeffs, 0)))
  681. return 0;
  682. /* as above, once we consume the above params, delete them from the list */
  683. sk_BIGNUM_delete(exps, 0);
  684. sk_BIGNUM_delete(exps, 0);
  685. sk_BIGNUM_delete(coeffs, 0);
  686. }
  687. #ifndef FIPS_MODULE
  688. old_infos = r->prime_infos;
  689. #endif
  690. if (pnum > 2) {
  691. #ifndef FIPS_MODULE
  692. int i;
  693. prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum);
  694. if (prime_infos == NULL)
  695. return 0;
  696. for (i = 2; i < pnum; i++) {
  697. BIGNUM *prime = sk_BIGNUM_pop(primes);
  698. BIGNUM *exp = sk_BIGNUM_pop(exps);
  699. BIGNUM *coeff = sk_BIGNUM_pop(coeffs);
  700. RSA_PRIME_INFO *pinfo = NULL;
  701. if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL))
  702. goto err;
  703. /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */
  704. if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL)
  705. goto err;
  706. pinfo->r = prime;
  707. pinfo->d = exp;
  708. pinfo->t = coeff;
  709. BN_set_flags(pinfo->r, BN_FLG_CONSTTIME);
  710. BN_set_flags(pinfo->d, BN_FLG_CONSTTIME);
  711. BN_set_flags(pinfo->t, BN_FLG_CONSTTIME);
  712. (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo);
  713. }
  714. r->prime_infos = prime_infos;
  715. if (!ossl_rsa_multip_calc_product(r)) {
  716. r->prime_infos = old_infos;
  717. goto err;
  718. }
  719. #else
  720. return 0;
  721. #endif
  722. }
  723. #ifndef FIPS_MODULE
  724. if (old_infos != NULL) {
  725. /*
  726. * This is hard to deal with, since the old infos could
  727. * also be set by this function and r, d, t should not
  728. * be freed in that case. So currently, stay consistent
  729. * with other *set0* functions: just free it...
  730. */
  731. sk_RSA_PRIME_INFO_pop_free(old_infos, ossl_rsa_multip_info_free);
  732. }
  733. #endif
  734. r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT;
  735. r->dirty_cnt++;
  736. return 1;
  737. #ifndef FIPS_MODULE
  738. err:
  739. /* r, d, t should not be freed */
  740. sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex);
  741. return 0;
  742. #endif
  743. }
  744. DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM)
  745. int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes,
  746. STACK_OF(BIGNUM_const) *exps,
  747. STACK_OF(BIGNUM_const) *coeffs)
  748. {
  749. #ifndef FIPS_MODULE
  750. RSA_PRIME_INFO *pinfo;
  751. int i, pnum;
  752. #endif
  753. if (r == NULL)
  754. return 0;
  755. /* If |p| is NULL, there are no CRT parameters */
  756. if (RSA_get0_p(r) == NULL)
  757. return 1;
  758. sk_BIGNUM_const_push(primes, RSA_get0_p(r));
  759. sk_BIGNUM_const_push(primes, RSA_get0_q(r));
  760. sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r));
  761. sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r));
  762. sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r));
  763. #ifndef FIPS_MODULE
  764. pnum = RSA_get_multi_prime_extra_count(r);
  765. for (i = 0; i < pnum; i++) {
  766. pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i);
  767. sk_BIGNUM_const_push(primes, pinfo->r);
  768. sk_BIGNUM_const_push(exps, pinfo->d);
  769. sk_BIGNUM_const_push(coeffs, pinfo->t);
  770. }
  771. #endif
  772. return 1;
  773. }
  774. #ifndef FIPS_MODULE
  775. /* Helpers to set or get diverse hash algorithm names */
  776. static int int_set_rsa_md_name(EVP_PKEY_CTX *ctx,
  777. /* For checks */
  778. int keytype, int optype,
  779. /* For EVP_PKEY_CTX_set_params() */
  780. const char *mdkey, const char *mdname,
  781. const char *propkey, const char *mdprops)
  782. {
  783. OSSL_PARAM params[3], *p = params;
  784. if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
  785. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  786. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  787. return -2;
  788. }
  789. /* If key type not RSA return error */
  790. switch (keytype) {
  791. case -1:
  792. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  793. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  794. return -1;
  795. break;
  796. default:
  797. if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
  798. return -1;
  799. break;
  800. }
  801. /* Cast away the const. This is read only so should be safe */
  802. *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, 0);
  803. if (evp_pkey_ctx_is_provided(ctx) && mdprops != NULL) {
  804. /* Cast away the const. This is read only so should be safe */
  805. *p++ = OSSL_PARAM_construct_utf8_string(propkey, (char *)mdprops, 0);
  806. }
  807. *p++ = OSSL_PARAM_construct_end();
  808. return evp_pkey_ctx_set_params_strict(ctx, params);
  809. }
  810. /* Helpers to set or get diverse hash algorithm names */
  811. static int int_get_rsa_md_name(EVP_PKEY_CTX *ctx,
  812. /* For checks */
  813. int keytype, int optype,
  814. /* For EVP_PKEY_CTX_get_params() */
  815. const char *mdkey,
  816. char *mdname, size_t mdnamesize)
  817. {
  818. OSSL_PARAM params[2], *p = params;
  819. if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) {
  820. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  821. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  822. return -2;
  823. }
  824. /* If key type not RSA return error */
  825. switch (keytype) {
  826. case -1:
  827. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  828. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  829. return -1;
  830. break;
  831. default:
  832. if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype)))
  833. return -1;
  834. break;
  835. }
  836. /* Cast away the const. This is read only so should be safe */
  837. *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, mdnamesize);
  838. *p++ = OSSL_PARAM_construct_end();
  839. return evp_pkey_ctx_get_params_strict(ctx, params);
  840. }
  841. /*
  842. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  843. * simply because that's easier.
  844. */
  845. int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode)
  846. {
  847. return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING,
  848. pad_mode, NULL);
  849. }
  850. /*
  851. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  852. * simply because that's easier.
  853. */
  854. int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode)
  855. {
  856. return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING,
  857. 0, pad_mode);
  858. }
  859. /*
  860. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  861. * simply because that's easier.
  862. */
  863. int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  864. {
  865. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  866. EVP_PKEY_CTRL_MD, 0, (void *)(md));
  867. }
  868. int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX *ctx,
  869. const char *mdname,
  870. const char *mdprops)
  871. {
  872. return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  873. OSSL_PKEY_PARAM_RSA_DIGEST, mdname,
  874. OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, mdprops);
  875. }
  876. /*
  877. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  878. * simply because that's easier.
  879. */
  880. int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  881. {
  882. /* If key type not RSA return error */
  883. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  884. return -1;
  885. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  886. EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md));
  887. }
  888. int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  889. const char *mdprops)
  890. {
  891. return
  892. int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  893. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname,
  894. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops);
  895. }
  896. int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name,
  897. size_t namesize)
  898. {
  899. return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  900. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST,
  901. name, namesize);
  902. }
  903. /*
  904. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  905. * simply because that's easier.
  906. */
  907. int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  908. {
  909. /* If key type not RSA return error */
  910. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  911. return -1;
  912. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT,
  913. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md);
  914. }
  915. /*
  916. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  917. * simply because that's easier.
  918. */
  919. int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  920. {
  921. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  922. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
  923. }
  924. int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname,
  925. const char *mdprops)
  926. {
  927. return int_set_rsa_md_name(ctx, -1,
  928. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  929. OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
  930. OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops);
  931. }
  932. int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name,
  933. size_t namesize)
  934. {
  935. return int_get_rsa_md_name(ctx, -1,
  936. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  937. OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize);
  938. }
  939. /*
  940. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  941. * simply because that's easier.
  942. */
  943. int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md)
  944. {
  945. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  946. EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md));
  947. }
  948. int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx,
  949. const char *mdname)
  950. {
  951. return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  952. OSSL_PKEY_PARAM_MGF1_DIGEST, mdname,
  953. NULL, NULL);
  954. }
  955. /*
  956. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  957. * simply because that's easier.
  958. */
  959. int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md)
  960. {
  961. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT,
  962. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md));
  963. }
  964. int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen)
  965. {
  966. OSSL_PARAM rsa_params[2], *p = rsa_params;
  967. const char *empty = "";
  968. /*
  969. * Needed as we swap label with empty if it is NULL, and label is
  970. * freed at the end of this function.
  971. */
  972. void *plabel = label;
  973. int ret;
  974. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  975. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  976. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  977. return -2;
  978. }
  979. /* If key type not RSA return error */
  980. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  981. return -1;
  982. /* Accept NULL for backward compatibility */
  983. if (label == NULL && llen == 0)
  984. plabel = (void *)empty;
  985. /* Cast away the const. This is read only so should be safe */
  986. *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  987. (void *)plabel, (size_t)llen);
  988. *p++ = OSSL_PARAM_construct_end();
  989. ret = evp_pkey_ctx_set_params_strict(ctx, rsa_params);
  990. if (ret <= 0)
  991. return ret;
  992. /* Ownership is supposed to be transferred to the callee. */
  993. OPENSSL_free(label);
  994. return 1;
  995. }
  996. int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label)
  997. {
  998. OSSL_PARAM rsa_params[2], *p = rsa_params;
  999. size_t labellen;
  1000. if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) {
  1001. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1002. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1003. return -2;
  1004. }
  1005. /* If key type not RSA return error */
  1006. if (!EVP_PKEY_CTX_is_a(ctx, "RSA"))
  1007. return -1;
  1008. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL,
  1009. (void **)label, 0);
  1010. *p++ = OSSL_PARAM_construct_end();
  1011. if (!EVP_PKEY_CTX_get_params(ctx, rsa_params))
  1012. return -1;
  1013. labellen = rsa_params[0].return_size;
  1014. if (labellen > INT_MAX)
  1015. return -1;
  1016. return (int)labellen;
  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_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
  1023. {
  1024. /*
  1025. * For some reason, the optype was set to this:
  1026. *
  1027. * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
  1028. *
  1029. * However, we do use RSA-PSS with the whole gamut of diverse signature
  1030. * and verification operations, so the optype gets upgraded to this:
  1031. *
  1032. * EVP_PKEY_OP_TYPE_SIG
  1033. */
  1034. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
  1035. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL);
  1036. }
  1037. /*
  1038. * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper,
  1039. * simply because that's easier.
  1040. */
  1041. int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen)
  1042. {
  1043. /*
  1044. * Because of circumstances, the optype is updated from:
  1045. *
  1046. * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY
  1047. *
  1048. * to:
  1049. *
  1050. * EVP_PKEY_OP_TYPE_SIG
  1051. */
  1052. return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG,
  1053. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen);
  1054. }
  1055. int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen)
  1056. {
  1057. OSSL_PARAM pad_params[2], *p = pad_params;
  1058. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1059. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1060. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1061. return -2;
  1062. }
  1063. if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1064. return -1;
  1065. *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN,
  1066. &saltlen);
  1067. *p++ = OSSL_PARAM_construct_end();
  1068. return evp_pkey_ctx_set_params_strict(ctx, pad_params);
  1069. }
  1070. int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits)
  1071. {
  1072. OSSL_PARAM params[2], *p = params;
  1073. size_t bits2 = bits;
  1074. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1075. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1076. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1077. return -2;
  1078. }
  1079. /* If key type not RSA return error */
  1080. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  1081. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1082. return -1;
  1083. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2);
  1084. *p++ = OSSL_PARAM_construct_end();
  1085. return evp_pkey_ctx_set_params_strict(ctx, params);
  1086. }
  1087. int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
  1088. {
  1089. int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN,
  1090. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
  1091. /*
  1092. * Satisfy memory semantics for pre-3.0 callers of
  1093. * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input
  1094. * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success.
  1095. */
  1096. if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) {
  1097. BN_free(ctx->rsa_pubexp);
  1098. ctx->rsa_pubexp = pubexp;
  1099. }
  1100. return ret;
  1101. }
  1102. int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp)
  1103. {
  1104. int ret = 0;
  1105. /*
  1106. * When we're dealing with a provider, there's no need to duplicate
  1107. * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway.
  1108. */
  1109. if (evp_pkey_ctx_is_legacy(ctx)) {
  1110. pubexp = BN_dup(pubexp);
  1111. if (pubexp == NULL)
  1112. return 0;
  1113. }
  1114. ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN,
  1115. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp);
  1116. if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0)
  1117. BN_free(pubexp);
  1118. return ret;
  1119. }
  1120. int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes)
  1121. {
  1122. OSSL_PARAM params[2], *p = params;
  1123. size_t primes2 = primes;
  1124. if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) {
  1125. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1126. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  1127. return -2;
  1128. }
  1129. /* If key type not RSA return error */
  1130. if (!EVP_PKEY_CTX_is_a(ctx, "RSA")
  1131. && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS"))
  1132. return -1;
  1133. *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2);
  1134. *p++ = OSSL_PARAM_construct_end();
  1135. return evp_pkey_ctx_set_params_strict(ctx, params);
  1136. }
  1137. #endif