aes.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631
  1. /**
  2. * \file aes.h
  3. *
  4. * \brief This file contains AES definitions and functions.
  5. *
  6. * The Advanced Encryption Standard (AES) specifies a FIPS-approved
  7. * cryptographic algorithm that can be used to protect electronic
  8. * data.
  9. *
  10. * The AES algorithm is a symmetric block cipher that can
  11. * encrypt and decrypt information. For more information, see
  12. * <em>FIPS Publication 197: Advanced Encryption Standard</em> and
  13. * <em>ISO/IEC 18033-2:2006: Information technology -- Security
  14. * techniques -- Encryption algorithms -- Part 2: Asymmetric
  15. * ciphers</em>.
  16. *
  17. * The AES-XTS block mode is standardized by NIST SP 800-38E
  18. * <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>
  19. * and described in detail by IEEE P1619
  20. * <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.
  21. */
  22. /*
  23. * Copyright The Mbed TLS Contributors
  24. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  25. */
  26. #ifndef MBEDTLS_AES_H
  27. #define MBEDTLS_AES_H
  28. #include "mbedtls/private_access.h"
  29. #include "mbedtls/build_info.h"
  30. #include "mbedtls/platform_util.h"
  31. #include <stddef.h>
  32. #include <stdint.h>
  33. /* padlock.c and aesni.c rely on these values! */
  34. #define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
  35. #define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
  36. /* Error codes in range 0x0020-0x0022 */
  37. /** Invalid key length. */
  38. #define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
  39. /** Invalid data input length. */
  40. #define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
  41. /* Error codes in range 0x0021-0x0025 */
  42. /** Invalid input data. */
  43. #define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. #if !defined(MBEDTLS_AES_ALT)
  48. // Regular implementation
  49. //
  50. /**
  51. * \brief The AES context-type definition.
  52. */
  53. typedef struct mbedtls_aes_context {
  54. int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
  55. size_t MBEDTLS_PRIVATE(rk_offset); /*!< The offset in array elements to AES
  56. round keys in the buffer. */
  57. #if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) && !defined(MBEDTLS_PADLOCK_C)
  58. uint32_t MBEDTLS_PRIVATE(buf)[44]; /*!< Aligned data buffer to hold
  59. 10 round keys for 128-bit case. */
  60. #else
  61. uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can
  62. hold 32 extra Bytes, which can be used for
  63. one of the following purposes:
  64. <ul><li>Alignment if VIA padlock is
  65. used.</li>
  66. <li>Simplifying key expansion in the 256-bit
  67. case by generating an extra round key.
  68. </li></ul> */
  69. #endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH && !MBEDTLS_PADLOCK_C */
  70. }
  71. mbedtls_aes_context;
  72. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  73. /**
  74. * \brief The AES XTS context-type definition.
  75. */
  76. typedef struct mbedtls_aes_xts_context {
  77. mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
  78. encryption or decryption. */
  79. mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
  80. computation. */
  81. } mbedtls_aes_xts_context;
  82. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  83. #else /* MBEDTLS_AES_ALT */
  84. #include "aes_alt.h"
  85. #endif /* MBEDTLS_AES_ALT */
  86. /**
  87. * \brief This function initializes the specified AES context.
  88. *
  89. * It must be the first API called before using
  90. * the context.
  91. *
  92. * \param ctx The AES context to initialize. This must not be \c NULL.
  93. */
  94. void mbedtls_aes_init(mbedtls_aes_context *ctx);
  95. /**
  96. * \brief This function releases and clears the specified AES context.
  97. *
  98. * \param ctx The AES context to clear.
  99. * If this is \c NULL, this function does nothing.
  100. * Otherwise, the context must have been at least initialized.
  101. */
  102. void mbedtls_aes_free(mbedtls_aes_context *ctx);
  103. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  104. /**
  105. * \brief This function initializes the specified AES XTS context.
  106. *
  107. * It must be the first API called before using
  108. * the context.
  109. *
  110. * \param ctx The AES XTS context to initialize. This must not be \c NULL.
  111. */
  112. void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
  113. /**
  114. * \brief This function releases and clears the specified AES XTS context.
  115. *
  116. * \param ctx The AES XTS context to clear.
  117. * If this is \c NULL, this function does nothing.
  118. * Otherwise, the context must have been at least initialized.
  119. */
  120. void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
  121. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  122. /**
  123. * \brief This function sets the encryption key.
  124. *
  125. * \param ctx The AES context to which the key should be bound.
  126. * It must be initialized.
  127. * \param key The encryption key.
  128. * This must be a readable buffer of size \p keybits bits.
  129. * \param keybits The size of data passed in bits. Valid options are:
  130. * <ul><li>128 bits</li>
  131. * <li>192 bits</li>
  132. * <li>256 bits</li></ul>
  133. *
  134. * \return \c 0 on success.
  135. * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  136. */
  137. MBEDTLS_CHECK_RETURN_TYPICAL
  138. int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
  139. unsigned int keybits);
  140. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  141. /**
  142. * \brief This function sets the decryption key.
  143. *
  144. * \param ctx The AES context to which the key should be bound.
  145. * It must be initialized.
  146. * \param key The decryption key.
  147. * This must be a readable buffer of size \p keybits bits.
  148. * \param keybits The size of data passed. Valid options are:
  149. * <ul><li>128 bits</li>
  150. * <li>192 bits</li>
  151. * <li>256 bits</li></ul>
  152. *
  153. * \return \c 0 on success.
  154. * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  155. */
  156. MBEDTLS_CHECK_RETURN_TYPICAL
  157. int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
  158. unsigned int keybits);
  159. #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
  160. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  161. /**
  162. * \brief This function prepares an XTS context for encryption and
  163. * sets the encryption key.
  164. *
  165. * \param ctx The AES XTS context to which the key should be bound.
  166. * It must be initialized.
  167. * \param key The encryption key. This is comprised of the XTS key1
  168. * concatenated with the XTS key2.
  169. * This must be a readable buffer of size \p keybits bits.
  170. * \param keybits The size of \p key passed in bits. Valid options are:
  171. * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
  172. * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
  173. *
  174. * \return \c 0 on success.
  175. * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  176. */
  177. MBEDTLS_CHECK_RETURN_TYPICAL
  178. int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
  179. const unsigned char *key,
  180. unsigned int keybits);
  181. /**
  182. * \brief This function prepares an XTS context for decryption and
  183. * sets the decryption key.
  184. *
  185. * \param ctx The AES XTS context to which the key should be bound.
  186. * It must be initialized.
  187. * \param key The decryption key. This is comprised of the XTS key1
  188. * concatenated with the XTS key2.
  189. * This must be a readable buffer of size \p keybits bits.
  190. * \param keybits The size of \p key passed in bits. Valid options are:
  191. * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
  192. * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
  193. *
  194. * \return \c 0 on success.
  195. * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
  196. */
  197. MBEDTLS_CHECK_RETURN_TYPICAL
  198. int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
  199. const unsigned char *key,
  200. unsigned int keybits);
  201. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  202. /**
  203. * \brief This function performs an AES single-block encryption or
  204. * decryption operation.
  205. *
  206. * It performs the operation defined in the \p mode parameter
  207. * (encrypt or decrypt), on the input data buffer defined in
  208. * the \p input parameter.
  209. *
  210. * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
  211. * mbedtls_aes_setkey_dec() must be called before the first
  212. * call to this API with the same context.
  213. *
  214. * \param ctx The AES context to use for encryption or decryption.
  215. * It must be initialized and bound to a key.
  216. * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
  217. * #MBEDTLS_AES_DECRYPT.
  218. * \param input The buffer holding the input data.
  219. * It must be readable and at least \c 16 Bytes long.
  220. * \param output The buffer where the output data will be written.
  221. * It must be writeable and at least \c 16 Bytes long.
  222. * \return \c 0 on success.
  223. */
  224. MBEDTLS_CHECK_RETURN_TYPICAL
  225. int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
  226. int mode,
  227. const unsigned char input[16],
  228. unsigned char output[16]);
  229. #if defined(MBEDTLS_CIPHER_MODE_CBC)
  230. /**
  231. * \brief This function performs an AES-CBC encryption or decryption operation
  232. * on full blocks.
  233. *
  234. * It performs the operation defined in the \p mode
  235. * parameter (encrypt/decrypt), on the input data buffer defined in
  236. * the \p input parameter.
  237. *
  238. * It can be called as many times as needed, until all the input
  239. * data is processed. mbedtls_aes_init(), and either
  240. * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
  241. * before the first call to this API with the same context.
  242. *
  243. * \note This function operates on full blocks, that is, the input size
  244. * must be a multiple of the AES block size of \c 16 Bytes.
  245. *
  246. * \note Upon exit, the content of the IV is updated so that you can
  247. * call the same function again on the next
  248. * block(s) of data and get the same result as if it was
  249. * encrypted in one call. This allows a "streaming" usage.
  250. * If you need to retain the contents of the IV, you should
  251. * either save it manually or use the cipher module instead.
  252. *
  253. *
  254. * \param ctx The AES context to use for encryption or decryption.
  255. * It must be initialized and bound to a key.
  256. * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
  257. * #MBEDTLS_AES_DECRYPT.
  258. * \param length The length of the input data in Bytes. This must be a
  259. * multiple of the block size (\c 16 Bytes).
  260. * \param iv Initialization vector (updated after use).
  261. * It must be a readable and writeable buffer of \c 16 Bytes.
  262. * \param input The buffer holding the input data.
  263. * It must be readable and of size \p length Bytes.
  264. * \param output The buffer holding the output data.
  265. * It must be writeable and of size \p length Bytes.
  266. *
  267. * \return \c 0 on success.
  268. * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
  269. * on failure.
  270. */
  271. MBEDTLS_CHECK_RETURN_TYPICAL
  272. int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
  273. int mode,
  274. size_t length,
  275. unsigned char iv[16],
  276. const unsigned char *input,
  277. unsigned char *output);
  278. #endif /* MBEDTLS_CIPHER_MODE_CBC */
  279. #if defined(MBEDTLS_CIPHER_MODE_XTS)
  280. /**
  281. * \brief This function performs an AES-XTS encryption or decryption
  282. * operation for an entire XTS data unit.
  283. *
  284. * AES-XTS encrypts or decrypts blocks based on their location as
  285. * defined by a data unit number. The data unit number must be
  286. * provided by \p data_unit.
  287. *
  288. * NIST SP 800-38E limits the maximum size of a data unit to 2^20
  289. * AES blocks. If the data unit is larger than this, this function
  290. * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
  291. *
  292. * \param ctx The AES XTS context to use for AES XTS operations.
  293. * It must be initialized and bound to a key.
  294. * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
  295. * #MBEDTLS_AES_DECRYPT.
  296. * \param length The length of a data unit in Bytes. This can be any
  297. * length between 16 bytes and 2^24 bytes inclusive
  298. * (between 1 and 2^20 block cipher blocks).
  299. * \param data_unit The address of the data unit encoded as an array of 16
  300. * bytes in little-endian format. For disk encryption, this
  301. * is typically the index of the block device sector that
  302. * contains the data.
  303. * \param input The buffer holding the input data (which is an entire
  304. * data unit). This function reads \p length Bytes from \p
  305. * input.
  306. * \param output The buffer holding the output data (which is an entire
  307. * data unit). This function writes \p length Bytes to \p
  308. * output.
  309. *
  310. * \return \c 0 on success.
  311. * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
  312. * smaller than an AES block in size (16 Bytes) or if \p
  313. * length is larger than 2^20 blocks (16 MiB).
  314. */
  315. MBEDTLS_CHECK_RETURN_TYPICAL
  316. int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
  317. int mode,
  318. size_t length,
  319. const unsigned char data_unit[16],
  320. const unsigned char *input,
  321. unsigned char *output);
  322. #endif /* MBEDTLS_CIPHER_MODE_XTS */
  323. #if defined(MBEDTLS_CIPHER_MODE_CFB)
  324. /**
  325. * \brief This function performs an AES-CFB128 encryption or decryption
  326. * operation.
  327. *
  328. * It performs the operation defined in the \p mode
  329. * parameter (encrypt or decrypt), on the input data buffer
  330. * defined in the \p input parameter.
  331. *
  332. * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
  333. * regardless of whether you are performing an encryption or decryption
  334. * operation, that is, regardless of the \p mode parameter. This is
  335. * because CFB mode uses the same key schedule for encryption and
  336. * decryption.
  337. *
  338. * \note Upon exit, the content of the IV is updated so that you can
  339. * call the same function again on the next
  340. * block(s) of data and get the same result as if it was
  341. * encrypted in one call. This allows a "streaming" usage.
  342. * If you need to retain the contents of the
  343. * IV, you must either save it manually or use the cipher
  344. * module instead.
  345. *
  346. *
  347. * \param ctx The AES context to use for encryption or decryption.
  348. * It must be initialized and bound to a key.
  349. * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
  350. * #MBEDTLS_AES_DECRYPT.
  351. * \param length The length of the input data in Bytes.
  352. * \param iv_off The offset in IV (updated after use).
  353. * It must point to a valid \c size_t.
  354. * \param iv The initialization vector (updated after use).
  355. * It must be a readable and writeable buffer of \c 16 Bytes.
  356. * \param input The buffer holding the input data.
  357. * It must be readable and of size \p length Bytes.
  358. * \param output The buffer holding the output data.
  359. * It must be writeable and of size \p length Bytes.
  360. *
  361. * \return \c 0 on success.
  362. */
  363. MBEDTLS_CHECK_RETURN_TYPICAL
  364. int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
  365. int mode,
  366. size_t length,
  367. size_t *iv_off,
  368. unsigned char iv[16],
  369. const unsigned char *input,
  370. unsigned char *output);
  371. /**
  372. * \brief This function performs an AES-CFB8 encryption or decryption
  373. * operation.
  374. *
  375. * It performs the operation defined in the \p mode
  376. * parameter (encrypt/decrypt), on the input data buffer defined
  377. * in the \p input parameter.
  378. *
  379. * Due to the nature of CFB, you must use the same key schedule for
  380. * both encryption and decryption operations. Therefore, you must
  381. * use the context initialized with mbedtls_aes_setkey_enc() for
  382. * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
  383. *
  384. * \note Upon exit, the content of the IV is updated so that you can
  385. * call the same function again on the next
  386. * block(s) of data and get the same result as if it was
  387. * encrypted in one call. This allows a "streaming" usage.
  388. * If you need to retain the contents of the
  389. * IV, you should either save it manually or use the cipher
  390. * module instead.
  391. *
  392. *
  393. * \param ctx The AES context to use for encryption or decryption.
  394. * It must be initialized and bound to a key.
  395. * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
  396. * #MBEDTLS_AES_DECRYPT
  397. * \param length The length of the input data.
  398. * \param iv The initialization vector (updated after use).
  399. * It must be a readable and writeable buffer of \c 16 Bytes.
  400. * \param input The buffer holding the input data.
  401. * It must be readable and of size \p length Bytes.
  402. * \param output The buffer holding the output data.
  403. * It must be writeable and of size \p length Bytes.
  404. *
  405. * \return \c 0 on success.
  406. */
  407. MBEDTLS_CHECK_RETURN_TYPICAL
  408. int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
  409. int mode,
  410. size_t length,
  411. unsigned char iv[16],
  412. const unsigned char *input,
  413. unsigned char *output);
  414. #endif /*MBEDTLS_CIPHER_MODE_CFB */
  415. #if defined(MBEDTLS_CIPHER_MODE_OFB)
  416. /**
  417. * \brief This function performs an AES-OFB (Output Feedback Mode)
  418. * encryption or decryption operation.
  419. *
  420. * For OFB, you must set up the context with
  421. * mbedtls_aes_setkey_enc(), regardless of whether you are
  422. * performing an encryption or decryption operation. This is
  423. * because OFB mode uses the same key schedule for encryption and
  424. * decryption.
  425. *
  426. * The OFB operation is identical for encryption or decryption,
  427. * therefore no operation mode needs to be specified.
  428. *
  429. * \note Upon exit, the content of iv, the Initialisation Vector, is
  430. * updated so that you can call the same function again on the next
  431. * block(s) of data and get the same result as if it was encrypted
  432. * in one call. This allows a "streaming" usage, by initialising
  433. * iv_off to 0 before the first call, and preserving its value
  434. * between calls.
  435. *
  436. * For non-streaming use, the iv should be initialised on each call
  437. * to a unique value, and iv_off set to 0 on each call.
  438. *
  439. * If you need to retain the contents of the initialisation vector,
  440. * you must either save it manually or use the cipher module
  441. * instead.
  442. *
  443. * \warning For the OFB mode, the initialisation vector must be unique
  444. * every encryption operation. Reuse of an initialisation vector
  445. * will compromise security.
  446. *
  447. * \param ctx The AES context to use for encryption or decryption.
  448. * It must be initialized and bound to a key.
  449. * \param length The length of the input data.
  450. * \param iv_off The offset in IV (updated after use).
  451. * It must point to a valid \c size_t.
  452. * \param iv The initialization vector (updated after use).
  453. * It must be a readable and writeable buffer of \c 16 Bytes.
  454. * \param input The buffer holding the input data.
  455. * It must be readable and of size \p length Bytes.
  456. * \param output The buffer holding the output data.
  457. * It must be writeable and of size \p length Bytes.
  458. *
  459. * \return \c 0 on success.
  460. */
  461. MBEDTLS_CHECK_RETURN_TYPICAL
  462. int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
  463. size_t length,
  464. size_t *iv_off,
  465. unsigned char iv[16],
  466. const unsigned char *input,
  467. unsigned char *output);
  468. #endif /* MBEDTLS_CIPHER_MODE_OFB */
  469. #if defined(MBEDTLS_CIPHER_MODE_CTR)
  470. /**
  471. * \brief This function performs an AES-CTR encryption or decryption
  472. * operation.
  473. *
  474. * Due to the nature of CTR, you must use the same key schedule
  475. * for both encryption and decryption operations. Therefore, you
  476. * must use the context initialized with mbedtls_aes_setkey_enc()
  477. * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
  478. *
  479. * \warning You must never reuse a nonce value with the same key. Doing so
  480. * would void the encryption for the two messages encrypted with
  481. * the same nonce and key.
  482. *
  483. * There are two common strategies for managing nonces with CTR:
  484. *
  485. * 1. You can handle everything as a single message processed over
  486. * successive calls to this function. In that case, you want to
  487. * set \p nonce_counter and \p nc_off to 0 for the first call, and
  488. * then preserve the values of \p nonce_counter, \p nc_off and \p
  489. * stream_block across calls to this function as they will be
  490. * updated by this function.
  491. *
  492. * With this strategy, you must not encrypt more than 2**128
  493. * blocks of data with the same key.
  494. *
  495. * 2. You can encrypt separate messages by dividing the \p
  496. * nonce_counter buffer in two areas: the first one used for a
  497. * per-message nonce, handled by yourself, and the second one
  498. * updated by this function internally.
  499. *
  500. * For example, you might reserve the first 12 bytes for the
  501. * per-message nonce, and the last 4 bytes for internal use. In that
  502. * case, before calling this function on a new message you need to
  503. * set the first 12 bytes of \p nonce_counter to your chosen nonce
  504. * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
  505. * stream_block to be ignored). That way, you can encrypt at most
  506. * 2**96 messages of up to 2**32 blocks each with the same key.
  507. *
  508. * The per-message nonce (or information sufficient to reconstruct
  509. * it) needs to be communicated with the ciphertext and must be unique.
  510. * The recommended way to ensure uniqueness is to use a message
  511. * counter. An alternative is to generate random nonces, but this
  512. * limits the number of messages that can be securely encrypted:
  513. * for example, with 96-bit random nonces, you should not encrypt
  514. * more than 2**32 messages with the same key.
  515. *
  516. * Note that for both strategies, sizes are measured in blocks and
  517. * that an AES block is 16 bytes.
  518. *
  519. * \warning Upon return, \p stream_block contains sensitive data. Its
  520. * content must not be written to insecure storage and should be
  521. * securely discarded as soon as it's no longer needed.
  522. *
  523. * \param ctx The AES context to use for encryption or decryption.
  524. * It must be initialized and bound to a key.
  525. * \param length The length of the input data.
  526. * \param nc_off The offset in the current \p stream_block, for
  527. * resuming within the current cipher stream. The
  528. * offset pointer should be 0 at the start of a stream.
  529. * It must point to a valid \c size_t.
  530. * \param nonce_counter The 128-bit nonce and counter.
  531. * It must be a readable-writeable buffer of \c 16 Bytes.
  532. * \param stream_block The saved stream block for resuming. This is
  533. * overwritten by the function.
  534. * It must be a readable-writeable buffer of \c 16 Bytes.
  535. * \param input The buffer holding the input data.
  536. * It must be readable and of size \p length Bytes.
  537. * \param output The buffer holding the output data.
  538. * It must be writeable and of size \p length Bytes.
  539. *
  540. * \return \c 0 on success.
  541. */
  542. MBEDTLS_CHECK_RETURN_TYPICAL
  543. int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
  544. size_t length,
  545. size_t *nc_off,
  546. unsigned char nonce_counter[16],
  547. unsigned char stream_block[16],
  548. const unsigned char *input,
  549. unsigned char *output);
  550. #endif /* MBEDTLS_CIPHER_MODE_CTR */
  551. /**
  552. * \brief Internal AES block encryption function. This is only
  553. * exposed to allow overriding it using
  554. * \c MBEDTLS_AES_ENCRYPT_ALT.
  555. *
  556. * \param ctx The AES context to use for encryption.
  557. * \param input The plaintext block.
  558. * \param output The output (ciphertext) block.
  559. *
  560. * \return \c 0 on success.
  561. */
  562. MBEDTLS_CHECK_RETURN_TYPICAL
  563. int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
  564. const unsigned char input[16],
  565. unsigned char output[16]);
  566. #if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
  567. /**
  568. * \brief Internal AES block decryption function. This is only
  569. * exposed to allow overriding it using see
  570. * \c MBEDTLS_AES_DECRYPT_ALT.
  571. *
  572. * \param ctx The AES context to use for decryption.
  573. * \param input The ciphertext block.
  574. * \param output The output (plaintext) block.
  575. *
  576. * \return \c 0 on success.
  577. */
  578. MBEDTLS_CHECK_RETURN_TYPICAL
  579. int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
  580. const unsigned char input[16],
  581. unsigned char output[16]);
  582. #endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
  583. #if defined(MBEDTLS_SELF_TEST)
  584. /**
  585. * \brief Checkup routine.
  586. *
  587. * \return \c 0 on success.
  588. * \return \c 1 on failure.
  589. */
  590. MBEDTLS_CHECK_RETURN_CRITICAL
  591. int mbedtls_aes_self_test(int verbose);
  592. #endif /* MBEDTLS_SELF_TEST */
  593. #ifdef __cplusplus
  594. }
  595. #endif
  596. #endif /* aes.h */