]>
git.saurik.com Git - apple/network_cmds.git/blob - eaytest.tproj/rijndael-alg-fst.c
1 /* $KAME: rijndael-alg-fst.c,v 1.9 2001/06/19 15:21:05 itojun Exp $ */
4 * rijndael-alg-fst.c v2.3 April '2000
6 * Optimised ANSI C code
8 * authors: v1.0: Antoon Bosselaers
12 * This code is placed in the public domain.
15 #include <sys/cdefs.h>
16 #include <sys/types.h>
18 #include <sys/systm.h>
22 #include <rijndael-alg-fst.h>
23 #include <rijndael_local.h>
25 #include <boxes-fst.dat>
28 #define bcopy(a, b, c) memcpy((b), (a), (c))
29 #define bzero(a, b) memset((a), 0, (b))
30 #define panic(a) err(1, (a))
32 int rijndaelKeySched(word8 k
[MAXKC
][4], word8 W
[MAXROUNDS
+1][4][4], int ROUNDS
) {
33 /* Calculate the necessary round keys
34 * The number of calculations depends on keyBits and blockBits
36 int j
, r
, t
, rconpointer
= 0;
44 for (j
= KC
-1; j
>= 0; j
--) {
45 *((word32
*)tk
[j
]) = *((word32
*)k
[j
]);
49 /* copy values into round key array */
50 for (j
= 0; (j
< KC
) && (r
< ROUNDS
+ 1); ) {
51 for (; (j
< KC
) && (t
< 4); j
++, t
++) {
52 *((word32
*)W
[r
][t
]) = *((word32
*)tk
[j
]);
60 while (r
< ROUNDS
+ 1) { /* while not enough round key material calculated */
61 /* calculate new values */
62 tk
[0][0] ^= S
[tk
[KC
-1][1]];
63 tk
[0][1] ^= S
[tk
[KC
-1][2]];
64 tk
[0][2] ^= S
[tk
[KC
-1][3]];
65 tk
[0][3] ^= S
[tk
[KC
-1][0]];
66 tk
[0][0] ^= rcon
[rconpointer
++];
69 for (j
= 1; j
< KC
; j
++) {
70 *((word32
*)tk
[j
]) ^= *((word32
*)tk
[j
-1]);
73 for (j
= 1; j
< KC
/2; j
++) {
74 *((word32
*)tk
[j
]) ^= *((word32
*)tk
[j
-1]);
76 tk
[KC
/2][0] ^= S
[tk
[KC
/2 - 1][0]];
77 tk
[KC
/2][1] ^= S
[tk
[KC
/2 - 1][1]];
78 tk
[KC
/2][2] ^= S
[tk
[KC
/2 - 1][2]];
79 tk
[KC
/2][3] ^= S
[tk
[KC
/2 - 1][3]];
80 for (j
= KC
/2 + 1; j
< KC
; j
++) {
81 *((word32
*)tk
[j
]) ^= *((word32
*)tk
[j
-1]);
84 /* copy values into round key array */
85 for (j
= 0; (j
< KC
) && (r
< ROUNDS
+ 1); ) {
86 for (; (j
< KC
) && (t
< 4); j
++, t
++) {
87 *((word32
*)W
[r
][t
]) = *((word32
*)tk
[j
]);
99 int rijndaelKeyEncToDec(word8 W
[MAXROUNDS
+1][4][4], int ROUNDS
) {
103 for (r
= 1; r
< ROUNDS
; r
++) {
106 *((const word32
*)U1
[w
[0]])
107 ^ *((const word32
*)U2
[w
[1]])
108 ^ *((const word32
*)U3
[w
[2]])
109 ^ *((const word32
*)U4
[w
[3]]);
113 *((const word32
*)U1
[w
[0]])
114 ^ *((const word32
*)U2
[w
[1]])
115 ^ *((const word32
*)U3
[w
[2]])
116 ^ *((const word32
*)U4
[w
[3]]);
120 *((const word32
*)U1
[w
[0]])
121 ^ *((const word32
*)U2
[w
[1]])
122 ^ *((const word32
*)U3
[w
[2]])
123 ^ *((const word32
*)U4
[w
[3]]);
127 *((const word32
*)U1
[w
[0]])
128 ^ *((const word32
*)U2
[w
[1]])
129 ^ *((const word32
*)U3
[w
[2]])
130 ^ *((const word32
*)U4
[w
[3]]);
136 * Encrypt a single block.
138 int rijndaelEncrypt(word8 in
[16], word8 out
[16], word8 rk
[MAXROUNDS
+1][4][4], int ROUNDS
) {
150 #define temp xtemp.x8
152 memcpy(a
, in
, sizeof a
);
154 *((word32
*)temp
[0]) = *((word32
*)(a
)) ^ *((word32
*)rk
[0][0]);
155 *((word32
*)temp
[1]) = *((word32
*)(a
+ 4)) ^ *((word32
*)rk
[0][1]);
156 *((word32
*)temp
[2]) = *((word32
*)(a
+ 8)) ^ *((word32
*)rk
[0][2]);
157 *((word32
*)temp
[3]) = *((word32
*)(a
+12)) ^ *((word32
*)rk
[0][3]);
158 *((word32
*)(b
)) = *((const word32
*)T1
[temp
[0][0]])
159 ^ *((const word32
*)T2
[temp
[1][1]])
160 ^ *((const word32
*)T3
[temp
[2][2]])
161 ^ *((const word32
*)T4
[temp
[3][3]]);
162 *((word32
*)(b
+ 4)) = *((const word32
*)T1
[temp
[1][0]])
163 ^ *((const word32
*)T2
[temp
[2][1]])
164 ^ *((const word32
*)T3
[temp
[3][2]])
165 ^ *((const word32
*)T4
[temp
[0][3]]);
166 *((word32
*)(b
+ 8)) = *((const word32
*)T1
[temp
[2][0]])
167 ^ *((const word32
*)T2
[temp
[3][1]])
168 ^ *((const word32
*)T3
[temp
[0][2]])
169 ^ *((const word32
*)T4
[temp
[1][3]]);
170 *((word32
*)(b
+12)) = *((const word32
*)T1
[temp
[3][0]])
171 ^ *((const word32
*)T2
[temp
[0][1]])
172 ^ *((const word32
*)T3
[temp
[1][2]])
173 ^ *((const word32
*)T4
[temp
[2][3]]);
174 for (r
= 1; r
< ROUNDS
-1; r
++) {
175 *((word32
*)temp
[0]) = *((word32
*)(b
)) ^ *((word32
*)rk
[r
][0]);
176 *((word32
*)temp
[1]) = *((word32
*)(b
+ 4)) ^ *((word32
*)rk
[r
][1]);
177 *((word32
*)temp
[2]) = *((word32
*)(b
+ 8)) ^ *((word32
*)rk
[r
][2]);
178 *((word32
*)temp
[3]) = *((word32
*)(b
+12)) ^ *((word32
*)rk
[r
][3]);
180 *((word32
*)(b
)) = *((const word32
*)T1
[temp
[0][0]])
181 ^ *((const word32
*)T2
[temp
[1][1]])
182 ^ *((const word32
*)T3
[temp
[2][2]])
183 ^ *((const word32
*)T4
[temp
[3][3]]);
184 *((word32
*)(b
+ 4)) = *((const word32
*)T1
[temp
[1][0]])
185 ^ *((const word32
*)T2
[temp
[2][1]])
186 ^ *((const word32
*)T3
[temp
[3][2]])
187 ^ *((const word32
*)T4
[temp
[0][3]]);
188 *((word32
*)(b
+ 8)) = *((const word32
*)T1
[temp
[2][0]])
189 ^ *((const word32
*)T2
[temp
[3][1]])
190 ^ *((const word32
*)T3
[temp
[0][2]])
191 ^ *((const word32
*)T4
[temp
[1][3]]);
192 *((word32
*)(b
+12)) = *((const word32
*)T1
[temp
[3][0]])
193 ^ *((const word32
*)T2
[temp
[0][1]])
194 ^ *((const word32
*)T3
[temp
[1][2]])
195 ^ *((const word32
*)T4
[temp
[2][3]]);
197 /* last round is special */
198 *((word32
*)temp
[0]) = *((word32
*)(b
)) ^ *((word32
*)rk
[ROUNDS
-1][0]);
199 *((word32
*)temp
[1]) = *((word32
*)(b
+ 4)) ^ *((word32
*)rk
[ROUNDS
-1][1]);
200 *((word32
*)temp
[2]) = *((word32
*)(b
+ 8)) ^ *((word32
*)rk
[ROUNDS
-1][2]);
201 *((word32
*)temp
[3]) = *((word32
*)(b
+12)) ^ *((word32
*)rk
[ROUNDS
-1][3]);
202 b
[ 0] = T1
[temp
[0][0]][1];
203 b
[ 1] = T1
[temp
[1][1]][1];
204 b
[ 2] = T1
[temp
[2][2]][1];
205 b
[ 3] = T1
[temp
[3][3]][1];
206 b
[ 4] = T1
[temp
[1][0]][1];
207 b
[ 5] = T1
[temp
[2][1]][1];
208 b
[ 6] = T1
[temp
[3][2]][1];
209 b
[ 7] = T1
[temp
[0][3]][1];
210 b
[ 8] = T1
[temp
[2][0]][1];
211 b
[ 9] = T1
[temp
[3][1]][1];
212 b
[10] = T1
[temp
[0][2]][1];
213 b
[11] = T1
[temp
[1][3]][1];
214 b
[12] = T1
[temp
[3][0]][1];
215 b
[13] = T1
[temp
[0][1]][1];
216 b
[14] = T1
[temp
[1][2]][1];
217 b
[15] = T1
[temp
[2][3]][1];
218 *((word32
*)(b
)) ^= *((word32
*)rk
[ROUNDS
][0]);
219 *((word32
*)(b
+ 4)) ^= *((word32
*)rk
[ROUNDS
][1]);
220 *((word32
*)(b
+ 8)) ^= *((word32
*)rk
[ROUNDS
][2]);
221 *((word32
*)(b
+12)) ^= *((word32
*)rk
[ROUNDS
][3]);
223 memcpy(out
, b
, sizeof b
/* XXX out */);
231 #ifdef INTERMEDIATE_VALUE_KAT
233 * Encrypt only a certain number of rounds.
234 * Only used in the Intermediate Value Known Answer Test.
236 int rijndaelEncryptRound(word8 a
[4][4], word8 rk
[MAXROUNDS
+1][4][4], int ROUNDS
, int rounds
) {
240 /* make number of rounds sane */
241 if (rounds
> ROUNDS
) {
245 *((word32
*)a
[0]) = *((word32
*)a
[0]) ^ *((word32
*)rk
[0][0]);
246 *((word32
*)a
[1]) = *((word32
*)a
[1]) ^ *((word32
*)rk
[0][1]);
247 *((word32
*)a
[2]) = *((word32
*)a
[2]) ^ *((word32
*)rk
[0][2]);
248 *((word32
*)a
[3]) = *((word32
*)a
[3]) ^ *((word32
*)rk
[0][3]);
250 for (r
= 1; (r
<= rounds
) && (r
< ROUNDS
); r
++) {
251 *((word32
*)temp
[0]) = *((const word32
*)T1
[a
[0][0]])
252 ^ *((const word32
*)T2
[a
[1][1]])
253 ^ *((const word32
*)T3
[a
[2][2]])
254 ^ *((const word32
*)T4
[a
[3][3]]);
255 *((word32
*)temp
[1]) = *((const word32
*)T1
[a
[1][0]])
256 ^ *((const word32
*)T2
[a
[2][1]])
257 ^ *((const word32
*)T3
[a
[3][2]])
258 ^ *((const word32
*)T4
[a
[0][3]]);
259 *((word32
*)temp
[2]) = *((const word32
*)T1
[a
[2][0]])
260 ^ *((const word32
*)T2
[a
[3][1]])
261 ^ *((const word32
*)T3
[a
[0][2]])
262 ^ *((const word32
*)T4
[a
[1][3]]);
263 *((word32
*)temp
[3]) = *((const word32
*)T1
[a
[3][0]])
264 ^ *((const word32
*)T2
[a
[0][1]])
265 ^ *((const word32
*)T3
[a
[1][2]])
266 ^ *((const word32
*)T4
[a
[2][3]]);
267 *((word32
*)a
[0]) = *((word32
*)temp
[0]) ^ *((word32
*)rk
[r
][0]);
268 *((word32
*)a
[1]) = *((word32
*)temp
[1]) ^ *((word32
*)rk
[r
][1]);
269 *((word32
*)a
[2]) = *((word32
*)temp
[2]) ^ *((word32
*)rk
[r
][2]);
270 *((word32
*)a
[3]) = *((word32
*)temp
[3]) ^ *((word32
*)rk
[r
][3]);
272 if (rounds
== ROUNDS
) {
273 /* last round is special */
274 temp
[0][0] = T1
[a
[0][0]][1];
275 temp
[0][1] = T1
[a
[1][1]][1];
276 temp
[0][2] = T1
[a
[2][2]][1];
277 temp
[0][3] = T1
[a
[3][3]][1];
278 temp
[1][0] = T1
[a
[1][0]][1];
279 temp
[1][1] = T1
[a
[2][1]][1];
280 temp
[1][2] = T1
[a
[3][2]][1];
281 temp
[1][3] = T1
[a
[0][3]][1];
282 temp
[2][0] = T1
[a
[2][0]][1];
283 temp
[2][1] = T1
[a
[3][1]][1];
284 temp
[2][2] = T1
[a
[0][2]][1];
285 temp
[2][3] = T1
[a
[1][3]][1];
286 temp
[3][0] = T1
[a
[3][0]][1];
287 temp
[3][1] = T1
[a
[0][1]][1];
288 temp
[3][2] = T1
[a
[1][2]][1];
289 temp
[3][3] = T1
[a
[2][3]][1];
290 *((word32
*)a
[0]) = *((word32
*)temp
[0]) ^ *((word32
*)rk
[ROUNDS
][0]);
291 *((word32
*)a
[1]) = *((word32
*)temp
[1]) ^ *((word32
*)rk
[ROUNDS
][1]);
292 *((word32
*)a
[2]) = *((word32
*)temp
[2]) ^ *((word32
*)rk
[ROUNDS
][2]);
293 *((word32
*)a
[3]) = *((word32
*)temp
[3]) ^ *((word32
*)rk
[ROUNDS
][3]);
298 #endif /* INTERMEDIATE_VALUE_KAT */
301 * Decrypt a single block.
303 int rijndaelDecrypt(word8 in
[16], word8 out
[16], word8 rk
[MAXROUNDS
+1][4][4], int ROUNDS
) {
315 #define temp xtemp.x8
317 memcpy(a
, in
, sizeof a
);
319 *((word32
*)temp
[0]) = *((word32
*)(a
)) ^ *((word32
*)rk
[ROUNDS
][0]);
320 *((word32
*)temp
[1]) = *((word32
*)(a
+ 4)) ^ *((word32
*)rk
[ROUNDS
][1]);
321 *((word32
*)temp
[2]) = *((word32
*)(a
+ 8)) ^ *((word32
*)rk
[ROUNDS
][2]);
322 *((word32
*)temp
[3]) = *((word32
*)(a
+12)) ^ *((word32
*)rk
[ROUNDS
][3]);
324 *((word32
*)(b
)) = *((const word32
*)T5
[temp
[0][0]])
325 ^ *((const word32
*)T6
[temp
[3][1]])
326 ^ *((const word32
*)T7
[temp
[2][2]])
327 ^ *((const word32
*)T8
[temp
[1][3]]);
328 *((word32
*)(b
+ 4)) = *((const word32
*)T5
[temp
[1][0]])
329 ^ *((const word32
*)T6
[temp
[0][1]])
330 ^ *((const word32
*)T7
[temp
[3][2]])
331 ^ *((const word32
*)T8
[temp
[2][3]]);
332 *((word32
*)(b
+ 8)) = *((const word32
*)T5
[temp
[2][0]])
333 ^ *((const word32
*)T6
[temp
[1][1]])
334 ^ *((const word32
*)T7
[temp
[0][2]])
335 ^ *((const word32
*)T8
[temp
[3][3]]);
336 *((word32
*)(b
+12)) = *((const word32
*)T5
[temp
[3][0]])
337 ^ *((const word32
*)T6
[temp
[2][1]])
338 ^ *((const word32
*)T7
[temp
[1][2]])
339 ^ *((const word32
*)T8
[temp
[0][3]]);
340 for (r
= ROUNDS
-1; r
> 1; r
--) {
341 *((word32
*)temp
[0]) = *((word32
*)(b
)) ^ *((word32
*)rk
[r
][0]);
342 *((word32
*)temp
[1]) = *((word32
*)(b
+ 4)) ^ *((word32
*)rk
[r
][1]);
343 *((word32
*)temp
[2]) = *((word32
*)(b
+ 8)) ^ *((word32
*)rk
[r
][2]);
344 *((word32
*)temp
[3]) = *((word32
*)(b
+12)) ^ *((word32
*)rk
[r
][3]);
345 *((word32
*)(b
)) = *((const word32
*)T5
[temp
[0][0]])
346 ^ *((const word32
*)T6
[temp
[3][1]])
347 ^ *((const word32
*)T7
[temp
[2][2]])
348 ^ *((const word32
*)T8
[temp
[1][3]]);
349 *((word32
*)(b
+ 4)) = *((const word32
*)T5
[temp
[1][0]])
350 ^ *((const word32
*)T6
[temp
[0][1]])
351 ^ *((const word32
*)T7
[temp
[3][2]])
352 ^ *((const word32
*)T8
[temp
[2][3]]);
353 *((word32
*)(b
+ 8)) = *((const word32
*)T5
[temp
[2][0]])
354 ^ *((const word32
*)T6
[temp
[1][1]])
355 ^ *((const word32
*)T7
[temp
[0][2]])
356 ^ *((const word32
*)T8
[temp
[3][3]]);
357 *((word32
*)(b
+12)) = *((const word32
*)T5
[temp
[3][0]])
358 ^ *((const word32
*)T6
[temp
[2][1]])
359 ^ *((const word32
*)T7
[temp
[1][2]])
360 ^ *((const word32
*)T8
[temp
[0][3]]);
362 /* last round is special */
363 *((word32
*)temp
[0]) = *((word32
*)(b
)) ^ *((word32
*)rk
[1][0]);
364 *((word32
*)temp
[1]) = *((word32
*)(b
+ 4)) ^ *((word32
*)rk
[1][1]);
365 *((word32
*)temp
[2]) = *((word32
*)(b
+ 8)) ^ *((word32
*)rk
[1][2]);
366 *((word32
*)temp
[3]) = *((word32
*)(b
+12)) ^ *((word32
*)rk
[1][3]);
367 b
[ 0] = S5
[temp
[0][0]];
368 b
[ 1] = S5
[temp
[3][1]];
369 b
[ 2] = S5
[temp
[2][2]];
370 b
[ 3] = S5
[temp
[1][3]];
371 b
[ 4] = S5
[temp
[1][0]];
372 b
[ 5] = S5
[temp
[0][1]];
373 b
[ 6] = S5
[temp
[3][2]];
374 b
[ 7] = S5
[temp
[2][3]];
375 b
[ 8] = S5
[temp
[2][0]];
376 b
[ 9] = S5
[temp
[1][1]];
377 b
[10] = S5
[temp
[0][2]];
378 b
[11] = S5
[temp
[3][3]];
379 b
[12] = S5
[temp
[3][0]];
380 b
[13] = S5
[temp
[2][1]];
381 b
[14] = S5
[temp
[1][2]];
382 b
[15] = S5
[temp
[0][3]];
383 *((word32
*)(b
)) ^= *((word32
*)rk
[0][0]);
384 *((word32
*)(b
+ 4)) ^= *((word32
*)rk
[0][1]);
385 *((word32
*)(b
+ 8)) ^= *((word32
*)rk
[0][2]);
386 *((word32
*)(b
+12)) ^= *((word32
*)rk
[0][3]);
388 memcpy(out
, b
, sizeof b
/* XXX out */);
397 #ifdef INTERMEDIATE_VALUE_KAT
399 * Decrypt only a certain number of rounds.
400 * Only used in the Intermediate Value Known Answer Test.
401 * Operations rearranged such that the intermediate values
402 * of decryption correspond with the intermediate values
405 int rijndaelDecryptRound(word8 a
[4][4], word8 rk
[MAXROUNDS
+1][4][4], int ROUNDS
, int rounds
) {
407 word8 temp
[4], shift
;
409 /* make number of rounds sane */
410 if (rounds
> ROUNDS
) {
413 /* first round is special: */
414 *(word32
*)a
[0] ^= *(word32
*)rk
[ROUNDS
][0];
415 *(word32
*)a
[1] ^= *(word32
*)rk
[ROUNDS
][1];
416 *(word32
*)a
[2] ^= *(word32
*)rk
[ROUNDS
][2];
417 *(word32
*)a
[3] ^= *(word32
*)rk
[ROUNDS
][3];
418 for (i
= 0; i
< 4; i
++) {
419 a
[i
][0] = Si
[a
[i
][0]];
420 a
[i
][1] = Si
[a
[i
][1]];
421 a
[i
][2] = Si
[a
[i
][2]];
422 a
[i
][3] = Si
[a
[i
][3]];
424 for (i
= 1; i
< 4; i
++) {
426 temp
[0] = a
[(0 + shift
) & 3][i
];
427 temp
[1] = a
[(1 + shift
) & 3][i
];
428 temp
[2] = a
[(2 + shift
) & 3][i
];
429 temp
[3] = a
[(3 + shift
) & 3][i
];
435 /* ROUNDS-1 ordinary rounds */
436 for (r
= ROUNDS
-1; r
> rounds
; r
--) {
437 *(word32
*)a
[0] ^= *(word32
*)rk
[r
][0];
438 *(word32
*)a
[1] ^= *(word32
*)rk
[r
][1];
439 *(word32
*)a
[2] ^= *(word32
*)rk
[r
][2];
440 *(word32
*)a
[3] ^= *(word32
*)rk
[r
][3];
443 *((const word32
*)U1
[a
[0][0]])
444 ^ *((const word32
*)U2
[a
[0][1]])
445 ^ *((const word32
*)U3
[a
[0][2]])
446 ^ *((const word32
*)U4
[a
[0][3]]);
449 *((const word32
*)U1
[a
[1][0]])
450 ^ *((const word32
*)U2
[a
[1][1]])
451 ^ *((const word32
*)U3
[a
[1][2]])
452 ^ *((const word32
*)U4
[a
[1][3]]);
455 *((const word32
*)U1
[a
[2][0]])
456 ^ *((const word32
*)U2
[a
[2][1]])
457 ^ *((const word32
*)U3
[a
[2][2]])
458 ^ *((const word32
*)U4
[a
[2][3]]);
461 *((const word32
*)U1
[a
[3][0]])
462 ^ *((const word32
*)U2
[a
[3][1]])
463 ^ *((const word32
*)U3
[a
[3][2]])
464 ^ *((const word32
*)U4
[a
[3][3]]);
465 for (i
= 0; i
< 4; i
++) {
466 a
[i
][0] = Si
[a
[i
][0]];
467 a
[i
][1] = Si
[a
[i
][1]];
468 a
[i
][2] = Si
[a
[i
][2]];
469 a
[i
][3] = Si
[a
[i
][3]];
471 for (i
= 1; i
< 4; i
++) {
473 temp
[0] = a
[(0 + shift
) & 3][i
];
474 temp
[1] = a
[(1 + shift
) & 3][i
];
475 temp
[2] = a
[(2 + shift
) & 3][i
];
476 temp
[3] = a
[(3 + shift
) & 3][i
];
484 /* End with the extra key addition */
485 *(word32
*)a
[0] ^= *(word32
*)rk
[0][0];
486 *(word32
*)a
[1] ^= *(word32
*)rk
[0][1];
487 *(word32
*)a
[2] ^= *(word32
*)rk
[0][2];
488 *(word32
*)a
[3] ^= *(word32
*)rk
[0][3];
492 #endif /* INTERMEDIATE_VALUE_KAT */