]>
Commit | Line | Data |
---|---|---|
13fec989 A |
1 | /* |
2 | --------------------------------------------------------------------------- | |
3 | Copyright (c) 2003, Dr Brian Gladman, Worcester, UK. All rights reserved. | |
4 | ||
5 | LICENSE TERMS | |
6 | ||
7 | The free distribution and use of this software in both source and binary | |
8 | form is allowed (with or without changes) provided that: | |
9 | ||
10 | 1. distributions of this source code include the above copyright | |
11 | notice, this list of conditions and the following disclaimer; | |
12 | ||
13 | 2. distributions in binary form include the above copyright | |
14 | notice, this list of conditions and the following disclaimer | |
15 | in the documentation and/or other associated materials; | |
16 | ||
17 | 3. the copyright holder's name is not used to endorse products | |
18 | built using this software without specific written permission. | |
19 | ||
20 | ALTERNATIVELY, provided that this notice is retained in full, this product | |
21 | may be distributed under the terms of the GNU General Public License (GPL), | |
22 | in which case the provisions of the GPL apply INSTEAD OF those given above. | |
23 | ||
24 | DISCLAIMER | |
25 | ||
26 | This software is provided 'as is' with no explicit or implied warranties | |
27 | in respect of its properties, including, but not limited to, correctness | |
28 | and/or fitness for purpose. | |
29 | --------------------------------------------------------------------------- | |
2d21ac55 | 30 | Issue 31/01/2006 |
13fec989 A |
31 | |
32 | This file contains the definitions required to use AES in C. See aesopt.h | |
33 | for optimisation details. | |
34 | */ | |
35 | ||
2d21ac55 | 36 | #ifndef _AES_H |
13fec989 A |
37 | #define _AES_H |
38 | ||
13fec989 A |
39 | |
40 | #if defined(__cplusplus) | |
41 | extern "C" | |
42 | { | |
43 | #endif | |
44 | ||
45 | #define AES_128 /* define if AES with 128 bit keys is needed */ | |
46 | #define AES_192 /* define if AES with 192 bit keys is needed */ | |
47 | #define AES_256 /* define if AES with 256 bit keys is needed */ | |
48 | #define AES_VAR /* define if a variable key size is needed */ | |
2d21ac55 | 49 | #define AES_MODES /* define if support is needed for modes */ |
13fec989 A |
50 | |
51 | /* The following must also be set in assembler files if being used */ | |
52 | ||
53 | #define AES_ENCRYPT /* if support for encryption is needed */ | |
54 | #define AES_DECRYPT /* if support for decryption is needed */ | |
2d21ac55 A |
55 | #define AES_ERR_CHK /* for parameter checks & error return codes */ |
56 | #define AES_REV_DKS /* define to reverse decryption key schedule */ | |
13fec989 A |
57 | |
58 | #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */ | |
59 | #define N_COLS 4 /* the number of columns in the state */ | |
60 | ||
b0d623f7 | 61 | typedef unsigned int uint_32t; |
2d21ac55 A |
62 | typedef unsigned char uint_8t; |
63 | typedef unsigned short uint_16t; | |
64 | typedef unsigned char aes_08t; | |
b0d623f7 | 65 | typedef unsigned int aes_32t; |
2d21ac55 A |
66 | |
67 | #define void_ret void | |
68 | #define int_ret int | |
69 | ||
13fec989 A |
70 | /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ |
71 | /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ | |
2d21ac55 | 72 | /* or 44, 52 or 60 32-bit words. */ |
13fec989 A |
73 | |
74 | #if defined( AES_VAR ) || defined( AES_256 ) | |
75 | #define KS_LENGTH 60 | |
76 | #elif defined( AES_192 ) | |
77 | #define KS_LENGTH 52 | |
78 | #else | |
79 | #define KS_LENGTH 44 | |
80 | #endif | |
81 | ||
2d21ac55 A |
82 | |
83 | ||
84 | /* the character array 'inf' in the following structures is used */ | |
85 | /* to hold AES context information. This AES code uses cx->inf.b[0] */ | |
86 | /* to hold the number of rounds multiplied by 16. The other three */ | |
87 | /* elements can be used by code that implements additional modes */ | |
88 | ||
89 | #if defined (__i386__) | |
90 | ||
91 | #if defined( AES_ERR_CHK ) | |
92 | #define aes_rval int_ret | |
93 | #else | |
94 | #define aes_rval void_ret | |
95 | #endif | |
96 | ||
97 | typedef union | |
98 | { uint_32t l; | |
99 | uint_8t b[4]; | |
100 | } aes_inf; | |
101 | ||
102 | typedef struct | |
103 | { uint_32t ks[KS_LENGTH]; | |
104 | aes_inf inf; | |
105 | } aes_encrypt_ctx; | |
106 | ||
107 | typedef struct | |
108 | { uint_32t ks[KS_LENGTH]; | |
109 | aes_inf inf; | |
110 | } aes_decrypt_ctx; | |
111 | ||
112 | #else | |
113 | ||
13fec989 A |
114 | #if defined( AES_ERR_CHK ) |
115 | #define aes_ret int | |
116 | #define aes_good 0 | |
117 | #define aes_error -1 | |
118 | #else | |
119 | #define aes_ret void | |
120 | #endif | |
121 | ||
13fec989 | 122 | #define aes_rval aes_ret |
13fec989 A |
123 | |
124 | typedef struct | |
125 | { aes_32t ks[KS_LENGTH]; | |
126 | aes_32t rn; | |
127 | } aes_encrypt_ctx; | |
128 | ||
129 | typedef struct | |
130 | { aes_32t ks[KS_LENGTH]; | |
131 | aes_32t rn; | |
132 | } aes_decrypt_ctx; | |
133 | ||
2d21ac55 A |
134 | #endif |
135 | ||
13fec989 A |
136 | typedef struct |
137 | { | |
138 | aes_decrypt_ctx decrypt; | |
139 | aes_encrypt_ctx encrypt; | |
140 | } aes_ctx; | |
141 | ||
142 | ||
2d21ac55 | 143 | /* implemented in case of wrong call for fixed tables */ |
13fec989 A |
144 | |
145 | void gen_tabs(void); | |
146 | ||
2d21ac55 A |
147 | |
148 | /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */ | |
149 | /* those in the range 128 <= key_len <= 256 are given in bits */ | |
13fec989 A |
150 | |
151 | #if defined( AES_ENCRYPT ) | |
152 | ||
153 | #if defined(AES_128) || defined(AES_VAR) | |
2d21ac55 | 154 | aes_rval aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]); |
13fec989 A |
155 | #endif |
156 | ||
157 | #if defined(AES_192) || defined(AES_VAR) | |
2d21ac55 | 158 | aes_rval aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]); |
13fec989 A |
159 | #endif |
160 | ||
161 | #if defined(AES_256) || defined(AES_VAR) | |
2d21ac55 | 162 | aes_rval aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]); |
13fec989 A |
163 | #endif |
164 | ||
165 | #if defined(AES_VAR) | |
2d21ac55 A |
166 | aes_rval aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]); |
167 | #endif | |
168 | ||
169 | #if defined (__i386__) | |
170 | aes_rval aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]); | |
13fec989 A |
171 | #endif |
172 | ||
173 | aes_rval aes_encrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk, | |
174 | unsigned char *out_blk, const aes_encrypt_ctx cx[1]); | |
2d21ac55 | 175 | |
13fec989 A |
176 | #endif |
177 | ||
178 | #if defined( AES_DECRYPT ) | |
179 | ||
180 | #if defined(AES_128) || defined(AES_VAR) | |
2d21ac55 | 181 | aes_rval aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]); |
13fec989 A |
182 | #endif |
183 | ||
184 | #if defined(AES_192) || defined(AES_VAR) | |
2d21ac55 | 185 | aes_rval aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]); |
13fec989 A |
186 | #endif |
187 | ||
188 | #if defined(AES_256) || defined(AES_VAR) | |
2d21ac55 | 189 | aes_rval aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]); |
13fec989 A |
190 | #endif |
191 | ||
192 | #if defined(AES_VAR) | |
2d21ac55 A |
193 | aes_rval aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]); |
194 | #endif | |
195 | ||
196 | #if defined (__i386__) | |
197 | aes_rval aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]); | |
13fec989 A |
198 | #endif |
199 | ||
200 | aes_rval aes_decrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk, | |
201 | unsigned char *out_blk, const aes_decrypt_ctx cx[1]); | |
2d21ac55 A |
202 | |
203 | ||
13fec989 A |
204 | #endif |
205 | ||
2d21ac55 | 206 | |
13fec989 A |
207 | #if defined(__cplusplus) |
208 | } | |
209 | #endif | |
210 | ||
211 | #endif |