aes.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. /**
  2. * \file aes.h
  3. *
  4. * \brief AES block cipher
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  22. */
  23. #ifndef MBEDTLS_AES_H
  24. #define MBEDTLS_AES_H
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #include <stddef.h>
  31. #include <stdint.h>
  32. /* padlock.c and aesni.c rely on these values! */
  33. #define MBEDTLS_AES_ENCRYPT 1
  34. #define MBEDTLS_AES_DECRYPT 0
  35. #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
  36. #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
  37. #if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
  38. !defined(inline) && !defined(__cplusplus)
  39. #define inline __inline
  40. #endif
  41. #if !defined(MBEDTLS_AES_ALT)
  42. // Regular implementation
  43. //
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * \brief AES context structure
  49. *
  50. * \note buf is able to hold 32 extra bytes, which can be used:
  51. * - for alignment purposes if VIA padlock is used, and/or
  52. * - to simplify key expansion in the 256-bit case by
  53. * generating an extra round key
  54. */
  55. typedef struct
  56. {
  57. int nr; /*!< number of rounds */
  58. uint32_t *rk; /*!< AES round keys */
  59. uint32_t buf[68]; /*!< unaligned data */
  60. }
  61. mbedtls_aes_context;
  62. /**
  63. * \brief Initialize AES context
  64. *
  65. * \param ctx AES context to be initialized
  66. */
  67. void mbedtls_aes_init( mbedtls_aes_context *ctx );
  68. /**
  69. * \brief Clear AES context
  70. *
  71. * \param ctx AES context to be cleared
  72. */
  73. void mbedtls_aes_free( mbedtls_aes_context *ctx );
  74. /**
  75. * \brief AES key schedule (encryption)
  76. *
  77. * \param ctx AES context to be initialized
  78. * \param key encryption key
  79. * \param keybits must be 128, 192 or 256
  80. *
  81. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  82. */
  83. int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
  84. unsigned int keybits );
  85. /**
  86. * \brief AES key schedule (decryption)
  87. *
  88. * \param ctx AES context to be initialized
  89. * \param key decryption key
  90. * \param keybits must be 128, 192 or 256
  91. *
  92. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  93. */
  94. int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
  95. unsigned int keybits );
  96. /**
  97. * \brief AES-ECB block encryption/decryption
  98. *
  99. * \param ctx AES context
  100. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  101. * \param input 16-byte input block
  102. * \param output 16-byte output block
  103. *
  104. * \return 0 if successful
  105. */
  106. int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
  107. int mode,
  108. const unsigned char input[16],
  109. unsigned char output[16] );
  110. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  111. /**
  112. * \brief AES-CBC buffer encryption/decryption
  113. * Length should be a multiple of the block
  114. * size (16 bytes)
  115. *
  116. * \note Upon exit, the content of the IV is updated so that you can
  117. * call the function same function again on the following
  118. * block(s) of data and get the same result as if it was
  119. * encrypted in one call. This allows a "streaming" usage.
  120. * If on the other hand you need to retain the contents of the
  121. * IV, you should either save it manually or use the cipher
  122. * module instead.
  123. *
  124. * \param ctx AES context
  125. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  126. * \param length length of the input data
  127. * \param iv initialization vector (updated after use)
  128. * \param input buffer holding the input data
  129. * \param output buffer holding the output data
  130. *
  131. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
  132. */
  133. int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
  134. int mode,
  135. size_t length,
  136. unsigned char iv[16],
  137. const unsigned char *input,
  138. unsigned char *output );
  139. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  140. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  141. /**
  142. * \brief AES-CFB128 buffer encryption/decryption.
  143. *
  144. * Note: Due to the nature of CFB you should use the same key schedule for
  145. * both encryption and decryption. So a context initialized with
  146. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  147. *
  148. * \note Upon exit, the content of the IV is updated so that you can
  149. * call the function same function again on the following
  150. * block(s) of data and get the same result as if it was
  151. * encrypted in one call. This allows a "streaming" usage.
  152. * If on the other hand you need to retain the contents of the
  153. * IV, you should either save it manually or use the cipher
  154. * module instead.
  155. *
  156. * \param ctx AES context
  157. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  158. * \param length length of the input data
  159. * \param iv_off offset in IV (updated after use)
  160. * \param iv initialization vector (updated after use)
  161. * \param input buffer holding the input data
  162. * \param output buffer holding the output data
  163. *
  164. * \return 0 if successful
  165. */
  166. int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
  167. int mode,
  168. size_t length,
  169. size_t *iv_off,
  170. unsigned char iv[16],
  171. const unsigned char *input,
  172. unsigned char *output );
  173. /**
  174. * \brief AES-CFB8 buffer encryption/decryption.
  175. *
  176. * Note: Due to the nature of CFB you should use the same key schedule for
  177. * both encryption and decryption. So a context initialized with
  178. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  179. *
  180. * \note Upon exit, the content of the IV is updated so that you can
  181. * call the function same function again on the following
  182. * block(s) of data and get the same result as if it was
  183. * encrypted in one call. This allows a "streaming" usage.
  184. * If on the other hand you need to retain the contents of the
  185. * IV, you should either save it manually or use the cipher
  186. * module instead.
  187. *
  188. * \param ctx AES context
  189. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  190. * \param length length of the input data
  191. * \param iv initialization vector (updated after use)
  192. * \param input buffer holding the input data
  193. * \param output buffer holding the output data
  194. *
  195. * \return 0 if successful
  196. */
  197. int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
  198. int mode,
  199. size_t length,
  200. unsigned char iv[16],
  201. const unsigned char *input,
  202. unsigned char *output );
  203. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  204. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  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. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_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 mbedtls_aes_crypt_ctr( mbedtls_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. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  235. /**
  236. * \brief Internal AES block encryption function
  237. * (Only exposed to allow overriding it,
  238. * see MBEDTLS_AES_ENCRYPT_ALT)
  239. *
  240. * \param ctx AES context
  241. * \param input Plaintext block
  242. * \param output Output (ciphertext) block
  243. *
  244. * \return 0 if successful
  245. */
  246. int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
  247. const unsigned char input[16],
  248. unsigned char output[16] );
  249. /**
  250. * \brief Internal AES block decryption function
  251. * (Only exposed to allow overriding it,
  252. * see MBEDTLS_AES_DECRYPT_ALT)
  253. *
  254. * \param ctx AES context
  255. * \param input Ciphertext block
  256. * \param output Output (plaintext) block
  257. *
  258. * \return 0 if successful
  259. */
  260. int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
  261. const unsigned char input[16],
  262. unsigned char output[16] );
  263. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  264. #if defined(MBEDTLS_DEPRECATED_WARNING)
  265. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  266. #else
  267. #define MBEDTLS_DEPRECATED
  268. #endif
  269. /**
  270. * \brief Deprecated internal AES block encryption function
  271. * without return value.
  272. *
  273. * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
  274. *
  275. * \param ctx AES context
  276. * \param input Plaintext block
  277. * \param output Output (ciphertext) block
  278. */
  279. MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
  280. const unsigned char input[16],
  281. unsigned char output[16] );
  282. /**
  283. * \brief Deprecated internal AES block decryption function
  284. * without return value.
  285. *
  286. * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
  287. *
  288. * \param ctx AES context
  289. * \param input Ciphertext block
  290. * \param output Output (plaintext) block
  291. */
  292. MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
  293. const unsigned char input[16],
  294. unsigned char output[16] );
  295. #undef MBEDTLS_DEPRECATED
  296. #endif /* !MBEDTLS_DEPRECATED_REMOVED */
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #else /* MBEDTLS_AES_ALT */
  301. #include "aes_alt.h"
  302. #endif /* MBEDTLS_AES_ALT */
  303. #ifdef __cplusplus
  304. extern "C" {
  305. #endif
  306. /**
  307. * \brief Checkup routine
  308. *
  309. * \return 0 if successful, or 1 if the test failed
  310. */
  311. int mbedtls_aes_self_test( int verbose );
  312. #ifdef __cplusplus
  313. }
  314. #endif
  315. #endif /* aes.h */