blob: da7ab5496e45ac2ae90b21d93597b80934533f2d [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +01004 * \brief This file contains AES definitions and functions.
5 *
6 * The Advanced Encryption Standard (AES) specifies a FIPS-approved
Rose Zadik7f441272018-01-22 11:48:23 +00007 * cryptographic algorithm that can be used to protect electronic
8 * data.
9 *
10 * The AES algorithm is a symmetric block cipher that can
11 * encrypt and decrypt information. For more information, see
12 * <em>FIPS Publication 197: Advanced Encryption Standard</em> and
13 * <em>ISO/IEC 18033-2:2006: Information technology -- Security
14 * techniques -- Encryption algorithms -- Part 2: Asymmetric
15 * ciphers</em>.
Jaeden Amerof167deb2018-05-30 19:20:48 +010016 *
17 * The AES-XTS block mode is standardized by NIST SP 800-38E
18 * <https://nvlpubs.nist.gov/nistpubs/legacy/sp/nistspecialpublication800-38e.pdf>
19 * and described in detail by IEEE P1619
20 * <https://ieeexplore.ieee.org/servlet/opac?punumber=4375278>.
Darryl Greena40a1012018-01-05 15:33:17 +000021 */
Rose Zadik5ad7aea2018-03-26 12:00:09 +010022
Rose Zadik7f441272018-01-22 11:48:23 +000023/* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +020024 * SPDX-License-Identifier: Apache-2.0
25 *
26 * Licensed under the Apache License, Version 2.0 (the "License"); you may
27 * not use this file except in compliance with the License.
28 * You may obtain a copy of the License at
29 *
30 * http://www.apache.org/licenses/LICENSE-2.0
31 *
32 * Unless required by applicable law or agreed to in writing, software
33 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
34 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35 * See the License for the specific language governing permissions and
36 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000037 *
Rose Zadik7f441272018-01-22 11:48:23 +000038 * This file is part of Mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000039 */
Rose Zadik7f441272018-01-22 11:48:23 +000040
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#ifndef MBEDTLS_AES_H
42#define MBEDTLS_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000043
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020045#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020046#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020048#endif
Paul Bakker90995b52013-06-24 19:20:35 +020049
Rich Evans00ab4702015-02-06 13:43:58 +000050#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020051#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000052
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010053/* padlock.c and aesni.c rely on these values! */
Rose Zadik7f441272018-01-22 11:48:23 +000054#define MBEDTLS_AES_ENCRYPT 1 /**< AES encryption. */
55#define MBEDTLS_AES_DECRYPT 0 /**< AES decryption. */
Paul Bakker5121ce52009-01-03 21:22:43 +000056
Andres Amaya Garciac5380642017-11-28 19:57:51 +000057/* Error codes in range 0x0020-0x0022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
59#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000060
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000061/* Error codes in range 0x0021-0x0025 */
62#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 /**< Invalid input data. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030063
64/* MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE is deprecated and should not be used. */
Rose Zadik7f441272018-01-22 11:48:23 +000065#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030066
67/* MBEDTLS_ERR_AES_HW_ACCEL_FAILED is deprecated and should not be used. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010068#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Andres AGf5bf7182017-03-03 14:09:56 +000070#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
71 !defined(inline) && !defined(__cplusplus)
72#define inline __inline
73#endif
74
Paul Bakker407a0da2013-06-27 14:29:21 +020075#ifdef __cplusplus
76extern "C" {
77#endif
78
Ron Eldorb2aacec2017-05-18 16:53:08 +030079#if !defined(MBEDTLS_AES_ALT)
80// Regular implementation
81//
82
Paul Bakker5121ce52009-01-03 21:22:43 +000083/**
Rose Zadik7f441272018-01-22 11:48:23 +000084 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000085 */
Dawid Drozd428cc522018-07-24 10:02:47 +020086typedef struct mbedtls_aes_context
Paul Bakker5121ce52009-01-03 21:22:43 +000087{
Rose Zadik7f441272018-01-22 11:48:23 +000088 int nr; /*!< The number of rounds. */
89 uint32_t *rk; /*!< AES round keys. */
90 uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
91 hold 32 extra Bytes, which can be used for
92 one of the following purposes:
93 <ul><li>Alignment if VIA padlock is
94 used.</li>
95 <li>Simplifying key expansion in the 256-bit
96 case by generating an extra round key.
97 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000098}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
Jaeden Amero9366feb2018-05-29 18:55:17 +0100101#if defined(MBEDTLS_CIPHER_MODE_XTS)
102/**
103 * \brief The AES XTS context-type definition.
104 */
Dawid Drozd428cc522018-07-24 10:02:47 +0200105typedef struct mbedtls_aes_xts_context
Jaeden Amero9366feb2018-05-29 18:55:17 +0100106{
107 mbedtls_aes_context crypt; /*!< The AES context to use for AES block
108 encryption or decryption. */
109 mbedtls_aes_context tweak; /*!< The AES context used for tweak
110 computation. */
111} mbedtls_aes_xts_context;
112#endif /* MBEDTLS_CIPHER_MODE_XTS */
113
Ron Eldorb2aacec2017-05-18 16:53:08 +0300114#else /* MBEDTLS_AES_ALT */
115#include "aes_alt.h"
116#endif /* MBEDTLS_AES_ALT */
117
Paul Bakker5121ce52009-01-03 21:22:43 +0000118/**
Rose Zadik7f441272018-01-22 11:48:23 +0000119 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200120 *
Rose Zadik7f441272018-01-22 11:48:23 +0000121 * It must be the first API called before using
122 * the context.
123 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100124 * \param ctx The AES context to initialize. Must not be NULL.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200127
128/**
Rose Zadik7f441272018-01-22 11:48:23 +0000129 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200130 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100131 * \param ctx The AES context to clear. If NULL, no action is taken.
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 Amero9366feb2018-05-29 18:55:17 +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 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100142 * \param ctx The AES XTS context to initialize. Must not be NULL.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100143 */
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 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100149 * \param ctx The AES XTS context to clear. If NULL, no action is taken.
Jaeden Amero9366feb2018-05-29 18:55:17 +0100150 */
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 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100157 * \param ctx The AES context to which the key should be bound. Must not
158 * be NULL.
159 * \param key The encryption key. Must not be NULL.
Rose Zadik7f441272018-01-22 11:48:23 +0000160 * \param keybits The size of data passed in bits. Valid options are:
161 * <ul><li>128 bits</li>
162 * <li>192 bits</li>
163 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000164 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100165 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100166 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200169 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
171/**
Rose Zadik7f441272018-01-22 11:48:23 +0000172 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 *
Manuel Pégourié-Gonnard44c5d582018-12-10 16:56:14 +0100174 * \param ctx The AES context to which the key should be bound. Must not
175 * be NULL.
176 * \param key The decryption key. Must not be NULL.
Rose Zadik7f441272018-01-22 11:48:23 +0000177 * \param keybits The size of data passed. Valid options are:
178 * <ul><li>128 bits</li>
179 * <li>192 bits</li>
180 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000181 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100182 * \return \c 0 on success.
183 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200186 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Jaeden Amero9366feb2018-05-29 18:55:17 +0100188#if defined(MBEDTLS_CIPHER_MODE_XTS)
189/**
190 * \brief This function prepares an XTS context for encryption and
191 * sets the encryption key.
192 *
193 * \param ctx The AES XTS context to which the key should be bound.
194 * \param key The encryption key. This is comprised of the XTS key1
195 * concatenated with the XTS key2.
196 * \param keybits The size of \p key passed in bits. Valid options are:
197 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
198 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
199 *
200 * \return \c 0 on success.
201 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
202 */
203int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
204 const unsigned char *key,
205 unsigned int keybits );
206
207/**
208 * \brief This function prepares an XTS context for decryption and
209 * sets the decryption key.
210 *
211 * \param ctx The AES XTS context to which the key should be bound.
212 * \param key The decryption key. This is comprised of the XTS key1
213 * concatenated with the XTS key2.
214 * \param keybits The size of \p key passed in bits. Valid options are:
215 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
216 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
217 *
218 * \return \c 0 on success.
219 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
220 */
221int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
222 const unsigned char *key,
223 unsigned int keybits );
224#endif /* MBEDTLS_CIPHER_MODE_XTS */
225
Paul Bakker5121ce52009-01-03 21:22:43 +0000226/**
Rose Zadik7f441272018-01-22 11:48:23 +0000227 * \brief This function performs an AES single-block encryption or
228 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 *
Rose Zadik7f441272018-01-22 11:48:23 +0000230 * It performs the operation defined in the \p mode parameter
231 * (encrypt or decrypt), on the input data buffer defined in
232 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000233 *
Rose Zadik7f441272018-01-22 11:48:23 +0000234 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
235 * mbedtls_aes_setkey_dec() must be called before the first
236 * call to this API with the same context.
237 *
238 * \param ctx The AES context to use for encryption or decryption.
239 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
240 * #MBEDTLS_AES_DECRYPT.
241 * \param input The 16-Byte buffer holding the input data.
242 * \param output The 16-Byte buffer holding the output data.
243
244 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000247 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000248 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 unsigned char output[16] );
250
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000252/**
Rose Zadik7f441272018-01-22 11:48:23 +0000253 * \brief This function performs an AES-CBC encryption or decryption operation
254 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 *
Rose Zadik7f441272018-01-22 11:48:23 +0000256 * It performs the operation defined in the \p mode
257 * parameter (encrypt/decrypt), on the input data buffer defined in
258 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000259 *
Rose Zadik7f441272018-01-22 11:48:23 +0000260 * It can be called as many times as needed, until all the input
261 * data is processed. mbedtls_aes_init(), and either
262 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
263 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000264 *
Rose Zadik7f441272018-01-22 11:48:23 +0000265 * \note This function operates on aligned blocks, that is, the input size
266 * must be a multiple of the AES block size of 16 Bytes.
267 *
268 * \note Upon exit, the content of the IV is updated so that you can
269 * call the same function again on the next
270 * block(s) of data and get the same result as if it was
271 * encrypted in one call. This allows a "streaming" usage.
272 * If you need to retain the contents of the IV, you should
273 * either save it manually or use the cipher module instead.
274 *
275 *
276 * \param ctx The AES context to use for encryption or decryption.
277 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
278 * #MBEDTLS_AES_DECRYPT.
279 * \param length The length of the input data in Bytes. This must be a
280 * multiple of the block size (16 Bytes).
281 * \param iv Initialization vector (updated after use).
282 * \param input The buffer holding the input data.
283 * \param output The buffer holding the output data.
284 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100285 * \return \c 0 on success.
286 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000287 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000288 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000290 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000291 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000292 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000293 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000296
Aorimn5f778012016-06-09 23:22:58 +0200297#if defined(MBEDTLS_CIPHER_MODE_XTS)
298/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100299 * \brief This function performs an AES-XTS encryption or decryption
300 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200301 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100302 * AES-XTS encrypts or decrypts blocks based on their location as
303 * defined by a data unit number. The data unit number must be
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100304 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200305 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100306 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
307 * AES blocks. If the data unit is larger than this, this function
308 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
309 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100310 * \param ctx The AES XTS context to use for AES XTS operations.
311 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
312 * #MBEDTLS_AES_DECRYPT.
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100313 * \param length The length of a data unit in bytes. This can be any
314 * length between 16 bytes and 2^24 bytes inclusive
315 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100316 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100317 * bytes in little-endian format. For disk encryption, this
318 * is typically the index of the block device sector that
319 * contains the data.
320 * \param input The buffer holding the input data (which is an entire
321 * data unit). This function reads \p length bytes from \p
322 * input.
323 * \param output The buffer holding the output data (which is an entire
324 * data unit). This function writes \p length bytes to \p
325 * output.
Aorimn5f778012016-06-09 23:22:58 +0200326 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100327 * \return \c 0 on success.
328 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100329 * smaller than an AES block in size (16 bytes) or if \p
330 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200331 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100332int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
333 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100334 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100335 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100336 const unsigned char *input,
337 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200338#endif /* MBEDTLS_CIPHER_MODE_XTS */
339
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000341/**
Rose Zadik7f441272018-01-22 11:48:23 +0000342 * \brief This function performs an AES-CFB128 encryption or decryption
343 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000344 *
Rose Zadik7f441272018-01-22 11:48:23 +0000345 * It performs the operation defined in the \p mode
346 * parameter (encrypt or decrypt), on the input data buffer
347 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000348 *
Rose Zadik7f441272018-01-22 11:48:23 +0000349 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
350 * regardless of whether you are performing an encryption or decryption
351 * operation, that is, regardless of the \p mode parameter. This is
352 * because CFB mode uses the same key schedule for encryption and
353 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000354 *
Rose Zadik7f441272018-01-22 11:48:23 +0000355 * \note Upon exit, the content of the IV is updated so that you can
356 * call the same function again on the next
357 * block(s) of data and get the same result as if it was
358 * encrypted in one call. This allows a "streaming" usage.
359 * If you need to retain the contents of the
360 * IV, you must either save it manually or use the cipher
361 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000362 *
Rose Zadik7f441272018-01-22 11:48:23 +0000363 *
364 * \param ctx The AES context to use for encryption or decryption.
365 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
366 * #MBEDTLS_AES_DECRYPT.
367 * \param length The length of the input data.
368 * \param iv_off The offset in IV (updated after use).
369 * \param iv The initialization vector (updated after use).
370 * \param input The buffer holding the input data.
371 * \param output The buffer holding the output data.
372 *
373 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000374 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000376 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000377 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000378 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000379 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000380 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000381 unsigned char *output );
382
Paul Bakker9a736322012-11-14 12:39:52 +0000383/**
Rose Zadik7f441272018-01-22 11:48:23 +0000384 * \brief This function performs an AES-CFB8 encryption or decryption
385 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100386 *
Rose Zadik7f441272018-01-22 11:48:23 +0000387 * It performs the operation defined in the \p mode
388 * parameter (encrypt/decrypt), on the input data buffer defined
389 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100390 *
Rose Zadik7f441272018-01-22 11:48:23 +0000391 * Due to the nature of CFB, you must use the same key schedule for
392 * both encryption and decryption operations. Therefore, you must
393 * use the context initialized with mbedtls_aes_setkey_enc() for
394 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000395 *
Rose Zadik7f441272018-01-22 11:48:23 +0000396 * \note Upon exit, the content of the IV is updated so that you can
397 * call the same function again on the next
398 * block(s) of data and get the same result as if it was
399 * encrypted in one call. This allows a "streaming" usage.
400 * If you need to retain the contents of the
401 * IV, you should either save it manually or use the cipher
402 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100403 *
Rose Zadik7f441272018-01-22 11:48:23 +0000404 *
405 * \param ctx The AES context to use for encryption or decryption.
406 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
407 * #MBEDTLS_AES_DECRYPT
408 * \param length The length of the input data.
409 * \param iv The initialization vector (updated after use).
410 * \param input The buffer holding the input data.
411 * \param output The buffer holding the output data.
412 *
413 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100414 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200415int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100416 int mode,
417 size_t length,
418 unsigned char iv[16],
419 const unsigned char *input,
420 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200421#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100422
Simon Butcher76a5b222018-04-22 22:57:27 +0100423#if defined(MBEDTLS_CIPHER_MODE_OFB)
424/**
Simon Butcher5db13622018-06-04 22:11:25 +0100425 * \brief This function performs an AES-OFB (Output Feedback Mode)
426 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100427 *
Simon Butcher5db13622018-06-04 22:11:25 +0100428 * For OFB, you must set up the context with
429 * mbedtls_aes_setkey_enc(), regardless of whether you are
430 * performing an encryption or decryption operation. This is
431 * because OFB mode uses the same key schedule for encryption and
432 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100433 *
Simon Butcher5db13622018-06-04 22:11:25 +0100434 * The OFB operation is identical for encryption or decryption,
435 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100436 *
Simon Butcher5db13622018-06-04 22:11:25 +0100437 * \note Upon exit, the content of iv, the Initialisation Vector, is
438 * updated so that you can call the same function again on the next
439 * block(s) of data and get the same result as if it was encrypted
440 * in one call. This allows a "streaming" usage, by initialising
441 * iv_off to 0 before the first call, and preserving its value
442 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100443 *
Simon Butcher5db13622018-06-04 22:11:25 +0100444 * For non-streaming use, the iv should be initialised on each call
445 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100446 *
Simon Butcher5db13622018-06-04 22:11:25 +0100447 * If you need to retain the contents of the initialisation vector,
448 * you must either save it manually or use the cipher module
449 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100450 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100451 * \warning For the OFB mode, the initialisation vector must be unique
452 * every encryption operation. Reuse of an initialisation vector
453 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100454 *
455 * \param ctx The AES context to use for encryption or decryption.
456 * \param length The length of the input data.
457 * \param iv_off The offset in IV (updated after use).
458 * \param iv The initialization vector (updated after use).
459 * \param input The buffer holding the input data.
460 * \param output The buffer holding the output data.
461 *
462 * \return \c 0 on success.
463 */
464int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
465 size_t length,
466 size_t *iv_off,
467 unsigned char iv[16],
468 const unsigned char *input,
469 unsigned char *output );
470
471#endif /* MBEDTLS_CIPHER_MODE_OFB */
472
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200473#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100474/**
Rose Zadik7f441272018-01-22 11:48:23 +0000475 * \brief This function performs an AES-CTR encryption or decryption
476 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000477 *
Rose Zadik7f441272018-01-22 11:48:23 +0000478 * This function performs the operation defined in the \p mode
479 * parameter (encrypt/decrypt), on the input data buffer
480 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000481 *
Rose Zadik7f441272018-01-22 11:48:23 +0000482 * Due to the nature of CTR, you must use the same key schedule
483 * for both encryption and decryption operations. Therefore, you
484 * must use the context initialized with mbedtls_aes_setkey_enc()
485 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000486 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100487 * \warning You must never reuse a nonce value with the same key. Doing so
488 * would void the encryption for the two messages encrypted with
489 * the same nonce and key.
490 *
491 * There are two common strategies for managing nonces with CTR:
492 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200493 * 1. You can handle everything as a single message processed over
494 * successive calls to this function. In that case, you want to
495 * set \p nonce_counter and \p nc_off to 0 for the first call, and
496 * then preserve the values of \p nonce_counter, \p nc_off and \p
497 * stream_block across calls to this function as they will be
498 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100499 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200500 * With this strategy, you must not encrypt more than 2**128
501 * blocks of data with the same key.
502 *
503 * 2. You can encrypt separate messages by dividing the \p
504 * nonce_counter buffer in two areas: the first one used for a
505 * per-message nonce, handled by yourself, and the second one
506 * updated by this function internally.
507 *
508 * For example, you might reserve the first 12 bytes for the
509 * per-message nonce, and the last 4 bytes for internal use. In that
510 * case, before calling this function on a new message you need to
511 * set the first 12 bytes of \p nonce_counter to your chosen nonce
512 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
513 * stream_block to be ignored). That way, you can encrypt at most
514 * 2**96 messages of up to 2**32 blocks each with the same key.
515 *
516 * The per-message nonce (or information sufficient to reconstruct
517 * it) needs to be communicated with the ciphertext and must be unique.
518 * The recommended way to ensure uniqueness is to use a message
519 * counter. An alternative is to generate random nonces, but this
520 * limits the number of messages that can be securely encrypted:
521 * for example, with 96-bit random nonces, you should not encrypt
522 * more than 2**32 messages with the same key.
523 *
524 * Note that for both stategies, sizes are measured in blocks and
525 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000526 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200527 * \warning Upon return, \p stream_block contains sensitive data. Its
528 * content must not be written to insecure storage and should be
529 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000530 *
Rose Zadik7f441272018-01-22 11:48:23 +0000531 * \param ctx The AES context to use for encryption or decryption.
532 * \param length The length of the input data.
533 * \param nc_off The offset in the current \p stream_block, for
534 * resuming within the current cipher stream. The
535 * offset pointer should be 0 at the start of a stream.
536 * \param nonce_counter The 128-bit nonce and counter.
537 * \param stream_block The saved stream block for resuming. This is
538 * overwritten by the function.
539 * \param input The buffer holding the input data.
540 * \param output The buffer holding the output data.
541 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100542 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000543 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200544int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000545 size_t length,
546 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000547 unsigned char nonce_counter[16],
548 unsigned char stream_block[16],
549 const unsigned char *input,
550 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200552
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200553/**
Rose Zadik7f441272018-01-22 11:48:23 +0000554 * \brief Internal AES block encryption function. This is only
555 * exposed to allow overriding it using
556 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200557 *
Rose Zadik7f441272018-01-22 11:48:23 +0000558 * \param ctx The AES context to use for encryption.
559 * \param input The plaintext block.
560 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000561 *
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200563 */
Andres AGf5bf7182017-03-03 14:09:56 +0000564int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
565 const unsigned char input[16],
566 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200567
568/**
Rose Zadik7f441272018-01-22 11:48:23 +0000569 * \brief Internal AES block decryption function. This is only
570 * exposed to allow overriding it using see
571 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200572 *
Rose Zadik7f441272018-01-22 11:48:23 +0000573 * \param ctx The AES context to use for decryption.
574 * \param input The ciphertext block.
575 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000576 *
Rose Zadik7f441272018-01-22 11:48:23 +0000577 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200578 */
Andres AGf5bf7182017-03-03 14:09:56 +0000579int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
580 const unsigned char input[16],
581 unsigned char output[16] );
582
583#if !defined(MBEDTLS_DEPRECATED_REMOVED)
584#if defined(MBEDTLS_DEPRECATED_WARNING)
585#define MBEDTLS_DEPRECATED __attribute__((deprecated))
586#else
587#define MBEDTLS_DEPRECATED
588#endif
589/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100590 * \brief Deprecated internal AES block encryption function
591 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000592 *
Rose Zadik7f441272018-01-22 11:48:23 +0000593 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000594 *
Rose Zadik7f441272018-01-22 11:48:23 +0000595 * \param ctx The AES context to use for encryption.
596 * \param input Plaintext block.
597 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000598 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100599MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
600 const unsigned char input[16],
601 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000602
603/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100604 * \brief Deprecated internal AES block decryption 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_decrypt_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 decryption.
610 * \param input Ciphertext block.
611 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000612 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100613MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
614 const unsigned char input[16],
615 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000616
617#undef MBEDTLS_DEPRECATED
618#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200619
Paul Bakker5121ce52009-01-03 21:22:43 +0000620/**
Rose Zadik7f441272018-01-22 11:48:23 +0000621 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000622 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100623 * \return \c 0 on success.
624 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000625 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000627
628#ifdef __cplusplus
629}
630#endif
631
632#endif /* aes.h */