rsa.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /* rsa.h
  2. *
  3. * Copyright (C) 2006-2023 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 2 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. \file wolfssl/wolfcrypt/rsa.h
  23. */
  24. /*
  25. DESCRIPTION
  26. This library provides the interface to the RSA.
  27. RSA keys can be used to encrypt, decrypt, sign and verify data.
  28. */
  29. #ifndef WOLF_CRYPT_RSA_H
  30. #define WOLF_CRYPT_RSA_H
  31. #include <wolfssl/wolfcrypt/types.h>
  32. #ifndef NO_RSA
  33. /* RSA default exponent */
  34. #ifndef WC_RSA_EXPONENT
  35. #define WC_RSA_EXPONENT 65537L
  36. #endif
  37. #if defined(WC_RSA_NONBLOCK)
  38. /* enable support for fast math based non-blocking exptmod */
  39. /* this splits the RSA function into many smaller operations */
  40. #ifndef USE_FAST_MATH
  41. #error RSA non-blocking mode only supported using fast math
  42. #endif
  43. #ifndef TFM_TIMING_RESISTANT
  44. #error RSA non-blocking mode only supported with timing resistance enabled
  45. #endif
  46. /* RSA bounds check is not supported with RSA non-blocking mode */
  47. #undef NO_RSA_BOUNDS_CHECK
  48. #define NO_RSA_BOUNDS_CHECK
  49. #endif
  50. /* allow for user to plug in own crypto */
  51. #if !defined(HAVE_FIPS) && (defined(HAVE_USER_RSA) || defined(HAVE_FAST_RSA))
  52. #include "user_rsa.h"
  53. #else
  54. #include <wolfssl/wolfcrypt/wolfmath.h>
  55. #include <wolfssl/wolfcrypt/random.h>
  56. #if defined(HAVE_FIPS) && \
  57. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  58. #include <wolfssl/wolfcrypt/fips.h>
  59. #endif
  60. /* header file needed for OAEP padding */
  61. #include <wolfssl/wolfcrypt/hash.h>
  62. #ifdef WOLFSSL_XILINX_CRYPT
  63. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  64. #include <wolfssl/wolfcrypt/port/xilinx/xil-versal-glue.h>
  65. #else
  66. #include <xsecure_rsa.h>
  67. #endif
  68. #endif
  69. #if defined(WOLFSSL_CRYPTOCELL)
  70. #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
  71. #endif
  72. #if defined(WOLFSSL_KCAPI_RSA)
  73. #include <wolfssl/wolfcrypt/port/kcapi/kcapi_rsa.h>
  74. #endif
  75. #if defined(WOLFSSL_DEVCRYPTO_RSA)
  76. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  77. #endif
  78. #if defined(WOLFSSL_RENESAS_FSPSM)
  79. #include <wolfssl/wolfcrypt/port/renesas/renesas-fspsm-crypt.h>
  80. #endif
  81. #ifdef __cplusplus
  82. extern "C" {
  83. #endif
  84. #ifndef RSA_MIN_SIZE
  85. #define RSA_MIN_SIZE 512
  86. #endif
  87. #ifndef RSA_MAX_SIZE
  88. #ifdef USE_FAST_MATH
  89. /* FP implementation support numbers up to FP_MAX_BITS / 2 bits. */
  90. #define RSA_MAX_SIZE (FP_MAX_BITS / 2)
  91. #if defined(WOLFSSL_MYSQL_COMPATIBLE) && RSA_MAX_SIZE < 8192
  92. #error "MySQL needs FP_MAX_BITS at least at 16384"
  93. #endif
  94. #elif defined(WOLFSSL_SP_MATH_ALL) || defined(WOLFSSL_SP_MATH)
  95. /* SP implementation supports numbers of SP_INT_BITS bits. */
  96. #define RSA_MAX_SIZE (((SP_INT_BITS + 7) / 8) * 8)
  97. #if defined(WOLFSSL_MYSQL_COMPATIBLE) && RSA_MAX_SIZE < 8192
  98. #error "MySQL needs SP_INT_BITS at least at 8192"
  99. #endif
  100. #else
  101. #ifdef WOLFSSL_MYSQL_COMPATIBLE
  102. /* Integer maths is dynamic but we only go up to 8192 bits. */
  103. #define RSA_MAX_SIZE 8192
  104. #else
  105. /* Integer maths is dynamic but we only go up to 4096 bits. */
  106. #define RSA_MAX_SIZE 4096
  107. #endif
  108. #endif
  109. #endif
  110. /* avoid redefinition of structs */
  111. #if !defined(HAVE_FIPS) || \
  112. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  113. #ifdef WOLFSSL_ASYNC_CRYPT
  114. #include <wolfssl/wolfcrypt/async.h>
  115. #ifdef WOLFSSL_CERT_GEN
  116. #include <wolfssl/wolfcrypt/asn.h>
  117. #endif
  118. #endif
  119. enum {
  120. RSA_PUBLIC = 0,
  121. RSA_PRIVATE = 1,
  122. RSA_TYPE_UNKNOWN = -1,
  123. RSA_PUBLIC_ENCRYPT = 0,
  124. RSA_PUBLIC_DECRYPT = 1,
  125. RSA_PRIVATE_ENCRYPT = 2,
  126. RSA_PRIVATE_DECRYPT = 3,
  127. RSA_BLOCK_TYPE_1 = 1,
  128. RSA_BLOCK_TYPE_2 = 2,
  129. RSA_MIN_PAD_SZ = 11, /* separator + 0 + pad value + 8 pads */
  130. RSA_PSS_PAD_SZ = 8,
  131. RSA_PSS_SALT_MAX_SZ = 62,
  132. #ifdef OPENSSL_EXTRA
  133. RSA_PKCS1_PADDING_SIZE = 11,
  134. RSA_PKCS1_OAEP_PADDING_SIZE = 42, /* (2 * hashlen(SHA-1)) + 2 */
  135. #endif
  136. #ifdef WC_RSA_PSS
  137. RSA_PSS_PAD_TERM = 0xBC,
  138. #endif
  139. RSA_PSS_SALT_LEN_DEFAULT = -1,
  140. #ifdef WOLFSSL_PSS_SALT_LEN_DISCOVER
  141. RSA_PSS_SALT_LEN_DISCOVER = -2,
  142. #endif
  143. #ifdef WOLF_PRIVATE_KEY_ID
  144. RSA_MAX_ID_LEN = 32,
  145. RSA_MAX_LABEL_LEN = 32,
  146. #endif
  147. WOLF_ENUM_DUMMY_LAST_ELEMENT(RSA)
  148. };
  149. #ifdef WC_RSA_NONBLOCK
  150. typedef struct RsaNb {
  151. exptModNb_t exptmod; /* non-block expt_mod */
  152. mp_int tmp;
  153. } RsaNb;
  154. #endif
  155. /* RSA */
  156. struct RsaKey {
  157. mp_int n, e;
  158. #ifndef WOLFSSL_RSA_PUBLIC_ONLY
  159. mp_int d, p, q;
  160. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || !defined(RSA_LOW_MEM)
  161. mp_int dP, dQ, u;
  162. #endif
  163. #endif
  164. void* heap; /* for user memory overrides */
  165. byte* data; /* temp buffer for async RSA */
  166. int type; /* public or private */
  167. int state;
  168. word32 dataLen;
  169. #ifdef WC_RSA_BLINDING
  170. WC_RNG* rng; /* for PrivateDecrypt blinding */
  171. #endif
  172. #ifdef WOLFSSL_SE050
  173. word32 keyId;
  174. byte keyIdSet;
  175. #endif
  176. #ifdef WOLF_CRYPTO_CB
  177. void* devCtx;
  178. int devId;
  179. #endif
  180. #if defined(HAVE_PKCS11)
  181. byte isPkcs11 : 1; /* indicate if PKCS11 is preferred */
  182. #endif
  183. #ifdef WOLFSSL_ASYNC_CRYPT
  184. WC_ASYNC_DEV asyncDev;
  185. #ifdef WOLFSSL_CERT_GEN
  186. CertSignCtx certSignCtx; /* context info for cert sign (MakeSignature) */
  187. #endif
  188. #endif /* WOLFSSL_ASYNC_CRYPT */
  189. #ifdef WOLFSSL_XILINX_CRYPT
  190. word32 pubExp; /* to keep values in scope they are here in struct */
  191. byte* mod;
  192. #if defined(WOLFSSL_XILINX_CRYPT_VERSAL)
  193. int mSz;
  194. wc_Xsecure xSec;
  195. #else
  196. XSecure_Rsa xRsa;
  197. #endif
  198. #endif
  199. #if defined(WOLFSSL_KCAPI_RSA)
  200. struct kcapi_handle* handle;
  201. #endif
  202. #ifdef WOLF_PRIVATE_KEY_ID
  203. byte id[RSA_MAX_ID_LEN];
  204. int idLen;
  205. char label[RSA_MAX_LABEL_LEN];
  206. int labelLen;
  207. #endif
  208. #if defined(WOLFSSL_ASYNC_CRYPT) || !defined(WOLFSSL_RSA_VERIFY_INLINE) && \
  209. !defined(WOLFSSL_NO_MALLOC)
  210. byte dataIsAlloc;
  211. #endif
  212. #ifdef WC_RSA_NONBLOCK
  213. RsaNb* nb;
  214. #endif
  215. #ifdef WOLFSSL_AFALG_XILINX_RSA
  216. int alFd;
  217. int rdFd;
  218. #endif
  219. #if defined(WOLFSSL_CRYPTOCELL)
  220. rsa_context_t ctx;
  221. #endif
  222. #if defined(WOLFSSL_CAAM)
  223. word32 blackKey;
  224. #endif
  225. #if defined(WOLFSSL_DEVCRYPTO_RSA)
  226. WC_CRYPTODEV ctx;
  227. #endif
  228. #if defined(WOLFSSL_RENESAS_FSPSM)
  229. FSPSM_RSA_CTX ctx;
  230. #endif
  231. };
  232. #ifndef WC_RSAKEY_TYPE_DEFINED
  233. typedef struct RsaKey RsaKey;
  234. #define WC_RSAKEY_TYPE_DEFINED
  235. #endif
  236. #endif /* HAVE_FIPS */
  237. WOLFSSL_API int wc_InitRsaKey(RsaKey* key, void* heap);
  238. WOLFSSL_API int wc_InitRsaKey_ex(RsaKey* key, void* heap, int devId);
  239. WOLFSSL_API int wc_FreeRsaKey(RsaKey* key);
  240. #ifdef WOLF_PRIVATE_KEY_ID
  241. WOLFSSL_API int wc_InitRsaKey_Id(RsaKey* key, unsigned char* id, int len,
  242. void* heap, int devId);
  243. WOLFSSL_API int wc_InitRsaKey_Label(RsaKey* key, const char* label, void* heap,
  244. int devId);
  245. #endif
  246. WOLFSSL_API int wc_CheckRsaKey(RsaKey* key);
  247. #ifdef WOLFSSL_XILINX_CRYPT
  248. WOLFSSL_LOCAL int wc_InitRsaHw(RsaKey* key);
  249. #endif /* WOLFSSL_XILINX_CRYPT */
  250. #ifdef WOLFSSL_SE050
  251. WOLFSSL_API int wc_RsaUseKeyId(RsaKey* key, word32 keyId, word32 flags);
  252. WOLFSSL_API int wc_RsaGetKeyId(RsaKey* key, word32* keyId);
  253. #endif /* WOLFSSL_SE050 */
  254. WOLFSSL_API int wc_RsaFunction(const byte* in, word32 inLen, byte* out,
  255. word32* outLen, int type, RsaKey* key, WC_RNG* rng);
  256. WOLFSSL_API int wc_RsaPublicEncrypt(const byte* in, word32 inLen, byte* out,
  257. word32 outLen, RsaKey* key, WC_RNG* rng);
  258. WOLFSSL_API int wc_RsaPrivateDecryptInline(byte* in, word32 inLen, byte** out,
  259. RsaKey* key);
  260. WOLFSSL_API int wc_RsaPrivateDecrypt(const byte* in, word32 inLen, byte* out,
  261. word32 outLen, RsaKey* key);
  262. WOLFSSL_API int wc_RsaSSL_Sign(const byte* in, word32 inLen, byte* out,
  263. word32 outLen, RsaKey* key, WC_RNG* rng);
  264. WOLFSSL_API int wc_RsaPSS_Sign(const byte* in, word32 inLen, byte* out,
  265. word32 outLen, enum wc_HashType hash, int mgf,
  266. RsaKey* key, WC_RNG* rng);
  267. WOLFSSL_API int wc_RsaPSS_Sign_ex(const byte* in, word32 inLen, byte* out,
  268. word32 outLen, enum wc_HashType hash,
  269. int mgf, int saltLen, RsaKey* key,
  270. WC_RNG* rng);
  271. WOLFSSL_API int wc_RsaSSL_VerifyInline(byte* in, word32 inLen, byte** out,
  272. RsaKey* key);
  273. WOLFSSL_API int wc_RsaSSL_Verify(const byte* in, word32 inLen, byte* out,
  274. word32 outLen, RsaKey* key);
  275. WOLFSSL_API int wc_RsaSSL_Verify_ex(const byte* in, word32 inLen, byte* out,
  276. word32 outLen, RsaKey* key, int pad_type);
  277. WOLFSSL_API int wc_RsaSSL_Verify_ex2(const byte* in, word32 inLen, byte* out,
  278. word32 outLen, RsaKey* key, int pad_type,
  279. enum wc_HashType hash);
  280. WOLFSSL_API int wc_RsaPSS_VerifyInline(byte* in, word32 inLen, byte** out,
  281. enum wc_HashType hash, int mgf,
  282. RsaKey* key);
  283. WOLFSSL_API int wc_RsaPSS_VerifyInline_ex(byte* in, word32 inLen, byte** out,
  284. enum wc_HashType hash, int mgf,
  285. int saltLen, RsaKey* key);
  286. WOLFSSL_API int wc_RsaPSS_Verify(byte* in, word32 inLen, byte* out,
  287. word32 outLen, enum wc_HashType hash, int mgf,
  288. RsaKey* key);
  289. WOLFSSL_API int wc_RsaPSS_Verify_ex(byte* in, word32 inLen, byte* out,
  290. word32 outLen, enum wc_HashType hash,
  291. int mgf, int saltLen, RsaKey* key);
  292. WOLFSSL_API int wc_RsaPSS_CheckPadding(const byte* in, word32 inLen, byte* sig,
  293. word32 sigSz,
  294. enum wc_HashType hashType);
  295. WOLFSSL_API int wc_RsaPSS_CheckPadding_ex(const byte* in, word32 inLen,
  296. byte* sig, word32 sigSz,
  297. enum wc_HashType hashType,
  298. int saltLen, int bits);
  299. WOLFSSL_API int wc_RsaPSS_CheckPadding_ex2(const byte* in, word32 inLen,
  300. byte* sig, word32 sigSz,
  301. enum wc_HashType hashType,
  302. int saltLen, int bits, void* heap);
  303. WOLFSSL_API int wc_RsaPSS_VerifyCheckInline(byte* in, word32 inLen, byte** out,
  304. const byte* digest, word32 digentLen,
  305. enum wc_HashType hash, int mgf,
  306. RsaKey* key);
  307. WOLFSSL_API int wc_RsaPSS_VerifyCheck(byte* in, word32 inLen,
  308. byte* out, word32 outLen,
  309. const byte* digest, word32 digestLen,
  310. enum wc_HashType hash, int mgf,
  311. RsaKey* key);
  312. WOLFSSL_API int wc_RsaEncryptSize(const RsaKey* key);
  313. #if !defined(HAVE_FIPS) || \
  314. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  315. /* to avoid asn duplicate symbols @wc_fips */
  316. WOLFSSL_API int wc_RsaPrivateKeyDecode(const byte* input, word32* inOutIdx,
  317. RsaKey* key, word32 inSz);
  318. #if !defined(HAVE_FIPS) || \
  319. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION > 2))
  320. WOLFSSL_API int wc_RsaPrivateKeyValidate(const byte* input, word32* inOutIdx,
  321. int* keySz, word32 inSz);
  322. #endif
  323. WOLFSSL_API int wc_RsaPublicKeyDecode(const byte* input, word32* inOutIdx,
  324. RsaKey* key, word32 inSz);
  325. WOLFSSL_API int wc_RsaPublicKeyDecodeRaw(const byte* n, word32 nSz,
  326. const byte* e, word32 eSz, RsaKey* key);
  327. #if defined(WOLFSSL_KEY_GEN) || defined(OPENSSL_EXTRA) || \
  328. defined(WOLFSSL_KCAPI_RSA) || defined(WOLFSSL_SE050)
  329. WOLFSSL_API int wc_RsaKeyToDer(RsaKey* key, byte* output, word32 inLen);
  330. #endif
  331. #ifdef WC_RSA_BLINDING
  332. WOLFSSL_API int wc_RsaSetRNG(RsaKey* key, WC_RNG* rng);
  333. #endif
  334. #ifdef WC_RSA_NONBLOCK
  335. WOLFSSL_API int wc_RsaSetNonBlock(RsaKey* key, RsaNb* nb);
  336. #ifdef WC_RSA_NONBLOCK_TIME
  337. WOLFSSL_API int wc_RsaSetNonBlockTime(RsaKey* key, word32 maxBlockUs,
  338. word32 cpuMHz);
  339. #endif
  340. #endif
  341. /*
  342. choice of padding added after fips, so not available when using fips RSA
  343. */
  344. /* Mask Generation Function Identifiers */
  345. #define WC_MGF1NONE 0
  346. #define WC_MGF1SHA1 26
  347. #define WC_MGF1SHA224 4
  348. #define WC_MGF1SHA256 1
  349. #define WC_MGF1SHA384 2
  350. #define WC_MGF1SHA512 3
  351. #define WC_MGF1SHA512_224 5
  352. #define WC_MGF1SHA512_256 6
  353. /* Padding types */
  354. #define WC_RSA_PKCSV15_PAD 0
  355. #define WC_RSA_OAEP_PAD 1
  356. #define WC_RSA_PSS_PAD 2
  357. #define WC_RSA_NO_PAD 3
  358. WOLFSSL_API int wc_RsaPublicEncrypt_ex(const byte* in, word32 inLen, byte* out,
  359. word32 outLen, RsaKey* key, WC_RNG* rng, int type,
  360. enum wc_HashType hash, int mgf, byte* label, word32 labelSz);
  361. WOLFSSL_API int wc_RsaPrivateDecrypt_ex(const byte* in, word32 inLen,
  362. byte* out, word32 outLen, RsaKey* key, int type,
  363. enum wc_HashType hash, int mgf, byte* label, word32 labelSz);
  364. WOLFSSL_API int wc_RsaPrivateDecryptInline_ex(byte* in, word32 inLen,
  365. byte** out, RsaKey* key, int type, enum wc_HashType hash,
  366. int mgf, byte* label, word32 labelSz);
  367. #if defined(WC_RSA_DIRECT) || defined(WC_RSA_NO_PADDING)
  368. WOLFSSL_API int wc_RsaDirect(byte* in, word32 inLen, byte* out, word32* outSz,
  369. RsaKey* key, int type, WC_RNG* rng);
  370. #endif
  371. #endif /* HAVE_FIPS */
  372. WOLFSSL_API int wc_RsaFlattenPublicKey(RsaKey* key, byte* e, word32* eSz,
  373. byte* n, word32* nSz);
  374. WOLFSSL_API int wc_RsaExportKey(RsaKey* key,
  375. byte* e, word32* eSz,
  376. byte* n, word32* nSz,
  377. byte* d, word32* dSz,
  378. byte* p, word32* pSz,
  379. byte* q, word32* qSz);
  380. #ifdef WOLFSSL_KEY_GEN
  381. WOLFSSL_API int wc_MakeRsaKey(RsaKey* key, int size, long e, WC_RNG* rng);
  382. WOLFSSL_API int wc_CheckProbablePrime_ex(const byte* p, word32 pSz,
  383. const byte* q, word32 qSz,
  384. const byte* e, word32 eSz,
  385. int nlen, int* isPrime, WC_RNG* rng);
  386. WOLFSSL_API int wc_CheckProbablePrime(const byte* p, word32 pSz,
  387. const byte* q, word32 qSz,
  388. const byte* e, word32 eSz,
  389. int nlen, int* isPrime);
  390. #endif
  391. WOLFSSL_LOCAL int wc_RsaPad_ex(const byte* input, word32 inputLen, byte* pkcsBlock,
  392. word32 pkcsBlockLen, byte padValue, WC_RNG* rng, int padType,
  393. enum wc_HashType hType, int mgf, byte* optLabel, word32 labelLen,
  394. int saltLen, int bits, void* heap);
  395. WOLFSSL_LOCAL int wc_RsaUnPad_ex(byte* pkcsBlock, word32 pkcsBlockLen, byte** out,
  396. byte padValue, int padType, enum wc_HashType hType,
  397. int mgf, byte* optLabel, word32 labelLen, int saltLen,
  398. int bits, void* heap);
  399. WOLFSSL_LOCAL int wc_hash2mgf(enum wc_HashType hType);
  400. #endif /* HAVE_USER_RSA */
  401. #ifdef __cplusplus
  402. } /* extern "C" */
  403. #endif
  404. #endif /* NO_RSA */
  405. #endif /* WOLF_CRYPT_RSA_H */