aes.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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(MBEDTLS_AES_ALT)
  38. // Regular implementation
  39. //
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * \brief AES context structure
  45. *
  46. * \note buf is able to hold 32 extra bytes, which can be used:
  47. * - for alignment purposes if VIA padlock is used, and/or
  48. * - to simplify key expansion in the 256-bit case by
  49. * generating an extra round key
  50. */
  51. typedef struct
  52. {
  53. int nr; /*!< number of rounds */
  54. uint32_t *rk; /*!< AES round keys */
  55. uint32_t buf[68]; /*!< unaligned data */
  56. }
  57. mbedtls_aes_context;
  58. /**
  59. * \brief Initialize AES context
  60. *
  61. * \param ctx AES context to be initialized
  62. */
  63. void mbedtls_aes_init( mbedtls_aes_context *ctx );
  64. /**
  65. * \brief Clear AES context
  66. *
  67. * \param ctx AES context to be cleared
  68. */
  69. void mbedtls_aes_free( mbedtls_aes_context *ctx );
  70. /**
  71. * \brief AES key schedule (encryption)
  72. *
  73. * \param ctx AES context to be initialized
  74. * \param key encryption key
  75. * \param keybits must be 128, 192 or 256
  76. *
  77. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  78. */
  79. int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
  80. unsigned int keybits );
  81. /**
  82. * \brief AES key schedule (decryption)
  83. *
  84. * \param ctx AES context to be initialized
  85. * \param key decryption key
  86. * \param keybits must be 128, 192 or 256
  87. *
  88. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
  89. */
  90. int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
  91. unsigned int keybits );
  92. /**
  93. * \brief AES-ECB block encryption/decryption
  94. *
  95. * \param ctx AES context
  96. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  97. * \param input 16-byte input block
  98. * \param output 16-byte output block
  99. *
  100. * \return 0 if successful
  101. */
  102. int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
  103. int mode,
  104. const unsigned char input[16],
  105. unsigned char output[16] );
  106. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  107. /**
  108. * \brief AES-CBC buffer encryption/decryption
  109. * Length should be a multiple of the block
  110. * size (16 bytes)
  111. *
  112. * \note Upon exit, the content of the IV is updated so that you can
  113. * call the function same function again on the following
  114. * block(s) of data and get the same result as if it was
  115. * encrypted in one call. This allows a "streaming" usage.
  116. * If on the other hand you need to retain the contents of the
  117. * IV, you should either save it manually or use the cipher
  118. * module instead.
  119. *
  120. * \param ctx AES context
  121. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  122. * \param length length of the input data
  123. * \param iv initialization vector (updated after use)
  124. * \param input buffer holding the input data
  125. * \param output buffer holding the output data
  126. *
  127. * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
  128. */
  129. int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
  130. int mode,
  131. size_t length,
  132. unsigned char iv[16],
  133. const unsigned char *input,
  134. unsigned char *output );
  135. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  136. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  137. /**
  138. * \brief AES-CFB128 buffer encryption/decryption.
  139. *
  140. * Note: Due to the nature of CFB you should use the same key schedule for
  141. * both encryption and decryption. So a context initialized with
  142. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  143. *
  144. * \note Upon exit, the content of the IV is updated so that you can
  145. * call the function same function again on the following
  146. * block(s) of data and get the same result as if it was
  147. * encrypted in one call. This allows a "streaming" usage.
  148. * If on the other hand you need to retain the contents of the
  149. * IV, you should either save it manually or use the cipher
  150. * module instead.
  151. *
  152. * \param ctx AES context
  153. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  154. * \param length length of the input data
  155. * \param iv_off offset in IV (updated after use)
  156. * \param iv initialization vector (updated after use)
  157. * \param input buffer holding the input data
  158. * \param output buffer holding the output data
  159. *
  160. * \return 0 if successful
  161. */
  162. int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
  163. int mode,
  164. size_t length,
  165. size_t *iv_off,
  166. unsigned char iv[16],
  167. const unsigned char *input,
  168. unsigned char *output );
  169. /**
  170. * \brief AES-CFB8 buffer encryption/decryption.
  171. *
  172. * Note: Due to the nature of CFB you should use the same key schedule for
  173. * both encryption and decryption. So a context initialized with
  174. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  175. *
  176. * \note Upon exit, the content of the IV is updated so that you can
  177. * call the function same function again on the following
  178. * block(s) of data and get the same result as if it was
  179. * encrypted in one call. This allows a "streaming" usage.
  180. * If on the other hand you need to retain the contents of the
  181. * IV, you should either save it manually or use the cipher
  182. * module instead.
  183. *
  184. * \param ctx AES context
  185. * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
  186. * \param length length of the input data
  187. * \param iv initialization vector (updated after use)
  188. * \param input buffer holding the input data
  189. * \param output buffer holding the output data
  190. *
  191. * \return 0 if successful
  192. */
  193. int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
  194. int mode,
  195. size_t length,
  196. unsigned char iv[16],
  197. const unsigned char *input,
  198. unsigned char *output );
  199. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  200. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  201. /**
  202. * \brief AES-CTR buffer encryption/decryption
  203. *
  204. * Warning: You have to keep the maximum use of your counter in mind!
  205. *
  206. * Note: Due to the nature of CTR you should use the same key schedule for
  207. * both encryption and decryption. So a context initialized with
  208. * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
  209. *
  210. * \param ctx AES context
  211. * \param length The length of the data
  212. * \param nc_off The offset in the current stream_block (for resuming
  213. * within current cipher stream). The offset pointer to
  214. * should be 0 at the start of a stream.
  215. * \param nonce_counter The 128-bit nonce and counter.
  216. * \param stream_block The saved stream-block for resuming. Is overwritten
  217. * by the function.
  218. * \param input The input data stream
  219. * \param output The output data stream
  220. *
  221. * \return 0 if successful
  222. */
  223. int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
  224. size_t length,
  225. size_t *nc_off,
  226. unsigned char nonce_counter[16],
  227. unsigned char stream_block[16],
  228. const unsigned char *input,
  229. unsigned char *output );
  230. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  231. /**
  232. * \brief Internal AES block encryption function
  233. * (Only exposed to allow overriding it,
  234. * see MBEDTLS_AES_ENCRYPT_ALT)
  235. *
  236. * \param ctx AES context
  237. * \param input Plaintext block
  238. * \param output Output (ciphertext) block
  239. */
  240. void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
  241. const unsigned char input[16],
  242. unsigned char output[16] );
  243. /**
  244. * \brief Internal AES block decryption function
  245. * (Only exposed to allow overriding it,
  246. * see MBEDTLS_AES_DECRYPT_ALT)
  247. *
  248. * \param ctx AES context
  249. * \param input Ciphertext block
  250. * \param output Output (plaintext) block
  251. */
  252. void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
  253. const unsigned char input[16],
  254. unsigned char output[16] );
  255. #ifdef __cplusplus
  256. }
  257. #endif
  258. #else /* MBEDTLS_AES_ALT */
  259. #include "aes_alt.h"
  260. #endif /* MBEDTLS_AES_ALT */
  261. #ifdef __cplusplus
  262. extern "C" {
  263. #endif
  264. /**
  265. * \brief Checkup routine
  266. *
  267. * \return 0 if successful, or 1 if the test failed
  268. */
  269. int mbedtls_aes_self_test( int verbose );
  270. #ifdef __cplusplus
  271. }
  272. #endif
  273. #endif /* aes.h */