blob: c42ca7ac86fb031577b0061258d2767d599444bc [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
Ron Eldorb2aacec2017-05-18 16:53:08 +030092#else /* MBEDTLS_AES_ALT */
93#include "aes_alt.h"
94#endif /* MBEDTLS_AES_ALT */
95
Paul Bakker5121ce52009-01-03 21:22:43 +000096/**
Rose Zadik7f441272018-01-22 11:48:23 +000097 * \brief This function initializes the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020098 *
Rose Zadik7f441272018-01-22 11:48:23 +000099 * It must be the first API called before using
100 * the context.
101 *
102 * \param ctx The AES context to initialize.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200103 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200105
106/**
Rose Zadik7f441272018-01-22 11:48:23 +0000107 * \brief This function releases and clears the specified AES context.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200108 *
Rose Zadik7f441272018-01-22 11:48:23 +0000109 * \param ctx The AES context to clear.
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +0200112
113/**
Rose Zadik7f441272018-01-22 11:48:23 +0000114 * \brief This function sets the encryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 *
Rose Zadik7f441272018-01-22 11:48:23 +0000116 * \param ctx The AES context to which the key should be bound.
117 * \param key The encryption key.
118 * \param keybits The size of data passed in bits. Valid options are:
119 * <ul><li>128 bits</li>
120 * <li>192 bits</li>
121 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000122 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100123 * \return \c 0 on success.
Rose Zadik819d13d2018-04-16 09:35:15 +0100124 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200127 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128
129/**
Rose Zadik7f441272018-01-22 11:48:23 +0000130 * \brief This function sets the decryption key.
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 *
Rose Zadik7f441272018-01-22 11:48:23 +0000132 * \param ctx The AES context to which the key should be bound.
133 * \param key The decryption key.
134 * \param keybits The size of data passed. Valid options are:
135 * <ul><li>128 bits</li>
136 * <li>192 bits</li>
137 * <li>256 bits</li></ul>
Paul Bakker2b222c82009-07-27 21:03:45 +0000138 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100139 * \return \c 0 on success.
140 * \return #MBEDTLS_ERR_AES_INVALID_KEY_LENGTH on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200143 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000144
145/**
Rose Zadik7f441272018-01-22 11:48:23 +0000146 * \brief This function performs an AES single-block encryption or
147 * decryption operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 *
Rose Zadik7f441272018-01-22 11:48:23 +0000149 * It performs the operation defined in the \p mode parameter
150 * (encrypt or decrypt), on the input data buffer defined in
151 * the \p input parameter.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000152 *
Rose Zadik7f441272018-01-22 11:48:23 +0000153 * mbedtls_aes_init(), and either mbedtls_aes_setkey_enc() or
154 * mbedtls_aes_setkey_dec() must be called before the first
155 * call to this API with the same context.
156 *
157 * \param ctx The AES context to use for encryption or decryption.
158 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
159 * #MBEDTLS_AES_DECRYPT.
160 * \param input The 16-Byte buffer holding the input data.
161 * \param output The 16-Byte buffer holding the output data.
162
163 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000166 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000167 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 unsigned char output[16] );
169
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000171/**
Rose Zadik7f441272018-01-22 11:48:23 +0000172 * \brief This function performs an AES-CBC encryption or decryption operation
173 * on full blocks.
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 *
Rose Zadik7f441272018-01-22 11:48:23 +0000175 * It performs the operation defined in the \p mode
176 * parameter (encrypt/decrypt), on the input data buffer defined in
177 * the \p input parameter.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000178 *
Rose Zadik7f441272018-01-22 11:48:23 +0000179 * It can be called as many times as needed, until all the input
180 * data is processed. mbedtls_aes_init(), and either
181 * mbedtls_aes_setkey_enc() or mbedtls_aes_setkey_dec() must be called
182 * before the first call to this API with the same context.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000183 *
Rose Zadik7f441272018-01-22 11:48:23 +0000184 * \note This function operates on aligned blocks, that is, the input size
185 * must be a multiple of the AES block size of 16 Bytes.
186 *
187 * \note Upon exit, the content of the IV is updated so that you can
188 * call the same function again on the next
189 * block(s) of data and get the same result as if it was
190 * encrypted in one call. This allows a "streaming" usage.
191 * If you need to retain the contents of the IV, you should
192 * either save it manually or use the cipher module instead.
193 *
194 *
195 * \param ctx The AES context to use for encryption or decryption.
196 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
197 * #MBEDTLS_AES_DECRYPT.
198 * \param length The length of the input data in Bytes. This must be a
199 * multiple of the block size (16 Bytes).
200 * \param iv Initialization vector (updated after use).
201 * \param input The buffer holding the input data.
202 * \param output The buffer holding the output data.
203 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100204 * \return \c 0 on success.
205 * \return #MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Rose Zadik7f441272018-01-22 11:48:23 +0000206 * on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000210 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000212 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000213 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000215
Aorimn0089d362016-01-31 12:15:51 +0100216#if defined(MBEDTLS_CIPHER_MODE_XEX)
217/**
218 * \brief AES-XEX buffer encryption/decryption
219 * Length should be a multiple of the block size (16 bytes)
220 *
221 * \param crypt_ctx AES context for encrypting data
222 * \param tweak_ctx AES context for xor-ing with data
223 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
224 * \param length length of the input data
225 * \param iv initialization vector
226 * \param input buffer holding the input data
227 * \param output buffer holding the output data
228 *
229 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
230 */
231int mbedtls_aes_crypt_xex( mbedtls_aes_context *crypt_ctx,
232 mbedtls_aes_context *tweak_ctx,
233 int mode,
234 size_t length,
235 unsigned char iv[16],
236 const unsigned char *input,
237 unsigned char *output );
238#endif /* MBEDTLS_CIPHER_MODE_XEX */
239
Aorimn5f778012016-06-09 23:22:58 +0200240#if defined(MBEDTLS_CIPHER_MODE_XTS)
241/**
242 * \brief AES-XTS buffer encryption/decryption
243 * Length should be greater or equal than the block size (16
244 * bytes, 128 bits)
245 *
246 * Warning: The bits_length parameter must given be in bits, not bytes like the
247 * other modes
248 *
249 * \param crypt_ctx AES context for encrypting data
250 * \param tweak_ctx AES context for xor-ing with data
251 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
252 * \param bits_length length of the input data (in bits)
253 * \param iv initialization vector
254 * \param input buffer holding the input data
255 * \param output buffer holding the output data
256 *
257 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
258 */
259int mbedtls_aes_crypt_xts( mbedtls_aes_context *crypt_ctx,
260 mbedtls_aes_context *tweak_ctx,
261 int mode,
262 size_t bits_length,
263 unsigned char iv[16],
264 const unsigned char *input,
265 unsigned char *output );
266#endif /* MBEDTLS_CIPHER_MODE_XTS */
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000269/**
Rose Zadik7f441272018-01-22 11:48:23 +0000270 * \brief This function performs an AES-CFB128 encryption or decryption
271 * operation.
Paul Bakker5121ce52009-01-03 21:22:43 +0000272 *
Rose Zadik7f441272018-01-22 11:48:23 +0000273 * It performs the operation defined in the \p mode
274 * parameter (encrypt or decrypt), on the input data buffer
275 * defined in the \p input parameter.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000276 *
Rose Zadik7f441272018-01-22 11:48:23 +0000277 * For CFB, you must set up the context with mbedtls_aes_setkey_enc(),
278 * regardless of whether you are performing an encryption or decryption
279 * operation, that is, regardless of the \p mode parameter. This is
280 * because CFB mode uses the same key schedule for encryption and
281 * decryption.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000282 *
Rose Zadik7f441272018-01-22 11:48:23 +0000283 * \note Upon exit, the content of the IV is updated so that you can
284 * call the same function again on the next
285 * block(s) of data and get the same result as if it was
286 * encrypted in one call. This allows a "streaming" usage.
287 * If you need to retain the contents of the
288 * IV, you must either save it manually or use the cipher
289 * module instead.
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000290 *
Rose Zadik7f441272018-01-22 11:48:23 +0000291 *
292 * \param ctx The AES context to use for encryption or decryption.
293 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
294 * #MBEDTLS_AES_DECRYPT.
295 * \param length The length of the input data.
296 * \param iv_off The offset in IV (updated after use).
297 * \param iv The initialization vector (updated after use).
298 * \param input The buffer holding the input data.
299 * \param output The buffer holding the output data.
300 *
301 * \return \c 0 on success.
Paul Bakker5121ce52009-01-03 21:22:43 +0000302 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000304 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000305 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000306 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000307 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000308 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000309 unsigned char *output );
310
Paul Bakker9a736322012-11-14 12:39:52 +0000311/**
Rose Zadik7f441272018-01-22 11:48:23 +0000312 * \brief This function performs an AES-CFB8 encryption or decryption
313 * operation.
Paul Bakker556efba2014-01-24 15:38:12 +0100314 *
Rose Zadik7f441272018-01-22 11:48:23 +0000315 * It performs the operation defined in the \p mode
316 * parameter (encrypt/decrypt), on the input data buffer defined
317 * in the \p input parameter.
Paul Bakker556efba2014-01-24 15:38:12 +0100318 *
Rose Zadik7f441272018-01-22 11:48:23 +0000319 * Due to the nature of CFB, you must use the same key schedule for
320 * both encryption and decryption operations. Therefore, you must
321 * use the context initialized with mbedtls_aes_setkey_enc() for
322 * both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000323 *
Rose Zadik7f441272018-01-22 11:48:23 +0000324 * \note Upon exit, the content of the IV is updated so that you can
325 * call the same function again on the next
326 * block(s) of data and get the same result as if it was
327 * encrypted in one call. This allows a "streaming" usage.
328 * If you need to retain the contents of the
329 * IV, you should either save it manually or use the cipher
330 * module instead.
Paul Bakker556efba2014-01-24 15:38:12 +0100331 *
Rose Zadik7f441272018-01-22 11:48:23 +0000332 *
333 * \param ctx The AES context to use for encryption or decryption.
334 * \param mode The AES operation: #MBEDTLS_AES_ENCRYPT or
335 * #MBEDTLS_AES_DECRYPT
336 * \param length The length of the input data.
337 * \param iv The initialization vector (updated after use).
338 * \param input The buffer holding the input data.
339 * \param output The buffer holding the output data.
340 *
341 * \return \c 0 on success.
Paul Bakker556efba2014-01-24 15:38:12 +0100342 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100344 int mode,
345 size_t length,
346 unsigned char iv[16],
347 const unsigned char *input,
348 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200349#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100350
Simon Butcher76a5b222018-04-22 22:57:27 +0100351#if defined(MBEDTLS_CIPHER_MODE_OFB)
352/**
Simon Butcher5db13622018-06-04 22:11:25 +0100353 * \brief This function performs an AES-OFB (Output Feedback Mode)
354 * encryption or decryption operation.
Simon Butcher76a5b222018-04-22 22:57:27 +0100355 *
Simon Butcher5db13622018-06-04 22:11:25 +0100356 * For OFB, you must set up the context with
357 * mbedtls_aes_setkey_enc(), regardless of whether you are
358 * performing an encryption or decryption operation. This is
359 * because OFB mode uses the same key schedule for encryption and
360 * decryption.
Simon Butcher76a5b222018-04-22 22:57:27 +0100361 *
Simon Butcher5db13622018-06-04 22:11:25 +0100362 * The OFB operation is identical for encryption or decryption,
363 * therefore no operation mode needs to be specified.
Simon Butcher76a5b222018-04-22 22:57:27 +0100364 *
Simon Butcher5db13622018-06-04 22:11:25 +0100365 * \note Upon exit, the content of iv, the Initialisation Vector, is
366 * updated so that you can call the same function again on the next
367 * block(s) of data and get the same result as if it was encrypted
368 * in one call. This allows a "streaming" usage, by initialising
369 * iv_off to 0 before the first call, and preserving its value
370 * between calls.
Simon Butcher968646c2018-06-02 18:27:04 +0100371 *
Simon Butcher5db13622018-06-04 22:11:25 +0100372 * For non-streaming use, the iv should be initialised on each call
373 * to a unique value, and iv_off set to 0 on each call.
Simon Butcher968646c2018-06-02 18:27:04 +0100374 *
Simon Butcher5db13622018-06-04 22:11:25 +0100375 * If you need to retain the contents of the initialisation vector,
376 * you must either save it manually or use the cipher module
377 * instead.
Simon Butcher968646c2018-06-02 18:27:04 +0100378 *
Jaeden Amerocb2c9352018-06-08 10:34:08 +0100379 * \warning For the OFB mode, the initialisation vector must be unique
380 * every encryption operation. Reuse of an initialisation vector
381 * will compromise security.
Simon Butcher76a5b222018-04-22 22:57:27 +0100382 *
383 * \param ctx The AES context to use for encryption or decryption.
384 * \param length The length of the input data.
385 * \param iv_off The offset in IV (updated after use).
386 * \param iv The initialization vector (updated after use).
387 * \param input The buffer holding the input data.
388 * \param output The buffer holding the output data.
389 *
390 * \return \c 0 on success.
391 */
392int mbedtls_aes_crypt_ofb( mbedtls_aes_context *ctx,
393 size_t length,
394 size_t *iv_off,
395 unsigned char iv[16],
396 const unsigned char *input,
397 unsigned char *output );
398
399#endif /* MBEDTLS_CIPHER_MODE_OFB */
400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100402/**
Rose Zadik7f441272018-01-22 11:48:23 +0000403 * \brief This function performs an AES-CTR encryption or decryption
404 * operation.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000405 *
Rose Zadik7f441272018-01-22 11:48:23 +0000406 * This function performs the operation defined in the \p mode
407 * parameter (encrypt/decrypt), on the input data buffer
408 * defined in the \p input parameter.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000409 *
Rose Zadik7f441272018-01-22 11:48:23 +0000410 * Due to the nature of CTR, you must use the same key schedule
411 * for both encryption and decryption operations. Therefore, you
412 * must use the context initialized with mbedtls_aes_setkey_enc()
413 * for both #MBEDTLS_AES_ENCRYPT and #MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000414 *
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100415 * \warning You must never reuse a nonce value with the same key. Doing so
416 * would void the encryption for the two messages encrypted with
417 * the same nonce and key.
418 *
419 * There are two common strategies for managing nonces with CTR:
420 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200421 * 1. You can handle everything as a single message processed over
422 * successive calls to this function. In that case, you want to
423 * set \p nonce_counter and \p nc_off to 0 for the first call, and
424 * then preserve the values of \p nonce_counter, \p nc_off and \p
425 * stream_block across calls to this function as they will be
426 * updated by this function.
Manuel Pégourié-Gonnard22997b72018-02-28 12:29:41 +0100427 *
Manuel Pégourié-Gonnard4f24e952018-05-24 11:59:30 +0200428 * With this strategy, you must not encrypt more than 2**128
429 * blocks of data with the same key.
430 *
431 * 2. You can encrypt separate messages by dividing the \p
432 * nonce_counter buffer in two areas: the first one used for a
433 * per-message nonce, handled by yourself, and the second one
434 * updated by this function internally.
435 *
436 * For example, you might reserve the first 12 bytes for the
437 * per-message nonce, and the last 4 bytes for internal use. In that
438 * case, before calling this function on a new message you need to
439 * set the first 12 bytes of \p nonce_counter to your chosen nonce
440 * value, the last 4 to 0, and \p nc_off to 0 (which will cause \p
441 * stream_block to be ignored). That way, you can encrypt at most
442 * 2**96 messages of up to 2**32 blocks each with the same key.
443 *
444 * The per-message nonce (or information sufficient to reconstruct
445 * it) needs to be communicated with the ciphertext and must be unique.
446 * The recommended way to ensure uniqueness is to use a message
447 * counter. An alternative is to generate random nonces, but this
448 * limits the number of messages that can be securely encrypted:
449 * for example, with 96-bit random nonces, you should not encrypt
450 * more than 2**32 messages with the same key.
451 *
452 * Note that for both stategies, sizes are measured in blocks and
453 * that an AES block is 16 bytes.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000454 *
Manuel Pégourié-Gonnardfa0c47d2018-05-24 19:02:06 +0200455 * \warning Upon return, \p stream_block contains sensitive data. Its
456 * content must not be written to insecure storage and should be
457 * securely discarded as soon as it's no longer needed.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000458 *
Rose Zadik7f441272018-01-22 11:48:23 +0000459 * \param ctx The AES context to use for encryption or decryption.
460 * \param length The length of the input data.
461 * \param nc_off The offset in the current \p stream_block, for
462 * resuming within the current cipher stream. The
463 * offset pointer should be 0 at the start of a stream.
464 * \param nonce_counter The 128-bit nonce and counter.
465 * \param stream_block The saved stream block for resuming. This is
466 * overwritten by the function.
467 * \param input The buffer holding the input data.
468 * \param output The buffer holding the output data.
469 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100470 * \return \c 0 on success.
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000471 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200472int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000473 size_t length,
474 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000475 unsigned char nonce_counter[16],
476 unsigned char stream_block[16],
477 const unsigned char *input,
478 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200480
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200481/**
Rose Zadik7f441272018-01-22 11:48:23 +0000482 * \brief Internal AES block encryption function. This is only
483 * exposed to allow overriding it using
484 * \c MBEDTLS_AES_ENCRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200485 *
Rose Zadik7f441272018-01-22 11:48:23 +0000486 * \param ctx The AES context to use for encryption.
487 * \param input The plaintext block.
488 * \param output The output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000489 *
Rose Zadik7f441272018-01-22 11:48:23 +0000490 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200491 */
Andres AGf5bf7182017-03-03 14:09:56 +0000492int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
493 const unsigned char input[16],
494 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200495
496/**
Rose Zadik7f441272018-01-22 11:48:23 +0000497 * \brief Internal AES block decryption function. This is only
498 * exposed to allow overriding it using see
499 * \c MBEDTLS_AES_DECRYPT_ALT.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200500 *
Rose Zadik7f441272018-01-22 11:48:23 +0000501 * \param ctx The AES context to use for decryption.
502 * \param input The ciphertext block.
503 * \param output The output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000504 *
Rose Zadik7f441272018-01-22 11:48:23 +0000505 * \return \c 0 on success.
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200506 */
Andres AGf5bf7182017-03-03 14:09:56 +0000507int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
508 const unsigned char input[16],
509 unsigned char output[16] );
510
511#if !defined(MBEDTLS_DEPRECATED_REMOVED)
512#if defined(MBEDTLS_DEPRECATED_WARNING)
513#define MBEDTLS_DEPRECATED __attribute__((deprecated))
514#else
515#define MBEDTLS_DEPRECATED
516#endif
517/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100518 * \brief Deprecated internal AES block encryption function
519 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000520 *
Rose Zadik7f441272018-01-22 11:48:23 +0000521 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000522 *
Rose Zadik7f441272018-01-22 11:48:23 +0000523 * \param ctx The AES context to use for encryption.
524 * \param input Plaintext block.
525 * \param output Output (ciphertext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000526 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100527MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
528 const unsigned char input[16],
529 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000530
531/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100532 * \brief Deprecated internal AES block decryption function
533 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000534 *
Rose Zadik7f441272018-01-22 11:48:23 +0000535 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0.
Andres AGf5bf7182017-03-03 14:09:56 +0000536 *
Rose Zadik7f441272018-01-22 11:48:23 +0000537 * \param ctx The AES context to use for decryption.
538 * \param input Ciphertext block.
539 * \param output Output (plaintext) block.
Andres AGf5bf7182017-03-03 14:09:56 +0000540 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100541MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
542 const unsigned char input[16],
543 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000544
545#undef MBEDTLS_DEPRECATED
546#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200547
Paul Bakker5121ce52009-01-03 21:22:43 +0000548/**
Rose Zadik7f441272018-01-22 11:48:23 +0000549 * \brief Checkup routine.
Paul Bakker5121ce52009-01-03 21:22:43 +0000550 *
Rose Zadik5ad7aea2018-03-26 12:00:09 +0100551 * \return \c 0 on success.
552 * \return \c 1 on failure.
Paul Bakker5121ce52009-01-03 21:22:43 +0000553 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000555
556#ifdef __cplusplus
557}
558#endif
559
560#endif /* aes.h */