rsa.h (17841B)
1 /* rsa.h 2 * 3 * Copyright (C) 2006-2025 wolfSSL Inc. 4 * 5 * This file is part of wolfSSL. 6 * 7 * wolfSSL is free software; you can redistribute it and/or modify 8 * it under the terms of the GNU General Public License as published by 9 * the Free Software Foundation; either version 3 of the License, or 10 * (at your option) any later version. 11 * 12 * wolfSSL is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA 20 */ 21 22 /*! 23 \file wolfssl/wolfcrypt/rsa.h 24 */ 25 26 /* 27 28 DESCRIPTION 29 This library provides the interface to the RSA. 30 RSA keys can be used to encrypt, decrypt, sign and verify data. 31 32 */ 33 #ifndef WOLF_CRYPT_RSA_H 34 #define WOLF_CRYPT_RSA_H 35 36 #include <wolfssl/wolfcrypt/types.h> 37 38 #ifndef NO_RSA 39 40 41 /* RSA default exponent */ 42 #ifndef WC_RSA_EXPONENT 43 #define WC_RSA_EXPONENT 65537L 44 #endif 45 46 #if defined(WC_RSA_NONBLOCK) 47 /* enable support for fast math based non-blocking exptmod */ 48 /* this splits the RSA function into many smaller operations */ 49 #ifndef USE_FAST_MATH 50 #error RSA non-blocking mode only supported using fast math 51 #endif 52 #ifndef TFM_TIMING_RESISTANT 53 #error RSA non-blocking mode only supported with timing resistance enabled 54 #endif 55 56 /* RSA bounds check is not supported with RSA non-blocking mode */ 57 #undef NO_RSA_BOUNDS_CHECK 58 #define NO_RSA_BOUNDS_CHECK 59 #endif 60 61 #include <wolfssl/wolfcrypt/wolfmath.h> 62 #include <wolfssl/wolfcrypt/random.h> 63 64 #if defined(HAVE_FIPS) && \ 65 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 66 #include <wolfssl/wolfcrypt/fips.h> 67 #endif 68 69 /* header file needed for OAEP padding */ 70 #include <wolfssl/wolfcrypt/hash.h> 71 72 #ifdef WOLFSSL_XILINX_CRYPT 73 #ifdef WOLFSSL_XILINX_CRYPT_VERSAL 74 #include <wolfssl/wolfcrypt/port/xilinx/xil-versal-glue.h> 75 #else 76 #include <xsecure_rsa.h> 77 #endif 78 #endif 79 80 #if defined(WOLFSSL_CRYPTOCELL) 81 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h> 82 #endif 83 84 #if defined(WOLFSSL_KCAPI_RSA) 85 #include <wolfssl/wolfcrypt/port/kcapi/kcapi_rsa.h> 86 #endif 87 88 #if defined(WOLFSSL_DEVCRYPTO_RSA) 89 #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h> 90 #endif 91 92 #if defined(WOLFSSL_RENESAS_FSPSM) 93 #include <wolfssl/wolfcrypt/port/renesas/renesas-fspsm-crypt.h> 94 #endif 95 96 #ifdef __cplusplus 97 extern "C" { 98 #endif 99 100 #if FIPS_VERSION3_GE(6,0,0) 101 extern const unsigned int wolfCrypt_FIPS_rsa_ro_sanity[2]; 102 WOLFSSL_LOCAL int wolfCrypt_FIPS_RSA_sanity(void); 103 #endif 104 105 #ifndef RSA_MIN_SIZE 106 #if defined(HAVE_WOLFENGINE) || defined(HAVE_WOLFPROVIDER) 107 #define RSA_MIN_SIZE 1024 108 #else 109 #define RSA_MIN_SIZE 2048 110 #endif 111 #endif 112 113 #ifndef RSA_MAX_SIZE 114 #ifdef USE_FAST_MATH 115 /* FP implementation support numbers up to FP_MAX_BITS / 2 bits. */ 116 #define RSA_MAX_SIZE (FP_MAX_BITS / 2) 117 #if defined(WOLFSSL_MYSQL_COMPATIBLE) && RSA_MAX_SIZE < 8192 118 #error "MySQL needs FP_MAX_BITS at least at 16384" 119 #endif 120 #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH) 121 /* SP implementation supports numbers of SP_INT_BITS bits. */ 122 #define RSA_MAX_SIZE (((SP_INT_BITS + 7) / 8) * 8) 123 #if defined(WOLFSSL_MYSQL_COMPATIBLE) && RSA_MAX_SIZE < 8192 124 #error "MySQL needs SP_INT_BITS at least at 8192" 125 #endif 126 #else 127 #ifdef WOLFSSL_MYSQL_COMPATIBLE 128 /* Integer maths is dynamic but we only go up to 8192 bits. */ 129 #define RSA_MAX_SIZE 8192 130 #else 131 /* Integer maths is dynamic but we only go up to 4096 bits. */ 132 #define RSA_MAX_SIZE 4096 133 #endif 134 #endif 135 #endif 136 137 /* avoid redefinition of structs */ 138 #if !defined(HAVE_FIPS) || \ 139 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 140 141 #ifdef WOLFSSL_ASYNC_CRYPT 142 #include <wolfssl/wolfcrypt/async.h> 143 #ifdef WOLFSSL_CERT_GEN 144 #include <wolfssl/wolfcrypt/asn.h> 145 #endif 146 #endif 147 148 #if FIPS_VERSION3_GE(6,0,0) 149 #define WC_RSA_FIPS_GEN_MIN 2048 150 #define WC_RSA_FIPS_SIG_MIN (WC_RSA_FIPS_GEN_MIN/8) 151 #endif 152 153 enum { 154 RSA_PUBLIC = 0, 155 RSA_PRIVATE = 1, 156 157 RSA_TYPE_UNKNOWN = -1, 158 RSA_PUBLIC_ENCRYPT = 0, 159 RSA_PUBLIC_DECRYPT = 1, 160 RSA_PRIVATE_ENCRYPT = 2, 161 RSA_PRIVATE_DECRYPT = 3, 162 163 RSA_BLOCK_TYPE_1 = 1, 164 RSA_BLOCK_TYPE_2 = 2, 165 166 RSA_MIN_PAD_SZ = 11, /* separator + 0 + pad value + 8 pads */ 167 168 RSA_PSS_PAD_SZ = 8, 169 RSA_PSS_SALT_MAX_SZ = 62, 170 171 #ifdef OPENSSL_EXTRA 172 WC_RSA_PKCS1_PADDING_SIZE = 11, 173 WC_RSA_PKCS1_OAEP_PADDING_SIZE = 42, /* (2 * hashlen(SHA-1)) + 2 */ 174 #ifndef OPENSSL_COEXIST 175 #define RSA_PKCS1_PADDING_SIZE WC_RSA_PKCS1_PADDING_SIZE 176 #define RSA_PKCS1_OAEP_PADDING_SIZE WC_RSA_PKCS1_OAEP_PADDING_SIZE 177 #endif 178 #endif 179 #ifdef WC_RSA_PSS 180 RSA_PSS_PAD_TERM = 0xBC, 181 #endif 182 183 RSA_PSS_SALT_LEN_DEFAULT = -1, 184 #ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER 185 RSA_PSS_SALT_LEN_DISCOVER = -2, 186 #endif 187 188 #ifdef WOLF_PRIVATE_KEY_ID 189 RSA_MAX_ID_LEN = 32, 190 RSA_MAX_LABEL_LEN = 32, 191 #endif 192 193 WOLF_ENUM_DUMMY_LAST_ELEMENT(RSA) 194 }; 195 196 #ifdef WC_RSA_NONBLOCK 197 typedef struct RsaNb { 198 exptModNb_t exptmod; /* non-block expt_mod */ 199 mp_int tmp; 200 } RsaNb; 201 #endif 202 203 /* RSA */ 204 struct RsaKey { 205 mp_int n, e; 206 #ifndef WOLFSSL_RSA_PUBLIC_ONLY 207 mp_int d, p, q; 208 #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) 209 mp_int dP, dQ, u; 210 #endif 211 #endif 212 void* heap; /* for user memory overrides */ 213 byte* data; /* temp buffer for async RSA */ 214 int type; /* public or private */ 215 int state; 216 word32 dataLen; 217 #ifdef WC_RSA_BLINDING 218 WC_RNG* rng; /* for PrivateDecrypt blinding */ 219 #endif 220 #ifdef WOLFSSL_SE050 221 word32 keyId; 222 byte keyIdSet; 223 #endif 224 #ifdef WOLF_CRYPTO_CB 225 void* devCtx; 226 int devId; 227 #endif 228 #ifdef WOLFSSL_ASYNC_CRYPT 229 WC_ASYNC_DEV asyncDev; 230 #ifdef WOLFSSL_CERT_GEN 231 CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */ 232 #endif 233 #endif /* WOLFSSL_ASYNC_CRYPT */ 234 #ifdef WOLFSSL_XILINX_CRYPT 235 word32 pubExp; /* to keep values in scope they are here in struct */ 236 byte* mod; 237 #if defined(WOLFSSL_XILINX_CRYPT_VERSAL) 238 int mSz; 239 wc_Xsecure xSec; 240 #else 241 XSecure_Rsa xRsa; 242 #endif 243 #endif 244 #if defined(WOLFSSL_KCAPI_RSA) 245 struct kcapi_handle* handle; 246 #endif 247 #ifdef WOLF_PRIVATE_KEY_ID 248 byte id[RSA_MAX_ID_LEN]; 249 int idLen; 250 char label[RSA_MAX_LABEL_LEN]; 251 int labelLen; 252 #endif 253 #if !defined(WOLFSSL_NO_MALLOC) && (defined(WOLFSSL_ASYNC_CRYPT) || \ 254 (!defined(WOLFSSL_RSA_VERIFY_ONLY) && !defined(WOLFSSL_RSA_VERIFY_INLINE))) 255 byte dataIsAlloc; 256 #endif 257 #ifdef WC_RSA_NONBLOCK 258 RsaNb* nb; 259 #endif 260 #ifdef WOLFSSL_AFALG_XILINX_RSA 261 int alFd; 262 int rdFd; 263 #endif 264 #if defined(WOLFSSL_CRYPTOCELL) 265 rsa_context_t ctx; 266 #endif 267 #if defined(WOLFSSL_CAAM) 268 word32 blackKey; 269 #endif 270 #if defined(WOLFSSL_DEVCRYPTO_RSA) 271 WC_CRYPTODEV ctx; 272 #endif 273 #if defined(WOLFSSL_RENESAS_FSPSM) 274 FSPSM_RSA_CTX ctx; 275 #endif 276 }; 277 278 #ifndef WC_RSAKEY_TYPE_DEFINED 279 typedef struct RsaKey RsaKey; 280 #define WC_RSAKEY_TYPE_DEFINED 281 #endif 282 283 #endif /* HAVE_FIPS */ 284 285 #if defined(WOLF_CRYPTO_CB) && defined(WOLF_CRYPTO_CB_RSA_PAD) 286 struct RsaPadding { 287 byte pad_value; 288 int pad_type; 289 enum wc_HashType hash; 290 int mgf; 291 byte* label; 292 word32 labelSz; 293 int saltLen; 294 int unpadded; 295 }; 296 typedef struct RsaPadding RsaPadding; 297 #endif 298 299 WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap); 300 WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId); 301 WOLFSSL_API int wc_FreeRsaKey(RsaKey* key); 302 #ifndef WC_NO_CONSTRUCTORS 303 WOLFSSL_API RsaKey* wc_NewRsaKey(void* heap, int devId, int *result_code); 304 WOLFSSL_API int wc_DeleteRsaKey(RsaKey* key, RsaKey** key_p); 305 #endif 306 307 #ifdef WOLF_PRIVATE_KEY_ID 308 WOLFSSL_API int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len, 309 void* heap, int devId); 310 WOLFSSL_API int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap, 311 int devId); 312 #endif 313 WOLFSSL_API int wc_CheckRsaKey(RsaKey* key); 314 #ifdef WOLFSSL_XILINX_CRYPT 315 WOLFSSL_LOCAL int wc_InitRsaHw(RsaKey* key); 316 #endif /* WOLFSSL_XILINX_CRYPT */ 317 #ifdef WOLFSSL_SE050 318 WOLFSSL_API int wc_RsaUseKeyId(RsaKey* key, word32 keyId, word32 flags); 319 WOLFSSL_API int wc_RsaGetKeyId(RsaKey* key, word32* keyId); 320 #endif /* WOLFSSL_SE050 */ 321 322 WOLFSSL_API int wc_RsaFunction(const byte* in, word32 inLen, byte* out, 323 word32* outLen, int type, RsaKey* key, WC_RNG* rng); 324 325 WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out, 326 word32 outLen, RsaKey* key, WC_RNG* rng); 327 WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out, 328 RsaKey* key); 329 WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out, 330 word32 outLen, RsaKey* key); 331 WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out, 332 word32 outLen, RsaKey* key, WC_RNG* rng); 333 WOLFSSL_API int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out, 334 word32 outLen, enum wc_HashType hash, int mgf, 335 RsaKey* key, WC_RNG* rng); 336 WOLFSSL_API int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out, 337 word32 outLen, enum wc_HashType hash, 338 int mgf, int saltLen, RsaKey* key, 339 WC_RNG* rng); 340 WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out, 341 RsaKey* key); 342 WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out, 343 word32 outLen, RsaKey* key); 344 WOLFSSL_API int wc_RsaSSL_Verify_ex(const byte* in, word32 inLen, byte* out, 345 word32 outLen, RsaKey* key, int pad_type); 346 WOLFSSL_API int wc_RsaSSL_Verify_ex2(const byte* in, word32 inLen, byte* out, 347 word32 outLen, RsaKey* key, int pad_type, 348 enum wc_HashType hash); 349 WOLFSSL_API int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out, 350 enum wc_HashType hash, int mgf, 351 RsaKey* key); 352 WOLFSSL_API int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out, 353 enum wc_HashType hash, int mgf, 354 int saltLen, RsaKey* key); 355 WOLFSSL_API int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out, 356 word32 outLen, enum wc_HashType hash, int mgf, 357 RsaKey* key); 358 WOLFSSL_API int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out, 359 word32 outLen, enum wc_HashType hash, 360 int mgf, int saltLen, RsaKey* key); 361 WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen, byte* sig, 362 word32 sigSz, 363 enum wc_HashType hashType); 364 WOLFSSL_API int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inLen, 365 byte* sig, word32 sigSz, 366 enum wc_HashType hashType, 367 int saltLen, int bits); 368 WOLFSSL_API int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inLen, 369 byte* sig, word32 sigSz, 370 enum wc_HashType hashType, 371 int saltLen, int bits, void* heap); 372 WOLFSSL_API int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out, 373 const byte* digest, word32 digentLen, 374 enum wc_HashType hash, int mgf, 375 RsaKey* key); 376 WOLFSSL_API int wc_RsaPSS_VerifyCheck(byte* in, word32 inLen, 377 byte* out, word32 outLen, 378 const byte* digest, word32 digestLen, 379 enum wc_HashType hash, int mgf, 380 RsaKey* key); 381 382 WOLFSSL_API int wc_RsaEncryptSize(const RsaKey* key); 383 384 #if !defined(HAVE_FIPS) || \ 385 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 386 /* to avoid asn duplicate symbols @wc_fips */ 387 WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx, 388 RsaKey* key, word32 inSz); 389 #if !defined(HAVE_FIPS) || \ 390 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2)) 391 WOLFSSL_API int wc_RsaPrivateKeyValidate(const byte* input, word32* inOutIdx, 392 int* keySz, word32 inSz); 393 #endif 394 WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx, 395 RsaKey* key, word32 inSz); 396 WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz, 397 const byte* e, word32 eSz, RsaKey* key); 398 #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \ 399 defined(WOLFSSL_KCAPI_RSA) || defined(WOLFSSL_SE050) 400 WOLFSSL_API int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen); 401 #endif 402 403 #ifdef WC_RSA_BLINDING 404 WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng); 405 #endif 406 #ifdef WC_RSA_NONBLOCK 407 WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb); 408 #ifdef WC_RSA_NONBLOCK_TIME 409 WOLFSSL_API int wc_RsaSetNonBlockTime(RsaKey* key, word32 maxBlockUs, 410 word32 cpuMHz); 411 #endif 412 #endif 413 414 /* 415 choice of padding added after fips, so not available when using fips RSA 416 */ 417 418 /* Mask Generation Function Identifiers */ 419 #define WC_MGF1NONE 0 420 #define WC_MGF1SHA1 26 421 #define WC_MGF1SHA224 4 422 #define WC_MGF1SHA256 1 423 #define WC_MGF1SHA384 2 424 #define WC_MGF1SHA512 3 425 #define WC_MGF1SHA512_224 5 426 #define WC_MGF1SHA512_256 6 427 428 /* Padding types */ 429 #define WC_RSA_PKCSV15_PAD 0 430 #define WC_RSA_OAEP_PAD 1 431 #define WC_RSA_PSS_PAD 2 432 #define WC_RSA_NO_PAD 3 433 434 WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out, 435 word32 outLen, RsaKey* key, WC_RNG* rng, int type, 436 enum wc_HashType hash, int mgf, byte* label, word32 labelSz); 437 WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen, 438 byte* out, word32 outLen, RsaKey* key, int type, 439 enum wc_HashType hash, int mgf, byte* label, word32 labelSz); 440 WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen, 441 byte** out, RsaKey* key, int type, enum wc_HashType hash, 442 int mgf, byte* label, word32 labelSz); 443 #if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING) || defined(OPENSSL_EXTRA) || defined(OPENSSL_EXTRA_X509_SMALL) 444 WOLFSSL_API int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz, 445 RsaKey* key, int type, WC_RNG* rng); 446 #endif 447 448 #endif /* HAVE_FIPS */ 449 450 WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz, 451 byte* n, word32* nSz); 452 WOLFSSL_API int wc_RsaExportKey(RsaKey* key, 453 byte* e, word32* eSz, 454 byte* n, word32* nSz, 455 byte* d, word32* dSz, 456 byte* p, word32* pSz, 457 byte* q, word32* qSz); 458 459 #ifdef WOLFSSL_KEY_GEN 460 WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng); 461 WOLFSSL_API int wc_CheckProbablePrime_ex(const byte* p, word32 pSz, 462 const byte* q, word32 qSz, 463 const byte* e, word32 eSz, 464 int nlen, int* isPrime, WC_RNG* rng); 465 WOLFSSL_API int wc_CheckProbablePrime(const byte* p, word32 pSz, 466 const byte* q, word32 qSz, 467 const byte* e, word32 eSz, 468 int nlen, int* isPrime); 469 #endif 470 471 WOLFSSL_API int wc_RsaPad_ex(const byte* input, word32 inputLen, 472 byte* pkcsBlock, word32 pkcsBlockLen, byte padValue, 473 WC_RNG* rng, int padType, enum wc_HashType hType, int mgf, 474 byte* optLabel, word32 labelLen, int saltLen, int bits, void* heap); 475 WOLFSSL_API int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, 476 byte** out, byte padValue, int padType, enum wc_HashType hType, int mgf, 477 byte* optLabel, word32 labelLen, int saltLen, int bits, void* heap); 478 479 WOLFSSL_LOCAL int wc_hash2mgf(enum wc_HashType hType); 480 WOLFSSL_LOCAL int RsaFunctionCheckIn(const byte* in, word32 inLen, RsaKey* key, 481 int checkSmallCt); 482 483 WOLFSSL_API int wc_RsaPrivateKeyDecodeRaw(const byte* n, word32 nSz, 484 const byte* e, word32 eSz, const byte* d, word32 dSz, 485 const byte* u, word32 uSz, const byte* p, word32 pSz, 486 const byte* q, word32 qSz, const byte* dP, word32 dPSz, 487 const byte* dQ, word32 dQSz, RsaKey* key); 488 489 #ifdef __cplusplus 490 } /* extern "C" */ 491 #endif 492 493 #endif /* NO_RSA */ 494 #endif /* WOLF_CRYPT_RSA_H */ 495