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