]> git.saurik.com Git - apple/xnu.git/blob - bsd/crypto/aes/aes.h
49c845da6ef406335b95682e3f01bc89a3619e67
[apple/xnu.git] / bsd / crypto / aes / aes.h
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 ---------------------------------------------------------------------------
30 Issue 31/01/2006
31
32 This file contains the definitions required to use AES in C. See aesopt.h
33 for optimisation details.
34 */
35
36 #ifndef _AES_H
37 #define _AES_H
38
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 */
49 #define AES_MODES /* define if support is needed for modes */
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 */
55 #define AES_ERR_CHK /* for parameter checks & error return codes */
56 #define AES_REV_DKS /* define to reverse decryption key schedule */
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
61 typedef unsigned int uint_32t;
62 typedef unsigned char uint_8t;
63 typedef unsigned short uint_16t;
64 typedef unsigned char aes_08t;
65 typedef unsigned int aes_32t;
66
67 #define void_ret void
68 #define int_ret int
69
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 */
72 /* or 44, 52 or 60 32-bit words. */
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
82
83 #if 0 // defined (__i386__) || defined (__x86_64__)
84
85 /*
86 looks like no other code for (i386/x86_64) is using the following definitions any more.
87 I comment this out, so the C code in the directory gen/ can be used to compile for test/development purpose.
88 Note : this is not going to change anything in the i386/x86_64 kernel.
89 (source code in i386/, mostly in assembly, does not reference to this header file.)
90
91 cclee 10-20-2010
92 */
93
94 /* the character array 'inf' in the following structures is used */
95 /* to hold AES context information. This AES code uses cx->inf.b[0] */
96 /* to hold the number of rounds multiplied by 16. The other three */
97 /* elements can be used by code that implements additional modes */
98
99 #if defined( AES_ERR_CHK )
100 #define aes_rval int_ret
101 #else
102 #define aes_rval void_ret
103 #endif
104
105 typedef union
106 { uint_32t l;
107 uint_8t b[4];
108 } aes_inf;
109
110 typedef struct
111 { uint_32t ks[KS_LENGTH];
112 aes_inf inf;
113 } aes_encrypt_ctx;
114
115 typedef struct
116 { uint_32t ks[KS_LENGTH];
117 aes_inf inf;
118 } aes_decrypt_ctx;
119
120 #else
121
122 #if defined( AES_ERR_CHK )
123 #define aes_ret int
124 #define aes_good 0
125 #define aes_error -1
126 #else
127 #define aes_ret void
128 #endif
129
130 #define aes_rval aes_ret
131
132 typedef struct
133 { aes_32t ks[KS_LENGTH];
134 aes_32t rn;
135 } aes_encrypt_ctx;
136
137 typedef struct
138 { aes_32t ks[KS_LENGTH];
139 aes_32t rn;
140 } aes_decrypt_ctx;
141
142 #endif
143
144 typedef struct
145 {
146 aes_decrypt_ctx decrypt;
147 aes_encrypt_ctx encrypt;
148 } aes_ctx;
149
150
151 /* implemented in case of wrong call for fixed tables */
152
153 void gen_tabs(void);
154
155
156 /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
157 /* those in the range 128 <= key_len <= 256 are given in bits */
158
159 #if defined( AES_ENCRYPT )
160
161 #if defined(AES_128) || defined(AES_VAR)
162 aes_rval aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
163 #endif
164
165 #if defined(AES_192) || defined(AES_VAR)
166 aes_rval aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
167 #endif
168
169 #if defined(AES_256) || defined(AES_VAR)
170 aes_rval aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
171 #endif
172
173 #if defined(AES_VAR)
174 aes_rval aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
175 #endif
176
177 #if defined (__i386__) || defined (__x86_64__)
178 aes_rval aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
179 #endif
180
181 aes_rval aes_encrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk,
182 unsigned char *out_blk, const aes_encrypt_ctx cx[1]);
183
184 #endif
185
186 #if defined( AES_DECRYPT )
187
188 #if defined(AES_128) || defined(AES_VAR)
189 aes_rval aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
190 #endif
191
192 #if defined(AES_192) || defined(AES_VAR)
193 aes_rval aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
194 #endif
195
196 #if defined(AES_256) || defined(AES_VAR)
197 aes_rval aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
198 #endif
199
200 #if defined(AES_VAR)
201 aes_rval aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
202 #endif
203
204 #if defined (__i386__) || defined (__x86_64__)
205 aes_rval aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
206 #endif
207
208 aes_rval aes_decrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk,
209 unsigned char *out_blk, const aes_decrypt_ctx cx[1]);
210
211
212 #endif
213
214
215 #if defined(__cplusplus)
216 }
217 #endif
218
219 #endif