blob: e166e9c6eae8ca016ae5f69ce8b2676e249438cb [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
Mohammad Azim Khane5b5bd72017-11-24 10:52:51 +000056/* Error codes in range 0x0021-0x0025 */
57#define MBEDTLS_ERR_AES_BAD_INPUT_DATA -0x0021 /**< Invalid input data. */
Rose Zadik7f441272018-01-22 11:48:23 +000058#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available. For example, an unsupported AES key size. */
Gilles Peskine7ecab3d2018-01-26 17:56:38 +010059#define MBEDTLS_ERR_AES_HW_ACCEL_FAILED -0x0025 /**< AES hardware accelerator failed. */
Paul Bakker5121ce52009-01-03 21:22:43 +000060
Andres AGf5bf7182017-03-03 14:09:56 +000061#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
62 !defined(inline) && !defined(__cplusplus)
63#define inline __inline
64#endif
65
Paul Bakker407a0da2013-06-27 14:29:21 +020066#ifdef __cplusplus
67extern "C" {
68#endif
69
Ron Eldorb2aacec2017-05-18 16:53:08 +030070#if !defined(MBEDTLS_AES_ALT)
71// Regular implementation
72//
73
Paul Bakker5121ce52009-01-03 21:22:43 +000074/**
Rose Zadik7f441272018-01-22 11:48:23 +000075 * \brief The AES context-type definition.
Paul Bakker5121ce52009-01-03 21:22:43 +000076 */
77typedef struct
78{
Rose Zadik7f441272018-01-22 11:48:23 +000079 int nr; /*!< The number of rounds. */
80 uint32_t *rk; /*!< AES round keys. */
81 uint32_t buf[68]; /*!< Unaligned data buffer. This buffer can
82 hold 32 extra Bytes, which can be used for
83 one of the following purposes:
84 <ul><li>Alignment if VIA padlock is
85 used.</li>
86 <li>Simplifying key expansion in the 256-bit
87 case by generating an extra round key.
88 </li></ul> */
Paul Bakker5121ce52009-01-03 21:22:43 +000089}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Jaeden Amero9366feb2018-05-29 18:55:17 +010092#if defined(MBEDTLS_CIPHER_MODE_XTS)
93/**
94 * \brief The AES XTS context-type definition.
95 */
96typedef struct
97{
98 mbedtls_aes_context crypt; /*!< The AES context to use for AES block
99 encryption or decryption. */
100 mbedtls_aes_context tweak; /*!< The AES context used for tweak
101 computation. */
102} mbedtls_aes_xts_context;
103#endif /* MBEDTLS_CIPHER_MODE_XTS */
104
Ron Eldorb2aacec2017-05-18 16:53:08 +0300105#else /* MBEDTLS_AES_ALT */
106#include "aes_alt.h"
107#endif /* MBEDTLS_AES_ALT */
108
Paul Bakker5121ce52009-01-03 21:22:43 +0000109/**
Rose Zadik7f441272018-01-22 11:48:23 +0000110 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200111 *
Rose Zadik7f441272018-01-22 11:48:23 +0000112 * It must be the first API called before using
113 * the context.
114 *
115 * \param ctx The AES context to initialize.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200116 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200118
119/**
Rose Zadik7f441272018-01-22 11:48:23 +0000120 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200121 *
Rose Zadik7f441272018-01-22 11:48:23 +0000122 * \param ctx The AES context to clear.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200125
Jaeden Amero9366feb2018-05-29 18:55:17 +0100126#if defined(MBEDTLS_CIPHER_MODE_XTS)
127/**
128 * \brief This function initializes the specified AES XTS context.
129 *
130 * It must be the first API called before using
131 * the context.
132 *
133 * \param ctx The AES XTS context to initialize.
134 */
135void mbedtls_aes_xts_init( mbedtls_aes_xts_context *ctx );
136
137/**
138 * \brief This function releases and clears the specified AES XTS context.
139 *
140 * \param ctx The AES XTS context to clear.
141 */
142void mbedtls_aes_xts_free( mbedtls_aes_xts_context *ctx );
143#endif /* MBEDTLS_CIPHER_MODE_XTS */
144
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200145/**
Rose Zadik7f441272018-01-22 11:48:23 +0000146 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 *
Rose Zadik7f441272018-01-22 11:48:23 +0000148 * \param ctx The AES context to which the key should be bound.
149 * \param key The encryption key.
150 * \param keybits The size of data passed in bits. Valid options are:
151 * <ul><li>128 bits</li>
152 * <li>192 bits</li>
153 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000154 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100155 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100156 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200159 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160
161/**
Rose Zadik7f441272018-01-22 11:48:23 +0000162 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
Rose Zadik7f441272018-01-22 11:48:23 +0000164 * \param ctx The AES context to which the key should be bound.
165 * \param key The decryption key.
166 * \param keybits The size of data passed. Valid options are:
167 * <ul><li>128 bits</li>
168 * <li>192 bits</li>
169 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000170 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100171 * \return \c 0 on success.
172 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200175 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000176
Jaeden Amero9366feb2018-05-29 18:55:17 +0100177#if defined(MBEDTLS_CIPHER_MODE_XTS)
178/**
179 * \brief This function prepares an XTS context for encryption and
180 * sets the encryption key.
181 *
182 * \param ctx The AES XTS context to which the key should be bound.
183 * \param key The encryption key. This is comprised of the XTS key1
184 * concatenated with the XTS key2.
185 * \param keybits The size of \p key passed in bits. Valid options are:
186 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
187 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
188 *
189 * \return \c 0 on success.
190 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
191 */
192int mbedtls_aes_xts_setkey_enc( mbedtls_aes_xts_context *ctx,
193 const unsigned char *key,
194 unsigned int keybits );
195
196/**
197 * \brief This function prepares an XTS context for decryption and
198 * sets the decryption key.
199 *
200 * \param ctx The AES XTS context to which the key should be bound.
201 * \param key The decryption key. This is comprised of the XTS key1
202 * concatenated with the XTS key2.
203 * \param keybits The size of \p key passed in bits. Valid options are:
204 * <ul><li>256 bits (each of key1 and key2 is a 128-bit key)</li>
205 * <li>512 bits (each of key1 and key2 is a 256-bit key)</li></ul>
206 *
207 * \return \c 0 on success.
208 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
209 */
210int mbedtls_aes_xts_setkey_dec( mbedtls_aes_xts_context *ctx,
211 const unsigned char *key,
212 unsigned int keybits );
213#endif /* MBEDTLS_CIPHER_MODE_XTS */
214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215/**
Rose Zadik7f441272018-01-22 11:48:23 +0000216 * \brief This function performs an AES single-block encryption or
217 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 *
Rose Zadik7f441272018-01-22 11:48:23 +0000219 * It performs the operation defined in the \p mode parameter
220 * (encrypt or decrypt), on the input data buffer defined in
221 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000222 *
Rose Zadik7f441272018-01-22 11:48:23 +0000223 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
224 * mbedtls_aes_setkey_dec() must be called before the first
225 * call to this API with the same context.
226 *
227 * \param ctx The AES context to use for encryption or decryption.
228 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
229 * #MBEDTLS_AES_DECRYPT.
230 * \param input The 16-Byte buffer holding the input data.
231 * \param output The 16-Byte buffer holding the output data.
232
233 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000237 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 unsigned char output[16] );
239
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000241/**
Rose Zadik7f441272018-01-22 11:48:23 +0000242 * \brief This function performs an AES-CBC encryption or decryption operation
243 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000244 *
Rose Zadik7f441272018-01-22 11:48:23 +0000245 * It performs the operation defined in the \p mode
246 * parameter (encrypt/decrypt), on the input data buffer defined in
247 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000248 *
Rose Zadik7f441272018-01-22 11:48:23 +0000249 * It can be called as many times as needed, until all the input
250 * data is processed. mbedtls_aes_init(), and either
251 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
252 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000253 *
Rose Zadik7f441272018-01-22 11:48:23 +0000254 * \note This function operates on aligned blocks, that is, the input size
255 * must be a multiple of the AES block size of 16 Bytes.
256 *
257 * \note Upon exit, the content of the IV is updated so that you can
258 * call the same function again on the next
259 * block(s) of data and get the same result as if it was
260 * encrypted in one call. This allows a "streaming" usage.
261 * If you need to retain the contents of the IV, you should
262 * either save it manually or use the cipher module instead.
263 *
264 *
265 * \param ctx The AES context to use for encryption or decryption.
266 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
267 * #MBEDTLS_AES_DECRYPT.
268 * \param length The length of the input data in Bytes. This must be a
269 * multiple of the block size (16 Bytes).
270 * \param iv Initialization vector (updated after use).
271 * \param input The buffer holding the input data.
272 * \param output The buffer holding the output data.
273 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100274 * \return \c 0 on success.
275 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000276 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000280 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000281 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000282 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200284#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000285
Aorimn5f778012016-06-09 23:22:58 +0200286#if defined(MBEDTLS_CIPHER_MODE_XTS)
287/**
Jaeden Amero9366feb2018-05-29 18:55:17 +0100288 * \brief This function performs an AES-XTS encryption or decryption
289 * operation for an entire XTS data unit.
Aorimn5f778012016-06-09 23:22:58 +0200290 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100291 * AES-XTS encrypts or decrypts blocks based on their location as
292 * defined by a data unit number. The data unit number must be
293 * provided by \p iv.
Aorimn5f778012016-06-09 23:22:58 +0200294 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100295 * \param ctx The AES XTS context to use for AES XTS operations.
296 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
297 * #MBEDTLS_AES_DECRYPT.
298 * \param bits_length The length of a data unit in bits.
299 * \param iv The address of the data unit encoded as an array of 16
300 * bytes in little-endian format. For disk encryption, this
301 * is typically the index of the block device sector that
302 * contains the data.
303 * \param input The buffer holding the input data (which is an entire
304 * data unit). This function reads \p length bytes from \p
305 * input.
306 * \param output The buffer holding the output data (which is an entire
307 * data unit). This function writes \p length bytes to \p
308 * output.
Aorimn5f778012016-06-09 23:22:58 +0200309 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100310 * \return \c 0 on success.
311 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
312 * smaller than an AES block in size (16 bytes).
Aorimn5f778012016-06-09 23:22:58 +0200313 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100314int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
315 int mode,
316 size_t bits_length,
317 const unsigned char iv[16],
318 const unsigned char *input,
319 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200320#endif /* MBEDTLS_CIPHER_MODE_XTS */
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000323/**
Rose Zadik7f441272018-01-22 11:48:23 +0000324 * \brief This function performs an AES-CFB128 encryption or decryption
325 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000326 *
Rose Zadik7f441272018-01-22 11:48:23 +0000327 * It performs the operation defined in the \p mode
328 * parameter (encrypt or decrypt), on the input data buffer
329 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000330 *
Rose Zadik7f441272018-01-22 11:48:23 +0000331 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
332 * regardless of whether you are performing an encryption or decryption
333 * operation, that is, regardless of the \p mode parameter. This is
334 * because CFB mode uses the same key schedule for encryption and
335 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000336 *
Rose Zadik7f441272018-01-22 11:48:23 +0000337 * \note Upon exit, the content of the IV is updated so that you can
338 * call the same function again on the next
339 * block(s) of data and get the same result as if it was
340 * encrypted in one call. This allows a "streaming" usage.
341 * If you need to retain the contents of the
342 * IV, you must either save it manually or use the cipher
343 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000344 *
Rose Zadik7f441272018-01-22 11:48:23 +0000345 *
346 * \param ctx The AES context to use for encryption or decryption.
347 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
348 * #MBEDTLS_AES_DECRYPT.
349 * \param length The length of the input data.
350 * \param iv_off The offset in IV (updated after use).
351 * \param iv The initialization vector (updated after use).
352 * \param input The buffer holding the input data.
353 * \param output The buffer holding the output data.
354 *
355 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000356 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200357int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000358 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000359 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000360 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000361 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000362 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 unsigned char *output );
364
Paul Bakker9a736322012-11-14 12:39:52 +0000365/**
Rose Zadik7f441272018-01-22 11:48:23 +0000366 * \brief This function performs an AES-CFB8 encryption or decryption
367 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100368 *
Rose Zadik7f441272018-01-22 11:48:23 +0000369 * It performs the operation defined in the \p mode
370 * parameter (encrypt/decrypt), on the input data buffer defined
371 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100372 *
Rose Zadik7f441272018-01-22 11:48:23 +0000373 * Due to the nature of CFB, you must use the same key schedule for
374 * both encryption and decryption operations. Therefore, you must
375 * use the context initialized with mbedtls_aes_setkey_enc() for
376 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000377 *
Rose Zadik7f441272018-01-22 11:48:23 +0000378 * \note Upon exit, the content of the IV is updated so that you can
379 * call the same function again on the next
380 * block(s) of data and get the same result as if it was
381 * encrypted in one call. This allows a "streaming" usage.
382 * If you need to retain the contents of the
383 * IV, you should either save it manually or use the cipher
384 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100385 *
Rose Zadik7f441272018-01-22 11:48:23 +0000386 *
387 * \param ctx The AES context to use for encryption or decryption.
388 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
389 * #MBEDTLS_AES_DECRYPT
390 * \param length The length of the input data.
391 * \param iv The initialization vector (updated after use).
392 * \param input The buffer holding the input data.
393 * \param output The buffer holding the output data.
394 *
395 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100396 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100398 int mode,
399 size_t length,
400 unsigned char iv[16],
401 const unsigned char *input,
402 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200403#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100404
Simon Butcher76a5b222018-04-22 22:57:27 +0100405#if defined(MBEDTLS_CIPHER_MODE_OFB)
406/**
Simon Butcher5db13622018-06-04 22:11:25 +0100407 * \brief This function performs an AES-OFB (Output Feedback Mode)
408 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100409 *
Simon Butcher5db13622018-06-04 22:11:25 +0100410 * For OFB, you must set up the context with
411 * mbedtls_aes_setkey_enc(), regardless of whether you are
412 * performing an encryption or decryption operation. This is
413 * because OFB mode uses the same key schedule for encryption and
414 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100415 *
Simon Butcher5db13622018-06-04 22:11:25 +0100416 * The OFB operation is identical for encryption or decryption,
417 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100418 *
Simon Butcher5db13622018-06-04 22:11:25 +0100419 * \note Upon exit, the content of iv, the Initialisation Vector, is
420 * updated so that you can call the same function again on the next
421 * block(s) of data and get the same result as if it was encrypted
422 * in one call. This allows a "streaming" usage, by initialising
423 * iv_off to 0 before the first call, and preserving its value
424 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100425 *
Simon Butcher5db13622018-06-04 22:11:25 +0100426 * For non-streaming use, the iv should be initialised on each call
427 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100428 *
Simon Butcher5db13622018-06-04 22:11:25 +0100429 * If you need to retain the contents of the initialisation vector,
430 * you must either save it manually or use the cipher module
431 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100432 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100433 * \warning For the OFB mode, the initialisation vector must be unique
434 * every encryption operation. Reuse of an initialisation vector
435 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100436 *
437 * \param ctx The AES context to use for encryption or decryption.
438 * \param length The length of the input data.
439 * \param iv_off The offset in IV (updated after use).
440 * \param iv The initialization vector (updated after use).
441 * \param input The buffer holding the input data.
442 * \param output The buffer holding the output data.
443 *
444 * \return \c 0 on success.
445 */
446int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
447 size_t length,
448 size_t *iv_off,
449 unsigned char iv[16],
450 const unsigned char *input,
451 unsigned char *output );
452
453#endif /* MBEDTLS_CIPHER_MODE_OFB */
454
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200455#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100456/**
Rose Zadik7f441272018-01-22 11:48:23 +0000457 * \brief This function performs an AES-CTR encryption or decryption
458 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000459 *
Rose Zadik7f441272018-01-22 11:48:23 +0000460 * This function performs the operation defined in the \p mode
461 * parameter (encrypt/decrypt), on the input data buffer
462 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000463 *
Rose Zadik7f441272018-01-22 11:48:23 +0000464 * Due to the nature of CTR, you must use the same key schedule
465 * for both encryption and decryption operations. Therefore, you
466 * must use the context initialized with mbedtls_aes_setkey_enc()
467 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000468 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100469 * \warning You must never reuse a nonce value with the same key. Doing so
470 * would void the encryption for the two messages encrypted with
471 * the same nonce and key.
472 *
473 * There are two common strategies for managing nonces with CTR:
474 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200475 * 1. You can handle everything as a single message processed over
476 * successive calls to this function. In that case, you want to
477 * set \p nonce_counter and \p nc_off to 0 for the first call, and
478 * then preserve the values of \p nonce_counter, \p nc_off and \p
479 * stream_block across calls to this function as they will be
480 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100481 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200482 * With this strategy, you must not encrypt more than 2**128
483 * blocks of data with the same key.
484 *
485 * 2. You can encrypt separate messages by dividing the \p
486 * nonce_counter buffer in two areas: the first one used for a
487 * per-message nonce, handled by yourself, and the second one
488 * updated by this function internally.
489 *
490 * For example, you might reserve the first 12 bytes for the
491 * per-message nonce, and the last 4 bytes for internal use. In that
492 * case, before calling this function on a new message you need to
493 * set the first 12 bytes of \p nonce_counter to your chosen nonce
494 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
495 * stream_block to be ignored). That way, you can encrypt at most
496 * 2**96 messages of up to 2**32 blocks each with the same key.
497 *
498 * The per-message nonce (or information sufficient to reconstruct
499 * it) needs to be communicated with the ciphertext and must be unique.
500 * The recommended way to ensure uniqueness is to use a message
501 * counter. An alternative is to generate random nonces, but this
502 * limits the number of messages that can be securely encrypted:
503 * for example, with 96-bit random nonces, you should not encrypt
504 * more than 2**32 messages with the same key.
505 *
506 * Note that for both stategies, sizes are measured in blocks and
507 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000508 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200509 * \warning Upon return, \p stream_block contains sensitive data. Its
510 * content must not be written to insecure storage and should be
511 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000512 *
Rose Zadik7f441272018-01-22 11:48:23 +0000513 * \param ctx The AES context to use for encryption or decryption.
514 * \param length The length of the input data.
515 * \param nc_off The offset in the current \p stream_block, for
516 * resuming within the current cipher stream. The
517 * offset pointer should be 0 at the start of a stream.
518 * \param nonce_counter The 128-bit nonce and counter.
519 * \param stream_block The saved stream block for resuming. This is
520 * overwritten by the function.
521 * \param input The buffer holding the input data.
522 * \param output The buffer holding the output data.
523 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100524 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000525 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200526int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000527 size_t length,
528 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000529 unsigned char nonce_counter[16],
530 unsigned char stream_block[16],
531 const unsigned char *input,
532 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200534
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200535/**
Rose Zadik7f441272018-01-22 11:48:23 +0000536 * \brief Internal AES block encryption function. This is only
537 * exposed to allow overriding it using
538 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200539 *
Rose Zadik7f441272018-01-22 11:48:23 +0000540 * \param ctx The AES context to use for encryption.
541 * \param input The plaintext block.
542 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000543 *
Rose Zadik7f441272018-01-22 11:48:23 +0000544 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200545 */
Andres AGf5bf7182017-03-03 14:09:56 +0000546int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
547 const unsigned char input[16],
548 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200549
550/**
Rose Zadik7f441272018-01-22 11:48:23 +0000551 * \brief Internal AES block decryption function. This is only
552 * exposed to allow overriding it using see
553 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200554 *
Rose Zadik7f441272018-01-22 11:48:23 +0000555 * \param ctx The AES context to use for decryption.
556 * \param input The ciphertext block.
557 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000558 *
Rose Zadik7f441272018-01-22 11:48:23 +0000559 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200560 */
Andres AGf5bf7182017-03-03 14:09:56 +0000561int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
562 const unsigned char input[16],
563 unsigned char output[16] );
564
565#if !defined(MBEDTLS_DEPRECATED_REMOVED)
566#if defined(MBEDTLS_DEPRECATED_WARNING)
567#define MBEDTLS_DEPRECATED __attribute__((deprecated))
568#else
569#define MBEDTLS_DEPRECATED
570#endif
571/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100572 * \brief Deprecated internal AES block encryption function
573 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000574 *
Rose Zadik7f441272018-01-22 11:48:23 +0000575 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000576 *
Rose Zadik7f441272018-01-22 11:48:23 +0000577 * \param ctx The AES context to use for encryption.
578 * \param input Plaintext block.
579 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000580 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100581MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
582 const unsigned char input[16],
583 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000584
585/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100586 * \brief Deprecated internal AES block decryption function
587 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000588 *
Rose Zadik7f441272018-01-22 11:48:23 +0000589 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000590 *
Rose Zadik7f441272018-01-22 11:48:23 +0000591 * \param ctx The AES context to use for decryption.
592 * \param input Ciphertext block.
593 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000594 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100595MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
596 const unsigned char input[16],
597 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000598
599#undef MBEDTLS_DEPRECATED
600#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200601
Paul Bakker5121ce52009-01-03 21:22:43 +0000602/**
Rose Zadik7f441272018-01-22 11:48:23 +0000603 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000604 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100605 * \return \c 0 on success.
606 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000607 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200608int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000609
610#ifdef __cplusplus
611}
612#endif
613
614#endif /* aes.h */