blob: 559e1d84e277848b2851fd85cd792209370c38b5 [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
Paul Bakker5121ce52009-01-03 21:22:43 +000056/**
Rose Zadik7f441272018-01-22 11:48:23 +000057 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
Gilles Peskine449bd832023-01-11 14:50:10 +010059typedef struct mbedtls_aes_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020060 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
Werner Lewis6d719442022-06-13 12:28:07 +010061 size_t MBEDTLS_PRIVATE(rk_offset); /*!< The offset in array elements to AES
Gilles Peskine449bd832023-01-11 14:50:10 +010062 round keys in the buffer. */
Thomas Daubney62af02c2024-06-14 10:37:13 +010063#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH)
Yanray Wang8b9877b2023-05-05 14:46:04 +080064 uint32_t MBEDTLS_PRIVATE(buf)[44]; /*!< Aligned data buffer to hold
Yanray Wangab4fb0d2023-05-10 10:06:11 +080065 10 round keys for 128-bit case. */
Arto Kinnunenb1c626b2023-04-14 17:21:22 +080066#else
Mateusz Starzyk846f0212021-05-19 19:44:07 +020067 uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can
Gilles Peskine449bd832023-01-11 14:50:10 +010068 hold 32 extra Bytes, which can be used for
Thomas Daubney62af02c2024-06-14 10:37:13 +010069 simplifying key expansion in the 256-bit
70 case by generating an extra round key. */
71#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH */
Paul Bakker5121ce52009-01-03 21:22:43 +000072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Jaeden Amero9366feb2018-05-29 18:55:17 +010075#if defined(MBEDTLS_CIPHER_MODE_XTS)
76/**
77 * \brief The AES XTS context-type definition.
78 */
Gilles Peskine449bd832023-01-11 14:50:10 +010079typedef struct mbedtls_aes_xts_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020080 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Gilles Peskine449bd832023-01-11 14:50:10 +010081 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020082 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Gilles Peskine449bd832023-01-11 14:50:10 +010083 computation. */
Jaeden Amero9366feb2018-05-29 18:55:17 +010084} mbedtls_aes_xts_context;
85#endif /* MBEDTLS_CIPHER_MODE_XTS */
86
Paul Bakker5121ce52009-01-03 21:22:43 +000087/**
Rose Zadik7f441272018-01-22 11:48:23 +000088 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089 *
Rose Zadik7f441272018-01-22 11:48:23 +000090 * It must be the first API called before using
91 * the context.
92 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050093 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020094 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void mbedtls_aes_init(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020096
97/**
Rose Zadik7f441272018-01-22 11:48:23 +000098 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020099 *
Rose Zadik7f441272018-01-22 11:48:23 +0000100 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * If this is \c NULL, this function does nothing.
102 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200103 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100104void mbedtls_aes_free(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105
Jaeden Amero9366feb2018-05-29 18:55:17 +0100106#if defined(MBEDTLS_CIPHER_MODE_XTS)
107/**
108 * \brief This function initializes the specified AES XTS context.
109 *
110 * It must be the first API called before using
111 * the context.
112 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500113 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100114 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100115void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100116
117/**
118 * \brief This function releases and clears the specified AES XTS context.
119 *
120 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500121 * If this is \c NULL, this function does nothing.
122 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100123 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100124void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100125#endif /* MBEDTLS_CIPHER_MODE_XTS */
126
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200127/**
Rose Zadik7f441272018-01-22 11:48:23 +0000128 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Rose Zadik7f441272018-01-22 11:48:23 +0000130 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500131 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000132 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500133 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000134 * \param keybits The size of data passed in bits. Valid options are:
135 * <ul><li>128 bits</li>
136 * <li>192 bits</li>
137 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000138 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100139 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100140 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200142MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
144 unsigned int keybits);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Yanray Wangb67b4742023-10-31 17:10:32 +0800146#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Paul Bakker5121ce52009-01-03 21:22:43 +0000147/**
Rose Zadik7f441272018-01-22 11:48:23 +0000148 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 *
Rose Zadik7f441272018-01-22 11:48:23 +0000150 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500151 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000152 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500153 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000154 * \param keybits The size of data passed. Valid options are:
155 * <ul><li>128 bits</li>
156 * <li>192 bits</li>
157 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000158 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100159 * \return \c 0 on success.
160 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200162MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100163int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
164 unsigned int keybits);
Yanray Wangb67b4742023-10-31 17:10:32 +0800165#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
Jaeden Amero9366feb2018-05-29 18:55:17 +0100167#if defined(MBEDTLS_CIPHER_MODE_XTS)
168/**
169 * \brief This function prepares an XTS context for encryption and
170 * sets the encryption key.
171 *
172 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100174 * \param key The encryption key. This is comprised of the XTS key1
175 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500176 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100177 * \param keybits The size of \p key passed in bits. Valid options are:
178 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
179 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
180 *
181 * \return \c 0 on success.
182 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
183 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200184MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100185int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
186 const unsigned char *key,
187 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100188
189/**
190 * \brief This function prepares an XTS context for decryption and
191 * sets the decryption key.
192 *
193 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500194 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100195 * \param key The decryption key. This is comprised of the XTS key1
196 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100198 * \param keybits The size of \p key passed in bits. Valid options are:
199 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
200 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
201 *
202 * \return \c 0 on success.
203 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
204 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200205MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100206int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
207 const unsigned char *key,
208 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100209#endif /* MBEDTLS_CIPHER_MODE_XTS */
210
Paul Bakker5121ce52009-01-03 21:22:43 +0000211/**
Rose Zadik7f441272018-01-22 11:48:23 +0000212 * \brief This function performs an AES single-block encryption or
213 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 *
Rose Zadik7f441272018-01-22 11:48:23 +0000215 * It performs the operation defined in the \p mode parameter
216 * (encrypt or decrypt), on the input data buffer defined in
217 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000218 *
Rose Zadik7f441272018-01-22 11:48:23 +0000219 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
220 * mbedtls_aes_setkey_dec() must be called before the first
221 * call to this API with the same context.
222 *
223 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500224 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000225 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
226 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500227 * \param input The buffer holding the input data.
228 * It must be readable and at least \c 16 Bytes long.
229 * \param output The buffer where the output data will be written.
230 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000231
232 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200234MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100235int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
236 int mode,
237 const unsigned char input[16],
238 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000241/**
Rose Zadik7f441272018-01-22 11:48:23 +0000242 * \brief This function performs an AES-CBC encryption or decryption operation
243 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 *
Rose Zadik7f441272018-01-22 11:48:23 +0000245 * It performs the operation defined in the \p mode
246 * parameter (encrypt/decrypt), on the input data buffer defined in
247 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000248 *
Rose Zadik7f441272018-01-22 11:48:23 +0000249 * It can be called as many times as needed, until all the input
250 * data is processed. mbedtls_aes_init(), and either
251 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
252 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000253 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500254 * \note This function operates on full blocks, that is, the input size
255 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000256 *
257 * \note Upon exit, the content of the IV is updated so that you can
258 * call the same function again on the next
259 * block(s) of data and get the same result as if it was
260 * encrypted in one call. This allows a "streaming" usage.
261 * If you need to retain the contents of the IV, you should
262 * either save it manually or use the cipher module instead.
263 *
264 *
265 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500266 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000267 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
268 * #MBEDTLS_AES_DECRYPT.
269 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000271 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000273 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500274 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000275 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000277 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100278 * \return \c 0 on success.
279 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000280 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200282MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100283int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
284 int mode,
285 size_t length,
286 unsigned char iv[16],
287 const unsigned char *input,
288 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Aorimn5f778012016-06-09 23:22:58 +0200291#if defined(MBEDTLS_CIPHER_MODE_XTS)
292/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100293 * \brief This function performs an AES-XTS encryption or decryption
294 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200295 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100296 * AES-XTS encrypts or decrypts blocks based on their location as
297 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100298 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200299 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100300 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
301 * AES blocks. If the data unit is larger than this, this function
302 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
303 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100304 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500305 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100306 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
307 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500308 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100309 * length between 16 bytes and 2^24 bytes inclusive
310 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100311 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100312 * bytes in little-endian format. For disk encryption, this
313 * is typically the index of the block device sector that
314 * contains the data.
315 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500316 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100317 * input.
318 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500319 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100320 * output.
Aorimn5f778012016-06-09 23:22:58 +0200321 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100322 * \return \c 0 on success.
323 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500324 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100325 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200326 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200327MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100328int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
329 int mode,
330 size_t length,
331 const unsigned char data_unit[16],
332 const unsigned char *input,
333 unsigned char *output);
Aorimn5f778012016-06-09 23:22:58 +0200334#endif /* MBEDTLS_CIPHER_MODE_XTS */
335
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200336#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000337/**
Rose Zadik7f441272018-01-22 11:48:23 +0000338 * \brief This function performs an AES-CFB128 encryption or decryption
339 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000340 *
Rose Zadik7f441272018-01-22 11:48:23 +0000341 * It performs the operation defined in the \p mode
342 * parameter (encrypt or decrypt), on the input data buffer
343 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000344 *
Rose Zadik7f441272018-01-22 11:48:23 +0000345 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
346 * regardless of whether you are performing an encryption or decryption
347 * operation, that is, regardless of the \p mode parameter. This is
348 * because CFB mode uses the same key schedule for encryption and
349 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000350 *
Rose Zadik7f441272018-01-22 11:48:23 +0000351 * \note Upon exit, the content of the IV is updated so that you can
352 * call the same function again on the next
353 * block(s) of data and get the same result as if it was
354 * encrypted in one call. This allows a "streaming" usage.
355 * If you need to retain the contents of the
356 * IV, you must either save it manually or use the cipher
357 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000358 *
Rose Zadik7f441272018-01-22 11:48:23 +0000359 *
360 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500361 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000362 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
363 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500364 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000365 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500366 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000367 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500368 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000369 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500370 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000371 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500372 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000373 *
374 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200376MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100377int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
378 int mode,
379 size_t length,
380 size_t *iv_off,
381 unsigned char iv[16],
382 const unsigned char *input,
383 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000384
Paul Bakker9a736322012-11-14 12:39:52 +0000385/**
Rose Zadik7f441272018-01-22 11:48:23 +0000386 * \brief This function performs an AES-CFB8 encryption or decryption
387 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100388 *
Rose Zadik7f441272018-01-22 11:48:23 +0000389 * It performs the operation defined in the \p mode
390 * parameter (encrypt/decrypt), on the input data buffer defined
391 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100392 *
Rose Zadik7f441272018-01-22 11:48:23 +0000393 * Due to the nature of CFB, you must use the same key schedule for
394 * both encryption and decryption operations. Therefore, you must
395 * use the context initialized with mbedtls_aes_setkey_enc() for
396 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000397 *
Rose Zadik7f441272018-01-22 11:48:23 +0000398 * \note Upon exit, the content of the IV is updated so that you can
399 * call the same function again on the next
400 * block(s) of data and get the same result as if it was
401 * encrypted in one call. This allows a "streaming" usage.
402 * If you need to retain the contents of the
403 * IV, you should either save it manually or use the cipher
404 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100405 *
Rose Zadik7f441272018-01-22 11:48:23 +0000406 *
407 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500408 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000409 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
410 * #MBEDTLS_AES_DECRYPT
411 * \param length The length of the input data.
412 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500413 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000414 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500415 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000416 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500417 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000418 *
419 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100420 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200421MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100422int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
423 int mode,
424 size_t length,
425 unsigned char iv[16],
426 const unsigned char *input,
427 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200428#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100429
Simon Butcher76a5b222018-04-22 22:57:27 +0100430#if defined(MBEDTLS_CIPHER_MODE_OFB)
431/**
Simon Butcher5db13622018-06-04 22:11:25 +0100432 * \brief This function performs an AES-OFB (Output Feedback Mode)
433 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100434 *
Simon Butcher5db13622018-06-04 22:11:25 +0100435 * For OFB, you must set up the context with
436 * mbedtls_aes_setkey_enc(), regardless of whether you are
437 * performing an encryption or decryption operation. This is
438 * because OFB mode uses the same key schedule for encryption and
439 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100440 *
Simon Butcher5db13622018-06-04 22:11:25 +0100441 * The OFB operation is identical for encryption or decryption,
442 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100443 *
Simon Butcher5db13622018-06-04 22:11:25 +0100444 * \note Upon exit, the content of iv, the Initialisation Vector, is
445 * updated so that you can call the same function again on the next
446 * block(s) of data and get the same result as if it was encrypted
447 * in one call. This allows a "streaming" usage, by initialising
448 * iv_off to 0 before the first call, and preserving its value
449 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100450 *
Simon Butcher5db13622018-06-04 22:11:25 +0100451 * For non-streaming use, the iv should be initialised on each call
452 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100453 *
Simon Butcher5db13622018-06-04 22:11:25 +0100454 * If you need to retain the contents of the initialisation vector,
455 * you must either save it manually or use the cipher module
456 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100457 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100458 * \warning For the OFB mode, the initialisation vector must be unique
459 * every encryption operation. Reuse of an initialisation vector
460 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100461 *
462 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500463 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100464 * \param length The length of the input data.
465 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500466 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100467 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500468 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100469 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500470 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100471 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500472 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100473 *
474 * \return \c 0 on success.
475 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200476MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100477int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
478 size_t length,
479 size_t *iv_off,
480 unsigned char iv[16],
481 const unsigned char *input,
482 unsigned char *output);
Simon Butcher76a5b222018-04-22 22:57:27 +0100483
484#endif /* MBEDTLS_CIPHER_MODE_OFB */
485
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200486#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100487/**
Rose Zadik7f441272018-01-22 11:48:23 +0000488 * \brief This function performs an AES-CTR encryption or decryption
489 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000490 *
Rose Zadik7f441272018-01-22 11:48:23 +0000491 * Due to the nature of CTR, you must use the same key schedule
492 * for both encryption and decryption operations. Therefore, you
493 * must use the context initialized with mbedtls_aes_setkey_enc()
494 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000495 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100496 * \warning You must never reuse a nonce value with the same key. Doing so
497 * would void the encryption for the two messages encrypted with
498 * the same nonce and key.
499 *
500 * There are two common strategies for managing nonces with CTR:
501 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200502 * 1. You can handle everything as a single message processed over
503 * successive calls to this function. In that case, you want to
504 * set \p nonce_counter and \p nc_off to 0 for the first call, and
505 * then preserve the values of \p nonce_counter, \p nc_off and \p
506 * stream_block across calls to this function as they will be
507 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100508 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200509 * With this strategy, you must not encrypt more than 2**128
510 * blocks of data with the same key.
511 *
512 * 2. You can encrypt separate messages by dividing the \p
513 * nonce_counter buffer in two areas: the first one used for a
514 * per-message nonce, handled by yourself, and the second one
515 * updated by this function internally.
516 *
517 * For example, you might reserve the first 12 bytes for the
518 * per-message nonce, and the last 4 bytes for internal use. In that
519 * case, before calling this function on a new message you need to
520 * set the first 12 bytes of \p nonce_counter to your chosen nonce
521 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
522 * stream_block to be ignored). That way, you can encrypt at most
523 * 2**96 messages of up to 2**32 blocks each with the same key.
524 *
525 * The per-message nonce (or information sufficient to reconstruct
526 * it) needs to be communicated with the ciphertext and must be unique.
527 * The recommended way to ensure uniqueness is to use a message
528 * counter. An alternative is to generate random nonces, but this
529 * limits the number of messages that can be securely encrypted:
530 * for example, with 96-bit random nonces, you should not encrypt
531 * more than 2**32 messages with the same key.
532 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100533 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200534 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000535 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200536 * \warning Upon return, \p stream_block contains sensitive data. Its
537 * content must not be written to insecure storage and should be
538 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000539 *
Rose Zadik7f441272018-01-22 11:48:23 +0000540 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500541 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000542 * \param length The length of the input data.
543 * \param nc_off The offset in the current \p stream_block, for
544 * resuming within the current cipher stream. The
545 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500546 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000547 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500548 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000549 * \param stream_block The saved stream block for resuming. This is
550 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500551 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000552 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500553 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000554 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500555 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000556 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100557 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000558 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200559MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100560int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
561 size_t length,
562 size_t *nc_off,
563 unsigned char nonce_counter[16],
564 unsigned char stream_block[16],
565 const unsigned char *input,
566 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200568
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200569/**
Rose Zadik7f441272018-01-22 11:48:23 +0000570 * \brief Internal AES block encryption function. This is only
571 * exposed to allow overriding it using
572 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200573 *
Rose Zadik7f441272018-01-22 11:48:23 +0000574 * \param ctx The AES context to use for encryption.
575 * \param input The plaintext block.
576 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000577 *
Rose Zadik7f441272018-01-22 11:48:23 +0000578 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200579 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200580MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100581int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
582 const unsigned char input[16],
583 unsigned char output[16]);
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200584
Yanray Wangb67b4742023-10-31 17:10:32 +0800585#if !defined(MBEDTLS_BLOCK_CIPHER_NO_DECRYPT)
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200586/**
Rose Zadik7f441272018-01-22 11:48:23 +0000587 * \brief Internal AES block decryption function. This is only
588 * exposed to allow overriding it using see
589 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200590 *
Rose Zadik7f441272018-01-22 11:48:23 +0000591 * \param ctx The AES context to use for decryption.
592 * \param input The ciphertext block.
593 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000594 *
Rose Zadik7f441272018-01-22 11:48:23 +0000595 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200596 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200597MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100598int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
599 const unsigned char input[16],
600 unsigned char output[16]);
Yanray Wangb67b4742023-10-31 17:10:32 +0800601#endif /* !MBEDTLS_BLOCK_CIPHER_NO_DECRYPT */
Andres AGf5bf7182017-03-03 14:09:56 +0000602
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500603#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000604/**
Rose Zadik7f441272018-01-22 11:48:23 +0000605 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000606 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100607 * \return \c 0 on success.
608 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000609 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200610MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100611int mbedtls_aes_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000612
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500613#endif /* MBEDTLS_SELF_TEST */
614
Paul Bakker5121ce52009-01-03 21:22:43 +0000615#ifdef __cplusplus
616}
617#endif
618
619#endif /* aes.h */