Mercurial > dropbear
comparison src/ciphers/rc6.c @ 191:1c15b283127b libtomcrypt-orig
Import of libtomcrypt 1.02 with manual path rename rearrangement etc
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 06 May 2005 13:23:02 +0000 |
parents | |
children | 39d5d58461d6 |
comparison
equal
deleted
inserted
replaced
143:5d99163f7e32 | 191:1c15b283127b |
---|---|
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 /** | |
13 @file rc6.c | |
14 RC6 code by Tom St Denis | |
15 */ | |
16 #include "tomcrypt.h" | |
17 | |
18 #ifdef RC6 | |
19 | |
20 const struct ltc_cipher_descriptor rc6_desc = | |
21 { | |
22 "rc6", | |
23 3, | |
24 8, 128, 16, 20, | |
25 &rc6_setup, | |
26 &rc6_ecb_encrypt, | |
27 &rc6_ecb_decrypt, | |
28 &rc6_test, | |
29 &rc6_done, | |
30 &rc6_keysize, | |
31 NULL, NULL, NULL, NULL, NULL, NULL, NULL | |
32 }; | |
33 | |
34 static const ulong32 stab[44] = { | |
35 0xb7e15163UL, 0x5618cb1cUL, 0xf45044d5UL, 0x9287be8eUL, 0x30bf3847UL, 0xcef6b200UL, 0x6d2e2bb9UL, 0x0b65a572UL, | |
36 0xa99d1f2bUL, 0x47d498e4UL, 0xe60c129dUL, 0x84438c56UL, 0x227b060fUL, 0xc0b27fc8UL, 0x5ee9f981UL, 0xfd21733aUL, | |
37 0x9b58ecf3UL, 0x399066acUL, 0xd7c7e065UL, 0x75ff5a1eUL, 0x1436d3d7UL, 0xb26e4d90UL, 0x50a5c749UL, 0xeedd4102UL, | |
38 0x8d14babbUL, 0x2b4c3474UL, 0xc983ae2dUL, 0x67bb27e6UL, 0x05f2a19fUL, 0xa42a1b58UL, 0x42619511UL, 0xe0990ecaUL, | |
39 0x7ed08883UL, 0x1d08023cUL, 0xbb3f7bf5UL, 0x5976f5aeUL, 0xf7ae6f67UL, 0x95e5e920UL, 0x341d62d9UL, 0xd254dc92UL, | |
40 0x708c564bUL, 0x0ec3d004UL, 0xacfb49bdUL, 0x4b32c376UL }; | |
41 | |
42 /** | |
43 Initialize the RC6 block cipher | |
44 @param key The symmetric key you wish to pass | |
45 @param keylen The key length in bytes | |
46 @param num_rounds The number of rounds desired (0 for default) | |
47 @param skey The key in as scheduled by this function. | |
48 @return CRYPT_OK if successful | |
49 */ | |
50 #ifdef LTC_CLEAN_STACK | |
51 static int _rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) | |
52 #else | |
53 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) | |
54 #endif | |
55 { | |
56 ulong32 L[64], S[50], A, B, i, j, v, s, l; | |
57 | |
58 LTC_ARGCHK(key != NULL); | |
59 LTC_ARGCHK(skey != NULL); | |
60 | |
61 /* test parameters */ | |
62 if (num_rounds != 0 && num_rounds != 20) { | |
63 return CRYPT_INVALID_ROUNDS; | |
64 } | |
65 | |
66 /* key must be between 64 and 1024 bits */ | |
67 if (keylen < 8 || keylen > 128) { | |
68 return CRYPT_INVALID_KEYSIZE; | |
69 } | |
70 | |
71 /* copy the key into the L array */ | |
72 for (A = i = j = 0; i < (ulong32)keylen; ) { | |
73 A = (A << 8) | ((ulong32)(key[i++] & 255)); | |
74 if (!(i & 3)) { | |
75 L[j++] = BSWAP(A); | |
76 A = 0; | |
77 } | |
78 } | |
79 | |
80 /* handle odd sized keys */ | |
81 if (keylen & 3) { | |
82 A <<= (8 * (4 - (keylen&3))); | |
83 L[j++] = BSWAP(A); | |
84 } | |
85 | |
86 /* setup the S array */ | |
87 XMEMCPY(S, stab, 44 * sizeof(stab[0])); | |
88 | |
89 /* mix buffer */ | |
90 s = 3 * MAX(44, j); | |
91 l = j; | |
92 for (A = B = i = j = v = 0; v < s; v++) { | |
93 A = S[i] = ROLc(S[i] + A + B, 3); | |
94 B = L[j] = ROL(L[j] + A + B, (A+B)); | |
95 if (++i == 44) { i = 0; } | |
96 if (++j == l) { j = 0; } | |
97 } | |
98 | |
99 /* copy to key */ | |
100 for (i = 0; i < 44; i++) { | |
101 skey->rc6.K[i] = S[i]; | |
102 } | |
103 return CRYPT_OK; | |
104 } | |
105 | |
106 #ifdef LTC_CLEAN_STACK | |
107 int rc6_setup(const unsigned char *key, int keylen, int num_rounds, symmetric_key *skey) | |
108 { | |
109 int x; | |
110 x = _rc6_setup(key, keylen, num_rounds, skey); | |
111 burn_stack(sizeof(ulong32) * 122); | |
112 return x; | |
113 } | |
114 #endif | |
115 | |
116 /** | |
117 Encrypts a block of text with RC6 | |
118 @param pt The input plaintext (16 bytes) | |
119 @param ct The output ciphertext (16 bytes) | |
120 @param skey The key as scheduled | |
121 */ | |
122 #ifdef LTC_CLEAN_STACK | |
123 static void _rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
124 #else | |
125 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
126 #endif | |
127 { | |
128 ulong32 a,b,c,d,t,u, *K; | |
129 int r; | |
130 | |
131 LTC_ARGCHK(skey != NULL); | |
132 LTC_ARGCHK(pt != NULL); | |
133 LTC_ARGCHK(ct != NULL); | |
134 LOAD32L(a,&pt[0]);LOAD32L(b,&pt[4]);LOAD32L(c,&pt[8]);LOAD32L(d,&pt[12]); | |
135 | |
136 b += skey->rc6.K[0]; | |
137 d += skey->rc6.K[1]; | |
138 | |
139 #define RND(a,b,c,d) \ | |
140 t = (b * (b + b + 1)); t = ROLc(t, 5); \ | |
141 u = (d * (d + d + 1)); u = ROLc(u, 5); \ | |
142 a = ROL(a^t,u) + K[0]; \ | |
143 c = ROL(c^u,t) + K[1]; K += 2; | |
144 | |
145 K = skey->rc6.K + 2; | |
146 for (r = 0; r < 20; r += 4) { | |
147 RND(a,b,c,d); | |
148 RND(b,c,d,a); | |
149 RND(c,d,a,b); | |
150 RND(d,a,b,c); | |
151 } | |
152 | |
153 #undef RND | |
154 | |
155 a += skey->rc6.K[42]; | |
156 c += skey->rc6.K[43]; | |
157 STORE32L(a,&ct[0]);STORE32L(b,&ct[4]);STORE32L(c,&ct[8]);STORE32L(d,&ct[12]); | |
158 } | |
159 | |
160 #ifdef LTC_CLEAN_STACK | |
161 void rc6_ecb_encrypt(const unsigned char *pt, unsigned char *ct, symmetric_key *skey) | |
162 { | |
163 _rc6_ecb_encrypt(pt, ct, skey); | |
164 burn_stack(sizeof(ulong32) * 6 + sizeof(int)); | |
165 } | |
166 #endif | |
167 | |
168 /** | |
169 Decrypts a block of text with RC6 | |
170 @param ct The input ciphertext (16 bytes) | |
171 @param pt The output plaintext (16 bytes) | |
172 @param skey The key as scheduled | |
173 */ | |
174 #ifdef LTC_CLEAN_STACK | |
175 static void _rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
176 #else | |
177 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
178 #endif | |
179 { | |
180 ulong32 a,b,c,d,t,u, *K; | |
181 int r; | |
182 | |
183 LTC_ARGCHK(skey != NULL); | |
184 LTC_ARGCHK(pt != NULL); | |
185 LTC_ARGCHK(ct != NULL); | |
186 | |
187 LOAD32L(a,&ct[0]);LOAD32L(b,&ct[4]);LOAD32L(c,&ct[8]);LOAD32L(d,&ct[12]); | |
188 a -= skey->rc6.K[42]; | |
189 c -= skey->rc6.K[43]; | |
190 | |
191 #define RND(a,b,c,d) \ | |
192 t = (b * (b + b + 1)); t = ROLc(t, 5); \ | |
193 u = (d * (d + d + 1)); u = ROLc(u, 5); \ | |
194 c = ROR(c - K[1], t) ^ u; \ | |
195 a = ROR(a - K[0], u) ^ t; K -= 2; | |
196 | |
197 K = skey->rc6.K + 40; | |
198 | |
199 for (r = 0; r < 20; r += 4) { | |
200 RND(d,a,b,c); | |
201 RND(c,d,a,b); | |
202 RND(b,c,d,a); | |
203 RND(a,b,c,d); | |
204 } | |
205 | |
206 #undef RND | |
207 | |
208 b -= skey->rc6.K[0]; | |
209 d -= skey->rc6.K[1]; | |
210 STORE32L(a,&pt[0]);STORE32L(b,&pt[4]);STORE32L(c,&pt[8]);STORE32L(d,&pt[12]); | |
211 } | |
212 | |
213 #ifdef LTC_CLEAN_STACK | |
214 void rc6_ecb_decrypt(const unsigned char *ct, unsigned char *pt, symmetric_key *skey) | |
215 { | |
216 _rc6_ecb_decrypt(ct, pt, skey); | |
217 burn_stack(sizeof(ulong32) * 6 + sizeof(int)); | |
218 } | |
219 #endif | |
220 | |
221 /** | |
222 Performs a self-test of the RC6 block cipher | |
223 @return CRYPT_OK if functional, CRYPT_NOP if self-test has been disabled | |
224 */ | |
225 int rc6_test(void) | |
226 { | |
227 #ifndef LTC_TEST | |
228 return CRYPT_NOP; | |
229 #else | |
230 static const struct { | |
231 int keylen; | |
232 unsigned char key[32], pt[16], ct[16]; | |
233 } tests[] = { | |
234 { | |
235 16, | |
236 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, | |
237 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, | |
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, | |
240 { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, | |
241 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 }, | |
242 { 0x52, 0x4e, 0x19, 0x2f, 0x47, 0x15, 0xc6, 0x23, | |
243 0x1f, 0x51, 0xf6, 0x36, 0x7e, 0xa4, 0x3f, 0x18 } | |
244 }, | |
245 { | |
246 24, | |
247 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, | |
248 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, | |
249 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0, | |
250 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, | |
251 { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, | |
252 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 }, | |
253 { 0x68, 0x83, 0x29, 0xd0, 0x19, 0xe5, 0x05, 0x04, | |
254 0x1e, 0x52, 0xe9, 0x2a, 0xf9, 0x52, 0x91, 0xd4 } | |
255 }, | |
256 { | |
257 32, | |
258 { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, | |
259 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, | |
260 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0, | |
261 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }, | |
262 { 0x02, 0x13, 0x24, 0x35, 0x46, 0x57, 0x68, 0x79, | |
263 0x8a, 0x9b, 0xac, 0xbd, 0xce, 0xdf, 0xe0, 0xf1 }, | |
264 { 0xc8, 0x24, 0x18, 0x16, 0xf0, 0xd7, 0xe4, 0x89, | |
265 0x20, 0xad, 0x16, 0xa1, 0x67, 0x4e, 0x5d, 0x48 } | |
266 } | |
267 }; | |
268 unsigned char tmp[2][16]; | |
269 int x, y, err; | |
270 symmetric_key key; | |
271 | |
272 for (x = 0; x < (int)(sizeof(tests) / sizeof(tests[0])); x++) { | |
273 /* setup key */ | |
274 if ((err = rc6_setup(tests[x].key, tests[x].keylen, 0, &key)) != CRYPT_OK) { | |
275 return err; | |
276 } | |
277 | |
278 /* encrypt and decrypt */ | |
279 rc6_ecb_encrypt(tests[x].pt, tmp[0], &key); | |
280 rc6_ecb_decrypt(tmp[0], tmp[1], &key); | |
281 | |
282 /* compare */ | |
283 if (memcmp(tmp[0], tests[x].ct, 16) || memcmp(tmp[1], tests[x].pt, 16)) { | |
284 #if 0 | |
285 printf("\n\nFailed test %d\n", x); | |
286 if (memcmp(tmp[0], tests[x].ct, 16)) { | |
287 printf("Ciphertext: "); | |
288 for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]); | |
289 printf("\nExpected : "); | |
290 for (y = 0; y < 16; y++) printf("%02x ", tests[x].ct[y]); | |
291 printf("\n"); | |
292 } | |
293 if (memcmp(tmp[1], tests[x].pt, 16)) { | |
294 printf("Plaintext: "); | |
295 for (y = 0; y < 16; y++) printf("%02x ", tmp[0][y]); | |
296 printf("\nExpected : "); | |
297 for (y = 0; y < 16; y++) printf("%02x ", tests[x].pt[y]); | |
298 printf("\n"); | |
299 } | |
300 #endif | |
301 return CRYPT_FAIL_TESTVECTOR; | |
302 } | |
303 | |
304 /* now see if we can encrypt all zero bytes 1000 times, decrypt and come back where we started */ | |
305 for (y = 0; y < 16; y++) tmp[0][y] = 0; | |
306 for (y = 0; y < 1000; y++) rc6_ecb_encrypt(tmp[0], tmp[0], &key); | |
307 for (y = 0; y < 1000; y++) rc6_ecb_decrypt(tmp[0], tmp[0], &key); | |
308 for (y = 0; y < 16; y++) if (tmp[0][y] != 0) return CRYPT_FAIL_TESTVECTOR; | |
309 } | |
310 return CRYPT_OK; | |
311 #endif | |
312 } | |
313 | |
314 /** Terminate the context | |
315 @param skey The scheduled key | |
316 */ | |
317 void rc6_done(symmetric_key *skey) | |
318 { | |
319 } | |
320 | |
321 /** | |
322 Gets suitable key size | |
323 @param keysize [in/out] The length of the recommended key (in bytes). This function will store the suitable size back in this variable. | |
324 @return CRYPT_OK if the input key size is acceptable. | |
325 */ | |
326 int rc6_keysize(int *keysize) | |
327 { | |
328 LTC_ARGCHK(keysize != NULL); | |
329 if (*keysize < 8) { | |
330 return CRYPT_INVALID_KEYSIZE; | |
331 } else if (*keysize > 128) { | |
332 *keysize = 128; | |
333 } | |
334 return CRYPT_OK; | |
335 } | |
336 | |
337 #endif /*RC6*/ | |
338 | |
339 |