blob: 1cd20fe06cb610647cd4dad4dd1b705f68a2ad86 [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 */
Dawid Drozd428cc522018-07-24 10:02:47 +020075typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +000076{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020077 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
Werner Lewis6d719442022-06-13 12:28:07 +010078 size_t MBEDTLS_PRIVATE(rk_offset); /*!< The offset in array elements to AES
79 round keys in the buffer. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020080 uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer can
Rose Zadik7f441272018-01-22 11:48:23 +000081 hold 32 extra Bytes, which can be used for
82 one of the following purposes:
83 <ul><li>Alignment if VIA padlock is
84 used.</li>
85 <li>Simplifying key expansion in the 256-bit
86 case by generating an extra round key.
87 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000088}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000090
Jaeden Amero9366feb2018-05-29 18:55:17 +010091#if defined(MBEDTLS_CIPHER_MODE_XTS)
92/**
93 * \brief The AES XTS context-type definition.
94 */
Dawid Drozd428cc522018-07-24 10:02:47 +020095typedef struct mbedtls_aes_xts_context
Jaeden Amero9366feb2018-05-29 18:55:17 +010096{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020097 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Jaeden Amero9366feb2018-05-29 18:55:17 +010098 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +020099 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Jaeden Amero9366feb2018-05-29 18:55:17 +0100100 computation. */
101} mbedtls_aes_xts_context;
102#endif /* MBEDTLS_CIPHER_MODE_XTS */
103
Ron Eldorb2aacec2017-05-18 16:53:08 +0300104#else /* MBEDTLS_AES_ALT */
105#include "aes_alt.h"
106#endif /* MBEDTLS_AES_ALT */
107
Paul Bakker5121ce52009-01-03 21:22:43 +0000108/**
Rose Zadik7f441272018-01-22 11:48:23 +0000109 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200110 *
Rose Zadik7f441272018-01-22 11:48:23 +0000111 * It must be the first API called before using
112 * the context.
113 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200115 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200117
118/**
Rose Zadik7f441272018-01-22 11:48:23 +0000119 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120 *
Rose Zadik7f441272018-01-22 11:48:23 +0000121 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500122 * If this is \c NULL, this function does nothing.
123 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200124 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200126
Jaeden Amero9366feb2018-05-29 18:55:17 +0100127#if defined(MBEDTLS_CIPHER_MODE_XTS)
128/**
129 * \brief This function initializes the specified AES XTS context.
130 *
131 * It must be the first API called before using
132 * the context.
133 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500134 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100135 */
136void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
137
138/**
139 * \brief This function releases and clears the specified AES XTS context.
140 *
141 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * If this is \c NULL, this function does nothing.
143 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100144 */
145void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
146#endif /* MBEDTLS_CIPHER_MODE_XTS */
147
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200148/**
Rose Zadik7f441272018-01-22 11:48:23 +0000149 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 *
Rose Zadik7f441272018-01-22 11:48:23 +0000151 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500152 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000153 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000155 * \param keybits The size of data passed in bits. Valid options are:
156 * <ul><li>128 bits</li>
157 * <li>192 bits</li>
158 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000159 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100160 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100161 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200163MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200165 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000166
167/**
Rose Zadik7f441272018-01-22 11:48:23 +0000168 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 *
Rose Zadik7f441272018-01-22 11:48:23 +0000170 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500171 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000172 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500173 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000174 * \param keybits The size of data passed. Valid options are:
175 * <ul><li>128 bits</li>
176 * <li>192 bits</li>
177 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000178 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100179 * \return \c 0 on success.
180 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200182MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200184 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Jaeden Amero9366feb2018-05-29 18:55:17 +0100186#if defined(MBEDTLS_CIPHER_MODE_XTS)
187/**
188 * \brief This function prepares an XTS context for encryption and
189 * sets the encryption key.
190 *
191 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500192 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100193 * \param key The encryption key. This is comprised of the XTS key1
194 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500195 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100196 * \param keybits The size of \p key passed in bits. Valid options are:
197 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
198 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
199 *
200 * \return \c 0 on success.
201 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
202 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200203MBEDTLS_CHECK_RETURN_TYPICAL
Jaeden Amero9366feb2018-05-29 18:55:17 +0100204int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
205 const unsigned char *key,
206 unsigned int keybits );
207
208/**
209 * \brief This function prepares an XTS context for decryption and
210 * sets the decryption key.
211 *
212 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100214 * \param key The decryption key. This is comprised of the XTS key1
215 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500216 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100217 * \param keybits The size of \p key passed in bits. Valid options are:
218 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
219 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
220 *
221 * \return \c 0 on success.
222 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
223 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200224MBEDTLS_CHECK_RETURN_TYPICAL
Jaeden Amero9366feb2018-05-29 18:55:17 +0100225int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
226 const unsigned char *key,
227 unsigned int keybits );
228#endif /* MBEDTLS_CIPHER_MODE_XTS */
229
Paul Bakker5121ce52009-01-03 21:22:43 +0000230/**
Rose Zadik7f441272018-01-22 11:48:23 +0000231 * \brief This function performs an AES single-block encryption or
232 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000233 *
Rose Zadik7f441272018-01-22 11:48:23 +0000234 * It performs the operation defined in the \p mode parameter
235 * (encrypt or decrypt), on the input data buffer defined in
236 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000237 *
Rose Zadik7f441272018-01-22 11:48:23 +0000238 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
239 * mbedtls_aes_setkey_dec() must be called before the first
240 * call to this API with the same context.
241 *
242 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500243 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000244 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
245 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500246 * \param input The buffer holding the input data.
247 * It must be readable and at least \c 16 Bytes long.
248 * \param output The buffer where the output data will be written.
249 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000250
251 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200253MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000256 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 unsigned char output[16] );
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000260/**
Rose Zadik7f441272018-01-22 11:48:23 +0000261 * \brief This function performs an AES-CBC encryption or decryption operation
262 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 *
Rose Zadik7f441272018-01-22 11:48:23 +0000264 * It performs the operation defined in the \p mode
265 * parameter (encrypt/decrypt), on the input data buffer defined in
266 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000267 *
Rose Zadik7f441272018-01-22 11:48:23 +0000268 * It can be called as many times as needed, until all the input
269 * data is processed. mbedtls_aes_init(), and either
270 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
271 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000272 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500273 * \note This function operates on full blocks, that is, the input size
274 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000275 *
276 * \note Upon exit, the content of the IV is updated so that you can
277 * call the same function again on the next
278 * block(s) of data and get the same result as if it was
279 * encrypted in one call. This allows a "streaming" usage.
280 * If you need to retain the contents of the IV, you should
281 * either save it manually or use the cipher module instead.
282 *
283 *
284 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500285 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000286 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
287 * #MBEDTLS_AES_DECRYPT.
288 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500289 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000290 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500291 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000292 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500293 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000294 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500295 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000296 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100297 * \return \c 0 on success.
298 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000299 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000300 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200301MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000304 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000306 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200308#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000309
Aorimn5f778012016-06-09 23:22:58 +0200310#if defined(MBEDTLS_CIPHER_MODE_XTS)
311/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100312 * \brief This function performs an AES-XTS encryption or decryption
313 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200314 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100315 * AES-XTS encrypts or decrypts blocks based on their location as
316 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100317 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200318 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100319 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
320 * AES blocks. If the data unit is larger than this, this function
321 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
322 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100323 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500324 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100325 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
326 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500327 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100328 * length between 16 bytes and 2^24 bytes inclusive
329 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100330 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100331 * bytes in little-endian format. For disk encryption, this
332 * is typically the index of the block device sector that
333 * contains the data.
334 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500335 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100336 * input.
337 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500338 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100339 * output.
Aorimn5f778012016-06-09 23:22:58 +0200340 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100341 * \return \c 0 on success.
342 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500343 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100344 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200345 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200346MBEDTLS_CHECK_RETURN_TYPICAL
Jaeden Amero9366feb2018-05-29 18:55:17 +0100347int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
348 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100349 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100350 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100351 const unsigned char *input,
352 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200353#endif /* MBEDTLS_CIPHER_MODE_XTS */
354
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000356/**
Rose Zadik7f441272018-01-22 11:48:23 +0000357 * \brief This function performs an AES-CFB128 encryption or decryption
358 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000359 *
Rose Zadik7f441272018-01-22 11:48:23 +0000360 * It performs the operation defined in the \p mode
361 * parameter (encrypt or decrypt), on the input data buffer
362 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000363 *
Rose Zadik7f441272018-01-22 11:48:23 +0000364 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
365 * regardless of whether you are performing an encryption or decryption
366 * operation, that is, regardless of the \p mode parameter. This is
367 * because CFB mode uses the same key schedule for encryption and
368 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000369 *
Rose Zadik7f441272018-01-22 11:48:23 +0000370 * \note Upon exit, the content of the IV is updated so that you can
371 * call the same function again on the next
372 * block(s) of data and get the same result as if it was
373 * encrypted in one call. This allows a "streaming" usage.
374 * If you need to retain the contents of the
375 * IV, you must either save it manually or use the cipher
376 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000377 *
Rose Zadik7f441272018-01-22 11:48:23 +0000378 *
379 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
382 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500383 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000384 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500385 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000386 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500387 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000388 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500389 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000390 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500391 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000392 *
393 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200395MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200396int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000397 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000398 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000399 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000400 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000401 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000402 unsigned char *output );
403
Paul Bakker9a736322012-11-14 12:39:52 +0000404/**
Rose Zadik7f441272018-01-22 11:48:23 +0000405 * \brief This function performs an AES-CFB8 encryption or decryption
406 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100407 *
Rose Zadik7f441272018-01-22 11:48:23 +0000408 * It performs the operation defined in the \p mode
409 * parameter (encrypt/decrypt), on the input data buffer defined
410 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100411 *
Rose Zadik7f441272018-01-22 11:48:23 +0000412 * Due to the nature of CFB, you must use the same key schedule for
413 * both encryption and decryption operations. Therefore, you must
414 * use the context initialized with mbedtls_aes_setkey_enc() for
415 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000416 *
Rose Zadik7f441272018-01-22 11:48:23 +0000417 * \note Upon exit, the content of the IV is updated so that you can
418 * call the same function again on the next
419 * block(s) of data and get the same result as if it was
420 * encrypted in one call. This allows a "streaming" usage.
421 * If you need to retain the contents of the
422 * IV, you should either save it manually or use the cipher
423 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100424 *
Rose Zadik7f441272018-01-22 11:48:23 +0000425 *
426 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500427 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000428 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
429 * #MBEDTLS_AES_DECRYPT
430 * \param length The length of the input data.
431 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500432 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000433 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500434 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000435 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500436 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000437 *
438 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100439 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200440MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200441int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100442 int mode,
443 size_t length,
444 unsigned char iv[16],
445 const unsigned char *input,
446 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100448
Simon Butcher76a5b222018-04-22 22:57:27 +0100449#if defined(MBEDTLS_CIPHER_MODE_OFB)
450/**
Simon Butcher5db13622018-06-04 22:11:25 +0100451 * \brief This function performs an AES-OFB (Output Feedback Mode)
452 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100453 *
Simon Butcher5db13622018-06-04 22:11:25 +0100454 * For OFB, you must set up the context with
455 * mbedtls_aes_setkey_enc(), regardless of whether you are
456 * performing an encryption or decryption operation. This is
457 * because OFB mode uses the same key schedule for encryption and
458 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100459 *
Simon Butcher5db13622018-06-04 22:11:25 +0100460 * The OFB operation is identical for encryption or decryption,
461 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100462 *
Simon Butcher5db13622018-06-04 22:11:25 +0100463 * \note Upon exit, the content of iv, the Initialisation Vector, is
464 * updated so that you can call the same function again on the next
465 * block(s) of data and get the same result as if it was encrypted
466 * in one call. This allows a "streaming" usage, by initialising
467 * iv_off to 0 before the first call, and preserving its value
468 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100469 *
Simon Butcher5db13622018-06-04 22:11:25 +0100470 * For non-streaming use, the iv should be initialised on each call
471 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100472 *
Simon Butcher5db13622018-06-04 22:11:25 +0100473 * If you need to retain the contents of the initialisation vector,
474 * you must either save it manually or use the cipher module
475 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100476 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100477 * \warning For the OFB mode, the initialisation vector must be unique
478 * every encryption operation. Reuse of an initialisation vector
479 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100480 *
481 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 * \param length The length of the input data.
484 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500485 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100486 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500487 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100488 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500489 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100490 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500491 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100492 *
493 * \return \c 0 on success.
494 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200495MBEDTLS_CHECK_RETURN_TYPICAL
Simon Butcher76a5b222018-04-22 22:57:27 +0100496int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
497 size_t length,
498 size_t *iv_off,
499 unsigned char iv[16],
500 const unsigned char *input,
501 unsigned char *output );
502
503#endif /* MBEDTLS_CIPHER_MODE_OFB */
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100506/**
Rose Zadik7f441272018-01-22 11:48:23 +0000507 * \brief This function performs an AES-CTR encryption or decryption
508 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000509 *
Rose Zadik7f441272018-01-22 11:48:23 +0000510 * Due to the nature of CTR, you must use the same key schedule
511 * for both encryption and decryption operations. Therefore, you
512 * must use the context initialized with mbedtls_aes_setkey_enc()
513 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000514 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100515 * \warning You must never reuse a nonce value with the same key. Doing so
516 * would void the encryption for the two messages encrypted with
517 * the same nonce and key.
518 *
519 * There are two common strategies for managing nonces with CTR:
520 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200521 * 1. You can handle everything as a single message processed over
522 * successive calls to this function. In that case, you want to
523 * set \p nonce_counter and \p nc_off to 0 for the first call, and
524 * then preserve the values of \p nonce_counter, \p nc_off and \p
525 * stream_block across calls to this function as they will be
526 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100527 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200528 * With this strategy, you must not encrypt more than 2**128
529 * blocks of data with the same key.
530 *
531 * 2. You can encrypt separate messages by dividing the \p
532 * nonce_counter buffer in two areas: the first one used for a
533 * per-message nonce, handled by yourself, and the second one
534 * updated by this function internally.
535 *
536 * For example, you might reserve the first 12 bytes for the
537 * per-message nonce, and the last 4 bytes for internal use. In that
538 * case, before calling this function on a new message you need to
539 * set the first 12 bytes of \p nonce_counter to your chosen nonce
540 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
541 * stream_block to be ignored). That way, you can encrypt at most
542 * 2**96 messages of up to 2**32 blocks each with the same key.
543 *
544 * The per-message nonce (or information sufficient to reconstruct
545 * it) needs to be communicated with the ciphertext and must be unique.
546 * The recommended way to ensure uniqueness is to use a message
547 * counter. An alternative is to generate random nonces, but this
548 * limits the number of messages that can be securely encrypted:
549 * for example, with 96-bit random nonces, you should not encrypt
550 * more than 2**32 messages with the same key.
551 *
Tom Cosgrove1e211442022-05-26 11:51:00 +0100552 * Note that for both strategies, sizes are measured in blocks and
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200553 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000554 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200555 * \warning Upon return, \p stream_block contains sensitive data. Its
556 * content must not be written to insecure storage and should be
557 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000558 *
Rose Zadik7f441272018-01-22 11:48:23 +0000559 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500560 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000561 * \param length The length of the input data.
562 * \param nc_off The offset in the current \p stream_block, for
563 * resuming within the current cipher stream. The
564 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000566 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500567 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000568 * \param stream_block The saved stream block for resuming. This is
569 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500570 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000571 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500572 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000573 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500574 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000575 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100576 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000577 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200578MBEDTLS_CHECK_RETURN_TYPICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000580 size_t length,
581 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000582 unsigned char nonce_counter[16],
583 unsigned char stream_block[16],
584 const unsigned char *input,
585 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200587
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200588/**
Rose Zadik7f441272018-01-22 11:48:23 +0000589 * \brief Internal AES block encryption function. This is only
590 * exposed to allow overriding it using
591 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200592 *
Rose Zadik7f441272018-01-22 11:48:23 +0000593 * \param ctx The AES context to use for encryption.
594 * \param input The plaintext block.
595 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000596 *
Rose Zadik7f441272018-01-22 11:48:23 +0000597 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200598 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200599MBEDTLS_CHECK_RETURN_TYPICAL
Andres AGf5bf7182017-03-03 14:09:56 +0000600int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
601 const unsigned char input[16],
602 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200603
604/**
Rose Zadik7f441272018-01-22 11:48:23 +0000605 * \brief Internal AES block decryption function. This is only
606 * exposed to allow overriding it using see
607 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200608 *
Rose Zadik7f441272018-01-22 11:48:23 +0000609 * \param ctx The AES context to use for decryption.
610 * \param input The ciphertext block.
611 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000612 *
Rose Zadik7f441272018-01-22 11:48:23 +0000613 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200614 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200615MBEDTLS_CHECK_RETURN_TYPICAL
Andres AGf5bf7182017-03-03 14:09:56 +0000616int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
617 const unsigned char input[16],
618 unsigned char output[16] );
619
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500620#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000621/**
Rose Zadik7f441272018-01-22 11:48:23 +0000622 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000623 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100624 * \return \c 0 on success.
625 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000626 */
Gilles Peskinee41803a2021-09-23 17:35:37 +0200627MBEDTLS_CHECK_RETURN_CRITICAL
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200628int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000629
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500630#endif /* MBEDTLS_SELF_TEST */
631
Paul Bakker5121ce52009-01-03 21:22:43 +0000632#ifdef __cplusplus
633}
634#endif
635
636#endif /* aes.h */