blob: 77ecffd8650439dcd1ec952c339b7c3317fca9c8 [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
158/**
Rose Zadik7f441272018-01-22 11:48:23 +0000159 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 *
Rose Zadik7f441272018-01-22 11:48:23 +0000161 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500162 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000163 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500164 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000165 * \param keybits The size of data passed. Valid options are:
166 * <ul><li>128 bits</li>
167 * <li>192 bits</li>
168 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000169 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100170 * \return \c 0 on success.
171 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200173MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100174int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
175 unsigned int keybits);
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Jaeden Amero9366feb2018-05-29 18:55:17 +0100177#if defined(MBEDTLS_CIPHER_MODE_XTS)
178/**
179 * \brief This function prepares an XTS context for encryption and
180 * sets the encryption key.
181 *
182 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500183 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100184 * \param key The encryption key. This is comprised of the XTS key1
185 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500186 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100187 * \param keybits The size of \p key passed in bits. Valid options are:
188 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
189 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
190 *
191 * \return \c 0 on success.
192 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
193 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200194MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100195int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
196 const unsigned char *key,
197 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100198
199/**
200 * \brief This function prepares an XTS context for decryption and
201 * sets the decryption key.
202 *
203 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500204 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100205 * \param key The decryption key. This is comprised of the XTS key1
206 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500207 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100208 * \param keybits The size of \p key passed in bits. Valid options are:
209 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
210 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
211 *
212 * \return \c 0 on success.
213 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
214 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200215MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100216int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
217 const unsigned char *key,
218 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100219#endif /* MBEDTLS_CIPHER_MODE_XTS */
220
Paul Bakker5121ce52009-01-03 21:22:43 +0000221/**
Rose Zadik7f441272018-01-22 11:48:23 +0000222 * \brief This function performs an AES single-block encryption or
223 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 *
Rose Zadik7f441272018-01-22 11:48:23 +0000225 * It performs the operation defined in the \p mode parameter
226 * (encrypt or decrypt), on the input data buffer defined in
227 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000228 *
Rose Zadik7f441272018-01-22 11:48:23 +0000229 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
230 * mbedtls_aes_setkey_dec() must be called before the first
231 * call to this API with the same context.
232 *
233 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500234 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000235 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
236 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500237 * \param input The buffer holding the input data.
238 * It must be readable and at least \c 16 Bytes long.
239 * \param output The buffer where the output data will be written.
240 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000241
242 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200244MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100245int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
246 int mode,
247 const unsigned char input[16],
248 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000249
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000251/**
Rose Zadik7f441272018-01-22 11:48:23 +0000252 * \brief This function performs an AES-CBC encryption or decryption operation
253 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 *
Rose Zadik7f441272018-01-22 11:48:23 +0000255 * It performs the operation defined in the \p mode
256 * parameter (encrypt/decrypt), on the input data buffer defined in
257 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000258 *
Rose Zadik7f441272018-01-22 11:48:23 +0000259 * It can be called as many times as needed, until all the input
260 * data is processed. mbedtls_aes_init(), and either
261 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
262 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000263 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500264 * \note This function operates on full blocks, that is, the input size
265 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000266 *
267 * \note Upon exit, the content of the IV is updated so that you can
268 * call the same function again on the next
269 * block(s) of data and get the same result as if it was
270 * encrypted in one call. This allows a "streaming" usage.
271 * If you need to retain the contents of the IV, you should
272 * either save it manually or use the cipher module instead.
273 *
274 *
275 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000277 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
278 * #MBEDTLS_AES_DECRYPT.
279 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000281 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000283 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000285 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000287 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100288 * \return \c 0 on success.
289 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000290 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000291 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200292MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100293int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
294 int mode,
295 size_t length,
296 unsigned char iv[16],
297 const unsigned char *input,
298 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000300
Aorimn5f778012016-06-09 23:22:58 +0200301#if defined(MBEDTLS_CIPHER_MODE_XTS)
302/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100303 * \brief This function performs an AES-XTS encryption or decryption
304 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200305 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100306 * AES-XTS encrypts or decrypts blocks based on their location as
307 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100308 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200309 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100310 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
311 * AES blocks. If the data unit is larger than this, this function
312 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
313 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100314 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500315 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100316 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
317 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100319 * length between 16 bytes and 2^24 bytes inclusive
320 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100321 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100322 * bytes in little-endian format. For disk encryption, this
323 * is typically the index of the block device sector that
324 * contains the data.
325 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100327 * input.
328 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100330 * output.
Aorimn5f778012016-06-09 23:22:58 +0200331 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100332 * \return \c 0 on success.
333 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100335 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200336 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200337MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100338int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
339 int mode,
340 size_t length,
341 const unsigned char data_unit[16],
342 const unsigned char *input,
343 unsigned char *output);
Aorimn5f778012016-06-09 23:22:58 +0200344#endif /* MBEDTLS_CIPHER_MODE_XTS */
345
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200346#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000347/**
Rose Zadik7f441272018-01-22 11:48:23 +0000348 * \brief This function performs an AES-CFB128 encryption or decryption
349 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000350 *
Rose Zadik7f441272018-01-22 11:48:23 +0000351 * It performs the operation defined in the \p mode
352 * parameter (encrypt or decrypt), on the input data buffer
353 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000354 *
Rose Zadik7f441272018-01-22 11:48:23 +0000355 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
356 * regardless of whether you are performing an encryption or decryption
357 * operation, that is, regardless of the \p mode parameter. This is
358 * because CFB mode uses the same key schedule for encryption and
359 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000360 *
Rose Zadik7f441272018-01-22 11:48:23 +0000361 * \note Upon exit, the content of the IV is updated so that you can
362 * call the same function again on the next
363 * block(s) of data and get the same result as if it was
364 * encrypted in one call. This allows a "streaming" usage.
365 * If you need to retain the contents of the
366 * IV, you must either save it manually or use the cipher
367 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000368 *
Rose Zadik7f441272018-01-22 11:48:23 +0000369 *
370 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500371 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000372 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
373 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500374 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000375 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000377 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000379 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000383 *
384 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000385 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200386MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100387int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
388 int mode,
389 size_t length,
390 size_t *iv_off,
391 unsigned char iv[16],
392 const unsigned char *input,
393 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000394
Paul Bakker9a736322012-11-14 12:39:52 +0000395/**
Rose Zadik7f441272018-01-22 11:48:23 +0000396 * \brief This function performs an AES-CFB8 encryption or decryption
397 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100398 *
Rose Zadik7f441272018-01-22 11:48:23 +0000399 * It performs the operation defined in the \p mode
400 * parameter (encrypt/decrypt), on the input data buffer defined
401 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100402 *
Rose Zadik7f441272018-01-22 11:48:23 +0000403 * Due to the nature of CFB, you must use the same key schedule for
404 * both encryption and decryption operations. Therefore, you must
405 * use the context initialized with mbedtls_aes_setkey_enc() for
406 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000407 *
Rose Zadik7f441272018-01-22 11:48:23 +0000408 * \note Upon exit, the content of the IV is updated so that you can
409 * call the same function again on the next
410 * block(s) of data and get the same result as if it was
411 * encrypted in one call. This allows a "streaming" usage.
412 * If you need to retain the contents of the
413 * IV, you should either save it manually or use the cipher
414 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100415 *
Rose Zadik7f441272018-01-22 11:48:23 +0000416 *
417 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500418 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000419 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
420 * #MBEDTLS_AES_DECRYPT
421 * \param length The length of the input data.
422 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000424 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500425 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000426 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000428 *
429 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100430 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200431MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100432int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
433 int mode,
434 size_t length,
435 unsigned char iv[16],
436 const unsigned char *input,
437 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100439
Simon Butcher76a5b222018-04-22 22:57:27 +0100440#if defined(MBEDTLS_CIPHER_MODE_OFB)
441/**
Simon Butcher5db13622018-06-04 22:11:25 +0100442 * \brief This function performs an AES-OFB (Output Feedback Mode)
443 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100444 *
Simon Butcher5db13622018-06-04 22:11:25 +0100445 * For OFB, you must set up the context with
446 * mbedtls_aes_setkey_enc(), regardless of whether you are
447 * performing an encryption or decryption operation. This is
448 * because OFB mode uses the same key schedule for encryption and
449 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100450 *
Simon Butcher5db13622018-06-04 22:11:25 +0100451 * The OFB operation is identical for encryption or decryption,
452 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100453 *
Simon Butcher5db13622018-06-04 22:11:25 +0100454 * \note Upon exit, the content of iv, the Initialisation Vector, is
455 * updated so that you can call the same function again on the next
456 * block(s) of data and get the same result as if it was encrypted
457 * in one call. This allows a "streaming" usage, by initialising
458 * iv_off to 0 before the first call, and preserving its value
459 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100460 *
Simon Butcher5db13622018-06-04 22:11:25 +0100461 * For non-streaming use, the iv should be initialised on each call
462 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100463 *
Simon Butcher5db13622018-06-04 22:11:25 +0100464 * If you need to retain the contents of the initialisation vector,
465 * you must either save it manually or use the cipher module
466 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100467 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100468 * \warning For the OFB mode, the initialisation vector must be unique
469 * every encryption operation. Reuse of an initialisation vector
470 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100471 *
472 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500473 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100474 * \param length The length of the input data.
475 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100477 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100479 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100481 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 *
484 * \return \c 0 on success.
485 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200486MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100487int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
488 size_t length,
489 size_t *iv_off,
490 unsigned char iv[16],
491 const unsigned char *input,
492 unsigned char *output);
Simon Butcher76a5b222018-04-22 22:57:27 +0100493
494#endif /* MBEDTLS_CIPHER_MODE_OFB */
495
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100497/**
Rose Zadik7f441272018-01-22 11:48:23 +0000498 * \brief This function performs an AES-CTR encryption or decryption
499 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000500 *
Rose Zadik7f441272018-01-22 11:48:23 +0000501 * Due to the nature of CTR, you must use the same key schedule
502 * for both encryption and decryption operations. Therefore, you
503 * must use the context initialized with mbedtls_aes_setkey_enc()
504 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000505 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100506 * \warning You must never reuse a nonce value with the same key. Doing so
507 * would void the encryption for the two messages encrypted with
508 * the same nonce and key.
509 *
510 * There are two common strategies for managing nonces with CTR:
511 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200512 * 1. You can handle everything as a single message processed over
513 * successive calls to this function. In that case, you want to
514 * set \p nonce_counter and \p nc_off to 0 for the first call, and
515 * then preserve the values of \p nonce_counter, \p nc_off and \p
516 * stream_block across calls to this function as they will be
517 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100518 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200519 * With this strategy, you must not encrypt more than 2**128
520 * blocks of data with the same key.
521 *
522 * 2. You can encrypt separate messages by dividing the \p
523 * nonce_counter buffer in two areas: the first one used for a
524 * per-message nonce, handled by yourself, and the second one
525 * updated by this function internally.
526 *
527 * For example, you might reserve the first 12 bytes for the
528 * per-message nonce, and the last 4 bytes for internal use. In that
529 * case, before calling this function on a new message you need to
530 * set the first 12 bytes of \p nonce_counter to your chosen nonce
531 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
532 * stream_block to be ignored). That way, you can encrypt at most
533 * 2**96 messages of up to 2**32 blocks each with the same key.
534 *
535 * The per-message nonce (or information sufficient to reconstruct
536 * it) needs to be communicated with the ciphertext and must be unique.
537 * The recommended way to ensure uniqueness is to use a message
538 * counter. An alternative is to generate random nonces, but this
539 * limits the number of messages that can be securely encrypted:
540 * for example, with 96-bit random nonces, you should not encrypt
541 * more than 2**32 messages with the same key.
542 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100543 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200544 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000545 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200546 * \warning Upon return, \p stream_block contains sensitive data. Its
547 * content must not be written to insecure storage and should be
548 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000549 *
Rose Zadik7f441272018-01-22 11:48:23 +0000550 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500551 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000552 * \param length The length of the input data.
553 * \param nc_off The offset in the current \p stream_block, for
554 * resuming within the current cipher stream. The
555 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000557 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500558 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000559 * \param stream_block The saved stream block for resuming. This is
560 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500561 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000564 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000566 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100567 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000568 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200569MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100570int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
571 size_t length,
572 size_t *nc_off,
573 unsigned char nonce_counter[16],
574 unsigned char stream_block[16],
575 const unsigned char *input,
576 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200577#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200578
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200579/**
Rose Zadik7f441272018-01-22 11:48:23 +0000580 * \brief Internal AES block encryption function. This is only
581 * exposed to allow overriding it using
582 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200583 *
Rose Zadik7f441272018-01-22 11:48:23 +0000584 * \param ctx The AES context to use for encryption.
585 * \param input The plaintext block.
586 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000587 *
Rose Zadik7f441272018-01-22 11:48:23 +0000588 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200589 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200590MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100591int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
592 const unsigned char input[16],
593 unsigned char output[16]);
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200594
595/**
Rose Zadik7f441272018-01-22 11:48:23 +0000596 * \brief Internal AES block decryption function. This is only
597 * exposed to allow overriding it using see
598 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200599 *
Rose Zadik7f441272018-01-22 11:48:23 +0000600 * \param ctx The AES context to use for decryption.
601 * \param input The ciphertext block.
602 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000603 *
Rose Zadik7f441272018-01-22 11:48:23 +0000604 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200605 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200606MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100607int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
608 const unsigned char input[16],
609 unsigned char output[16]);
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 */