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