]>
| Commit | Line | Data |
|---|---|---|
| 2039c421 | 1 | /* |
| e6633241 | 2 | * Copyright 1995-2025 The OpenSSL Project Authors. All Rights Reserved. |
| d02b48c6 | 3 | * |
| 2a7b6f39 | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
| 2039c421 RS |
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 | |
| d02b48c6 RE |
8 | */ |
| 9 | ||
| c5f87134 P |
10 | /* |
| 11 | * RSA low level APIs are deprecated for public use, but still ok for | |
| 12 | * internal use. | |
| 13 | */ | |
| 14 | #include "internal/deprecated.h" | |
| 15 | ||
| ec577822 | 16 | #include <openssl/crypto.h> |
| 89abd1b6 | 17 | #include <openssl/core_names.h> |
| 3f773c91 TM |
18 | #ifndef FIPS_MODULE |
| 19 | # include <openssl/engine.h> | |
| 20 | #endif | |
| 89abd1b6 | 21 | #include <openssl/evp.h> |
| da9988e0 | 22 | #include <openssl/param_build.h> |
| b39fc560 | 23 | #include "internal/cryptlib.h" |
| cd420b0b | 24 | #include "internal/refcount.h" |
| 25f2138b | 25 | #include "crypto/bn.h" |
| 25f2138b | 26 | #include "crypto/evp.h" |
| c3a4fa4c | 27 | #include "crypto/rsa.h" |
| 902568bb | 28 | #include "crypto/sparse_array.h" |
| 55f02cb6 | 29 | #include "crypto/security_bits.h" |
| 706457b7 | 30 | #include "rsa_local.h" |
| d02b48c6 | 31 | |
| b4250010 | 32 | static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx); |
| afb638f1 | 33 | |
| f844f9eb | 34 | #ifndef FIPS_MODULE |
| 6b691a5c | 35 | RSA *RSA_new(void) |
| 0f113f3e | 36 | { |
| afb638f1 | 37 | return rsa_new_intern(NULL, NULL); |
| 0f113f3e | 38 | } |
| ce8b2574 | 39 | |
| 29c1f061 | 40 | const RSA_METHOD *RSA_get_method(const RSA *rsa) |
| 0f113f3e MC |
41 | { |
| 42 | return rsa->meth; | |
| 43 | } | |
| cb78486d GT |
44 | |
| 45 | int RSA_set_method(RSA *rsa, const RSA_METHOD *meth) | |
| 0f113f3e MC |
46 | { |
| 47 | /* | |
| 48 | * NB: The caller is specifically setting a method, so it's not up to us | |
| 49 | * to deal with which ENGINE it comes from. | |
| 50 | */ | |
| 51 | const RSA_METHOD *mtmp; | |
| 52 | mtmp = rsa->meth; | |
| 53 | if (mtmp->finish) | |
| 54 | mtmp->finish(rsa); | |
| 0b13e9f0 | 55 | #ifndef OPENSSL_NO_ENGINE |
| 7c96dbcd RS |
56 | ENGINE_finish(rsa->engine); |
| 57 | rsa->engine = NULL; | |
| 0b13e9f0 | 58 | #endif |
| 0f113f3e MC |
59 | rsa->meth = meth; |
| 60 | if (meth->init) | |
| 61 | meth->init(rsa); | |
| 62 | return 1; | |
| 63 | } | |
| ce8b2574 | 64 | |
| 5270e702 | 65 | RSA *RSA_new_method(ENGINE *engine) |
| afb638f1 MC |
66 | { |
| 67 | return rsa_new_intern(engine, NULL); | |
| 68 | } | |
| 69 | #endif | |
| 70 | ||
| b4250010 | 71 | RSA *ossl_rsa_new_with_ctx(OSSL_LIB_CTX *libctx) |
| afb638f1 MC |
72 | { |
| 73 | return rsa_new_intern(NULL, libctx); | |
| 74 | } | |
| 75 | ||
| b4250010 | 76 | static RSA *rsa_new_intern(ENGINE *engine, OSSL_LIB_CTX *libctx) |
| 0f113f3e | 77 | { |
| 11ed851d | 78 | RSA *ret = OPENSSL_zalloc(sizeof(*ret)); |
| d02b48c6 | 79 | |
| e077455e | 80 | if (ret == NULL) |
| 0f113f3e | 81 | return NULL; |
| d02b48c6 | 82 | |
| 11ed851d F |
83 | ret->lock = CRYPTO_THREAD_lock_new(); |
| 84 | if (ret->lock == NULL) { | |
| e077455e | 85 | ERR_raise(ERR_LIB_RSA, ERR_R_CRYPTO_LIB); |
| 11ed851d F |
86 | OPENSSL_free(ret); |
| 87 | return NULL; | |
| 88 | } | |
| 89 | ||
| 97beb77f P |
90 | if (!CRYPTO_NEW_REF(&ret->references, 1)) { |
| 91 | CRYPTO_THREAD_lock_free(ret->lock); | |
| 92 | OPENSSL_free(ret); | |
| 93 | return NULL; | |
| 94 | } | |
| 97937cfc | 95 | |
| 902568bb NH |
96 | ret->blindings_sa = ossl_rsa_alloc_blinding(); |
| 97 | if (ret->blindings_sa == NULL) | |
| 98 | goto err; | |
| 99 | ||
| afb638f1 | 100 | ret->libctx = libctx; |
| 0f113f3e | 101 | ret->meth = RSA_get_default_method(); |
| f844f9eb | 102 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
| 11ed851d | 103 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
| 0f113f3e MC |
104 | if (engine) { |
| 105 | if (!ENGINE_init(engine)) { | |
| 9311d0c4 | 106 | ERR_raise(ERR_LIB_RSA, ERR_R_ENGINE_LIB); |
| 11ed851d | 107 | goto err; |
| 0f113f3e MC |
108 | } |
| 109 | ret->engine = engine; | |
| 90862ab4 | 110 | } else { |
| 0f113f3e | 111 | ret->engine = ENGINE_get_default_RSA(); |
| 90862ab4 | 112 | } |
| 0f113f3e MC |
113 | if (ret->engine) { |
| 114 | ret->meth = ENGINE_get_RSA(ret->engine); | |
| 7c96dbcd | 115 | if (ret->meth == NULL) { |
| 9311d0c4 | 116 | ERR_raise(ERR_LIB_RSA, ERR_R_ENGINE_LIB); |
| 11ed851d | 117 | goto err; |
| 0f113f3e MC |
118 | } |
| 119 | } | |
| 0b13e9f0 | 120 | #endif |
| 0c9de428 | 121 | |
| 0f113f3e | 122 | ret->flags = ret->meth->flags & ~RSA_FLAG_NON_FIPS_ALLOW; |
| f844f9eb | 123 | #ifndef FIPS_MODULE |
| 0f113f3e | 124 | if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_RSA, ret, &ret->ex_data)) { |
| 11ed851d | 125 | goto err; |
| d188a536 | 126 | } |
| a3327784 | 127 | #endif |
| d188a536 AG |
128 | |
| 129 | if ((ret->meth->init != NULL) && !ret->meth->init(ret)) { | |
| 9311d0c4 | 130 | ERR_raise(ERR_LIB_RSA, ERR_R_INIT_FAIL); |
| 11ed851d | 131 | goto err; |
| 0f113f3e | 132 | } |
| d188a536 AG |
133 | |
| 134 | return ret; | |
| 11ed851d | 135 | |
| 544648a8 | 136 | err: |
| 11ed851d F |
137 | RSA_free(ret); |
| 138 | return NULL; | |
| 0f113f3e | 139 | } |
| d02b48c6 | 140 | |
| 6b691a5c | 141 | void RSA_free(RSA *r) |
| 0f113f3e MC |
142 | { |
| 143 | int i; | |
| d02b48c6 | 144 | |
| 0f113f3e MC |
145 | if (r == NULL) |
| 146 | return; | |
| d02b48c6 | 147 | |
| 97937cfc | 148 | CRYPTO_DOWN_REF(&r->references, &i); |
| dc10ffc2 | 149 | REF_PRINT_COUNT("RSA", i, r); |
| 0f113f3e MC |
150 | if (i > 0) |
| 151 | return; | |
| f3f1cf84 | 152 | REF_ASSERT_ISNT(i < 0); |
| d02b48c6 | 153 | |
| 0c5d725e | 154 | if (r->meth != NULL && r->meth->finish != NULL) |
| 0f113f3e | 155 | r->meth->finish(r); |
| f844f9eb | 156 | #if !defined(OPENSSL_NO_ENGINE) && !defined(FIPS_MODULE) |
| 412bafdc | 157 | ENGINE_finish(r->engine); |
| 0b13e9f0 | 158 | #endif |
| d02b48c6 | 159 | |
| f844f9eb | 160 | #ifndef FIPS_MODULE |
| 0f113f3e | 161 | CRYPTO_free_ex_data(CRYPTO_EX_INDEX_RSA, r, &r->ex_data); |
| a3327784 | 162 | #endif |
| 7abe8305 | 163 | |
| d188a536 | 164 | CRYPTO_THREAD_lock_free(r->lock); |
| 97937cfc | 165 | CRYPTO_FREE_REF(&r->references); |
| d188a536 | 166 | |
| e73c1faa | 167 | #ifdef OPENSSL_PEDANTIC_ZEROIZATION |
| fa338aa7 DJL |
168 | BN_clear_free(r->n); |
| 169 | BN_clear_free(r->e); | |
| 170 | #else | |
| c033101d MB |
171 | BN_free(r->n); |
| 172 | BN_free(r->e); | |
| fa338aa7 | 173 | #endif |
| 23a1d5e9 RS |
174 | BN_clear_free(r->d); |
| 175 | BN_clear_free(r->p); | |
| 176 | BN_clear_free(r->q); | |
| 177 | BN_clear_free(r->dmp1); | |
| 178 | BN_clear_free(r->dmq1); | |
| 179 | BN_clear_free(r->iqmp); | |
| 4f2271d5 SL |
180 | |
| 181 | #if defined(FIPS_MODULE) && !defined(OPENSSL_NO_ACVP_TESTS) | |
| 4158b0dc | 182 | ossl_rsa_acvp_test_free(r->acvp_test); |
| 4f2271d5 SL |
183 | #endif |
| 184 | ||
| f844f9eb | 185 | #ifndef FIPS_MODULE |
| d771441d | 186 | RSA_PSS_PARAMS_free(r->pss); |
| 4158b0dc | 187 | sk_RSA_PRIME_INFO_pop_free(r->prime_infos, ossl_rsa_multip_info_free); |
| afb638f1 | 188 | #endif |
| 902568bb | 189 | ossl_rsa_free_blinding(r); |
| 0f113f3e MC |
190 | OPENSSL_free(r); |
| 191 | } | |
| d02b48c6 | 192 | |
| 6ac4e8bd | 193 | int RSA_up_ref(RSA *r) |
| 0f113f3e | 194 | { |
| d188a536 AG |
195 | int i; |
| 196 | ||
| 97937cfc | 197 | if (CRYPTO_UP_REF(&r->references, &i) <= 0) |
| d188a536 | 198 | return 0; |
| f3f1cf84 | 199 | |
| dc10ffc2 | 200 | REF_PRINT_COUNT("RSA", i, r); |
| f3f1cf84 | 201 | REF_ASSERT_ISNT(i < 2); |
| 8686c474 | 202 | return i > 1 ? 1 : 0; |
| 0f113f3e | 203 | } |
| 5cbc2e8b | 204 | |
| b4250010 | 205 | OSSL_LIB_CTX *ossl_rsa_get0_libctx(RSA *r) |
| 8a758e96 RL |
206 | { |
| 207 | return r->libctx; | |
| 208 | } | |
| 209 | ||
| 6963979f RL |
210 | void ossl_rsa_set0_libctx(RSA *r, OSSL_LIB_CTX *libctx) |
| 211 | { | |
| 212 | r->libctx = libctx; | |
| 213 | } | |
| 214 | ||
| f844f9eb | 215 | #ifndef FIPS_MODULE |
| dd9d233e | 216 | int RSA_set_ex_data(RSA *r, int idx, void *arg) |
| 0f113f3e | 217 | { |
| 8686c474 | 218 | return CRYPTO_set_ex_data(&r->ex_data, idx, arg); |
| 0f113f3e | 219 | } |
| 58964a49 | 220 | |
| 29c1f061 | 221 | void *RSA_get_ex_data(const RSA *r, int idx) |
| 0f113f3e | 222 | { |
| 8686c474 | 223 | return CRYPTO_get_ex_data(&r->ex_data, idx); |
| 0f113f3e | 224 | } |
| a3327784 | 225 | #endif |
| 58964a49 | 226 | |
| 97b0b713 P |
227 | /* |
| 228 | * Define a scaling constant for our fixed point arithmetic. | |
| 229 | * This value must be a power of two because the base two logarithm code | |
| 230 | * makes this assumption. The exponent must also be a multiple of three so | |
| 231 | * that the scale factor has an exact cube root. Finally, the scale factor | |
| 232 | * should not be so large that a multiplication of two scaled numbers | |
| 233 | * overflows a 64 bit unsigned integer. | |
| 234 | */ | |
| 235 | static const unsigned int scale = 1 << 18; | |
| 236 | static const unsigned int cbrt_scale = 1 << (2 * 18 / 3); | |
| 237 | ||
| 238 | /* Define some constants, none exceed 32 bits */ | |
| 239 | static const unsigned int log_2 = 0x02c5c8; /* scale * log(2) */ | |
| 240 | static const unsigned int log_e = 0x05c551; /* scale * log2(M_E) */ | |
| 241 | static const unsigned int c1_923 = 0x07b126; /* scale * 1.923 */ | |
| 242 | static const unsigned int c4_690 = 0x12c28f; /* scale * 4.690 */ | |
| 243 | ||
| 244 | /* | |
| 2beb004b | 245 | * Multiply two scaled integers together and rescale the result. |
| 97b0b713 P |
246 | */ |
| 247 | static ossl_inline uint64_t mul2(uint64_t a, uint64_t b) | |
| 248 | { | |
| 249 | return a * b / scale; | |
| 250 | } | |
| 251 | ||
| 252 | /* | |
| 253 | * Calculate the cube root of a 64 bit scaled integer. | |
| 254 | * Although the cube root of a 64 bit number does fit into a 32 bit unsigned | |
| 255 | * integer, this is not guaranteed after scaling, so this function has a | |
| 256 | * 64 bit return. This uses the shifting nth root algorithm with some | |
| 257 | * algebraic simplifications. | |
| 258 | */ | |
| 259 | static uint64_t icbrt64(uint64_t x) | |
| 260 | { | |
| 261 | uint64_t r = 0; | |
| 262 | uint64_t b; | |
| 263 | int s; | |
| 264 | ||
| 265 | for (s = 63; s >= 0; s -= 3) { | |
| 266 | r <<= 1; | |
| 267 | b = 3 * r * (r + 1) + 1; | |
| 268 | if ((x >> s) >= b) { | |
| 269 | x -= b << s; | |
| 270 | r++; | |
| 271 | } | |
| 272 | } | |
| 273 | return r * cbrt_scale; | |
| 274 | } | |
| 275 | ||
| 276 | /* | |
| 277 | * Calculate the natural logarithm of a 64 bit scaled integer. | |
| 278 | * This is done by calculating a base two logarithm and scaling. | |
| 279 | * The maximum logarithm (base 2) is 64 and this reduces base e, so | |
| 280 | * a 32 bit result should not overflow. The argument passed must be | |
| 281 | * greater than unity so we don't need to handle negative results. | |
| 282 | */ | |
| 283 | static uint32_t ilog_e(uint64_t v) | |
| 284 | { | |
| 285 | uint32_t i, r = 0; | |
| 286 | ||
| 287 | /* | |
| 288 | * Scale down the value into the range 1 .. 2. | |
| 289 | * | |
| 290 | * If fractional numbers need to be processed, another loop needs | |
| 291 | * to go here that checks v < scale and if so multiplies it by 2 and | |
| 292 | * reduces r by scale. This also means making r signed. | |
| 293 | */ | |
| 294 | while (v >= 2 * scale) { | |
| 295 | v >>= 1; | |
| 296 | r += scale; | |
| 297 | } | |
| 298 | for (i = scale / 2; i != 0; i /= 2) { | |
| 299 | v = mul2(v, v); | |
| 300 | if (v >= 2 * scale) { | |
| 301 | v >>= 1; | |
| 302 | r += i; | |
| 303 | } | |
| 304 | } | |
| 305 | r = (r * (uint64_t)scale) / log_e; | |
| 306 | return r; | |
| 307 | } | |
| 308 | ||
| 309 | /* | |
| 310 | * NIST SP 800-56B rev 2 Appendix D: Maximum Security Strength Estimates for IFC | |
| 311 | * Modulus Lengths. | |
| 312 | * | |
| 55f02cb6 SL |
313 | * Note that this formula is also referred to in SP800-56A rev3 Appendix D: |
| 314 | * for FFC safe prime groups for modp and ffdhe. | |
| 315 | * After Table 25 and Table 26 it refers to | |
| 316 | * "The maximum security strength estimates were calculated using the formula in | |
| 317 | * Section 7.5 of the FIPS 140 IG and rounded to the nearest multiple of eight | |
| 318 | * bits". | |
| 319 | * | |
| 320 | * The formula is: | |
| 321 | * | |
| 97b0b713 P |
322 | * E = \frac{1.923 \sqrt[3]{nBits \cdot log_e(2)} |
| 323 | * \cdot(log_e(nBits \cdot log_e(2))^{2/3} - 4.69}{log_e(2)} | |
| 324 | * The two cube roots are merged together here. | |
| 325 | */ | |
| 9500c823 | 326 | uint16_t ossl_ifc_ffc_compute_security_bits(int n) |
| 97b0b713 P |
327 | { |
| 328 | uint64_t x; | |
| 329 | uint32_t lx; | |
| 1cf520e9 | 330 | uint16_t y, cap; |
| 97b0b713 | 331 | |
| 1cf520e9 P |
332 | /* |
| 333 | * Look for common values as listed in standards. | |
| f7e2e513 | 334 | * These values are not exactly equal to the results from the formulae in |
| 1cf520e9 P |
335 | * the standards but are defined to be canonical. |
| 336 | */ | |
| 97b0b713 | 337 | switch (n) { |
| 1cf520e9 | 338 | case 2048: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */ |
| 97b0b713 | 339 | return 112; |
| 1cf520e9 | 340 | case 3072: /* SP 800-56B rev 2 Appendix D and FIPS 140-2 IG 7.5 */ |
| 97b0b713 | 341 | return 128; |
| 1cf520e9 | 342 | case 4096: /* SP 800-56B rev 2 Appendix D */ |
| 97b0b713 | 343 | return 152; |
| 1cf520e9 | 344 | case 6144: /* SP 800-56B rev 2 Appendix D */ |
| 97b0b713 | 345 | return 176; |
| 1cf520e9 P |
346 | case 7680: /* FIPS 140-2 IG 7.5 */ |
| 347 | return 192; | |
| 348 | case 8192: /* SP 800-56B rev 2 Appendix D */ | |
| 97b0b713 | 349 | return 200; |
| 1cf520e9 P |
350 | case 15360: /* FIPS 140-2 IG 7.5 */ |
| 351 | return 256; | |
| 97b0b713 | 352 | } |
| 1cf520e9 | 353 | |
| 97b0b713 P |
354 | /* |
| 355 | * The first incorrect result (i.e. not accurate or off by one low) occurs | |
| 356 | * for n = 699668. The true value here is 1200. Instead of using this n | |
| 357 | * as the check threshold, the smallest n such that the correct result is | |
| 358 | * 1200 is used instead. | |
| 359 | */ | |
| 360 | if (n >= 687737) | |
| 361 | return 1200; | |
| 362 | if (n < 8) | |
| 363 | return 0; | |
| 364 | ||
| 1cf520e9 P |
365 | /* |
| 366 | * To ensure that the output is non-decreasing with respect to n, | |
| 367 | * a cap needs to be applied to the two values where the function over | |
| 368 | * estimates the strength (according to the above fast path). | |
| 369 | */ | |
| 370 | if (n <= 7680) | |
| 371 | cap = 192; | |
| 372 | else if (n <= 15360) | |
| 373 | cap = 256; | |
| 374 | else | |
| 375 | cap = 1200; | |
| 376 | ||
| 97b0b713 P |
377 | x = n * (uint64_t)log_2; |
| 378 | lx = ilog_e(x); | |
| 379 | y = (uint16_t)((mul2(c1_923, icbrt64(mul2(mul2(x, lx), lx))) - c4_690) | |
| 380 | / log_2); | |
| 1cf520e9 P |
381 | y = (y + 4) & ~7; |
| 382 | if (y > cap) | |
| 383 | y = cap; | |
| 384 | return y; | |
| 97b0b713 P |
385 | } |
| 386 | ||
| 55f02cb6 SL |
387 | |
| 388 | ||
| 2514fa79 | 389 | int RSA_security_bits(const RSA *rsa) |
| 0f113f3e | 390 | { |
| 0122add6 AP |
391 | int bits = BN_num_bits(rsa->n); |
| 392 | ||
| f844f9eb | 393 | #ifndef FIPS_MODULE |
| 0122add6 AP |
394 | if (rsa->version == RSA_ASN1_VERSION_MULTI) { |
| 395 | /* This ought to mean that we have private key at hand. */ | |
| 396 | int ex_primes = sk_RSA_PRIME_INFO_num(rsa->prime_infos); | |
| 397 | ||
| 4158b0dc | 398 | if (ex_primes <= 0 || (ex_primes + 2) > ossl_rsa_multip_cap(bits)) |
| 0122add6 AP |
399 | return 0; |
| 400 | } | |
| afb638f1 | 401 | #endif |
| 9500c823 | 402 | return ossl_ifc_ffc_compute_security_bits(bits); |
| 0f113f3e | 403 | } |
| 9862e9aa RL |
404 | |
| 405 | int RSA_set0_key(RSA *r, BIGNUM *n, BIGNUM *e, BIGNUM *d) | |
| 406 | { | |
| fd809cfd | 407 | /* If the fields n and e in r are NULL, the corresponding input |
| 1da12e34 RL |
408 | * parameters MUST be non-NULL for n and e. d may be |
| 409 | * left NULL (in case only the public key is used). | |
| 1da12e34 | 410 | */ |
| b84e1226 MC |
411 | if ((r->n == NULL && n == NULL) |
| 412 | || (r->e == NULL && e == NULL)) | |
| 9862e9aa RL |
413 | return 0; |
| 414 | ||
| 1da12e34 RL |
415 | if (n != NULL) { |
| 416 | BN_free(r->n); | |
| 417 | r->n = n; | |
| 418 | } | |
| 419 | if (e != NULL) { | |
| 420 | BN_free(r->e); | |
| 421 | r->e = e; | |
| 422 | } | |
| 423 | if (d != NULL) { | |
| c033101d | 424 | BN_clear_free(r->d); |
| 1da12e34 | 425 | r->d = d; |
| 311e903d | 426 | BN_set_flags(r->d, BN_FLG_CONSTTIME); |
| 1da12e34 | 427 | } |
| 29be6023 | 428 | r->dirty_cnt++; |
| 9862e9aa RL |
429 | |
| 430 | return 1; | |
| 431 | } | |
| 432 | ||
| 433 | int RSA_set0_factors(RSA *r, BIGNUM *p, BIGNUM *q) | |
| 434 | { | |
| fd809cfd | 435 | /* If the fields p and q in r are NULL, the corresponding input |
| 1da12e34 | 436 | * parameters MUST be non-NULL. |
| 1da12e34 | 437 | */ |
| b84e1226 MC |
438 | if ((r->p == NULL && p == NULL) |
| 439 | || (r->q == NULL && q == NULL)) | |
| 9862e9aa RL |
440 | return 0; |
| 441 | ||
| 1da12e34 | 442 | if (p != NULL) { |
| c033101d | 443 | BN_clear_free(r->p); |
| 1da12e34 | 444 | r->p = p; |
| 311e903d | 445 | BN_set_flags(r->p, BN_FLG_CONSTTIME); |
| 1da12e34 RL |
446 | } |
| 447 | if (q != NULL) { | |
| c033101d | 448 | BN_clear_free(r->q); |
| 1da12e34 | 449 | r->q = q; |
| 311e903d | 450 | BN_set_flags(r->q, BN_FLG_CONSTTIME); |
| 1da12e34 | 451 | } |
| 29be6023 | 452 | r->dirty_cnt++; |
| 9862e9aa RL |
453 | |
| 454 | return 1; | |
| 455 | } | |
| 456 | ||
| 457 | int RSA_set0_crt_params(RSA *r, BIGNUM *dmp1, BIGNUM *dmq1, BIGNUM *iqmp) | |
| 458 | { | |
| fd809cfd | 459 | /* If the fields dmp1, dmq1 and iqmp in r are NULL, the corresponding input |
| 1da12e34 | 460 | * parameters MUST be non-NULL. |
| 1da12e34 | 461 | */ |
| b84e1226 MC |
462 | if ((r->dmp1 == NULL && dmp1 == NULL) |
| 463 | || (r->dmq1 == NULL && dmq1 == NULL) | |
| 464 | || (r->iqmp == NULL && iqmp == NULL)) | |
| 9862e9aa RL |
465 | return 0; |
| 466 | ||
| 1da12e34 | 467 | if (dmp1 != NULL) { |
| c033101d | 468 | BN_clear_free(r->dmp1); |
| 1da12e34 | 469 | r->dmp1 = dmp1; |
| 311e903d | 470 | BN_set_flags(r->dmp1, BN_FLG_CONSTTIME); |
| 1da12e34 RL |
471 | } |
| 472 | if (dmq1 != NULL) { | |
| c033101d | 473 | BN_clear_free(r->dmq1); |
| 1da12e34 | 474 | r->dmq1 = dmq1; |
| 311e903d | 475 | BN_set_flags(r->dmq1, BN_FLG_CONSTTIME); |
| 1da12e34 RL |
476 | } |
| 477 | if (iqmp != NULL) { | |
| c033101d | 478 | BN_clear_free(r->iqmp); |
| 1da12e34 | 479 | r->iqmp = iqmp; |
| 311e903d | 480 | BN_set_flags(r->iqmp, BN_FLG_CONSTTIME); |
| 1da12e34 | 481 | } |
| 29be6023 | 482 | r->dirty_cnt++; |
| 9862e9aa RL |
483 | |
| 484 | return 1; | |
| 485 | } | |
| 486 | ||
| f844f9eb | 487 | #ifndef FIPS_MODULE |
| 665d899f PY |
488 | /* |
| 489 | * Is it better to export RSA_PRIME_INFO structure | |
| 490 | * and related functions to let user pass a triplet? | |
| 491 | */ | |
| 492 | int RSA_set0_multi_prime_params(RSA *r, BIGNUM *primes[], BIGNUM *exps[], | |
| 493 | BIGNUM *coeffs[], int pnum) | |
| 494 | { | |
| 495 | STACK_OF(RSA_PRIME_INFO) *prime_infos, *old = NULL; | |
| 496 | RSA_PRIME_INFO *pinfo; | |
| 497 | int i; | |
| 498 | ||
| 499 | if (primes == NULL || exps == NULL || coeffs == NULL || pnum == 0) | |
| 500 | return 0; | |
| 501 | ||
| 502 | prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); | |
| 503 | if (prime_infos == NULL) | |
| 504 | return 0; | |
| 505 | ||
| 506 | if (r->prime_infos != NULL) | |
| 507 | old = r->prime_infos; | |
| 508 | ||
| 509 | for (i = 0; i < pnum; i++) { | |
| 4158b0dc | 510 | pinfo = ossl_rsa_multip_info_new(); |
| 665d899f PY |
511 | if (pinfo == NULL) |
| 512 | goto err; | |
| 513 | if (primes[i] != NULL && exps[i] != NULL && coeffs[i] != NULL) { | |
| d2baf88c CPG |
514 | BN_clear_free(pinfo->r); |
| 515 | BN_clear_free(pinfo->d); | |
| 516 | BN_clear_free(pinfo->t); | |
| 665d899f PY |
517 | pinfo->r = primes[i]; |
| 518 | pinfo->d = exps[i]; | |
| 519 | pinfo->t = coeffs[i]; | |
| d2baf88c CPG |
520 | BN_set_flags(pinfo->r, BN_FLG_CONSTTIME); |
| 521 | BN_set_flags(pinfo->d, BN_FLG_CONSTTIME); | |
| 522 | BN_set_flags(pinfo->t, BN_FLG_CONSTTIME); | |
| 665d899f | 523 | } else { |
| 4158b0dc | 524 | ossl_rsa_multip_info_free(pinfo); |
| 665d899f PY |
525 | goto err; |
| 526 | } | |
| 527 | (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); | |
| 528 | } | |
| 529 | ||
| 530 | r->prime_infos = prime_infos; | |
| 531 | ||
| 4158b0dc | 532 | if (!ossl_rsa_multip_calc_product(r)) { |
| 665d899f PY |
533 | r->prime_infos = old; |
| 534 | goto err; | |
| 535 | } | |
| 536 | ||
| 537 | if (old != NULL) { | |
| 538 | /* | |
| 539 | * This is hard to deal with, since the old infos could | |
| 540 | * also be set by this function and r, d, t should not | |
| 541 | * be freed in that case. So currently, stay consistent | |
| 542 | * with other *set0* functions: just free it... | |
| 543 | */ | |
| 4158b0dc | 544 | sk_RSA_PRIME_INFO_pop_free(old, ossl_rsa_multip_info_free); |
| 665d899f PY |
545 | } |
| 546 | ||
| 547 | r->version = RSA_ASN1_VERSION_MULTI; | |
| 29be6023 | 548 | r->dirty_cnt++; |
| 665d899f PY |
549 | |
| 550 | return 1; | |
| 551 | err: | |
| 552 | /* r, d, t should not be freed */ | |
| 4158b0dc | 553 | sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex); |
| 665d899f PY |
554 | return 0; |
| 555 | } | |
| afb638f1 | 556 | #endif |
| 665d899f | 557 | |
| fd809cfd RL |
558 | void RSA_get0_key(const RSA *r, |
| 559 | const BIGNUM **n, const BIGNUM **e, const BIGNUM **d) | |
| 9862e9aa RL |
560 | { |
| 561 | if (n != NULL) | |
| 562 | *n = r->n; | |
| 563 | if (e != NULL) | |
| 564 | *e = r->e; | |
| 565 | if (d != NULL) | |
| 566 | *d = r->d; | |
| 567 | } | |
| 568 | ||
| fd809cfd | 569 | void RSA_get0_factors(const RSA *r, const BIGNUM **p, const BIGNUM **q) |
| 9862e9aa RL |
570 | { |
| 571 | if (p != NULL) | |
| 572 | *p = r->p; | |
| 573 | if (q != NULL) | |
| 574 | *q = r->q; | |
| 575 | } | |
| 576 | ||
| f844f9eb | 577 | #ifndef FIPS_MODULE |
| 665d899f PY |
578 | int RSA_get_multi_prime_extra_count(const RSA *r) |
| 579 | { | |
| 580 | int pnum; | |
| 581 | ||
| 582 | pnum = sk_RSA_PRIME_INFO_num(r->prime_infos); | |
| 583 | if (pnum <= 0) | |
| 584 | pnum = 0; | |
| 585 | return pnum; | |
| 586 | } | |
| 587 | ||
| 588 | int RSA_get0_multi_prime_factors(const RSA *r, const BIGNUM *primes[]) | |
| 589 | { | |
| 590 | int pnum, i; | |
| 591 | RSA_PRIME_INFO *pinfo; | |
| 592 | ||
| 593 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) | |
| 594 | return 0; | |
| 595 | ||
| 596 | /* | |
| 597 | * return other primes | |
| 598 | * it's caller's responsibility to allocate oth_primes[pnum] | |
| 599 | */ | |
| 600 | for (i = 0; i < pnum; i++) { | |
| 601 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); | |
| 602 | primes[i] = pinfo->r; | |
| 603 | } | |
| 604 | ||
| 605 | return 1; | |
| 606 | } | |
| afb638f1 | 607 | #endif |
| 665d899f | 608 | |
| 9862e9aa | 609 | void RSA_get0_crt_params(const RSA *r, |
| fd809cfd RL |
610 | const BIGNUM **dmp1, const BIGNUM **dmq1, |
| 611 | const BIGNUM **iqmp) | |
| 9862e9aa RL |
612 | { |
| 613 | if (dmp1 != NULL) | |
| 614 | *dmp1 = r->dmp1; | |
| 615 | if (dmq1 != NULL) | |
| 616 | *dmq1 = r->dmq1; | |
| 617 | if (iqmp != NULL) | |
| 618 | *iqmp = r->iqmp; | |
| 619 | } | |
| 620 | ||
| f844f9eb | 621 | #ifndef FIPS_MODULE |
| 665d899f PY |
622 | int RSA_get0_multi_prime_crt_params(const RSA *r, const BIGNUM *exps[], |
| 623 | const BIGNUM *coeffs[]) | |
| 624 | { | |
| 625 | int pnum; | |
| 626 | ||
| 627 | if ((pnum = RSA_get_multi_prime_extra_count(r)) == 0) | |
| 628 | return 0; | |
| 629 | ||
| 630 | /* return other primes */ | |
| 631 | if (exps != NULL || coeffs != NULL) { | |
| 632 | RSA_PRIME_INFO *pinfo; | |
| 633 | int i; | |
| 634 | ||
| 635 | /* it's the user's job to guarantee the buffer length */ | |
| 636 | for (i = 0; i < pnum; i++) { | |
| 637 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); | |
| 638 | if (exps != NULL) | |
| 639 | exps[i] = pinfo->d; | |
| 640 | if (coeffs != NULL) | |
| 641 | coeffs[i] = pinfo->t; | |
| 642 | } | |
| 643 | } | |
| 644 | ||
| 645 | return 1; | |
| 646 | } | |
| afb638f1 | 647 | #endif |
| 665d899f | 648 | |
| 6692ff77 DMSP |
649 | const BIGNUM *RSA_get0_n(const RSA *r) |
| 650 | { | |
| 651 | return r->n; | |
| 652 | } | |
| 653 | ||
| 654 | const BIGNUM *RSA_get0_e(const RSA *r) | |
| 655 | { | |
| 656 | return r->e; | |
| 657 | } | |
| 658 | ||
| 659 | const BIGNUM *RSA_get0_d(const RSA *r) | |
| 660 | { | |
| 661 | return r->d; | |
| 662 | } | |
| 663 | ||
| 664 | const BIGNUM *RSA_get0_p(const RSA *r) | |
| 665 | { | |
| 666 | return r->p; | |
| 667 | } | |
| 668 | ||
| 669 | const BIGNUM *RSA_get0_q(const RSA *r) | |
| 670 | { | |
| 671 | return r->q; | |
| 672 | } | |
| 673 | ||
| 674 | const BIGNUM *RSA_get0_dmp1(const RSA *r) | |
| 675 | { | |
| 676 | return r->dmp1; | |
| 677 | } | |
| 678 | ||
| 679 | const BIGNUM *RSA_get0_dmq1(const RSA *r) | |
| 680 | { | |
| 681 | return r->dmq1; | |
| 682 | } | |
| 683 | ||
| 684 | const BIGNUM *RSA_get0_iqmp(const RSA *r) | |
| 685 | { | |
| 686 | return r->iqmp; | |
| 687 | } | |
| 688 | ||
| 677add38 RL |
689 | const RSA_PSS_PARAMS *RSA_get0_pss_params(const RSA *r) |
| 690 | { | |
| 15671090 RL |
691 | #ifdef FIPS_MODULE |
| 692 | return NULL; | |
| 693 | #else | |
| 677add38 | 694 | return r->pss; |
| 15671090 RL |
695 | #endif |
| 696 | } | |
| 697 | ||
| cf333799 RL |
698 | /* Internal */ |
| 699 | int ossl_rsa_set0_pss_params(RSA *r, RSA_PSS_PARAMS *pss) | |
| 700 | { | |
| 701 | #ifdef FIPS_MODULE | |
| 702 | return 0; | |
| 703 | #else | |
| 704 | RSA_PSS_PARAMS_free(r->pss); | |
| 705 | r->pss = pss; | |
| 706 | return 1; | |
| 707 | #endif | |
| 708 | } | |
| 709 | ||
| 15671090 | 710 | /* Internal */ |
| 23b2fc0b | 711 | RSA_PSS_PARAMS_30 *ossl_rsa_get0_pss_params_30(RSA *r) |
| 15671090 RL |
712 | { |
| 713 | return &r->pss_params; | |
| 677add38 RL |
714 | } |
| 715 | ||
| 9862e9aa RL |
716 | void RSA_clear_flags(RSA *r, int flags) |
| 717 | { | |
| 718 | r->flags &= ~flags; | |
| 719 | } | |
| 720 | ||
| 721 | int RSA_test_flags(const RSA *r, int flags) | |
| 722 | { | |
| 723 | return r->flags & flags; | |
| 724 | } | |
| 725 | ||
| 726 | void RSA_set_flags(RSA *r, int flags) | |
| 727 | { | |
| 728 | r->flags |= flags; | |
| 729 | } | |
| 730 | ||
| 665d899f PY |
731 | int RSA_get_version(RSA *r) |
| 732 | { | |
| 733 | /* { two-prime(0), multi(1) } */ | |
| 734 | return r->version; | |
| 735 | } | |
| 736 | ||
| f844f9eb | 737 | #ifndef FIPS_MODULE |
| e0685d24 | 738 | ENGINE *RSA_get0_engine(const RSA *r) |
| 9862e9aa RL |
739 | { |
| 740 | return r->engine; | |
| 741 | } | |
| e5e04ee3 DSH |
742 | |
| 743 | int RSA_pkey_ctx_ctrl(EVP_PKEY_CTX *ctx, int optype, int cmd, int p1, void *p2) | |
| 744 | { | |
| 745 | /* If key type not RSA or RSA-PSS return error */ | |
| 746 | if (ctx != NULL && ctx->pmeth != NULL | |
| 747 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA | |
| 748 | && ctx->pmeth->pkey_id != EVP_PKEY_RSA_PSS) | |
| 749 | return -1; | |
| 750 | return EVP_PKEY_CTX_ctrl(ctx, -1, optype, cmd, p1, p2); | |
| 751 | } | |
| afb638f1 | 752 | #endif |
| c3a4fa4c RL |
753 | |
| 754 | DEFINE_STACK_OF(BIGNUM) | |
| 755 | ||
| f3be5366 NH |
756 | /* |
| 757 | * Note: This function deletes values from the parameter | |
| 758 | * stack values as they are consumed and set in the RSA key. | |
| 759 | */ | |
| 760 | int ossl_rsa_set0_all_params(RSA *r, STACK_OF(BIGNUM) *primes, | |
| 761 | STACK_OF(BIGNUM) *exps, | |
| 762 | STACK_OF(BIGNUM) *coeffs) | |
| c3a4fa4c | 763 | { |
| f844f9eb | 764 | #ifndef FIPS_MODULE |
| c3a4fa4c | 765 | STACK_OF(RSA_PRIME_INFO) *prime_infos, *old_infos = NULL; |
| afb638f1 | 766 | #endif |
| c3a4fa4c RL |
767 | int pnum; |
| 768 | ||
| 769 | if (primes == NULL || exps == NULL || coeffs == NULL) | |
| 770 | return 0; | |
| 771 | ||
| 772 | pnum = sk_BIGNUM_num(primes); | |
| f3be5366 NH |
773 | |
| 774 | /* we need at least 2 primes */ | |
| 2647726b | 775 | if (pnum < 2) |
| c3a4fa4c RL |
776 | return 0; |
| 777 | ||
| 778 | if (!RSA_set0_factors(r, sk_BIGNUM_value(primes, 0), | |
| 2647726b | 779 | sk_BIGNUM_value(primes, 1))) |
| c3a4fa4c RL |
780 | return 0; |
| 781 | ||
| f3be5366 NH |
782 | /* |
| 783 | * if we managed to set everything above, remove those elements from the | |
| 784 | * stack | |
| 785 | * Note, we do this after the above all to ensure that we have taken | |
| 786 | * ownership of all the elements in the RSA key to avoid memory leaks | |
| 787 | * we also use delete 0 here as we are grabbing items from the end of the | |
| 788 | * stack rather than the start, otherwise we could use pop | |
| 789 | */ | |
| 790 | sk_BIGNUM_delete(primes, 0); | |
| 791 | sk_BIGNUM_delete(primes, 0); | |
| 792 | ||
| 2647726b NH |
793 | if (pnum == sk_BIGNUM_num(exps) |
| 794 | && pnum == sk_BIGNUM_num(coeffs) + 1) { | |
| 795 | ||
| 796 | if (!RSA_set0_crt_params(r, sk_BIGNUM_value(exps, 0), | |
| 797 | sk_BIGNUM_value(exps, 1), | |
| 798 | sk_BIGNUM_value(coeffs, 0))) | |
| 799 | return 0; | |
| f3be5366 NH |
800 | |
| 801 | /* as above, once we consume the above params, delete them from the list */ | |
| 802 | sk_BIGNUM_delete(exps, 0); | |
| 803 | sk_BIGNUM_delete(exps, 0); | |
| 804 | sk_BIGNUM_delete(coeffs, 0); | |
| 2647726b NH |
805 | } |
| 806 | ||
| f844f9eb | 807 | #ifndef FIPS_MODULE |
| c3a4fa4c | 808 | old_infos = r->prime_infos; |
| afb638f1 | 809 | #endif |
| c3a4fa4c RL |
810 | |
| 811 | if (pnum > 2) { | |
| f844f9eb | 812 | #ifndef FIPS_MODULE |
| c3a4fa4c RL |
813 | int i; |
| 814 | ||
| 815 | prime_infos = sk_RSA_PRIME_INFO_new_reserve(NULL, pnum); | |
| 816 | if (prime_infos == NULL) | |
| 817 | return 0; | |
| 818 | ||
| 819 | for (i = 2; i < pnum; i++) { | |
| f3be5366 NH |
820 | BIGNUM *prime = sk_BIGNUM_pop(primes); |
| 821 | BIGNUM *exp = sk_BIGNUM_pop(exps); | |
| 822 | BIGNUM *coeff = sk_BIGNUM_pop(coeffs); | |
| c3a4fa4c RL |
823 | RSA_PRIME_INFO *pinfo = NULL; |
| 824 | ||
| 825 | if (!ossl_assert(prime != NULL && exp != NULL && coeff != NULL)) | |
| 826 | goto err; | |
| 827 | ||
| 4158b0dc | 828 | /* Using ossl_rsa_multip_info_new() is wasteful, so allocate directly */ |
| e077455e | 829 | if ((pinfo = OPENSSL_zalloc(sizeof(*pinfo))) == NULL) |
| c3a4fa4c | 830 | goto err; |
| c3a4fa4c RL |
831 | |
| 832 | pinfo->r = prime; | |
| 833 | pinfo->d = exp; | |
| 834 | pinfo->t = coeff; | |
| 835 | BN_set_flags(pinfo->r, BN_FLG_CONSTTIME); | |
| 836 | BN_set_flags(pinfo->d, BN_FLG_CONSTTIME); | |
| 837 | BN_set_flags(pinfo->t, BN_FLG_CONSTTIME); | |
| 838 | (void)sk_RSA_PRIME_INFO_push(prime_infos, pinfo); | |
| 839 | } | |
| 840 | ||
| 841 | r->prime_infos = prime_infos; | |
| 842 | ||
| 4158b0dc | 843 | if (!ossl_rsa_multip_calc_product(r)) { |
| c3a4fa4c RL |
844 | r->prime_infos = old_infos; |
| 845 | goto err; | |
| 846 | } | |
| afb638f1 MC |
847 | #else |
| 848 | return 0; | |
| 849 | #endif | |
| c3a4fa4c RL |
850 | } |
| 851 | ||
| f844f9eb | 852 | #ifndef FIPS_MODULE |
| c3a4fa4c RL |
853 | if (old_infos != NULL) { |
| 854 | /* | |
| 855 | * This is hard to deal with, since the old infos could | |
| 856 | * also be set by this function and r, d, t should not | |
| 857 | * be freed in that case. So currently, stay consistent | |
| 858 | * with other *set0* functions: just free it... | |
| 859 | */ | |
| 4158b0dc | 860 | sk_RSA_PRIME_INFO_pop_free(old_infos, ossl_rsa_multip_info_free); |
| c3a4fa4c | 861 | } |
| afb638f1 | 862 | #endif |
| c3a4fa4c RL |
863 | |
| 864 | r->version = pnum > 2 ? RSA_ASN1_VERSION_MULTI : RSA_ASN1_VERSION_DEFAULT; | |
| 865 | r->dirty_cnt++; | |
| 866 | ||
| 867 | return 1; | |
| f844f9eb | 868 | #ifndef FIPS_MODULE |
| c3a4fa4c RL |
869 | err: |
| 870 | /* r, d, t should not be freed */ | |
| 4158b0dc | 871 | sk_RSA_PRIME_INFO_pop_free(prime_infos, ossl_rsa_multip_info_free_ex); |
| c3a4fa4c | 872 | return 0; |
| afb638f1 | 873 | #endif |
| c3a4fa4c RL |
874 | } |
| 875 | ||
| 876 | DEFINE_SPECIAL_STACK_OF_CONST(BIGNUM_const, BIGNUM) | |
| 877 | ||
| 23b2fc0b P |
878 | int ossl_rsa_get0_all_params(RSA *r, STACK_OF(BIGNUM_const) *primes, |
| 879 | STACK_OF(BIGNUM_const) *exps, | |
| 880 | STACK_OF(BIGNUM_const) *coeffs) | |
| c3a4fa4c | 881 | { |
| f844f9eb | 882 | #ifndef FIPS_MODULE |
| c3a4fa4c RL |
883 | RSA_PRIME_INFO *pinfo; |
| 884 | int i, pnum; | |
| afb638f1 | 885 | #endif |
| c3a4fa4c RL |
886 | |
| 887 | if (r == NULL) | |
| 888 | return 0; | |
| 889 | ||
| a9127c1d RL |
890 | /* If |p| is NULL, there are no CRT parameters */ |
| 891 | if (RSA_get0_p(r) == NULL) | |
| 892 | return 1; | |
| 893 | ||
| c3a4fa4c RL |
894 | sk_BIGNUM_const_push(primes, RSA_get0_p(r)); |
| 895 | sk_BIGNUM_const_push(primes, RSA_get0_q(r)); | |
| 896 | sk_BIGNUM_const_push(exps, RSA_get0_dmp1(r)); | |
| 897 | sk_BIGNUM_const_push(exps, RSA_get0_dmq1(r)); | |
| 898 | sk_BIGNUM_const_push(coeffs, RSA_get0_iqmp(r)); | |
| afb638f1 | 899 | |
| f844f9eb | 900 | #ifndef FIPS_MODULE |
| afb638f1 | 901 | pnum = RSA_get_multi_prime_extra_count(r); |
| c3a4fa4c RL |
902 | for (i = 0; i < pnum; i++) { |
| 903 | pinfo = sk_RSA_PRIME_INFO_value(r->prime_infos, i); | |
| 904 | sk_BIGNUM_const_push(primes, pinfo->r); | |
| 905 | sk_BIGNUM_const_push(exps, pinfo->d); | |
| 906 | sk_BIGNUM_const_push(coeffs, pinfo->t); | |
| 907 | } | |
| afb638f1 | 908 | #endif |
| c3a4fa4c RL |
909 | |
| 910 | return 1; | |
| 911 | } | |
| 89abd1b6 | 912 | |
| 6dacee48 | 913 | #define safe_BN_num_bits(_k_) (((_k_) == NULL) ? 0 : BN_num_bits((_k_))) |
| 914 | int ossl_rsa_check_factors(RSA *r) | |
| 915 | { | |
| 916 | int valid = 0; | |
| 917 | int n, i, bits; | |
| 918 | STACK_OF(BIGNUM_const) *factors = sk_BIGNUM_const_new_null(); | |
| 919 | STACK_OF(BIGNUM_const) *exps = sk_BIGNUM_const_new_null(); | |
| 920 | STACK_OF(BIGNUM_const) *coeffs = sk_BIGNUM_const_new_null(); | |
| 921 | ||
| 922 | if (factors == NULL || exps == NULL || coeffs == NULL) | |
| 923 | goto done; | |
| 924 | ||
| 925 | /* | |
| 926 | * Simple sanity check for RSA key. All RSA key parameters | |
| 927 | * must be less-than/equal-to RSA parameter n. | |
| 928 | */ | |
| 929 | ossl_rsa_get0_all_params(r, factors, exps, coeffs); | |
| 930 | n = safe_BN_num_bits(RSA_get0_n(r)); | |
| 931 | ||
| 932 | if (safe_BN_num_bits(RSA_get0_d(r)) > n) | |
| 933 | goto done; | |
| 934 | ||
| 935 | for (i = 0; i < sk_BIGNUM_const_num(exps); i++) { | |
| 936 | bits = safe_BN_num_bits(sk_BIGNUM_const_value(exps, i)); | |
| 937 | if (bits > n) | |
| 938 | goto done; | |
| 939 | } | |
| 940 | ||
| 941 | for (i = 0; i < sk_BIGNUM_const_num(factors); i++) { | |
| 942 | bits = safe_BN_num_bits(sk_BIGNUM_const_value(factors, i)); | |
| 943 | if (bits > n) | |
| 944 | goto done; | |
| 945 | } | |
| 946 | ||
| 947 | for (i = 0; i < sk_BIGNUM_const_num(coeffs); i++) { | |
| 948 | bits = safe_BN_num_bits(sk_BIGNUM_const_value(coeffs, i)); | |
| 949 | if (bits > n) | |
| 950 | goto done; | |
| 951 | } | |
| 952 | ||
| 953 | valid = 1; | |
| 954 | ||
| 955 | done: | |
| 956 | sk_BIGNUM_const_free(factors); | |
| 957 | sk_BIGNUM_const_free(exps); | |
| 958 | sk_BIGNUM_const_free(coeffs); | |
| 959 | ||
| 960 | return valid; | |
| 961 | } | |
| 962 | ||
| f844f9eb | 963 | #ifndef FIPS_MODULE |
| 13f91a72 RL |
964 | /* Helpers to set or get diverse hash algorithm names */ |
| 965 | static int int_set_rsa_md_name(EVP_PKEY_CTX *ctx, | |
| 966 | /* For checks */ | |
| 967 | int keytype, int optype, | |
| 968 | /* For EVP_PKEY_CTX_set_params() */ | |
| 969 | const char *mdkey, const char *mdname, | |
| 970 | const char *propkey, const char *mdprops) | |
| 89abd1b6 | 971 | { |
| 13f91a72 | 972 | OSSL_PARAM params[3], *p = params; |
| 89abd1b6 | 973 | |
| 13f91a72 | 974 | if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) { |
| 89abd1b6 MC |
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 | ||
| 13f91a72 RL |
980 | /* If key type not RSA return error */ |
| 981 | switch (keytype) { | |
| 982 | case -1: | |
| 983 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA") | |
| 984 | && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS")) | |
| 985 | return -1; | |
| 986 | break; | |
| 987 | default: | |
| 988 | if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype))) | |
| 989 | return -1; | |
| 990 | break; | |
| 991 | } | |
| 89abd1b6 | 992 | |
| 13f91a72 RL |
993 | /* Cast away the const. This is read only so should be safe */ |
| 994 | *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, 0); | |
| 995 | if (evp_pkey_ctx_is_provided(ctx) && mdprops != NULL) { | |
| 996 | /* Cast away the const. This is read only so should be safe */ | |
| 997 | *p++ = OSSL_PARAM_construct_utf8_string(propkey, (char *)mdprops, 0); | |
| 998 | } | |
| 89abd1b6 MC |
999 | *p++ = OSSL_PARAM_construct_end(); |
| 1000 | ||
| 13f91a72 | 1001 | return evp_pkey_ctx_set_params_strict(ctx, params); |
| 89abd1b6 MC |
1002 | } |
| 1003 | ||
| 13f91a72 RL |
1004 | /* Helpers to set or get diverse hash algorithm names */ |
| 1005 | static int int_get_rsa_md_name(EVP_PKEY_CTX *ctx, | |
| 1006 | /* For checks */ | |
| 1007 | int keytype, int optype, | |
| 1008 | /* For EVP_PKEY_CTX_get_params() */ | |
| 1009 | const char *mdkey, | |
| 1010 | char *mdname, size_t mdnamesize) | |
| 89abd1b6 | 1011 | { |
| 13f91a72 | 1012 | OSSL_PARAM params[2], *p = params; |
| 89abd1b6 | 1013 | |
| 13f91a72 | 1014 | if (ctx == NULL || mdname == NULL || (ctx->operation & optype) == 0) { |
| 89abd1b6 MC |
1015 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1016 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
| 1017 | return -2; | |
| 1018 | } | |
| 1019 | ||
| 13f91a72 RL |
1020 | /* If key type not RSA return error */ |
| 1021 | switch (keytype) { | |
| 1022 | case -1: | |
| 1023 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA") | |
| 1024 | && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS")) | |
| 1025 | return -1; | |
| 1026 | break; | |
| 1027 | default: | |
| 1028 | if (!EVP_PKEY_CTX_is_a(ctx, evp_pkey_type2name(keytype))) | |
| 1029 | return -1; | |
| 1030 | break; | |
| 1031 | } | |
| 89abd1b6 | 1032 | |
| 13f91a72 RL |
1033 | /* Cast away the const. This is read only so should be safe */ |
| 1034 | *p++ = OSSL_PARAM_construct_utf8_string(mdkey, (char *)mdname, mdnamesize); | |
| 89abd1b6 MC |
1035 | *p++ = OSSL_PARAM_construct_end(); |
| 1036 | ||
| 13f91a72 RL |
1037 | return evp_pkey_ctx_get_params_strict(ctx, params); |
| 1038 | } | |
| 89abd1b6 | 1039 | |
| 13f91a72 RL |
1040 | /* |
| 1041 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1042 | * simply because that's easier. | |
| 13f91a72 RL |
1043 | */ |
| 1044 | int EVP_PKEY_CTX_set_rsa_padding(EVP_PKEY_CTX *ctx, int pad_mode) | |
| 1045 | { | |
| 1046 | return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_RSA_PADDING, | |
| 1047 | pad_mode, NULL); | |
| 1048 | } | |
| 89abd1b6 | 1049 | |
| 13f91a72 RL |
1050 | /* |
| 1051 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1052 | * simply because that's easier. | |
| 13f91a72 RL |
1053 | */ |
| 1054 | int EVP_PKEY_CTX_get_rsa_padding(EVP_PKEY_CTX *ctx, int *pad_mode) | |
| 1055 | { | |
| 1056 | return RSA_pkey_ctx_ctrl(ctx, -1, EVP_PKEY_CTRL_GET_RSA_PADDING, | |
| 1057 | 0, pad_mode); | |
| 89abd1b6 MC |
1058 | } |
| 1059 | ||
| 13f91a72 RL |
1060 | /* |
| 1061 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1062 | * simply because that's easier. | |
| 13f91a72 | 1063 | */ |
| e947a064 DB |
1064 | int EVP_PKEY_CTX_set_rsa_pss_keygen_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
| 1065 | { | |
| 13f91a72 RL |
1066 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN, |
| 1067 | EVP_PKEY_CTRL_MD, 0, (void *)(md)); | |
| e947a064 DB |
1068 | } |
| 1069 | ||
| 1070 | int EVP_PKEY_CTX_set_rsa_pss_keygen_md_name(EVP_PKEY_CTX *ctx, | |
| 1071 | const char *mdname, | |
| 1072 | const char *mdprops) | |
| 1073 | { | |
| 13f91a72 RL |
1074 | return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN, |
| 1075 | OSSL_PKEY_PARAM_RSA_DIGEST, mdname, | |
| 1076 | OSSL_PKEY_PARAM_RSA_DIGEST_PROPS, mdprops); | |
| e947a064 DB |
1077 | } |
| 1078 | ||
| 13f91a72 RL |
1079 | /* |
| 1080 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1081 | * simply because that's easier. | |
| 13f91a72 | 1082 | */ |
| 89abd1b6 MC |
1083 | int EVP_PKEY_CTX_set_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
| 1084 | { | |
| 0c3eb31b | 1085 | /* If key type not RSA return error */ |
| 1086 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA")) | |
| 1087 | return -1; | |
| 1088 | ||
| 13f91a72 RL |
1089 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 1090 | EVP_PKEY_CTRL_RSA_OAEP_MD, 0, (void *)(md)); | |
| 89abd1b6 MC |
1091 | } |
| 1092 | ||
| 1093 | int EVP_PKEY_CTX_set_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, const char *mdname, | |
| 1094 | const char *mdprops) | |
| 1095 | { | |
| 13f91a72 RL |
1096 | return |
| 1097 | int_set_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, | |
| 1098 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, mdname, | |
| 1099 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST_PROPS, mdprops); | |
| 89abd1b6 MC |
1100 | } |
| 1101 | ||
| 1102 | int EVP_PKEY_CTX_get_rsa_oaep_md_name(EVP_PKEY_CTX *ctx, char *name, | |
| 13f91a72 | 1103 | size_t namesize) |
| 89abd1b6 | 1104 | { |
| 13f91a72 RL |
1105 | return int_get_rsa_md_name(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 1106 | OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, | |
| 1107 | name, namesize); | |
| 89abd1b6 MC |
1108 | } |
| 1109 | ||
| 13f91a72 RL |
1110 | /* |
| 1111 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1112 | * simply because that's easier. | |
| 13f91a72 | 1113 | */ |
| 89abd1b6 MC |
1114 | int EVP_PKEY_CTX_get_rsa_oaep_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
| 1115 | { | |
| 0c3eb31b | 1116 | /* If key type not RSA return error */ |
| 1117 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA")) | |
| 1118 | return -1; | |
| 1119 | ||
| 13f91a72 RL |
1120 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_TYPE_CRYPT, |
| 1121 | EVP_PKEY_CTRL_GET_RSA_OAEP_MD, 0, (void *)md); | |
| 89abd1b6 MC |
1122 | } |
| 1123 | ||
| 13f91a72 RL |
1124 | /* |
| 1125 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1126 | * simply because that's easier. | |
| 13f91a72 RL |
1127 | */ |
| 1128 | int EVP_PKEY_CTX_set_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) | |
| 89abd1b6 | 1129 | { |
| 13f91a72 RL |
1130 | return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
| 1131 | EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md)); | |
| 89abd1b6 MC |
1132 | } |
| 1133 | ||
| 13f91a72 RL |
1134 | int EVP_PKEY_CTX_set_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, const char *mdname, |
| 1135 | const char *mdprops) | |
| e25761b1 | 1136 | { |
| 13f91a72 | 1137 | return int_set_rsa_md_name(ctx, -1, |
| e25761b1 | 1138 | EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG, |
| 13f91a72 RL |
1139 | OSSL_PKEY_PARAM_MGF1_DIGEST, mdname, |
| 1140 | OSSL_PKEY_PARAM_MGF1_PROPERTIES, mdprops); | |
| e25761b1 RL |
1141 | } |
| 1142 | ||
| 13f91a72 RL |
1143 | int EVP_PKEY_CTX_get_rsa_mgf1_md_name(EVP_PKEY_CTX *ctx, char *name, |
| 1144 | size_t namesize) | |
| e25761b1 | 1145 | { |
| 13f91a72 | 1146 | return int_get_rsa_md_name(ctx, -1, |
| e25761b1 | 1147 | EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG, |
| 13f91a72 | 1148 | OSSL_PKEY_PARAM_MGF1_DIGEST, name, namesize); |
| e25761b1 RL |
1149 | } |
| 1150 | ||
| 13f91a72 RL |
1151 | /* |
| 1152 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1153 | * simply because that's easier. | |
| 13f91a72 | 1154 | */ |
| e25761b1 RL |
1155 | int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD *md) |
| 1156 | { | |
| 13f91a72 RL |
1157 | return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN, |
| 1158 | EVP_PKEY_CTRL_RSA_MGF1_MD, 0, (void *)(md)); | |
| e25761b1 RL |
1159 | } |
| 1160 | ||
| 1161 | int EVP_PKEY_CTX_set_rsa_pss_keygen_mgf1_md_name(EVP_PKEY_CTX *ctx, | |
| 1162 | const char *mdname) | |
| 1163 | { | |
| 13f91a72 RL |
1164 | return int_set_rsa_md_name(ctx, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN, |
| 1165 | OSSL_PKEY_PARAM_MGF1_DIGEST, mdname, | |
| 1166 | NULL, NULL); | |
| 89abd1b6 MC |
1167 | } |
| 1168 | ||
| 13f91a72 RL |
1169 | /* |
| 1170 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1171 | * simply because that's easier. | |
| 13f91a72 | 1172 | */ |
| 89abd1b6 MC |
1173 | int EVP_PKEY_CTX_get_rsa_mgf1_md(EVP_PKEY_CTX *ctx, const EVP_MD **md) |
| 1174 | { | |
| 13f91a72 RL |
1175 | return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG | EVP_PKEY_OP_TYPE_CRYPT, |
| 1176 | EVP_PKEY_CTRL_GET_RSA_MGF1_MD, 0, (void *)(md)); | |
| 89abd1b6 MC |
1177 | } |
| 1178 | ||
| 1179 | int EVP_PKEY_CTX_set0_rsa_oaep_label(EVP_PKEY_CTX *ctx, void *label, int llen) | |
| 1180 | { | |
| 1181 | OSSL_PARAM rsa_params[2], *p = rsa_params; | |
| 21b98da9 DU |
1182 | const char *empty = ""; |
| 1183 | /* | |
| 1184 | * Needed as we swap label with empty if it is NULL, and label is | |
| 1185 | * freed at the end of this function. | |
| 1186 | */ | |
| 1187 | void *plabel = label; | |
| 00d5193b | 1188 | int ret; |
| 89abd1b6 MC |
1189 | |
| 1190 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
| 1191 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
| 1192 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
| 1193 | return -2; | |
| 1194 | } | |
| 1195 | ||
| 1196 | /* If key type not RSA return error */ | |
| 13f91a72 | 1197 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA")) |
| 89abd1b6 MC |
1198 | return -1; |
| 1199 | ||
| 21b98da9 DU |
1200 | /* Accept NULL for backward compatibility */ |
| 1201 | if (label == NULL && llen == 0) | |
| 1202 | plabel = (void *)empty; | |
| 1203 | ||
| 13f91a72 | 1204 | /* Cast away the const. This is read only so should be safe */ |
| 89abd1b6 | 1205 | *p++ = OSSL_PARAM_construct_octet_string(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, |
| 21b98da9 | 1206 | (void *)plabel, (size_t)llen); |
| 89abd1b6 MC |
1207 | *p++ = OSSL_PARAM_construct_end(); |
| 1208 | ||
| 00d5193b PH |
1209 | ret = evp_pkey_ctx_set_params_strict(ctx, rsa_params); |
| 1210 | if (ret <= 0) | |
| 1211 | return ret; | |
| 89abd1b6 | 1212 | |
| e304aa87 | 1213 | /* Ownership is supposed to be transferred to the callee. */ |
| 89abd1b6 MC |
1214 | OPENSSL_free(label); |
| 1215 | return 1; | |
| 1216 | } | |
| 1217 | ||
| 1218 | int EVP_PKEY_CTX_get0_rsa_oaep_label(EVP_PKEY_CTX *ctx, unsigned char **label) | |
| 1219 | { | |
| ba0a6d1d | 1220 | OSSL_PARAM rsa_params[2], *p = rsa_params; |
| 89abd1b6 MC |
1221 | size_t labellen; |
| 1222 | ||
| 1223 | if (ctx == NULL || !EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx)) { | |
| 1224 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
| 1225 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
| 1226 | return -2; | |
| 1227 | } | |
| 1228 | ||
| 1229 | /* If key type not RSA return error */ | |
| 13f91a72 | 1230 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA")) |
| 89abd1b6 MC |
1231 | return -1; |
| 1232 | ||
| 89abd1b6 MC |
1233 | *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, |
| 1234 | (void **)label, 0); | |
| 89abd1b6 MC |
1235 | *p++ = OSSL_PARAM_construct_end(); |
| 1236 | ||
| 1237 | if (!EVP_PKEY_CTX_get_params(ctx, rsa_params)) | |
| 1238 | return -1; | |
| 1239 | ||
| ba0a6d1d | 1240 | labellen = rsa_params[0].return_size; |
| 89abd1b6 MC |
1241 | if (labellen > INT_MAX) |
| 1242 | return -1; | |
| 1243 | ||
| 1244 | return (int)labellen; | |
| 1245 | } | |
| 6f4b7663 | 1246 | |
| 13f91a72 RL |
1247 | /* |
| 1248 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1249 | * simply because that's easier. | |
| 1250 | */ | |
| e25761b1 RL |
1251 | int EVP_PKEY_CTX_set_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int saltlen) |
| 1252 | { | |
| 13f91a72 RL |
1253 | /* |
| 1254 | * For some reason, the optype was set to this: | |
| 1255 | * | |
| 1256 | * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY | |
| 1257 | * | |
| 1258 | * However, we do use RSA-PSS with the whole gamut of diverse signature | |
| 1259 | * and verification operations, so the optype gets upgraded to this: | |
| 1260 | * | |
| 1261 | * EVP_PKEY_OP_TYPE_SIG | |
| 1262 | */ | |
| 1263 | return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG, | |
| 1264 | EVP_PKEY_CTRL_RSA_PSS_SALTLEN, saltlen, NULL); | |
| e25761b1 RL |
1265 | } |
| 1266 | ||
| 13f91a72 RL |
1267 | /* |
| 1268 | * This one is currently implemented as an EVP_PKEY_CTX_ctrl() wrapper, | |
| 1269 | * simply because that's easier. | |
| 1270 | */ | |
| 1271 | int EVP_PKEY_CTX_get_rsa_pss_saltlen(EVP_PKEY_CTX *ctx, int *saltlen) | |
| e25761b1 | 1272 | { |
| 13f91a72 RL |
1273 | /* |
| 1274 | * Because of circumstances, the optype is updated from: | |
| 1275 | * | |
| 1276 | * EVP_PKEY_OP_SIGN|EVP_PKEY_OP_VERIFY | |
| 1277 | * | |
| 1278 | * to: | |
| 1279 | * | |
| 1280 | * EVP_PKEY_OP_TYPE_SIG | |
| 1281 | */ | |
| 1282 | return RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_TYPE_SIG, | |
| 1283 | EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, 0, saltlen); | |
| e25761b1 RL |
1284 | } |
| 1285 | ||
| 13f91a72 | 1286 | int EVP_PKEY_CTX_set_rsa_pss_keygen_saltlen(EVP_PKEY_CTX *ctx, int saltlen) |
| 6f4b7663 RL |
1287 | { |
| 1288 | OSSL_PARAM pad_params[2], *p = pad_params; | |
| 1289 | ||
| 13f91a72 | 1290 | if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) { |
| 6f4b7663 RL |
1291 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); |
| 1292 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
| 1293 | return -2; | |
| 1294 | } | |
| 1295 | ||
| 13f91a72 | 1296 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA-PSS")) |
| 6f4b7663 RL |
1297 | return -1; |
| 1298 | ||
| 13f91a72 RL |
1299 | *p++ = OSSL_PARAM_construct_int(OSSL_SIGNATURE_PARAM_PSS_SALTLEN, |
| 1300 | &saltlen); | |
| 6f4b7663 RL |
1301 | *p++ = OSSL_PARAM_construct_end(); |
| 1302 | ||
| 13f91a72 | 1303 | return evp_pkey_ctx_set_params_strict(ctx, pad_params); |
| 6f4b7663 | 1304 | } |
| 2972af10 RL |
1305 | |
| 1306 | int EVP_PKEY_CTX_set_rsa_keygen_bits(EVP_PKEY_CTX *ctx, int bits) | |
| 1307 | { | |
| 1308 | OSSL_PARAM params[2], *p = params; | |
| 1309 | size_t bits2 = bits; | |
| 1310 | ||
| 1311 | if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) { | |
| 1312 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
| 1313 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
| 1314 | return -2; | |
| 1315 | } | |
| 1316 | ||
| 1317 | /* If key type not RSA return error */ | |
| 13f91a72 RL |
1318 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA") |
| 1319 | && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS")) | |
| 2972af10 RL |
1320 | return -1; |
| 1321 | ||
| 2972af10 RL |
1322 | *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_BITS, &bits2); |
| 1323 | *p++ = OSSL_PARAM_construct_end(); | |
| 1324 | ||
| 13f91a72 | 1325 | return evp_pkey_ctx_set_params_strict(ctx, params); |
| 2972af10 RL |
1326 | } |
| 1327 | ||
| 13f91a72 | 1328 | int EVP_PKEY_CTX_set_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp) |
| 2972af10 | 1329 | { |
| 13f91a72 | 1330 | int ret = RSA_pkey_ctx_ctrl(ctx, EVP_PKEY_OP_KEYGEN, |
| 3786d748 | 1331 | EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp); |
| 3786d748 | 1332 | |
| 1333 | /* | |
| 1334 | * Satisfy memory semantics for pre-3.0 callers of | |
| 1335 | * EVP_PKEY_CTX_set_rsa_keygen_pubexp(): their expectation is that input | |
| 1336 | * pubexp BIGNUM becomes managed by the EVP_PKEY_CTX on success. | |
| 1337 | */ | |
| 13f91a72 RL |
1338 | if (ret > 0 && evp_pkey_ctx_is_provided(ctx)) { |
| 1339 | BN_free(ctx->rsa_pubexp); | |
| 3786d748 | 1340 | ctx->rsa_pubexp = pubexp; |
| 13f91a72 | 1341 | } |
| 3786d748 | 1342 | |
| 2972af10 RL |
1343 | return ret; |
| 1344 | } | |
| 1345 | ||
| 3786d748 | 1346 | int EVP_PKEY_CTX_set1_rsa_keygen_pubexp(EVP_PKEY_CTX *ctx, BIGNUM *pubexp) |
| 1347 | { | |
| 13f91a72 RL |
1348 | int ret = 0; |
| 1349 | ||
| 1350 | /* | |
| 1351 | * When we're dealing with a provider, there's no need to duplicate | |
| 1352 | * pubexp, as it gets copied when transforming to an OSSL_PARAM anyway. | |
| 1353 | */ | |
| 9d1a2705 | 1354 | if (evp_pkey_ctx_is_legacy(ctx)) { |
| 13f91a72 | 1355 | pubexp = BN_dup(pubexp); |
| 9d1a2705 | 1356 | if (pubexp == NULL) |
| 1357 | return 0; | |
| 1358 | } | |
| 13f91a72 RL |
1359 | ret = EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_RSA, EVP_PKEY_OP_KEYGEN, |
| 1360 | EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, 0, pubexp); | |
| 1361 | if (evp_pkey_ctx_is_legacy(ctx) && ret <= 0) | |
| 1362 | BN_free(pubexp); | |
| 1363 | return ret; | |
| 3786d748 | 1364 | } |
| 1365 | ||
| 2972af10 RL |
1366 | int EVP_PKEY_CTX_set_rsa_keygen_primes(EVP_PKEY_CTX *ctx, int primes) |
| 1367 | { | |
| 1368 | OSSL_PARAM params[2], *p = params; | |
| 1369 | size_t primes2 = primes; | |
| 1370 | ||
| 1371 | if (ctx == NULL || !EVP_PKEY_CTX_IS_GEN_OP(ctx)) { | |
| 1372 | ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED); | |
| 1373 | /* Uses the same return values as EVP_PKEY_CTX_ctrl */ | |
| 1374 | return -2; | |
| 1375 | } | |
| 1376 | ||
| 1377 | /* If key type not RSA return error */ | |
| 13f91a72 RL |
1378 | if (!EVP_PKEY_CTX_is_a(ctx, "RSA") |
| 1379 | && !EVP_PKEY_CTX_is_a(ctx, "RSA-PSS")) | |
| 2972af10 RL |
1380 | return -1; |
| 1381 | ||
| 2972af10 RL |
1382 | *p++ = OSSL_PARAM_construct_size_t(OSSL_PKEY_PARAM_RSA_PRIMES, &primes2); |
| 1383 | *p++ = OSSL_PARAM_construct_end(); | |
| 1384 | ||
| 13f91a72 | 1385 | return evp_pkey_ctx_set_params_strict(ctx, params); |
| 2972af10 | 1386 | } |
| 902568bb | 1387 | |
| afb638f1 | 1388 | #endif |