]> cvs.zerfleddert.de Git - proxmark3-svn/blob - common/polarssl/aes.h
299cb4cdfe3d02b89641eceea2404e78e22e8585
[proxmark3-svn] / common / polarssl / aes.h
1 /**
2 * \file aes.h
3 *
4 * \brief AES block cipher
5 *
6 * Copyright (C) 2006-2014, Brainspark B.V.
7 *
8 * This file is part of PolarSSL (http://www.polarssl.org)
9 * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
10 *
11 * All rights reserved.
12 *
13 * 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.
26 */
27 #ifndef POLARSSL_AES_H
28 #define POLARSSL_AES_H
29
30 #if !defined(POLARSSL_CONFIG_FILE)
31 #include "polarssl_config.h"
32 #else
33 #include POLARSSL_CONFIG_FILE
34 #endif
35
36 #include <string.h>
37
38 #if defined(_MSC_VER) && !defined(EFIX64) && !defined(EFI32)
39 #include <basetsd.h>
40 typedef UINT32 uint32_t;
41 #else
42 #include <inttypes.h>
43 #endif
44
45 /* padlock.c and aesni.c rely on these values! */
46 #define AES_ENCRYPT 1
47 #define AES_DECRYPT 0
48
49 #define POLARSSL_ERR_AES_INVALID_KEY_LENGTH -0x0020 /**< Invalid key length. */
50 #define POLARSSL_ERR_AES_INVALID_INPUT_LENGTH -0x0022 /**< Invalid data input length. */
51
52 #if !defined(POLARSSL_AES_ALT)
53 // Regular implementation
54 //
55
56 #ifdef __cplusplus
57 extern "C" {
58 #endif
59
60 /**
61 * \brief AES context structure
62 *
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
67 */
68 typedef struct
69 {
70 int nr; /*!< number of rounds */
71 uint32_t *rk; /*!< AES round keys */
72 uint32_t buf[68]; /*!< unaligned data */
73 }
74 aes_context;
75
76 /**
77 * \brief Initialize AES context
78 *
79 * \param ctx AES context to be initialized
80 */
81 void aes_init( aes_context *ctx );
82
83 /**
84 * \brief Clear AES context
85 *
86 * \param ctx AES context to be cleared
87 */
88 void aes_free( aes_context *ctx );
89
90 /**
91 * \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
96 *
97 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
98 */
99 int aes_setkey_enc( aes_context *ctx, const unsigned char *key,
100 unsigned int keysize );
101
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
108 *
109 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_KEY_LENGTH
110 */
111 int aes_setkey_dec( aes_context *ctx, const unsigned char *key,
112 unsigned int keysize );
113
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
121 *
122 * \return 0 if successful
123 */
124 int aes_crypt_ecb( aes_context *ctx,
125 int mode,
126 const unsigned char input[16],
127 unsigned char output[16] );
128 #if defined(POLARSSL_CIPHER_MODE_CBC)
129 /**
130 * \brief AES-CBC buffer encryption/decryption
131 * Length should be a multiple of the block
132 * size (16 bytes)
133 *
134 * \param ctx AES context
135 * \param mode AES_ENCRYPT or AES_DECRYPT
136 * \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
140 *
141 * \return 0 if successful, or POLARSSL_ERR_AES_INVALID_INPUT_LENGTH
142 */
143 int aes_crypt_cbc( aes_context *ctx,
144 int mode,
145 size_t length,
146 unsigned char iv[16],
147 const unsigned char *input,
148 unsigned char *output );
149 #endif /* POLARSSL_CIPHER_MODE_CBC */
150
151 #if defined(POLARSSL_CIPHER_MODE_CFB)
152 /**
153 * \brief AES-CFB128 buffer encryption/decryption.
154 *
155 * 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
157 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
158 *
159 * \param ctx AES context
160 * \param mode AES_ENCRYPT or AES_DECRYPT
161 * \param length length of the input data
162 * \param iv_off offset in IV (updated after use)
163 * \param iv initialization vector (updated after use)
164 * \param input buffer holding the input data
165 * \param output buffer holding the output data
166 *
167 * \return 0 if successful
168 */
169 int aes_crypt_cfb128( aes_context *ctx,
170 int mode,
171 size_t length,
172 size_t *iv_off,
173 unsigned char iv[16],
174 const unsigned char *input,
175 unsigned char *output );
176
177 /**
178 * \brief AES-CFB8 buffer encryption/decryption.
179 *
180 * Note: Due to the nature of CFB you should use the same key schedule for
181 * both encryption and decryption. So a context initialized with
182 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
183 *
184 * \param ctx AES context
185 * \param mode AES_ENCRYPT or AES_DECRYPT
186 * \param length length of the input data
187 * \param iv initialization vector (updated after use)
188 * \param input buffer holding the input data
189 * \param output buffer holding the output data
190 *
191 * \return 0 if successful
192 */
193 int aes_crypt_cfb8( aes_context *ctx,
194 int mode,
195 size_t length,
196 unsigned char iv[16],
197 const unsigned char *input,
198 unsigned char *output );
199 #endif /*POLARSSL_CIPHER_MODE_CFB */
200
201 #if defined(POLARSSL_CIPHER_MODE_CTR)
202 /**
203 * \brief AES-CTR buffer encryption/decryption
204 *
205 * Warning: You have to keep the maximum use of your counter in mind!
206 *
207 * Note: Due to the nature of CTR you should use the same key schedule for
208 * both encryption and decryption. So a context initialized with
209 * aes_setkey_enc() for both AES_ENCRYPT and AES_DECRYPT.
210 *
211 * \param ctx AES context
212 * \param length The length of the data
213 * \param nc_off The offset in the current stream_block (for resuming
214 * within current cipher stream). The offset pointer to
215 * should be 0 at the start of a stream.
216 * \param nonce_counter The 128-bit nonce and counter.
217 * \param stream_block The saved stream-block for resuming. Is overwritten
218 * by the function.
219 * \param input The input data stream
220 * \param output The output data stream
221 *
222 * \return 0 if successful
223 */
224 int aes_crypt_ctr( aes_context *ctx,
225 size_t length,
226 size_t *nc_off,
227 unsigned char nonce_counter[16],
228 unsigned char stream_block[16],
229 const unsigned char *input,
230 unsigned char *output );
231 #endif /* POLARSSL_CIPHER_MODE_CTR */
232
233 #ifdef __cplusplus
234 }
235 #endif
236
237 #else /* POLARSSL_AES_ALT */
238 #include "aes_alt.h"
239 #endif /* POLARSSL_AES_ALT */
240
241 #ifdef __cplusplus
242 extern "C" {
243 #endif
244
245 /**
246 * \brief Checkup routine
247 *
248 * \return 0 if successful, or 1 if the test failed
249 */
250 int aes_self_test( int verbose );
251
252 #ifdef __cplusplus
253 }
254 #endif
255
256 #endif /* aes.h */
Impressum, Datenschutz