]>
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 | --------------------------------------------------------------------------- | |
30 | Issue 28/01/2004 | |
31 | ||
32 | This file contains the definitions required to use AES in C. See aesopt.h | |
33 | for optimisation details. | |
34 | */ | |
35 | ||
36 | #if !defined( _AES_H ) | |
37 | #define _AES_H | |
38 | ||
39 | /* This include is used to find 8 & 32 bit unsigned integer types */ | |
40 | #include <machine/limits.h> | |
41 | ||
42 | #if defined(__cplusplus) | |
43 | extern "C" | |
44 | { | |
45 | #endif | |
46 | ||
47 | #define AES_128 /* define if AES with 128 bit keys is needed */ | |
48 | #define AES_192 /* define if AES with 192 bit keys is needed */ | |
49 | #define AES_256 /* define if AES with 256 bit keys is needed */ | |
50 | #define AES_VAR /* define if a variable key size is needed */ | |
51 | ||
52 | /* The following must also be set in assembler files if being used */ | |
53 | ||
54 | #define AES_ENCRYPT /* if support for encryption is needed */ | |
55 | #define AES_DECRYPT /* if support for decryption is needed */ | |
56 | //#define AES_ERR_CHK /* for parameter checks & error return codes */ | |
57 | ||
58 | #if UCHAR_MAX == 0xff /* an unsigned 8 bit type */ | |
59 | typedef unsigned char aes_08t; | |
60 | #else | |
61 | # error Please define aes_08t as an 8-bit unsigned integer type in aes.h | |
62 | #endif | |
63 | ||
64 | #if UINT_MAX == 4294967295 /* an unsigned 32 bit type */ | |
65 | typedef unsigned int aes_32t; | |
66 | #elif ULONG_MAX == 4294967295ul | |
67 | typedef unsigned long aes_32t; | |
68 | #else | |
69 | # error Please define aes_32t as a 32-bit unsigned integer type in aes.h | |
70 | #endif | |
71 | ||
72 | #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */ | |
73 | #define N_COLS 4 /* the number of columns in the state */ | |
74 | ||
75 | /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */ | |
76 | /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */ | |
77 | /* or 44, 52 or 60 32-bit words. For simplicity this code allocates */ | |
78 | /* the maximum 60 word array for the key schedule for all key sizes */ | |
79 | ||
80 | #if defined( AES_VAR ) || defined( AES_256 ) | |
81 | #define KS_LENGTH 60 | |
82 | #elif defined( AES_192 ) | |
83 | #define KS_LENGTH 52 | |
84 | #else | |
85 | #define KS_LENGTH 44 | |
86 | #endif | |
87 | ||
88 | #if defined( AES_ERR_CHK ) | |
89 | #define aes_ret int | |
90 | #define aes_good 0 | |
91 | #define aes_error -1 | |
92 | #else | |
93 | #define aes_ret void | |
94 | #endif | |
95 | ||
96 | #if !defined( AES_DLL ) /* implement normal/DLL functions */ | |
97 | #define aes_rval aes_ret | |
98 | #else | |
99 | #define aes_rval aes_ret __declspec(dllexport) _stdcall | |
100 | #endif | |
101 | ||
102 | typedef struct | |
103 | { aes_32t ks[KS_LENGTH]; | |
104 | aes_32t rn; | |
105 | } aes_encrypt_ctx; | |
106 | ||
107 | typedef struct | |
108 | { aes_32t ks[KS_LENGTH]; | |
109 | aes_32t rn; | |
110 | } aes_decrypt_ctx; | |
111 | ||
112 | typedef struct | |
113 | { | |
114 | aes_decrypt_ctx decrypt; | |
115 | aes_encrypt_ctx encrypt; | |
116 | } aes_ctx; | |
117 | ||
118 | ||
119 | /* This routine must be called before first use if non-static */ | |
120 | /* tables are being used */ | |
121 | ||
122 | void gen_tabs(void); | |
123 | ||
124 | /* The key length (klen) is input in bytes when it is in the range */ | |
125 | /* 16 <= klen <= 32 or in bits when in the range 128 <= klen <= 256 */ | |
126 | ||
127 | #if defined( AES_ENCRYPT ) | |
128 | ||
129 | #if defined(AES_128) || defined(AES_VAR) | |
130 | aes_rval aes_encrypt_key128(const unsigned char *in_key, aes_encrypt_ctx cx[1]); | |
131 | #endif | |
132 | ||
133 | #if defined(AES_192) || defined(AES_VAR) | |
134 | aes_rval aes_encrypt_key192(const unsigned char *in_key, aes_encrypt_ctx cx[1]); | |
135 | #endif | |
136 | ||
137 | #if defined(AES_256) || defined(AES_VAR) | |
138 | aes_rval aes_encrypt_key256(const unsigned char *in_key, aes_encrypt_ctx cx[1]); | |
139 | #endif | |
140 | ||
141 | #if defined(AES_VAR) | |
142 | aes_rval aes_encrypt_key(const unsigned char *in_key, int key_len, aes_encrypt_ctx cx[1]); | |
143 | #endif | |
144 | ||
145 | aes_rval aes_encrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk, | |
146 | unsigned char *out_blk, const aes_encrypt_ctx cx[1]); | |
147 | #endif | |
148 | ||
149 | #if defined( AES_DECRYPT ) | |
150 | ||
151 | #if defined(AES_128) || defined(AES_VAR) | |
152 | aes_rval aes_decrypt_key128(const unsigned char *in_key, aes_decrypt_ctx cx[1]); | |
153 | #endif | |
154 | ||
155 | #if defined(AES_192) || defined(AES_VAR) | |
156 | aes_rval aes_decrypt_key192(const unsigned char *in_key, aes_decrypt_ctx cx[1]); | |
157 | #endif | |
158 | ||
159 | #if defined(AES_256) || defined(AES_VAR) | |
160 | aes_rval aes_decrypt_key256(const unsigned char *in_key, aes_decrypt_ctx cx[1]); | |
161 | #endif | |
162 | ||
163 | #if defined(AES_VAR) | |
164 | aes_rval aes_decrypt_key(const unsigned char *in_key, int key_len, aes_decrypt_ctx cx[1]); | |
165 | #endif | |
166 | ||
167 | aes_rval aes_decrypt_cbc(const unsigned char *in_blk, const unsigned char *in_iv, unsigned int num_blk, | |
168 | unsigned char *out_blk, const aes_decrypt_ctx cx[1]); | |
169 | #endif | |
170 | ||
171 | #if defined(__cplusplus) | |
172 | } | |
173 | #endif | |
174 | ||
175 | #endif |