blob: fb97c7b75fc5bdad59c5a0584384e7ccee1d4535 [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é-Gonnarda658a402015-01-23 09:45:19 +00006 * Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00008 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkerb96f1542010-07-18 20:36:00 +00009 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000010 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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
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. */
Paul Bakker2b222c82009-07-27 21:03:45 +000042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#if !defined(MBEDTLS_AES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020044// Regular implementation
45//
46
Paul Bakker407a0da2013-06-27 14:29:21 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Paul Bakker5121ce52009-01-03 21:22:43 +000051/**
52 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010053 *
54 * \note buf is able to hold 32 extra bytes, which can be used:
55 * - for alignment purposes if VIA padlock is used, and/or
56 * - to simplify key expansion in the 256-bit case by
57 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000058 */
59typedef struct
60{
61 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000062 uint32_t *rk; /*!< AES round keys */
63 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000064}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000066
Paul Bakker5121ce52009-01-03 21:22:43 +000067/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020068 * \brief Initialize AES context
69 *
70 * \param ctx AES context to be initialized
71 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020073
74/**
75 * \brief Clear AES context
76 *
77 * \param ctx AES context to be cleared
78 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020080
81/**
Paul Bakker5121ce52009-01-03 21:22:43 +000082 * \brief AES key schedule (encryption)
83 *
84 * \param ctx AES context to be initialized
85 * \param key encryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020086 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000087 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000089 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020091 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +000092
93/**
94 * \brief AES key schedule (decryption)
95 *
96 * \param ctx AES context to be initialized
97 * \param key decryption key
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +020098 * \param keybits must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000099 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Manuel Pégourié-Gonnardb8186a52015-06-18 14:58:58 +0200103 unsigned int keybits );
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105/**
106 * \brief AES-ECB block encryption/decryption
107 *
108 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 * \param input 16-byte input block
111 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000112 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000113 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000117 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 unsigned char output[16] );
119
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000121/**
122 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000123 * Length should be a multiple of the block
124 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000126 * \note Upon exit, the content of the IV is updated so that you can
127 * call the function same function again on the following
128 * block(s) of data and get the same result as if it was
129 * encrypted in one call. This allows a "streaming" usage.
130 * If on the other hand you need to retain the contents of the
131 * IV, you should either save it manually or use the cipher
132 * module instead.
133 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 * \param length length of the input data
137 * \param iv initialization vector (updated after use)
138 * \param input buffer holding the input data
139 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000140 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200143int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000145 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000147 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000152/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000153 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000155 * Note: Due to the nature of CFB you should use the same key schedule for
156 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000158 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000159 * \note Upon exit, the content of the IV is updated so that you can
160 * call the function same function again on the following
161 * block(s) of data and get the same result as if it was
162 * encrypted in one call. This allows a "streaming" usage.
163 * If on the other hand you need to retain the contents of the
164 * IV, you should either save it manually or use the cipher
165 * module instead.
166 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 * \param length length of the input data
170 * \param iv_off offset in IV (updated after use)
171 * \param iv initialization vector (updated after use)
172 * \param input buffer holding the input data
173 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000174 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000175 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000179 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000180 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000182 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 unsigned char *output );
184
Paul Bakker9a736322012-11-14 12:39:52 +0000185/**
Paul Bakker556efba2014-01-24 15:38:12 +0100186 * \brief AES-CFB8 buffer encryption/decryption.
187 *
188 * Note: Due to the nature of CFB you should use the same key schedule for
189 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakker556efba2014-01-24 15:38:12 +0100191 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000192 * \note Upon exit, the content of the IV is updated so that you can
193 * call the function same function again on the following
194 * block(s) of data and get the same result as if it was
195 * encrypted in one call. This allows a "streaming" usage.
196 * If on the other hand you need to retain the contents of the
197 * IV, you should either save it manually or use the cipher
198 * module instead.
199 *
Paul Bakker556efba2014-01-24 15:38:12 +0100200 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker556efba2014-01-24 15:38:12 +0100202 * \param length length of the input data
203 * \param iv initialization vector (updated after use)
204 * \param input buffer holding the input data
205 * \param output buffer holding the output data
206 *
207 * \return 0 if successful
208 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200209int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100210 int mode,
211 size_t length,
212 unsigned char iv[16],
213 const unsigned char *input,
214 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100218/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000219 * \brief AES-CTR buffer encryption/decryption
220 *
221 * Warning: You have to keep the maximum use of your counter in mind!
222 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000223 * Note: Due to the nature of CTR you should use the same key schedule for
224 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200225 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000226 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200227 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000228 * \param length The length of the data
229 * \param nc_off The offset in the current stream_block (for resuming
230 * within current cipher stream). The offset pointer to
231 * should be 0 at the start of a stream.
232 * \param nonce_counter The 128-bit nonce and counter.
233 * \param stream_block The saved stream-block for resuming. Is overwritten
234 * by the function.
235 * \param input The input data stream
236 * \param output The output data stream
237 *
238 * \return 0 if successful
239 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200240int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000241 size_t length,
242 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000243 unsigned char nonce_counter[16],
244 unsigned char stream_block[16],
245 const unsigned char *input,
246 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200248
Manuel Pégourié-Gonnard31993f22015-05-12 15:41:08 +0200249/**
250 * \brief Internal AES block encryption function
251 * (Only exposed to allow overriding it,
252 * see MBEDTLS_AES_ENCRYPT_ALT)
253 *
254 * \param ctx AES context
255 * \param input Plaintext block
256 * \param output Output (ciphertext) block
257 */
258void mbedtls_aes_encrypt( mbedtls_aes_context *ctx,
259 const unsigned char input[16],
260 unsigned char output[16] );
261
262/**
263 * \brief Internal AES block decryption function
264 * (Only exposed to allow overriding it,
265 * see MBEDTLS_AES_DECRYPT_ALT)
266 *
267 * \param ctx AES context
268 * \param input Ciphertext block
269 * \param output Output (plaintext) block
270 */
271void mbedtls_aes_decrypt( mbedtls_aes_context *ctx,
272 const unsigned char input[16],
273 unsigned char output[16] );
274
Paul Bakker90995b52013-06-24 19:20:35 +0200275#ifdef __cplusplus
276}
277#endif
278
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#else /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200280#include "aes_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200281#endif /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200282
283#ifdef __cplusplus
284extern "C" {
285#endif
286
Paul Bakker5121ce52009-01-03 21:22:43 +0000287/**
288 * \brief Checkup routine
289 *
290 * \return 0 if successful, or 1 if the test failed
291 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200292int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000293
294#ifdef __cplusplus
295}
296#endif
297
298#endif /* aes.h */