comparison libtomcrypt/src/ciphers/noekeon.c @ 399:a707e6148060

merge of '5fdf69ca60d1683cdd9f4c2595134bed26394834' and '6b61c50f4cf888bea302ac8fcf5dbb573b443251'
author Matt Johnston <matt@ucc.asn.au>
date Sat, 03 Feb 2007 08:20:34 +0000
parents 0cbe8f6dbf9e
children f849a5ca2efc
comparison
equal deleted inserted replaced
394:17d097fc111c 399:a707e6148060
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.com
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, 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 @return CRYPT_OK if successful
111 */
112 #ifdef LTC_CLEAN_STACK
113 static int _noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
114 #else
115 int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
116 #endif
117 {
118 ulong32 a,b,c,d,temp;
119 int r;
120
121 LTC_ARGCHK(skey != NULL);
122 LTC_ARGCHK(pt != NULL);
123 LTC_ARGCHK(ct != NULL);
124
125 LOAD32H(a,&pt[0]); LOAD32H(b,&pt[4]);
126 LOAD32H(c,&pt[8]); LOAD32H(d,&pt[12]);
127
128 #define ROUND(i) \
129 a ^= RC[i]; \
130 THETA(skey->noekeon.K, a,b,c,d); \
131 PI1(a,b,c,d); \
132 GAMMA(a,b,c,d); \
133 PI2(a,b,c,d);
134
135 for (r = 0; r < 16; ++r) {
136 ROUND(r);
137 }
138
139 #undef ROUND
140
141 a ^= RC[16];
142 THETA(skey->noekeon.K, a, b, c, d);
143
144 STORE32H(a,&ct[0]); STORE32H(b,&ct[4]);
145 STORE32H(c,&ct[8]); STORE32H(d,&ct[12]);
146
147 return CRYPT_OK;
148 }
149
150 #ifdef LTC_CLEAN_STACK
151 int noekeon_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey)
152 {
153 int err = _noekeon_ecb_encrypt(pt, ct, skey);
154 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
155 return CRYPT_OK;
156 }
157 #endif
158
159 /**
160 Decrypts a block of text with Noekeon
161 @param ct The input ciphertext (16 bytes)
162 @param pt The output plaintext (16 bytes)
163 @param skey The key as scheduled
164 @return CRYPT_OK if successful
165 */
166 #ifdef LTC_CLEAN_STACK
167 static int _noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
168 #else
169 int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
170 #endif
171 {
172 ulong32 a,b,c,d, temp;
173 int r;
174
175 LTC_ARGCHK(skey != NULL);
176 LTC_ARGCHK(pt != NULL);
177 LTC_ARGCHK(ct != NULL);
178
179 LOAD32H(a,&ct[0]); LOAD32H(b,&ct[4]);
180 LOAD32H(c,&ct[8]); LOAD32H(d,&ct[12]);
181
182
183 #define ROUND(i) \
184 THETA(skey->noekeon.dK, a,b,c,d); \
185 a ^= RC[i]; \
186 PI1(a,b,c,d); \
187 GAMMA(a,b,c,d); \
188 PI2(a,b,c,d);
189
190 for (r = 16; r > 0; --r) {
191 ROUND(r);
192 }
193
194 #undef ROUND
195
196 THETA(skey->noekeon.dK, a,b,c,d);
197 a ^= RC[0];
198 STORE32H(a,&pt[0]); STORE32H(b, &pt[4]);
199 STORE32H(c,&pt[8]); STORE32H(d, &pt[12]);
200 return CRYPT_OK;
201 }
202
203 #ifdef LTC_CLEAN_STACK
204 int noekeon_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey)
205 {
206 int err = _noekeon_ecb_decrypt(ct, pt, skey);
207 burn_stack(sizeof(ulong32) * 5 + sizeof(int));
208 return err;
209 }
210 #endif
211
212 /**
213 Performs a self-test of the Noekeon block cipher
214 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled
215 */
216 int noekeon_test(void)
217 {
218 #ifndef LTC_TEST
219 return CRYPT_NOP;
220 #else
221 static const struct {
222 int keylen;
223 unsigned char key[16], pt[16], ct[16];
224 } tests[] = {
225 {
226 16,
227 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
228 { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 },
229 { 0x18, 0xa6, 0xec, 0xe5, 0x28, 0xaa, 0x79, 0x73,
230 0x28, 0xb2, 0xc0, 0x91, 0xa0, 0x2f, 0x54, 0xc5}
231 }
232 };
233 symmetric_key key;
234 unsigned char tmp[2][16];
235 int err, i, y;
236
237 for (i = 0; i < (int)(sizeof(tests)/sizeof(tests[0])); i++) {
238 zeromem(&key, sizeof(key));
239 if ((err = noekeon_setup(tests[i].key, tests[i].keylen, 0, &key)) != CRYPT_OK) {
240 return err;
241 }
242
243 noekeon_ecb_encrypt(tests[i].pt, tmp[0], &key);
244 noekeon_ecb_decrypt(tmp[0], tmp[1], &key);
245 if (XMEMCMP(tmp[0], tests[i].ct, 16) || XMEMCMP(tmp[1], tests[i].pt, 16)) {
246 #if 0
247 printf("\n\nTest %d failed\n", i);
248 if (XMEMCMP(tmp[0], tests[i].ct, 16)) {
249 printf("CT: ");
250 for (i = 0; i < 16; i++) {
251 printf("%02x ", tmp[0][i]);
252 }
253 printf("\n");
254 } else {
255 printf("PT: ");
256 for (i = 0; i < 16; i++) {
257 printf("%02x ", tmp[1][i]);
258 }
259 printf("\n");
260 }
261 #endif
262 return CRYPT_FAIL_TESTVECTOR;
263 }
264
265 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */
266 for (y = 0; y < 16; y++) tmp[0][y] = 0;
267 for (y = 0; y < 1000; y++) noekeon_ecb_encrypt(tmp[0], tmp[0], &key);
268 for (y = 0; y < 1000; y++) noekeon_ecb_decrypt(tmp[0], tmp[0], &key);
269 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR;
270 }
271 return CRYPT_OK;
272 #endif
273 }
274
275 /** Terminate the context
276 @param skey The scheduled key
277 */
278 void noekeon_done(symmetric_key *skey)
279 {
280 }
281
282 /**
283 Gets suitable key size
284 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable.
285 @return CRYPT_OK if the input key size is acceptable.
286 */
287 int noekeon_keysize(int *keysize)
288 {
289 LTC_ARGCHK(keysize != NULL);
290 if (*keysize < 16) {
291 return CRYPT_INVALID_KEYSIZE;
292 } else {
293 *keysize = 16;
294 return CRYPT_OK;
295 }
296 }
297
298 #endif
299
300
301 /* $Source: /cvs/libtom/libtomcrypt/src/ciphers/noekeon.c,v $ */
302 /* $Revision: 1.12 $ */
303 /* $Date: 2006/11/08 23:01:06 $ */