aes.h 10 KB

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