blob: f1c3d3a8c80374faef8f55521b3824f3f4583c09 [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
Andres Amaya Garciac5380642017-11-28 19:57:51 +000039/* Error codes in range 0x0020-0x0022 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
41#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Andres Amaya Garciac5380642017-11-28 19:57:51 +000042
43/* Error codes in range 0x0023-0x0023 */
44#define MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE -0x0023 /**< Feature not available, e.g. unsupported AES key size. */
Paul Bakker2b222c82009-07-27 21:03:45 +000045
Andres AGf5bf7182017-03-03 14:09:56 +000046#if ( defined(__ARMCC_VERSION) || defined(_MSC_VER) ) && \
47 !defined(inline) && !defined(__cplusplus)
48#define inline __inline
49#endif
50
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051#if !defined(MBEDTLS_AES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020052// Regular implementation
53//
54
Paul Bakker407a0da2013-06-27 14:29:21 +020055#ifdef __cplusplus
56extern "C" {
57#endif
58
Paul Bakker5121ce52009-01-03 21:22:43 +000059/**
60 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010061 *
62 * \note buf is able to hold 32 extra bytes, which can be used:
63 * - for alignment purposes if VIA padlock is used, and/or
64 * - to simplify key expansion in the 256-bit case by
65 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000066 */
67typedef struct
68{
69 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000070 uint32_t *rk; /*!< AES round keys */
71 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000072}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Paul Bakker5121ce52009-01-03 21:22:43 +000075/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020076 * \brief Initialize AES context
77 *
78 * \param ctx AES context to be initialized
79 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020081
82/**
83 * \brief Clear AES context
84 *
85 * \param ctx AES context to be cleared
86 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020088
89/**
Paul Bakker5121ce52009-01-03 21:22:43 +000090 * \brief AES key schedule (encryption)
91 *
92 * \param ctx AES context to be initialized
93 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020094 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000095 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000097 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020098int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020099 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000100
101/**
102 * \brief AES key schedule (decryption)
103 *
104 * \param ctx AES context to be initialized
105 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200106 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +0000107 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000109 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200110int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200111 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112
113/**
114 * \brief AES-ECB block encryption/decryption
115 *
116 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 * \param input 16-byte input block
119 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000120 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000121 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000125 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 unsigned char output[16] );
127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000129/**
130 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000131 * Length should be a multiple of the block
132 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000134 * \note Upon exit, the content of the IV is updated so that you can
135 * call the function same function again on the following
136 * block(s) of data and get the same result as if it was
137 * encrypted in one call. This allows a "streaming" usage.
138 * If on the other hand you need to retain the contents of the
139 * IV, you should either save it manually or use the cipher
140 * module instead.
141 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 * \param length length of the input data
145 * \param iv initialization vector (updated after use)
146 * \param input buffer holding the input data
147 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000148 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000153 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000155 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000160/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000161 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000163 * Note: Due to the nature of CFB you should use the same key schedule for
164 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200165 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000166 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000167 * \note Upon exit, the content of the IV is updated so that you can
168 * call the function same function again on the following
169 * block(s) of data and get the same result as if it was
170 * encrypted in one call. This allows a "streaming" usage.
171 * If on the other hand you need to retain the contents of the
172 * IV, you should either save it manually or use the cipher
173 * module instead.
174 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200176 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 * \param length length of the input data
178 * \param iv_off offset in IV (updated after use)
179 * \param iv initialization vector (updated after use)
180 * \param input buffer holding the input data
181 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000182 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000183 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000187 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000188 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000190 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 unsigned char *output );
192
Paul Bakker9a736322012-11-14 12:39:52 +0000193/**
Paul Bakker556efba2014-01-24 15:38:12 +0100194 * \brief AES-CFB8 buffer encryption/decryption.
195 *
196 * Note: Due to the nature of CFB you should use the same key schedule for
197 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakker556efba2014-01-24 15:38:12 +0100199 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000200 * \note Upon exit, the content of the IV is updated so that you can
201 * call the function same function again on the following
202 * block(s) of data and get the same result as if it was
203 * encrypted in one call. This allows a "streaming" usage.
204 * If on the other hand you need to retain the contents of the
205 * IV, you should either save it manually or use the cipher
206 * module instead.
207 *
Paul Bakker556efba2014-01-24 15:38:12 +0100208 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker556efba2014-01-24 15:38:12 +0100210 * \param length length of the input data
211 * \param iv initialization vector (updated after use)
212 * \param input buffer holding the input data
213 * \param output buffer holding the output data
214 *
215 * \return 0 if successful
216 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100218 int mode,
219 size_t length,
220 unsigned char iv[16],
221 const unsigned char *input,
222 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100224
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100226/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000227 * \brief AES-CTR buffer encryption/decryption
228 *
229 * Warning: You have to keep the maximum use of your counter in mind!
230 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000231 * Note: Due to the nature of CTR you should use the same key schedule for
232 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000234 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200235 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000236 * \param length The length of the data
237 * \param nc_off The offset in the current stream_block (for resuming
238 * within current cipher stream). The offset pointer to
239 * should be 0 at the start of a stream.
240 * \param nonce_counter The 128-bit nonce and counter.
241 * \param stream_block The saved stream-block for resuming. Is overwritten
242 * by the function.
243 * \param input The input data stream
244 * \param output The output data stream
245 *
246 * \return 0 if successful
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000249 size_t length,
250 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000251 unsigned char nonce_counter[16],
252 unsigned char stream_block[16],
253 const unsigned char *input,
254 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200256
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200257/**
258 * \brief Internal AES block encryption function
259 * (Only exposed to allow overriding it,
260 * see MBEDTLS_AES_ENCRYPT_ALT)
261 *
262 * \param ctx AES context
263 * \param input Plaintext block
264 * \param output Output (ciphertext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000265 *
266 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200267 */
Andres AGf5bf7182017-03-03 14:09:56 +0000268int mbedtls_internal_aes_encrypt( mbedtls_aes_context *ctx,
269 const unsigned char input[16],
270 unsigned char output[16] );
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200271
272/**
273 * \brief Internal AES block decryption function
274 * (Only exposed to allow overriding it,
275 * see MBEDTLS_AES_DECRYPT_ALT)
276 *
277 * \param ctx AES context
278 * \param input Ciphertext block
279 * \param output Output (plaintext) block
Andres AGf5bf7182017-03-03 14:09:56 +0000280 *
281 * \return 0 if successful
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200282 */
Andres AGf5bf7182017-03-03 14:09:56 +0000283int mbedtls_internal_aes_decrypt( mbedtls_aes_context *ctx,
284 const unsigned char input[16],
285 unsigned char output[16] );
286
287#if !defined(MBEDTLS_DEPRECATED_REMOVED)
288#if defined(MBEDTLS_DEPRECATED_WARNING)
289#define MBEDTLS_DEPRECATED __attribute__((deprecated))
290#else
291#define MBEDTLS_DEPRECATED
292#endif
293/**
294 * \brief Internal AES block encryption function
295 * (Only exposed to allow overriding it,
296 * see MBEDTLS_AES_ENCRYPT_ALT)
297 *
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 */
304MBEDTLS_DEPRECATED static inline void mbedtls_aes_encrypt(
305 mbedtls_aes_context *ctx,
306 const unsigned char input[16],
307 unsigned char output[16] )
308{
309 mbedtls_internal_aes_encrypt( ctx, input, output );
310}
311
312/**
313 * \brief Internal AES block decryption function
314 * (Only exposed to allow overriding it,
315 * see MBEDTLS_AES_DECRYPT_ALT)
316 *
317 * \deprecated Superseded by mbedtls_aes_decrypt_ext() in 2.5.0
318 *
319 * \param ctx AES context
320 * \param input Ciphertext block
321 * \param output Output (plaintext) block
322 */
323MBEDTLS_DEPRECATED static inline void mbedtls_aes_decrypt(
324 mbedtls_aes_context *ctx,
325 const unsigned char input[16],
326 unsigned char output[16] )
327{
328 mbedtls_internal_aes_decrypt( ctx, input, output );
329}
330
331#undef MBEDTLS_DEPRECATED
332#endif /* !MBEDTLS_DEPRECATED_REMOVED */
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200333
Paul Bakker90995b52013-06-24 19:20:35 +0200334#ifdef __cplusplus
335}
336#endif
337
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200338#else /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200339#include "aes_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200340#endif /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200341
342#ifdef __cplusplus
343extern "C" {
344#endif
345
Paul Bakker5121ce52009-01-03 21:22:43 +0000346/**
347 * \brief Checkup routine
348 *
349 * \return 0 if successful, or 1 if the test failed
350 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200351int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000352
353#ifdef __cplusplus
354}
355#endif
356
357#endif /* aes.h */