aes.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /* aes.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/aes.h
  23. */
  24. /*
  25. DESCRIPTION
  26. This library provides the interfaces to the Advanced Encryption Standard (AES)
  27. for encrypting and decrypting data. AES is the standard known for a symmetric
  28. block cipher mechanism that uses n-bit binary string parameter key with 128-bits,
  29. 192-bits, and 256-bits of key sizes.
  30. */
  31. #ifndef WOLF_CRYPT_AES_H
  32. #define WOLF_CRYPT_AES_H
  33. #include <wolfssl/wolfcrypt/types.h>
  34. #if !defined(NO_AES) || defined(WOLFSSL_SM4)
  35. typedef struct Gcm {
  36. ALIGN16 byte H[16];
  37. #ifdef OPENSSL_EXTRA
  38. word32 aadH[4]; /* additional authenticated data GHASH */
  39. word32 aadLen; /* additional authenticated data len */
  40. #endif
  41. #ifdef GCM_TABLE
  42. /* key-based fast multiplication table. */
  43. ALIGN16 byte M0[256][16];
  44. #elif defined(GCM_TABLE_4BIT)
  45. #if defined(BIG_ENDIAN_ORDER) || defined(WC_16BIT_CPU)
  46. ALIGN16 byte M0[16][16];
  47. #else
  48. ALIGN16 byte M0[32][16];
  49. #endif
  50. #endif /* GCM_TABLE */
  51. } Gcm;
  52. WOLFSSL_LOCAL void GenerateM0(Gcm* gcm);
  53. #ifdef WOLFSSL_ARMASM
  54. WOLFSSL_LOCAL void GMULT(byte* X, byte* Y);
  55. #endif
  56. WOLFSSL_LOCAL void GHASH(Gcm* gcm, const byte* a, word32 aSz, const byte* c,
  57. word32 cSz, byte* s, word32 sSz);
  58. #endif
  59. #ifndef NO_AES
  60. #if defined(HAVE_FIPS) && \
  61. defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2)
  62. #include <wolfssl/wolfcrypt/fips.h>
  63. #endif /* HAVE_FIPS_VERSION >= 2 */
  64. #ifndef WC_NO_RNG
  65. #include <wolfssl/wolfcrypt/random.h>
  66. #endif
  67. #ifdef STM32_CRYPTO
  68. #include <wolfssl/wolfcrypt/port/st/stm32.h>
  69. #endif
  70. #ifdef WOLFSSL_IMXRT_DCP
  71. #include "fsl_dcp.h"
  72. #endif
  73. #ifdef WOLFSSL_XILINX_CRYPT
  74. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  75. #include <wolfssl/wolfcrypt/port/xilinx/xil-versal-glue.h>
  76. #include <xsecure_aesclient.h>
  77. #define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_AES_USER_KEY_0
  78. #else /* versal */
  79. #include <xsecure_aes.h>
  80. #define WOLFSSL_XILINX_AES_KEY_SRC XSECURE_CSU_AES_KEY_SRC_KUP
  81. #endif /* !versal */
  82. #endif /* WOLFSSL_XILINX_CRYPT */
  83. #if defined(WOLFSSL_XILINX_CRYPT) || defined(WOLFSSL_AFALG_XILINX_AES)
  84. #if !defined(WOLFSSL_XILINX_AES_KEY_SRC)
  85. #define WOLFSSL_XILINX_AES_KEY_SRC 0
  86. #endif /* !defined(WOLFSSL_XILINX_AES_KEY_SRC) */
  87. #endif /* all Xilinx crypto */
  88. #ifdef WOLFSSL_SE050
  89. #include <wolfssl/wolfcrypt/port/nxp/se050_port.h>
  90. #endif
  91. #if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES)
  92. /* included for struct msghdr */
  93. #include <wolfssl/wolfcrypt/port/af_alg/wc_afalg.h>
  94. #endif
  95. #if defined(WOLFSSL_KCAPI_AES)
  96. #include <wolfssl/wolfcrypt/port/kcapi/wc_kcapi.h>
  97. #endif
  98. #if defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC)
  99. #include <wolfssl/wolfcrypt/port/devcrypto/wc_devcrypto.h>
  100. #endif
  101. #ifdef WOLFSSL_SILABS_SE_ACCEL
  102. #include <wolfssl/wolfcrypt/port/silabs/silabs_aes.h>
  103. #endif
  104. #if defined(HAVE_AESGCM) && !defined(WC_NO_RNG)
  105. #include <wolfssl/wolfcrypt/random.h>
  106. #endif
  107. #if defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_AES)
  108. #include <psa/crypto.h>
  109. #endif
  110. #if defined(WOLFSSL_CRYPTOCELL)
  111. #include <wolfssl/wolfcrypt/port/arm/cryptoCell.h>
  112. #endif
  113. #if (defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  114. defined(WOLFSSL_RENESAS_TSIP_TLS_AES_CRYPT)) ||\
  115. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  116. #include <wolfssl/wolfcrypt/port/Renesas/renesas_tsip_types.h>
  117. #endif
  118. #if defined(WOLFSSL_RENESAS_FSPSM)
  119. #include <wolfssl/wolfcrypt/port/Renesas/renesas-fspsm-crypt.h>
  120. #endif
  121. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  122. #include <wolfssl/wolfcrypt/port/maxim/maxq10xx.h>
  123. #endif
  124. #ifdef __cplusplus
  125. extern "C" {
  126. #endif
  127. #ifndef WOLFSSL_AES_KEY_SIZE_ENUM
  128. #define WOLFSSL_AES_KEY_SIZE_ENUM
  129. /* these are required for FIPS and non-FIPS */
  130. enum {
  131. AES_128_KEY_SIZE = 16, /* for 128 bit */
  132. AES_192_KEY_SIZE = 24, /* for 192 bit */
  133. AES_256_KEY_SIZE = 32, /* for 256 bit */
  134. AES_IV_SIZE = 16 /* always block size */
  135. };
  136. #endif
  137. /* avoid redefinition of structs */
  138. #if !defined(HAVE_FIPS) || \
  139. (defined(HAVE_FIPS_VERSION) && (HAVE_FIPS_VERSION >= 2))
  140. #ifdef WOLFSSL_ASYNC_CRYPT
  141. #include <wolfssl/wolfcrypt/async.h>
  142. #endif
  143. enum {
  144. AES_ENC_TYPE = WC_CIPHER_AES, /* cipher unique type */
  145. AES_ENCRYPTION = 0,
  146. AES_DECRYPTION = 1,
  147. AES_BLOCK_SIZE = 16,
  148. KEYWRAP_BLOCK_SIZE = 8,
  149. GCM_NONCE_MAX_SZ = 16, /* wolfCrypt's maximum nonce size allowed. */
  150. GCM_NONCE_MID_SZ = 12, /* The default nonce size for AES-GCM. */
  151. GCM_NONCE_MIN_SZ = 8, /* wolfCrypt's minimum nonce size allowed. */
  152. CCM_NONCE_MIN_SZ = 7,
  153. CCM_NONCE_MAX_SZ = 13,
  154. CTR_SZ = 4,
  155. AES_IV_FIXED_SZ = 4,
  156. #ifdef WOLFSSL_AES_CFB
  157. AES_CFB_MODE = 1,
  158. #endif
  159. #ifdef WOLFSSL_AES_OFB
  160. AES_OFB_MODE = 2,
  161. #endif
  162. #ifdef WOLFSSL_AES_XTS
  163. AES_XTS_MODE = 3,
  164. #endif
  165. #ifdef WOLF_PRIVATE_KEY_ID
  166. AES_MAX_ID_LEN = 32,
  167. AES_MAX_LABEL_LEN = 32,
  168. #endif
  169. WOLF_ENUM_DUMMY_LAST_ELEMENT(AES)
  170. };
  171. struct Aes {
  172. /* AESNI needs key first, rounds 2nd, not sure why yet */
  173. ALIGN16 word32 key[60];
  174. word32 rounds;
  175. int keylen;
  176. ALIGN16 word32 reg[AES_BLOCK_SIZE / sizeof(word32)]; /* for CBC mode */
  177. ALIGN16 word32 tmp[AES_BLOCK_SIZE / sizeof(word32)]; /* same */
  178. #if defined(HAVE_AESGCM) || defined(HAVE_AESCCM)
  179. word32 invokeCtr[2];
  180. word32 nonceSz;
  181. #endif
  182. #ifdef HAVE_AESGCM
  183. Gcm gcm;
  184. #ifdef WOLFSSL_SE050
  185. sss_symmetric_t aes_ctx; /* used as the function context */
  186. int ctxInitDone;
  187. word32 keyId;
  188. byte keyIdSet;
  189. byte useSWCrypt; /* Use SW crypt instead of SE050, before SCP03 auth */
  190. #endif
  191. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  192. word32 y0;
  193. #endif
  194. #endif /* HAVE_AESGCM */
  195. #ifdef WOLFSSL_CAAM
  196. int blackKey; /* black key / hsm key id */
  197. #endif
  198. #ifdef WOLFSSL_AESNI
  199. byte use_aesni;
  200. #endif /* WOLFSSL_AESNI */
  201. #ifdef WOLF_CRYPTO_CB
  202. int devId;
  203. void* devCtx;
  204. #endif
  205. #ifdef WOLF_PRIVATE_KEY_ID
  206. byte id[AES_MAX_ID_LEN];
  207. int idLen;
  208. char label[AES_MAX_LABEL_LEN];
  209. int labelLen;
  210. #endif
  211. #ifdef WOLFSSL_ASYNC_CRYPT
  212. WC_ASYNC_DEV asyncDev;
  213. #endif /* WOLFSSL_ASYNC_CRYPT */
  214. #if defined(WOLFSSL_AES_COUNTER) || defined(WOLFSSL_AES_CFB) || \
  215. defined(WOLFSSL_AES_OFB) || defined(WOLFSSL_AES_XTS)
  216. word32 left; /* unused bytes left from last call */
  217. #endif
  218. #ifdef WOLFSSL_XILINX_CRYPT
  219. #ifdef WOLFSSL_XILINX_CRYPT_VERSAL
  220. wc_Xsecure xSec;
  221. XSecure_AesKeySize xKeySize;
  222. int aadStyle;
  223. byte keyInit[WOLFSSL_XSECURE_AES_KEY_SIZE] ALIGN64;
  224. #else
  225. XSecure_Aes xilAes;
  226. XCsuDma dma;
  227. word32 keyInit[8];
  228. #endif
  229. word32 kup;
  230. #endif
  231. #if defined(WOLFSSL_AFALG) || defined(WOLFSSL_AFALG_XILINX_AES)
  232. int alFd; /* server socket to bind to */
  233. int rdFd; /* socket to read from */
  234. struct msghdr msg;
  235. int dir; /* flag for encrpyt or decrypt */
  236. #ifdef WOLFSSL_AFALG_XILINX_AES
  237. word32 msgBuf[CMSG_SPACE(4) + CMSG_SPACE(sizeof(struct af_alg_iv) +
  238. GCM_NONCE_MID_SZ)];
  239. #endif
  240. #endif
  241. #if defined(WOLFSSL_KCAPI_AES)
  242. struct kcapi_handle* handle;
  243. int init;
  244. #endif
  245. #if defined(WOLF_CRYPTO_CB) || (defined(WOLFSSL_DEVCRYPTO) && \
  246. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))) || \
  247. (defined(WOLFSSL_ASYNC_CRYPT) && defined(WC_ASYNC_ENABLE_AES)) || \
  248. defined(WOLFSSL_KCAPI_AES)
  249. word32 devKey[AES_MAX_KEY_SIZE/WOLFSSL_BIT_SIZE/sizeof(word32)]; /* raw key */
  250. #ifdef HAVE_CAVIUM_OCTEON_SYNC
  251. int keySet;
  252. #endif
  253. #endif
  254. #if defined(WOLFSSL_DEVCRYPTO) && \
  255. (defined(WOLFSSL_DEVCRYPTO_AES) || defined(WOLFSSL_DEVCRYPTO_CBC))
  256. WC_CRYPTODEV ctx;
  257. #endif
  258. #if defined(WOLFSSL_CRYPTOCELL)
  259. aes_context_t ctx;
  260. #endif
  261. #if (defined(WOLFSSL_RENESAS_TSIP_TLS) && \
  262. defined(WOLFSSL_RENESAS_TSIP_TLS_AES_CRYPT)) ||\
  263. defined(WOLFSSL_RENESAS_TSIP_CRYPTONLY)
  264. TSIP_AES_CTX ctx;
  265. #endif
  266. #if defined(WOLFSSL_RENESAS_FSPSM)
  267. FSPSM_AES_CTX ctx;
  268. #endif
  269. #if defined(WOLFSSL_IMXRT_DCP)
  270. dcp_handle_t handle;
  271. #endif
  272. #if defined(WOLFSSL_SILABS_SE_ACCEL)
  273. silabs_aes_t ctx;
  274. #endif
  275. #ifdef WOLFSSL_MAXQ10XX_CRYPTO
  276. maxq_aes_t maxq_ctx;
  277. #endif
  278. #if defined(WOLFSSL_HAVE_PSA) && !defined(WOLFSSL_PSA_NO_AES)
  279. psa_key_id_t key_id;
  280. psa_cipher_operation_t psa_ctx;
  281. int ctx_initialized;
  282. int key_need_importing;
  283. #endif
  284. void* heap; /* memory hint to use */
  285. #ifdef WOLFSSL_AESGCM_STREAM
  286. #if !defined(WOLFSSL_SMALL_STACK) || defined(WOLFSSL_AESNI)
  287. ALIGN16 byte streamData[5 * AES_BLOCK_SIZE];
  288. #else
  289. byte* streamData;
  290. #endif
  291. word32 aSz;
  292. word32 cSz;
  293. byte over;
  294. byte aOver;
  295. byte cOver;
  296. byte gcmKeySet:1;
  297. byte nonceSet:1;
  298. byte ctrSet:1;
  299. #endif
  300. };
  301. #ifndef WC_AES_TYPE_DEFINED
  302. typedef struct Aes Aes;
  303. #define WC_AES_TYPE_DEFINED
  304. #endif
  305. #ifdef WOLFSSL_AES_XTS
  306. typedef struct XtsAes {
  307. Aes aes;
  308. Aes tweak;
  309. } XtsAes;
  310. #endif
  311. #ifdef HAVE_AESGCM
  312. typedef struct Gmac {
  313. Aes aes;
  314. } Gmac;
  315. #endif /* HAVE_AESGCM */
  316. #endif /* HAVE_FIPS */
  317. /* Authenticate cipher function prototypes */
  318. typedef int (*wc_AesAuthEncryptFunc)(Aes* aes, byte* out,
  319. const byte* in, word32 sz,
  320. const byte* iv, word32 ivSz,
  321. byte* authTag, word32 authTagSz,
  322. const byte* authIn, word32 authInSz);
  323. typedef int (*wc_AesAuthDecryptFunc)(Aes* aes, byte* out,
  324. const byte* in, word32 sz,
  325. const byte* iv, word32 ivSz,
  326. const byte* authTag, word32 authTagSz,
  327. const byte* authIn, word32 authInSz);
  328. /* AES-CBC */
  329. WOLFSSL_API int wc_AesSetKey(Aes* aes, const byte* key, word32 len,
  330. const byte* iv, int dir);
  331. WOLFSSL_API int wc_AesSetIV(Aes* aes, const byte* iv);
  332. #ifdef HAVE_AES_CBC
  333. WOLFSSL_API int wc_AesCbcEncrypt(Aes* aes, byte* out,
  334. const byte* in, word32 sz);
  335. WOLFSSL_API int wc_AesCbcDecrypt(Aes* aes, byte* out,
  336. const byte* in, word32 sz);
  337. #endif
  338. #ifdef WOLFSSL_AES_CFB
  339. WOLFSSL_API int wc_AesCfbEncrypt(Aes* aes, byte* out,
  340. const byte* in, word32 sz);
  341. WOLFSSL_API int wc_AesCfb1Encrypt(Aes* aes, byte* out,
  342. const byte* in, word32 sz);
  343. WOLFSSL_API int wc_AesCfb8Encrypt(Aes* aes, byte* out,
  344. const byte* in, word32 sz);
  345. #ifdef HAVE_AES_DECRYPT
  346. WOLFSSL_API int wc_AesCfbDecrypt(Aes* aes, byte* out,
  347. const byte* in, word32 sz);
  348. WOLFSSL_API int wc_AesCfb1Decrypt(Aes* aes, byte* out,
  349. const byte* in, word32 sz);
  350. WOLFSSL_API int wc_AesCfb8Decrypt(Aes* aes, byte* out,
  351. const byte* in, word32 sz);
  352. #endif /* HAVE_AES_DECRYPT */
  353. #endif /* WOLFSSL_AES_CFB */
  354. #ifdef WOLFSSL_AES_OFB
  355. WOLFSSL_API int wc_AesOfbEncrypt(Aes* aes, byte* out,
  356. const byte* in, word32 sz);
  357. #ifdef HAVE_AES_DECRYPT
  358. WOLFSSL_API int wc_AesOfbDecrypt(Aes* aes, byte* out,
  359. const byte* in, word32 sz);
  360. #endif /* HAVE_AES_DECRYPT */
  361. #endif /* WOLFSSL_AES_OFB */
  362. #ifdef HAVE_AES_ECB
  363. WOLFSSL_API int wc_AesEcbEncrypt(Aes* aes, byte* out,
  364. const byte* in, word32 sz);
  365. WOLFSSL_API int wc_AesEcbDecrypt(Aes* aes, byte* out,
  366. const byte* in, word32 sz);
  367. #endif
  368. /* AES-CTR */
  369. #ifdef WOLFSSL_AES_COUNTER
  370. WOLFSSL_API int wc_AesCtrEncrypt(Aes* aes, byte* out,
  371. const byte* in, word32 sz);
  372. WOLFSSL_API int wc_AesCtrSetKey(Aes* aes, const byte* key, word32 len,
  373. const byte* iv, int dir);
  374. #endif
  375. /* AES-DIRECT */
  376. #if defined(WOLFSSL_AES_DIRECT)
  377. #if defined(BUILDING_WOLFSSL)
  378. WOLFSSL_API WARN_UNUSED_RESULT int wc_AesEncryptDirect(Aes* aes, byte* out,
  379. const byte* in);
  380. WOLFSSL_API WARN_UNUSED_RESULT int wc_AesDecryptDirect(Aes* aes, byte* out,
  381. const byte* in);
  382. WOLFSSL_API WARN_UNUSED_RESULT int wc_AesSetKeyDirect(Aes* aes,
  383. const byte* key,
  384. word32 len,
  385. const byte* iv, int dir);
  386. #else
  387. WOLFSSL_API int wc_AesEncryptDirect(Aes* aes, byte* out, const byte* in);
  388. WOLFSSL_API int wc_AesDecryptDirect(Aes* aes, byte* out, const byte* in);
  389. WOLFSSL_API int wc_AesSetKeyDirect(Aes* aes, const byte* key, word32 len,
  390. const byte* iv, int dir);
  391. #endif
  392. #endif
  393. #ifdef HAVE_AESGCM
  394. #ifdef WOLFSSL_XILINX_CRYPT
  395. WOLFSSL_API int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len,
  396. word32 kup);
  397. #elif defined(WOLFSSL_AFALG_XILINX_AES)
  398. WOLFSSL_LOCAL int wc_AesGcmSetKey_ex(Aes* aes, const byte* key, word32 len,
  399. word32 kup);
  400. #endif
  401. WOLFSSL_API int wc_AesGcmSetKey(Aes* aes, const byte* key, word32 len);
  402. WOLFSSL_API int wc_AesGcmEncrypt(Aes* aes, byte* out,
  403. const byte* in, word32 sz,
  404. const byte* iv, word32 ivSz,
  405. byte* authTag, word32 authTagSz,
  406. const byte* authIn, word32 authInSz);
  407. WOLFSSL_API int wc_AesGcmDecrypt(Aes* aes, byte* out,
  408. const byte* in, word32 sz,
  409. const byte* iv, word32 ivSz,
  410. const byte* authTag, word32 authTagSz,
  411. const byte* authIn, word32 authInSz);
  412. #ifdef WOLFSSL_AESGCM_STREAM
  413. WOLFSSL_API int wc_AesGcmInit(Aes* aes, const byte* key, word32 len,
  414. const byte* iv, word32 ivSz);
  415. WOLFSSL_API int wc_AesGcmEncryptInit(Aes* aes, const byte* key, word32 len,
  416. const byte* iv, word32 ivSz);
  417. WOLFSSL_API int wc_AesGcmEncryptInit_ex(Aes* aes, const byte* key, word32 len,
  418. byte* ivOut, word32 ivOutSz);
  419. WOLFSSL_API int wc_AesGcmEncryptUpdate(Aes* aes, byte* out, const byte* in,
  420. word32 sz, const byte* authIn, word32 authInSz);
  421. WOLFSSL_API int wc_AesGcmEncryptFinal(Aes* aes, byte* authTag,
  422. word32 authTagSz);
  423. WOLFSSL_API int wc_AesGcmDecryptInit(Aes* aes, const byte* key, word32 len,
  424. const byte* iv, word32 ivSz);
  425. WOLFSSL_API int wc_AesGcmDecryptUpdate(Aes* aes, byte* out, const byte* in,
  426. word32 sz, const byte* authIn, word32 authInSz);
  427. WOLFSSL_API int wc_AesGcmDecryptFinal(Aes* aes, const byte* authTag,
  428. word32 authTagSz);
  429. #endif
  430. #ifndef WC_NO_RNG
  431. WOLFSSL_API int wc_AesGcmSetExtIV(Aes* aes, const byte* iv, word32 ivSz);
  432. WOLFSSL_API int wc_AesGcmSetIV(Aes* aes, word32 ivSz,
  433. const byte* ivFixed, word32 ivFixedSz,
  434. WC_RNG* rng);
  435. WOLFSSL_API int wc_AesGcmEncrypt_ex(Aes* aes, byte* out,
  436. const byte* in, word32 sz,
  437. byte* ivOut, word32 ivOutSz,
  438. byte* authTag, word32 authTagSz,
  439. const byte* authIn, word32 authInSz);
  440. #endif /* WC_NO_RNG */
  441. WOLFSSL_API int wc_GmacSetKey(Gmac* gmac, const byte* key, word32 len);
  442. WOLFSSL_API int wc_GmacUpdate(Gmac* gmac, const byte* iv, word32 ivSz,
  443. const byte* authIn, word32 authInSz,
  444. byte* authTag, word32 authTagSz);
  445. #ifndef WC_NO_RNG
  446. WOLFSSL_API int wc_Gmac(const byte* key, word32 keySz, byte* iv, word32 ivSz,
  447. const byte* authIn, word32 authInSz,
  448. byte* authTag, word32 authTagSz, WC_RNG* rng);
  449. WOLFSSL_API int wc_GmacVerify(const byte* key, word32 keySz,
  450. const byte* iv, word32 ivSz,
  451. const byte* authIn, word32 authInSz,
  452. const byte* authTag, word32 authTagSz);
  453. #endif /* WC_NO_RNG */
  454. #endif /* HAVE_AESGCM */
  455. #ifdef HAVE_AESCCM
  456. WOLFSSL_LOCAL int wc_AesCcmCheckTagSize(int sz);
  457. WOLFSSL_API int wc_AesCcmSetKey(Aes* aes, const byte* key, word32 keySz);
  458. WOLFSSL_API int wc_AesCcmEncrypt(Aes* aes, byte* out,
  459. const byte* in, word32 inSz,
  460. const byte* nonce, word32 nonceSz,
  461. byte* authTag, word32 authTagSz,
  462. const byte* authIn, word32 authInSz);
  463. WOLFSSL_API int wc_AesCcmDecrypt(Aes* aes, byte* out,
  464. const byte* in, word32 inSz,
  465. const byte* nonce, word32 nonceSz,
  466. const byte* authTag, word32 authTagSz,
  467. const byte* authIn, word32 authInSz);
  468. WOLFSSL_API int wc_AesCcmSetNonce(Aes* aes,
  469. const byte* nonce, word32 nonceSz);
  470. WOLFSSL_API int wc_AesCcmEncrypt_ex(Aes* aes, byte* out,
  471. const byte* in, word32 sz,
  472. byte* ivOut, word32 ivOutSz,
  473. byte* authTag, word32 authTagSz,
  474. const byte* authIn, word32 authInSz);
  475. #endif /* HAVE_AESCCM */
  476. #ifdef HAVE_AES_KEYWRAP
  477. WOLFSSL_API int wc_AesKeyWrap(const byte* key, word32 keySz,
  478. const byte* in, word32 inSz,
  479. byte* out, word32 outSz,
  480. const byte* iv);
  481. WOLFSSL_API int wc_AesKeyWrap_ex(Aes *aes,
  482. const byte* in, word32 inSz,
  483. byte* out, word32 outSz,
  484. const byte* iv);
  485. WOLFSSL_API int wc_AesKeyUnWrap(const byte* key, word32 keySz,
  486. const byte* in, word32 inSz,
  487. byte* out, word32 outSz,
  488. const byte* iv);
  489. WOLFSSL_API int wc_AesKeyUnWrap_ex(Aes *aes,
  490. const byte* in, word32 inSz,
  491. byte* out, word32 outSz,
  492. const byte* iv);
  493. #endif /* HAVE_AES_KEYWRAP */
  494. #ifdef WOLFSSL_AES_XTS
  495. WOLFSSL_API int wc_AesXtsSetKey(XtsAes* aes, const byte* key,
  496. word32 len, int dir, void* heap, int devId);
  497. WOLFSSL_API int wc_AesXtsEncryptSector(XtsAes* aes, byte* out,
  498. const byte* in, word32 sz, word64 sector);
  499. WOLFSSL_API int wc_AesXtsDecryptSector(XtsAes* aes, byte* out,
  500. const byte* in, word32 sz, word64 sector);
  501. WOLFSSL_API int wc_AesXtsEncrypt(XtsAes* aes, byte* out,
  502. const byte* in, word32 sz, const byte* i, word32 iSz);
  503. WOLFSSL_API int wc_AesXtsDecrypt(XtsAes* aes, byte* out,
  504. const byte* in, word32 sz, const byte* i, word32 iSz);
  505. WOLFSSL_API int wc_AesXtsEncryptConsecutiveSectors(XtsAes* aes,
  506. byte* out, const byte* in, word32 sz, word64 sector,
  507. word32 sectorSz);
  508. WOLFSSL_API int wc_AesXtsDecryptConsecutiveSectors(XtsAes* aes,
  509. byte* out, const byte* in, word32 sz, word64 sector,
  510. word32 sectorSz);
  511. WOLFSSL_API int wc_AesXtsFree(XtsAes* aes);
  512. #endif
  513. WOLFSSL_API int wc_AesGetKeySize(Aes* aes, word32* keySize);
  514. WOLFSSL_API int wc_AesInit(Aes* aes, void* heap, int devId);
  515. #ifdef WOLF_PRIVATE_KEY_ID
  516. WOLFSSL_API int wc_AesInit_Id(Aes* aes, unsigned char* id, int len, void* heap,
  517. int devId);
  518. WOLFSSL_API int wc_AesInit_Label(Aes* aes, const char* label, void* heap,
  519. int devId);
  520. #endif
  521. WOLFSSL_API void wc_AesFree(Aes* aes);
  522. #ifdef WOLFSSL_AES_SIV
  523. WOLFSSL_API
  524. int wc_AesSivEncrypt(const byte* key, word32 keySz, const byte* assoc,
  525. word32 assocSz, const byte* nonce, word32 nonceSz,
  526. const byte* in, word32 inSz, byte* siv, byte* out);
  527. WOLFSSL_API
  528. int wc_AesSivDecrypt(const byte* key, word32 keySz, const byte* assoc,
  529. word32 assocSz, const byte* nonce, word32 nonceSz,
  530. const byte* in, word32 inSz, byte* siv, byte* out);
  531. #endif
  532. #ifdef WOLFSSL_AES_EAX
  533. /* Because of the circular dependency between AES and CMAC, we need to prevent
  534. * inclusion of AES EAX from CMAC to avoid a recursive inclusion */
  535. #ifndef WOLF_CRYPT_CMAC_H
  536. #include <wolfssl/wolfcrypt/cmac.h>
  537. struct AesEax {
  538. Aes aes;
  539. Cmac nonceCmac;
  540. Cmac aadCmac;
  541. Cmac ciphertextCmac;
  542. byte nonceCmacFinal[AES_BLOCK_SIZE];
  543. byte aadCmacFinal[AES_BLOCK_SIZE];
  544. byte ciphertextCmacFinal[AES_BLOCK_SIZE];
  545. byte prefixBuf[AES_BLOCK_SIZE];
  546. };
  547. #endif /* !defined(WOLF_CRYPT_CMAC_H) */
  548. typedef struct AesEax AesEax;
  549. /* One-shot API */
  550. WOLFSSL_API int wc_AesEaxEncryptAuth(const byte* key, word32 keySz, byte* out,
  551. const byte* in, word32 inSz,
  552. const byte* nonce, word32 nonceSz,
  553. /* output computed auth tag */
  554. byte* authTag, word32 authTagSz,
  555. /* input data to authenticate (header) */
  556. const byte* authIn, word32 authInSz);
  557. WOLFSSL_API int wc_AesEaxDecryptAuth(const byte* key, word32 keySz, byte* out,
  558. const byte* in, word32 inSz,
  559. const byte* nonce, word32 nonceSz,
  560. /* auth tag to verify against */
  561. const byte* authTag, word32 authTagSz,
  562. /* input data to authenticate (header) */
  563. const byte* authIn, word32 authInSz);
  564. /* Incremental API */
  565. WOLFSSL_API int wc_AesEaxInit(AesEax* eax,
  566. const byte* key, word32 keySz,
  567. const byte* nonce, word32 nonceSz,
  568. const byte* authIn, word32 authInSz);
  569. WOLFSSL_API int wc_AesEaxEncryptUpdate(AesEax* eax, byte* out,
  570. const byte* in, word32 inSz,
  571. const byte* authIn, word32 authInSz);
  572. WOLFSSL_API int wc_AesEaxDecryptUpdate(AesEax* eax, byte* out,
  573. const byte* in, word32 inSz,
  574. const byte* authIn, word32 authInSz);
  575. WOLFSSL_API int wc_AesEaxAuthDataUpdate(AesEax* eax,
  576. const byte* authIn, word32 authInSz);
  577. WOLFSSL_API int wc_AesEaxEncryptFinal(AesEax* eax,
  578. byte* authTag, word32 authTagSz);
  579. WOLFSSL_API int wc_AesEaxDecryptFinal(AesEax* eax,
  580. const byte* authIn, word32 authInSz);
  581. WOLFSSL_API int wc_AesEaxFree(AesEax* eax);
  582. #endif /* WOLFSSL_AES_EAX */
  583. #ifdef __cplusplus
  584. } /* extern "C" */
  585. #endif
  586. #endif /* NO_AES */
  587. #endif /* WOLF_CRYPT_AES_H */