rsa_lib.c 35 KB

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