blob: 4a546acc91032940cae0e0eee1e3df198d454576 [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
Paul Bakker37ca75d2011-01-06 12:28:03 +00005 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02006 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02007 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
Paul Bakkerb96f1542010-07-18 20:36:00 +000020 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000021 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#ifndef MBEDTLS_AES_H
24#define MBEDTLS_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020027#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker90995b52013-06-24 19:20:35 +020031
Rich Evans00ab4702015-02-06 13:43:58 +000032#include <stddef.h>
Manuel Pégourié-Gonnardab229102015-04-15 11:53:16 +020033#include <stdint.h>
Paul Bakker5c2364c2012-10-01 14:41:15 +000034
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010035/* padlock.c and aesni.c rely on these values! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#define MBEDTLS_AES_ENCRYPT 1
37#define MBEDTLS_AES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000038
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020039#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
40#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000041
Andres AGf5bf7182017-03-03 14:09:56 +000042#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
43 !defined(inline) && !defined(__cplusplus)
44#define inline __inline
45#endif
46
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047#if !defined(MBEDTLS_AES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020048// Regular implementation
49//
50
Paul Bakker407a0da2013-06-27 14:29:21 +020051#ifdef __cplusplus
52extern "C" {
53#endif
54
Paul Bakker5121ce52009-01-03 21:22:43 +000055/**
56 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010057 *
58 * \note buf is able to hold 32 extra bytes, which can be used:
59 * - for alignment purposes if VIA padlock is used, and/or
60 * - to simplify key expansion in the 256-bit case by
61 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000062 */
63typedef struct
64{
65 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000066 uint32_t *rk; /*!< AES round keys */
67 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000068}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020069mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000070
Paul Bakker5121ce52009-01-03 21:22:43 +000071/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020072 * \brief Initialize AES context
73 *
74 * \param ctx AES context to be initialized
75 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077
78/**
79 * \brief Clear AES context
80 *
81 * \param ctx AES context to be cleared
82 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020084
85/**
Paul Bakker5121ce52009-01-03 21:22:43 +000086 * \brief AES key schedule (encryption)
87 *
88 * \param ctx AES context to be initialized
89 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020090 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000091 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020092 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000093 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020095 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +000096
97/**
98 * \brief AES key schedule (decryption)
99 *
100 * \param ctx AES context to be initialized
101 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200102 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +0000103 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200107 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109/**
110 * \brief AES-ECB block encryption/decryption
111 *
112 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 * \param input 16-byte input block
115 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000116 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000117 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000121 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 unsigned char output[16] );
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000125/**
126 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000127 * Length should be a multiple of the block
128 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000130 * \note Upon exit, the content of the IV is updated so that you can
131 * call the function same function again on the following
132 * block(s) of data and get the same result as if it was
133 * encrypted in one call. This allows a "streaming" usage.
134 * If on the other hand you need to retain the contents of the
135 * IV, you should either save it manually or use the cipher
136 * module instead.
137 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 * \param length length of the input data
141 * \param iv initialization vector (updated after use)
142 * \param input buffer holding the input data
143 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000144 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000149 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000151 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000154
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000156/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000157 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000159 * Note: Due to the nature of CFB you should use the same key schedule for
160 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000162 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000163 * \note Upon exit, the content of the IV is updated so that you can
164 * call the function same function again on the following
165 * block(s) of data and get the same result as if it was
166 * encrypted in one call. This allows a "streaming" usage.
167 * If on the other hand you need to retain the contents of the
168 * IV, you should either save it manually or use the cipher
169 * module instead.
170 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 * \param length length of the input data
174 * \param iv_off offset in IV (updated after use)
175 * \param iv initialization vector (updated after use)
176 * \param input buffer holding the input data
177 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000178 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000179 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000180 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000183 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000184 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000185 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000186 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 unsigned char *output );
188
Paul Bakker9a736322012-11-14 12:39:52 +0000189/**
Paul Bakker556efba2014-01-24 15:38:12 +0100190 * \brief AES-CFB8 buffer encryption/decryption.
191 *
192 * Note: Due to the nature of CFB you should use the same key schedule for
193 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200194 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakker556efba2014-01-24 15:38:12 +0100195 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000196 * \note Upon exit, the content of the IV is updated so that you can
197 * call the function same function again on the following
198 * block(s) of data and get the same result as if it was
199 * encrypted in one call. This allows a "streaming" usage.
200 * If on the other hand you need to retain the contents of the
201 * IV, you should either save it manually or use the cipher
202 * module instead.
203 *
Paul Bakker556efba2014-01-24 15:38:12 +0100204 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker556efba2014-01-24 15:38:12 +0100206 * \param length length of the input data
207 * \param iv initialization vector (updated after use)
208 * \param input buffer holding the input data
209 * \param output buffer holding the output data
210 *
211 * \return 0 if successful
212 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100214 int mode,
215 size_t length,
216 unsigned char iv[16],
217 const unsigned char *input,
218 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200219#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100220
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100222/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000223 * \brief AES-CTR buffer encryption/decryption
224 *
225 * Warning: You have to keep the maximum use of your counter in mind!
226 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000227 * Note: Due to the nature of CTR you should use the same key schedule for
228 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200229 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000230 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200231 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000232 * \param length The length of the data
233 * \param nc_off The offset in the current stream_block (for resuming
234 * within current cipher stream). The offset pointer to
235 * should be 0 at the start of a stream.
236 * \param nonce_counter The 128-bit nonce and counter.
237 * \param stream_block The saved stream-block for resuming. Is overwritten
238 * by the function.
239 * \param input The input data stream
240 * \param output The output data stream
241 *
242 * \return 0 if successful
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000245 size_t length,
246 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000247 unsigned char nonce_counter[16],
248 unsigned char stream_block[16],
249 const unsigned char *input,
250 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200252
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200253/**
254 * \brief Internal AES block encryption function
255 * (Only exposed to allow overriding it,
256 * see MBEDTLS_AES_ENCRYPT_ALT)
257 *
258 * \param ctx AES context
259 * \param input Plaintext block
260 * \param output Output (ciphertext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000261 *
262 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200263 */
Andres AGf5bf7182017-03-03 14:09:56 +0000264int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
265 const unsigned char input[16],
266 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200267
268/**
269 * \brief Internal AES block decryption function
270 * (Only exposed to allow overriding it,
271 * see MBEDTLS_AES_DECRYPT_ALT)
272 *
273 * \param ctx AES context
274 * \param input Ciphertext block
275 * \param output Output (plaintext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000276 *
277 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200278 */
Andres AGf5bf7182017-03-03 14:09:56 +0000279int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
280 const unsigned char input[16],
281 unsigned char output[16] );
282
283#if !defined(MBEDTLS_DEPRECATED_REMOVED)
284#if defined(MBEDTLS_DEPRECATED_WARNING)
285#define MBEDTLS_DEPRECATED __attribute__((deprecated))
286#else
287#define MBEDTLS_DEPRECATED
288#endif
289/**
Hanno Becker6d84ae72017-06-26 12:46:19 +0100290 * \brief Old AES block encryption function without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000291 *
292 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
293 *
294 * \param ctx AES context
295 * \param input Plaintext block
296 * \param output Output (ciphertext) block
297 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100298MBEDTLS_DEPRECATED void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
299 const unsigned char input[16],
300 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000301
302/**
Hanno Becker6d84ae72017-06-26 12:46:19 +0100303 * \brief Old AES block decryption function without return value.
Andres AGf5bf7182017-03-03 14:09:56 +0000304 *
305 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
306 *
307 * \param ctx AES context
308 * \param input Ciphertext block
309 * \param output Output (plaintext) block
310 */
Hanno Beckerbedc2052017-06-26 12:46:56 +0100311MBEDTLS_DEPRECATED void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
312 const unsigned char input[16],
313 unsigned char output[16] );
Andres AGf5bf7182017-03-03 14:09:56 +0000314
315#undef MBEDTLS_DEPRECATED
316#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200317
Paul Bakker90995b52013-06-24 19:20:35 +0200318#ifdef __cplusplus
319}
320#endif
321
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200322#else /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200323#include "aes_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200324#endif /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200325
326#ifdef __cplusplus
327extern "C" {
328#endif
329
Paul Bakker5121ce52009-01-03 21:22:43 +0000330/**
331 * \brief Checkup routine
332 *
333 * \return 0 if successful, or 1 if the test failed
334 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000336
337#ifdef __cplusplus
338}
339#endif
340
341#endif /* aes.h */