blob: 622ac3ce6047dc0dc6fa3a54b80e44d5ead56aac [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! */
Mateusz Starzyk16fec332021-07-22 16:43:35 +020050/** AES encryption. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020051#define MBEDTLS_AES_ENCRYPT 1
Mateusz Starzyk16fec332021-07-22 16:43:35 +020052/** AES decryption. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020053#define MBEDTLS_AES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Andres Amaya Garciac5380642017-11-28 19:57:51 +000055/* Error codes in range 0x0020-0x0022 */
Gilles Peskined2971572021-07-26 18:48:10 +020056/** Invalid key length. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020057#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020
Gilles Peskined2971572021-07-26 18:48:10 +020058/** Invalid data input length. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020059#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022
Paul Bakker2b222c82009-07-27 21:03:45 +000060
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000061/* Error codes in range 0x0021-0x0025 */
Gilles Peskined2971572021-07-26 18:48:10 +020062/** Invalid input data. */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020063#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021
Ron Eldor9924bdc2018-10-04 10:59:13 +030064
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020065#if (defined(__ARMCC_VERSION) || defined(_MSC_VER)) && !defined(inline) && \
66 !defined(__cplusplus)
67# define inline __inline
Andres AGf5bf7182017-03-03 14:09:56 +000068#endif
69
Paul Bakker407a0da2013-06-27 14:29:21 +020070#ifdef __cplusplus
71extern "C" {
72#endif
73
Ron Eldorb2aacec2017-05-18 16:53:08 +030074#if !defined(MBEDTLS_AES_ALT)
75// Regular implementation
76//
77
Paul Bakker5121ce52009-01-03 21:22:43 +000078/**
Rose Zadik7f441272018-01-22 11:48:23 +000079 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020081typedef struct mbedtls_aes_context {
82 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
83 uint32_t *MBEDTLS_PRIVATE(rk); /*!< AES round keys. */
84 uint32_t MBEDTLS_PRIVATE(buf)[68]; /*!< Unaligned data buffer. This buffer
85 can hold 32 extra Bytes, which can be used for one of
86 the following purposes: <ul><li>Alignment if VIA
87 padlock is used.</li> <li>Simplifying key expansion
88 in the 256-bit case by generating an extra round key.
89 </li></ul> */
90} mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020092# if defined(MBEDTLS_CIPHER_MODE_XTS)
Jaeden Amero9366feb2018-05-29 18:55:17 +010093/**
94 * \brief The AES XTS context-type definition.
95 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +020096typedef struct mbedtls_aes_xts_context {
97 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for
98 AES block encryption or decryption. */
99 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for
100 tweak computation. */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100101} mbedtls_aes_xts_context;
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200102# endif /* MBEDTLS_CIPHER_MODE_XTS */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100103
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200104#else /* MBEDTLS_AES_ALT */
105# include "aes_alt.h"
Ron Eldorb2aacec2017-05-18 16:53:08 +0300106#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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200136void mbedtls_aes_xts_init(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100137
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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200145void mbedtls_aes_xts_free(mbedtls_aes_xts_context *ctx);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100146#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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200163int mbedtls_aes_setkey_enc(mbedtls_aes_context *ctx,
164 const unsigned char *key,
165 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200182int mbedtls_aes_setkey_dec(mbedtls_aes_context *ctx,
183 const unsigned char *key,
184 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 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200203int mbedtls_aes_xts_setkey_enc(mbedtls_aes_xts_context *ctx,
204 const unsigned char *key,
205 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100206
207/**
208 * \brief This function prepares an XTS context for decryption and
209 * sets the decryption key.
210 *
211 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500212 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100213 * \param key The decryption key. This is comprised of the XTS key1
214 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500215 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100216 * \param keybits The size of \p key passed in bits. Valid options are:
217 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
218 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
219 *
220 * \return \c 0 on success.
221 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
222 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200223int mbedtls_aes_xts_setkey_dec(mbedtls_aes_xts_context *ctx,
224 const unsigned char *key,
225 unsigned int keybits);
Jaeden Amero9366feb2018-05-29 18:55:17 +0100226#endif /* MBEDTLS_CIPHER_MODE_XTS */
227
Paul Bakker5121ce52009-01-03 21:22:43 +0000228/**
Rose Zadik7f441272018-01-22 11:48:23 +0000229 * \brief This function performs an AES single-block encryption or
230 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000231 *
Rose Zadik7f441272018-01-22 11:48:23 +0000232 * It performs the operation defined in the \p mode parameter
233 * (encrypt or decrypt), on the input data buffer defined in
234 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000235 *
Rose Zadik7f441272018-01-22 11:48:23 +0000236 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
237 * mbedtls_aes_setkey_dec() must be called before the first
238 * call to this API with the same context.
239 *
240 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500241 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000242 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
243 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500244 * \param input The buffer holding the input data.
245 * It must be readable and at least \c 16 Bytes long.
246 * \param output The buffer where the output data will be written.
247 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000248
249 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200251int mbedtls_aes_crypt_ecb(mbedtls_aes_context *ctx,
252 int mode,
253 const unsigned char input[16],
254 unsigned char output[16]);
Paul Bakker5121ce52009-01-03 21:22:43 +0000255
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000257/**
Rose Zadik7f441272018-01-22 11:48:23 +0000258 * \brief This function performs an AES-CBC encryption or decryption operation
259 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 *
Rose Zadik7f441272018-01-22 11:48:23 +0000261 * It performs the operation defined in the \p mode
262 * parameter (encrypt/decrypt), on the input data buffer defined in
263 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000264 *
Rose Zadik7f441272018-01-22 11:48:23 +0000265 * It can be called as many times as needed, until all the input
266 * data is processed. mbedtls_aes_init(), and either
267 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
268 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000269 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500270 * \note This function operates on full blocks, that is, the input size
271 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000272 *
273 * \note Upon exit, the content of the IV is updated so that you can
274 * call the same function again on the next
275 * block(s) of data and get the same result as if it was
276 * encrypted in one call. This allows a "streaming" usage.
277 * If you need to retain the contents of the IV, you should
278 * either save it manually or use the cipher module instead.
279 *
280 *
281 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500282 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000283 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
284 * #MBEDTLS_AES_DECRYPT.
285 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000287 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000289 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000291 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000293 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100294 * \return \c 0 on success.
295 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000296 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200298int mbedtls_aes_crypt_cbc(mbedtls_aes_context *ctx,
299 int mode,
300 size_t length,
301 unsigned char iv[16],
302 const unsigned char *input,
303 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200304#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
Aorimn5f778012016-06-09 23:22:58 +0200306#if defined(MBEDTLS_CIPHER_MODE_XTS)
307/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100308 * \brief This function performs an AES-XTS encryption or decryption
309 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200310 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100311 * AES-XTS encrypts or decrypts blocks based on their location as
312 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100313 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200314 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100315 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
316 * AES blocks. If the data unit is larger than this, this function
317 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
318 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100319 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500320 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100321 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
322 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500323 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100324 * length between 16 bytes and 2^24 bytes inclusive
325 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100326 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100327 * bytes in little-endian format. For disk encryption, this
328 * is typically the index of the block device sector that
329 * contains the data.
330 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500331 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100332 * input.
333 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500334 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100335 * output.
Aorimn5f778012016-06-09 23:22:58 +0200336 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100337 * \return \c 0 on success.
338 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500339 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100340 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200341 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200342int mbedtls_aes_crypt_xts(mbedtls_aes_xts_context *ctx,
343 int mode,
344 size_t length,
345 const unsigned char data_unit[16],
346 const unsigned char *input,
347 unsigned char *output);
Aorimn5f778012016-06-09 23:22:58 +0200348#endif /* MBEDTLS_CIPHER_MODE_XTS */
349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000351/**
Rose Zadik7f441272018-01-22 11:48:23 +0000352 * \brief This function performs an AES-CFB128 encryption or decryption
353 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000354 *
Rose Zadik7f441272018-01-22 11:48:23 +0000355 * It performs the operation defined in the \p mode
356 * parameter (encrypt or decrypt), on the input data buffer
357 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000358 *
Rose Zadik7f441272018-01-22 11:48:23 +0000359 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
360 * regardless of whether you are performing an encryption or decryption
361 * operation, that is, regardless of the \p mode parameter. This is
362 * because CFB mode uses the same key schedule for encryption and
363 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000364 *
Rose Zadik7f441272018-01-22 11:48:23 +0000365 * \note Upon exit, the content of the IV is updated so that you can
366 * call the same function again on the next
367 * block(s) of data and get the same result as if it was
368 * encrypted in one call. This allows a "streaming" usage.
369 * If you need to retain the contents of the
370 * IV, you must either save it manually or use the cipher
371 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000372 *
Rose Zadik7f441272018-01-22 11:48:23 +0000373 *
374 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500375 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000376 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
377 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000379 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000383 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000385 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000387 *
388 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200390int mbedtls_aes_crypt_cfb128(mbedtls_aes_context *ctx,
391 int mode,
392 size_t length,
393 size_t *iv_off,
394 unsigned char iv[16],
395 const unsigned char *input,
396 unsigned char *output);
Paul Bakker5121ce52009-01-03 21:22:43 +0000397
Paul Bakker9a736322012-11-14 12:39:52 +0000398/**
Rose Zadik7f441272018-01-22 11:48:23 +0000399 * \brief This function performs an AES-CFB8 encryption or decryption
400 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100401 *
Rose Zadik7f441272018-01-22 11:48:23 +0000402 * It performs the operation defined in the \p mode
403 * parameter (encrypt/decrypt), on the input data buffer defined
404 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100405 *
Rose Zadik7f441272018-01-22 11:48:23 +0000406 * Due to the nature of CFB, you must use the same key schedule for
407 * both encryption and decryption operations. Therefore, you must
408 * use the context initialized with mbedtls_aes_setkey_enc() for
409 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000410 *
Rose Zadik7f441272018-01-22 11:48:23 +0000411 * \note Upon exit, the content of the IV is updated so that you can
412 * call the same function again on the next
413 * block(s) of data and get the same result as if it was
414 * encrypted in one call. This allows a "streaming" usage.
415 * If you need to retain the contents of the
416 * IV, you should either save it manually or use the cipher
417 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100418 *
Rose Zadik7f441272018-01-22 11:48:23 +0000419 *
420 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500421 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000422 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
423 * #MBEDTLS_AES_DECRYPT
424 * \param length The length of the input data.
425 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000427 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500428 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000429 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000431 *
432 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100433 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200434int mbedtls_aes_crypt_cfb8(mbedtls_aes_context *ctx,
435 int mode,
436 size_t length,
437 unsigned char iv[16],
438 const unsigned char *input,
439 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200440#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100441
Simon Butcher76a5b222018-04-22 22:57:27 +0100442#if defined(MBEDTLS_CIPHER_MODE_OFB)
443/**
Simon Butcher5db13622018-06-04 22:11:25 +0100444 * \brief This function performs an AES-OFB (Output Feedback Mode)
445 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100446 *
Simon Butcher5db13622018-06-04 22:11:25 +0100447 * For OFB, you must set up the context with
448 * mbedtls_aes_setkey_enc(), regardless of whether you are
449 * performing an encryption or decryption operation. This is
450 * because OFB mode uses the same key schedule for encryption and
451 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100452 *
Simon Butcher5db13622018-06-04 22:11:25 +0100453 * The OFB operation is identical for encryption or decryption,
454 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100455 *
Simon Butcher5db13622018-06-04 22:11:25 +0100456 * \note Upon exit, the content of iv, the Initialisation Vector, is
457 * updated so that you can call the same function again on the next
458 * block(s) of data and get the same result as if it was encrypted
459 * in one call. This allows a "streaming" usage, by initialising
460 * iv_off to 0 before the first call, and preserving its value
461 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100462 *
Simon Butcher5db13622018-06-04 22:11:25 +0100463 * For non-streaming use, the iv should be initialised on each call
464 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100465 *
Simon Butcher5db13622018-06-04 22:11:25 +0100466 * If you need to retain the contents of the initialisation vector,
467 * you must either save it manually or use the cipher module
468 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100469 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100470 * \warning For the OFB mode, the initialisation vector must be unique
471 * every encryption operation. Reuse of an initialisation vector
472 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100473 *
474 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500475 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100476 * \param length The length of the input data.
477 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100479 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100481 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100485 *
486 * \return \c 0 on success.
487 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200488int mbedtls_aes_crypt_ofb(mbedtls_aes_context *ctx,
489 size_t length,
490 size_t *iv_off,
491 unsigned char iv[16],
492 const unsigned char *input,
493 unsigned char *output);
Simon Butcher76a5b222018-04-22 22:57:27 +0100494
495#endif /* MBEDTLS_CIPHER_MODE_OFB */
496
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200497#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100498/**
Rose Zadik7f441272018-01-22 11:48:23 +0000499 * \brief This function performs an AES-CTR encryption or decryption
500 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000501 *
Rose Zadik7f441272018-01-22 11:48:23 +0000502 * This function performs the operation defined in the \p mode
503 * parameter (encrypt/decrypt), on the input data buffer
504 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000505 *
Rose Zadik7f441272018-01-22 11:48:23 +0000506 * Due to the nature of CTR, you must use the same key schedule
507 * for both encryption and decryption operations. Therefore, you
508 * must use the context initialized with mbedtls_aes_setkey_enc()
509 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000510 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100511 * \warning You must never reuse a nonce value with the same key. Doing so
512 * would void the encryption for the two messages encrypted with
513 * the same nonce and key.
514 *
515 * There are two common strategies for managing nonces with CTR:
516 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200517 * 1. You can handle everything as a single message processed over
518 * successive calls to this function. In that case, you want to
519 * set \p nonce_counter and \p nc_off to 0 for the first call, and
520 * then preserve the values of \p nonce_counter, \p nc_off and \p
521 * stream_block across calls to this function as they will be
522 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100523 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200524 * With this strategy, you must not encrypt more than 2**128
525 * blocks of data with the same key.
526 *
527 * 2. You can encrypt separate messages by dividing the \p
528 * nonce_counter buffer in two areas: the first one used for a
529 * per-message nonce, handled by yourself, and the second one
530 * updated by this function internally.
531 *
532 * For example, you might reserve the first 12 bytes for the
533 * per-message nonce, and the last 4 bytes for internal use. In that
534 * case, before calling this function on a new message you need to
535 * set the first 12 bytes of \p nonce_counter to your chosen nonce
536 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
537 * stream_block to be ignored). That way, you can encrypt at most
538 * 2**96 messages of up to 2**32 blocks each with the same key.
539 *
540 * The per-message nonce (or information sufficient to reconstruct
541 * it) needs to be communicated with the ciphertext and must be unique.
542 * The recommended way to ensure uniqueness is to use a message
543 * counter. An alternative is to generate random nonces, but this
544 * limits the number of messages that can be securely encrypted:
545 * for example, with 96-bit random nonces, you should not encrypt
546 * more than 2**32 messages with the same key.
547 *
548 * Note that for both stategies, sizes are measured in blocks and
549 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000550 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200551 * \warning Upon return, \p stream_block contains sensitive data. Its
552 * content must not be written to insecure storage and should be
553 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000554 *
Rose Zadik7f441272018-01-22 11:48:23 +0000555 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500556 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000557 * \param length The length of the input data.
558 * \param nc_off The offset in the current \p stream_block, for
559 * resuming within the current cipher stream. The
560 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500561 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000564 * \param stream_block The saved stream block for resuming. This is
565 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500566 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000567 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500568 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000569 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500570 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000571 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100572 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000573 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200574int mbedtls_aes_crypt_ctr(mbedtls_aes_context *ctx,
575 size_t length,
576 size_t *nc_off,
577 unsigned char nonce_counter[16],
578 unsigned char stream_block[16],
579 const unsigned char *input,
580 unsigned char *output);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200582
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200583/**
Rose Zadik7f441272018-01-22 11:48:23 +0000584 * \brief Internal AES block encryption function. This is only
585 * exposed to allow overriding it using
586 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200587 *
Rose Zadik7f441272018-01-22 11:48:23 +0000588 * \param ctx The AES context to use for encryption.
589 * \param input The plaintext block.
590 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000591 *
Rose Zadik7f441272018-01-22 11:48:23 +0000592 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200593 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200594int mbedtls_internal_aes_encrypt(mbedtls_aes_context *ctx,
595 const unsigned char input[16],
596 unsigned char output[16]);
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200597
598/**
Rose Zadik7f441272018-01-22 11:48:23 +0000599 * \brief Internal AES block decryption function. This is only
600 * exposed to allow overriding it using see
601 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200602 *
Rose Zadik7f441272018-01-22 11:48:23 +0000603 * \param ctx The AES context to use for decryption.
604 * \param input The ciphertext block.
605 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000606 *
Rose Zadik7f441272018-01-22 11:48:23 +0000607 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200608 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200609int mbedtls_internal_aes_decrypt(mbedtls_aes_context *ctx,
610 const unsigned char input[16],
611 unsigned char output[16]);
Andres AGf5bf7182017-03-03 14:09:56 +0000612
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500613#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000614/**
Rose Zadik7f441272018-01-22 11:48:23 +0000615 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100617 * \return \c 0 on success.
618 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 */
Mateusz Starzykc0eabdc2021-08-03 14:09:02 +0200620int mbedtls_aes_self_test(int verbose);
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500622#endif /* MBEDTLS_SELF_TEST */
623
Paul Bakker5121ce52009-01-03 21:22:43 +0000624#ifdef __cplusplus
625}
626#endif
627
628#endif /* aes.h */