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