blob: a8d4306147db843244ef5035b6d8c4013b521618 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020025 * SPDX-License-Identifier: Apache-2.0
26 *
27 * Licensed under the Apache License, Version 2.0 (the "License"); you may
28 * not use this file except in compliance with the License.
29 * You may obtain a copy of the License at
30 *
31 * http://www.apache.org/licenses/LICENSE-2.0
32 *
33 * Unless required by applicable law or agreed to in writing, software
34 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
35 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
36 * See the License for the specific language governing permissions and
37 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000038 */
Rose Zadik7f441272018-01-22 11:48:23 +000039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#ifndef MBEDTLS_AES_H
41#define MBEDTLS_AES_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020042#include "mbedtls/private_access.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Bence Szépkútic662b362021-05-27 11:25:03 +020044#include "mbedtls/build_info.h"
Mateusz Starzyke35f8f62021-08-04 15:38:09 +020045#include "mbedtls/platform_util.h"
Paul Bakker90995b52013-06-24 19:20:35 +020046
Rich Evans00ab4702015-02-06 13:43:58 +000047#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020048#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000049
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010050/* padlock.c and aesni.c rely on these values! */
Rose Zadik7f441272018-01-22 11:48:23 +000051#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
52#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
Paul Bakker5121ce52009-01-03 21:22:43 +000053
Andres Amaya Garciac5380642017-11-28 19:57:51 +000054/* Error codes in range 0x0020-0x0022 */
Gilles Peskined2971572021-07-26 18:48:10 +020055/** Invalid key length. */
56#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
57/** Invalid data input length. */
58#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
Paul Bakker2b222c82009-07-27 21:03:45 +000059
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000060/* Error codes in range 0x0021-0x0025 */
Gilles Peskined2971572021-07-26 18:48:10 +020061/** Invalid input data. */
62#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
Ron Eldor9924bdc2018-10-04 10:59:13 +030063
Paul Bakker407a0da2013-06-27 14:29:21 +020064#ifdef __cplusplus
65extern "C" {
66#endif
67
Ron Eldorb2aacec2017-05-18 16:53:08 +030068#if !defined(MBEDTLS_AES_ALT)
69// Regular implementation
70//
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072/**
Rose Zadik7f441272018-01-22 11:48:23 +000073 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000074 */
Gilles Peskine449bd832023-01-11 14:50:10 +010075typedef struct mbedtls_aes_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020076 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
Werner Lewis6d719442022-06-13 12:28:07 +010077 size_t MBEDTLS_PRIVATE(rk_offset); /*!< The offset in array elements to AES
Gilles Peskine449bd832023-01-11 14:50:10 +010078 round keys in the buffer. */
Arto Kinnunenb1c626b2023-04-14 17:21:22 +080079#if defined(MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH) && !defined(MBEDTLS_PADLOCK_C)
80 uint32_t MBEDTLS_PRIVATE(buf)[44];
81#else
Mateusz Starzyk846f0212021-05-19 19:44:07 +020082 uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can
Gilles Peskine449bd832023-01-11 14:50:10 +010083 hold 32 extra Bytes, which can be used for
84 one of the following purposes:
85 <ul><li>Alignment if VIA padlock is
86 used.</li>
87 <li>Simplifying key expansion in the 256-bit
88 case by generating an extra round key.
89 </li></ul> */
Arto Kinnunenb1c626b2023-04-14 17:21:22 +080090#endif /* MBEDTLS_AES_ONLY_128_BIT_KEY_LENGTH && !MBEDTLS_PADLOCK_C */
Paul Bakker5121ce52009-01-03 21:22:43 +000091}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Jaeden Amero9366feb2018-05-29 18:55:17 +010094#if defined(MBEDTLS_CIPHER_MODE_XTS)
95/**
96 * \brief The AES XTS context-type definition.
97 */
Gilles Peskine449bd832023-01-11 14:50:10 +010098typedef struct mbedtls_aes_xts_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020099 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 computation. */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100103} mbedtls_aes_xts_context;
104#endif /* MBEDTLS_CIPHER_MODE_XTS */
105
Ron Eldorb2aacec2017-05-18 16:53:08 +0300106#else /* MBEDTLS_AES_ALT */
107#include "aes_alt.h"
108#endif /* MBEDTLS_AES_ALT */
109
Paul Bakker5121ce52009-01-03 21:22:43 +0000110/**
Rose Zadik7f441272018-01-22 11:48:23 +0000111 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112 *
Rose Zadik7f441272018-01-22 11:48:23 +0000113 * It must be the first API called before using
114 * the context.
115 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500116 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200117 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100118void mbedtls_aes_init(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200119
120/**
Rose Zadik7f441272018-01-22 11:48:23 +0000121 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200122 *
Rose Zadik7f441272018-01-22 11:48:23 +0000123 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500124 * If this is \c NULL, this function does nothing.
125 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200126 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100127void mbedtls_aes_free(mbedtls_aes_context *ctx);
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200128
Jaeden Amero9366feb2018-05-29 18:55:17 +0100129#if defined(MBEDTLS_CIPHER_MODE_XTS)
130/**
131 * \brief This function initializes the specified AES XTS context.
132 *
133 * It must be the first API called before using
134 * the context.
135 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100137 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100138void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100139
140/**
141 * \brief This function releases and clears the specified AES XTS context.
142 *
143 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500144 * If this is \c NULL, this function does nothing.
145 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100148#endif /* MBEDTLS_CIPHER_MODE_XTS */
149
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200150/**
Rose Zadik7f441272018-01-22 11:48:23 +0000151 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 *
Rose Zadik7f441272018-01-22 11:48:23 +0000153 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000155 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000157 * \param keybits The size of data passed in bits. Valid options are:
158 * <ul><li>128 bits</li>
159 * <li>192 bits</li>
160 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000161 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100162 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100163 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200165MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100166int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx, const unsigned char *key,
167 unsigned int keybits);
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169/**
Rose Zadik7f441272018-01-22 11:48:23 +0000170 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 *
Rose Zadik7f441272018-01-22 11:48:23 +0000172 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000174 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500175 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000176 * \param keybits The size of data passed. Valid options are:
177 * <ul><li>128 bits</li>
178 * <li>192 bits</li>
179 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000180 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100181 * \return \c 0 on success.
182 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200184MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100185int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx, const unsigned char *key,
186 unsigned int keybits);
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Jaeden Amero9366feb2018-05-29 18:55:17 +0100188#if defined(MBEDTLS_CIPHER_MODE_XTS)
189/**
190 * \brief This function prepares an XTS context for encryption and
191 * sets the encryption 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 encryption 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_enc(mbedtls_aes_xts_context *ctx,
207 const unsigned char *key,
208 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100209
210/**
211 * \brief This function prepares an XTS context for decryption and
212 * sets the decryption key.
213 *
214 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100216 * \param key The decryption key. This is comprised of the XTS key1
217 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500218 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100219 * \param keybits The size of \p key passed in bits. Valid options are:
220 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
221 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
222 *
223 * \return \c 0 on success.
224 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
225 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200226MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100227int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
228 const unsigned char *key,
229 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100230#endif /* MBEDTLS_CIPHER_MODE_XTS */
231
Paul Bakker5121ce52009-01-03 21:22:43 +0000232/**
Rose Zadik7f441272018-01-22 11:48:23 +0000233 * \brief This function performs an AES single-block encryption or
234 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 *
Rose Zadik7f441272018-01-22 11:48:23 +0000236 * It performs the operation defined in the \p mode parameter
237 * (encrypt or decrypt), on the input data buffer defined in
238 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000239 *
Rose Zadik7f441272018-01-22 11:48:23 +0000240 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
241 * mbedtls_aes_setkey_dec() must be called before the first
242 * call to this API with the same context.
243 *
244 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500245 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000246 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
247 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500248 * \param input The buffer holding the input data.
249 * It must be readable and at least \c 16 Bytes long.
250 * \param output The buffer where the output data will be written.
251 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000252
253 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200255MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100256int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
257 int mode,
258 const unsigned char input[16],
259 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000262/**
Rose Zadik7f441272018-01-22 11:48:23 +0000263 * \brief This function performs an AES-CBC encryption or decryption operation
264 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 *
Rose Zadik7f441272018-01-22 11:48:23 +0000266 * It performs the operation defined in the \p mode
267 * parameter (encrypt/decrypt), on the input data buffer defined in
268 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000269 *
Rose Zadik7f441272018-01-22 11:48:23 +0000270 * It can be called as many times as needed, until all the input
271 * data is processed. mbedtls_aes_init(), and either
272 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
273 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000274 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500275 * \note This function operates on full blocks, that is, the input size
276 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000277 *
278 * \note Upon exit, the content of the IV is updated so that you can
279 * call the same function again on the next
280 * block(s) of data and get the same result as if it was
281 * encrypted in one call. This allows a "streaming" usage.
282 * If you need to retain the contents of the IV, you should
283 * either save it manually or use the cipher module instead.
284 *
285 *
286 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500287 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000288 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
289 * #MBEDTLS_AES_DECRYPT.
290 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000292 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500293 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000294 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000296 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500297 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000298 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100299 * \return \c 0 on success.
300 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000301 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200303MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100304int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
305 int mode,
306 size_t length,
307 unsigned char iv[16],
308 const unsigned char *input,
309 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000311
Aorimn5f778012016-06-09 23:22:58 +0200312#if defined(MBEDTLS_CIPHER_MODE_XTS)
313/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100314 * \brief This function performs an AES-XTS encryption or decryption
315 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200316 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100317 * AES-XTS encrypts or decrypts blocks based on their location as
318 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100319 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200320 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100321 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
322 * AES blocks. If the data unit is larger than this, this function
323 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
324 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100325 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500326 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100327 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
328 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100330 * length between 16 bytes and 2^24 bytes inclusive
331 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100332 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100333 * bytes in little-endian format. For disk encryption, this
334 * is typically the index of the block device sector that
335 * contains the data.
336 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500337 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100338 * input.
339 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500340 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100341 * output.
Aorimn5f778012016-06-09 23:22:58 +0200342 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100343 * \return \c 0 on success.
344 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500345 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100346 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200347 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200348MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100349int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
350 int mode,
351 size_t length,
352 const unsigned char data_unit[16],
353 const unsigned char *input,
354 unsigned char *output);
Aorimn5f778012016-06-09 23:22:58 +0200355#endif /* MBEDTLS_CIPHER_MODE_XTS */
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000358/**
Rose Zadik7f441272018-01-22 11:48:23 +0000359 * \brief This function performs an AES-CFB128 encryption or decryption
360 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 *
Rose Zadik7f441272018-01-22 11:48:23 +0000362 * It performs the operation defined in the \p mode
363 * parameter (encrypt or decrypt), on the input data buffer
364 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000365 *
Rose Zadik7f441272018-01-22 11:48:23 +0000366 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
367 * regardless of whether you are performing an encryption or decryption
368 * operation, that is, regardless of the \p mode parameter. This is
369 * because CFB mode uses the same key schedule for encryption and
370 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000371 *
Rose Zadik7f441272018-01-22 11:48:23 +0000372 * \note Upon exit, the content of the IV is updated so that you can
373 * call the same function again on the next
374 * block(s) of data and get the same result as if it was
375 * encrypted in one call. This allows a "streaming" usage.
376 * If you need to retain the contents of the
377 * IV, you must either save it manually or use the cipher
378 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000379 *
Rose Zadik7f441272018-01-22 11:48:23 +0000380 *
381 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000383 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
384 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000386 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000388 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000390 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500391 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000392 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500393 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000394 *
395 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200397MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100398int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
399 int mode,
400 size_t length,
401 size_t *iv_off,
402 unsigned char iv[16],
403 const unsigned char *input,
404 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000405
Paul Bakker9a736322012-11-14 12:39:52 +0000406/**
Rose Zadik7f441272018-01-22 11:48:23 +0000407 * \brief This function performs an AES-CFB8 encryption or decryption
408 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100409 *
Rose Zadik7f441272018-01-22 11:48:23 +0000410 * It performs the operation defined in the \p mode
411 * parameter (encrypt/decrypt), on the input data buffer defined
412 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100413 *
Rose Zadik7f441272018-01-22 11:48:23 +0000414 * Due to the nature of CFB, you must use the same key schedule for
415 * both encryption and decryption operations. Therefore, you must
416 * use the context initialized with mbedtls_aes_setkey_enc() for
417 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000418 *
Rose Zadik7f441272018-01-22 11:48:23 +0000419 * \note Upon exit, the content of the IV is updated so that you can
420 * call the same function again on the next
421 * block(s) of data and get the same result as if it was
422 * encrypted in one call. This allows a "streaming" usage.
423 * If you need to retain the contents of the
424 * IV, you should either save it manually or use the cipher
425 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100426 *
Rose Zadik7f441272018-01-22 11:48:23 +0000427 *
428 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500429 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000430 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
431 * #MBEDTLS_AES_DECRYPT
432 * \param length The length of the input data.
433 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500434 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000435 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500436 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000437 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500438 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000439 *
440 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100441 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200442MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100443int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
444 int mode,
445 size_t length,
446 unsigned char iv[16],
447 const unsigned char *input,
448 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100450
Simon Butcher76a5b222018-04-22 22:57:27 +0100451#if defined(MBEDTLS_CIPHER_MODE_OFB)
452/**
Simon Butcher5db13622018-06-04 22:11:25 +0100453 * \brief This function performs an AES-OFB (Output Feedback Mode)
454 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100455 *
Simon Butcher5db13622018-06-04 22:11:25 +0100456 * For OFB, you must set up the context with
457 * mbedtls_aes_setkey_enc(), regardless of whether you are
458 * performing an encryption or decryption operation. This is
459 * because OFB mode uses the same key schedule for encryption and
460 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100461 *
Simon Butcher5db13622018-06-04 22:11:25 +0100462 * The OFB operation is identical for encryption or decryption,
463 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100464 *
Simon Butcher5db13622018-06-04 22:11:25 +0100465 * \note Upon exit, the content of iv, the Initialisation Vector, is
466 * updated so that you can call the same function again on the next
467 * block(s) of data and get the same result as if it was encrypted
468 * in one call. This allows a "streaming" usage, by initialising
469 * iv_off to 0 before the first call, and preserving its value
470 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100471 *
Simon Butcher5db13622018-06-04 22:11:25 +0100472 * For non-streaming use, the iv should be initialised on each call
473 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100474 *
Simon Butcher5db13622018-06-04 22:11:25 +0100475 * If you need to retain the contents of the initialisation vector,
476 * you must either save it manually or use the cipher module
477 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100478 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100479 * \warning For the OFB mode, the initialisation vector must be unique
480 * every encryption operation. Reuse of an initialisation vector
481 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100482 *
483 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100485 * \param length The length of the input data.
486 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500487 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100488 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500489 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100490 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100492 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500493 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100494 *
495 * \return \c 0 on success.
496 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200497MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100498int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
499 size_t length,
500 size_t *iv_off,
501 unsigned char iv[16],
502 const unsigned char *input,
503 unsigned char *output);
Simon Butcher76a5b222018-04-22 22:57:27 +0100504
505#endif /* MBEDTLS_CIPHER_MODE_OFB */
506
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100508/**
Rose Zadik7f441272018-01-22 11:48:23 +0000509 * \brief This function performs an AES-CTR encryption or decryption
510 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000511 *
Rose Zadik7f441272018-01-22 11:48:23 +0000512 * Due to the nature of CTR, you must use the same key schedule
513 * for both encryption and decryption operations. Therefore, you
514 * must use the context initialized with mbedtls_aes_setkey_enc()
515 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000516 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100517 * \warning You must never reuse a nonce value with the same key. Doing so
518 * would void the encryption for the two messages encrypted with
519 * the same nonce and key.
520 *
521 * There are two common strategies for managing nonces with CTR:
522 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200523 * 1. You can handle everything as a single message processed over
524 * successive calls to this function. In that case, you want to
525 * set \p nonce_counter and \p nc_off to 0 for the first call, and
526 * then preserve the values of \p nonce_counter, \p nc_off and \p
527 * stream_block across calls to this function as they will be
528 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100529 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200530 * With this strategy, you must not encrypt more than 2**128
531 * blocks of data with the same key.
532 *
533 * 2. You can encrypt separate messages by dividing the \p
534 * nonce_counter buffer in two areas: the first one used for a
535 * per-message nonce, handled by yourself, and the second one
536 * updated by this function internally.
537 *
538 * For example, you might reserve the first 12 bytes for the
539 * per-message nonce, and the last 4 bytes for internal use. In that
540 * case, before calling this function on a new message you need to
541 * set the first 12 bytes of \p nonce_counter to your chosen nonce
542 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
543 * stream_block to be ignored). That way, you can encrypt at most
544 * 2**96 messages of up to 2**32 blocks each with the same key.
545 *
546 * The per-message nonce (or information sufficient to reconstruct
547 * it) needs to be communicated with the ciphertext and must be unique.
548 * The recommended way to ensure uniqueness is to use a message
549 * counter. An alternative is to generate random nonces, but this
550 * limits the number of messages that can be securely encrypted:
551 * for example, with 96-bit random nonces, you should not encrypt
552 * more than 2**32 messages with the same key.
553 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100554 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200555 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000556 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200557 * \warning Upon return, \p stream_block contains sensitive data. Its
558 * content must not be written to insecure storage and should be
559 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000560 *
Rose Zadik7f441272018-01-22 11:48:23 +0000561 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500562 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000563 * \param length The length of the input data.
564 * \param nc_off The offset in the current \p stream_block, for
565 * resuming within the current cipher stream. The
566 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000568 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500569 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000570 * \param stream_block The saved stream block for resuming. This is
571 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500572 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000573 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500574 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000575 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500576 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000577 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100578 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000579 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200580MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100581int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
582 size_t length,
583 size_t *nc_off,
584 unsigned char nonce_counter[16],
585 unsigned char stream_block[16],
586 const unsigned char *input,
587 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200588#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200589
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200590/**
Rose Zadik7f441272018-01-22 11:48:23 +0000591 * \brief Internal AES block encryption function. This is only
592 * exposed to allow overriding it using
593 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200594 *
Rose Zadik7f441272018-01-22 11:48:23 +0000595 * \param ctx The AES context to use for encryption.
596 * \param input The plaintext block.
597 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000598 *
Rose Zadik7f441272018-01-22 11:48:23 +0000599 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200600 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200601MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100602int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
603 const unsigned char input[16],
604 unsigned char output[16]);
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200605
606/**
Rose Zadik7f441272018-01-22 11:48:23 +0000607 * \brief Internal AES block decryption function. This is only
608 * exposed to allow overriding it using see
609 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200610 *
Rose Zadik7f441272018-01-22 11:48:23 +0000611 * \param ctx The AES context to use for decryption.
612 * \param input The ciphertext block.
613 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000614 *
Rose Zadik7f441272018-01-22 11:48:23 +0000615 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200616 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200617MBEDTLS_CHECK_RETURN_TYPICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100618int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
619 const unsigned char input[16],
620 unsigned char output[16]);
Andres AGf5bf7182017-03-03 14:09:56 +0000621
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500622#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000623/**
Rose Zadik7f441272018-01-22 11:48:23 +0000624 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100626 * \return \c 0 on success.
627 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000628 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200629MBEDTLS_CHECK_RETURN_CRITICAL
Gilles Peskine449bd832023-01-11 14:50:10 +0100630int mbedtls_aes_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000631
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500632#endif /* MBEDTLS_SELF_TEST */
633
Paul Bakker5121ce52009-01-03 21:22:43 +0000634#ifdef __cplusplus
635}
636#endif
637
638#endif /* aes.h */