aes.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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. /**
  36. * \brief AES context structure
  37. *
  38. */
  39. typedef struct {
  40. uint8_t key_bytes;
  41. volatile uint8_t key_in_hardware; /* This variable is used for fault injection checks, so marked volatile to avoid optimisation */
  42. uint8_t key[32];
  43. } esp_aes_context;
  44. /**
  45. * \brief The AES XTS context-type definition.
  46. */
  47. typedef struct
  48. {
  49. esp_aes_context crypt; /*!< The AES context to use for AES block
  50. encryption or decryption. */
  51. esp_aes_context tweak; /*!< The AES context used for tweak
  52. computation. */
  53. } esp_aes_xts_context;
  54. /**
  55. * \brief Lock access to AES hardware unit
  56. *
  57. * AES hardware unit can only be used by one
  58. * consumer at a time.
  59. *
  60. * esp_aes_xxx API calls automatically manage locking & unlocking of
  61. * hardware, this function is only needed if you want to call
  62. * ets_aes_xxx functions directly.
  63. */
  64. void esp_aes_acquire_hardware( void );
  65. /**
  66. * \brief Unlock access to AES hardware unit
  67. *
  68. * esp_aes_xxx API calls automatically manage locking & unlocking of
  69. * hardware, this function is only needed if you want to call
  70. * ets_aes_xxx functions directly.
  71. */
  72. void esp_aes_release_hardware( void );
  73. /**
  74. * \brief Initialize AES context
  75. *
  76. * \param ctx AES context to be initialized
  77. */
  78. void esp_aes_init( esp_aes_context *ctx );
  79. /**
  80. * \brief Clear AES context
  81. *
  82. * \param ctx AES context to be cleared
  83. */
  84. void esp_aes_free( esp_aes_context *ctx );
  85. /**
  86. * \brief This function initializes the specified AES XTS context.
  87. *
  88. * It must be the first API called before using
  89. * the context.
  90. *
  91. * \param ctx The AES XTS context to initialize.
  92. */
  93. void esp_aes_xts_init( esp_aes_xts_context *ctx );
  94. /**
  95. * \brief This function releases and clears the specified AES XTS context.
  96. *
  97. * \param ctx The AES XTS context to clear.
  98. */
  99. void esp_aes_xts_free( esp_aes_xts_context *ctx );
  100. /**
  101. * \brief AES set key schedule (encryption or decryption)
  102. *
  103. * \param ctx AES context to be initialized
  104. * \param key encryption key
  105. * \param keybits must be 128, 192 or 256
  106. *
  107. * \return 0 if successful, or ERR_AES_INVALID_KEY_LENGTH
  108. */
  109. int esp_aes_setkey( esp_aes_context *ctx, const unsigned char *key, unsigned int keybits );
  110. /**
  111. * \brief AES-ECB block encryption/decryption
  112. *
  113. * \param ctx AES context
  114. * \param mode AES_ENCRYPT or AES_DECRYPT
  115. * \param input 16-byte input block
  116. * \param output 16-byte output block
  117. *
  118. * \return 0 if successful
  119. */
  120. int esp_aes_crypt_ecb( esp_aes_context *ctx, int mode, const unsigned char input[16], unsigned char output[16] );
  121. /**
  122. * \brief AES-CBC buffer encryption/decryption
  123. * Length should be a multiple of the block
  124. * size (16 bytes)
  125. *
  126. * \note Upon exit, the content of the IV is updated so that you can
  127. * call the function same function again on the following
  128. * block(s) of data and get the same result as if it was
  129. * encrypted in one call. This allows a "streaming" usage.
  130. * If on the other hand you need to retain the contents of the
  131. * IV, you should either save it manually or use the cipher
  132. * module instead.
  133. *
  134. * \param ctx AES context
  135. * \param mode AES_ENCRYPT or AES_DECRYPT
  136. * \param length length of the input data
  137. * \param iv initialization vector (updated after use)
  138. * \param input buffer holding the input data
  139. * \param output buffer holding the output data
  140. *
  141. * \return 0 if successful, or ERR_AES_INVALID_INPUT_LENGTH
  142. */
  143. int esp_aes_crypt_cbc( esp_aes_context *ctx,
  144. int mode,
  145. size_t length,
  146. unsigned char iv[16],
  147. const unsigned char *input,
  148. unsigned char *output );
  149. /**
  150. * \brief AES-CFB128 buffer encryption/decryption.
  151. *
  152. * Note: Due to the nature of CFB you should use the same key schedule for
  153. * both encryption and decryption. So a context initialized with
  154. * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  155. *
  156. * \note Upon exit, the content of the IV is updated so that you can
  157. * call the function same function again on the following
  158. * block(s) of data and get the same result as if it was
  159. * encrypted in one call. This allows a "streaming" usage.
  160. * If on the other hand you need to retain the contents of the
  161. * IV, you should either save it manually or use the cipher
  162. * module instead.
  163. *
  164. * \param ctx AES context
  165. * \param mode AES_ENCRYPT or AES_DECRYPT
  166. * \param length length of the input data
  167. * \param iv_off offset in IV (updated after use)
  168. * \param iv initialization vector (updated after use)
  169. * \param input buffer holding the input data
  170. * \param output buffer holding the output data
  171. *
  172. * \return 0 if successful
  173. */
  174. int esp_aes_crypt_cfb128( esp_aes_context *ctx,
  175. int mode,
  176. size_t length,
  177. size_t *iv_off,
  178. unsigned char iv[16],
  179. const unsigned char *input,
  180. unsigned char *output );
  181. /**
  182. * \brief AES-CFB8 buffer encryption/decryption.
  183. *
  184. * Note: Due to the nature of CFB you should use the same key schedule for
  185. * both encryption and decryption. So a context initialized with
  186. * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  187. *
  188. * \note Upon exit, the content of the IV is updated so that you can
  189. * call the function same function again on the following
  190. * block(s) of data and get the same result as if it was
  191. * encrypted in one call. This allows a "streaming" usage.
  192. * If on the other hand you need to retain the contents of the
  193. * IV, you should either save it manually or use the cipher
  194. * module instead.
  195. *
  196. * \param ctx AES context
  197. * \param mode AES_ENCRYPT or AES_DECRYPT
  198. * \param length length of the input data
  199. * \param iv initialization vector (updated after use)
  200. * \param input buffer holding the input data
  201. * \param output buffer holding the output data
  202. *
  203. * \return 0 if successful
  204. */
  205. int esp_aes_crypt_cfb8( esp_aes_context *ctx,
  206. int mode,
  207. size_t length,
  208. unsigned char iv[16],
  209. const unsigned char *input,
  210. unsigned char *output );
  211. /**
  212. * \brief AES-CTR buffer encryption/decryption
  213. *
  214. * Warning: You have to keep the maximum use of your counter in mind!
  215. *
  216. * Note: Due to the nature of CTR you should use the same key schedule for
  217. * both encryption and decryption. So a context initialized with
  218. * esp_aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
  219. *
  220. * \param ctx AES context
  221. * \param length The length of the data
  222. * \param nc_off The offset in the current stream_block (for resuming
  223. * within current cipher stream). The offset pointer to
  224. * should be 0 at the start of a stream.
  225. * \param nonce_counter The 128-bit nonce and counter.
  226. * \param stream_block The saved stream-block for resuming. Is overwritten
  227. * by the function.
  228. * \param input The input data stream
  229. * \param output The output data stream
  230. *
  231. * \return 0 if successful
  232. */
  233. int esp_aes_crypt_ctr( esp_aes_context *ctx,
  234. size_t length,
  235. size_t *nc_off,
  236. unsigned char nonce_counter[16],
  237. unsigned char stream_block[16],
  238. const unsigned char *input,
  239. unsigned char *output );
  240. /**
  241. * \brief This function prepares an XTS context for encryption and
  242. * sets the encryption key.
  243. *
  244. * \param ctx The AES XTS context to which the key should be bound.
  245. * \param key The encryption key. This is comprised of the XTS key1
  246. * concatenated with the XTS key2.
  247. * \param keybits The size of \p key passed in bits. Valid options are:
  248. * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
  249. * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
  250. *
  251. * \return \c 0 on success.
  252. * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  253. */
  254. int esp_aes_xts_setkey_enc( esp_aes_xts_context *ctx,
  255. const unsigned char *key,
  256. unsigned int keybits );
  257. /**
  258. * \brief This function prepares an XTS context for decryption and
  259. * sets the decryption key.
  260. *
  261. * \param ctx The AES XTS context to which the key should be bound.
  262. * \param key The decryption key. This is comprised of the XTS key1
  263. * concatenated with the XTS key2.
  264. * \param keybits The size of \p key passed in bits. Valid options are:
  265. * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
  266. * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
  267. *
  268. * \return \c 0 on success.
  269. * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  270. */
  271. int esp_aes_xts_setkey_dec( esp_aes_xts_context *ctx,
  272. const unsigned char *key,
  273. unsigned int keybits );
  274. /**
  275. * \brief Internal AES block encryption function
  276. * (Only exposed to allow overriding it,
  277. * see AES_ENCRYPT_ALT)
  278. *
  279. * \param ctx AES context
  280. * \param input Plaintext block
  281. * \param output Output (ciphertext) block
  282. */
  283. int esp_internal_aes_encrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] );
  284. /** Deprecated, see esp_aes_internal_encrypt */
  285. void esp_aes_encrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] ) __attribute__((deprecated));
  286. /**
  287. * \brief Internal AES block decryption function
  288. * (Only exposed to allow overriding it,
  289. * see AES_DECRYPT_ALT)
  290. *
  291. * \param ctx AES context
  292. * \param input Ciphertext block
  293. * \param output Output (plaintext) block
  294. */
  295. int esp_internal_aes_decrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] );
  296. /** Deprecated, see esp_aes_internal_decrypt */
  297. void esp_aes_decrypt( esp_aes_context *ctx, const unsigned char input[16], unsigned char output[16] ) __attribute__((deprecated));
  298. #ifdef __cplusplus
  299. }
  300. #endif
  301. #endif /* aes.h */