blob: e23b9ca94ab55578cf99a15d8ce18609f65aae4a [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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010045#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020046#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020048#endif
Paul Bakker90995b52013-06-24 19:20:35 +020049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020051#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000052
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010053/* padlock.c and aesni.c rely on these values! */
Rose Zadik7f441272018-01-22 11:48:23 +000054#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
55#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Andres Amaya Garciac5380642017-11-28 19:57:51 +000057/* Error codes in range 0x0020-0x0022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
59#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000060
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000061/* Error codes in range 0x0021-0x0025 */
62#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 /**< Invalid input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030063
Andres AGf5bf7182017-03-03 14:09:56 +000064#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
65 !defined(inline) && !defined(__cplusplus)
66#define inline __inline
67#endif
68
Paul Bakker407a0da2013-06-27 14:29:21 +020069#ifdef __cplusplus
70extern "C" {
71#endif
72
Ron Eldorb2aacec2017-05-18 16:53:08 +030073#if !defined(MBEDTLS_AES_ALT)
74// Regular implementation
75//
76
Paul Bakker5121ce52009-01-03 21:22:43 +000077/**
Rose Zadik7f441272018-01-22 11:48:23 +000078 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000079 */
Dawid Drozd428cc522018-07-24 10:02:47 +020080typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +000081{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020082 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 can
Rose Zadik7f441272018-01-22 11:48:23 +000085 hold 32 extra Bytes, which can be used for
86 one of the following purposes:
87 <ul><li>Alignment if VIA padlock is
88 used.</li>
89 <li>Simplifying key expansion in the 256-bit
90 case by generating an extra round key.
91 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000092}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Jaeden Amero9366feb2018-05-29 18:55:17 +010095#if defined(MBEDTLS_CIPHER_MODE_XTS)
96/**
97 * \brief The AES XTS context-type definition.
98 */
Dawid Drozd428cc522018-07-24 10:02:47 +020099typedef struct mbedtls_aes_xts_context
Jaeden Amero9366feb2018-05-29 18:55:17 +0100100{
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200101 mbedtls_aes_context MBEDTLS_PRIVATE(crypt); /*!< The AES context to use for AES block
Jaeden Amero9366feb2018-05-29 18:55:17 +0100102 encryption or decryption. */
Mateusz Starzyk846f0212021-05-19 19:44:07 +0200103 mbedtls_aes_context MBEDTLS_PRIVATE(tweak); /*!< The AES context used for tweak
Jaeden Amero9366feb2018-05-29 18:55:17 +0100104 computation. */
105} mbedtls_aes_xts_context;
106#endif /* MBEDTLS_CIPHER_MODE_XTS */
107
Ron Eldorb2aacec2017-05-18 16:53:08 +0300108#else /* MBEDTLS_AES_ALT */
109#include "aes_alt.h"
110#endif /* MBEDTLS_AES_ALT */
111
Paul Bakker5121ce52009-01-03 21:22:43 +0000112/**
Rose Zadik7f441272018-01-22 11:48:23 +0000113 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200114 *
Rose Zadik7f441272018-01-22 11:48:23 +0000115 * It must be the first API called before using
116 * the context.
117 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500118 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200121
122/**
Rose Zadik7f441272018-01-22 11:48:23 +0000123 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200124 *
Rose Zadik7f441272018-01-22 11:48:23 +0000125 * \param ctx The AES context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500126 * If this is \c NULL, this function does nothing.
127 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200128 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200130
Jaeden Amero9366feb2018-05-29 18:55:17 +0100131#if defined(MBEDTLS_CIPHER_MODE_XTS)
132/**
133 * \brief This function initializes the specified AES XTS context.
134 *
135 * It must be the first API called before using
136 * the context.
137 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500138 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100139 */
140void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
141
142/**
143 * \brief This function releases and clears the specified AES XTS context.
144 *
145 * \param ctx The AES XTS context to clear.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * If this is \c NULL, this function does nothing.
147 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100148 */
149void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
150#endif /* MBEDTLS_CIPHER_MODE_XTS */
151
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200152/**
Rose Zadik7f441272018-01-22 11:48:23 +0000153 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 *
Rose Zadik7f441272018-01-22 11:48:23 +0000155 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500156 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000157 * \param key The encryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000159 * \param keybits The size of data passed in bits. Valid options are:
160 * <ul><li>128 bits</li>
161 * <li>192 bits</li>
162 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000163 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100164 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100165 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200168 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169
170/**
Rose Zadik7f441272018-01-22 11:48:23 +0000171 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 *
Rose Zadik7f441272018-01-22 11:48:23 +0000173 * \param ctx The AES context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500174 * It must be initialized.
Rose Zadik7f441272018-01-22 11:48:23 +0000175 * \param key The decryption key.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500176 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000177 * \param keybits The size of data passed. Valid options are:
178 * <ul><li>128 bits</li>
179 * <li>192 bits</li>
180 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000181 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200186 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Jaeden Amero9366feb2018-05-29 18:55:17 +0100188#if defined(MBEDTLS_CIPHER_MODE_XTS)
189/**
190 * \brief This function prepares an XTS context for encryption and
191 * sets the encryption key.
192 *
193 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500194 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100195 * \param key The encryption key. This is comprised of the XTS key1
196 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500197 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100198 * \param keybits The size of \p key passed in bits. Valid options are:
199 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
200 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
201 *
202 * \return \c 0 on success.
203 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
204 */
205int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
206 const unsigned char *key,
207 unsigned int keybits );
208
209/**
210 * \brief This function prepares an XTS context for decryption and
211 * sets the decryption key.
212 *
213 * \param ctx The AES XTS context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500214 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100215 * \param key The decryption key. This is comprised of the XTS key1
216 * concatenated with the XTS key2.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500217 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100218 * \param keybits The size of \p key passed in bits. Valid options are:
219 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
220 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
221 *
222 * \return \c 0 on success.
223 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
224 */
225int 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 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000254 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000255 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000256 unsigned char output[16] );
257
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200258#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000259/**
Rose Zadik7f441272018-01-22 11:48:23 +0000260 * \brief This function performs an AES-CBC encryption or decryption operation
261 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 *
Rose Zadik7f441272018-01-22 11:48:23 +0000263 * It performs the operation defined in the \p mode
264 * parameter (encrypt/decrypt), on the input data buffer defined in
265 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000266 *
Rose Zadik7f441272018-01-22 11:48:23 +0000267 * It can be called as many times as needed, until all the input
268 * data is processed. mbedtls_aes_init(), and either
269 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
270 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000271 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500272 * \note This function operates on full blocks, that is, the input size
273 * must be a multiple of the AES block size of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000274 *
275 * \note Upon exit, the content of the IV is updated so that you can
276 * call the same function again on the next
277 * block(s) of data and get the same result as if it was
278 * encrypted in one call. This allows a "streaming" usage.
279 * If you need to retain the contents of the IV, you should
280 * either save it manually or use the cipher module instead.
281 *
282 *
283 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500284 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000285 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
286 * #MBEDTLS_AES_DECRYPT.
287 * \param length The length of the input data in Bytes. This must be a
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500288 * multiple of the block size (\c 16 Bytes).
Rose Zadik7f441272018-01-22 11:48:23 +0000289 * \param iv Initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500290 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000291 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500292 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000293 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500294 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000295 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100296 * \return \c 0 on success.
297 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000298 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200300int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000301 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000302 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000303 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000304 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000307
Aorimn5f778012016-06-09 23:22:58 +0200308#if defined(MBEDTLS_CIPHER_MODE_XTS)
309/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100310 * \brief This function performs an AES-XTS encryption or decryption
311 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200312 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100313 * AES-XTS encrypts or decrypts blocks based on their location as
314 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100315 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200316 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100317 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
318 * AES blocks. If the data unit is larger than this, this function
319 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
320 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100321 * \param ctx The AES XTS context to use for AES XTS operations.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500322 * It must be initialized and bound to a key.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100323 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
324 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500325 * \param length The length of a data unit in Bytes. This can be any
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100326 * length between 16 bytes and 2^24 bytes inclusive
327 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100328 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100329 * bytes in little-endian format. For disk encryption, this
330 * is typically the index of the block device sector that
331 * contains the data.
332 * \param input The buffer holding the input data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500333 * data unit). This function reads \p length Bytes from \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100334 * input.
335 * \param output The buffer holding the output data (which is an entire
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500336 * data unit). This function writes \p length Bytes to \p
Jaeden Amero9366feb2018-05-29 18:55:17 +0100337 * output.
Aorimn5f778012016-06-09 23:22:58 +0200338 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100339 * \return \c 0 on success.
340 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500341 * smaller than an AES block in size (16 Bytes) or if \p
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100342 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200343 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100344int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
345 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100346 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100347 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100348 const unsigned char *input,
349 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200350#endif /* MBEDTLS_CIPHER_MODE_XTS */
351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000353/**
Rose Zadik7f441272018-01-22 11:48:23 +0000354 * \brief This function performs an AES-CFB128 encryption or decryption
355 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 *
Rose Zadik7f441272018-01-22 11:48:23 +0000357 * It performs the operation defined in the \p mode
358 * parameter (encrypt or decrypt), on the input data buffer
359 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000360 *
Rose Zadik7f441272018-01-22 11:48:23 +0000361 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
362 * regardless of whether you are performing an encryption or decryption
363 * operation, that is, regardless of the \p mode parameter. This is
364 * because CFB mode uses the same key schedule for encryption and
365 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000366 *
Rose Zadik7f441272018-01-22 11:48:23 +0000367 * \note Upon exit, the content of the IV is updated so that you can
368 * call the same function again on the next
369 * block(s) of data and get the same result as if it was
370 * encrypted in one call. This allows a "streaming" usage.
371 * If you need to retain the contents of the
372 * IV, you must either save it manually or use the cipher
373 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000374 *
Rose Zadik7f441272018-01-22 11:48:23 +0000375 *
376 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500377 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000378 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
379 * #MBEDTLS_AES_DECRYPT.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500380 * \param length The length of the input data in Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500382 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000383 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500384 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000385 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500386 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000387 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500388 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000389 *
390 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000391 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200392int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000394 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000395 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000396 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000397 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000398 unsigned char *output );
399
Paul Bakker9a736322012-11-14 12:39:52 +0000400/**
Rose Zadik7f441272018-01-22 11:48:23 +0000401 * \brief This function performs an AES-CFB8 encryption or decryption
402 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100403 *
Rose Zadik7f441272018-01-22 11:48:23 +0000404 * It performs the operation defined in the \p mode
405 * parameter (encrypt/decrypt), on the input data buffer defined
406 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100407 *
Rose Zadik7f441272018-01-22 11:48:23 +0000408 * Due to the nature of CFB, you must use the same key schedule for
409 * both encryption and decryption operations. Therefore, you must
410 * use the context initialized with mbedtls_aes_setkey_enc() for
411 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000412 *
Rose Zadik7f441272018-01-22 11:48:23 +0000413 * \note Upon exit, the content of the IV is updated so that you can
414 * call the same function again on the next
415 * block(s) of data and get the same result as if it was
416 * encrypted in one call. This allows a "streaming" usage.
417 * If you need to retain the contents of the
418 * IV, you should either save it manually or use the cipher
419 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100420 *
Rose Zadik7f441272018-01-22 11:48:23 +0000421 *
422 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500423 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000424 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
425 * #MBEDTLS_AES_DECRYPT
426 * \param length The length of the input data.
427 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500428 * It must be a readable and writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000429 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500430 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000431 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500432 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000433 *
434 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100435 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200436int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100437 int mode,
438 size_t length,
439 unsigned char iv[16],
440 const unsigned char *input,
441 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100443
Simon Butcher76a5b222018-04-22 22:57:27 +0100444#if defined(MBEDTLS_CIPHER_MODE_OFB)
445/**
Simon Butcher5db13622018-06-04 22:11:25 +0100446 * \brief This function performs an AES-OFB (Output Feedback Mode)
447 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100448 *
Simon Butcher5db13622018-06-04 22:11:25 +0100449 * For OFB, you must set up the context with
450 * mbedtls_aes_setkey_enc(), regardless of whether you are
451 * performing an encryption or decryption operation. This is
452 * because OFB mode uses the same key schedule for encryption and
453 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100454 *
Simon Butcher5db13622018-06-04 22:11:25 +0100455 * The OFB operation is identical for encryption or decryption,
456 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100457 *
Simon Butcher5db13622018-06-04 22:11:25 +0100458 * \note Upon exit, the content of iv, the Initialisation Vector, is
459 * updated so that you can call the same function again on the next
460 * block(s) of data and get the same result as if it was encrypted
461 * in one call. This allows a "streaming" usage, by initialising
462 * iv_off to 0 before the first call, and preserving its value
463 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100464 *
Simon Butcher5db13622018-06-04 22:11:25 +0100465 * For non-streaming use, the iv should be initialised on each call
466 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100467 *
Simon Butcher5db13622018-06-04 22:11:25 +0100468 * If you need to retain the contents of the initialisation vector,
469 * you must either save it manually or use the cipher module
470 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100471 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100472 * \warning For the OFB mode, the initialisation vector must be unique
473 * every encryption operation. Reuse of an initialisation vector
474 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100475 *
476 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500477 * It must be initialized and bound to a key.
Simon Butcher76a5b222018-04-22 22:57:27 +0100478 * \param length The length of the input data.
479 * \param iv_off The offset in IV (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500480 * It must point to a valid \c size_t.
Simon Butcher76a5b222018-04-22 22:57:27 +0100481 * \param iv The initialization vector (updated after use).
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500482 * It must be a readable and writeable buffer of \c 16 Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100483 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500484 * It must be readable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100485 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500486 * It must be writeable and of size \p length Bytes.
Simon Butcher76a5b222018-04-22 22:57:27 +0100487 *
488 * \return \c 0 on success.
489 */
490int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
491 size_t length,
492 size_t *iv_off,
493 unsigned char iv[16],
494 const unsigned char *input,
495 unsigned char *output );
496
497#endif /* MBEDTLS_CIPHER_MODE_OFB */
498
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200499#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100500/**
Rose Zadik7f441272018-01-22 11:48:23 +0000501 * \brief This function performs an AES-CTR encryption or decryption
502 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000503 *
Rose Zadik7f441272018-01-22 11:48:23 +0000504 * This function performs the operation defined in the \p mode
505 * parameter (encrypt/decrypt), on the input data buffer
506 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000507 *
Rose Zadik7f441272018-01-22 11:48:23 +0000508 * Due to the nature of CTR, you must use the same key schedule
509 * for both encryption and decryption operations. Therefore, you
510 * must use the context initialized with mbedtls_aes_setkey_enc()
511 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000512 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100513 * \warning You must never reuse a nonce value with the same key. Doing so
514 * would void the encryption for the two messages encrypted with
515 * the same nonce and key.
516 *
517 * There are two common strategies for managing nonces with CTR:
518 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200519 * 1. You can handle everything as a single message processed over
520 * successive calls to this function. In that case, you want to
521 * set \p nonce_counter and \p nc_off to 0 for the first call, and
522 * then preserve the values of \p nonce_counter, \p nc_off and \p
523 * stream_block across calls to this function as they will be
524 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100525 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200526 * With this strategy, you must not encrypt more than 2**128
527 * blocks of data with the same key.
528 *
529 * 2. You can encrypt separate messages by dividing the \p
530 * nonce_counter buffer in two areas: the first one used for a
531 * per-message nonce, handled by yourself, and the second one
532 * updated by this function internally.
533 *
534 * For example, you might reserve the first 12 bytes for the
535 * per-message nonce, and the last 4 bytes for internal use. In that
536 * case, before calling this function on a new message you need to
537 * set the first 12 bytes of \p nonce_counter to your chosen nonce
538 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
539 * stream_block to be ignored). That way, you can encrypt at most
540 * 2**96 messages of up to 2**32 blocks each with the same key.
541 *
542 * The per-message nonce (or information sufficient to reconstruct
543 * it) needs to be communicated with the ciphertext and must be unique.
544 * The recommended way to ensure uniqueness is to use a message
545 * counter. An alternative is to generate random nonces, but this
546 * limits the number of messages that can be securely encrypted:
547 * for example, with 96-bit random nonces, you should not encrypt
548 * more than 2**32 messages with the same key.
549 *
550 * Note that for both stategies, sizes are measured in blocks and
551 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000552 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200553 * \warning Upon return, \p stream_block contains sensitive data. Its
554 * content must not be written to insecure storage and should be
555 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000556 *
Rose Zadik7f441272018-01-22 11:48:23 +0000557 * \param ctx The AES context to use for encryption or decryption.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500558 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000559 * \param length The length of the input data.
560 * \param nc_off The offset in the current \p stream_block, for
561 * resuming within the current cipher stream. The
562 * offset pointer should be 0 at the start of a stream.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500563 * It must point to a valid \c size_t.
Rose Zadik7f441272018-01-22 11:48:23 +0000564 * \param nonce_counter The 128-bit nonce and counter.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500565 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000566 * \param stream_block The saved stream block for resuming. This is
567 * overwritten by the function.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500568 * It must be a readable-writeable buffer of \c 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000569 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500570 * It must be readable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000571 * \param output The buffer holding the output data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500572 * It must be writeable and of size \p length Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000573 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100574 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000575 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000577 size_t length,
578 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000579 unsigned char nonce_counter[16],
580 unsigned char stream_block[16],
581 const unsigned char *input,
582 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200583#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200584
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200585/**
Rose Zadik7f441272018-01-22 11:48:23 +0000586 * \brief Internal AES block encryption function. This is only
587 * exposed to allow overriding it using
588 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200589 *
Rose Zadik7f441272018-01-22 11:48:23 +0000590 * \param ctx The AES context to use for encryption.
591 * \param input The plaintext block.
592 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000593 *
Rose Zadik7f441272018-01-22 11:48:23 +0000594 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200595 */
Andres AGf5bf7182017-03-03 14:09:56 +0000596int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
597 const unsigned char input[16],
598 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200599
600/**
Rose Zadik7f441272018-01-22 11:48:23 +0000601 * \brief Internal AES block decryption function. This is only
602 * exposed to allow overriding it using see
603 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200604 *
Rose Zadik7f441272018-01-22 11:48:23 +0000605 * \param ctx The AES context to use for decryption.
606 * \param input The ciphertext block.
607 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000608 *
Rose Zadik7f441272018-01-22 11:48:23 +0000609 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200610 */
Andres AGf5bf7182017-03-03 14:09:56 +0000611int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
612 const unsigned char input[16],
613 unsigned char output[16] );
614
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500615#if defined(MBEDTLS_SELF_TEST)
Paul Bakker5121ce52009-01-03 21:22:43 +0000616/**
Rose Zadik7f441272018-01-22 11:48:23 +0000617 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000618 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100619 * \return \c 0 on success.
620 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000621 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200622int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000623
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500624#endif /* MBEDTLS_SELF_TEST */
625
Paul Bakker5121ce52009-01-03 21:22:43 +0000626#ifdef __cplusplus
627}
628#endif
629
630#endif /* aes.h */