blob: 0f8934f7229abde6a726e211c16d58af19c65b95 [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
Rose Zadik7f441272018-01-22 11:48:23 +000023/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020024 * SPDX-License-Identifier: Apache-2.0
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License"); you may
27 * not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000037 *
Rose Zadik7f441272018-01-22 11:48:23 +000038 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000039 */
Rose Zadik7f441272018-01-22 11:48:23 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#ifndef MBEDTLS_AES_H
42#define MBEDTLS_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020045#include "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
64/* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */
Rose Zadik7f441272018-01-22 11:48:23 +000065#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030066
67/* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010068#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Andres AGf5bf7182017-03-03 14:09:56 +000070#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
71 !defined(inline) && !defined(__cplusplus)
72#define inline __inline
73#endif
74
Paul Bakker407a0da2013-06-27 14:29:21 +020075#ifdef __cplusplus
76extern "C" {
77#endif
78
Ron Eldorb2aacec2017-05-18 16:53:08 +030079#if !defined(MBEDTLS_AES_ALT)
80// Regular implementation
81//
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/**
Rose Zadik7f441272018-01-22 11:48:23 +000084 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000085 */
Dawid Drozd428cc522018-07-24 10:02:47 +020086typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Rose Zadik7f441272018-01-22 11:48:23 +000088 int nr; /*!< The number of rounds. */
89 uint32_t *rk; /*!< AES round keys. */
90 uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
91 hold 32 extra Bytes, which can be used for
92 one of the following purposes:
93 <ul><li>Alignment if VIA padlock is
94 used.</li>
95 <li>Simplifying key expansion in the 256-bit
96 case by generating an extra round key.
97 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Jaeden Amero9366feb2018-05-29 18:55:17 +0100101#if defined(MBEDTLS_CIPHER_MODE_XTS)
102/**
103 * \brief The AES XTS context-type definition.
104 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200105typedef struct mbedtls_aes_xts_context
Jaeden Amero9366feb2018-05-29 18:55:17 +0100106{
107 mbedtls_aes_context crypt; /*!< The AES context to use for AES block
108 encryption or decryption. */
109 mbedtls_aes_context tweak; /*!< The AES context used for tweak
110 computation. */
111} mbedtls_aes_xts_context;
112#endif /* MBEDTLS_CIPHER_MODE_XTS */
113
Ron Eldorb2aacec2017-05-18 16:53:08 +0300114#else /* MBEDTLS_AES_ALT */
115#include "aes_alt.h"
116#endif /* MBEDTLS_AES_ALT */
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118/**
Rose Zadik7f441272018-01-22 11:48:23 +0000119 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120 *
Rose Zadik7f441272018-01-22 11:48:23 +0000121 * It must be the first API called before using
122 * the context.
123 *
Manuel Pégourié-Gonnarded459e62018-12-12 10:20:33 +0100124 * \param ctx The AES context to initialize. This must not be \c NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200127
128/**
Rose Zadik7f441272018-01-22 11:48:23 +0000129 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200130 *
Manuel Pégourié-Gonnarded459e62018-12-12 10:20:33 +0100131 * \param ctx The AES context to clear.
132 * If this is \c NULL, this function does nothing.
133 * Otherwise, the context must have been at least initialized.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200134 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200136
Jaeden Amero9366feb2018-05-29 18:55:17 +0100137#if defined(MBEDTLS_CIPHER_MODE_XTS)
138/**
139 * \brief This function initializes the specified AES XTS context.
140 *
141 * It must be the first API called before using
142 * the context.
143 *
Manuel Pégourié-Gonnarded459e62018-12-12 10:20:33 +0100144 * \param ctx The AES XTS context to initialize. This must not be \c NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100145 */
146void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
147
148/**
149 * \brief This function releases and clears the specified AES XTS context.
150 *
Manuel Pégourié-Gonnarded459e62018-12-12 10:20:33 +0100151 * \param ctx The AES XTS context to clear.
152 * If this is \c NULL, this function does nothing.
153 * Otherwise, the context must have been at least initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100154 */
155void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
156#endif /* MBEDTLS_CIPHER_MODE_XTS */
157
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200158/**
Rose Zadik7f441272018-01-22 11:48:23 +0000159 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 *
Manuel Pégourié-Gonnarded459e62018-12-12 10:20:33 +0100161 * \param ctx The AES context to which the key should be bound.
162 * It must be initialized.
163 * \param key The encryption key.
164 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000165 * \param keybits The size of data passed in bits. Valid options are:
166 * <ul><li>128 bits</li>
167 * <li>192 bits</li>
168 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000169 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100170 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100171 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200174 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
176/**
Rose Zadik7f441272018-01-22 11:48:23 +0000177 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 *
Manuel Pégourié-Gonnarded459e62018-12-12 10:20:33 +0100179 * \param ctx The AES context to which the key should be bound.
180 * It must be initialized.
181 * \param key The decryption key.
182 * This must be a readable buffer of size \p keybits bits.
Rose Zadik7f441272018-01-22 11:48:23 +0000183 * \param keybits The size of data passed. Valid options are:
184 * <ul><li>128 bits</li>
185 * <li>192 bits</li>
186 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000187 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100188 * \return \c 0 on success.
189 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200191int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200192 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Jaeden Amero9366feb2018-05-29 18:55:17 +0100194#if defined(MBEDTLS_CIPHER_MODE_XTS)
195/**
196 * \brief This function prepares an XTS context for encryption and
197 * sets the encryption key.
198 *
199 * \param ctx The AES XTS context to which the key should be bound.
Manuel Pégourié-Gonnard68e3dff2018-12-12 12:48:04 +0100200 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100201 * \param key The encryption key. This is comprised of the XTS key1
202 * concatenated with the XTS key2.
Manuel Pégourié-Gonnard68e3dff2018-12-12 12:48:04 +0100203 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100204 * \param keybits The size of \p key passed in bits. Valid options are:
205 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
206 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
207 *
208 * \return \c 0 on success.
209 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
210 */
211int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
212 const unsigned char *key,
213 unsigned int keybits );
214
215/**
216 * \brief This function prepares an XTS context for decryption and
217 * sets the decryption key.
218 *
219 * \param ctx The AES XTS context to which the key should be bound.
Manuel Pégourié-Gonnard68e3dff2018-12-12 12:48:04 +0100220 * It must be initialized.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100221 * \param key The decryption key. This is comprised of the XTS key1
222 * concatenated with the XTS key2.
Manuel Pégourié-Gonnard68e3dff2018-12-12 12:48:04 +0100223 * This must be a readable buffer of size \p keybits bits.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100224 * \param keybits The size of \p key passed in bits. Valid options are:
225 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
226 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
227 *
228 * \return \c 0 on success.
229 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
230 */
231int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
232 const unsigned char *key,
233 unsigned int keybits );
234#endif /* MBEDTLS_CIPHER_MODE_XTS */
235
Paul Bakker5121ce52009-01-03 21:22:43 +0000236/**
Rose Zadik7f441272018-01-22 11:48:23 +0000237 * \brief This function performs an AES single-block encryption or
238 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 *
Rose Zadik7f441272018-01-22 11:48:23 +0000240 * It performs the operation defined in the \p mode parameter
241 * (encrypt or decrypt), on the input data buffer defined in
242 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000243 *
Rose Zadik7f441272018-01-22 11:48:23 +0000244 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
245 * mbedtls_aes_setkey_dec() must be called before the first
246 * call to this API with the same context.
247 *
248 * \param ctx The AES context to use for encryption or decryption.
Manuel Pégourié-Gonnard1aca2602018-12-12 12:56:55 +0100249 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000250 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
251 * #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard1aca2602018-12-12 12:56:55 +0100252 * \param input The buffer holding the input data.
253 * It must be readable and at least 16 Bytes long.
254 * \param output The buffer where the output data will be written.
255 * It must be writeable and at least 16 Bytes long.
Rose Zadik7f441272018-01-22 11:48:23 +0000256
257 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000258 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000260 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000261 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000262 unsigned char output[16] );
263
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200264#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000265/**
Rose Zadik7f441272018-01-22 11:48:23 +0000266 * \brief This function performs an AES-CBC encryption or decryption operation
267 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000268 *
Rose Zadik7f441272018-01-22 11:48:23 +0000269 * It performs the operation defined in the \p mode
270 * parameter (encrypt/decrypt), on the input data buffer defined in
271 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000272 *
Rose Zadik7f441272018-01-22 11:48:23 +0000273 * It can be called as many times as needed, until all the input
274 * data is processed. mbedtls_aes_init(), and either
275 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
276 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000277 *
Manuel Pégourié-Gonnard3178d1a2018-12-12 13:05:00 +0100278 * \note This function operates on full blocks, that is, the input size
Rose Zadik7f441272018-01-22 11:48:23 +0000279 * must be a multiple of the AES block size of 16 Bytes.
280 *
281 * \note Upon exit, the content of the IV is updated so that you can
282 * call the same function again on the next
283 * block(s) of data and get the same result as if it was
284 * encrypted in one call. This allows a "streaming" usage.
285 * If you need to retain the contents of the IV, you should
286 * either save it manually or use the cipher module instead.
287 *
288 *
289 * \param ctx The AES context to use for encryption or decryption.
Manuel Pégourié-Gonnard3178d1a2018-12-12 13:05:00 +0100290 * It must be initialized and bound to a key.
Rose Zadik7f441272018-01-22 11:48:23 +0000291 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
292 * #MBEDTLS_AES_DECRYPT.
293 * \param length The length of the input data in Bytes. This must be a
294 * multiple of the block size (16 Bytes).
295 * \param iv Initialization vector (updated after use).
Manuel Pégourié-Gonnard3178d1a2018-12-12 13:05:00 +0100296 * It must be a readable and writeable buffer of 16 Bytes.
Rose Zadik7f441272018-01-22 11:48:23 +0000297 * \param input The buffer holding the input data.
Manuel Pégourié-Gonnard3178d1a2018-12-12 13:05:00 +0100298 * It must be readable and of size \p length.
Rose Zadik7f441272018-01-22 11:48:23 +0000299 * \param output The buffer holding the output data.
Manuel Pégourié-Gonnard3178d1a2018-12-12 13:05:00 +0100300 * It must be writeable and of size \p length.
Rose Zadik7f441272018-01-22 11:48:23 +0000301 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100302 * \return \c 0 on success.
303 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000304 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000305 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200306int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000308 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000310 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000311 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000313
Aorimn5f778012016-06-09 23:22:58 +0200314#if defined(MBEDTLS_CIPHER_MODE_XTS)
315/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100316 * \brief This function performs an AES-XTS encryption or decryption
317 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200318 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100319 * AES-XTS encrypts or decrypts blocks based on their location as
320 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100321 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200322 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100323 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
324 * AES blocks. If the data unit is larger than this, this function
325 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
326 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100327 * \param ctx The AES XTS context to use for AES XTS operations.
328 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
329 * #MBEDTLS_AES_DECRYPT.
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100330 * \param length The length of a data unit in bytes. This can be any
331 * length between 16 bytes and 2^24 bytes inclusive
332 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100333 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100334 * bytes in little-endian format. For disk encryption, this
335 * is typically the index of the block device sector that
336 * contains the data.
337 * \param input The buffer holding the input data (which is an entire
338 * data unit). This function reads \p length bytes from \p
339 * input.
340 * \param output The buffer holding the output data (which is an entire
341 * data unit). This function writes \p length bytes to \p
342 * output.
Aorimn5f778012016-06-09 23:22:58 +0200343 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100344 * \return \c 0 on success.
345 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100346 * smaller than an AES block in size (16 bytes) or if \p
347 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200348 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100349int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
350 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100351 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100352 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100353 const unsigned char *input,
354 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200355#endif /* MBEDTLS_CIPHER_MODE_XTS */
356
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000358/**
Rose Zadik7f441272018-01-22 11:48:23 +0000359 * \brief This function performs an AES-CFB128 encryption or decryption
360 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 *
Rose Zadik7f441272018-01-22 11:48:23 +0000362 * It performs the operation defined in the \p mode
363 * parameter (encrypt or decrypt), on the input data buffer
364 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000365 *
Rose Zadik7f441272018-01-22 11:48:23 +0000366 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
367 * regardless of whether you are performing an encryption or decryption
368 * operation, that is, regardless of the \p mode parameter. This is
369 * because CFB mode uses the same key schedule for encryption and
370 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000371 *
Rose Zadik7f441272018-01-22 11:48:23 +0000372 * \note Upon exit, the content of the IV is updated so that you can
373 * call the same function again on the next
374 * block(s) of data and get the same result as if it was
375 * encrypted in one call. This allows a "streaming" usage.
376 * If you need to retain the contents of the
377 * IV, you must either save it manually or use the cipher
378 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000379 *
Rose Zadik7f441272018-01-22 11:48:23 +0000380 *
381 * \param ctx The AES context to use for encryption or decryption.
382 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
383 * #MBEDTLS_AES_DECRYPT.
384 * \param length The length of the input data.
385 * \param iv_off The offset in IV (updated after use).
386 * \param iv The initialization vector (updated after use).
387 * \param input The buffer holding the input data.
388 * \param output The buffer holding the output data.
389 *
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.
423 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
424 * #MBEDTLS_AES_DECRYPT
425 * \param length The length of the input data.
426 * \param iv The initialization vector (updated after use).
427 * \param input The buffer holding the input data.
428 * \param output The buffer holding the output data.
429 *
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.
473 * \param length The length of the input data.
474 * \param iv_off The offset in IV (updated after use).
475 * \param iv The initialization vector (updated after use).
476 * \param input The buffer holding the input data.
477 * \param output The buffer holding the output data.
478 *
479 * \return \c 0 on success.
480 */
481int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
482 size_t length,
483 size_t *iv_off,
484 unsigned char iv[16],
485 const unsigned char *input,
486 unsigned char *output );
487
488#endif /* MBEDTLS_CIPHER_MODE_OFB */
489
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200490#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100491/**
Rose Zadik7f441272018-01-22 11:48:23 +0000492 * \brief This function performs an AES-CTR encryption or decryption
493 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000494 *
Rose Zadik7f441272018-01-22 11:48:23 +0000495 * This function performs the operation defined in the \p mode
496 * parameter (encrypt/decrypt), on the input data buffer
497 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000498 *
Rose Zadik7f441272018-01-22 11:48:23 +0000499 * Due to the nature of CTR, you must use the same key schedule
500 * for both encryption and decryption operations. Therefore, you
501 * must use the context initialized with mbedtls_aes_setkey_enc()
502 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000503 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100504 * \warning You must never reuse a nonce value with the same key. Doing so
505 * would void the encryption for the two messages encrypted with
506 * the same nonce and key.
507 *
508 * There are two common strategies for managing nonces with CTR:
509 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200510 * 1. You can handle everything as a single message processed over
511 * successive calls to this function. In that case, you want to
512 * set \p nonce_counter and \p nc_off to 0 for the first call, and
513 * then preserve the values of \p nonce_counter, \p nc_off and \p
514 * stream_block across calls to this function as they will be
515 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100516 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200517 * With this strategy, you must not encrypt more than 2**128
518 * blocks of data with the same key.
519 *
520 * 2. You can encrypt separate messages by dividing the \p
521 * nonce_counter buffer in two areas: the first one used for a
522 * per-message nonce, handled by yourself, and the second one
523 * updated by this function internally.
524 *
525 * For example, you might reserve the first 12 bytes for the
526 * per-message nonce, and the last 4 bytes for internal use. In that
527 * case, before calling this function on a new message you need to
528 * set the first 12 bytes of \p nonce_counter to your chosen nonce
529 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
530 * stream_block to be ignored). That way, you can encrypt at most
531 * 2**96 messages of up to 2**32 blocks each with the same key.
532 *
533 * The per-message nonce (or information sufficient to reconstruct
534 * it) needs to be communicated with the ciphertext and must be unique.
535 * The recommended way to ensure uniqueness is to use a message
536 * counter. An alternative is to generate random nonces, but this
537 * limits the number of messages that can be securely encrypted:
538 * for example, with 96-bit random nonces, you should not encrypt
539 * more than 2**32 messages with the same key.
540 *
541 * Note that for both stategies, sizes are measured in blocks and
542 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000543 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200544 * \warning Upon return, \p stream_block contains sensitive data. Its
545 * content must not be written to insecure storage and should be
546 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000547 *
Rose Zadik7f441272018-01-22 11:48:23 +0000548 * \param ctx The AES context to use for encryption or decryption.
549 * \param length The length of the input data.
550 * \param nc_off The offset in the current \p stream_block, for
551 * resuming within the current cipher stream. The
552 * offset pointer should be 0 at the start of a stream.
553 * \param nonce_counter The 128-bit nonce and counter.
554 * \param stream_block The saved stream block for resuming. This is
555 * overwritten by the function.
556 * \param input The buffer holding the input data.
557 * \param output The buffer holding the output data.
558 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100559 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000560 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000562 size_t length,
563 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000564 unsigned char nonce_counter[16],
565 unsigned char stream_block[16],
566 const unsigned char *input,
567 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200568#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200569
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200570/**
Rose Zadik7f441272018-01-22 11:48:23 +0000571 * \brief Internal AES block encryption function. This is only
572 * exposed to allow overriding it using
573 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200574 *
Rose Zadik7f441272018-01-22 11:48:23 +0000575 * \param ctx The AES context to use for encryption.
576 * \param input The plaintext block.
577 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000578 *
Rose Zadik7f441272018-01-22 11:48:23 +0000579 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200580 */
Andres AGf5bf7182017-03-03 14:09:56 +0000581int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
582 const unsigned char input[16],
583 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200584
585/**
Rose Zadik7f441272018-01-22 11:48:23 +0000586 * \brief Internal AES block decryption function. This is only
587 * exposed to allow overriding it using see
588 * \c MBEDTLS_AES_DECRYPT_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 decryption.
591 * \param input The ciphertext block.
592 * \param output The output (plaintext) 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_decrypt( mbedtls_aes_context *ctx,
597 const unsigned char input[16],
598 unsigned char output[16] );
599
600#if !defined(MBEDTLS_DEPRECATED_REMOVED)
601#if defined(MBEDTLS_DEPRECATED_WARNING)
602#define MBEDTLS_DEPRECATED __attribute__((deprecated))
603#else
604#define MBEDTLS_DEPRECATED
605#endif
606/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100607 * \brief Deprecated internal AES block encryption function
608 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000609 *
Rose Zadik7f441272018-01-22 11:48:23 +0000610 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000611 *
Rose Zadik7f441272018-01-22 11:48:23 +0000612 * \param ctx The AES context to use for encryption.
613 * \param input Plaintext block.
614 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000615 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100616MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
617 const unsigned char input[16],
618 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000619
620/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100621 * \brief Deprecated internal AES block decryption function
622 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000623 *
Rose Zadik7f441272018-01-22 11:48:23 +0000624 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000625 *
Rose Zadik7f441272018-01-22 11:48:23 +0000626 * \param ctx The AES context to use for decryption.
627 * \param input Ciphertext block.
628 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000629 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100630MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
631 const unsigned char input[16],
632 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000633
634#undef MBEDTLS_DEPRECATED
635#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200636
Paul Bakker5121ce52009-01-03 21:22:43 +0000637/**
Rose Zadik7f441272018-01-22 11:48:23 +0000638 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100640 * \return \c 0 on success.
641 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000642 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200643int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000644
645#ifdef __cplusplus
646}
647#endif
648
649#endif /* aes.h */