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