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