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