1 /*
2 ---------------------------------------------------------------------------
3 Copyright (c) 1998-2013, Brian Gladman, Worcester, UK. All rights reserved.
4 
5 The redistribution and use of this software (with or without changes)
6 is allowed without the payment of fees or royalties provided that:
7 
8   source code distributions include the above copyright notice, this
9   list of conditions and the following disclaimer;
10 
11   binary distributions include the above copyright notice, this list
12   of conditions and the following disclaimer in their documentation.
13 
14 This software is provided 'as is' with no explicit or implied warranties
15 in respect of its operation, including, but not limited to, correctness
16 and fitness for purpose.
17 ---------------------------------------------------------------------------
18 Issue Date: 20/12/2007
19 
20  This file contains the definitions required to use AES in C. See aesopt.h
21  for optimisation details.
22 */
23 
24 #ifndef _AES_H
25 #define _AES_H
26 
27 #include <stdlib.h>
28 
29 /*  This include is used to find 8 & 32 bit unsigned integer types  */
30 #include "brg_types.h"
31 
32 #if defined(__cplusplus)
33 extern "C"
34 {
35 #endif
36 
37 #define AES_128     /* if a fast 128 bit key scheduler is needed    */
38 #define AES_192     /* if a fast 192 bit key scheduler is needed    */
39 #define AES_256     /* if a fast 256 bit key scheduler is needed    */
40 #define AES_VAR     /* if variable key size scheduler is needed     */
41 #define AES_MODES   /* if support is needed for modes               */
42 
43 /* The following must also be set in assembler files if being used  */
44 
45 #define AES_ENCRYPT /* if support for encryption is needed          */
46 #define AES_DECRYPT /* if support for decryption is needed          */
47 
48 #define AES_BLOCK_SIZE  16  /* the AES block size in bytes          */
49 #define N_COLS           4  /* the number of columns in the state   */
50 
51 /* The key schedule length is 11, 13 or 15 16-byte blocks for 128,  */
52 /* 192 or 256-bit keys respectively. That is 176, 208 or 240 bytes  */
53 /* or 44, 52 or 60 32-bit words.                                    */
54 
55 #if defined( AES_VAR ) || defined( AES_256 )
56 #define KS_LENGTH       60
57 #elif defined( AES_192 )
58 #define KS_LENGTH       52
59 #else
60 #define KS_LENGTH       44
61 #endif
62 
63 #define AES_RETURN INT_RETURN
64 
65 /* the character array 'inf' in the following structures is used    */
66 /* to hold AES context information. This AES code uses cx->inf.b[0] */
67 /* to hold the number of rounds multiplied by 16. The other three   */
68 /* elements can be used by code that implements additional modes    */
69 
70 typedef union
71 {   uint32_t l;
72     uint8_t b[4];
73 } aes_inf;
74 
75 #ifdef _MSC_VER
76 #pragma warning( disable : 4324 )
77 #endif
78 
79 #ifdef _WIN64
80 __declspec(align(16))
81 #endif
82 typedef struct
83 {   uint32_t ks[KS_LENGTH];
84     aes_inf inf;
85 } aes_encrypt_ctx;
86 
87 #ifdef _WIN64
88 __declspec(align(16))
89 #endif
90 typedef struct
91 {   uint32_t ks[KS_LENGTH];
92     aes_inf inf;
93 } aes_decrypt_ctx;
94 
95 #ifdef _MSC_VER
96 #pragma warning( default : 4324 )
97 #endif
98 
99 /* This routine must be called before first use if non-static       */
100 /* tables are being used                                            */
101 
102 AES_RETURN aes_init(void);
103 
104 /* Key lengths in the range 16 <= key_len <= 32 are given in bytes, */
105 /* those in the range 128 <= key_len <= 256 are given in bits       */
106 
107 #if defined( AES_ENCRYPT )
108 
109 #if defined( AES_128 ) || defined( AES_VAR)
110 AES_RETURN aes_encrypt_key128(const unsigned char *key, aes_encrypt_ctx cx[1]);
111 #endif
112 
113 #if defined( AES_192 ) || defined( AES_VAR)
114 AES_RETURN aes_encrypt_key192(const unsigned char *key, aes_encrypt_ctx cx[1]);
115 #endif
116 
117 #if defined( AES_256 ) || defined( AES_VAR)
118 AES_RETURN aes_encrypt_key256(const unsigned char *key, aes_encrypt_ctx cx[1]);
119 #endif
120 
121 #if defined( AES_VAR )
122 AES_RETURN aes_encrypt_key(const unsigned char *key, int key_len, aes_encrypt_ctx cx[1]);
123 #endif
124 
125 AES_RETURN aes_encrypt(const unsigned char *in, unsigned char *out, const aes_encrypt_ctx cx[1]);
126 
127 #endif
128 
129 #if defined( AES_DECRYPT )
130 
131 #if defined( AES_128 ) || defined( AES_VAR)
132 AES_RETURN aes_decrypt_key128(const unsigned char *key, aes_decrypt_ctx cx[1]);
133 #endif
134 
135 #if defined( AES_192 ) || defined( AES_VAR)
136 AES_RETURN aes_decrypt_key192(const unsigned char *key, aes_decrypt_ctx cx[1]);
137 #endif
138 
139 #if defined( AES_256 ) || defined( AES_VAR)
140 AES_RETURN aes_decrypt_key256(const unsigned char *key, aes_decrypt_ctx cx[1]);
141 #endif
142 
143 #if defined( AES_VAR )
144 AES_RETURN aes_decrypt_key(const unsigned char *key, int key_len, aes_decrypt_ctx cx[1]);
145 #endif
146 
147 AES_RETURN aes_decrypt(const unsigned char *in, unsigned char *out, const aes_decrypt_ctx cx[1]);
148 
149 #endif
150 
151 #if defined( AES_MODES )
152 
153 /* Multiple calls to the following subroutines for multiple block   */
154 /* ECB, CBC, CFB, OFB and CTR mode encryption can be used to handle */
155 /* long messages incrementally provided that the context AND the iv */
156 /* are preserved between all such calls.  For the ECB and CBC modes */
157 /* each individual call within a series of incremental calls must   */
158 /* process only full blocks (i.e. len must be a multiple of 16) but */
159 /* the CFB, OFB and CTR mode calls can handle multiple incremental  */
160 /* calls of any length. Each mode is reset when a new AES key is    */
161 /* set but ECB and CBC operations can be reset without setting a    */
162 /* new key by setting a new IV value.  To reset CFB, OFB and CTR    */
163 /* without setting the key, aes_mode_reset() must be called and the */
164 /* IV must be set.  NOTE: All these calls update the IV on exit so  */
165 /* this has to be reset if a new operation with the same IV as the  */
166 /* previous one is required (or decryption follows encryption with  */
167 /* the same IV array).                                              */
168 
169 AES_RETURN aes_test_alignment_detection(unsigned int n);
170 
171 AES_RETURN aes_ecb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
172                     int len, const aes_encrypt_ctx cx[1]);
173 
174 AES_RETURN aes_ecb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
175                     int len, const aes_decrypt_ctx cx[1]);
176 
177 AES_RETURN aes_cbc_encrypt(const unsigned char *ibuf, unsigned char *obuf,
178                     int len, unsigned char *iv, const aes_encrypt_ctx cx[1]);
179 
180 AES_RETURN aes_cbc_decrypt(const unsigned char *ibuf, unsigned char *obuf,
181                     int len, unsigned char *iv, const aes_decrypt_ctx cx[1]);
182 
183 AES_RETURN aes_mode_reset(aes_encrypt_ctx cx[1]);
184 
185 AES_RETURN aes_cfb_encrypt(const unsigned char *ibuf, unsigned char *obuf,
186                     int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
187 
188 AES_RETURN aes_cfb_decrypt(const unsigned char *ibuf, unsigned char *obuf,
189                     int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
190 
191 #define aes_ofb_encrypt aes_ofb_crypt
192 #define aes_ofb_decrypt aes_ofb_crypt
193 
194 AES_RETURN aes_ofb_crypt(const unsigned char *ibuf, unsigned char *obuf,
195                     int len, unsigned char *iv, aes_encrypt_ctx cx[1]);
196 
197 typedef void cbuf_inc(unsigned char *cbuf);
198 
199 #define aes_ctr_encrypt aes_ctr_crypt
200 #define aes_ctr_decrypt aes_ctr_crypt
201 
202 AES_RETURN aes_ctr_crypt(const unsigned char *ibuf, unsigned char *obuf,
203             int len, unsigned char *cbuf, cbuf_inc ctr_inc, aes_encrypt_ctx cx[1]);
204 
205 #endif
206 
207 #if defined(__cplusplus)
208 }
209 #endif
210 
211 #endif
212