aes.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /**
  2. * \brief AES block cipher, ESP32 hardware accelerated version
  3. * Based on mbedTLS FIPS-197 compliant version.
  4. *
  5. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  6. * Additions Copyright (C) 2016, Espressif Systems (Shanghai) PTE Ltd
  7. * SPDX-License-Identifier: Apache-2.0
  8. *
  9. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  10. * not use this file except in compliance with the License.
  11. * You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing, software
  16. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  17. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. * See the License for the specific language governing permissions and
  19. * limitations under the License.
  20. *
  21. *
  22. */
  23. #ifndef ESP_AES_H
  24. #define ESP_AES_H
  25. #include "esp_types.h"
  26. #include "rom/aes.h"
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* padlock.c and aesni.c rely on these values! */
  31. #define ESP_AES_ENCRYPT 1
  32. #define ESP_AES_DECRYPT 0
  33. #define ERR_ESP_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  34. #define ERR_ESP_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  35. typedef struct {
  36. enum AES_BITS aesbits;
  37. uint8_t key[32];
  38. } key_context, KEY_CTX;
  39. /**
  40. * \brief AES context structure
  41. *
  42. * \note buf is able to hold 32 extra bytes, which can be used:
  43. * - for alignment purposes if VIA padlock is used, and/or
  44. * - to simplify key expansion in the 256-bit case by
  45. * generating an extra round key
  46. */
  47. typedef struct {
  48. int nr; /*!< number of rounds */
  49. uint32_t *rk; /*!< AES round keys */
  50. KEY_CTX enc;
  51. KEY_CTX dec;
  52. } esp_aes_context;
  53. /**
  54. * \brief Lock access to AES hardware unit
  55. *
  56. * AES hardware unit can only be used by one
  57. * consumer at a time.
  58. *
  59. * esp_aes_xxx API calls automatically manage locking & unlocking of
  60. * hardware, this function is only needed if you want to call
  61. * ets_aes_xxx functions directly.
  62. */
  63. void esp_aes_acquire_hardware( void );
  64. /**
  65. * \brief Unlock access to AES hardware unit
  66. *
  67. * esp_aes_xxx API calls automatically manage locking & unlocking of
  68. * hardware, this function is only needed if you want to call
  69. * ets_aes_xxx functions directly.
  70. */
  71. void esp_aes_release_hardware( void );
  72. /**
  73. * \brief Initialize AES context
  74. *
  75. * \param ctx AES context to be initialized
  76. */
  77. void esp_aes_init( esp_aes_context *ctx );
  78. /**
  79. * \brief Clear AES context
  80. *
  81. * \param ctx AES context to be cleared
  82. */
  83. void esp_aes_free( esp_aes_context *ctx );
  84. /**
  85. * \brief AES key schedule (encryption)
  86. *
  87. * \param ctx AES context to be initialized
  88. * \param key encryption key
  89. * \param keybits must be 128, 192 or 256
  90. *
  91. * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
  92. */
  93. int esp_aes_setkey_enc( esp_aes_context *ctx, const unsigned char *key, unsigned int keybits );
  94. /**
  95. * \brief AES key schedule (decryption)
  96. *
  97. * \param ctx AES context to be initialized
  98. * \param key decryption key
  99. * \param keybits must be 128, 192 or 256
  100. *
  101. * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
  102. */
  103. int esp_aes_setkey_dec( esp_aes_context *ctx, const unsigned char *key, unsigned int keybits );
  104. /**
  105. * \brief AES-ECB block encryption/decryption
  106. *
  107. * \param ctx AES context
  108. * \param mode AES_ENCRYPT or AES_DECRYPT
  109. * \param input 16-byte input block
  110. * \param output 16-byte output block
  111. *
  112. * \return 0 if successful
  113. */
  114. int esp_aes_crypt_ecb( esp_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16] );
  115. /**
  116. * \brief AES-CBC buffer encryption/decryption
  117. * Length should be a multiple of the block
  118. * size (16 bytes)
  119. *
  120. * \note Upon exit, the content of the IV is updated so that you can
  121. * call the function same function again on the following
  122. * block(s) of data and get the same result as if it was
  123. * encrypted in one call. This allows a "streaming" usage.
  124. * If on the other hand you need to retain the contents of the
  125. * IV, you should either save it manually or use the cipher
  126. * module instead.
  127. *
  128. * \param ctx AES context
  129. * \param mode AES_ENCRYPT or AES_DECRYPT
  130. * \param length length of the input data
  131. * \param iv initialization vector (updated after use)
  132. * \param input buffer holding the input data
  133. * \param output buffer holding the output data
  134. *
  135. * \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH
  136. */
  137. int esp_aes_crypt_cbc( esp_aes_context *ctx,
  138. int mode,
  139. size_t length,
  140. unsigned char iv[16],
  141. const unsigned char *input,
  142. unsigned char *output );
  143. /**
  144. * \brief AES-CFB128 buffer encryption/decryption.
  145. *
  146. * Note: Due to the nature of CFB you should use the same key schedule for
  147. * both encryption and decryption. So a context initialized with
  148. * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  149. *
  150. * \note Upon exit, the content of the IV is updated so that you can
  151. * call the function same function again on the following
  152. * block(s) of data and get the same result as if it was
  153. * encrypted in one call. This allows a "streaming" usage.
  154. * If on the other hand you need to retain the contents of the
  155. * IV, you should either save it manually or use the cipher
  156. * module instead.
  157. *
  158. * \param ctx AES context
  159. * \param mode AES_ENCRYPT or AES_DECRYPT
  160. * \param length length of the input data
  161. * \param iv_off offset in IV (updated after use)
  162. * \param iv initialization vector (updated after use)
  163. * \param input buffer holding the input data
  164. * \param output buffer holding the output data
  165. *
  166. * \return 0 if successful
  167. */
  168. int esp_aes_crypt_cfb128( esp_aes_context *ctx,
  169. int mode,
  170. size_t length,
  171. size_t *iv_off,
  172. unsigned char iv[16],
  173. const unsigned char *input,
  174. unsigned char *output );
  175. /**
  176. * \brief AES-CFB8 buffer encryption/decryption.
  177. *
  178. * Note: Due to the nature of CFB you should use the same key schedule for
  179. * both encryption and decryption. So a context initialized with
  180. * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  181. *
  182. * \note Upon exit, the content of the IV is updated so that you can
  183. * call the function same function again on the following
  184. * block(s) of data and get the same result as if it was
  185. * encrypted in one call. This allows a "streaming" usage.
  186. * If on the other hand you need to retain the contents of the
  187. * IV, you should either save it manually or use the cipher
  188. * module instead.
  189. *
  190. * \param ctx AES context
  191. * \param mode AES_ENCRYPT or AES_DECRYPT
  192. * \param length length of the input data
  193. * \param iv initialization vector (updated after use)
  194. * \param input buffer holding the input data
  195. * \param output buffer holding the output data
  196. *
  197. * \return 0 if successful
  198. */
  199. int esp_aes_crypt_cfb8( esp_aes_context *ctx,
  200. int mode,
  201. size_t length,
  202. unsigned char iv[16],
  203. const unsigned char *input,
  204. unsigned char *output );
  205. /**
  206. * \brief AES-CTR buffer encryption/decryption
  207. *
  208. * Warning: You have to keep the maximum use of your counter in mind!
  209. *
  210. * Note: Due to the nature of CTR you should use the same key schedule for
  211. * both encryption and decryption. So a context initialized with
  212. * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  213. *
  214. * \param ctx AES context
  215. * \param length The length of the data
  216. * \param nc_off The offset in the current stream_block (for resuming
  217. * within current cipher stream). The offset pointer to
  218. * should be 0 at the start of a stream.
  219. * \param nonce_counter The 128-bit nonce and counter.
  220. * \param stream_block The saved stream-block for resuming. Is overwritten
  221. * by the function.
  222. * \param input The input data stream
  223. * \param output The output data stream
  224. *
  225. * \return 0 if successful
  226. */
  227. int esp_aes_crypt_ctr( esp_aes_context *ctx,
  228. size_t length,
  229. size_t *nc_off,
  230. unsigned char nonce_counter[16],
  231. unsigned char stream_block[16],
  232. const unsigned char *input,
  233. unsigned char *output );
  234. /**
  235. * \brief Internal AES block encryption function
  236. * (Only exposed to allow overriding it,
  237. * see AES_ENCRYPT_ALT)
  238. *
  239. * \param ctx AES context
  240. * \param input Plaintext block
  241. * \param output Output (ciphertext) block
  242. */
  243. void esp_aes_encrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] );
  244. /**
  245. * \brief Internal AES block decryption function
  246. * (Only exposed to allow overriding it,
  247. * see AES_DECRYPT_ALT)
  248. *
  249. * \param ctx AES context
  250. * \param input Ciphertext block
  251. * \param output Output (plaintext) block
  252. */
  253. void esp_aes_decrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] );
  254. #ifdef __cplusplus
  255. }
  256. #endif
  257. #endif /* aes.h */