blob: d5eb1fd5c2c141dceea437c56a08e909eabdfa06 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +01004 * \brief This file contains AES definitions and functions.
5 *
6 * The Advanced Encryption Standard (AES) specifies a FIPS-approved
Rose Zadik7f441272018-01-22 11:48:23 +00007 * 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>.
Jaeden Amerof167deb2018-05-30 19:20:48 +010016 *
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>.
Darryl Greena40a1012018-01-05 15:33:17 +000021 */
Rose Zadik5ad7aea2018-03-26 12:00:09 +010022
Bence Szépkúti86974652020-06-15 11:59:37 +020023/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020024 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +000025 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Rose Zadik7f441272018-01-22 11:48:23 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#ifndef MBEDTLS_AES_H
29#define MBEDTLS_AES_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020030#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000031
Bence Szépkútic662b362021-05-27 11:25:03 +020032#include "mbedtls/build_info.h"
Mateusz Starzyke35f8f62021-08-04 15:38:09 +020033#include "mbedtls/platform_util.h"
Paul Bakker90995b52013-06-24 19:20:35 +020034
Rich Evans00ab4702015-02-06 13:43:58 +000035#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020036#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000037
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010038/* padlock.c and aesni.c rely on these values! */
Rose Zadik7f441272018-01-22 11:48:23 +000039#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
40#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
Paul Bakker5121ce52009-01-03 21:22:43 +000041
Andres Amaya Garciac5380642017-11-28 19:57:51 +000042/* Error codes in range 0x0020-0x0022 */
Gilles Peskined2971572021-07-26 18:48:10 +020043/** Invalid key length. */
44#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
45/** Invalid data input length. */
46#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
Paul Bakker2b222c82009-07-27 21:03:45 +000047
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000048/* Error codes in range 0x0021-0x0025 */
Gilles Peskined2971572021-07-26 18:48:10 +020049/** Invalid input data. */
50#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
Ron Eldor9924bdc2018-10-04 10:59:13 +030051
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Ron Eldorb2aacec2017-05-18 16:53:08 +030056#if !defined(MBEDTLS_AES_ALT)
57// Regular implementation
58//
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
Rose Zadik7f441272018-01-22 11:48:23 +000061 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
Gilles Peskine449bd832023-01-11 14:50:10 +010063typedef struct mbedtls_aes_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020064 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
Werner Lewis6d719442022-06-13 12:28:07 +010065 size_t MBEDTLS_PRIVATE(rk_offset); /*!< The offset in array elements to AES
Gilles Peskine449bd832023-01-11 14:50:10 +010066 round keys in the buffer. */
Arto Kinnunenb1c626b2023-04-14 17:21:22 +080067#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) && !defined(MBEDTLS_PADLOCK_C)
Yanray Wang8b9877b2023-05-05 14:46:04 +080068 uint32_t MBEDTLS_PRIVATE(buf)[44]; /*!< Aligned data buffer to hold
Yanray Wangab4fb0d2023-05-10 10:06:11 +080069 10 round keys for 128-bit case. */
Arto Kinnunenb1c626b2023-04-14 17:21:22 +080070#else
Mateusz Starzyk846f0212021-05-19 19:44:07 +020071 uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can
Gilles Peskine449bd832023-01-11 14:50:10 +010072 hold 32 extra Bytes, which can be used for
73 one of the following purposes:
74 <ul><li>Alignment if VIA padlock is
75 used.</li>
76 <li>Simplifying key expansion in the 256-bit
77 case by generating an extra round key.
78 </li></ul> */
Arto Kinnunenb1c626b2023-04-14 17:21:22 +080079#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH && !MBEDTLS_PADLOCK_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000080}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000082
Jaeden Amero9366feb2018-05-29 18:55:17 +010083#if defined(MBEDTLS_CIPHER_MODE_XTS)
84/**
85 * \brief The AES XTS context-type definition.
86 */
Gilles Peskine449bd832023-01-11 14:50:10 +010087typedef struct mbedtls_aes_xts_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020088 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Gilles Peskine449bd832023-01-11 14:50:10 +010089 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020090 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Gilles Peskine449bd832023-01-11 14:50:10 +010091 computation. */
Jaeden Amero9366feb2018-05-29 18:55:17 +010092} mbedtls_aes_xts_context;
93#endif /* MBEDTLS_CIPHER_MODE_XTS */
94
Ron Eldorb2aacec2017-05-18 16:53:08 +030095#else /* MBEDTLS_AES_ALT */
96#include "aes_alt.h"
97#endif /* MBEDTLS_AES_ALT */
98
Paul Bakker5121ce52009-01-03 21:22:43 +000099/**
Rose Zadik7f441272018-01-22 11:48:23 +0000100 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200101 *
Rose Zadik7f441272018-01-22 11:48:23 +0000102 * It must be the first API called before using
103 * the context.
104 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200106 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100107void mbedtls_aes_init(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200108
109/**
Rose Zadik7f441272018-01-22 11:48:23 +0000110 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200111 *
Rose Zadik7f441272018-01-22 11:48:23 +0000112 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * If this is \c NULL, this function does nothing.
114 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200115 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100116void mbedtls_aes_free(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200117
Jaeden Amero9366feb2018-05-29 18:55:17 +0100118#if defined(MBEDTLS_CIPHER_MODE_XTS)
119/**
120 * \brief This function initializes the specified AES XTS context.
121 *
122 * It must be the first API called before using
123 * the context.
124 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100128
129/**
130 * \brief This function releases and clears the specified AES XTS context.
131 *
132 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * If this is \c NULL, this function does nothing.
134 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100135 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100136void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100137#endif /* MBEDTLS_CIPHER_MODE_XTS */
138
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200139/**
Rose Zadik7f441272018-01-22 11:48:23 +0000140 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 *
Rose Zadik7f441272018-01-22 11:48:23 +0000142 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500143 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000144 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500145 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000146 * \param keybits The size of data passed in bits. Valid options are:
147 * <ul><li>128 bits</li>
148 * <li>192 bits</li>
149 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000150 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100151 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100152 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200154MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100155int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
156 unsigned int keybits);
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Yanray Wangb67b4742023-10-31 17:10:32 +0800158#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000159/**
Rose Zadik7f441272018-01-22 11:48:23 +0000160 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 *
Rose Zadik7f441272018-01-22 11:48:23 +0000162 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000164 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500165 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000166 * \param keybits The size of data passed. Valid options are:
167 * <ul><li>128 bits</li>
168 * <li>192 bits</li>
169 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000170 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100171 * \return \c 0 on success.
172 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200174MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100175int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
176 unsigned int keybits);
Yanray Wangb67b4742023-10-31 17:10:32 +0800177#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000178
Jaeden Amero9366feb2018-05-29 18:55:17 +0100179#if defined(MBEDTLS_CIPHER_MODE_XTS)
180/**
181 * \brief This function prepares an XTS context for encryption and
182 * sets the encryption key.
183 *
184 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500185 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100186 * \param key The encryption key. This is comprised of the XTS key1
187 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500188 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100189 * \param keybits The size of \p key passed in bits. Valid options are:
190 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
191 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
192 *
193 * \return \c 0 on success.
194 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
195 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200196MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100197int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
198 const unsigned char *key,
199 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100200
201/**
202 * \brief This function prepares an XTS context for decryption and
203 * sets the decryption key.
204 *
205 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500206 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100207 * \param key The decryption key. This is comprised of the XTS key1
208 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500209 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100210 * \param keybits The size of \p key passed in bits. Valid options are:
211 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
212 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
213 *
214 * \return \c 0 on success.
215 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
216 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200217MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100218int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
219 const unsigned char *key,
220 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100221#endif /* MBEDTLS_CIPHER_MODE_XTS */
222
Paul Bakker5121ce52009-01-03 21:22:43 +0000223/**
Rose Zadik7f441272018-01-22 11:48:23 +0000224 * \brief This function performs an AES single-block encryption or
225 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000226 *
Rose Zadik7f441272018-01-22 11:48:23 +0000227 * It performs the operation defined in the \p mode parameter
228 * (encrypt or decrypt), on the input data buffer defined in
229 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000230 *
Rose Zadik7f441272018-01-22 11:48:23 +0000231 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
232 * mbedtls_aes_setkey_dec() must be called before the first
233 * call to this API with the same context.
234 *
235 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500236 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000237 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
238 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500239 * \param input The buffer holding the input data.
240 * It must be readable and at least \c 16 Bytes long.
241 * \param output The buffer where the output data will be written.
242 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000243
244 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200246MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100247int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
248 int mode,
249 const unsigned char input[16],
250 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000253/**
Rose Zadik7f441272018-01-22 11:48:23 +0000254 * \brief This function performs an AES-CBC encryption or decryption operation
255 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 *
Rose Zadik7f441272018-01-22 11:48:23 +0000257 * It performs the operation defined in the \p mode
258 * parameter (encrypt/decrypt), on the input data buffer defined in
259 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000260 *
Rose Zadik7f441272018-01-22 11:48:23 +0000261 * It can be called as many times as needed, until all the input
262 * data is processed. mbedtls_aes_init(), and either
263 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
264 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000265 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 * \note This function operates on full blocks, that is, the input size
267 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000268 *
269 * \note Upon exit, the content of the IV is updated so that you can
270 * call the same function again on the next
271 * block(s) of data and get the same result as if it was
272 * encrypted in one call. This allows a "streaming" usage.
273 * If you need to retain the contents of the IV, you should
274 * either save it manually or use the cipher module instead.
275 *
276 *
277 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000279 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
280 * #MBEDTLS_AES_DECRYPT.
281 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000283 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000285 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000287 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000289 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100290 * \return \c 0 on success.
291 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000292 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000293 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200294MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100295int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
296 int mode,
297 size_t length,
298 unsigned char iv[16],
299 const unsigned char *input,
300 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Aorimn5f778012016-06-09 23:22:58 +0200303#if defined(MBEDTLS_CIPHER_MODE_XTS)
304/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100305 * \brief This function performs an AES-XTS encryption or decryption
306 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200307 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100308 * AES-XTS encrypts or decrypts blocks based on their location as
309 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100310 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200311 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100312 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
313 * AES blocks. If the data unit is larger than this, this function
314 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
315 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100316 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500317 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100318 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
319 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100321 * length between 16 bytes and 2^24 bytes inclusive
322 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100323 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100324 * bytes in little-endian format. For disk encryption, this
325 * is typically the index of the block device sector that
326 * contains the data.
327 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500328 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100329 * input.
330 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500331 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100332 * output.
Aorimn5f778012016-06-09 23:22:58 +0200333 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100334 * \return \c 0 on success.
335 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500336 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100337 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200338 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200339MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100340int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
341 int mode,
342 size_t length,
343 const unsigned char data_unit[16],
344 const unsigned char *input,
345 unsigned char *output);
Aorimn5f778012016-06-09 23:22:58 +0200346#endif /* MBEDTLS_CIPHER_MODE_XTS */
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000349/**
Rose Zadik7f441272018-01-22 11:48:23 +0000350 * \brief This function performs an AES-CFB128 encryption or decryption
351 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 *
Rose Zadik7f441272018-01-22 11:48:23 +0000353 * It performs the operation defined in the \p mode
354 * parameter (encrypt or decrypt), on the input data buffer
355 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000356 *
Rose Zadik7f441272018-01-22 11:48:23 +0000357 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
358 * regardless of whether you are performing an encryption or decryption
359 * operation, that is, regardless of the \p mode parameter. This is
360 * because CFB mode uses the same key schedule for encryption and
361 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000362 *
Rose Zadik7f441272018-01-22 11:48:23 +0000363 * \note Upon exit, the content of the IV is updated so that you can
364 * call the same function again on the next
365 * block(s) of data and get the same result as if it was
366 * encrypted in one call. This allows a "streaming" usage.
367 * If you need to retain the contents of the
368 * IV, you must either save it manually or use the cipher
369 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000370 *
Rose Zadik7f441272018-01-22 11:48:23 +0000371 *
372 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500373 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000374 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
375 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000377 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000379 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000383 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000385 *
386 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200388MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100389int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
390 int mode,
391 size_t length,
392 size_t *iv_off,
393 unsigned char iv[16],
394 const unsigned char *input,
395 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000396
Paul Bakker9a736322012-11-14 12:39:52 +0000397/**
Rose Zadik7f441272018-01-22 11:48:23 +0000398 * \brief This function performs an AES-CFB8 encryption or decryption
399 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100400 *
Rose Zadik7f441272018-01-22 11:48:23 +0000401 * It performs the operation defined in the \p mode
402 * parameter (encrypt/decrypt), on the input data buffer defined
403 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100404 *
Rose Zadik7f441272018-01-22 11:48:23 +0000405 * Due to the nature of CFB, you must use the same key schedule for
406 * both encryption and decryption operations. Therefore, you must
407 * use the context initialized with mbedtls_aes_setkey_enc() for
408 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000409 *
Rose Zadik7f441272018-01-22 11:48:23 +0000410 * \note Upon exit, the content of the IV is updated so that you can
411 * call the same function again on the next
412 * block(s) of data and get the same result as if it was
413 * encrypted in one call. This allows a "streaming" usage.
414 * If you need to retain the contents of the
415 * IV, you should either save it manually or use the cipher
416 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100417 *
Rose Zadik7f441272018-01-22 11:48:23 +0000418 *
419 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500420 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000421 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
422 * #MBEDTLS_AES_DECRYPT
423 * \param length The length of the input data.
424 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500425 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000426 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000428 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000430 *
431 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100432 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200433MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100434int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
435 int mode,
436 size_t length,
437 unsigned char iv[16],
438 const unsigned char *input,
439 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100441
Simon Butcher76a5b222018-04-22 22:57:27 +0100442#if defined(MBEDTLS_CIPHER_MODE_OFB)
443/**
Simon Butcher5db13622018-06-04 22:11:25 +0100444 * \brief This function performs an AES-OFB (Output Feedback Mode)
445 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100446 *
Simon Butcher5db13622018-06-04 22:11:25 +0100447 * For OFB, you must set up the context with
448 * mbedtls_aes_setkey_enc(), regardless of whether you are
449 * performing an encryption or decryption operation. This is
450 * because OFB mode uses the same key schedule for encryption and
451 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100452 *
Simon Butcher5db13622018-06-04 22:11:25 +0100453 * The OFB operation is identical for encryption or decryption,
454 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100455 *
Simon Butcher5db13622018-06-04 22:11:25 +0100456 * \note Upon exit, the content of iv, the Initialisation Vector, is
457 * updated so that you can call the same function again on the next
458 * block(s) of data and get the same result as if it was encrypted
459 * in one call. This allows a "streaming" usage, by initialising
460 * iv_off to 0 before the first call, and preserving its value
461 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100462 *
Simon Butcher5db13622018-06-04 22:11:25 +0100463 * For non-streaming use, the iv should be initialised on each call
464 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100465 *
Simon Butcher5db13622018-06-04 22:11:25 +0100466 * If you need to retain the contents of the initialisation vector,
467 * you must either save it manually or use the cipher module
468 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100469 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100470 * \warning For the OFB mode, the initialisation vector must be unique
471 * every encryption operation. Reuse of an initialisation vector
472 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100473 *
474 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500475 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100476 * \param length The length of the input data.
477 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100479 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100481 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100485 *
486 * \return \c 0 on success.
487 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200488MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100489int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
490 size_t length,
491 size_t *iv_off,
492 unsigned char iv[16],
493 const unsigned char *input,
494 unsigned char *output);
Simon Butcher76a5b222018-04-22 22:57:27 +0100495
496#endif /* MBEDTLS_CIPHER_MODE_OFB */
497
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100499/**
Rose Zadik7f441272018-01-22 11:48:23 +0000500 * \brief This function performs an AES-CTR encryption or decryption
501 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000502 *
Rose Zadik7f441272018-01-22 11:48:23 +0000503 * Due to the nature of CTR, you must use the same key schedule
504 * for both encryption and decryption operations. Therefore, you
505 * must use the context initialized with mbedtls_aes_setkey_enc()
506 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000507 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100508 * \warning You must never reuse a nonce value with the same key. Doing so
509 * would void the encryption for the two messages encrypted with
510 * the same nonce and key.
511 *
512 * There are two common strategies for managing nonces with CTR:
513 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200514 * 1. You can handle everything as a single message processed over
515 * successive calls to this function. In that case, you want to
516 * set \p nonce_counter and \p nc_off to 0 for the first call, and
517 * then preserve the values of \p nonce_counter, \p nc_off and \p
518 * stream_block across calls to this function as they will be
519 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100520 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200521 * With this strategy, you must not encrypt more than 2**128
522 * blocks of data with the same key.
523 *
524 * 2. You can encrypt separate messages by dividing the \p
525 * nonce_counter buffer in two areas: the first one used for a
526 * per-message nonce, handled by yourself, and the second one
527 * updated by this function internally.
528 *
529 * For example, you might reserve the first 12 bytes for the
530 * per-message nonce, and the last 4 bytes for internal use. In that
531 * case, before calling this function on a new message you need to
532 * set the first 12 bytes of \p nonce_counter to your chosen nonce
533 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
534 * stream_block to be ignored). That way, you can encrypt at most
535 * 2**96 messages of up to 2**32 blocks each with the same key.
536 *
537 * The per-message nonce (or information sufficient to reconstruct
538 * it) needs to be communicated with the ciphertext and must be unique.
539 * The recommended way to ensure uniqueness is to use a message
540 * counter. An alternative is to generate random nonces, but this
541 * limits the number of messages that can be securely encrypted:
542 * for example, with 96-bit random nonces, you should not encrypt
543 * more than 2**32 messages with the same key.
544 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100545 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200546 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000547 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200548 * \warning Upon return, \p stream_block contains sensitive data. Its
549 * content must not be written to insecure storage and should be
550 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000551 *
Rose Zadik7f441272018-01-22 11:48:23 +0000552 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000554 * \param length The length of the input data.
555 * \param nc_off The offset in the current \p stream_block, for
556 * resuming within the current cipher stream. The
557 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500558 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000559 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500560 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000561 * \param stream_block The saved stream block for resuming. This is
562 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000564 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000566 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000568 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100569 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000570 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200571MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100572int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
573 size_t length,
574 size_t *nc_off,
575 unsigned char nonce_counter[16],
576 unsigned char stream_block[16],
577 const unsigned char *input,
578 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200580
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200581/**
Rose Zadik7f441272018-01-22 11:48:23 +0000582 * \brief Internal AES block encryption function. This is only
583 * exposed to allow overriding it using
584 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200585 *
Rose Zadik7f441272018-01-22 11:48:23 +0000586 * \param ctx The AES context to use for encryption.
587 * \param input The plaintext block.
588 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000589 *
Rose Zadik7f441272018-01-22 11:48:23 +0000590 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200591 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200592MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100593int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
594 const unsigned char input[16],
595 unsigned char output[16]);
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200596
Yanray Wangb67b4742023-10-31 17:10:32 +0800597#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200598/**
Rose Zadik7f441272018-01-22 11:48:23 +0000599 * \brief Internal AES block decryption function. This is only
600 * exposed to allow overriding it using see
601 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200602 *
Rose Zadik7f441272018-01-22 11:48:23 +0000603 * \param ctx The AES context to use for decryption.
604 * \param input The ciphertext block.
605 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000606 *
Rose Zadik7f441272018-01-22 11:48:23 +0000607 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200608 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200609MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100610int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
611 const unsigned char input[16],
612 unsigned char output[16]);
Yanray Wangb67b4742023-10-31 17:10:32 +0800613#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Andres AGf5bf7182017-03-03 14:09:56 +0000614
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500615#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000616/**
Rose Zadik7f441272018-01-22 11:48:23 +0000617 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100619 * \return \c 0 on success.
620 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200622MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100623int mbedtls_aes_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000624
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500625#endif /* MBEDTLS_SELF_TEST */
626
Paul Bakker5121ce52009-01-03 21:22:43 +0000627#ifdef __cplusplus
628}
629#endif
630
631#endif /* aes.h */