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