rsa.h 16 KB

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