3
|
1 /* ---- SYMMETRIC KEY STUFF ----- |
|
2 * |
|
3 * We put each of the ciphers scheduled keys in their own structs then we put all of |
|
4 * the key formats in one union. This makes the function prototypes easier to use. |
|
5 */ |
|
6 #ifdef BLOWFISH |
|
7 struct blowfish_key { |
|
8 ulong32 S[4][256]; |
|
9 ulong32 K[18]; |
|
10 }; |
|
11 #endif |
|
12 |
|
13 #ifdef RC5 |
|
14 struct rc5_key { |
|
15 int rounds; |
|
16 ulong32 K[50]; |
|
17 }; |
|
18 #endif |
|
19 |
|
20 #ifdef RC6 |
|
21 struct rc6_key { |
|
22 ulong32 K[44]; |
|
23 }; |
|
24 #endif |
|
25 |
|
26 #ifdef SAFERP |
|
27 struct saferp_key { |
|
28 unsigned char K[33][16]; |
|
29 long rounds; |
|
30 }; |
|
31 #endif |
|
32 |
|
33 #ifdef RIJNDAEL |
|
34 struct rijndael_key { |
|
35 ulong32 eK[64], dK[64]; |
|
36 int Nr; |
|
37 }; |
|
38 #endif |
|
39 |
|
40 #ifdef XTEA |
|
41 struct xtea_key { |
|
42 unsigned long A[32], B[32]; |
|
43 }; |
|
44 #endif |
|
45 |
|
46 #ifdef TWOFISH |
|
47 #ifndef TWOFISH_SMALL |
|
48 struct twofish_key { |
|
49 ulong32 S[4][256], K[40]; |
|
50 }; |
|
51 #else |
|
52 struct twofish_key { |
|
53 ulong32 K[40]; |
|
54 unsigned char S[32], start; |
|
55 }; |
|
56 #endif |
|
57 #endif |
|
58 |
|
59 #ifdef SAFER |
|
60 #define SAFER_K64_DEFAULT_NOF_ROUNDS 6 |
|
61 #define SAFER_K128_DEFAULT_NOF_ROUNDS 10 |
|
62 #define SAFER_SK64_DEFAULT_NOF_ROUNDS 8 |
|
63 #define SAFER_SK128_DEFAULT_NOF_ROUNDS 10 |
|
64 #define SAFER_MAX_NOF_ROUNDS 13 |
|
65 #define SAFER_BLOCK_LEN 8 |
|
66 #define SAFER_KEY_LEN (1 + SAFER_BLOCK_LEN * (1 + 2 * SAFER_MAX_NOF_ROUNDS)) |
|
67 typedef unsigned char safer_block_t[SAFER_BLOCK_LEN]; |
|
68 typedef unsigned char safer_key_t[SAFER_KEY_LEN]; |
|
69 struct safer_key { safer_key_t key; }; |
|
70 #endif |
|
71 |
|
72 #ifdef RC2 |
|
73 struct rc2_key { unsigned xkey[64]; }; |
|
74 #endif |
|
75 |
|
76 #ifdef DES |
|
77 struct des_key { |
|
78 ulong32 ek[32], dk[32]; |
|
79 }; |
|
80 |
|
81 struct des3_key { |
|
82 ulong32 ek[3][32], dk[3][32]; |
|
83 }; |
|
84 #endif |
|
85 |
|
86 #ifdef CAST5 |
|
87 struct cast5_key { |
|
88 ulong32 K[32], keylen; |
|
89 }; |
|
90 #endif |
|
91 |
|
92 #ifdef NOEKEON |
|
93 struct noekeon_key { |
|
94 ulong32 K[4], dK[4]; |
|
95 }; |
|
96 #endif |
|
97 |
|
98 #ifdef SKIPJACK |
|
99 struct skipjack_key { |
|
100 unsigned char key[10]; |
|
101 }; |
|
102 #endif |
|
103 |
|
104 typedef union Symmetric_key { |
|
105 #ifdef DES |
|
106 struct des_key des; |
|
107 struct des3_key des3; |
|
108 #endif |
|
109 #ifdef RC2 |
|
110 struct rc2_key rc2; |
|
111 #endif |
|
112 #ifdef SAFER |
|
113 struct safer_key safer; |
|
114 #endif |
|
115 #ifdef TWOFISH |
|
116 struct twofish_key twofish; |
|
117 #endif |
|
118 #ifdef BLOWFISH |
|
119 struct blowfish_key blowfish; |
|
120 #endif |
|
121 #ifdef RC5 |
|
122 struct rc5_key rc5; |
|
123 #endif |
|
124 #ifdef RC6 |
|
125 struct rc6_key rc6; |
|
126 #endif |
|
127 #ifdef SAFERP |
|
128 struct saferp_key saferp; |
|
129 #endif |
|
130 #ifdef RIJNDAEL |
|
131 struct rijndael_key rijndael; |
|
132 #endif |
|
133 #ifdef XTEA |
|
134 struct xtea_key xtea; |
|
135 #endif |
|
136 #ifdef CAST5 |
|
137 struct cast5_key cast5; |
|
138 #endif |
|
139 #ifdef NOEKEON |
|
140 struct noekeon_key noekeon; |
|
141 #endif |
|
142 #ifdef SKIPJACK |
|
143 struct skipjack_key skipjack; |
|
144 #endif |
|
145 } symmetric_key; |
|
146 |
|
147 /* A block cipher ECB structure */ |
|
148 typedef struct { |
|
149 int cipher, blocklen; |
|
150 symmetric_key key; |
|
151 } symmetric_ECB; |
|
152 |
|
153 /* A block cipher CFB structure */ |
|
154 typedef struct { |
|
155 int cipher, blocklen, padlen; |
|
156 unsigned char IV[MAXBLOCKSIZE], pad[MAXBLOCKSIZE]; |
|
157 symmetric_key key; |
|
158 } symmetric_CFB; |
|
159 |
|
160 /* A block cipher OFB structure */ |
|
161 typedef struct { |
|
162 int cipher, blocklen, padlen; |
|
163 unsigned char IV[MAXBLOCKSIZE]; |
|
164 symmetric_key key; |
|
165 } symmetric_OFB; |
|
166 |
|
167 /* A block cipher CBC structure */ |
|
168 typedef struct Symmetric_CBC { |
|
169 int cipher, blocklen; |
|
170 unsigned char IV[MAXBLOCKSIZE]; |
|
171 symmetric_key key; |
|
172 } symmetric_CBC; |
|
173 |
|
174 /* A block cipher CTR structure */ |
|
175 typedef struct Symmetric_CTR { |
|
176 int cipher, blocklen, padlen, mode; |
|
177 unsigned char ctr[MAXBLOCKSIZE], pad[MAXBLOCKSIZE]; |
|
178 symmetric_key key; |
|
179 } symmetric_CTR; |
|
180 |
|
181 /* cipher descriptor table, last entry has "name == NULL" to mark the end of table */ |
|
182 extern struct _cipher_descriptor { |
|
183 char *name; |
|
184 unsigned char ID; |
|
185 int min_key_length, max_key_length, block_length, default_rounds; |
|
186 int (*setup)(const unsigned char *key, int keylength, int num_rounds, symmetric_key *skey); |
|
187 void (*ecb_encrypt)(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
188 void (*ecb_decrypt)(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
189 int (*test)(void); |
|
190 int (*keysize)(int *desired_keysize); |
|
191 } cipher_descriptor[]; |
|
192 |
|
193 #ifdef BLOWFISH |
|
194 extern int blowfish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
195 extern void blowfish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
196 extern void blowfish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
197 extern int blowfish_test(void); |
|
198 extern int blowfish_keysize(int *desired_keysize); |
|
199 extern const struct _cipher_descriptor blowfish_desc; |
|
200 #endif |
|
201 |
|
202 #ifdef RC5 |
|
203 extern int rc5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
204 extern void rc5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
205 extern void rc5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
206 extern int rc5_test(void); |
|
207 extern int rc5_keysize(int *desired_keysize); |
|
208 extern const struct _cipher_descriptor rc5_desc; |
|
209 #endif |
|
210 |
|
211 #ifdef RC6 |
|
212 extern int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
213 extern void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
214 extern void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
215 extern int rc6_test(void); |
|
216 extern int rc6_keysize(int *desired_keysize); |
|
217 extern const struct _cipher_descriptor rc6_desc; |
|
218 #endif |
|
219 |
|
220 #ifdef RC2 |
|
221 extern int rc2_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
222 extern void rc2_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
223 extern void rc2_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
224 extern int rc2_test(void); |
|
225 extern int rc2_keysize(int *desired_keysize); |
|
226 extern const struct _cipher_descriptor rc2_desc; |
|
227 #endif |
|
228 |
|
229 #ifdef SAFERP |
|
230 extern int saferp_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
231 extern void saferp_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
232 extern void saferp_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
233 extern int saferp_test(void); |
|
234 extern int saferp_keysize(int *desired_keysize); |
|
235 extern const struct _cipher_descriptor saferp_desc; |
|
236 #endif |
|
237 |
|
238 #ifdef SAFER |
|
239 extern int safer_k64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
240 extern int safer_sk64_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
241 extern int safer_k128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
242 extern int safer_sk128_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
243 extern void safer_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
244 extern void safer_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
245 |
|
246 extern int safer_k64_test(void); |
|
247 extern int safer_sk64_test(void); |
|
248 extern int safer_sk128_test(void); |
|
249 |
|
250 extern int safer_64_keysize(int *desired_keysize); |
|
251 extern int safer_128_keysize(int *desired_keysize); |
|
252 extern const struct _cipher_descriptor safer_k64_desc, safer_k128_desc, safer_sk64_desc, safer_sk128_desc; |
|
253 #endif |
|
254 |
|
255 #ifdef RIJNDAEL |
|
256 |
|
257 /* make aes an alias */ |
|
258 #define aes_setup rijndael_setup |
|
259 #define aes_ecb_encrypt rijndael_ecb_encrypt |
|
260 #define aes_ecb_decrypt rijndael_ecb_decrypt |
|
261 #define aes_test rijndael_test |
|
262 #define aes_keysize rijndael_keysize |
|
263 |
15
|
264 #define aes_enc_setup rijndael_enc_setup |
|
265 #define aes_enc_ecb_encrypt rijndael_enc_ecb_encrypt |
|
266 #define aes_enc_keysize rijndael_enc_keysize |
|
267 |
3
|
268 extern int rijndael_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
269 extern void rijndael_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
270 extern void rijndael_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
271 extern int rijndael_test(void); |
|
272 extern int rijndael_keysize(int *desired_keysize); |
15
|
273 |
|
274 extern int rijndael_enc_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
275 extern void rijndael_enc_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
276 extern int rijndael_enc_keysize(int *desired_keysize); |
|
277 |
3
|
278 extern const struct _cipher_descriptor rijndael_desc, aes_desc; |
15
|
279 extern const struct _cipher_descriptor rijndael_enc_desc, aes_enc_desc; |
3
|
280 #endif |
|
281 |
|
282 #ifdef XTEA |
|
283 extern int xtea_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
284 extern void xtea_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
285 extern void xtea_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
286 extern int xtea_test(void); |
|
287 extern int xtea_keysize(int *desired_keysize); |
|
288 extern const struct _cipher_descriptor xtea_desc; |
|
289 #endif |
|
290 |
|
291 #ifdef TWOFISH |
|
292 extern int twofish_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
293 extern void twofish_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
294 extern void twofish_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
295 extern int twofish_test(void); |
|
296 extern int twofish_keysize(int *desired_keysize); |
|
297 extern const struct _cipher_descriptor twofish_desc; |
|
298 #endif |
|
299 |
|
300 #ifdef DES |
|
301 extern int des_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
302 extern void des_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
303 extern void des_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
304 extern int des_test(void); |
|
305 extern int des_keysize(int *desired_keysize); |
|
306 |
|
307 extern int des3_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
308 extern void des3_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
309 extern void des3_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
310 extern int des3_test(void); |
|
311 extern int des3_keysize(int *desired_keysize); |
|
312 |
|
313 extern const struct _cipher_descriptor des_desc, des3_desc; |
|
314 #endif |
|
315 |
|
316 #ifdef CAST5 |
|
317 extern int cast5_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
318 extern void cast5_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
319 extern void cast5_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
320 extern int cast5_test(void); |
|
321 extern int cast5_keysize(int *desired_keysize); |
|
322 extern const struct _cipher_descriptor cast5_desc; |
|
323 #endif |
|
324 |
|
325 #ifdef NOEKEON |
|
326 extern int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
327 extern void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
328 extern void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
329 extern int noekeon_test(void); |
|
330 extern int noekeon_keysize(int *desired_keysize); |
|
331 extern const struct _cipher_descriptor noekeon_desc; |
|
332 #endif |
|
333 |
|
334 #ifdef SKIPJACK |
|
335 extern int skipjack_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey); |
|
336 extern void skipjack_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key); |
|
337 extern void skipjack_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key); |
|
338 extern int skipjack_test(void); |
|
339 extern int skipjack_keysize(int *desired_keysize); |
|
340 extern const struct _cipher_descriptor skipjack_desc; |
|
341 #endif |
|
342 |
|
343 #ifdef ECB |
|
344 extern int ecb_start(int cipher, const unsigned char *key, |
|
345 int keylen, int num_rounds, symmetric_ECB *ecb); |
|
346 extern int ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_ECB *ecb); |
|
347 extern int ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_ECB *ecb); |
|
348 #endif |
|
349 |
|
350 #ifdef CFB |
|
351 extern int cfb_start(int cipher, const unsigned char *IV, const unsigned char *key, |
|
352 int keylen, int num_rounds, symmetric_CFB *cfb); |
|
353 extern int cfb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CFB *cfb); |
|
354 extern int cfb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CFB *cfb); |
15
|
355 extern int cfb_getiv(unsigned char *IV, unsigned long *len, symmetric_CFB *cfb); |
|
356 extern int cfb_setiv(const unsigned char *IV, unsigned long len, symmetric_CFB *cfb); |
3
|
357 #endif |
|
358 |
|
359 #ifdef OFB |
|
360 extern int ofb_start(int cipher, const unsigned char *IV, const unsigned char *key, |
|
361 int keylen, int num_rounds, symmetric_OFB *ofb); |
|
362 extern int ofb_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_OFB *ofb); |
|
363 extern int ofb_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_OFB *ofb); |
15
|
364 extern int ofb_getiv(unsigned char *IV, unsigned long *len, symmetric_OFB *ofb); |
|
365 extern int ofb_setiv(const unsigned char *IV, unsigned long len, symmetric_OFB *ofb); |
3
|
366 #endif |
|
367 |
|
368 #ifdef CBC |
|
369 extern int cbc_start(int cipher, const unsigned char *IV, const unsigned char *key, |
|
370 int keylen, int num_rounds, symmetric_CBC *cbc); |
|
371 extern int cbc_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_CBC *cbc); |
|
372 extern int cbc_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_CBC *cbc); |
15
|
373 extern int cbc_getiv(unsigned char *IV, unsigned long *len, symmetric_CBC *cbc); |
|
374 extern int cbc_setiv(const unsigned char *IV, unsigned long len, symmetric_CBC *cbc); |
3
|
375 #endif |
|
376 |
|
377 #ifdef CTR |
|
378 extern int ctr_start(int cipher, const unsigned char *IV, const unsigned char *key, |
|
379 int keylen, int num_rounds, symmetric_CTR *ctr); |
|
380 extern int ctr_encrypt(const unsigned char *pt, unsigned char *ct, unsigned long len, symmetric_CTR *ctr); |
|
381 extern int ctr_decrypt(const unsigned char *ct, unsigned char *pt, unsigned long len, symmetric_CTR *ctr); |
15
|
382 extern int ctr_getiv(unsigned char *IV, unsigned long *len, symmetric_CTR *ctr); |
|
383 extern int ctr_setiv(const unsigned char *IV, unsigned long len, symmetric_CTR *ctr); |
3
|
384 #endif |
|
385 |
|
386 |
|
387 |
|
388 extern int find_cipher(const char *name); |
|
389 extern int find_cipher_any(const char *name, int blocklen, int keylen); |
|
390 extern int find_cipher_id(unsigned char ID); |
|
391 |
|
392 extern int register_cipher(const struct _cipher_descriptor *cipher); |
|
393 extern int unregister_cipher(const struct _cipher_descriptor *cipher); |
|
394 |
|
395 extern int cipher_is_valid(int idx); |
|
396 |