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