blob: 71dcea9e5564a2218efc963e3452a2e1dfaa6feb [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/**
2 * \file aes.h
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00003 *
Paul Bakkerf3b86c12011-01-27 15:24:17 +00004 * \brief AES block cipher
Darryl Greena40a1012018-01-05 15:33:17 +00005 */
6/*
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02007 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02008 * SPDX-License-Identifier: Apache-2.0
9 *
10 * Licensed under the Apache License, Version 2.0 (the "License"); you may
11 * not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
13 *
14 * http://www.apache.org/licenses/LICENSE-2.0
15 *
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
18 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000021 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000022 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000023 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#ifndef MBEDTLS_AES_H
25#define MBEDTLS_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000026
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020028#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020029#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020031#endif
Paul Bakker90995b52013-06-24 19:20:35 +020032
Rich Evans00ab4702015-02-06 13:43:58 +000033#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020034#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000035
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010036/* padlock.c and aesni.c rely on these values! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#define MBEDTLS_AES_ENCRYPT 1
38#define MBEDTLS_AES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000039
Andres Amaya Garciac5380642017-11-28 19:57:51 +000040/* Error codes in range 0x0020-0x0022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
42#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000043
Andres Amaya Garciac5380642017-11-28 19:57:51 +000044/* Error codes in range 0x0023-0x0023 */
45#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available, e.g. unsupported AES key size. */
Paul Bakker5121ce52009-01-03 21:22:43 +000046
Andres AGf5bf7182017-03-03 14:09:56 +000047#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
48 !defined(inline) && !defined(__cplusplus)
49#define inline __inline
50#endif
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_AES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020053// Regular implementation
54//
55
Paul Bakker407a0da2013-06-27 14:29:21 +020056#ifdef __cplusplus
57extern "C" {
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
61 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010062 *
63 * \note buf is able to hold 32 extra bytes, which can be used:
64 * - for alignment purposes if VIA padlock is used, and/or
65 * - to simplify key expansion in the 256-bit case by
66 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000067 */
68typedef struct
69{
70 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000071 uint32_t *rk; /*!< AES round keys */
72 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000073}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077 * \brief Initialize AES context
78 *
79 * \param ctx AES context to be initialized
80 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020082
83/**
84 * \brief Clear AES context
85 *
86 * \param ctx AES context to be cleared
87 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020089
90/**
Paul Bakker5121ce52009-01-03 21:22:43 +000091 * \brief AES key schedule (encryption)
92 *
93 * \param ctx AES context to be initialized
94 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020095 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000096 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000098 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200100 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102/**
103 * \brief AES key schedule (decryption)
104 *
105 * \param ctx AES context to be initialized
106 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200107 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +0000108 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200112 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief AES-ECB block encryption/decryption
116 *
117 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 * \param input 16-byte input block
120 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000121 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000122 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 unsigned char output[16] );
128
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200129#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000130/**
131 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000132 * Length should be a multiple of the block
133 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000135 * \note Upon exit, the content of the IV is updated so that you can
136 * call the function same function again on the following
137 * block(s) of data and get the same result as if it was
138 * encrypted in one call. This allows a "streaming" usage.
139 * If on the other hand you need to retain the contents of the
140 * IV, you should either save it manually or use the cipher
141 * module instead.
142 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 * \param length length of the input data
146 * \param iv initialization vector (updated after use)
147 * \param input buffer holding the input data
148 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000149 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000154 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000156 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000161/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000162 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000164 * Note: Due to the nature of CFB you should use the same key schedule for
165 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000167 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000168 * \note Upon exit, the content of the IV is updated so that you can
169 * call the function same function again on the following
170 * block(s) of data and get the same result as if it was
171 * encrypted in one call. This allows a "streaming" usage.
172 * If on the other hand you need to retain the contents of the
173 * IV, you should either save it manually or use the cipher
174 * module instead.
175 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 * \param length length of the input data
179 * \param iv_off offset in IV (updated after use)
180 * \param iv initialization vector (updated after use)
181 * \param input buffer holding the input data
182 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000183 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000184 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000188 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000189 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000191 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000192 unsigned char *output );
193
Paul Bakker9a736322012-11-14 12:39:52 +0000194/**
Paul Bakker556efba2014-01-24 15:38:12 +0100195 * \brief AES-CFB8 buffer encryption/decryption.
196 *
197 * Note: Due to the nature of CFB you should use the same key schedule for
198 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200199 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakker556efba2014-01-24 15:38:12 +0100200 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000201 * \note Upon exit, the content of the IV is updated so that you can
202 * call the function same function again on the following
203 * block(s) of data and get the same result as if it was
204 * encrypted in one call. This allows a "streaming" usage.
205 * If on the other hand you need to retain the contents of the
206 * IV, you should either save it manually or use the cipher
207 * module instead.
208 *
Paul Bakker556efba2014-01-24 15:38:12 +0100209 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker556efba2014-01-24 15:38:12 +0100211 * \param length length of the input data
212 * \param iv initialization vector (updated after use)
213 * \param input buffer holding the input data
214 * \param output buffer holding the output data
215 *
216 * \return 0 if successful
217 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100219 int mode,
220 size_t length,
221 unsigned char iv[16],
222 const unsigned char *input,
223 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100227/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000228 * \brief AES-CTR buffer encryption/decryption
229 *
230 * Warning: You have to keep the maximum use of your counter in mind!
231 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000232 * Note: Due to the nature of CTR you should use the same key schedule for
233 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200234 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000235 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200236 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000237 * \param length The length of the data
238 * \param nc_off The offset in the current stream_block (for resuming
239 * within current cipher stream). The offset pointer to
240 * should be 0 at the start of a stream.
241 * \param nonce_counter The 128-bit nonce and counter.
242 * \param stream_block The saved stream-block for resuming. Is overwritten
243 * by the function.
244 * \param input The input data stream
245 * \param output The output data stream
246 *
247 * \return 0 if successful
248 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000250 size_t length,
251 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000252 unsigned char nonce_counter[16],
253 unsigned char stream_block[16],
254 const unsigned char *input,
255 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200256#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200257
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200258/**
259 * \brief Internal AES block encryption function
260 * (Only exposed to allow overriding it,
261 * see MBEDTLS_AES_ENCRYPT_ALT)
262 *
263 * \param ctx AES context
264 * \param input Plaintext block
265 * \param output Output (ciphertext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000266 *
267 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200268 */
Andres AGf5bf7182017-03-03 14:09:56 +0000269int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
270 const unsigned char input[16],
271 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200272
273/**
274 * \brief Internal AES block decryption function
275 * (Only exposed to allow overriding it,
276 * see MBEDTLS_AES_DECRYPT_ALT)
277 *
278 * \param ctx AES context
279 * \param input Ciphertext block
280 * \param output Output (plaintext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000281 *
282 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200283 */
Andres AGf5bf7182017-03-03 14:09:56 +0000284int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
285 const unsigned char input[16],
286 unsigned char output[16] );
287
288#if !defined(MBEDTLS_DEPRECATED_REMOVED)
289#if defined(MBEDTLS_DEPRECATED_WARNING)
290#define MBEDTLS_DEPRECATED __attribute__((deprecated))
291#else
292#define MBEDTLS_DEPRECATED
293#endif
294/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100295 * \brief Deprecated internal AES block encryption function
296 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000297 *
298 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
299 *
300 * \param ctx AES context
301 * \param input Plaintext block
302 * \param output Output (ciphertext) block
303 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100304MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
305 const unsigned char input[16],
306 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000307
308/**
Hanno Beckerca1cdb22017-07-20 09:50:59 +0100309 * \brief Deprecated internal AES block decryption function
310 * without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000311 *
312 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
313 *
314 * \param ctx AES context
315 * \param input Ciphertext block
316 * \param output Output (plaintext) block
317 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100318MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
319 const unsigned char input[16],
320 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000321
322#undef MBEDTLS_DEPRECATED
323#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200324
Paul Bakker90995b52013-06-24 19:20:35 +0200325#ifdef __cplusplus
326}
327#endif
328
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200329#else /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200330#include "aes_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200331#endif /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200332
333#ifdef __cplusplus
334extern "C" {
335#endif
336
Paul Bakker5121ce52009-01-03 21:22:43 +0000337/**
338 * \brief Checkup routine
339 *
340 * \return 0 if successful, or 1 if the test failed
341 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200342int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000343
344#ifdef __cplusplus
345}
346#endif
347
348#endif /* aes.h */