cryptlib  3.4.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros
aes.h
Go to the documentation of this file.
1 /*
2  ---------------------------------------------------------------------------
3  Copyright (c) 1998-2006, 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 09/09/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 #include <stdlib.h>
40 
41 /* This include is used to find 8 & 32 bit unsigned integer types */
42 #if defined( INC_ALL ) /* pcg */
43  #include "brg_types.h"
44 #else
45  #include "crypt/brg_types.h"
46 #endif /* Compiler-specific includes */
47 
48 #if defined(__cplusplus)
49 extern "C"
50 {
51 #endif
52 
53 #define AES_128 /* define if AES with 128 bit keys is needed */
54 #define AES_192 /* define if AES with 192 bit keys is needed */
55 #define AES_256 /* define if AES with 256 bit keys is needed */
56 #define AES_VAR /* define if a variable key size is needed */
57 #define AES_MODES /* define if support is needed for modes */
58 
59 /* The following must also be set in assembler files if being used */
60 
61 #define AES_ENCRYPT /* if support for encryption is needed */
62 #define AES_DECRYPT /* if support for decryption is needed */
63 #define AES_ERR_CHK /* for parameter checks & error return codes */
64 #define AES_REV_DKS /* define to reverse decryption key schedule */
65 
66 #define AES_BLOCK_SIZE 16 /* the AES block size in bytes */
67 #define N_COLS 4 /* the number of columns in the state */
68 
69 /* The key schedule length is 11, 13 or 15 16-byte blocks for 128, */
70 /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes */
71 /* or 44, 52 or 60 32-bit words. */
72 
73 #if defined( AES_VAR ) || defined( AES_256 )
74 #define KS_LENGTH 60
75 #elif defined( AES_192 )
76 #define KS_LENGTH 52
77 #else
78 #define KS_LENGTH 44
79 #endif
80 
81 #if defined( AES_ERR_CHK )
82 #define AES_RETURN INT_RETURN
83 #else
84 #define AES_RETURN VOID_RETURN
85 #endif
86 
87 /* the character array 'inf' in the following structures is used */
88 /* to hold AES context information. This AES code uses cx->inf.b[0] */
89 /* to hold the number of rounds multiplied by 16. The other three */
90 /* elements can be used by code that implements additional modes */
91 
92 typedef union
93 { uint_32t l;
94  uint_8t b[4];
95 } aes_inf;
96 
97 typedef struct
98 { uint_32t ks[KS_LENGTH];
101 
102 typedef struct
103 { uint_32t ks[KS_LENGTH];
106 
107 /* This routine must be called before first use if non-static */
108 /* tables are being used */
109 
110 AES_RETURN aes_init(void);
111 
112 /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
113 /* those in the range 128 <= key_len <= 256 are given in bits */
114 
115 #if defined( AES_ENCRYPT )
116 
117 #if defined(AES_128) || defined(AES_VAR)
118 AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
119 #endif
120 
121 #if defined(AES_192) || defined(AES_VAR)
122 AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
123 #endif
124 
125 #if defined(AES_256) || defined(AES_VAR)
126 AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
127 #endif
128 
129 #if defined(AES_VAR)
130 AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
131 #endif
132 
133 AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
134 
135 #endif
136 
137 #if defined( AES_DECRYPT )
138 
139 #if defined(AES_128) || defined(AES_VAR)
140 AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
141 #endif
142 
143 #if defined(AES_192) || defined(AES_VAR)
144 AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
145 #endif
146 
147 #if defined(AES_256) || defined(AES_VAR)
148 AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
149 #endif
150 
151 #if defined(AES_VAR)
152 AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
153 #endif
154 
155 AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
156 
157 #endif
158 
159 #if defined(AES_MODES)
160 
161 /* Multiple calls to the following subroutines for multiple block */
162 /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
163 /* long messages incremantally provided that the context AND the iv */
164 /* are preserved between all such calls. For the ECB and CBC modes */
165 /* each individual call within a series of incremental calls must */
166 /* process only full blocks (i.e. len must be a multiple of 16) but */
167 /* the CFB, OFB and CTR mode calls can handle multiple incremental */
168 /* calls of any length. Each mode is reset when a new AES key is */
169 /* set but ECB and CBC operations can be reset without setting a */
170 /* new key by setting a new IV value. To reset CFB, OFB and CTR */
171 /* without setting the key, aes_mode_reset() must be called and the */
172 /* IV must be set. NOTE: All these calls update the IV on exit so */
173 /* this has to be reset if a new operation with the same IV as the */
174 /* previous one is required (or decryption follows encryption with */
175 /* the same IV array). */
176 
178 
179 AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
180  int len, const aes_encrypt_ctx cx[1]);
181 
182 AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
183  int len, const aes_decrypt_ctx cx[1]);
184 
185 AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
186  int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
187 
188 AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
189  int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
190 
192 
193 AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
194  int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
195 
196 AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
197  int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
198 
199 #define aes_ofb_encrypt aes_ofb_crypt
200 #define aes_ofb_decrypt aes_ofb_crypt
201 
202 AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
203  int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
204 
205 typedef void cbuf_inc(unsigned char *cbuf);
206 
207 #define aes_ctr_encrypt aes_ctr_crypt
208 #define aes_ctr_decrypt aes_ctr_crypt
209 
210 AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
211  int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
212 
213 #endif
214 
215 #if defined(__cplusplus)
216 }
217 #endif
218 
219 #endif