blob: a773c75e991567e62fecff55dd8e0cf385429a9c [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>
Paul Bakker23986e52011-04-24 08:57:21 +000034
Paul Bakkerfa6a6202013-10-28 18:48:30 +010035#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000036#include <basetsd.h>
37typedef UINT32 uint32_t;
38#else
39#include <inttypes.h>
40#endif
41
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010042/* padlock.c and aesni.c rely on these values! */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043#define MBEDTLS_AES_ENCRYPT 1
44#define MBEDTLS_AES_DECRYPT 0
Paul Bakker5121ce52009-01-03 21:22:43 +000045
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046#define MBEDTLS_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
47#define MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_AES_ALT)
Paul Bakker90995b52013-06-24 19:20:35 +020050// Regular implementation
51//
52
Paul Bakker407a0da2013-06-27 14:29:21 +020053#ifdef __cplusplus
54extern "C" {
55#endif
56
Paul Bakker5121ce52009-01-03 21:22:43 +000057/**
58 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010059 *
60 * \note buf is able to hold 32 extra bytes, which can be used:
61 * - for alignment purposes if VIA padlock is used, and/or
62 * - to simplify key expansion in the 256-bit case by
63 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000064 */
65typedef struct
66{
67 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000068 uint32_t *rk; /*!< AES round keys */
69 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000070}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020071mbedtls_aes_context;
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker5121ce52009-01-03 21:22:43 +000073/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020074 * \brief Initialize AES context
75 *
76 * \param ctx AES context to be initialized
77 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078void mbedtls_aes_init( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020079
80/**
81 * \brief Clear AES context
82 *
83 * \param ctx AES context to be cleared
84 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085void mbedtls_aes_free( mbedtls_aes_context *ctx );
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020086
87/**
Paul Bakker5121ce52009-01-03 21:22:43 +000088 * \brief AES key schedule (encryption)
89 *
90 * \param ctx AES context to be initialized
91 * \param key encryption key
92 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000093 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000095 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096int mbedtls_aes_setkey_enc( mbedtls_aes_context *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020097 unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000098
99/**
100 * \brief AES key schedule (decryption)
101 *
102 * \param ctx AES context to be initialized
103 * \param key decryption key
104 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +0000105 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200108int mbedtls_aes_setkey_dec( mbedtls_aes_context *ctx, const unsigned char *key,
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200109 unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
111/**
112 * \brief AES-ECB block encryption/decryption
113 *
114 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 * \param input 16-byte input block
117 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000118 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000119 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000120 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200121int mbedtls_aes_crypt_ecb( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000122 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000123 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 unsigned char output[16] );
125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000127/**
128 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000129 * Length should be a multiple of the block
130 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000132 * \note Upon exit, the content of the IV is updated so that you can
133 * call the function same function again on the following
134 * block(s) of data and get the same result as if it was
135 * encrypted in one call. This allows a "streaming" usage.
136 * If on the other hand you need to retain the contents of the
137 * IV, you should either save it manually or use the cipher
138 * module instead.
139 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 * \param length length of the input data
143 * \param iv initialization vector (updated after use)
144 * \param input buffer holding the input data
145 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000146 *
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 * \return 0 if successful, or MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149int mbedtls_aes_crypt_cbc( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000150 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000151 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000153 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000154 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200155#endif /* MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157#if defined(MBEDTLS_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000158/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000159 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000161 * Note: Due to the nature of CFB you should use the same key schedule for
162 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000164 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000165 * \note Upon exit, the content of the IV is updated so that you can
166 * call the function same function again on the following
167 * block(s) of data and get the same result as if it was
168 * encrypted in one call. This allows a "streaming" usage.
169 * If on the other hand you need to retain the contents of the
170 * IV, you should either save it manually or use the cipher
171 * module instead.
172 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker5121ce52009-01-03 21:22:43 +0000175 * \param length length of the input data
176 * \param iv_off offset in IV (updated after use)
177 * \param iv initialization vector (updated after use)
178 * \param input buffer holding the input data
179 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000180 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000181 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183int mbedtls_aes_crypt_cfb128( mbedtls_aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000185 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000186 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000187 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000188 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000189 unsigned char *output );
190
Paul Bakker9a736322012-11-14 12:39:52 +0000191/**
Paul Bakker556efba2014-01-24 15:38:12 +0100192 * \brief AES-CFB8 buffer encryption/decryption.
193 *
194 * Note: Due to the nature of CFB you should use the same key schedule for
195 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakker556efba2014-01-24 15:38:12 +0100197 *
Manuel Pégourié-Gonnard2be147a2015-01-23 16:19:47 +0000198 * \note Upon exit, the content of the IV is updated so that you can
199 * call the function same function again on the following
200 * block(s) of data and get the same result as if it was
201 * encrypted in one call. This allows a "streaming" usage.
202 * If on the other hand you need to retain the contents of the
203 * IV, you should either save it manually or use the cipher
204 * module instead.
205 *
Paul Bakker556efba2014-01-24 15:38:12 +0100206 * \param ctx AES context
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 * \param mode MBEDTLS_AES_ENCRYPT or MBEDTLS_AES_DECRYPT
Paul Bakker556efba2014-01-24 15:38:12 +0100208 * \param length length of the input data
209 * \param iv initialization vector (updated after use)
210 * \param input buffer holding the input data
211 * \param output buffer holding the output data
212 *
213 * \return 0 if successful
214 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200215int mbedtls_aes_crypt_cfb8( mbedtls_aes_context *ctx,
Paul Bakker556efba2014-01-24 15:38:12 +0100216 int mode,
217 size_t length,
218 unsigned char iv[16],
219 const unsigned char *input,
220 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221#endif /*MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100222
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200223#if defined(MBEDTLS_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100224/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000225 * \brief AES-CTR buffer encryption/decryption
226 *
227 * Warning: You have to keep the maximum use of your counter in mind!
228 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000229 * Note: Due to the nature of CTR you should use the same key schedule for
230 * both encryption and decryption. So a context initialized with
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 * mbedtls_aes_setkey_enc() for both MBEDTLS_AES_ENCRYPT and MBEDTLS_AES_DECRYPT.
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000232 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200233 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000234 * \param length The length of the data
235 * \param nc_off The offset in the current stream_block (for resuming
236 * within current cipher stream). The offset pointer to
237 * should be 0 at the start of a stream.
238 * \param nonce_counter The 128-bit nonce and counter.
239 * \param stream_block The saved stream-block for resuming. Is overwritten
240 * by the function.
241 * \param input The input data stream
242 * \param output The output data stream
243 *
244 * \return 0 if successful
245 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200246int mbedtls_aes_crypt_ctr( mbedtls_aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000247 size_t length,
248 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000249 unsigned char nonce_counter[16],
250 unsigned char stream_block[16],
251 const unsigned char *input,
252 unsigned char *output );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200253#endif /* MBEDTLS_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200254
255#ifdef __cplusplus
256}
257#endif
258
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259#else /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200260#include "aes_alt.h"
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200261#endif /* MBEDTLS_AES_ALT */
Paul Bakker90995b52013-06-24 19:20:35 +0200262
263#ifdef __cplusplus
264extern "C" {
265#endif
266
Paul Bakker5121ce52009-01-03 21:22:43 +0000267/**
268 * \brief Checkup routine
269 *
270 * \return 0 if successful, or 1 if the test failed
271 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272int mbedtls_aes_self_test( int verbose );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273
274#ifdef __cplusplus
275}
276#endif
277
278#endif /* aes.h */