OSDN Git Service

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