rsa_lib.c 35 KB

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