blob: f6603d59625d79aa0a2bcea21d4078edad71fdf9 [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. */
Rose Zadik7f441272018-01-22 11:48:23 +000063#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010064#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Andres AGf5bf7182017-03-03 14:09:56 +000066#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
67 !defined(inline) && !defined(__cplusplus)
68#define inline __inline
69#endif
70
Paul Bakker407a0da2013-06-27 14:29:21 +020071#ifdef __cplusplus
72extern "C" {
73#endif
74
Ron Eldorb2aacec2017-05-18 16:53:08 +030075#if !defined(MBEDTLS_AES_ALT)
76// Regular implementation
77//
78
Paul Bakker5121ce52009-01-03 21:22:43 +000079/**
Rose Zadik7f441272018-01-22 11:48:23 +000080 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000081 */
82typedef struct
83{
Rose Zadik7f441272018-01-22 11:48:23 +000084 int nr; /*!< The number of rounds. */
85 uint32_t *rk; /*!< AES round keys. */
86 uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
87 hold 32 extra Bytes, which can be used for
88 one of the following purposes:
89 <ul><li>Alignment if VIA padlock is
90 used.</li>
91 <li>Simplifying key expansion in the 256-bit
92 case by generating an extra round key.
93 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000094}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Jaeden Amero9366feb2018-05-29 18:55:17 +010097#if defined(MBEDTLS_CIPHER_MODE_XTS)
98/**
99 * \brief The AES XTS context-type definition.
100 */
101typedef struct
102{
103 mbedtls_aes_context crypt; /*!< The AES context to use for AES block
104 encryption or decryption. */
105 mbedtls_aes_context tweak; /*!< The AES context used for tweak
106 computation. */
107} mbedtls_aes_xts_context;
108#endif /* MBEDTLS_CIPHER_MODE_XTS */
109
Ron Eldorb2aacec2017-05-18 16:53:08 +0300110#else /* MBEDTLS_AES_ALT */
111#include "aes_alt.h"
112#endif /* MBEDTLS_AES_ALT */
113
Paul Bakker5121ce52009-01-03 21:22:43 +0000114/**
Rose Zadik7f441272018-01-22 11:48:23 +0000115 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200116 *
Rose Zadik7f441272018-01-22 11:48:23 +0000117 * It must be the first API called before using
118 * the context.
119 *
120 * \param ctx The AES context to initialize.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200121 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200123
124/**
Rose Zadik7f441272018-01-22 11:48:23 +0000125 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200126 *
Rose Zadik7f441272018-01-22 11:48:23 +0000127 * \param ctx The AES context to clear.
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 *
138 * \param ctx The AES XTS context to initialize.
139 */
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.
146 */
147void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
148#endif /* MBEDTLS_CIPHER_MODE_XTS */
149
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200150/**
Rose Zadik7f441272018-01-22 11:48:23 +0000151 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 *
Rose Zadik7f441272018-01-22 11:48:23 +0000153 * \param ctx The AES context to which the key should be bound.
154 * \param key The encryption key.
155 * \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.
170 * \param key The decryption key.
171 * \param keybits The size of data passed. Valid options are:
172 * <ul><li>128 bits</li>
173 * <li>192 bits</li>
174 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000175 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100176 * \return \c 0 on success.
177 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200179int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200180 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181
Jaeden Amero9366feb2018-05-29 18:55:17 +0100182#if defined(MBEDTLS_CIPHER_MODE_XTS)
183/**
184 * \brief This function prepares an XTS context for encryption and
185 * sets the encryption key.
186 *
187 * \param ctx The AES XTS context to which the key should be bound.
188 * \param key The encryption key. This is comprised of the XTS key1
189 * concatenated with the XTS key2.
190 * \param keybits The size of \p key passed in bits. Valid options are:
191 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
192 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
193 *
194 * \return \c 0 on success.
195 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
196 */
197int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
198 const unsigned char *key,
199 unsigned int keybits );
200
201/**
202 * \brief This function prepares an XTS context for decryption and
203 * sets the decryption key.
204 *
205 * \param ctx The AES XTS context to which the key should be bound.
206 * \param key The decryption key. This is comprised of the XTS key1
207 * concatenated with the XTS key2.
208 * \param keybits The size of \p key passed in bits. Valid options are:
209 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
210 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
211 *
212 * \return \c 0 on success.
213 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
214 */
215int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
216 const unsigned char *key,
217 unsigned int keybits );
218#endif /* MBEDTLS_CIPHER_MODE_XTS */
219
Paul Bakker5121ce52009-01-03 21:22:43 +0000220/**
Rose Zadik7f441272018-01-22 11:48:23 +0000221 * \brief This function performs an AES single-block encryption or
222 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 *
Rose Zadik7f441272018-01-22 11:48:23 +0000224 * It performs the operation defined in the \p mode parameter
225 * (encrypt or decrypt), on the input data buffer defined in
226 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000227 *
Rose Zadik7f441272018-01-22 11:48:23 +0000228 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
229 * mbedtls_aes_setkey_dec() must be called before the first
230 * call to this API with the same context.
231 *
232 * \param ctx The AES context to use for encryption or decryption.
233 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
234 * #MBEDTLS_AES_DECRYPT.
235 * \param input The 16-Byte buffer holding the input data.
236 * \param output The 16-Byte buffer holding the output data.
237
238 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000241 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000242 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 unsigned char output[16] );
244
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000246/**
Rose Zadik7f441272018-01-22 11:48:23 +0000247 * \brief This function performs an AES-CBC encryption or decryption operation
248 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 *
Rose Zadik7f441272018-01-22 11:48:23 +0000250 * It performs the operation defined in the \p mode
251 * parameter (encrypt/decrypt), on the input data buffer defined in
252 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000253 *
Rose Zadik7f441272018-01-22 11:48:23 +0000254 * It can be called as many times as needed, until all the input
255 * data is processed. mbedtls_aes_init(), and either
256 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
257 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000258 *
Rose Zadik7f441272018-01-22 11:48:23 +0000259 * \note This function operates on aligned blocks, that is, the input size
260 * must be a multiple of the AES block size of 16 Bytes.
261 *
262 * \note Upon exit, the content of the IV is updated so that you can
263 * call the same function again on the next
264 * block(s) of data and get the same result as if it was
265 * encrypted in one call. This allows a "streaming" usage.
266 * If you need to retain the contents of the IV, you should
267 * either save it manually or use the cipher module instead.
268 *
269 *
270 * \param ctx The AES context to use for encryption or decryption.
271 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
272 * #MBEDTLS_AES_DECRYPT.
273 * \param length The length of the input data in Bytes. This must be a
274 * multiple of the block size (16 Bytes).
275 * \param iv Initialization vector (updated after use).
276 * \param input The buffer holding the input data.
277 * \param output The buffer holding the output data.
278 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100279 * \return \c 0 on success.
280 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000281 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000282 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200283int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000284 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000285 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000287 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Aorimn5f778012016-06-09 23:22:58 +0200291#if defined(MBEDTLS_CIPHER_MODE_XTS)
292/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100293 * \brief This function performs an AES-XTS encryption or decryption
294 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200295 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100296 * AES-XTS encrypts or decrypts blocks based on their location as
297 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100298 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200299 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100300 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
301 * AES blocks. If the data unit is larger than this, this function
302 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
303 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100304 * \param ctx The AES XTS context to use for AES XTS operations.
305 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
306 * #MBEDTLS_AES_DECRYPT.
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100307 * \param length The length of a data unit in bytes. This can be any
308 * length between 16 bytes and 2^24 bytes inclusive
309 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100310 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100311 * bytes in little-endian format. For disk encryption, this
312 * is typically the index of the block device sector that
313 * contains the data.
314 * \param input The buffer holding the input data (which is an entire
315 * data unit). This function reads \p length bytes from \p
316 * input.
317 * \param output The buffer holding the output data (which is an entire
318 * data unit). This function writes \p length bytes to \p
319 * output.
Aorimn5f778012016-06-09 23:22:58 +0200320 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100321 * \return \c 0 on success.
322 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100323 * smaller than an AES block in size (16 bytes) or if \p
324 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200325 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100326int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
327 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100328 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100329 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100330 const unsigned char *input,
331 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200332#endif /* MBEDTLS_CIPHER_MODE_XTS */
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000335/**
Rose Zadik7f441272018-01-22 11:48:23 +0000336 * \brief This function performs an AES-CFB128 encryption or decryption
337 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000338 *
Rose Zadik7f441272018-01-22 11:48:23 +0000339 * It performs the operation defined in the \p mode
340 * parameter (encrypt or decrypt), on the input data buffer
341 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000342 *
Rose Zadik7f441272018-01-22 11:48:23 +0000343 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
344 * regardless of whether you are performing an encryption or decryption
345 * operation, that is, regardless of the \p mode parameter. This is
346 * because CFB mode uses the same key schedule for encryption and
347 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000348 *
Rose Zadik7f441272018-01-22 11:48:23 +0000349 * \note Upon exit, the content of the IV is updated so that you can
350 * call the same function again on the next
351 * block(s) of data and get the same result as if it was
352 * encrypted in one call. This allows a "streaming" usage.
353 * If you need to retain the contents of the
354 * IV, you must either save it manually or use the cipher
355 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000356 *
Rose Zadik7f441272018-01-22 11:48:23 +0000357 *
358 * \param ctx The AES context to use for encryption or decryption.
359 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
360 * #MBEDTLS_AES_DECRYPT.
361 * \param length The length of the input data.
362 * \param iv_off The offset in IV (updated after use).
363 * \param iv The initialization vector (updated after use).
364 * \param input The buffer holding the input data.
365 * \param output The buffer holding the output data.
366 *
367 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200369int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000371 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000372 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000373 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000374 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000375 unsigned char *output );
376
Paul Bakker9a736322012-11-14 12:39:52 +0000377/**
Rose Zadik7f441272018-01-22 11:48:23 +0000378 * \brief This function performs an AES-CFB8 encryption or decryption
379 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100380 *
Rose Zadik7f441272018-01-22 11:48:23 +0000381 * It performs the operation defined in the \p mode
382 * parameter (encrypt/decrypt), on the input data buffer defined
383 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100384 *
Rose Zadik7f441272018-01-22 11:48:23 +0000385 * Due to the nature of CFB, you must use the same key schedule for
386 * both encryption and decryption operations. Therefore, you must
387 * use the context initialized with mbedtls_aes_setkey_enc() for
388 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000389 *
Rose Zadik7f441272018-01-22 11:48:23 +0000390 * \note Upon exit, the content of the IV is updated so that you can
391 * call the same function again on the next
392 * block(s) of data and get the same result as if it was
393 * encrypted in one call. This allows a "streaming" usage.
394 * If you need to retain the contents of the
395 * IV, you should either save it manually or use the cipher
396 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100397 *
Rose Zadik7f441272018-01-22 11:48:23 +0000398 *
399 * \param ctx The AES context to use for encryption or decryption.
400 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
401 * #MBEDTLS_AES_DECRYPT
402 * \param length The length of the input data.
403 * \param iv The initialization vector (updated after use).
404 * \param input The buffer holding the input data.
405 * \param output The buffer holding the output data.
406 *
407 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100408 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200409int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100410 int mode,
411 size_t length,
412 unsigned char iv[16],
413 const unsigned char *input,
414 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100416
Simon Butcher76a5b222018-04-22 22:57:27 +0100417#if defined(MBEDTLS_CIPHER_MODE_OFB)
418/**
Simon Butcher5db13622018-06-04 22:11:25 +0100419 * \brief This function performs an AES-OFB (Output Feedback Mode)
420 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100421 *
Simon Butcher5db13622018-06-04 22:11:25 +0100422 * For OFB, you must set up the context with
423 * mbedtls_aes_setkey_enc(), regardless of whether you are
424 * performing an encryption or decryption operation. This is
425 * because OFB mode uses the same key schedule for encryption and
426 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100427 *
Simon Butcher5db13622018-06-04 22:11:25 +0100428 * The OFB operation is identical for encryption or decryption,
429 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100430 *
Simon Butcher5db13622018-06-04 22:11:25 +0100431 * \note Upon exit, the content of iv, the Initialisation Vector, is
432 * updated so that you can call the same function again on the next
433 * block(s) of data and get the same result as if it was encrypted
434 * in one call. This allows a "streaming" usage, by initialising
435 * iv_off to 0 before the first call, and preserving its value
436 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100437 *
Simon Butcher5db13622018-06-04 22:11:25 +0100438 * For non-streaming use, the iv should be initialised on each call
439 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100440 *
Simon Butcher5db13622018-06-04 22:11:25 +0100441 * If you need to retain the contents of the initialisation vector,
442 * you must either save it manually or use the cipher module
443 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100444 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100445 * \warning For the OFB mode, the initialisation vector must be unique
446 * every encryption operation. Reuse of an initialisation vector
447 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100448 *
449 * \param ctx The AES context to use for encryption or decryption.
450 * \param length The length of the input data.
451 * \param iv_off The offset in IV (updated after use).
452 * \param iv The initialization vector (updated after use).
453 * \param input The buffer holding the input data.
454 * \param output The buffer holding the output data.
455 *
456 * \return \c 0 on success.
457 */
458int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
459 size_t length,
460 size_t *iv_off,
461 unsigned char iv[16],
462 const unsigned char *input,
463 unsigned char *output );
464
465#endif /* MBEDTLS_CIPHER_MODE_OFB */
466
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200467#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100468/**
Rose Zadik7f441272018-01-22 11:48:23 +0000469 * \brief This function performs an AES-CTR encryption or decryption
470 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000471 *
Rose Zadik7f441272018-01-22 11:48:23 +0000472 * This function performs the operation defined in the \p mode
473 * parameter (encrypt/decrypt), on the input data buffer
474 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000475 *
Rose Zadik7f441272018-01-22 11:48:23 +0000476 * Due to the nature of CTR, you must use the same key schedule
477 * for both encryption and decryption operations. Therefore, you
478 * must use the context initialized with mbedtls_aes_setkey_enc()
479 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000480 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100481 * \warning You must never reuse a nonce value with the same key. Doing so
482 * would void the encryption for the two messages encrypted with
483 * the same nonce and key.
484 *
485 * There are two common strategies for managing nonces with CTR:
486 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200487 * 1. You can handle everything as a single message processed over
488 * successive calls to this function. In that case, you want to
489 * set \p nonce_counter and \p nc_off to 0 for the first call, and
490 * then preserve the values of \p nonce_counter, \p nc_off and \p
491 * stream_block across calls to this function as they will be
492 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100493 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200494 * With this strategy, you must not encrypt more than 2**128
495 * blocks of data with the same key.
496 *
497 * 2. You can encrypt separate messages by dividing the \p
498 * nonce_counter buffer in two areas: the first one used for a
499 * per-message nonce, handled by yourself, and the second one
500 * updated by this function internally.
501 *
502 * For example, you might reserve the first 12 bytes for the
503 * per-message nonce, and the last 4 bytes for internal use. In that
504 * case, before calling this function on a new message you need to
505 * set the first 12 bytes of \p nonce_counter to your chosen nonce
506 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
507 * stream_block to be ignored). That way, you can encrypt at most
508 * 2**96 messages of up to 2**32 blocks each with the same key.
509 *
510 * The per-message nonce (or information sufficient to reconstruct
511 * it) needs to be communicated with the ciphertext and must be unique.
512 * The recommended way to ensure uniqueness is to use a message
513 * counter. An alternative is to generate random nonces, but this
514 * limits the number of messages that can be securely encrypted:
515 * for example, with 96-bit random nonces, you should not encrypt
516 * more than 2**32 messages with the same key.
517 *
518 * Note that for both stategies, sizes are measured in blocks and
519 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000520 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200521 * \warning Upon return, \p stream_block contains sensitive data. Its
522 * content must not be written to insecure storage and should be
523 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000524 *
Rose Zadik7f441272018-01-22 11:48:23 +0000525 * \param ctx The AES context to use for encryption or decryption.
526 * \param length The length of the input data.
527 * \param nc_off The offset in the current \p stream_block, for
528 * resuming within the current cipher stream. The
529 * offset pointer should be 0 at the start of a stream.
530 * \param nonce_counter The 128-bit nonce and counter.
531 * \param stream_block The saved stream block for resuming. This is
532 * overwritten by the function.
533 * \param input The buffer holding the input data.
534 * \param output The buffer holding the output data.
535 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100536 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000537 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000539 size_t length,
540 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000541 unsigned char nonce_counter[16],
542 unsigned char stream_block[16],
543 const unsigned char *input,
544 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200546
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200547/**
Rose Zadik7f441272018-01-22 11:48:23 +0000548 * \brief Internal AES block encryption function. This is only
549 * exposed to allow overriding it using
550 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200551 *
Rose Zadik7f441272018-01-22 11:48:23 +0000552 * \param ctx The AES context to use for encryption.
553 * \param input The plaintext block.
554 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000555 *
Rose Zadik7f441272018-01-22 11:48:23 +0000556 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200557 */
Andres AGf5bf7182017-03-03 14:09:56 +0000558int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
559 const unsigned char input[16],
560 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200561
562/**
Rose Zadik7f441272018-01-22 11:48:23 +0000563 * \brief Internal AES block decryption function. This is only
564 * exposed to allow overriding it using see
565 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200566 *
Rose Zadik7f441272018-01-22 11:48:23 +0000567 * \param ctx The AES context to use for decryption.
568 * \param input The ciphertext block.
569 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000570 *
Rose Zadik7f441272018-01-22 11:48:23 +0000571 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200572 */
Andres AGf5bf7182017-03-03 14:09:56 +0000573int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
574 const unsigned char input[16],
575 unsigned char output[16] );
576
577#if !defined(MBEDTLS_DEPRECATED_REMOVED)
578#if defined(MBEDTLS_DEPRECATED_WARNING)
579#define MBEDTLS_DEPRECATED __attribute__((deprecated))
580#else
581#define MBEDTLS_DEPRECATED
582#endif
583/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100584 * \brief Deprecated internal AES block encryption function
585 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000586 *
Rose Zadik7f441272018-01-22 11:48:23 +0000587 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000588 *
Rose Zadik7f441272018-01-22 11:48:23 +0000589 * \param ctx The AES context to use for encryption.
590 * \param input Plaintext block.
591 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000592 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100593MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
594 const unsigned char input[16],
595 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000596
597/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100598 * \brief Deprecated internal AES block decryption function
599 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000600 *
Rose Zadik7f441272018-01-22 11:48:23 +0000601 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000602 *
Rose Zadik7f441272018-01-22 11:48:23 +0000603 * \param ctx The AES context to use for decryption.
604 * \param input Ciphertext block.
605 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000606 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100607MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
608 const unsigned char input[16],
609 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000610
611#undef MBEDTLS_DEPRECATED
612#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200613
Paul Bakker5121ce52009-01-03 21:22:43 +0000614/**
Rose Zadik7f441272018-01-22 11:48:23 +0000615 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000616 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100617 * \return \c 0 on success.
618 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000619 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000621
622#ifdef __cplusplus
623}
624#endif
625
626#endif /* aes.h */