blob: eb75935c1f81d5af4ef7221a0fb65fe7efe68b0e [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"
Paul Bakker90995b52013-06-24 19:20:35 +020045
Rich Evans00ab4702015-02-06 13:43:58 +000046#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020047#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000048
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010049/* padlock.c and aesni.c rely on these values! */
Rose Zadik7f441272018-01-22 11:48:23 +000050#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
51#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
Paul Bakker5121ce52009-01-03 21:22:43 +000052
Andres Amaya Garciac5380642017-11-28 19:57:51 +000053/* Error codes in range 0x0020-0x0022 */
Gilles Peskined2971572021-07-26 18:48:10 +020054/** Invalid key length. */
55#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
56/** Invalid data input length. */
57#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
Paul Bakker2b222c82009-07-27 21:03:45 +000058
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000059/* Error codes in range 0x0021-0x0025 */
Gilles Peskined2971572021-07-26 18:48:10 +020060/** Invalid input data. */
61#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
Ron Eldor9924bdc2018-10-04 10:59:13 +030062
Andres AGf5bf7182017-03-03 14:09:56 +000063#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
64 !defined(inline) && !defined(__cplusplus)
65#define inline __inline
66#endif
67
Paul Bakker407a0da2013-06-27 14:29:21 +020068#ifdef __cplusplus
69extern "C" {
70#endif
71
Ron Eldorb2aacec2017-05-18 16:53:08 +030072#if !defined(MBEDTLS_AES_ALT)
73// Regular implementation
74//
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Rose Zadik7f441272018-01-22 11:48:23 +000077 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000078 */
Dawid Drozd428cc522018-07-24 10:02:47 +020079typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +000080{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020081 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
82 uint32_t *MBEDTLS_PRIVATE(rk); /*!< AES round keys. */
83 uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can
Rose Zadik7f441272018-01-22 11:48:23 +000084 hold 32 extra Bytes, which can be used for
85 one of the following purposes:
86 <ul><li>Alignment if VIA padlock is
87 used.</li>
88 <li>Simplifying key expansion in the 256-bit
89 case by generating an extra round key.
90 </li></ul> */
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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020098typedef struct mbedtls_aes_xts_context
Jaeden Amero9366feb2018-05-29 18:55:17 +010099{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200100 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Jaeden Amero9366feb2018-05-29 18:55:17 +0100101 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200102 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Jaeden Amero9366feb2018-05-29 18:55:17 +0100103 computation. */
104} mbedtls_aes_xts_context;
105#endif /* MBEDTLS_CIPHER_MODE_XTS */
106
Ron Eldorb2aacec2017-05-18 16:53:08 +0300107#else /* MBEDTLS_AES_ALT */
108#include "aes_alt.h"
109#endif /* MBEDTLS_AES_ALT */
110
Paul Bakker5121ce52009-01-03 21:22:43 +0000111/**
Rose Zadik7f441272018-01-22 11:48:23 +0000112 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200113 *
Rose Zadik7f441272018-01-22 11:48:23 +0000114 * It must be the first API called before using
115 * the context.
116 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500117 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120
121/**
Rose Zadik7f441272018-01-22 11:48:23 +0000122 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200123 *
Rose Zadik7f441272018-01-22 11:48:23 +0000124 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500125 * If this is \c NULL, this function does nothing.
126 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200127 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200129
Jaeden Amero9366feb2018-05-29 18:55:17 +0100130#if defined(MBEDTLS_CIPHER_MODE_XTS)
131/**
132 * \brief This function initializes the specified AES XTS context.
133 *
134 * It must be the first API called before using
135 * the context.
136 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100138 */
139void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
140
141/**
142 * \brief This function releases and clears the specified AES XTS context.
143 *
144 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500145 * If this is \c NULL, this function does nothing.
146 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100147 */
148void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
149#endif /* MBEDTLS_CIPHER_MODE_XTS */
150
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200151/**
Rose Zadik7f441272018-01-22 11:48:23 +0000152 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 *
Rose Zadik7f441272018-01-22 11:48:23 +0000154 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500155 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000156 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500157 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000158 * \param keybits The size of data passed in bits. Valid options are:
159 * <ul><li>128 bits</li>
160 * <li>192 bits</li>
161 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000162 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100163 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100164 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200166MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200168 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
Rose Zadik7f441272018-01-22 11:48:23 +0000171 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 *
Rose Zadik7f441272018-01-22 11:48:23 +0000173 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000175 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500176 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000177 * \param keybits The size of data passed. Valid options are:
178 * <ul><li>128 bits</li>
179 * <li>192 bits</li>
180 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000181 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200185MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200187 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Jaeden Amero9366feb2018-05-29 18:55:17 +0100189#if defined(MBEDTLS_CIPHER_MODE_XTS)
190/**
191 * \brief This function prepares an XTS context for encryption and
192 * sets the encryption key.
193 *
194 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100196 * \param key The encryption key. This is comprised of the XTS key1
197 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500198 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100199 * \param keybits The size of \p key passed in bits. Valid options are:
200 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
201 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
202 *
203 * \return \c 0 on success.
204 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
205 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200206MBEDTLS_CHECK_RETURN
Jaeden Amero9366feb2018-05-29 18:55:17 +0100207int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
208 const unsigned char *key,
209 unsigned int keybits );
210
211/**
212 * \brief This function prepares an XTS context for decryption and
213 * sets the decryption key.
214 *
215 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100217 * \param key The decryption key. This is comprised of the XTS key1
218 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500219 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100220 * \param keybits The size of \p key passed in bits. Valid options are:
221 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
222 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
223 *
224 * \return \c 0 on success.
225 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
226 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200227MBEDTLS_CHECK_RETURN
Jaeden Amero9366feb2018-05-29 18:55:17 +0100228int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
229 const unsigned char *key,
230 unsigned int keybits );
231#endif /* MBEDTLS_CIPHER_MODE_XTS */
232
Paul Bakker5121ce52009-01-03 21:22:43 +0000233/**
Rose Zadik7f441272018-01-22 11:48:23 +0000234 * \brief This function performs an AES single-block encryption or
235 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 *
Rose Zadik7f441272018-01-22 11:48:23 +0000237 * It performs the operation defined in the \p mode parameter
238 * (encrypt or decrypt), on the input data buffer defined in
239 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000240 *
Rose Zadik7f441272018-01-22 11:48:23 +0000241 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
242 * mbedtls_aes_setkey_dec() must be called before the first
243 * call to this API with the same context.
244 *
245 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000247 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
248 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500249 * \param input The buffer holding the input data.
250 * It must be readable and at least \c 16 Bytes long.
251 * \param output The buffer where the output data will be written.
252 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000253
254 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200256MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200257int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000259 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 unsigned char output[16] );
261
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000263/**
Rose Zadik7f441272018-01-22 11:48:23 +0000264 * \brief This function performs an AES-CBC encryption or decryption operation
265 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 *
Rose Zadik7f441272018-01-22 11:48:23 +0000267 * It performs the operation defined in the \p mode
268 * parameter (encrypt/decrypt), on the input data buffer defined in
269 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000270 *
Rose Zadik7f441272018-01-22 11:48:23 +0000271 * It can be called as many times as needed, until all the input
272 * data is processed. mbedtls_aes_init(), and either
273 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
274 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000275 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500276 * \note This function operates on full blocks, that is, the input size
277 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000278 *
279 * \note Upon exit, the content of the IV is updated so that you can
280 * call the same function again on the next
281 * block(s) of data and get the same result as if it was
282 * encrypted in one call. This allows a "streaming" usage.
283 * If you need to retain the contents of the IV, you should
284 * either save it manually or use the cipher module instead.
285 *
286 *
287 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000289 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
290 * #MBEDTLS_AES_DECRYPT.
291 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000293 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500294 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000295 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500296 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000297 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500298 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000299 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100300 * \return \c 0 on success.
301 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000302 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200304MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000307 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000309 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000310 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000312
Aorimn5f778012016-06-09 23:22:58 +0200313#if defined(MBEDTLS_CIPHER_MODE_XTS)
314/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100315 * \brief This function performs an AES-XTS encryption or decryption
316 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200317 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100318 * AES-XTS encrypts or decrypts blocks based on their location as
319 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100320 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200321 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100322 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
323 * AES blocks. If the data unit is larger than this, this function
324 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
325 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100326 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100328 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
329 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500330 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100331 * length between 16 bytes and 2^24 bytes inclusive
332 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100333 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100334 * bytes in little-endian format. For disk encryption, this
335 * is typically the index of the block device sector that
336 * contains the data.
337 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100339 * input.
340 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100342 * output.
Aorimn5f778012016-06-09 23:22:58 +0200343 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100344 * \return \c 0 on success.
345 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500346 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100347 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200348 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200349MBEDTLS_CHECK_RETURN
Jaeden Amero9366feb2018-05-29 18:55:17 +0100350int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
351 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100352 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100353 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100354 const unsigned char *input,
355 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200356#endif /* MBEDTLS_CIPHER_MODE_XTS */
357
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200358#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000359/**
Rose Zadik7f441272018-01-22 11:48:23 +0000360 * \brief This function performs an AES-CFB128 encryption or decryption
361 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000362 *
Rose Zadik7f441272018-01-22 11:48:23 +0000363 * It performs the operation defined in the \p mode
364 * parameter (encrypt or decrypt), on the input data buffer
365 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000366 *
Rose Zadik7f441272018-01-22 11:48:23 +0000367 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
368 * regardless of whether you are performing an encryption or decryption
369 * operation, that is, regardless of the \p mode parameter. This is
370 * because CFB mode uses the same key schedule for encryption and
371 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000372 *
Rose Zadik7f441272018-01-22 11:48:23 +0000373 * \note Upon exit, the content of the IV is updated so that you can
374 * call the same function again on the next
375 * block(s) of data and get the same result as if it was
376 * encrypted in one call. This allows a "streaming" usage.
377 * If you need to retain the contents of the
378 * IV, you must either save it manually or use the cipher
379 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000380 *
Rose Zadik7f441272018-01-22 11:48:23 +0000381 *
382 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000384 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
385 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000387 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500388 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000389 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500390 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000391 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500392 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000393 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500394 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000395 *
396 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200398MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200399int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000401 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000402 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000403 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000404 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000405 unsigned char *output );
406
Paul Bakker9a736322012-11-14 12:39:52 +0000407/**
Rose Zadik7f441272018-01-22 11:48:23 +0000408 * \brief This function performs an AES-CFB8 encryption or decryption
409 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100410 *
Rose Zadik7f441272018-01-22 11:48:23 +0000411 * It performs the operation defined in the \p mode
412 * parameter (encrypt/decrypt), on the input data buffer defined
413 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100414 *
Rose Zadik7f441272018-01-22 11:48:23 +0000415 * Due to the nature of CFB, you must use the same key schedule for
416 * both encryption and decryption operations. Therefore, you must
417 * use the context initialized with mbedtls_aes_setkey_enc() for
418 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000419 *
Rose Zadik7f441272018-01-22 11:48:23 +0000420 * \note Upon exit, the content of the IV is updated so that you can
421 * call the same function again on the next
422 * block(s) of data and get the same result as if it was
423 * encrypted in one call. This allows a "streaming" usage.
424 * If you need to retain the contents of the
425 * IV, you should either save it manually or use the cipher
426 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100427 *
Rose Zadik7f441272018-01-22 11:48:23 +0000428 *
429 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000431 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
432 * #MBEDTLS_AES_DECRYPT
433 * \param length The length of the input data.
434 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500435 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000436 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500437 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000438 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500439 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000440 *
441 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100442 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200443MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200444int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100445 int mode,
446 size_t length,
447 unsigned char iv[16],
448 const unsigned char *input,
449 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100451
Simon Butcher76a5b222018-04-22 22:57:27 +0100452#if defined(MBEDTLS_CIPHER_MODE_OFB)
453/**
Simon Butcher5db13622018-06-04 22:11:25 +0100454 * \brief This function performs an AES-OFB (Output Feedback Mode)
455 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100456 *
Simon Butcher5db13622018-06-04 22:11:25 +0100457 * For OFB, you must set up the context with
458 * mbedtls_aes_setkey_enc(), regardless of whether you are
459 * performing an encryption or decryption operation. This is
460 * because OFB mode uses the same key schedule for encryption and
461 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100462 *
Simon Butcher5db13622018-06-04 22:11:25 +0100463 * The OFB operation is identical for encryption or decryption,
464 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100465 *
Simon Butcher5db13622018-06-04 22:11:25 +0100466 * \note Upon exit, the content of iv, the Initialisation Vector, is
467 * updated so that you can call the same function again on the next
468 * block(s) of data and get the same result as if it was encrypted
469 * in one call. This allows a "streaming" usage, by initialising
470 * iv_off to 0 before the first call, and preserving its value
471 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100472 *
Simon Butcher5db13622018-06-04 22:11:25 +0100473 * For non-streaming use, the iv should be initialised on each call
474 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100475 *
Simon Butcher5db13622018-06-04 22:11:25 +0100476 * If you need to retain the contents of the initialisation vector,
477 * you must either save it manually or use the cipher module
478 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100479 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100480 * \warning For the OFB mode, the initialisation vector must be unique
481 * every encryption operation. Reuse of an initialisation vector
482 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 *
484 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100486 * \param length The length of the input data.
487 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500488 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100489 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500490 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100491 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500492 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100493 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500494 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100495 *
496 * \return \c 0 on success.
497 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200498MBEDTLS_CHECK_RETURN
Simon Butcher76a5b222018-04-22 22:57:27 +0100499int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
500 size_t length,
501 size_t *iv_off,
502 unsigned char iv[16],
503 const unsigned char *input,
504 unsigned char *output );
505
506#endif /* MBEDTLS_CIPHER_MODE_OFB */
507
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200508#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100509/**
Rose Zadik7f441272018-01-22 11:48:23 +0000510 * \brief This function performs an AES-CTR encryption or decryption
511 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000512 *
Rose Zadik7f441272018-01-22 11:48:23 +0000513 * This function performs the operation defined in the \p mode
514 * parameter (encrypt/decrypt), on the input data buffer
515 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000516 *
Rose Zadik7f441272018-01-22 11:48:23 +0000517 * Due to the nature of CTR, you must use the same key schedule
518 * for both encryption and decryption operations. Therefore, you
519 * must use the context initialized with mbedtls_aes_setkey_enc()
520 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000521 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100522 * \warning You must never reuse a nonce value with the same key. Doing so
523 * would void the encryption for the two messages encrypted with
524 * the same nonce and key.
525 *
526 * There are two common strategies for managing nonces with CTR:
527 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200528 * 1. You can handle everything as a single message processed over
529 * successive calls to this function. In that case, you want to
530 * set \p nonce_counter and \p nc_off to 0 for the first call, and
531 * then preserve the values of \p nonce_counter, \p nc_off and \p
532 * stream_block across calls to this function as they will be
533 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100534 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200535 * With this strategy, you must not encrypt more than 2**128
536 * blocks of data with the same key.
537 *
538 * 2. You can encrypt separate messages by dividing the \p
539 * nonce_counter buffer in two areas: the first one used for a
540 * per-message nonce, handled by yourself, and the second one
541 * updated by this function internally.
542 *
543 * For example, you might reserve the first 12 bytes for the
544 * per-message nonce, and the last 4 bytes for internal use. In that
545 * case, before calling this function on a new message you need to
546 * set the first 12 bytes of \p nonce_counter to your chosen nonce
547 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
548 * stream_block to be ignored). That way, you can encrypt at most
549 * 2**96 messages of up to 2**32 blocks each with the same key.
550 *
551 * The per-message nonce (or information sufficient to reconstruct
552 * it) needs to be communicated with the ciphertext and must be unique.
553 * The recommended way to ensure uniqueness is to use a message
554 * counter. An alternative is to generate random nonces, but this
555 * limits the number of messages that can be securely encrypted:
556 * for example, with 96-bit random nonces, you should not encrypt
557 * more than 2**32 messages with the same key.
558 *
559 * Note that for both stategies, sizes are measured in blocks and
560 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000561 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200562 * \warning Upon return, \p stream_block contains sensitive data. Its
563 * content must not be written to insecure storage and should be
564 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000565 *
Rose Zadik7f441272018-01-22 11:48:23 +0000566 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000568 * \param length The length of the input data.
569 * \param nc_off The offset in the current \p stream_block, for
570 * resuming within the current cipher stream. The
571 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500572 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000573 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500574 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000575 * \param stream_block The saved stream block for resuming. This is
576 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500577 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000578 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500579 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000580 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500581 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000582 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100583 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000584 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200585MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000587 size_t length,
588 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000589 unsigned char nonce_counter[16],
590 unsigned char stream_block[16],
591 const unsigned char *input,
592 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200593#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200594
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200595/**
Rose Zadik7f441272018-01-22 11:48:23 +0000596 * \brief Internal AES block encryption function. This is only
597 * exposed to allow overriding it using
598 * \c MBEDTLS_AES_ENCRYPT_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 encryption.
601 * \param input The plaintext block.
602 * \param output The output (ciphertext) 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 Peskine7820a572021-07-07 21:08:28 +0200606MBEDTLS_CHECK_RETURN
Andres AGf5bf7182017-03-03 14:09:56 +0000607int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
608 const unsigned char input[16],
609 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200610
611/**
Rose Zadik7f441272018-01-22 11:48:23 +0000612 * \brief Internal AES block decryption function. This is only
613 * exposed to allow overriding it using see
614 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200615 *
Rose Zadik7f441272018-01-22 11:48:23 +0000616 * \param ctx The AES context to use for decryption.
617 * \param input The ciphertext block.
618 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000619 *
Rose Zadik7f441272018-01-22 11:48:23 +0000620 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200621 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200622MBEDTLS_CHECK_RETURN
Andres AGf5bf7182017-03-03 14:09:56 +0000623int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
624 const unsigned char input[16],
625 unsigned char output[16] );
626
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500627#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000628/**
Rose Zadik7f441272018-01-22 11:48:23 +0000629 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000630 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100631 * \return \c 0 on success.
632 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000633 */
Gilles Peskine7820a572021-07-07 21:08:28 +0200634MBEDTLS_CHECK_RETURN
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000636
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500637#endif /* MBEDTLS_SELF_TEST */
638
Paul Bakker5121ce52009-01-03 21:22:43 +0000639#ifdef __cplusplus
640}
641#endif
642
643#endif /* aes.h */