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