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