Mercurial > dropbear
comparison libtomcrypt/src/ciphers/noekeon.c @ 285:1b9e69c058d2
propagate from branch 'au.asn.ucc.matt.ltc.dropbear' (head 20dccfc09627970a312d77fb41dc2970b62689c3)
to branch 'au.asn.ucc.matt.dropbear' (head fdf4a7a3b97ae5046139915de7e40399cceb2c01)
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Wed, 08 Mar 2006 13:23:58 +0000 |
parents | |
children | 0cbe8f6dbf9e |
comparison
equal
deleted
inserted
replaced
281:997e6f7dc01e | 285:1b9e69c058d2 |
---|---|
1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis | |
2 * | |
3 * LibTomCrypt is a library that provides various cryptographic | |
4 * algorithms in a highly modular and flexible manner. | |
5 * | |
6 * The library is free for all purposes without any express | |
7 * guarantee it works. | |
8 * | |
9 * Tom St Denis, [email protected], http://libtomcrypt.org | |
10 */ | |
11 /** | |
12 @file noekeon.c | |
13 Implementation of the Noekeon block cipher by Tom St Denis | |
14 */ | |
15 #include "tomcrypt.h" | |
16 | |
17 #ifdef NOEKEON | |
18 | |
19 const struct ltc_cipher_descriptor noekeon_desc = | |
20 { | |
21 "noekeon", | |
22 16, | |
23 16, 16, 16, 16, | |
24 &noekeon_setup, | |
25 &noekeon_ecb_encrypt, | |
26 &noekeon_ecb_decrypt, | |
27 &noekeon_test, | |
28 &noekeon_done, | |
29 &noekeon_keysize, | |
30 NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
31 }; | |
32 | |
33 static const ulong32 RC[] = { | |
34 0x00000080UL, 0x0000001bUL, 0x00000036UL, 0x0000006cUL, | |
35 0x000000d8UL, 0x000000abUL, 0x0000004dUL, 0x0000009aUL, | |
36 0x0000002fUL, 0x0000005eUL, 0x000000bcUL, 0x00000063UL, | |
37 0x000000c6UL, 0x00000097UL, 0x00000035UL, 0x0000006aUL, | |
38 0x000000d4UL | |
39 }; | |
40 | |
41 #define kTHETA(a, b, c, d) \ | |
42 temp = a^c; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \ | |
43 b ^= temp; d ^= temp; \ | |
44 temp = b^d; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \ | |
45 a ^= temp; c ^= temp; | |
46 | |
47 #define THETA(k, a, b, c, d) \ | |
48 temp = a^c; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \ | |
49 b ^= temp ^ k[1]; d ^= temp ^ k[3]; \ | |
50 temp = b^d; temp = temp ^ ROLc(temp, 8) ^ RORc(temp, 8); \ | |
51 a ^= temp ^ k[0]; c ^= temp ^ k[2]; | |
52 | |
53 #define GAMMA(a, b, c, d) \ | |
54 b ^= ~(d|c); \ | |
55 a ^= c&b; \ | |
56 temp = d; d = a; a = temp;\ | |
57 c ^= a ^ b ^ d; \ | |
58 b ^= ~(d|c); \ | |
59 a ^= c&b; | |
60 | |
61 #define PI1(a, b, c, d) \ | |
62 a = ROLc(a, 1); c = ROLc(c, 5); d = ROLc(d, 2); | |
63 | |
64 #define PI2(a, b, c, d) \ | |
65 a = RORc(a, 1); c = RORc(c, 5); d = RORc(d, 2); | |
66 | |
67 /** | |
68 Initialize the Noekeon block cipher | |
69 @param key The symmetric key you wish to pass | |
70 @param keylen The key length in bytes | |
71 @param num_rounds The number of rounds desired (0 for default) | |
72 @param skey The key in as scheduled by this function. | |
73 @return CRYPT_OK if successful | |
74 */ | |
75 int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) | |
76 { | |
77 ulong32 temp; | |
78 | |
79 LTC_ARGCHK(key != NULL); | |
80 LTC_ARGCHK(skey != NULL); | |
81 | |
82 if (keylen != 16) { | |
83 return CRYPT_INVALID_KEYSIZE; | |
84 } | |
85 | |
86 if (num_rounds != 16 && num_rounds != 0) { | |
87 return CRYPT_INVALID_ROUNDS; | |
88 } | |
89 | |
90 LOAD32H(skey->noekeon.K[0],&key[0]); | |
91 LOAD32H(skey->noekeon.K[1],&key[4]); | |
92 LOAD32H(skey->noekeon.K[2],&key[8]); | |
93 LOAD32H(skey->noekeon.K[3],&key[12]); | |
94 | |
95 LOAD32H(skey->noekeon.dK[0],&key[0]); | |
96 LOAD32H(skey->noekeon.dK[1],&key[4]); | |
97 LOAD32H(skey->noekeon.dK[2],&key[8]); | |
98 LOAD32H(skey->noekeon.dK[3],&key[12]); | |
99 | |
100 kTHETA(skey->noekeon.dK[0], skey->noekeon.dK[1], skey->noekeon.dK[2], skey->noekeon.dK[3]); | |
101 | |
102 return CRYPT_OK; | |
103 } | |
104 | |
105 /** | |
106 Encrypts a block of text with Noekeon | |
107 @param pt The input plaintext (16 bytes) | |
108 @param ct The output ciphertext (16 bytes) | |
109 @param skey The key as scheduled | |
110 */ | |
111 #ifdef LTC_CLEAN_STACK | |
112 static void _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
113 #else | |
114 void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
115 #endif | |
116 { | |
117 ulong32 a,b,c,d,temp; | |
118 int r; | |
119 | |
120 LTC_ARGCHK(skey != NULL); | |
121 LTC_ARGCHK(pt != NULL); | |
122 LTC_ARGCHK(ct != NULL); | |
123 | |
124 LOAD32H(a,&pt[0]); LOAD32H(b,&pt[4]); | |
125 LOAD32H(c,&pt[8]); LOAD32H(d,&pt[12]); | |
126 | |
127 #define ROUND(i) \ | |
128 a ^= RC[i]; \ | |
129 THETA(skey->noekeon.K, a,b,c,d); \ | |
130 PI1(a,b,c,d); \ | |
131 GAMMA(a,b,c,d); \ | |
132 PI2(a,b,c,d); | |
133 | |
134 for (r = 0; r < 16; ++r) { | |
135 ROUND(r); | |
136 } | |
137 | |
138 #undef ROUND | |
139 | |
140 a ^= RC[16]; | |
141 THETA(skey->noekeon.K, a, b, c, d); | |
142 | |
143 STORE32H(a,&ct[0]); STORE32H(b,&ct[4]); | |
144 STORE32H(c,&ct[8]); STORE32H(d,&ct[12]); | |
145 } | |
146 | |
147 #ifdef LTC_CLEAN_STACK | |
148 void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
149 { | |
150 _noekeon_ecb_encrypt(pt, ct, skey); | |
151 burn_stack(sizeof(ulong32) * 5 + sizeof(int)); | |
152 } | |
153 #endif | |
154 | |
155 /** | |
156 Decrypts a block of text with Noekeon | |
157 @param ct The input ciphertext (16 bytes) | |
158 @param pt The output plaintext (16 bytes) | |
159 @param skey The key as scheduled | |
160 */ | |
161 #ifdef LTC_CLEAN_STACK | |
162 static void _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
163 #else | |
164 void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
165 #endif | |
166 { | |
167 ulong32 a,b,c,d, temp; | |
168 int r; | |
169 | |
170 LTC_ARGCHK(skey != NULL); | |
171 LTC_ARGCHK(pt != NULL); | |
172 LTC_ARGCHK(ct != NULL); | |
173 | |
174 LOAD32H(a,&ct[0]); LOAD32H(b,&ct[4]); | |
175 LOAD32H(c,&ct[8]); LOAD32H(d,&ct[12]); | |
176 | |
177 | |
178 #define ROUND(i) \ | |
179 THETA(skey->noekeon.dK, a,b,c,d); \ | |
180 a ^= RC[i]; \ | |
181 PI1(a,b,c,d); \ | |
182 GAMMA(a,b,c,d); \ | |
183 PI2(a,b,c,d); | |
184 | |
185 for (r = 16; r > 0; --r) { | |
186 ROUND(r); | |
187 } | |
188 | |
189 #undef ROUND | |
190 | |
191 THETA(skey->noekeon.dK, a,b,c,d); | |
192 a ^= RC[0]; | |
193 STORE32H(a,&pt[0]); STORE32H(b, &pt[4]); | |
194 STORE32H(c,&pt[8]); STORE32H(d, &pt[12]); | |
195 } | |
196 | |
197 #ifdef LTC_CLEAN_STACK | |
198 void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
199 { | |
200 _noekeon_ecb_decrypt(ct, pt, skey); | |
201 burn_stack(sizeof(ulong32) * 5 + sizeof(int)); | |
202 } | |
203 #endif | |
204 | |
205 /** | |
206 Performs a self-test of the Noekeon block cipher | |
207 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled | |
208 */ | |
209 int noekeon_test(void) | |
210 { | |
211 #ifndef LTC_TEST | |
212 return CRYPT_NOP; | |
213 #else | |
214 static const struct { | |
215 int keylen; | |
216 unsigned char key[16], pt[16], ct[16]; | |
217 } tests[] = { | |
218 { | |
219 16, | |
220 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, | |
221 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, | |
222 { 0x18, 0xa6, 0xec, 0xe5, 0x28, 0xaa, 0x79, 0x73, | |
223 0x28, 0xb2, 0xc0, 0x91, 0xa0, 0x2f, 0x54, 0xc5} | |
224 } | |
225 }; | |
226 symmetric_key key; | |
227 unsigned char tmp[2][16]; | |
228 int err, i, y; | |
229 | |
230 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { | |
231 zeromem(&key, sizeof(key)); | |
232 if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { | |
233 return err; | |
234 } | |
235 | |
236 noekeon_ecb_encrypt(tests[i].pt, tmp[0], &key); | |
237 noekeon_ecb_decrypt(tmp[0], tmp[1], &key); | |
238 if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) { | |
239 #if 0 | |
240 printf("\n\nTest %d failed\n", i); | |
241 if (memcmp(tmp[0], tests[i].ct, 16)) { | |
242 printf("CT: "); | |
243 for (i = 0; i < 16; i++) { | |
244 printf("%02x ", tmp[0][i]); | |
245 } | |
246 printf("\n"); | |
247 } else { | |
248 printf("PT: "); | |
249 for (i = 0; i < 16; i++) { | |
250 printf("%02x ", tmp[1][i]); | |
251 } | |
252 printf("\n"); | |
253 } | |
254 #endif | |
255 return CRYPT_FAIL_TESTVECTOR; | |
256 } | |
257 | |
258 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | |
259 for (y = 0; y < 16; y++) tmp[0][y] = 0; | |
260 for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key); | |
261 for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key); | |
262 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; | |
263 } | |
264 return CRYPT_OK; | |
265 #endif | |
266 } | |
267 | |
268 /** Terminate the context | |
269 @param skey The scheduled key | |
270 */ | |
271 void noekeon_done(symmetric_key *skey) | |
272 { | |
273 } | |
274 | |
275 /** | |
276 Gets suitable key size | |
277 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. | |
278 @return CRYPT_OK if the input key size is acceptable. | |
279 */ | |
280 int noekeon_keysize(int *keysize) | |
281 { | |
282 LTC_ARGCHK(keysize != NULL); | |
283 if (*keysize < 16) { | |
284 return CRYPT_INVALID_KEYSIZE; | |
285 } else { | |
286 *keysize = 16; | |
287 return CRYPT_OK; | |
288 } | |
289 } | |
290 | |
291 #endif | |
292 | |
293 | |
294 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/noekeon.c,v $ */ | |
295 /* $Revision: 1.7 $ */ | |
296 /* $Date: 2005/05/05 14:35:58 $ */ |