Mercurial > dropbear
comparison noekeon.c @ 0:d7da3b1e1540 libtomcrypt
put back the 0.95 makefile which was inadvertently merged over
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 31 May 2004 18:21:40 +0000 |
parents | |
children | 5d99163f7e32 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:d7da3b1e1540 |
---|---|
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 /* Implementation of the Noekeon block cipher by Tom St Denis */ | |
12 #include "mycrypt.h" | |
13 | |
14 #ifdef NOEKEON | |
15 | |
16 const struct _cipher_descriptor noekeon_desc = | |
17 { | |
18 "noekeon", | |
19 16, | |
20 16, 16, 16, 16, | |
21 &noekeon_setup, | |
22 &noekeon_ecb_encrypt, | |
23 &noekeon_ecb_decrypt, | |
24 &noekeon_test, | |
25 &noekeon_keysize | |
26 }; | |
27 | |
28 static const ulong32 RC[] = { | |
29 0x00000080UL, 0x0000001bUL, 0x00000036UL, 0x0000006cUL, | |
30 0x000000d8UL, 0x000000abUL, 0x0000004dUL, 0x0000009aUL, | |
31 0x0000002fUL, 0x0000005eUL, 0x000000bcUL, 0x00000063UL, | |
32 0x000000c6UL, 0x00000097UL, 0x00000035UL, 0x0000006aUL, | |
33 0x000000d4UL | |
34 }; | |
35 | |
36 | |
37 #define kTHETA(a, b, c, d) \ | |
38 temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \ | |
39 b ^= temp; d ^= temp; \ | |
40 temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \ | |
41 a ^= temp; c ^= temp; | |
42 | |
43 #define THETA(k, a, b, c, d) \ | |
44 temp = a^c; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \ | |
45 b ^= temp ^ k[1]; d ^= temp ^ k[3]; \ | |
46 temp = b^d; temp = temp ^ ROL(temp, 8) ^ ROR(temp, 8); \ | |
47 a ^= temp ^ k[0]; c ^= temp ^ k[2]; | |
48 | |
49 #define GAMMA(a, b, c, d) \ | |
50 b ^= ~(d|c); \ | |
51 a ^= c&b; \ | |
52 temp = d; d = a; a = temp;\ | |
53 c ^= a ^ b ^ d; \ | |
54 b ^= ~(d|c); \ | |
55 a ^= c&b; | |
56 | |
57 #define PI1(a, b, c, d) \ | |
58 a = ROL(a, 1); c = ROL(c, 5); d = ROL(d, 2); | |
59 | |
60 #define PI2(a, b, c, d) \ | |
61 a = ROR(a, 1); c = ROR(c, 5); d = ROR(d, 2); | |
62 | |
63 int noekeon_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) | |
64 { | |
65 ulong32 temp; | |
66 | |
67 _ARGCHK(key != NULL); | |
68 _ARGCHK(skey != NULL); | |
69 | |
70 if (keylen != 16) { | |
71 return CRYPT_INVALID_KEYSIZE; | |
72 } | |
73 | |
74 if (num_rounds != 16 && num_rounds != 0) { | |
75 return CRYPT_INVALID_ROUNDS; | |
76 } | |
77 | |
78 LOAD32H(skey->noekeon.K[0],&key[0]); | |
79 LOAD32H(skey->noekeon.K[1],&key[4]); | |
80 LOAD32H(skey->noekeon.K[2],&key[8]); | |
81 LOAD32H(skey->noekeon.K[3],&key[12]); | |
82 | |
83 LOAD32H(skey->noekeon.dK[0],&key[0]); | |
84 LOAD32H(skey->noekeon.dK[1],&key[4]); | |
85 LOAD32H(skey->noekeon.dK[2],&key[8]); | |
86 LOAD32H(skey->noekeon.dK[3],&key[12]); | |
87 | |
88 kTHETA(skey->noekeon.dK[0], skey->noekeon.dK[1], skey->noekeon.dK[2], skey->noekeon.dK[3]); | |
89 | |
90 return CRYPT_OK; | |
91 } | |
92 | |
93 #ifdef CLEAN_STACK | |
94 static void _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key) | |
95 #else | |
96 void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key) | |
97 #endif | |
98 { | |
99 ulong32 a,b,c,d,temp; | |
100 #ifdef SMALL_CODE | |
101 int r; | |
102 #endif | |
103 | |
104 _ARGCHK(key != NULL); | |
105 _ARGCHK(pt != NULL); | |
106 _ARGCHK(ct != NULL); | |
107 | |
108 LOAD32H(a,&pt[0]); LOAD32H(b,&pt[4]); | |
109 LOAD32H(c,&pt[8]); LOAD32H(d,&pt[12]); | |
110 | |
111 #define ROUND(i) \ | |
112 a ^= RC[i]; \ | |
113 THETA(key->noekeon.K, a,b,c,d); \ | |
114 PI1(a,b,c,d); \ | |
115 GAMMA(a,b,c,d); \ | |
116 PI2(a,b,c,d); | |
117 | |
118 #ifdef SMALL_CODE | |
119 for (r = 0; r < 16; ++r) { | |
120 ROUND(r); | |
121 } | |
122 #else | |
123 ROUND( 0); ROUND( 1); ROUND( 2); ROUND( 3); | |
124 ROUND( 4); ROUND( 5); ROUND( 6); ROUND( 7); | |
125 ROUND( 8); ROUND( 9); ROUND(10); ROUND(11); | |
126 ROUND(12); ROUND(13); ROUND(14); ROUND(15); | |
127 #endif | |
128 | |
129 #undef ROUND | |
130 | |
131 a ^= RC[16]; | |
132 THETA(key->noekeon.K, a, b, c, d); | |
133 | |
134 STORE32H(a,&ct[0]); STORE32H(b,&ct[4]); | |
135 STORE32H(c,&ct[8]); STORE32H(d,&ct[12]); | |
136 } | |
137 | |
138 #ifdef CLEAN_STACK | |
139 void noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *key) | |
140 { | |
141 _noekeon_ecb_encrypt(pt, ct, key); | |
142 burn_stack(sizeof(ulong32) * 5 + sizeof(int)); | |
143 } | |
144 #endif | |
145 | |
146 #ifdef CLEAN_STACK | |
147 static void _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key) | |
148 #else | |
149 void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key) | |
150 #endif | |
151 { | |
152 ulong32 a,b,c,d, temp; | |
153 #ifdef SMALL_CODE | |
154 int r; | |
155 #endif | |
156 | |
157 _ARGCHK(key != NULL); | |
158 _ARGCHK(pt != NULL); | |
159 _ARGCHK(ct != NULL); | |
160 | |
161 LOAD32H(a,&ct[0]); LOAD32H(b,&ct[4]); | |
162 LOAD32H(c,&ct[8]); LOAD32H(d,&ct[12]); | |
163 | |
164 | |
165 #define ROUND(i) \ | |
166 THETA(key->noekeon.dK, a,b,c,d); \ | |
167 a ^= RC[i]; \ | |
168 PI1(a,b,c,d); \ | |
169 GAMMA(a,b,c,d); \ | |
170 PI2(a,b,c,d); | |
171 | |
172 #ifdef SMALL_CODE | |
173 for (r = 16; r > 0; --r) { | |
174 ROUND(r); | |
175 } | |
176 #else | |
177 ROUND(16); ROUND(15); ROUND(14); ROUND(13); | |
178 ROUND(12); ROUND(11); ROUND(10); ROUND( 9); | |
179 ROUND( 8); ROUND( 7); ROUND( 6); ROUND( 5); | |
180 ROUND( 4); ROUND( 3); ROUND( 2); ROUND( 1); | |
181 #endif | |
182 | |
183 #undef ROUND | |
184 | |
185 THETA(key->noekeon.dK, a,b,c,d); | |
186 a ^= RC[0]; | |
187 STORE32H(a,&pt[0]); STORE32H(b, &pt[4]); | |
188 STORE32H(c,&pt[8]); STORE32H(d, &pt[12]); | |
189 } | |
190 | |
191 #ifdef CLEAN_STACK | |
192 void noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *key) | |
193 { | |
194 _noekeon_ecb_decrypt(ct, pt, key); | |
195 burn_stack(sizeof(ulong32) * 5 + sizeof(int)); | |
196 } | |
197 #endif | |
198 | |
199 int noekeon_test(void) | |
200 { | |
201 #ifndef LTC_TEST | |
202 return CRYPT_NOP; | |
203 #else | |
204 static const struct { | |
205 int keylen; | |
206 unsigned char key[16], pt[16], ct[16]; | |
207 } tests[] = { | |
208 { | |
209 16, | |
210 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, | |
211 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }, | |
212 { 0x18, 0xa6, 0xec, 0xe5, 0x28, 0xaa, 0x79, 0x73, | |
213 0x28, 0xb2, 0xc0, 0x91, 0xa0, 0x2f, 0x54, 0xc5} | |
214 } | |
215 }; | |
216 symmetric_key key; | |
217 unsigned char tmp[2][16]; | |
218 int err, i, y; | |
219 | |
220 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) { | |
221 zeromem(&key, sizeof(key)); | |
222 if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) { | |
223 return err; | |
224 } | |
225 | |
226 noekeon_ecb_encrypt(tests[i].pt, tmp[0], &key); | |
227 noekeon_ecb_decrypt(tmp[0], tmp[1], &key); | |
228 if (memcmp(tmp[0], tests[i].ct, 16) || memcmp(tmp[1], tests[i].pt, 16)) { | |
229 #if 0 | |
230 printf("\n\nTest %d failed\n", i); | |
231 if (memcmp(tmp[0], tests[i].ct, 16)) { | |
232 printf("CT: "); | |
233 for (i = 0; i < 16; i++) { | |
234 printf("%02x ", tmp[0][i]); | |
235 } | |
236 printf("\n"); | |
237 } else { | |
238 printf("PT: "); | |
239 for (i = 0; i < 16; i++) { | |
240 printf("%02x ", tmp[1][i]); | |
241 } | |
242 printf("\n"); | |
243 } | |
244 #endif | |
245 return CRYPT_FAIL_TESTVECTOR; | |
246 } | |
247 | |
248 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | |
249 for (y = 0; y < 16; y++) tmp[0][y] = 0; | |
250 for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key); | |
251 for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key); | |
252 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; | |
253 } | |
254 return CRYPT_OK; | |
255 #endif | |
256 } | |
257 | |
258 int noekeon_keysize(int *desired_keysize) | |
259 { | |
260 _ARGCHK(desired_keysize != NULL); | |
261 if (*desired_keysize < 16) { | |
262 return CRYPT_INVALID_KEYSIZE; | |
263 } else { | |
264 *desired_keysize = 16; | |
265 return CRYPT_OK; | |
266 } | |
267 } | |
268 | |
269 #endif | |
270 |