blob: 005098083533a8afb41cdd8a594cb9422f68704d [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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
55#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000056
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000057/* Error codes in range 0x0021-0x0025 */
58#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 /**< Invalid input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030059
Andres AGf5bf7182017-03-03 14:09:56 +000060#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
61 !defined(inline) && !defined(__cplusplus)
62#define inline __inline
63#endif
64
Paul Bakker407a0da2013-06-27 14:29:21 +020065#ifdef __cplusplus
66extern "C" {
67#endif
68
Ron Eldorb2aacec2017-05-18 16:53:08 +030069#if !defined(MBEDTLS_AES_ALT)
70// Regular implementation
71//
72
Paul Bakker5121ce52009-01-03 21:22:43 +000073/**
Rose Zadik7f441272018-01-22 11:48:23 +000074 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000075 */
Dawid Drozd428cc522018-07-24 10:02:47 +020076typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +000077{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020078 int MBEDTLS_PRIVATE(nr); /*!< The number of rounds. */
79 uint32_t *MBEDTLS_PRIVATE(rk); /*!< AES round keys. */
80 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200164 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
166/**
Rose Zadik7f441272018-01-22 11:48:23 +0000167 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 *
Rose Zadik7f441272018-01-22 11:48:23 +0000169 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500170 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000171 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000173 * \param keybits The size of data passed. Valid options are:
174 * <ul><li>128 bits</li>
175 * <li>192 bits</li>
176 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000177 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100178 * \return \c 0 on success.
179 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200182 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000183
Jaeden Amero9366feb2018-05-29 18:55:17 +0100184#if defined(MBEDTLS_CIPHER_MODE_XTS)
185/**
186 * \brief This function prepares an XTS context for encryption and
187 * sets the encryption key.
188 *
189 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500190 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100191 * \param key The encryption key. This is comprised of the XTS key1
192 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500193 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100194 * \param keybits The size of \p key passed in bits. Valid options are:
195 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
196 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
197 *
198 * \return \c 0 on success.
199 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
200 */
201int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
202 const unsigned char *key,
203 unsigned int keybits );
204
205/**
206 * \brief This function prepares an XTS context for decryption and
207 * sets the decryption key.
208 *
209 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500210 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100211 * \param key The decryption key. This is comprised of the XTS key1
212 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500213 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100214 * \param keybits The size of \p key passed in bits. Valid options are:
215 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
216 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
217 *
218 * \return \c 0 on success.
219 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
220 */
221int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
222 const unsigned char *key,
223 unsigned int keybits );
224#endif /* MBEDTLS_CIPHER_MODE_XTS */
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226/**
Rose Zadik7f441272018-01-22 11:48:23 +0000227 * \brief This function performs an AES single-block encryption or
228 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
Rose Zadik7f441272018-01-22 11:48:23 +0000230 * It performs the operation defined in the \p mode parameter
231 * (encrypt or decrypt), on the input data buffer defined in
232 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000233 *
Rose Zadik7f441272018-01-22 11:48:23 +0000234 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
235 * mbedtls_aes_setkey_dec() must be called before the first
236 * call to this API with the same context.
237 *
238 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500239 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000240 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
241 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500242 * \param input The buffer holding the input data.
243 * It must be readable and at least \c 16 Bytes long.
244 * \param output The buffer where the output data will be written.
245 * It must be writeable and at least \c 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000246
247 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000250 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000251 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 unsigned char output[16] );
253
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000255/**
Rose Zadik7f441272018-01-22 11:48:23 +0000256 * \brief This function performs an AES-CBC encryption or decryption operation
257 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 *
Rose Zadik7f441272018-01-22 11:48:23 +0000259 * It performs the operation defined in the \p mode
260 * parameter (encrypt/decrypt), on the input data buffer defined in
261 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000262 *
Rose Zadik7f441272018-01-22 11:48:23 +0000263 * It can be called as many times as needed, until all the input
264 * data is processed. mbedtls_aes_init(), and either
265 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
266 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000267 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500268 * \note This function operates on full blocks, that is, the input size
269 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000270 *
271 * \note Upon exit, the content of the IV is updated so that you can
272 * call the same function again on the next
273 * block(s) of data and get the same result as if it was
274 * encrypted in one call. This allows a "streaming" usage.
275 * If you need to retain the contents of the IV, you should
276 * either save it manually or use the cipher module instead.
277 *
278 *
279 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500280 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000281 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
282 * #MBEDTLS_AES_DECRYPT.
283 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000285 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500286 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000287 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000289 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000291 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100292 * \return \c 0 on success.
293 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000294 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000295 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000298 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000300 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000303
Aorimn5f778012016-06-09 23:22:58 +0200304#if defined(MBEDTLS_CIPHER_MODE_XTS)
305/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100306 * \brief This function performs an AES-XTS encryption or decryption
307 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200308 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100309 * AES-XTS encrypts or decrypts blocks based on their location as
310 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100311 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200312 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100313 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
314 * AES blocks. If the data unit is larger than this, this function
315 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
316 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100317 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500318 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100319 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
320 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500321 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100322 * length between 16 bytes and 2^24 bytes inclusive
323 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100324 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100325 * bytes in little-endian format. For disk encryption, this
326 * is typically the index of the block device sector that
327 * contains the data.
328 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500329 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100330 * input.
331 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500332 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100333 * output.
Aorimn5f778012016-06-09 23:22:58 +0200334 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100335 * \return \c 0 on success.
336 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500337 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100338 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200339 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100340int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
341 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100342 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100343 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100344 const unsigned char *input,
345 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200346#endif /* MBEDTLS_CIPHER_MODE_XTS */
347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000349/**
Rose Zadik7f441272018-01-22 11:48:23 +0000350 * \brief This function performs an AES-CFB128 encryption or decryption
351 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000352 *
Rose Zadik7f441272018-01-22 11:48:23 +0000353 * It performs the operation defined in the \p mode
354 * parameter (encrypt or decrypt), on the input data buffer
355 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000356 *
Rose Zadik7f441272018-01-22 11:48:23 +0000357 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
358 * regardless of whether you are performing an encryption or decryption
359 * operation, that is, regardless of the \p mode parameter. This is
360 * because CFB mode uses the same key schedule for encryption and
361 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000362 *
Rose Zadik7f441272018-01-22 11:48:23 +0000363 * \note Upon exit, the content of the IV is updated so that you can
364 * call the same function again on the next
365 * block(s) of data and get the same result as if it was
366 * encrypted in one call. This allows a "streaming" usage.
367 * If you need to retain the contents of the
368 * IV, you must either save it manually or use the cipher
369 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000370 *
Rose Zadik7f441272018-01-22 11:48:23 +0000371 *
372 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500373 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000374 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
375 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500376 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000377 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500378 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000379 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000383 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000385 *
386 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000387 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200388int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000389 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000390 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000391 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000392 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000393 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000394 unsigned char *output );
395
Paul Bakker9a736322012-11-14 12:39:52 +0000396/**
Rose Zadik7f441272018-01-22 11:48:23 +0000397 * \brief This function performs an AES-CFB8 encryption or decryption
398 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100399 *
Rose Zadik7f441272018-01-22 11:48:23 +0000400 * It performs the operation defined in the \p mode
401 * parameter (encrypt/decrypt), on the input data buffer defined
402 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100403 *
Rose Zadik7f441272018-01-22 11:48:23 +0000404 * Due to the nature of CFB, you must use the same key schedule for
405 * both encryption and decryption operations. Therefore, you must
406 * use the context initialized with mbedtls_aes_setkey_enc() for
407 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000408 *
Rose Zadik7f441272018-01-22 11:48:23 +0000409 * \note Upon exit, the content of the IV is updated so that you can
410 * call the same function again on the next
411 * block(s) of data and get the same result as if it was
412 * encrypted in one call. This allows a "streaming" usage.
413 * If you need to retain the contents of the
414 * IV, you should either save it manually or use the cipher
415 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100416 *
Rose Zadik7f441272018-01-22 11:48:23 +0000417 *
418 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500419 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000420 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
421 * #MBEDTLS_AES_DECRYPT
422 * \param length The length of the input data.
423 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500424 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000425 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500426 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000427 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500428 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000429 *
430 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100431 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200432int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100433 int mode,
434 size_t length,
435 unsigned char iv[16],
436 const unsigned char *input,
437 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200438#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100439
Simon Butcher76a5b222018-04-22 22:57:27 +0100440#if defined(MBEDTLS_CIPHER_MODE_OFB)
441/**
Simon Butcher5db13622018-06-04 22:11:25 +0100442 * \brief This function performs an AES-OFB (Output Feedback Mode)
443 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100444 *
Simon Butcher5db13622018-06-04 22:11:25 +0100445 * For OFB, you must set up the context with
446 * mbedtls_aes_setkey_enc(), regardless of whether you are
447 * performing an encryption or decryption operation. This is
448 * because OFB mode uses the same key schedule for encryption and
449 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100450 *
Simon Butcher5db13622018-06-04 22:11:25 +0100451 * The OFB operation is identical for encryption or decryption,
452 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100453 *
Simon Butcher5db13622018-06-04 22:11:25 +0100454 * \note Upon exit, the content of iv, the Initialisation Vector, is
455 * updated so that you can call the same function again on the next
456 * block(s) of data and get the same result as if it was encrypted
457 * in one call. This allows a "streaming" usage, by initialising
458 * iv_off to 0 before the first call, and preserving its value
459 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100460 *
Simon Butcher5db13622018-06-04 22:11:25 +0100461 * For non-streaming use, the iv should be initialised on each call
462 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100463 *
Simon Butcher5db13622018-06-04 22:11:25 +0100464 * If you need to retain the contents of the initialisation vector,
465 * you must either save it manually or use the cipher module
466 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100467 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100468 * \warning For the OFB mode, the initialisation vector must be unique
469 * every encryption operation. Reuse of an initialisation vector
470 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100471 *
472 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500473 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100474 * \param length The length of the input data.
475 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500476 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100477 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500478 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100479 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100481 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 *
484 * \return \c 0 on success.
485 */
486int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
487 size_t length,
488 size_t *iv_off,
489 unsigned char iv[16],
490 const unsigned char *input,
491 unsigned char *output );
492
493#endif /* MBEDTLS_CIPHER_MODE_OFB */
494
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200495#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100496/**
Rose Zadik7f441272018-01-22 11:48:23 +0000497 * \brief This function performs an AES-CTR encryption or decryption
498 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000499 *
Rose Zadik7f441272018-01-22 11:48:23 +0000500 * This function performs the operation defined in the \p mode
501 * parameter (encrypt/decrypt), on the input data buffer
502 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000503 *
Rose Zadik7f441272018-01-22 11:48:23 +0000504 * Due to the nature of CTR, you must use the same key schedule
505 * for both encryption and decryption operations. Therefore, you
506 * must use the context initialized with mbedtls_aes_setkey_enc()
507 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000508 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100509 * \warning You must never reuse a nonce value with the same key. Doing so
510 * would void the encryption for the two messages encrypted with
511 * the same nonce and key.
512 *
513 * There are two common strategies for managing nonces with CTR:
514 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200515 * 1. You can handle everything as a single message processed over
516 * successive calls to this function. In that case, you want to
517 * set \p nonce_counter and \p nc_off to 0 for the first call, and
518 * then preserve the values of \p nonce_counter, \p nc_off and \p
519 * stream_block across calls to this function as they will be
520 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100521 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200522 * With this strategy, you must not encrypt more than 2**128
523 * blocks of data with the same key.
524 *
525 * 2. You can encrypt separate messages by dividing the \p
526 * nonce_counter buffer in two areas: the first one used for a
527 * per-message nonce, handled by yourself, and the second one
528 * updated by this function internally.
529 *
530 * For example, you might reserve the first 12 bytes for the
531 * per-message nonce, and the last 4 bytes for internal use. In that
532 * case, before calling this function on a new message you need to
533 * set the first 12 bytes of \p nonce_counter to your chosen nonce
534 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
535 * stream_block to be ignored). That way, you can encrypt at most
536 * 2**96 messages of up to 2**32 blocks each with the same key.
537 *
538 * The per-message nonce (or information sufficient to reconstruct
539 * it) needs to be communicated with the ciphertext and must be unique.
540 * The recommended way to ensure uniqueness is to use a message
541 * counter. An alternative is to generate random nonces, but this
542 * limits the number of messages that can be securely encrypted:
543 * for example, with 96-bit random nonces, you should not encrypt
544 * more than 2**32 messages with the same key.
545 *
546 * Note that for both stategies, sizes are measured in blocks and
547 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000548 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200549 * \warning Upon return, \p stream_block contains sensitive data. Its
550 * content must not be written to insecure storage and should be
551 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000552 *
Rose Zadik7f441272018-01-22 11:48:23 +0000553 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500554 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000555 * \param length The length of the input data.
556 * \param nc_off The offset in the current \p stream_block, for
557 * resuming within the current cipher stream. The
558 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500559 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000560 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500561 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \param stream_block The saved stream block for resuming. This is
563 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500564 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000565 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500566 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000567 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500568 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000569 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100570 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000571 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200572int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000573 size_t length,
574 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000575 unsigned char nonce_counter[16],
576 unsigned char stream_block[16],
577 const unsigned char *input,
578 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200579#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200580
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200581/**
Rose Zadik7f441272018-01-22 11:48:23 +0000582 * \brief Internal AES block encryption function. This is only
583 * exposed to allow overriding it using
584 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200585 *
Rose Zadik7f441272018-01-22 11:48:23 +0000586 * \param ctx The AES context to use for encryption.
587 * \param input The plaintext block.
588 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000589 *
Rose Zadik7f441272018-01-22 11:48:23 +0000590 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200591 */
Andres AGf5bf7182017-03-03 14:09:56 +0000592int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
593 const unsigned char input[16],
594 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200595
596/**
Rose Zadik7f441272018-01-22 11:48:23 +0000597 * \brief Internal AES block decryption function. This is only
598 * exposed to allow overriding it using see
599 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200600 *
Rose Zadik7f441272018-01-22 11:48:23 +0000601 * \param ctx The AES context to use for decryption.
602 * \param input The ciphertext block.
603 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000604 *
Rose Zadik7f441272018-01-22 11:48:23 +0000605 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200606 */
Andres AGf5bf7182017-03-03 14:09:56 +0000607int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
608 const unsigned char input[16],
609 unsigned char output[16] );
610
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500611#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000612/**
Rose Zadik7f441272018-01-22 11:48:23 +0000613 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100615 * \return \c 0 on success.
616 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000617 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000619
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500620#endif /* MBEDTLS_SELF_TEST */
621
Paul Bakker5121ce52009-01-03 21:22:43 +0000622#ifdef __cplusplus
623}
624#endif
625
626#endif /* aes.h */