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