blob: 8936f0a2752f4325aa0070024f52a7f5be9c0f0b [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>.
Darryl Greena40a1012018-01-05 15:33:17 +000016 */
Rose Zadik5ad7aea2018-03-26 12:00:09 +010017
Rose Zadik7f441272018-01-22 11:48:23 +000018/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020019 * SPDX-License-Identifier: Apache-2.0
20 *
21 * Licensed under the Apache License, Version 2.0 (the "License"); you may
22 * not use this file except in compliance with the License.
23 * You may obtain a copy of the License at
24 *
25 * http://www.apache.org/licenses/LICENSE-2.0
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000032 *
Rose Zadik7f441272018-01-22 11:48:23 +000033 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000034 */
Rose Zadik7f441272018-01-22 11:48:23 +000035
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#ifndef MBEDTLS_AES_H
37#define MBEDTLS_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020040#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020041#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020043#endif
Paul Bakker90995b52013-06-24 19:20:35 +020044
Rich Evans00ab4702015-02-06 13:43:58 +000045#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020046#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000047
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010048/* padlock.c and aesni.c rely on these values! */
Rose Zadik7f441272018-01-22 11:48:23 +000049#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
50#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
Paul Bakker5121ce52009-01-03 21:22:43 +000051
Andres Amaya Garciac5380642017-11-28 19:57:51 +000052/* Error codes in range 0x0020-0x0022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
54#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000055
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010056/* Error codes in range 0x0023-0x0025 */
Rose Zadik7f441272018-01-22 11:48:23 +000057#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010058#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
Simon Butcher1a925bc2018-05-14 13:58:22 +010059#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0027 /**< Invalid
60input data. */
61
62#if defined( MBEDTLS_CHECK_PARAMS )
63#define MBEDTLS_AES_VALIDATE( cond ) do{ if( !(cond) ) \
64 return MBEDTLS_ERR_AES_BAD_INPUT_DATA; \
65 } while(0);
66#else
67/* No validation of parameters will be performed */
68#define MBEDTLS_AES_VALIDATE( cond)
69#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Andres AGf5bf7182017-03-03 14:09:56 +000071#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
72 !defined(inline) && !defined(__cplusplus)
73#define inline __inline
74#endif
75
Paul Bakker407a0da2013-06-27 14:29:21 +020076#ifdef __cplusplus
77extern "C" {
78#endif
79
Ron Eldorb2aacec2017-05-18 16:53:08 +030080#if !defined(MBEDTLS_AES_ALT)
81// Regular implementation
82//
83
Paul Bakker5121ce52009-01-03 21:22:43 +000084/**
Rose Zadik7f441272018-01-22 11:48:23 +000085 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000086 */
87typedef struct
88{
Rose Zadik7f441272018-01-22 11:48:23 +000089 int nr; /*!< The number of rounds. */
90 uint32_t *rk; /*!< AES round keys. */
91 uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
92 hold 32 extra Bytes, which can be used for
93 one of the following purposes:
94 <ul><li>Alignment if VIA padlock is
95 used.</li>
96 <li>Simplifying key expansion in the 256-bit
97 case by generating an extra round key.
98 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000099}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Jaeden Amero9d3eba42018-04-28 15:02:45 +0100102#if defined(MBEDTLS_CIPHER_MODE_XTS)
103/**
104 * \brief The AES XTS context-type definition.
105 */
106typedef struct
107{
108 mbedtls_aes_context crypt; /*!< The AES context to use for AES block
109 encryption or decryption. */
110 mbedtls_aes_context tweak; /*!< The AES context used for tweak 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 *
124 * \param ctx The AES context to initialize.
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 *
Rose Zadik7f441272018-01-22 11:48:23 +0000131 * \param ctx The AES context to clear.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200132 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200134
Jaeden Amero9d3eba42018-04-28 15:02:45 +0100135#if defined(MBEDTLS_CIPHER_MODE_XTS)
136/**
137 * \brief This function initializes the specified AES XTS context.
138 *
139 * It must be the first API called before using
140 * the context.
141 *
142 * \param ctx The AES XTS context to initialize.
143 */
144void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
145
146/**
147 * \brief This function releases and clears the specified AES XTS context.
148 *
149 * \param ctx The AES XTS context to clear.
150 */
151void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
152#endif /* MBEDTLS_CIPHER_MODE_XTS */
153
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200154/**
Rose Zadik7f441272018-01-22 11:48:23 +0000155 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 *
Rose Zadik7f441272018-01-22 11:48:23 +0000157 * \param ctx The AES context to which the key should be bound.
158 * \param key The encryption key.
159 * \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.
174 * \param key The decryption key.
175 * \param keybits The size of data passed. Valid options are:
176 * <ul><li>128 bits</li>
177 * <li>192 bits</li>
178 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000179 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100180 * \return \c 0 on success.
181 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200184 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000185
Jaeden Amero9d3eba42018-04-28 15:02:45 +0100186#if defined(MBEDTLS_CIPHER_MODE_XTS)
187/**
188 * \brief This function sets the encryption key.
189 *
190 * \param ctx The AES XTS context to which the key should be bound.
191 * \param key The encryption key. This is comprised of the XTS key1
192 * concatenated with the XTS key2.
193 * \param keybits The size of data passed in bits. Valid options are:
194 * <ul><li>256 bits</li>
195 * <li>384 bits</li>
196 * <li>512 bits</li></ul>
197 *
198 * \return \c 0 on success.
199 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
200 */
201int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
202 const unsigned char *key,
203 unsigned int keybits );
204
205/**
206 * \brief This function sets the decryption key.
207 *
208 * \param ctx The AES XTS context to which the key should be bound.
209 * \param key The decryption key. This is comprised of the XTS key1
210 * concatenated with the XTS key2.
211 * \param keybits The size of data passed. Valid options are:
212 * <ul><li>256 bits</li>
213 * <li>384 bits</li>
214 * <li>512 bits</li></ul>
215 *
216 * \return \c 0 on success.
217 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
218 */
219int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
220 const unsigned char *key,
221 unsigned int keybits );
222#endif /* MBEDTLS_CIPHER_MODE_XTS */
223
Paul Bakker5121ce52009-01-03 21:22:43 +0000224/**
Rose Zadik7f441272018-01-22 11:48:23 +0000225 * \brief This function performs an AES single-block encryption or
226 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000227 *
Rose Zadik7f441272018-01-22 11:48:23 +0000228 * It performs the operation defined in the \p mode parameter
229 * (encrypt or decrypt), on the input data buffer defined in
230 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000231 *
Rose Zadik7f441272018-01-22 11:48:23 +0000232 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
233 * mbedtls_aes_setkey_dec() must be called before the first
234 * call to this API with the same context.
235 *
236 * \param ctx The AES context to use for encryption or decryption.
237 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
238 * #MBEDTLS_AES_DECRYPT.
239 * \param input The 16-Byte buffer holding the input data.
240 * \param output The 16-Byte buffer holding the output data.
241
242 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000246 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 unsigned char output[16] );
248
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000250/**
Rose Zadik7f441272018-01-22 11:48:23 +0000251 * \brief This function performs an AES-CBC encryption or decryption operation
252 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 *
Rose Zadik7f441272018-01-22 11:48:23 +0000254 * It performs the operation defined in the \p mode
255 * parameter (encrypt/decrypt), on the input data buffer defined in
256 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000257 *
Rose Zadik7f441272018-01-22 11:48:23 +0000258 * It can be called as many times as needed, until all the input
259 * data is processed. mbedtls_aes_init(), and either
260 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
261 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000262 *
Rose Zadik7f441272018-01-22 11:48:23 +0000263 * \note This function operates on aligned blocks, that is, the input size
264 * must be a multiple of the AES block size of 16 Bytes.
265 *
266 * \note Upon exit, the content of the IV is updated so that you can
267 * call the same function again on the next
268 * block(s) of data and get the same result as if it was
269 * encrypted in one call. This allows a "streaming" usage.
270 * If you need to retain the contents of the IV, you should
271 * either save it manually or use the cipher module instead.
272 *
273 *
274 * \param ctx The AES context to use for encryption or decryption.
275 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
276 * #MBEDTLS_AES_DECRYPT.
277 * \param length The length of the input data in Bytes. This must be a
278 * multiple of the block size (16 Bytes).
279 * \param iv Initialization vector (updated after use).
280 * \param input The buffer holding the input data.
281 * \param output The buffer holding the output data.
282 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100283 * \return \c 0 on success.
284 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000285 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000286 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000289 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000291 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000296/**
Rose Zadik7f441272018-01-22 11:48:23 +0000297 * \brief This function performs an AES-CFB128 encryption or decryption
298 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000299 *
Rose Zadik7f441272018-01-22 11:48:23 +0000300 * It performs the operation defined in the \p mode
301 * parameter (encrypt or decrypt), on the input data buffer
302 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000303 *
Rose Zadik7f441272018-01-22 11:48:23 +0000304 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
305 * regardless of whether you are performing an encryption or decryption
306 * operation, that is, regardless of the \p mode parameter. This is
307 * because CFB mode uses the same key schedule for encryption and
308 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000309 *
Rose Zadik7f441272018-01-22 11:48:23 +0000310 * \note Upon exit, the content of the IV is updated so that you can
311 * call the same function again on the next
312 * block(s) of data and get the same result as if it was
313 * encrypted in one call. This allows a "streaming" usage.
314 * If you need to retain the contents of the
315 * IV, you must either save it manually or use the cipher
316 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000317 *
Rose Zadik7f441272018-01-22 11:48:23 +0000318 *
319 * \param ctx The AES context to use for encryption or decryption.
320 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
321 * #MBEDTLS_AES_DECRYPT.
322 * \param length The length of the input data.
323 * \param iv_off The offset in IV (updated after use).
324 * \param iv The initialization vector (updated after use).
325 * \param input The buffer holding the input data.
326 * \param output The buffer holding the output data.
327 *
328 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000329 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200330int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000331 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000332 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000333 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000334 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000335 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000336 unsigned char *output );
337
Paul Bakker9a736322012-11-14 12:39:52 +0000338/**
Rose Zadik7f441272018-01-22 11:48:23 +0000339 * \brief This function performs an AES-CFB8 encryption or decryption
340 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100341 *
Rose Zadik7f441272018-01-22 11:48:23 +0000342 * It performs the operation defined in the \p mode
343 * parameter (encrypt/decrypt), on the input data buffer defined
344 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100345 *
Rose Zadik7f441272018-01-22 11:48:23 +0000346 * Due to the nature of CFB, you must use the same key schedule for
347 * both encryption and decryption operations. Therefore, you must
348 * use the context initialized with mbedtls_aes_setkey_enc() for
349 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000350 *
Rose Zadik7f441272018-01-22 11:48:23 +0000351 * \note Upon exit, the content of the IV is updated so that you can
352 * call the same function again on the next
353 * block(s) of data and get the same result as if it was
354 * encrypted in one call. This allows a "streaming" usage.
355 * If you need to retain the contents of the
356 * IV, you should either save it manually or use the cipher
357 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100358 *
Rose Zadik7f441272018-01-22 11:48:23 +0000359 *
360 * \param ctx The AES context to use for encryption or decryption.
361 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
362 * #MBEDTLS_AES_DECRYPT
363 * \param length The length of the input data.
364 * \param iv The initialization vector (updated after use).
365 * \param input The buffer holding the input data.
366 * \param output The buffer holding the output data.
367 *
368 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100369 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200370int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100371 int mode,
372 size_t length,
373 unsigned char iv[16],
374 const unsigned char *input,
375 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200376#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100377
Simon Butcherfb9f6612018-04-22 22:57:27 +0100378#if defined(MBEDTLS_CIPHER_MODE_OFB)
379/**
380 * \brief This function performs an AES-OFB (Output Feedback Mode) encryption
381 * or decryption operation.
382 *
383 * For OFB, you must set up the context with mbedtls_aes_setkey_enc(),
384 * regardless of whether you are performing an encryption or decryption
385 * operation. This is because OFB mode uses the same key schedule for
386 * encryption and decryption.
387 *
388 * The OFB operation is identical for encryption or decryption, therefore
389 * no operation mode needs to be specified.
390 *
391 * \note Upon exit, the content of the IV is updated so that you can
392 * call the same function again on the next
393 * block(s) of data and get the same result as if it was
394 * encrypted in one call. This allows a "streaming" usage.
395 * If you need to retain the contents of the
396 * IV, you must either save it manually or use the cipher
397 * module instead.
398 *
399 *
400 * \param ctx The AES context to use for encryption or decryption.
401 * \param length The length of the input data.
402 * \param iv_off The offset in IV (updated after use).
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.
408 */
409int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
410 size_t length,
411 size_t *iv_off,
412 unsigned char iv[16],
413 const unsigned char *input,
414 unsigned char *output );
415
416#endif /* MBEDTLS_CIPHER_MODE_OFB */
417
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200418#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100419/**
Rose Zadik7f441272018-01-22 11:48:23 +0000420 * \brief This function performs an AES-CTR encryption or decryption
421 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000422 *
Rose Zadik7f441272018-01-22 11:48:23 +0000423 * This function performs the operation defined in the \p mode
424 * parameter (encrypt/decrypt), on the input data buffer
425 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000426 *
Rose Zadik7f441272018-01-22 11:48:23 +0000427 * Due to the nature of CTR, you must use the same key schedule
428 * for both encryption and decryption operations. Therefore, you
429 * must use the context initialized with mbedtls_aes_setkey_enc()
430 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000431 *
Rose Zadik7f441272018-01-22 11:48:23 +0000432 * \warning You must keep the maximum use of your counter in mind.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000433 *
Rose Zadik7f441272018-01-22 11:48:23 +0000434 * \param ctx The AES context to use for encryption or decryption.
435 * \param length The length of the input data.
436 * \param nc_off The offset in the current \p stream_block, for
437 * resuming within the current cipher stream. The
438 * offset pointer should be 0 at the start of a stream.
439 * \param nonce_counter The 128-bit nonce and counter.
440 * \param stream_block The saved stream block for resuming. This is
441 * overwritten by the function.
442 * \param input The buffer holding the input data.
443 * \param output The buffer holding the output data.
444 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100445 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000446 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200447int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000448 size_t length,
449 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000450 unsigned char nonce_counter[16],
451 unsigned char stream_block[16],
452 const unsigned char *input,
453 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200454#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200455
Jaeden Amero9d3eba42018-04-28 15:02:45 +0100456#if defined(MBEDTLS_CIPHER_MODE_XTS)
457/**
458 * \brief This function performs an AES-XTS encryption or decryption
459 * operation for an entire XTS sector.
460 *
461 * AES-XTS encrypts or decrypts blocks based on their location as
462 * defined by a sector number. These must be provided by \p sector.
463 *
464 * NIST SP 800-38E limits the maximum size of a data unit to 2**20
465 * AES blocks. If the data unit is larger than this, this function
466 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
467 *
468 * \param ctx The AES XTS context to use for AES XTS operations.
469 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
470 * #MBEDTLS_AES_DECRYPT.
471 * \param length The length of both an entire sector and the input data
472 * in bytes. The length must be at least 16 bytes. The
473 * length does not need to be a multiple of 16 bytes.
474 * \param data_unit The data unit (commonly a block device sector) address
475 * in byte array form. The least significant byte must be
476 * at sector[0]. The most significant byte must be at
477 * sector[15]. Array must be 16 bytes in length.
478 * \param input The buffer holding the input data.
479 * \param output The buffer holding the output data.
480 *
481 * \return \c 0 on success.
482 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if length smaller
483 * than an AES block in size (16 bytes) or if the length is
484 * larger than 2**20 blocks.
485 */
486int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
487 int mode,
488 size_t length,
489 const unsigned char data_unit[16],
490 const unsigned char *input,
491 unsigned char *output );
492#endif /* MBEDTLS_CIPHER_MODE_XTS */
493
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200494/**
Rose Zadik7f441272018-01-22 11:48:23 +0000495 * \brief Internal AES block encryption function. This is only
496 * exposed to allow overriding it using
497 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200498 *
Rose Zadik7f441272018-01-22 11:48:23 +0000499 * \param ctx The AES context to use for encryption.
500 * \param input The plaintext block.
501 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000502 *
Rose Zadik7f441272018-01-22 11:48:23 +0000503 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200504 */
Andres AGf5bf7182017-03-03 14:09:56 +0000505int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
506 const unsigned char input[16],
507 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200508
509/**
Rose Zadik7f441272018-01-22 11:48:23 +0000510 * \brief Internal AES block decryption function. This is only
511 * exposed to allow overriding it using see
512 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200513 *
Rose Zadik7f441272018-01-22 11:48:23 +0000514 * \param ctx The AES context to use for decryption.
515 * \param input The ciphertext block.
516 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000517 *
Rose Zadik7f441272018-01-22 11:48:23 +0000518 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200519 */
Andres AGf5bf7182017-03-03 14:09:56 +0000520int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
521 const unsigned char input[16],
522 unsigned char output[16] );
523
524#if !defined(MBEDTLS_DEPRECATED_REMOVED)
525#if defined(MBEDTLS_DEPRECATED_WARNING)
526#define MBEDTLS_DEPRECATED __attribute__((deprecated))
527#else
528#define MBEDTLS_DEPRECATED
529#endif
530/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100531 * \brief Deprecated internal AES block encryption function
532 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000533 *
Rose Zadik7f441272018-01-22 11:48:23 +0000534 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000535 *
Rose Zadik7f441272018-01-22 11:48:23 +0000536 * \param ctx The AES context to use for encryption.
537 * \param input Plaintext block.
538 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000539 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100540MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
541 const unsigned char input[16],
542 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000543
544/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100545 * \brief Deprecated internal AES block decryption function
546 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000547 *
Rose Zadik7f441272018-01-22 11:48:23 +0000548 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000549 *
Rose Zadik7f441272018-01-22 11:48:23 +0000550 * \param ctx The AES context to use for decryption.
551 * \param input Ciphertext block.
552 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000553 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100554MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
555 const unsigned char input[16],
556 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000557
558#undef MBEDTLS_DEPRECATED
559#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200560
Paul Bakker5121ce52009-01-03 21:22:43 +0000561/**
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000563 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100564 * \return \c 0 on success.
565 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000566 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200567int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000568
569#ifdef __cplusplus
570}
571#endif
572
573#endif /* aes.h */