27 #ifndef WOLF_CRYPT_RSA_H 28 #define WOLF_CRYPT_RSA_H 36 #ifndef WC_RSA_EXPONENT 37 #define WC_RSA_EXPONENT 65537L 40 #if defined(WC_RSA_NONBLOCK) 44 #error RSA non-blocking mode only supported using fast math 46 #ifndef TFM_TIMING_RESISTANT 47 #error RSA non-blocking mode only supported with timing resistance enabled 51 #undef NO_RSA_BOUNDS_CHECK 52 #define NO_RSA_BOUNDS_CHECK 56 #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA)) 60 #if defined(HAVE_FIPS) && \ 61 (!defined(HAVE_FIPS_VERSION) || (HAVE_FIPS_VERSION < 2)) 63 #include <cyassl/ctaocrypt/rsa.h> 64 #if defined(CYASSL_KEY_GEN) && !defined(WOLFSSL_KEY_GEN) 65 #define WOLFSSL_KEY_GEN 68 #include <wolfssl/wolfcrypt/integer.h> 71 #if defined(HAVE_FIPS) && \ 72 defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2) 73 #include <wolfssl/wolfcrypt/fips.h> 79 #ifdef WOLFSSL_XILINX_CRYPT 80 #include "xsecure_rsa.h" 83 #if defined(WOLFSSL_CRYPTOCELL) 84 #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h> 97 #if !defined(HAVE_FIPS) || \ 98 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 100 #ifdef WOLFSSL_ASYNC_CRYPT 101 #include <wolfssl/wolfcrypt/async.h> 102 #ifdef WOLFSSL_CERT_GEN 111 RSA_TYPE_UNKNOWN = -1,
112 RSA_PUBLIC_ENCRYPT = 0,
113 RSA_PUBLIC_DECRYPT = 1,
114 RSA_PRIVATE_ENCRYPT = 2,
115 RSA_PRIVATE_DECRYPT = 3,
117 RSA_BLOCK_TYPE_1 = 1,
118 RSA_BLOCK_TYPE_2 = 2,
123 RSA_PSS_SALT_MAX_SZ = 62,
126 RSA_PKCS1_PADDING_SIZE = 11,
127 RSA_PKCS1_OAEP_PADDING_SIZE = 42,
130 RSA_PSS_PAD_TERM = 0xBC,
133 RSA_PSS_SALT_LEN_DEFAULT = -1,
134 #ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER 135 RSA_PSS_SALT_LEN_DISCOVER = -2,
143 #ifdef WC_RSA_NONBLOCK 153 #ifndef WOLFSSL_RSA_PUBLIC_ONLY 155 #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM) 164 #ifdef WC_RSA_BLINDING 167 #ifdef WOLF_CRYPTO_CB 170 #ifdef WOLFSSL_ASYNC_CRYPT 171 WC_ASYNC_DEV asyncDev;
172 #ifdef WOLFSSL_CERT_GEN 176 #ifdef WOLFSSL_XILINX_CRYPT 182 byte
id[RSA_MAX_ID_LEN];
185 #if defined(WOLFSSL_ASYNC_CRYPT) || !defined(WOLFSSL_RSA_VERIFY_INLINE) 188 #ifdef WC_RSA_NONBLOCK 191 #ifdef WOLFSSL_AFALG_XILINX_RSA 195 #if defined(WOLFSSL_CRYPTOCELL) 200 #ifndef WC_RSAKEY_TYPE_DEFINED 202 #define WC_RSAKEY_TYPE_DEFINED 208 WOLFSSL_API
int wc_InitRsaKey_ex(
RsaKey* key,
void* heap,
int devId);
211 WOLFSSL_API
int wc_InitRsaKey_Id(
RsaKey* key,
unsigned char*
id,
int len,
212 void* heap,
int devId);
214 WOLFSSL_API
int wc_CheckRsaKey(
RsaKey* key);
215 #ifdef WOLFSSL_XILINX_CRYPT 216 WOLFSSL_LOCAL
int wc_InitRsaHw(
RsaKey* key);
219 WOLFSSL_API
int wc_RsaFunction(
const byte* in, word32 inLen, byte* out,
227 word32 outLen,
RsaKey* key);
228 WOLFSSL_API
int wc_RsaSSL_Sign(
const byte* in, word32 inLen, byte* out,
230 WOLFSSL_API
int wc_RsaPSS_Sign(
const byte* in, word32 inLen, byte* out,
231 word32 outLen,
enum wc_HashType hash,
int mgf,
233 WOLFSSL_API
int wc_RsaPSS_Sign_ex(
const byte* in, word32 inLen, byte* out,
234 word32 outLen,
enum wc_HashType hash,
235 int mgf,
int saltLen,
RsaKey* key,
240 word32 outLen,
RsaKey* key);
241 WOLFSSL_API
int wc_RsaSSL_Verify_ex(
const byte* in, word32 inLen, byte* out,
242 word32 outLen,
RsaKey* key,
int pad_type);
243 WOLFSSL_API
int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out,
244 enum wc_HashType hash,
int mgf,
246 WOLFSSL_API
int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out,
247 enum wc_HashType hash,
int mgf,
248 int saltLen,
RsaKey* key);
249 WOLFSSL_API
int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out,
250 word32 outLen,
enum wc_HashType hash,
int mgf,
252 WOLFSSL_API
int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out,
253 word32 outLen,
enum wc_HashType hash,
254 int mgf,
int saltLen,
RsaKey* key);
255 WOLFSSL_API
int wc_RsaPSS_CheckPadding(
const byte* in, word32 inLen, byte* sig,
257 enum wc_HashType hashType);
258 WOLFSSL_API
int wc_RsaPSS_CheckPadding_ex(
const byte* in, word32 inLen,
259 byte* sig, word32 sigSz,
260 enum wc_HashType hashType,
261 int saltLen,
int bits);
262 WOLFSSL_API
int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
263 const byte* digest, word32 digentLen,
264 enum wc_HashType hash,
int mgf,
266 WOLFSSL_API
int wc_RsaPSS_VerifyCheck(byte* in, word32 inLen,
267 byte* out, word32 outLen,
268 const byte* digest, word32 digestLen,
269 enum wc_HashType hash,
int mgf,
274 #if !defined(HAVE_FIPS) || \ 275 (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)) 282 const byte* e, word32 eSz,
RsaKey* key);
286 #ifdef WC_RSA_BLINDING 289 #ifdef WC_RSA_NONBLOCK 291 #ifdef WC_RSA_NONBLOCK_TIME 302 #define WC_MGF1NONE 0 303 #define WC_MGF1SHA1 26 304 #define WC_MGF1SHA224 4 305 #define WC_MGF1SHA256 1 306 #define WC_MGF1SHA384 2 307 #define WC_MGF1SHA512 3 310 #define WC_RSA_PKCSV15_PAD 0 311 #define WC_RSA_OAEP_PAD 1 312 #define WC_RSA_PSS_PAD 2 313 #define WC_RSA_NO_PAD 3 317 enum wc_HashType hash,
int mgf, byte* label, word32 lableSz);
319 byte* out, word32 outLen,
RsaKey* key,
int type,
320 enum wc_HashType hash,
int mgf, byte* label, word32 lableSz);
322 byte** out,
RsaKey* key,
int type,
enum wc_HashType hash,
323 int mgf, byte* label, word32 lableSz);
324 #if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING) 325 WOLFSSL_API
int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz,
333 WOLFSSL_API
int wc_RsaExportKey(
RsaKey* key,
334 byte* e, word32* eSz,
335 byte* n, word32* nSz,
336 byte* d, word32* dSz,
337 byte* p, word32* pSz,
338 byte* q, word32* qSz);
342 #ifdef WOLFSSL_KEY_GEN 344 WOLFSSL_API
int wc_CheckProbablePrime_ex(
const byte* p, word32 pSz,
345 const byte* q, word32 qSz,
346 const byte* e, word32 eSz,
347 int nlen,
int* isPrime,
WC_RNG* rng);
348 WOLFSSL_API
int wc_CheckProbablePrime(
const byte* p, word32 pSz,
349 const byte* q, word32 qSz,
350 const byte* e, word32 eSz,
351 int nlen,
int* isPrime);
354 WOLFSSL_LOCAL
int wc_RsaPad_ex(
const byte* input, word32 inputLen, byte* pkcsBlock,
355 word32 pkcsBlockLen, byte padValue,
WC_RNG* rng,
int padType,
356 enum wc_HashType hType,
int mgf, byte* optLabel, word32 labelLen,
357 int saltLen,
int bits,
void* heap);
358 WOLFSSL_LOCAL
int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
359 byte padValue,
int padType,
enum wc_HashType hType,
360 int mgf, byte* optLabel, word32 labelLen,
int saltLen,
361 int bits,
void* heap);
WOLFSSL_API int wc_RsaEncryptSize(RsaKey *key)
Returns the encryption size for the provided key structure.
Definition: rsa.c:3540
WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte *n, word32 nSz, const byte *e, word32 eSz, RsaKey *key)
This function decodes the raw elements of an RSA public key, taking in the public modulus (n) and exp...
Definition: asn.c:4340
WOLFSSL_API int wc_FreeRsaKey(RsaKey *key)
This function frees a provided RsaKey struct using mp_clear.
Definition: rsa.c:512
WOLFSSL_API int wc_RsaSetNonBlock(RsaKey *key, RsaNb *nb)
This function sets the non-blocking RSA context. When a RsaNb context is set it enables fast math bas...
Definition: rsa.c:4172
WOLFSSL_API int wc_RsaPublicEncrypt(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key, WC_RNG *rng)
This function encrypts a message from in and stores the result in out. It requires an initialized pub...
Definition: rsa.c:3027
Definition: cryptoCell.h:77
WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key, int type, enum wc_HashType hash, int mgf, byte *label, word32 lableSz)
This function uses RSA to decrypt a message and gives the option of what padding type.
Definition: rsa.c:3097
Definition: integer.h:200
WOLFSSL_API int wc_RsaSSL_VerifyInline(byte *in, word32 inLen, byte **out, RsaKey *key)
Used to verify that the message was signed by RSA key. The output uses the same byte array as the inp...
Definition: rsa.c:3116
WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte *input, word32 *inOutIdx, RsaKey *, word32)
This function parses a DER-formatted RSA private key, extracts the private key and stores it in the g...
Definition: asn.c:2567
WOLFSSL_API int wc_RsaPublicKeyDecode(const byte *input, word32 *inOutIdx, RsaKey *, word32)
This function parses a DER-formatted RSA public key, extracts the public key and stores it in the giv...
Definition: asn.c:4321
WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key, WC_RNG *rng, int type, enum wc_HashType hash, int mgf, byte *label, word32 lableSz)
This function performs RSA encrypt while allowing the choice of which padding to use.
Definition: rsa.c:3037
WOLFSSL_API int wc_RsaKeyToDer(RsaKey *, byte *output, word32 inLen)
This function converts an RsaKey key to DER format. The result is written to output and it returns th...
Definition: asn.c:11179
WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey *, byte *, word32 *, byte *, word32 *)
Flattens the RsaKey structure into individual elements (e, n) used for the RSA algorithm.
Definition: rsa.c:3562
WOLFSSL_API int wc_RsaPrivateDecrypt(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key)
This functions provides private RSA decryption.
Definition: rsa.c:3082
WOLFSSL_API int wc_RsaSSL_Sign(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key, WC_RNG *rng)
Signs the provided array with the private key.
Definition: rsa.c:3481
WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte *in, word32 inLen, byte **out, RsaKey *key, int type, enum wc_HashType hash, int mgf, byte *label, word32 lableSz)
This function uses RSA to decrypt a message inline and gives the option of what padding type...
Definition: rsa.c:3065
WOLFSSL_API int wc_RsaKeyToPublicDer(RsaKey *, byte *output, word32 inLen)
Convert Rsa Public key to DER format. Writes to output, and returns count of bytes written...
Definition: asn.c:11254
WOLFSSL_API int wc_RsaSSL_Verify(const byte *in, word32 inLen, byte *out, word32 outLen, RsaKey *key)
Used to verify that the message was signed by key.
Definition: rsa.c:3131
WOLFSSL_API int wc_InitRsaKey(RsaKey *key, void *heap)
This function initializes a provided RsaKey struct. It also takes in a heap identifier, for use with user defined memory overrides (see XMALLOC, XFREE, XREALLOC).
Definition: rsa.c:333
WOLFSSL_API int wc_MakeRsaKey(RsaKey *key, int size, long e, WC_RNG *rng)
This function generates a RSA private key of length size (in bits) and given exponent (e)...
Definition: rsa.c:3886
WOLFSSL_API int wc_RsaSetNonBlockTime(RsaKey *key, word32 maxBlockUs, word32 cpuMHz)
This function configures the maximum amount of blocking time in microseconds. It uses a pre-computed ...
Definition: rsa.c:4187
Definition: user_rsa.h:60
WOLFSSL_API int wc_RsaPrivateDecryptInline(byte *in, word32 inLen, byte **out, RsaKey *key)
This functions is utilized by the wc_RsaPrivateDecrypt function for decrypting.
Definition: rsa.c:3050