blob: 7399995f28cc9804c919c669f9cbb3128b4b2e3a [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 Bakker90995b52013-06-24 19:20:35 +02006 * Copyright (C) 2006-2013, 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
Paul Bakker90995b52013-06-24 19:20:35 +020030#include "config.h"
31
Paul Bakker23986e52011-04-24 08:57:21 +000032#include <string.h>
33
Paul Bakkerfa6a6202013-10-28 18:48:30 +010034#if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
Paul Bakker5c2364c2012-10-01 14:41:15 +000035#include <basetsd.h>
36typedef UINT32 uint32_t;
37#else
38#include <inttypes.h>
39#endif
40
Manuel Pégourié-Gonnard5b685652013-12-18 11:45:21 +010041/* padlock.c and aesni.c rely on these values! */
Paul Bakker5121ce52009-01-03 21:22:43 +000042#define AES_ENCRYPT 1
43#define AES_DECRYPT 0
44
Paul Bakker9d781402011-05-09 16:17:09 +000045#define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
46#define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
Paul Bakker2b222c82009-07-27 21:03:45 +000047
Paul Bakker90995b52013-06-24 19:20:35 +020048#if !defined(POLARSSL_AES_ALT)
49// 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}
70aes_context;
71
Paul Bakker5121ce52009-01-03 21:22:43 +000072/**
73 * \brief AES key schedule (encryption)
74 *
75 * \param ctx AES context to be initialized
76 * \param key encryption key
77 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000078 *
79 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000080 */
Paul Bakker23986e52011-04-24 08:57:21 +000081int aes_setkey_enc( aes_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000082
83/**
84 * \brief AES key schedule (decryption)
85 *
86 * \param ctx AES context to be initialized
87 * \param key decryption key
88 * \param keysize must be 128, 192 or 256
Paul Bakker2b222c82009-07-27 21:03:45 +000089 *
90 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +000091 */
Paul Bakker23986e52011-04-24 08:57:21 +000092int aes_setkey_dec( aes_context *ctx, const unsigned char *key, unsigned int keysize );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94/**
95 * \brief AES-ECB block encryption/decryption
96 *
97 * \param ctx AES context
98 * \param mode AES_ENCRYPT or AES_DECRYPT
99 * \param input 16-byte input block
100 * \param output 16-byte output block
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000101 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000102 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000104int aes_crypt_ecb( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000105 int mode,
Paul Bakkerff60ee62010-03-16 21:09:09 +0000106 const unsigned char input[16],
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 unsigned char output[16] );
108
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200109#if defined(POLARSSL_CIPHER_MODE_CBC)
Paul Bakker5121ce52009-01-03 21:22:43 +0000110/**
111 * \brief AES-CBC buffer encryption/decryption
Paul Bakker4c067eb2009-05-17 10:25:19 +0000112 * Length should be a multiple of the block
113 * size (16 bytes)
Paul Bakker5121ce52009-01-03 21:22:43 +0000114 *
115 * \param ctx AES context
116 * \param mode AES_ENCRYPT or AES_DECRYPT
117 * \param length length of the input data
118 * \param iv initialization vector (updated after use)
119 * \param input buffer holding the input data
120 * \param output buffer holding the output data
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000121 *
122 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000124int aes_crypt_cbc( aes_context *ctx,
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 int mode,
Paul Bakker23986e52011-04-24 08:57:21 +0000126 size_t length,
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000128 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 unsigned char *output );
Manuel Pégourié-Gonnard92cb1d32013-09-13 16:24:20 +0200130#endif /* POLARSSL_CIPHER_MODE_CBC */
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
132/**
Paul Bakker4c067eb2009-05-17 10:25:19 +0000133 * \brief AES-CFB128 buffer encryption/decryption.
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000135 * Note: Due to the nature of CFB you should use the same key schedule for
136 * both encryption and decryption. So a context initialized with
137 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
138 *
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 * \param ctx AES context
140 * \param mode AES_ENCRYPT or AES_DECRYPT
141 * \param length length of the input data
142 * \param iv_off offset in IV (updated after use)
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 *
Paul Bakker27caa8a2010-03-21 15:43:59 +0000147 * \return 0 if successful
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 */
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000149int aes_crypt_cfb128( 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 Bakker1ef71df2011-06-09 14:14:58 +0000152 size_t *iv_off,
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 unsigned char iv[16],
Paul Bakkerff60ee62010-03-16 21:09:09 +0000154 const unsigned char *input,
Paul Bakker5121ce52009-01-03 21:22:43 +0000155 unsigned char *output );
156
Paul Bakker9a736322012-11-14 12:39:52 +0000157/**
Paul Bakker556efba2014-01-24 15:38:12 +0100158 * \brief AES-CFB8 buffer encryption/decryption.
159 *
160 * 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
162 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
163 *
164 * \param ctx AES context
165 * \param mode AES_ENCRYPT or AES_DECRYPT
166 * \param length length of the input data
167 * \param iv initialization vector (updated after use)
168 * \param input buffer holding the input data
169 * \param output buffer holding the output data
170 *
171 * \return 0 if successful
172 */
173int aes_crypt_cfb8( aes_context *ctx,
174 int mode,
175 size_t length,
176 unsigned char iv[16],
177 const unsigned char *input,
178 unsigned char *output );
179
180/**
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000181 * \brief AES-CTR buffer encryption/decryption
182 *
183 * Warning: You have to keep the maximum use of your counter in mind!
184 *
Paul Bakkerca6f3e22011-10-06 13:11:08 +0000185 * Note: Due to the nature of CTR you should use the same key schedule for
186 * both encryption and decryption. So a context initialized with
187 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
188 *
Paul Bakkerdcbfdcc2013-09-10 16:16:50 +0200189 * \param ctx AES context
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000190 * \param length The length of the data
191 * \param nc_off The offset in the current stream_block (for resuming
192 * within current cipher stream). The offset pointer to
193 * should be 0 at the start of a stream.
194 * \param nonce_counter The 128-bit nonce and counter.
195 * \param stream_block The saved stream-block for resuming. Is overwritten
196 * by the function.
197 * \param input The input data stream
198 * \param output The output data stream
199 *
200 * \return 0 if successful
201 */
202int aes_crypt_ctr( aes_context *ctx,
Paul Bakker1ef71df2011-06-09 14:14:58 +0000203 size_t length,
204 size_t *nc_off,
Paul Bakkerb6ecaf52011-04-19 14:29:23 +0000205 unsigned char nonce_counter[16],
206 unsigned char stream_block[16],
207 const unsigned char *input,
208 unsigned char *output );
Paul Bakker90995b52013-06-24 19:20:35 +0200209
210#ifdef __cplusplus
211}
212#endif
213
214#else /* POLARSSL_AES_ALT */
215#include "aes_alt.h"
216#endif /* POLARSSL_AES_ALT */
217
218#ifdef __cplusplus
219extern "C" {
220#endif
221
Paul Bakker5121ce52009-01-03 21:22:43 +0000222/**
223 * \brief Checkup routine
224 *
225 * \return 0 if successful, or 1 if the test failed
226 */
227int aes_self_test( int verbose );
228
229#ifdef __cplusplus
230}
231#endif
232
233#endif /* aes.h */