blob: a07c8a5f9803525edb635a6b1ed956ab90bbbbe2 [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
Thomas Daubney4e5d1832024-06-25 15:21:48 +010038/* aesni.c relies 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. */
Thomas Daubney62af02c2024-06-14 10:37:13 +010067#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
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
Thomas Daubney62af02c2024-06-14 10:37:13 +010073 simplifying key expansion in the 256-bit
74 case by generating an extra round key. */
75#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Paul Bakker5121ce52009-01-03 21:22:43 +000076}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000078
Jaeden Amero9366feb2018-05-29 18:55:17 +010079#if defined(MBEDTLS_CIPHER_MODE_XTS)
80/**
81 * \brief The AES XTS context-type definition.
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083typedef struct mbedtls_aes_xts_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020084 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Gilles Peskine449bd832023-01-11 14:50:10 +010085 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020086 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Gilles Peskine449bd832023-01-11 14:50:10 +010087 computation. */
Jaeden Amero9366feb2018-05-29 18:55:17 +010088} mbedtls_aes_xts_context;
89#endif /* MBEDTLS_CIPHER_MODE_XTS */
90
Ron Eldorb2aacec2017-05-18 16:53:08 +030091#else /* MBEDTLS_AES_ALT */
92#include "aes_alt.h"
93#endif /* MBEDTLS_AES_ALT */
94
Paul Bakker5121ce52009-01-03 21:22:43 +000095/**
Rose Zadik7f441272018-01-22 11:48:23 +000096 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020097 *
Rose Zadik7f441272018-01-22 11:48:23 +000098 * It must be the first API called before using
99 * the context.
100 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200102 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100103void mbedtls_aes_init(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200104
105/**
Rose Zadik7f441272018-01-22 11:48:23 +0000106 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200107 *
Rose Zadik7f441272018-01-22 11:48:23 +0000108 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500109 * If this is \c NULL, this function does nothing.
110 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200111 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100112void mbedtls_aes_free(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200113
Jaeden Amero9366feb2018-05-29 18:55:17 +0100114#if defined(MBEDTLS_CIPHER_MODE_XTS)
115/**
116 * \brief This function initializes the specified AES XTS context.
117 *
118 * It must be the first API called before using
119 * the context.
120 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100122 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100123void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100124
125/**
126 * \brief This function releases and clears the specified AES XTS context.
127 *
128 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500129 * If this is \c NULL, this function does nothing.
130 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100131 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100132void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100133#endif /* MBEDTLS_CIPHER_MODE_XTS */
134
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200135/**
Rose Zadik7f441272018-01-22 11:48:23 +0000136 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 *
Rose Zadik7f441272018-01-22 11:48:23 +0000138 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500139 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000140 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000142 * \param keybits The size of data passed in bits. Valid options are:
143 * <ul><li>128 bits</li>
144 * <li>192 bits</li>
145 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000146 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100147 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100148 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200150MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100151int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
152 unsigned int keybits);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Yanray Wangb67b4742023-10-31 17:10:32 +0800154#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000155/**
Rose Zadik7f441272018-01-22 11:48:23 +0000156 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 *
Rose Zadik7f441272018-01-22 11:48:23 +0000158 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500159 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000160 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500161 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000162 * \param keybits The size of data passed. Valid options are:
163 * <ul><li>128 bits</li>
164 * <li>192 bits</li>
165 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000166 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100167 * \return \c 0 on success.
168 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200170MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100171int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
172 unsigned int keybits);
Yanray Wangb67b4742023-10-31 17:10:32 +0800173#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Jaeden Amero9366feb2018-05-29 18:55:17 +0100175#if defined(MBEDTLS_CIPHER_MODE_XTS)
176/**
177 * \brief This function prepares an XTS context for encryption and
178 * sets the encryption key.
179 *
180 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500181 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100182 * \param key The encryption key. This is comprised of the XTS key1
183 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500184 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100185 * \param keybits The size of \p key passed in bits. Valid options are:
186 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
187 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
188 *
189 * \return \c 0 on success.
190 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
191 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200192MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100193int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
194 const unsigned char *key,
195 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100196
197/**
198 * \brief This function prepares an XTS context for decryption and
199 * sets the decryption key.
200 *
201 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500202 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100203 * \param key The decryption key. This is comprised of the XTS key1
204 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500205 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100206 * \param keybits The size of \p key passed in bits. Valid options are:
207 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
208 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
209 *
210 * \return \c 0 on success.
211 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
212 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200213MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100214int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
215 const unsigned char *key,
216 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100217#endif /* MBEDTLS_CIPHER_MODE_XTS */
218
Paul Bakker5121ce52009-01-03 21:22:43 +0000219/**
Rose Zadik7f441272018-01-22 11:48:23 +0000220 * \brief This function performs an AES single-block encryption or
221 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 *
Rose Zadik7f441272018-01-22 11:48:23 +0000223 * It performs the operation defined in the \p mode parameter
224 * (encrypt or decrypt), on the input data buffer defined in
225 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000226 *
Rose Zadik7f441272018-01-22 11:48:23 +0000227 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
228 * mbedtls_aes_setkey_dec() must be called before the first
229 * call to this API with the same context.
230 *
231 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500232 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000233 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
234 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500235 * \param input The buffer holding the input data.
236 * It must be readable and at least \c 16 Bytes long.
237 * \param output The buffer where the output data will be written.
238 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000239
240 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200242MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100243int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
244 int mode,
245 const unsigned char input[16],
246 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000247
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000249/**
Rose Zadik7f441272018-01-22 11:48:23 +0000250 * \brief This function performs an AES-CBC encryption or decryption operation
251 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 *
Rose Zadik7f441272018-01-22 11:48:23 +0000253 * It performs the operation defined in the \p mode
254 * parameter (encrypt/decrypt), on the input data buffer defined in
255 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000256 *
Rose Zadik7f441272018-01-22 11:48:23 +0000257 * It can be called as many times as needed, until all the input
258 * data is processed. mbedtls_aes_init(), and either
259 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
260 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000261 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500262 * \note This function operates on full blocks, that is, the input size
263 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000264 *
265 * \note Upon exit, the content of the IV is updated so that you can
266 * call the same function again on the next
267 * block(s) of data and get the same result as if it was
268 * encrypted in one call. This allows a "streaming" usage.
269 * If you need to retain the contents of the IV, you should
270 * either save it manually or use the cipher module instead.
271 *
272 *
273 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000275 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
276 * #MBEDTLS_AES_DECRYPT.
277 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500278 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000279 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000281 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000283 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000285 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100286 * \return \c 0 on success.
287 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000288 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000289 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200290MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100291int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
292 int mode,
293 size_t length,
294 unsigned char iv[16],
295 const unsigned char *input,
296 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200297#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000298
Aorimn5f778012016-06-09 23:22:58 +0200299#if defined(MBEDTLS_CIPHER_MODE_XTS)
300/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100301 * \brief This function performs an AES-XTS encryption or decryption
302 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200303 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100304 * AES-XTS encrypts or decrypts blocks based on their location as
305 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100306 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200307 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100308 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
309 * AES blocks. If the data unit is larger than this, this function
310 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
311 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100312 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500313 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100314 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
315 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100317 * length between 16 bytes and 2^24 bytes inclusive
318 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100319 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100320 * bytes in little-endian format. For disk encryption, this
321 * is typically the index of the block device sector that
322 * contains the data.
323 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500324 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100325 * input.
326 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100328 * output.
Aorimn5f778012016-06-09 23:22:58 +0200329 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100330 * \return \c 0 on success.
331 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500332 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100333 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200334 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200335MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100336int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
337 int mode,
338 size_t length,
339 const unsigned char data_unit[16],
340 const unsigned char *input,
341 unsigned char *output);
Aorimn5f778012016-06-09 23:22:58 +0200342#endif /* MBEDTLS_CIPHER_MODE_XTS */
343
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200344#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000345/**
Rose Zadik7f441272018-01-22 11:48:23 +0000346 * \brief This function performs an AES-CFB128 encryption or decryption
347 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000348 *
Rose Zadik7f441272018-01-22 11:48:23 +0000349 * It performs the operation defined in the \p mode
350 * parameter (encrypt or decrypt), on the input data buffer
351 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000352 *
Rose Zadik7f441272018-01-22 11:48:23 +0000353 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
354 * regardless of whether you are performing an encryption or decryption
355 * operation, that is, regardless of the \p mode parameter. This is
356 * because CFB mode uses the same key schedule for encryption and
357 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000358 *
Rose Zadik7f441272018-01-22 11:48:23 +0000359 * \note Upon exit, the content of the IV is updated so that you can
360 * call the same function again on the next
361 * block(s) of data and get the same result as if it was
362 * encrypted in one call. This allows a "streaming" usage.
363 * If you need to retain the contents of the
364 * IV, you must either save it manually or use the cipher
365 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000366 *
Rose Zadik7f441272018-01-22 11:48:23 +0000367 *
368 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500369 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000370 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
371 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000373 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500374 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000375 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000377 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000379 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 *
382 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000383 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200384MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100385int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
386 int mode,
387 size_t length,
388 size_t *iv_off,
389 unsigned char iv[16],
390 const unsigned char *input,
391 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000392
Paul Bakker9a736322012-11-14 12:39:52 +0000393/**
Rose Zadik7f441272018-01-22 11:48:23 +0000394 * \brief This function performs an AES-CFB8 encryption or decryption
395 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100396 *
Rose Zadik7f441272018-01-22 11:48:23 +0000397 * It performs the operation defined in the \p mode
398 * parameter (encrypt/decrypt), on the input data buffer defined
399 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100400 *
Rose Zadik7f441272018-01-22 11:48:23 +0000401 * Due to the nature of CFB, you must use the same key schedule for
402 * both encryption and decryption operations. Therefore, you must
403 * use the context initialized with mbedtls_aes_setkey_enc() for
404 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000405 *
Rose Zadik7f441272018-01-22 11:48:23 +0000406 * \note Upon exit, the content of the IV is updated so that you can
407 * call the same function again on the next
408 * block(s) of data and get the same result as if it was
409 * encrypted in one call. This allows a "streaming" usage.
410 * If you need to retain the contents of the
411 * IV, you should either save it manually or use the cipher
412 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100413 *
Rose Zadik7f441272018-01-22 11:48:23 +0000414 *
415 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500416 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000417 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
418 * #MBEDTLS_AES_DECRYPT
419 * \param length The length of the input data.
420 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500421 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000422 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000424 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500425 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000426 *
427 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100428 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200429MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100430int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
431 int mode,
432 size_t length,
433 unsigned char iv[16],
434 const unsigned char *input,
435 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100437
Simon Butcher76a5b222018-04-22 22:57:27 +0100438#if defined(MBEDTLS_CIPHER_MODE_OFB)
439/**
Simon Butcher5db13622018-06-04 22:11:25 +0100440 * \brief This function performs an AES-OFB (Output Feedback Mode)
441 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100442 *
Simon Butcher5db13622018-06-04 22:11:25 +0100443 * For OFB, you must set up the context with
444 * mbedtls_aes_setkey_enc(), regardless of whether you are
445 * performing an encryption or decryption operation. This is
446 * because OFB mode uses the same key schedule for encryption and
447 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100448 *
Simon Butcher5db13622018-06-04 22:11:25 +0100449 * The OFB operation is identical for encryption or decryption,
450 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100451 *
Simon Butcher5db13622018-06-04 22:11:25 +0100452 * \note Upon exit, the content of iv, the Initialisation Vector, is
453 * updated so that you can call the same function again on the next
454 * block(s) of data and get the same result as if it was encrypted
455 * in one call. This allows a "streaming" usage, by initialising
456 * iv_off to 0 before the first call, and preserving its value
457 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100458 *
Simon Butcher5db13622018-06-04 22:11:25 +0100459 * For non-streaming use, the iv should be initialised on each call
460 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100461 *
Simon Butcher5db13622018-06-04 22:11:25 +0100462 * If you need to retain the contents of the initialisation vector,
463 * you must either save it manually or use the cipher module
464 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100465 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100466 * \warning For the OFB mode, the initialisation vector must be unique
467 * every encryption operation. Reuse of an initialisation vector
468 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100469 *
470 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500471 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100472 * \param length The length of the input data.
473 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500474 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100475 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100477 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100479 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100481 *
482 * \return \c 0 on success.
483 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200484MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100485int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
486 size_t length,
487 size_t *iv_off,
488 unsigned char iv[16],
489 const unsigned char *input,
490 unsigned char *output);
Simon Butcher76a5b222018-04-22 22:57:27 +0100491
492#endif /* MBEDTLS_CIPHER_MODE_OFB */
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100495/**
Rose Zadik7f441272018-01-22 11:48:23 +0000496 * \brief This function performs an AES-CTR encryption or decryption
497 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000498 *
Rose Zadik7f441272018-01-22 11:48:23 +0000499 * Due to the nature of CTR, you must use the same key schedule
500 * for both encryption and decryption operations. Therefore, you
501 * must use the context initialized with mbedtls_aes_setkey_enc()
502 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000503 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100504 * \warning You must never reuse a nonce value with the same key. Doing so
505 * would void the encryption for the two messages encrypted with
506 * the same nonce and key.
507 *
508 * There are two common strategies for managing nonces with CTR:
509 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200510 * 1. You can handle everything as a single message processed over
511 * successive calls to this function. In that case, you want to
512 * set \p nonce_counter and \p nc_off to 0 for the first call, and
513 * then preserve the values of \p nonce_counter, \p nc_off and \p
514 * stream_block across calls to this function as they will be
515 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100516 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200517 * With this strategy, you must not encrypt more than 2**128
518 * blocks of data with the same key.
519 *
520 * 2. You can encrypt separate messages by dividing the \p
521 * nonce_counter buffer in two areas: the first one used for a
522 * per-message nonce, handled by yourself, and the second one
523 * updated by this function internally.
524 *
525 * For example, you might reserve the first 12 bytes for the
526 * per-message nonce, and the last 4 bytes for internal use. In that
527 * case, before calling this function on a new message you need to
528 * set the first 12 bytes of \p nonce_counter to your chosen nonce
529 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
530 * stream_block to be ignored). That way, you can encrypt at most
531 * 2**96 messages of up to 2**32 blocks each with the same key.
532 *
533 * The per-message nonce (or information sufficient to reconstruct
534 * it) needs to be communicated with the ciphertext and must be unique.
535 * The recommended way to ensure uniqueness is to use a message
536 * counter. An alternative is to generate random nonces, but this
537 * limits the number of messages that can be securely encrypted:
538 * for example, with 96-bit random nonces, you should not encrypt
539 * more than 2**32 messages with the same key.
540 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100541 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200542 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000543 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200544 * \warning Upon return, \p stream_block contains sensitive data. Its
545 * content must not be written to insecure storage and should be
546 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000547 *
Rose Zadik7f441272018-01-22 11:48:23 +0000548 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500549 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000550 * \param length The length of the input data.
551 * \param nc_off The offset in the current \p stream_block, for
552 * resuming within the current cipher stream. The
553 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500554 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000555 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000557 * \param stream_block The saved stream block for resuming. This is
558 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500559 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000560 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500561 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000564 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100565 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000566 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200567MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100568int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
569 size_t length,
570 size_t *nc_off,
571 unsigned char nonce_counter[16],
572 unsigned char stream_block[16],
573 const unsigned char *input,
574 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200575#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200576
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200577/**
Rose Zadik7f441272018-01-22 11:48:23 +0000578 * \brief Internal AES block encryption function. This is only
579 * exposed to allow overriding it using
580 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200581 *
Rose Zadik7f441272018-01-22 11:48:23 +0000582 * \param ctx The AES context to use for encryption.
583 * \param input The plaintext block.
584 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000585 *
Rose Zadik7f441272018-01-22 11:48:23 +0000586 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200587 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200588MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100589int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
590 const unsigned char input[16],
591 unsigned char output[16]);
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200592
Yanray Wangb67b4742023-10-31 17:10:32 +0800593#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200594/**
Rose Zadik7f441272018-01-22 11:48:23 +0000595 * \brief Internal AES block decryption function. This is only
596 * exposed to allow overriding it using see
597 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200598 *
Rose Zadik7f441272018-01-22 11:48:23 +0000599 * \param ctx The AES context to use for decryption.
600 * \param input The ciphertext block.
601 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000602 *
Rose Zadik7f441272018-01-22 11:48:23 +0000603 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200604 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200605MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100606int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
607 const unsigned char input[16],
608 unsigned char output[16]);
Yanray Wangb67b4742023-10-31 17:10:32 +0800609#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Andres AGf5bf7182017-03-03 14:09:56 +0000610
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500611#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000612/**
Rose Zadik7f441272018-01-22 11:48:23 +0000613 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100615 * \return \c 0 on success.
616 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200618MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100619int mbedtls_aes_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000620
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500621#endif /* MBEDTLS_SELF_TEST */
622
Paul Bakker5121ce52009-01-03 21:22:43 +0000623#ifdef __cplusplus
624}
625#endif
626
627#endif /* aes.h */