blob: 74c528fb62f85b81099d89dcd8fa3ad4b82be863 [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
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100293 * provided by \p data_unit.
Aorimn5f778012016-06-09 23:22:58 +0200294 *
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100295 * NIST SP 800-38E limits the maximum size of a data unit to 2^20
296 * AES blocks. If the data unit is larger than this, this function
297 * returns #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH.
298 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100299 * \param ctx The AES XTS context to use for AES XTS operations.
300 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
301 * #MBEDTLS_AES_DECRYPT.
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100302 * \param length The length of a data unit in bytes. This can be any
303 * length between 16 bytes and 2^24 bytes inclusive
304 * (between 1 and 2^20 block cipher blocks).
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100305 * \param data_unit The address of the data unit encoded as an array of 16
Jaeden Amero9366feb2018-05-29 18:55:17 +0100306 * bytes in little-endian format. For disk encryption, this
307 * is typically the index of the block device sector that
308 * contains the data.
309 * \param input The buffer holding the input data (which is an entire
310 * data unit). This function reads \p length bytes from \p
311 * input.
312 * \param output The buffer holding the output data (which is an entire
313 * data unit). This function writes \p length bytes to \p
314 * output.
Aorimn5f778012016-06-09 23:22:58 +0200315 *
Jaeden Amero9366feb2018-05-29 18:55:17 +0100316 * \return \c 0 on success.
317 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH if \p length is
Jaeden Amero0a8b0202018-05-30 15:36:06 +0100318 * smaller than an AES block in size (16 bytes) or if \p
319 * length is larger than 2^20 blocks (16 MiB).
Aorimn5f778012016-06-09 23:22:58 +0200320 */
Jaeden Amero9366feb2018-05-29 18:55:17 +0100321int mbedtls_aes_crypt_xts( mbedtls_aes_xts_context *ctx,
322 int mode,
Jaeden Amero5162b932018-05-29 12:55:24 +0100323 size_t length,
Jaeden Amerocd9fc5e2018-05-30 15:23:24 +0100324 const unsigned char data_unit[16],
Jaeden Amero9366feb2018-05-29 18:55:17 +0100325 const unsigned char *input,
326 unsigned char *output );
Aorimn5f778012016-06-09 23:22:58 +0200327#endif /* MBEDTLS_CIPHER_MODE_XTS */
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000330/**
Rose Zadik7f441272018-01-22 11:48:23 +0000331 * \brief This function performs an AES-CFB128 encryption or decryption
332 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000333 *
Rose Zadik7f441272018-01-22 11:48:23 +0000334 * It performs the operation defined in the \p mode
335 * parameter (encrypt or decrypt), on the input data buffer
336 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000337 *
Rose Zadik7f441272018-01-22 11:48:23 +0000338 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
339 * regardless of whether you are performing an encryption or decryption
340 * operation, that is, regardless of the \p mode parameter. This is
341 * because CFB mode uses the same key schedule for encryption and
342 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000343 *
Rose Zadik7f441272018-01-22 11:48:23 +0000344 * \note Upon exit, the content of the IV is updated so that you can
345 * call the same function again on the next
346 * block(s) of data and get the same result as if it was
347 * encrypted in one call. This allows a "streaming" usage.
348 * If you need to retain the contents of the
349 * IV, you must either save it manually or use the cipher
350 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000351 *
Rose Zadik7f441272018-01-22 11:48:23 +0000352 *
353 * \param ctx The AES context to use for encryption or decryption.
354 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
355 * #MBEDTLS_AES_DECRYPT.
356 * \param length The length of the input data.
357 * \param iv_off The offset in IV (updated after use).
358 * \param iv The initialization vector (updated after use).
359 * \param input The buffer holding the input data.
360 * \param output The buffer holding the output data.
361 *
362 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000363 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200364int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000365 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000366 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000367 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000368 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000369 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000370 unsigned char *output );
371
Paul Bakker9a736322012-11-14 12:39:52 +0000372/**
Rose Zadik7f441272018-01-22 11:48:23 +0000373 * \brief This function performs an AES-CFB8 encryption or decryption
374 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100375 *
Rose Zadik7f441272018-01-22 11:48:23 +0000376 * It performs the operation defined in the \p mode
377 * parameter (encrypt/decrypt), on the input data buffer defined
378 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100379 *
Rose Zadik7f441272018-01-22 11:48:23 +0000380 * Due to the nature of CFB, you must use the same key schedule for
381 * both encryption and decryption operations. Therefore, you must
382 * use the context initialized with mbedtls_aes_setkey_enc() for
383 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000384 *
Rose Zadik7f441272018-01-22 11:48:23 +0000385 * \note Upon exit, the content of the IV is updated so that you can
386 * call the same function again on the next
387 * block(s) of data and get the same result as if it was
388 * encrypted in one call. This allows a "streaming" usage.
389 * If you need to retain the contents of the
390 * IV, you should either save it manually or use the cipher
391 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100392 *
Rose Zadik7f441272018-01-22 11:48:23 +0000393 *
394 * \param ctx The AES context to use for encryption or decryption.
395 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
396 * #MBEDTLS_AES_DECRYPT
397 * \param length The length of the input data.
398 * \param iv The initialization vector (updated after use).
399 * \param input The buffer holding the input data.
400 * \param output The buffer holding the output data.
401 *
402 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100403 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200404int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100405 int mode,
406 size_t length,
407 unsigned char iv[16],
408 const unsigned char *input,
409 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200410#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100411
Simon Butcher76a5b222018-04-22 22:57:27 +0100412#if defined(MBEDTLS_CIPHER_MODE_OFB)
413/**
Simon Butcher5db13622018-06-04 22:11:25 +0100414 * \brief This function performs an AES-OFB (Output Feedback Mode)
415 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100416 *
Simon Butcher5db13622018-06-04 22:11:25 +0100417 * For OFB, you must set up the context with
418 * mbedtls_aes_setkey_enc(), regardless of whether you are
419 * performing an encryption or decryption operation. This is
420 * because OFB mode uses the same key schedule for encryption and
421 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100422 *
Simon Butcher5db13622018-06-04 22:11:25 +0100423 * The OFB operation is identical for encryption or decryption,
424 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100425 *
Simon Butcher5db13622018-06-04 22:11:25 +0100426 * \note Upon exit, the content of iv, the Initialisation Vector, is
427 * updated so that you can call the same function again on the next
428 * block(s) of data and get the same result as if it was encrypted
429 * in one call. This allows a "streaming" usage, by initialising
430 * iv_off to 0 before the first call, and preserving its value
431 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100432 *
Simon Butcher5db13622018-06-04 22:11:25 +0100433 * For non-streaming use, the iv should be initialised on each call
434 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100435 *
Simon Butcher5db13622018-06-04 22:11:25 +0100436 * If you need to retain the contents of the initialisation vector,
437 * you must either save it manually or use the cipher module
438 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100439 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100440 * \warning For the OFB mode, the initialisation vector must be unique
441 * every encryption operation. Reuse of an initialisation vector
442 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100443 *
444 * \param ctx The AES context to use for encryption or decryption.
445 * \param length The length of the input data.
446 * \param iv_off The offset in IV (updated after use).
447 * \param iv The initialization vector (updated after use).
448 * \param input The buffer holding the input data.
449 * \param output The buffer holding the output data.
450 *
451 * \return \c 0 on success.
452 */
453int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
454 size_t length,
455 size_t *iv_off,
456 unsigned char iv[16],
457 const unsigned char *input,
458 unsigned char *output );
459
460#endif /* MBEDTLS_CIPHER_MODE_OFB */
461
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200462#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100463/**
Rose Zadik7f441272018-01-22 11:48:23 +0000464 * \brief This function performs an AES-CTR encryption or decryption
465 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000466 *
Rose Zadik7f441272018-01-22 11:48:23 +0000467 * This function performs the operation defined in the \p mode
468 * parameter (encrypt/decrypt), on the input data buffer
469 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000470 *
Rose Zadik7f441272018-01-22 11:48:23 +0000471 * Due to the nature of CTR, you must use the same key schedule
472 * for both encryption and decryption operations. Therefore, you
473 * must use the context initialized with mbedtls_aes_setkey_enc()
474 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000475 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100476 * \warning You must never reuse a nonce value with the same key. Doing so
477 * would void the encryption for the two messages encrypted with
478 * the same nonce and key.
479 *
480 * There are two common strategies for managing nonces with CTR:
481 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200482 * 1. You can handle everything as a single message processed over
483 * successive calls to this function. In that case, you want to
484 * set \p nonce_counter and \p nc_off to 0 for the first call, and
485 * then preserve the values of \p nonce_counter, \p nc_off and \p
486 * stream_block across calls to this function as they will be
487 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100488 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200489 * With this strategy, you must not encrypt more than 2**128
490 * blocks of data with the same key.
491 *
492 * 2. You can encrypt separate messages by dividing the \p
493 * nonce_counter buffer in two areas: the first one used for a
494 * per-message nonce, handled by yourself, and the second one
495 * updated by this function internally.
496 *
497 * For example, you might reserve the first 12 bytes for the
498 * per-message nonce, and the last 4 bytes for internal use. In that
499 * case, before calling this function on a new message you need to
500 * set the first 12 bytes of \p nonce_counter to your chosen nonce
501 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
502 * stream_block to be ignored). That way, you can encrypt at most
503 * 2**96 messages of up to 2**32 blocks each with the same key.
504 *
505 * The per-message nonce (or information sufficient to reconstruct
506 * it) needs to be communicated with the ciphertext and must be unique.
507 * The recommended way to ensure uniqueness is to use a message
508 * counter. An alternative is to generate random nonces, but this
509 * limits the number of messages that can be securely encrypted:
510 * for example, with 96-bit random nonces, you should not encrypt
511 * more than 2**32 messages with the same key.
512 *
513 * Note that for both stategies, sizes are measured in blocks and
514 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000515 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200516 * \warning Upon return, \p stream_block contains sensitive data. Its
517 * content must not be written to insecure storage and should be
518 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000519 *
Rose Zadik7f441272018-01-22 11:48:23 +0000520 * \param ctx The AES context to use for encryption or decryption.
521 * \param length The length of the input data.
522 * \param nc_off The offset in the current \p stream_block, for
523 * resuming within the current cipher stream. The
524 * offset pointer should be 0 at the start of a stream.
525 * \param nonce_counter The 128-bit nonce and counter.
526 * \param stream_block The saved stream block for resuming. This is
527 * overwritten by the function.
528 * \param input The buffer holding the input data.
529 * \param output The buffer holding the output data.
530 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100531 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000532 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000534 size_t length,
535 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000536 unsigned char nonce_counter[16],
537 unsigned char stream_block[16],
538 const unsigned char *input,
539 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200541
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200542/**
Rose Zadik7f441272018-01-22 11:48:23 +0000543 * \brief Internal AES block encryption function. This is only
544 * exposed to allow overriding it using
545 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200546 *
Rose Zadik7f441272018-01-22 11:48:23 +0000547 * \param ctx The AES context to use for encryption.
548 * \param input The plaintext block.
549 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000550 *
Rose Zadik7f441272018-01-22 11:48:23 +0000551 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200552 */
Andres AGf5bf7182017-03-03 14:09:56 +0000553int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
554 const unsigned char input[16],
555 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200556
557/**
Rose Zadik7f441272018-01-22 11:48:23 +0000558 * \brief Internal AES block decryption function. This is only
559 * exposed to allow overriding it using see
560 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200561 *
Rose Zadik7f441272018-01-22 11:48:23 +0000562 * \param ctx The AES context to use for decryption.
563 * \param input The ciphertext block.
564 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000565 *
Rose Zadik7f441272018-01-22 11:48:23 +0000566 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200567 */
Andres AGf5bf7182017-03-03 14:09:56 +0000568int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
569 const unsigned char input[16],
570 unsigned char output[16] );
571
572#if !defined(MBEDTLS_DEPRECATED_REMOVED)
573#if defined(MBEDTLS_DEPRECATED_WARNING)
574#define MBEDTLS_DEPRECATED __attribute__((deprecated))
575#else
576#define MBEDTLS_DEPRECATED
577#endif
578/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100579 * \brief Deprecated internal AES block encryption function
580 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000581 *
Rose Zadik7f441272018-01-22 11:48:23 +0000582 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000583 *
Rose Zadik7f441272018-01-22 11:48:23 +0000584 * \param ctx The AES context to use for encryption.
585 * \param input Plaintext block.
586 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000587 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100588MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
589 const unsigned char input[16],
590 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000591
592/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100593 * \brief Deprecated internal AES block decryption function
594 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000595 *
Rose Zadik7f441272018-01-22 11:48:23 +0000596 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000597 *
Rose Zadik7f441272018-01-22 11:48:23 +0000598 * \param ctx The AES context to use for decryption.
599 * \param input Ciphertext block.
600 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000601 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100602MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
603 const unsigned char input[16],
604 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000605
606#undef MBEDTLS_DEPRECATED
607#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200608
Paul Bakker5121ce52009-01-03 21:22:43 +0000609/**
Rose Zadik7f441272018-01-22 11:48:23 +0000610 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000611 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100612 * \return \c 0 on success.
613 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000614 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200615int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000616
617#ifdef __cplusplus
618}
619#endif
620
621#endif /* aes.h */