blob: 62c1f9234e4ac84183f3996b9525d7b751e4c8cb [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
Simon Butcher5201e412018-12-06 17:40:14 +000070#if defined( MBEDTLS_CHECK_PARAMS )
71#define MBEDTLS_AES_VALIDATE_RET( cond ) do{ if( !(cond) ) { \
Manuel Pégourié-Gonnard8e661bf2018-12-10 12:41:46 +010072 MBEDTLS_PARAM_FAILED( #cond ); \
Simon Butcher5201e412018-12-06 17:40:14 +000073 return MBEDTLS_ERR_AES_BAD_INPUT_DATA;} \
74 } while(0);
75
76#define MBEDTLS_AES_VALIDATE( cond ) do{ if( !(cond) ) { \
Manuel Pégourié-Gonnard8e661bf2018-12-10 12:41:46 +010077 MBEDTLS_PARAM_FAILED( #cond ); \
Simon Butcher5201e412018-12-06 17:40:14 +000078 return; } \
79 } while(0);
80#else
81/* No validation of parameters will be performed */
82#define MBEDTLS_AES_VALIDATE_RET( cond )
83#define MBEDTLS_AES_VALIDATE( cond)
84#endif
85
Andres AGf5bf7182017-03-03 14:09:56 +000086#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
87 !defined(inline) && !defined(__cplusplus)
88#define inline __inline
89#endif
90
Paul Bakker407a0da2013-06-27 14:29:21 +020091#ifdef __cplusplus
92extern "C" {
93#endif
94
Ron Eldorb2aacec2017-05-18 16:53:08 +030095#if !defined(MBEDTLS_AES_ALT)
96// Regular implementation
97//
98
Paul Bakker5121ce52009-01-03 21:22:43 +000099/**
Rose Zadik7f441272018-01-22 11:48:23 +0000100 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200102typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +0000103{
Rose Zadik7f441272018-01-22 11:48:23 +0000104 int nr; /*!< The number of rounds. */
105 uint32_t *rk; /*!< AES round keys. */
106 uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
107 hold 32 extra Bytes, which can be used for
108 one of the following purposes:
109 <ul><li>Alignment if VIA padlock is
110 used.</li>
111 <li>Simplifying key expansion in the 256-bit
112 case by generating an extra round key.
113 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +0000114}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Jaeden Amero9366feb2018-05-29 18:55:17 +0100117#if defined(MBEDTLS_CIPHER_MODE_XTS)
118/**
119 * \brief The AES XTS context-type definition.
120 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200121typedef struct mbedtls_aes_xts_context
Jaeden Amero9366feb2018-05-29 18:55:17 +0100122{
123 mbedtls_aes_context crypt; /*!< The AES context to use for AES block
124 encryption or decryption. */
125 mbedtls_aes_context tweak; /*!< The AES context used for tweak
126 computation. */
127} mbedtls_aes_xts_context;
128#endif /* MBEDTLS_CIPHER_MODE_XTS */
129
Ron Eldorb2aacec2017-05-18 16:53:08 +0300130#else /* MBEDTLS_AES_ALT */
131#include "aes_alt.h"
132#endif /* MBEDTLS_AES_ALT */
133
Paul Bakker5121ce52009-01-03 21:22:43 +0000134/**
Rose Zadik7f441272018-01-22 11:48:23 +0000135 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200136 *
Rose Zadik7f441272018-01-22 11:48:23 +0000137 * It must be the first API called before using
138 * the context.
139 *
140 * \param ctx The AES context to initialize.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200143
144/**
Rose Zadik7f441272018-01-22 11:48:23 +0000145 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200146 *
Rose Zadik7f441272018-01-22 11:48:23 +0000147 * \param ctx The AES context to clear.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200150
Jaeden Amero9366feb2018-05-29 18:55:17 +0100151#if defined(MBEDTLS_CIPHER_MODE_XTS)
152/**
153 * \brief This function initializes the specified AES XTS context.
154 *
155 * It must be the first API called before using
156 * the context.
157 *
158 * \param ctx The AES XTS context to initialize.
159 */
160void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
161
162/**
163 * \brief This function releases and clears the specified AES XTS context.
164 *
165 * \param ctx The AES XTS context to clear.
166 */
167void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
168#endif /* MBEDTLS_CIPHER_MODE_XTS */
169
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200170/**
Rose Zadik7f441272018-01-22 11:48:23 +0000171 * \brief This function sets the encryption 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 encryption key.
175 * \param keybits The size of data passed in bits. 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.
Rose Zadik819d13d2018-04-16 09:35:15 +0100181 * \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_enc( 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
186/**
Rose Zadik7f441272018-01-22 11:48:23 +0000187 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 *
Rose Zadik7f441272018-01-22 11:48:23 +0000189 * \param ctx The AES context to which the key should be bound.
190 * \param key The decryption key.
191 * \param keybits The size of data passed. Valid options are:
192 * <ul><li>128 bits</li>
193 * <li>192 bits</li>
194 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000195 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100196 * \return \c 0 on success.
197 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000198 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200200 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
Jaeden Amero9366feb2018-05-29 18:55:17 +0100202#if defined(MBEDTLS_CIPHER_MODE_XTS)
203/**
204 * \brief This function prepares an XTS context for encryption and
205 * sets the encryption key.
206 *
207 * \param ctx The AES XTS context to which the key should be bound.
208 * \param key The encryption key. This is comprised of the XTS key1
209 * concatenated with the XTS key2.
210 * \param keybits The size of \p key passed in bits. Valid options are:
211 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
212 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
213 *
214 * \return \c 0 on success.
215 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
216 */
217int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
218 const unsigned char *key,
219 unsigned int keybits );
220
221/**
222 * \brief This function prepares an XTS context for decryption and
223 * sets the decryption key.
224 *
225 * \param ctx The AES XTS context to which the key should be bound.
226 * \param key The decryption key. This is comprised of the XTS key1
227 * concatenated with the XTS key2.
228 * \param keybits The size of \p key passed in bits. Valid options are:
229 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
230 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
231 *
232 * \return \c 0 on success.
233 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
234 */
235int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
236 const unsigned char *key,
237 unsigned int keybits );
238#endif /* MBEDTLS_CIPHER_MODE_XTS */
239
Paul Bakker5121ce52009-01-03 21:22:43 +0000240/**
Rose Zadik7f441272018-01-22 11:48:23 +0000241 * \brief This function performs an AES single-block encryption or
242 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 *
Rose Zadik7f441272018-01-22 11:48:23 +0000244 * It performs the operation defined in the \p mode parameter
245 * (encrypt or decrypt), on the input data buffer defined in
246 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000247 *
Rose Zadik7f441272018-01-22 11:48:23 +0000248 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
249 * mbedtls_aes_setkey_dec() must be called before the first
250 * call to this API with the same context.
251 *
252 * \param ctx The AES context to use for encryption or decryption.
253 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
254 * #MBEDTLS_AES_DECRYPT.
255 * \param input The 16-Byte buffer holding the input data.
256 * \param output The 16-Byte buffer holding the output data.
257
258 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000259 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200260int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000261 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000262 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000263 unsigned char output[16] );
264
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000266/**
Rose Zadik7f441272018-01-22 11:48:23 +0000267 * \brief This function performs an AES-CBC encryption or decryption operation
268 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000269 *
Rose Zadik7f441272018-01-22 11:48:23 +0000270 * It performs the operation defined in the \p mode
271 * parameter (encrypt/decrypt), on the input data buffer defined in
272 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000273 *
Rose Zadik7f441272018-01-22 11:48:23 +0000274 * It can be called as many times as needed, until all the input
275 * data is processed. mbedtls_aes_init(), and either
276 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
277 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000278 *
Rose Zadik7f441272018-01-22 11:48:23 +0000279 * \note This function operates on aligned blocks, that is, the input size
280 * must be a multiple of the AES block size of 16 Bytes.
281 *
282 * \note Upon exit, the content of the IV is updated so that you can
283 * call the same function again on the next
284 * block(s) of data and get the same result as if it was
285 * encrypted in one call. This allows a "streaming" usage.
286 * If you need to retain the contents of the IV, you should
287 * either save it manually or use the cipher module instead.
288 *
289 *
290 * \param ctx The AES context to use for encryption or decryption.
291 * \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).
296 * \param input The buffer holding the input data.
297 * \param output The buffer holding the output data.
298 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100299 * \return \c 0 on success.
300 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000301 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000305 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000306 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000307 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000308 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200309#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000310
Aorimn5f778012016-06-09 23:22:58 +0200311#if defined(MBEDTLS_CIPHER_MODE_XTS)
312/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100313 * \brief This function performs an AES-XTS encryption or decryption
314 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200315 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100316 * AES-XTS encrypts or decrypts blocks based on their location as
317 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100318 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200319 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100320 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
321 * AES blocks. If the data unit is larger than this, this function
322 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
323 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100324 * \param ctx The AES XTS context to use for AES XTS operations.
325 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
326 * #MBEDTLS_AES_DECRYPT.
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100327 * \param length The length of a data unit in bytes. This can be any
328 * length between 16 bytes and 2^24 bytes inclusive
329 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100330 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100331 * bytes in little-endian format. For disk encryption, this
332 * is typically the index of the block device sector that
333 * contains the data.
334 * \param input The buffer holding the input data (which is an entire
335 * data unit). This function reads \p length bytes from \p
336 * input.
337 * \param output The buffer holding the output data (which is an entire
338 * data unit). This function writes \p length bytes to \p
339 * output.
Aorimn5f778012016-06-09 23:22:58 +0200340 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100341 * \return \c 0 on success.
342 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100343 * smaller than an AES block in size (16 bytes) or if \p
344 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200345 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100346int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
347 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100348 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100349 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100350 const unsigned char *input,
351 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200352#endif /* MBEDTLS_CIPHER_MODE_XTS */
353
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200354#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000355/**
Rose Zadik7f441272018-01-22 11:48:23 +0000356 * \brief This function performs an AES-CFB128 encryption or decryption
357 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 *
Rose Zadik7f441272018-01-22 11:48:23 +0000359 * It performs the operation defined in the \p mode
360 * parameter (encrypt or decrypt), on the input data buffer
361 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000362 *
Rose Zadik7f441272018-01-22 11:48:23 +0000363 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
364 * regardless of whether you are performing an encryption or decryption
365 * operation, that is, regardless of the \p mode parameter. This is
366 * because CFB mode uses the same key schedule for encryption and
367 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000368 *
Rose Zadik7f441272018-01-22 11:48:23 +0000369 * \note Upon exit, the content of the IV is updated so that you can
370 * call the same function again on the next
371 * block(s) of data and get the same result as if it was
372 * encrypted in one call. This allows a "streaming" usage.
373 * If you need to retain the contents of the
374 * IV, you must either save it manually or use the cipher
375 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000376 *
Rose Zadik7f441272018-01-22 11:48:23 +0000377 *
378 * \param ctx The AES context to use for encryption or decryption.
379 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
380 * #MBEDTLS_AES_DECRYPT.
381 * \param length The length of the input data.
382 * \param iv_off The offset in IV (updated after use).
383 * \param iv The initialization vector (updated after use).
384 * \param input The buffer holding the input data.
385 * \param output The buffer holding the output data.
386 *
387 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000388 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200389int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000390 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000391 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000392 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000393 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000394 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000395 unsigned char *output );
396
Paul Bakker9a736322012-11-14 12:39:52 +0000397/**
Rose Zadik7f441272018-01-22 11:48:23 +0000398 * \brief This function performs an AES-CFB8 encryption or decryption
399 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100400 *
Rose Zadik7f441272018-01-22 11:48:23 +0000401 * It performs the operation defined in the \p mode
402 * parameter (encrypt/decrypt), on the input data buffer defined
403 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100404 *
Rose Zadik7f441272018-01-22 11:48:23 +0000405 * Due to the nature of CFB, you must use the same key schedule for
406 * both encryption and decryption operations. Therefore, you must
407 * use the context initialized with mbedtls_aes_setkey_enc() for
408 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000409 *
Rose Zadik7f441272018-01-22 11:48:23 +0000410 * \note Upon exit, the content of the IV is updated so that you can
411 * call the same function again on the next
412 * block(s) of data and get the same result as if it was
413 * encrypted in one call. This allows a "streaming" usage.
414 * If you need to retain the contents of the
415 * IV, you should either save it manually or use the cipher
416 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100417 *
Rose Zadik7f441272018-01-22 11:48:23 +0000418 *
419 * \param ctx The AES context to use for encryption or decryption.
420 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
421 * #MBEDTLS_AES_DECRYPT
422 * \param length The length of the input data.
423 * \param iv The initialization vector (updated after use).
424 * \param input The buffer holding the input data.
425 * \param output The buffer holding the output data.
426 *
427 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100428 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200429int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100430 int mode,
431 size_t length,
432 unsigned char iv[16],
433 const unsigned char *input,
434 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200435#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100436
Simon Butcher76a5b222018-04-22 22:57:27 +0100437#if defined(MBEDTLS_CIPHER_MODE_OFB)
438/**
Simon Butcher5db13622018-06-04 22:11:25 +0100439 * \brief This function performs an AES-OFB (Output Feedback Mode)
440 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100441 *
Simon Butcher5db13622018-06-04 22:11:25 +0100442 * For OFB, you must set up the context with
443 * mbedtls_aes_setkey_enc(), regardless of whether you are
444 * performing an encryption or decryption operation. This is
445 * because OFB mode uses the same key schedule for encryption and
446 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100447 *
Simon Butcher5db13622018-06-04 22:11:25 +0100448 * The OFB operation is identical for encryption or decryption,
449 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100450 *
Simon Butcher5db13622018-06-04 22:11:25 +0100451 * \note Upon exit, the content of iv, the Initialisation Vector, is
452 * updated so that you can call the same function again on the next
453 * block(s) of data and get the same result as if it was encrypted
454 * in one call. This allows a "streaming" usage, by initialising
455 * iv_off to 0 before the first call, and preserving its value
456 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100457 *
Simon Butcher5db13622018-06-04 22:11:25 +0100458 * For non-streaming use, the iv should be initialised on each call
459 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100460 *
Simon Butcher5db13622018-06-04 22:11:25 +0100461 * If you need to retain the contents of the initialisation vector,
462 * you must either save it manually or use the cipher module
463 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100464 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100465 * \warning For the OFB mode, the initialisation vector must be unique
466 * every encryption operation. Reuse of an initialisation vector
467 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100468 *
469 * \param ctx The AES context to use for encryption or decryption.
470 * \param length The length of the input data.
471 * \param iv_off The offset in IV (updated after use).
472 * \param iv The initialization vector (updated after use).
473 * \param input The buffer holding the input data.
474 * \param output The buffer holding the output data.
475 *
476 * \return \c 0 on success.
477 */
478int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
479 size_t length,
480 size_t *iv_off,
481 unsigned char iv[16],
482 const unsigned char *input,
483 unsigned char *output );
484
485#endif /* MBEDTLS_CIPHER_MODE_OFB */
486
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100488/**
Rose Zadik7f441272018-01-22 11:48:23 +0000489 * \brief This function performs an AES-CTR encryption or decryption
490 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000491 *
Rose Zadik7f441272018-01-22 11:48:23 +0000492 * This function performs the operation defined in the \p mode
493 * parameter (encrypt/decrypt), on the input data buffer
494 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000495 *
Rose Zadik7f441272018-01-22 11:48:23 +0000496 * Due to the nature of CTR, you must use the same key schedule
497 * for both encryption and decryption operations. Therefore, you
498 * must use the context initialized with mbedtls_aes_setkey_enc()
499 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000500 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100501 * \warning You must never reuse a nonce value with the same key. Doing so
502 * would void the encryption for the two messages encrypted with
503 * the same nonce and key.
504 *
505 * There are two common strategies for managing nonces with CTR:
506 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200507 * 1. You can handle everything as a single message processed over
508 * successive calls to this function. In that case, you want to
509 * set \p nonce_counter and \p nc_off to 0 for the first call, and
510 * then preserve the values of \p nonce_counter, \p nc_off and \p
511 * stream_block across calls to this function as they will be
512 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100513 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200514 * With this strategy, you must not encrypt more than 2**128
515 * blocks of data with the same key.
516 *
517 * 2. You can encrypt separate messages by dividing the \p
518 * nonce_counter buffer in two areas: the first one used for a
519 * per-message nonce, handled by yourself, and the second one
520 * updated by this function internally.
521 *
522 * For example, you might reserve the first 12 bytes for the
523 * per-message nonce, and the last 4 bytes for internal use. In that
524 * case, before calling this function on a new message you need to
525 * set the first 12 bytes of \p nonce_counter to your chosen nonce
526 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
527 * stream_block to be ignored). That way, you can encrypt at most
528 * 2**96 messages of up to 2**32 blocks each with the same key.
529 *
530 * The per-message nonce (or information sufficient to reconstruct
531 * it) needs to be communicated with the ciphertext and must be unique.
532 * The recommended way to ensure uniqueness is to use a message
533 * counter. An alternative is to generate random nonces, but this
534 * limits the number of messages that can be securely encrypted:
535 * for example, with 96-bit random nonces, you should not encrypt
536 * more than 2**32 messages with the same key.
537 *
538 * Note that for both stategies, sizes are measured in blocks and
539 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000540 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200541 * \warning Upon return, \p stream_block contains sensitive data. Its
542 * content must not be written to insecure storage and should be
543 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000544 *
Rose Zadik7f441272018-01-22 11:48:23 +0000545 * \param ctx The AES context to use for encryption or decryption.
546 * \param length The length of the input data.
547 * \param nc_off The offset in the current \p stream_block, for
548 * resuming within the current cipher stream. The
549 * offset pointer should be 0 at the start of a stream.
550 * \param nonce_counter The 128-bit nonce and counter.
551 * \param stream_block The saved stream block for resuming. This is
552 * overwritten by the function.
553 * \param input The buffer holding the input data.
554 * \param output The buffer holding the output data.
555 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100556 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000557 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000559 size_t length,
560 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000561 unsigned char nonce_counter[16],
562 unsigned char stream_block[16],
563 const unsigned char *input,
564 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200565#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200566
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200567/**
Rose Zadik7f441272018-01-22 11:48:23 +0000568 * \brief Internal AES block encryption function. This is only
569 * exposed to allow overriding it using
570 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200571 *
Rose Zadik7f441272018-01-22 11:48:23 +0000572 * \param ctx The AES context to use for encryption.
573 * \param input The plaintext block.
574 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000575 *
Rose Zadik7f441272018-01-22 11:48:23 +0000576 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200577 */
Andres AGf5bf7182017-03-03 14:09:56 +0000578int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
579 const unsigned char input[16],
580 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200581
582/**
Rose Zadik7f441272018-01-22 11:48:23 +0000583 * \brief Internal AES block decryption function. This is only
584 * exposed to allow overriding it using see
585 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200586 *
Rose Zadik7f441272018-01-22 11:48:23 +0000587 * \param ctx The AES context to use for decryption.
588 * \param input The ciphertext block.
589 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000590 *
Rose Zadik7f441272018-01-22 11:48:23 +0000591 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200592 */
Andres AGf5bf7182017-03-03 14:09:56 +0000593int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
594 const unsigned char input[16],
595 unsigned char output[16] );
596
597#if !defined(MBEDTLS_DEPRECATED_REMOVED)
598#if defined(MBEDTLS_DEPRECATED_WARNING)
599#define MBEDTLS_DEPRECATED __attribute__((deprecated))
600#else
601#define MBEDTLS_DEPRECATED
602#endif
603/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100604 * \brief Deprecated internal AES block encryption function
605 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000606 *
Rose Zadik7f441272018-01-22 11:48:23 +0000607 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000608 *
Rose Zadik7f441272018-01-22 11:48:23 +0000609 * \param ctx The AES context to use for encryption.
610 * \param input Plaintext block.
611 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000612 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100613MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
614 const unsigned char input[16],
615 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000616
617/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100618 * \brief Deprecated internal AES block decryption function
619 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000620 *
Rose Zadik7f441272018-01-22 11:48:23 +0000621 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000622 *
Rose Zadik7f441272018-01-22 11:48:23 +0000623 * \param ctx The AES context to use for decryption.
624 * \param input Ciphertext block.
625 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000626 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100627MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
628 const unsigned char input[16],
629 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000630
631#undef MBEDTLS_DEPRECATED
632#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200633
Paul Bakker5121ce52009-01-03 21:22:43 +0000634/**
Rose Zadik7f441272018-01-22 11:48:23 +0000635 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000636 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100637 * \return \c 0 on success.
638 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000639 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000641
642#ifdef __cplusplus
643}
644#endif
645
646#endif /* aes.h */