blob: 2e9092f95ce63ba41acafc64c81533f77ed37c1a [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 *
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +02006 * Copyright (C) 2006-2014, Brainspark B.V.
Paul Bakkerb96f1542010-07-18 20:36:00 +00007 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
Paul Bakker84f12b72010-07-18 10:13:04 +00009 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
Paul Bakkerb96f1542010-07-18 20:36:00 +000010 *
Paul Bakker77b385e2009-07-28 17:23:11 +000011 * All rights reserved.
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000012 *
Paul Bakkere0ccd0a2009-01-04 16:27:10 +000013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Paul Bakker5121ce52009-01-03 21:22:43 +000026 */
Paul Bakker40e46942009-01-03 21:51:57 +000027#ifndef POLARSSL_AES_H
28#define POLARSSL_AES_H
Paul Bakker5121ce52009-01-03 21:22:43 +000029
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#if !defined(POLARSSL_CONFIG_FILE)
Paul Bakker90995b52013-06-24 19:20:35 +020031#include "config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020032#else
33#include POLARSSL_CONFIG_FILE
34#endif
Paul Bakker90995b52013-06-24 19:20:35 +020035
Paul Bakker23986e52011-04-24 08:57:21 +000036#include <string.h>
37
Paul Bakkerfa6a6202013-10-28 18:48:30 +010038#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000039#include <basetsd.h>
40typedef UINT32 uint32_t;
41#else
42#include <inttypes.h>
43#endif
44
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010045/* padlock.c and aesni.c rely on these values! */
Paul Bakker5121ce52009-01-03 21:22:43 +000046#define AES_ENCRYPT 1
47#define AES_DECRYPT 0
48
Paul Bakker9d781402011-05-09 16:17:09 +000049#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
50#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000051
Paul Bakker90995b52013-06-24 19:20:35 +020052#if !defined(POLARSSL_AES_ALT)
53// Regular implementation
54//
55
Paul Bakker407a0da2013-06-27 14:29:21 +020056#ifdef __cplusplus
57extern "C" {
58#endif
59
Paul Bakker5121ce52009-01-03 21:22:43 +000060/**
61 * \brief AES context structure
Manuel Pégourié-Gonnard4a5b9952013-12-29 13:50:32 +010062 *
63 * \note buf is able to hold 32 extra bytes, which can be used:
64 * - for alignment purposes if VIA padlock is used, and/or
65 * - to simplify key expansion in the 256-bit case by
66 * generating an extra round key
Paul Bakker5121ce52009-01-03 21:22:43 +000067 */
68typedef struct
69{
70 int nr; /*!< number of rounds */
Paul Bakker5c2364c2012-10-01 14:41:15 +000071 uint32_t *rk; /*!< AES round keys */
72 uint32_t buf[68]; /*!< unaligned data */
Paul Bakker5121ce52009-01-03 21:22:43 +000073}
74aes_context;
75
Paul Bakker5121ce52009-01-03 21:22:43 +000076/**
Paul Bakkerc7ea99a2014-06-18 11:12:03 +020077 * \brief Initialize AES context
78 *
79 * \param ctx AES context to be initialized
80 */
81void aes_init( aes_context *ctx );
82
83/**
84 * \brief Clear AES context
85 *
86 * \param ctx AES context to be cleared
87 */
88void aes_free( aes_context *ctx );
89
90/**
Paul Bakker5121ce52009-01-03 21:22:43 +000091 * \brief AES key schedule (encryption)
92 *
93 * \param ctx AES context to be initialized
94 * \param key encryption key
95 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000096 *
97 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000098 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +020099int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
100 unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
102/**
103 * \brief AES key schedule (decryption)
104 *
105 * \param ctx AES context to be initialized
106 * \param key decryption key
107 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +0000108 *
109 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 */
Paul Bakkerb9e4e2c2014-05-01 14:18:25 +0200111int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
112 unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113
114/**
115 * \brief AES-ECB block encryption/decryption
116 *
117 * \param ctx AES context
118 * \param mode AES_ENCRYPT or AES_DECRYPT
119 * \param input 16-byte input block
120 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000121 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000122 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000124int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000126 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 unsigned char output[16] );
128
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200129#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000130/**
131 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000132 * Length should be a multiple of the block
133 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
135 * \param ctx AES context
136 * \param mode AES_ENCRYPT or AES_DECRYPT
137 * \param length length of the input data
138 * \param iv initialization vector (updated after use)
139 * \param input buffer holding the input data
140 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000141 *
142 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000143 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000144int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000146 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000148 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200150#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Manuel Pégourié-Gonnard1ec220b2014-03-10 11:20:17 +0100152#if defined(POLARSSL_CIPHER_MODE_CFB)
Paul Bakker5121ce52009-01-03 21:22:43 +0000153/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000154 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000156 * Note: Due to the nature of CFB you should use the same key schedule for
157 * both encryption and decryption. So a context initialized with
158 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
159 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 * \param ctx AES context
161 * \param mode AES_ENCRYPT or AES_DECRYPT
162 * \param length length of the input data
163 * \param iv_off offset in IV (updated after use)
164 * \param iv initialization vector (updated after use)
165 * \param input buffer holding the input data
166 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000167 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000168 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000170int aes_crypt_cfb128( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000171 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000172 size_t length,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000173 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000175 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 unsigned char *output );
177
Paul Bakker9a736322012-11-14 12:39:52 +0000178/**
Paul Bakker556efba2014-01-24 15:38:12 +0100179 * \brief AES-CFB8 buffer encryption/decryption.
180 *
181 * Note: Due to the nature of CFB you should use the same key schedule for
182 * both encryption and decryption. So a context initialized with
183 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
184 *
185 * \param ctx AES context
186 * \param mode AES_ENCRYPT or AES_DECRYPT
187 * \param length length of the input data
188 * \param iv initialization vector (updated after use)
189 * \param input buffer holding the input data
190 * \param output buffer holding the output data
191 *
192 * \return 0 if successful
193 */
194int aes_crypt_cfb8( aes_context *ctx,
195 int mode,
196 size_t length,
197 unsigned char iv[16],
198 const unsigned char *input,
199 unsigned char *output );
Manuel Pégourié-Gonnard1ec220b2014-03-10 11:20:17 +0100200#endif /*POLARSSL_CIPHER_MODE_CFB */
Paul Bakker556efba2014-01-24 15:38:12 +0100201
Manuel Pégourié-Gonnard1ec220b2014-03-10 11:20:17 +0100202#if defined(POLARSSL_CIPHER_MODE_CTR)
Paul Bakker556efba2014-01-24 15:38:12 +0100203/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000204 * \brief AES-CTR buffer encryption/decryption
205 *
206 * Warning: You have to keep the maximum use of your counter in mind!
207 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000208 * Note: Due to the nature of CTR you should use the same key schedule for
209 * both encryption and decryption. So a context initialized with
210 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
211 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200212 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000213 * \param length The length of the data
214 * \param nc_off The offset in the current stream_block (for resuming
215 * within current cipher stream). The offset pointer to
216 * should be 0 at the start of a stream.
217 * \param nonce_counter The 128-bit nonce and counter.
218 * \param stream_block The saved stream-block for resuming. Is overwritten
219 * by the function.
220 * \param input The input data stream
221 * \param output The output data stream
222 *
223 * \return 0 if successful
224 */
225int aes_crypt_ctr( aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000226 size_t length,
227 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000228 unsigned char nonce_counter[16],
229 unsigned char stream_block[16],
230 const unsigned char *input,
231 unsigned char *output );
Manuel Pégourié-Gonnard1ec220b2014-03-10 11:20:17 +0100232#endif /* POLARSSL_CIPHER_MODE_CTR */
Paul Bakker90995b52013-06-24 19:20:35 +0200233
234#ifdef __cplusplus
235}
236#endif
237
238#else /* POLARSSL_AES_ALT */
239#include "aes_alt.h"
240#endif /* POLARSSL_AES_ALT */
241
242#ifdef __cplusplus
243extern "C" {
244#endif
245
Paul Bakker5121ce52009-01-03 21:22:43 +0000246/**
247 * \brief Checkup routine
248 *
249 * \return 0 if successful, or 1 if the test failed
250 */
251int aes_self_test( int verbose );
252
253#ifdef __cplusplus
254}
255#endif
256
257#endif /* aes.h */