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