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