blob: 660ec2addc79c9ec62a2833379fb6f00eead550c [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. */
Andres Amaya Garciafd487392017-06-14 16:19:12 +010041#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x006E /**< Feature not available, e.g. unsupported AES key size. */
Paul Bakker2b222c82009-07-27 21:03:45 +000042
Andres AGf5bf7182017-03-03 14:09:56 +000043#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
44 !defined(inline) && !defined(__cplusplus)
45#define inline __inline
46#endif
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_AES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020049// Regular implementation
50//
51
Paul Bakker407a0da2013-06-27 14:29:21 +020052#ifdef __cplusplus
53extern "C" {
54#endif
55
Paul Bakker5121ce52009-01-03 21:22:43 +000056/**
57 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010058 *
59 * \note buf is able to hold 32 extra bytes, which can be used:
60 * - for alignment purposes if VIA padlock is used, and/or
61 * - to simplify key expansion in the 256-bit case by
62 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000063 */
64typedef struct
65{
66 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000067 uint32_t *rk; /*!< AES round keys */
68 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000069}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Paul Bakker5121ce52009-01-03 21:22:43 +000072/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020073 * \brief Initialize AES context
74 *
75 * \param ctx AES context to be initialized
76 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020077void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020078
79/**
80 * \brief Clear AES context
81 *
82 * \param ctx AES context to be cleared
83 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020085
86/**
Paul Bakker5121ce52009-01-03 21:22:43 +000087 * \brief AES key schedule (encryption)
88 *
89 * \param ctx AES context to be initialized
90 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020091 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000092 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020093 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000094 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020095int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020096 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +000097
98/**
99 * \brief AES key schedule (decryption)
100 *
101 * \param ctx AES context to be initialized
102 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200103 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +0000104 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200105 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000106 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200108 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110/**
111 * \brief AES-ECB block encryption/decryption
112 *
113 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 * \param input 16-byte input block
116 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000117 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000118 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000122 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 unsigned char output[16] );
124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000126/**
127 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000128 * Length should be a multiple of the block
129 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000130 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000131 * \note Upon exit, the content of the IV is updated so that you can
132 * call the function same function again on the following
133 * block(s) of data and get the same result as if it was
134 * encrypted in one call. This allows a "streaming" usage.
135 * If on the other hand you need to retain the contents of the
136 * IV, you should either save it manually or use the cipher
137 * module instead.
138 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 * \param length length of the input data
142 * \param iv initialization vector (updated after use)
143 * \param input buffer holding the input data
144 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000145 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000150 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000152 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000157/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000158 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000160 * Note: Due to the nature of CFB you should use the same key schedule for
161 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000163 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000164 * \note Upon exit, the content of the IV is updated so that you can
165 * call the function same function again on the following
166 * block(s) of data and get the same result as if it was
167 * encrypted in one call. This allows a "streaming" usage.
168 * If on the other hand you need to retain the contents of the
169 * IV, you should either save it manually or use the cipher
170 * module instead.
171 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 * \param length length of the input data
175 * \param iv_off offset in IV (updated after use)
176 * \param iv initialization vector (updated after use)
177 * \param input buffer holding the input data
178 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000179 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000180 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200182int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000184 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000185 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000187 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 unsigned char *output );
189
Paul Bakker9a736322012-11-14 12:39:52 +0000190/**
Paul Bakker556efba2014-01-24 15:38:12 +0100191 * \brief AES-CFB8 buffer encryption/decryption.
192 *
193 * Note: Due to the nature of CFB you should use the same key schedule for
194 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakker556efba2014-01-24 15:38:12 +0100196 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000197 * \note Upon exit, the content of the IV is updated so that you can
198 * call the function same function again on the following
199 * block(s) of data and get the same result as if it was
200 * encrypted in one call. This allows a "streaming" usage.
201 * If on the other hand you need to retain the contents of the
202 * IV, you should either save it manually or use the cipher
203 * module instead.
204 *
Paul Bakker556efba2014-01-24 15:38:12 +0100205 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker556efba2014-01-24 15:38:12 +0100207 * \param length length of the input data
208 * \param iv initialization vector (updated after use)
209 * \param input buffer holding the input data
210 * \param output buffer holding the output data
211 *
212 * \return 0 if successful
213 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100215 int mode,
216 size_t length,
217 unsigned char iv[16],
218 const unsigned char *input,
219 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100221
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100223/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000224 * \brief AES-CTR buffer encryption/decryption
225 *
226 * Warning: You have to keep the maximum use of your counter in mind!
227 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000228 * Note: Due to the nature of CTR you should use the same key schedule for
229 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000231 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200232 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000233 * \param length The length of the data
234 * \param nc_off The offset in the current stream_block (for resuming
235 * within current cipher stream). The offset pointer to
236 * should be 0 at the start of a stream.
237 * \param nonce_counter The 128-bit nonce and counter.
238 * \param stream_block The saved stream-block for resuming. Is overwritten
239 * by the function.
240 * \param input The input data stream
241 * \param output The output data stream
242 *
243 * \return 0 if successful
244 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200245int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000246 size_t length,
247 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000248 unsigned char nonce_counter[16],
249 unsigned char stream_block[16],
250 const unsigned char *input,
251 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200253
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200254/**
255 * \brief Internal AES block encryption function
256 * (Only exposed to allow overriding it,
257 * see MBEDTLS_AES_ENCRYPT_ALT)
258 *
259 * \param ctx AES context
260 * \param input Plaintext block
261 * \param output Output (ciphertext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000262 *
263 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200264 */
Andres AGf5bf7182017-03-03 14:09:56 +0000265int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
266 const unsigned char input[16],
267 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200268
269/**
270 * \brief Internal AES block decryption function
271 * (Only exposed to allow overriding it,
272 * see MBEDTLS_AES_DECRYPT_ALT)
273 *
274 * \param ctx AES context
275 * \param input Ciphertext block
276 * \param output Output (plaintext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000277 *
278 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200279 */
Andres AGf5bf7182017-03-03 14:09:56 +0000280int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
281 const unsigned char input[16],
282 unsigned char output[16] );
283
284#if !defined(MBEDTLS_DEPRECATED_REMOVED)
285#if defined(MBEDTLS_DEPRECATED_WARNING)
286#define MBEDTLS_DEPRECATED __attribute__((deprecated))
287#else
288#define MBEDTLS_DEPRECATED
289#endif
290/**
291 * \brief Internal AES block encryption function
292 * (Only exposed to allow overriding it,
293 * see MBEDTLS_AES_ENCRYPT_ALT)
294 *
295 * \deprecated Superseded by mbedtls_aes_encrypt_ext() in 2.5.0
296 *
297 * \param ctx AES context
298 * \param input Plaintext block
299 * \param output Output (ciphertext) block
300 */
301MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
302 mbedtls_aes_context *ctx,
303 const unsigned char input[16],
304 unsigned char output[16] )
305{
306 mbedtls_internal_aes_encrypt( ctx, input, output );
307}
308
309/**
310 * \brief Internal AES block decryption function
311 * (Only exposed to allow overriding it,
312 * see MBEDTLS_AES_DECRYPT_ALT)
313 *
314 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
315 *
316 * \param ctx AES context
317 * \param input Ciphertext block
318 * \param output Output (plaintext) block
319 */
320MBEDTLS_DEPRECATED static inline void mbedtls_aes_decrypt(
321 mbedtls_aes_context *ctx,
322 const unsigned char input[16],
323 unsigned char output[16] )
324{
325 mbedtls_internal_aes_decrypt( ctx, input, output );
326}
327
328#undef MBEDTLS_DEPRECATED
329#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200330
Paul Bakker90995b52013-06-24 19:20:35 +0200331#ifdef __cplusplus
332}
333#endif
334
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200335#else /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200336#include "aes_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337#endif /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200338
339#ifdef __cplusplus
340extern "C" {
341#endif
342
Paul Bakker5121ce52009-01-03 21:22:43 +0000343/**
344 * \brief Checkup routine
345 *
346 * \return 0 if successful, or 1 if the test failed
347 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000349
350#ifdef __cplusplus
351}
352#endif
353
354#endif /* aes.h */