aes.h 12 KB

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