|
|
1.1 root 1: /*
2: ---------------------------------------------------------------------------
1.1.1.4 root 3: Copyright (c) 1998-2006, Brian Gladman, Worcester, UK. All rights reserved.
1.1 root 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: ---------------------------------------------------------------------------
1.1.1.4 root 30: Issue 09/09/2006
1.1 root 31:
32: This file contains the definitions required to use AES in C. See aesopt.h
33: for optimisation details.
34: */
35:
1.1.1.3 root 36: /* Adapted for TrueCrypt by the TrueCrypt Foundation */
37:
38: #ifndef _AES_H
1.1 root 39: #define _AES_H
40:
1.1.1.5 ! root 41: #include "Common/Tcdefs.h"
! 42:
1.1.1.3 root 43: #ifndef EXIT_SUCCESS
44: #define EXIT_SUCCESS 0
45: #define EXIT_FAILURE 1
1.1.1.2 root 46: #endif
1.1 root 47:
1.1.1.4 root 48: #ifndef RETURN_VALUES
49: # define RETURN_VALUES
50: # if defined( DLL_EXPORT )
51: # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
52: # define VOID_RETURN __declspec( dllexport ) void __stdcall
53: # define INT_RETURN __declspec( dllexport ) int __stdcall
54: # elif defined( __GNUC__ )
55: # define VOID_RETURN __declspec( __dllexport__ ) void
56: # define INT_RETURN __declspec( __dllexport__ ) int
57: # else
58: # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
59: # endif
60: # elif defined( DLL_IMPORT )
61: # if defined( _MSC_VER ) || defined ( __INTEL_COMPILER )
62: # define VOID_RETURN __declspec( dllimport ) void __stdcall
63: # define INT_RETURN __declspec( dllimport ) int __stdcall
64: # elif defined( __GNUC__ )
65: # define VOID_RETURN __declspec( __dllimport__ ) void
66: # define INT_RETURN __declspec( __dllimport__ ) int
67: # else
68: # error Use of the DLL is only available on the Microsoft, Intel and GCC compilers
69: # endif
70: # elif defined( __WATCOMC__ )
71: # define VOID_RETURN void __cdecl
72: # define INT_RETURN int __cdecl
73: # else
74: # define VOID_RETURN void
75: # define INT_RETURN int
76: # endif
77: #endif
78:
79: /* These defines are used to declare buffers in a way that allows
80: faster operations on longer variables to be used. In all these
81: defines 'size' must be a power of 2 and >= 8
82:
83: dec_unit_type(size,x) declares a variable 'x' of length
84: 'size' bits
85:
86: dec_bufr_type(size,bsize,x) declares a buffer 'x' of length 'bsize'
87: bytes defined as an array of variables
88: each of 'size' bits (bsize must be a
89: multiple of size / 8)
90:
91: ptr_cast(x,size) casts a pointer to a pointer to a
92: varaiable of length 'size' bits
93: */
94:
95: #define ui_type(size) uint_##size##t
96: #define dec_unit_type(size,x) typedef ui_type(size) x
97: #define dec_bufr_type(size,bsize,x) typedef ui_type(size) x[bsize / (size >> 3)]
98: #define ptr_cast(x,size) ((ui_type(size)*)(x))
1.1.1.3 root 99:
1.1 root 100: #if defined(__cplusplus)
101: extern "C"
102: {
103: #endif
104:
105: #define AES_128 /* define if AES with 128 bit keys is needed */
106: #define AES_192 /* define if AES with 192 bit keys is needed */
107: #define AES_256 /* define if AES with 256 bit keys is needed */
108: #define AES_VAR /* define if a variable key size is needed */
1.1.1.3 root 109: #define AES_MODES /* define if support is needed for modes */
1.1 root 110:
111: /* The following must also be set in assembler files if being used */
112:
113: #define AES_ENCRYPT /* if support for encryption is needed */
114: #define AES_DECRYPT /* if support for decryption is needed */
115: #define AES_ERR_CHK /* for parameter checks & error return codes */
1.1.1.3 root 116: #define AES_REV_DKS /* define to reverse decryption key schedule */
1.1 root 117:
118: #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
119: #define N_COLS 4 /* the number of columns in the state */
120:
121: /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
122: /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
1.1.1.3 root 123: /* or 44, 52 or 60 32-bit words. */
1.1 root 124:
125: #if defined( AES_VAR ) || defined( AES_256 )
126: #define KS_LENGTH 60
127: #elif defined( AES_192 )
128: #define KS_LENGTH 52
129: #else
130: #define KS_LENGTH 44
131: #endif
132:
133: #if defined( AES_ERR_CHK )
1.1.1.4 root 134: #define AES_RETURN INT_RETURN
1.1 root 135: #else
1.1.1.4 root 136: #define AES_RETURN VOID_RETURN
1.1 root 137: #endif
138:
1.1.1.3 root 139: /* the character array 'inf' in the following structures is used */
140: /* to hold AES context information. This AES code uses cx->inf.b[0] */
141: /* to hold the number of rounds multiplied by 16. The other three */
142: /* elements can be used by code that implements additional modes */
143:
144: typedef union
145: { uint_32t l;
146: uint_8t b[4];
147: } aes_inf;
1.1 root 148:
149: typedef struct
1.1.1.3 root 150: { uint_32t ks[KS_LENGTH];
151: aes_inf inf;
1.1 root 152: } aes_encrypt_ctx;
153:
154: typedef struct
1.1.1.3 root 155: { uint_32t ks[KS_LENGTH];
156: aes_inf inf;
1.1 root 157: } aes_decrypt_ctx;
158:
159: /* This routine must be called before first use if non-static */
160: /* tables are being used */
161:
1.1.1.4 root 162: AES_RETURN gen_tabs(void);
1.1 root 163:
1.1.1.3 root 164: /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
165: /* those in the range 128 <= key_len <= 256 are given in bits */
1.1 root 166:
167: #if defined( AES_ENCRYPT )
168:
169: #if defined(AES_128) || defined(AES_VAR)
1.1.1.4 root 170: AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
1.1 root 171: #endif
172:
173: #if defined(AES_192) || defined(AES_VAR)
1.1.1.4 root 174: AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
1.1 root 175: #endif
176:
177: #if defined(AES_256) || defined(AES_VAR)
1.1.1.4 root 178: AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
1.1 root 179: #endif
180:
181: #if defined(AES_VAR)
1.1.1.4 root 182: AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
1.1 root 183: #endif
184:
1.1.1.4 root 185: AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
1.1.1.3 root 186:
1.1 root 187: #endif
188:
189: #if defined( AES_DECRYPT )
190:
191: #if defined(AES_128) || defined(AES_VAR)
1.1.1.4 root 192: AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
1.1 root 193: #endif
194:
195: #if defined(AES_192) || defined(AES_VAR)
1.1.1.4 root 196: AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
1.1 root 197: #endif
198:
199: #if defined(AES_256) || defined(AES_VAR)
1.1.1.4 root 200: AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
1.1 root 201: #endif
202:
203: #if defined(AES_VAR)
1.1.1.4 root 204: AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
1.1.1.3 root 205: #endif
206:
1.1.1.4 root 207: AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
1.1.1.3 root 208:
1.1 root 209: #endif
210:
1.1.1.3 root 211: #if defined(AES_MODES)
212:
1.1.1.4 root 213: /* Multiple calls to the following subroutines for multiple block */
214: /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
215: /* long messages incremantally provided that the context AND the iv */
216: /* are preserved between all such calls. For the ECB and CBC modes */
217: /* each individual call within a series of incremental calls must */
218: /* process only full blocks (i.e. len must be a multiple of 16) but */
219: /* the CFB, OFB and CTR mode calls can handle multiple incremental */
220: /* calls of any length. Each mode is reset when a new AES key is */
221: /* set but ECB and CBC operations can be reset without setting a */
222: /* new key by setting a new IV value. To reset CFB, OFB and CTR */
223: /* without setting the key, aes_mode_reset() must be called and the */
224: /* IV must be set. NOTE: All these calls update the IV on exit so */
225: /* this has to be reset if a new operation with the same IV as the */
226: /* previous one is required (or decryption follows encryption with */
227: /* the same IV array). */
228:
229: AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 230: int len, const aes_encrypt_ctx cx[1]);
231:
1.1.1.4 root 232: AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 233: int len, const aes_decrypt_ctx cx[1]);
234:
1.1.1.4 root 235: AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 236: int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
237:
1.1.1.4 root 238: AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 239: int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
240:
1.1.1.4 root 241: AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);
1.1.1.3 root 242:
1.1.1.4 root 243: AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 244: int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
245:
1.1.1.4 root 246: AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 247: int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
248:
249: #define aes_ofb_encrypt aes_ofb_crypt
250: #define aes_ofb_decrypt aes_ofb_crypt
251:
1.1.1.4 root 252: AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 253: int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
254:
255: typedef void cbuf_inc(unsigned char *cbuf);
256:
257: #define aes_ctr_encrypt aes_ctr_crypt
258: #define aes_ctr_decrypt aes_ctr_crypt
259:
1.1.1.4 root 260: AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
1.1.1.3 root 261: int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
262:
1.1 root 263: #endif
264:
265: #if defined(__cplusplus)
266: }
267: #endif
268:
269: #endif
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.