]>
git.saurik.com Git - apple/security.git/blob - SecurityTests/cspxutils/aesVect/rijndael.c
4 /* This is an independent implementation of the encryption algorithm: */
6 /* RIJNDAEL by Joan Daemen and Vincent Rijmen */
8 /* which is a candidate algorithm in the Advanced Encryption Standard */
9 /* programme of the US National Institute of Standards and Technology. */
11 /* Copyright in this implementation is held by Dr B R Gladman but I */
12 /* hereby give permission for its free direct or derivative use subject */
13 /* to acknowledgment of its origin and compliance with any conditions */
14 /* that the originators of the algorithm place on its exploitation. */
16 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999 */
18 /* Timing data for Rijndael (rijndael.c)
20 Algorithm: rijndael (rijndael.c)
23 Key Setup: 305/1389 cycles (encrypt/decrypt)
24 Encrypt: 374 cycles = 68.4 mbits/sec
25 Decrypt: 352 cycles = 72.7 mbits/sec
26 Mean: 363 cycles = 70.5 mbits/sec
29 Key Setup: 277/1595 cycles (encrypt/decrypt)
30 Encrypt: 439 cycles = 58.3 mbits/sec
31 Decrypt: 425 cycles = 60.2 mbits/sec
32 Mean: 432 cycles = 59.3 mbits/sec
35 Key Setup: 374/1960 cycles (encrypt/decrypt)
36 Encrypt: 502 cycles = 51.0 mbits/sec
37 Decrypt: 498 cycles = 51.4 mbits/sec
38 Mean: 500 cycles = 51.2 mbits/sec
44 /* enable of block/word/byte swapping macros */
45 #define USE_SWAP_MACROS 1
47 static char *alg_name
[] = { (char *)"rijndael", (char *)"rijndael.c", (char *)"rijndael" };
61 u4byte ft_tab
[4][256];
62 u4byte it_tab
[4][256];
65 u4byte fl_tab
[4][256];
66 u4byte il_tab
[4][256];
75 #define ff_mult(a,b) (a && b ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
77 #define f_rn(bo, bi, n, k) \
78 bo[n] = ft_tab[0][byte(bi[n],0)] ^ \
79 ft_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
80 ft_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
81 ft_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
83 #define i_rn(bo, bi, n, k) \
84 bo[n] = it_tab[0][byte(bi[n],0)] ^ \
85 it_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
86 it_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
87 it_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
92 ( fl_tab[0][byte(x, 0)] ^ \
93 fl_tab[1][byte(x, 1)] ^ \
94 fl_tab[2][byte(x, 2)] ^ \
95 fl_tab[3][byte(x, 3)] )
97 #define f_rl(bo, bi, n, k) \
98 bo[n] = fl_tab[0][byte(bi[n],0)] ^ \
99 fl_tab[1][byte(bi[(n + 1) & 3],1)] ^ \
100 fl_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
101 fl_tab[3][byte(bi[(n + 3) & 3],3)] ^ *(k + n)
103 #define i_rl(bo, bi, n, k) \
104 bo[n] = il_tab[0][byte(bi[n],0)] ^ \
105 il_tab[1][byte(bi[(n + 3) & 3],1)] ^ \
106 il_tab[2][byte(bi[(n + 2) & 3],2)] ^ \
107 il_tab[3][byte(bi[(n + 1) & 3],3)] ^ *(k + n)
112 ((u4byte)sbx_tab[byte(x, 0)] << 0) ^ \
113 ((u4byte)sbx_tab[byte(x, 1)] << 8) ^ \
114 ((u4byte)sbx_tab[byte(x, 2)] << 16) ^ \
115 ((u4byte)sbx_tab[byte(x, 3)] << 24)
117 #define f_rl(bo, bi, n, k) \
118 bo[n] = (u4byte)sbx_tab[byte(bi[n],0)] ^ \
119 rotl(((u4byte)sbx_tab[byte(bi[(n + 1) & 3],1)]), 8) ^ \
120 rotl(((u4byte)sbx_tab[byte(bi[(n + 2) & 3],2)]), 16) ^ \
121 rotl(((u4byte)sbx_tab[byte(bi[(n + 3) & 3],3)]), 24) ^ *(k + n)
123 #define i_rl(bo, bi, n, k) \
124 bo[n] = (u4byte)isb_tab[byte(bi[n],0)] ^ \
125 rotl(((u4byte)isb_tab[byte(bi[(n + 3) & 3],1)]), 8) ^ \
126 rotl(((u4byte)isb_tab[byte(bi[(n + 2) & 3],2)]), 16) ^ \
127 rotl(((u4byte)isb_tab[byte(bi[(n + 1) & 3],3)]), 24) ^ *(k + n)
135 /* log and power tables for GF(2**8) finite field with */
136 /* 0x11b as modular polynomial - the simplest prmitive */
137 /* root is 0x11, used here to generate the tables */
139 for(i
= 0,p
= 1; i
< 256; ++i
)
141 pow_tab
[i
] = (u1byte
)p
; log_tab
[p
] = (u1byte
)i
;
143 p
= p
^ (p
<< 1) ^ (p
& 0x80 ? 0x01b : 0);
146 log_tab
[1] = 0; p
= 1;
148 for(i
= 0; i
< 10; ++i
)
152 p
= (p
<< 1) ^ (p
& 0x80 ? 0x1b : 0);
155 /* note that the affine byte transformation matrix in */
156 /* rijndael specification is in big endian format with */
157 /* bit 0 as the most significant bit. In the remainder */
158 /* of the specification the bits are numbered from the */
159 /* least significant end of a byte. */
161 for(i
= 0; i
< 256; ++i
)
163 p
= (i
? pow_tab
[255 - log_tab
[i
]] : 0); q
= p
;
164 q
= (q
>> 7) | (q
<< 1); p
^= q
;
165 q
= (q
>> 7) | (q
<< 1); p
^= q
;
166 q
= (q
>> 7) | (q
<< 1); p
^= q
;
167 q
= (q
>> 7) | (q
<< 1); p
^= q
^ 0x63;
168 sbx_tab
[i
] = (u1byte
)p
; isb_tab
[p
] = (u1byte
)i
;
171 for(i
= 0; i
< 256; ++i
)
177 t
= p
; fl_tab
[0][i
] = t
;
178 fl_tab
[1][i
] = rotl(t
, 8);
179 fl_tab
[2][i
] = rotl(t
, 16);
180 fl_tab
[3][i
] = rotl(t
, 24);
182 t
= ((u4byte
)ff_mult(2, p
)) |
185 ((u4byte
)ff_mult(3, p
) << 24);
188 ft_tab
[1][i
] = rotl(t
, 8);
189 ft_tab
[2][i
] = rotl(t
, 16);
190 ft_tab
[3][i
] = rotl(t
, 24);
196 t
= p
; il_tab
[0][i
] = t
;
197 il_tab
[1][i
] = rotl(t
, 8);
198 il_tab
[2][i
] = rotl(t
, 16);
199 il_tab
[3][i
] = rotl(t
, 24);
201 t
= ((u4byte
)ff_mult(14, p
)) |
202 ((u4byte
)ff_mult( 9, p
) << 8) |
203 ((u4byte
)ff_mult(13, p
) << 16) |
204 ((u4byte
)ff_mult(11, p
) << 24);
207 it_tab
[1][i
] = rotl(t
, 8);
208 it_tab
[2][i
] = rotl(t
, 16);
209 it_tab
[3][i
] = rotl(t
, 24);
215 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
217 #define imix_col(y,x) \
223 (y) ^= rotr(u ^ t, 8) ^ \
227 /* initialise the key schedule from the user supplied key */
230 { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
231 t ^= e_key[4 * i]; e_key[4 * i + 4] = t; \
232 t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t; \
233 t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t; \
234 t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t; \
238 { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
239 t ^= e_key[6 * i]; e_key[6 * i + 6] = t; \
240 t ^= e_key[6 * i + 1]; e_key[6 * i + 7] = t; \
241 t ^= e_key[6 * i + 2]; e_key[6 * i + 8] = t; \
242 t ^= e_key[6 * i + 3]; e_key[6 * i + 9] = t; \
243 t ^= e_key[6 * i + 4]; e_key[6 * i + 10] = t; \
244 t ^= e_key[6 * i + 5]; e_key[6 * i + 11] = t; \
248 { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
249 t ^= e_key[8 * i]; e_key[8 * i + 8] = t; \
250 t ^= e_key[8 * i + 1]; e_key[8 * i + 9] = t; \
251 t ^= e_key[8 * i + 2]; e_key[8 * i + 10] = t; \
252 t ^= e_key[8 * i + 3]; e_key[8 * i + 11] = t; \
253 t = e_key[8 * i + 4] ^ ls_box(t); \
254 e_key[8 * i + 12] = t; \
255 t ^= e_key[8 * i + 5]; e_key[8 * i + 13] = t; \
256 t ^= e_key[8 * i + 6]; e_key[8 * i + 14] = t; \
257 t ^= e_key[8 * i + 7]; e_key[8 * i + 15] = t; \
260 u4byte
*set_key(const u4byte in_key
[], const u4byte key_len
)
261 { u4byte i
, t
, u
, v
, w
;
267 k_len
= (key_len
+ 31) / 32;
270 get_key(e_key
, key_len
);
272 e_key
[0] = in_key
[0]; e_key
[1] = in_key
[1];
273 e_key
[2] = in_key
[2]; e_key
[3] = in_key
[3];
278 case 4: t
= e_key
[3];
279 for(i
= 0; i
< 10; ++i
)
287 /* done in get_key macros in USE_SWAP_MACROS case */
288 e_key
[4] = in_key
[4]; t
= e_key
[5] = in_key
[5];
290 for(i
= 0; i
< 8; ++i
)
298 e_key
[4] = in_key
[4]; e_key
[5] = in_key
[5];
299 e_key
[6] = in_key
[6]; t
= e_key
[7] = in_key
[7];
301 for(i
= 0; i
< 7; ++i
)
306 d_key
[0] = e_key
[0]; d_key
[1] = e_key
[1];
307 d_key
[2] = e_key
[2]; d_key
[3] = e_key
[3];
309 for(i
= 4; i
< 4 * k_len
+ 24; ++i
)
311 imix_col(d_key
[i
], e_key
[i
]);
317 /* encrypt a block of text */
319 #define f_nround(bo, bi, k) \
320 f_rn(bo, bi, 0, k); \
321 f_rn(bo, bi, 1, k); \
322 f_rn(bo, bi, 2, k); \
323 f_rn(bo, bi, 3, k); \
326 #define f_lround(bo, bi, k) \
327 f_rl(bo, bi, 0, k); \
328 f_rl(bo, bi, 1, k); \
329 f_rl(bo, bi, 2, k); \
332 void rEncrypt(const u4byte in_blk
[4], u4byte out_blk
[4])
333 { u4byte b0
[4], b1
[4], *kp
;
336 u4byte swap_block
[4];
337 get_block(swap_block
);
338 b0
[0] = swap_block
[0] ^ e_key
[0]; b0
[1] = swap_block
[1] ^ e_key
[1];
339 b0
[2] = swap_block
[2] ^ e_key
[2]; b0
[3] = swap_block
[3] ^ e_key
[3];
341 b0
[0] = in_blk
[0] ^ e_key
[0]; b0
[1] = in_blk
[1] ^ e_key
[1];
342 b0
[2] = in_blk
[2] ^ e_key
[2]; b0
[3] = in_blk
[3] ^ e_key
[3];
349 f_nround(b1
, b0
, kp
); f_nround(b0
, b1
, kp
);
354 f_nround(b1
, b0
, kp
); f_nround(b0
, b1
, kp
);
357 f_nround(b1
, b0
, kp
); f_nround(b0
, b1
, kp
);
358 f_nround(b1
, b0
, kp
); f_nround(b0
, b1
, kp
);
359 f_nround(b1
, b0
, kp
); f_nround(b0
, b1
, kp
);
360 f_nround(b1
, b0
, kp
); f_nround(b0
, b1
, kp
);
361 f_nround(b1
, b0
, kp
); f_lround(b0
, b1
, kp
);
366 out_blk
[0] = b0
[0]; out_blk
[1] = b0
[1];
367 out_blk
[2] = b0
[2]; out_blk
[3] = b0
[3];
371 /* decrypt a block of text */
373 #define i_nround(bo, bi, k) \
374 i_rn(bo, bi, 0, k); \
375 i_rn(bo, bi, 1, k); \
376 i_rn(bo, bi, 2, k); \
377 i_rn(bo, bi, 3, k); \
380 #define i_lround(bo, bi, k) \
381 i_rl(bo, bi, 0, k); \
382 i_rl(bo, bi, 1, k); \
383 i_rl(bo, bi, 2, k); \
386 void rDecrypt(const u4byte in_blk
[4], u4byte out_blk
[4])
387 { u4byte b0
[4], b1
[4], *kp
;
390 u4byte swap_block
[4];
391 get_block(swap_block
);
392 b0
[0] = swap_block
[0] ^ e_key
[4 * k_len
+ 24];
393 b0
[1] = swap_block
[1] ^ e_key
[4 * k_len
+ 25];
394 b0
[2] = swap_block
[2] ^ e_key
[4 * k_len
+ 26];
395 b0
[3] = swap_block
[3] ^ e_key
[4 * k_len
+ 27];
397 b0
[0] = in_blk
[0] ^ e_key
[4 * k_len
+ 24];
398 b0
[1] = in_blk
[1] ^ e_key
[4 * k_len
+ 25];
399 b0
[2] = in_blk
[2] ^ e_key
[4 * k_len
+ 26];
400 b0
[3] = in_blk
[3] ^ e_key
[4 * k_len
+ 27];
403 kp
= d_key
+ 4 * (k_len
+ 5);
407 i_nround(b1
, b0
, kp
); i_nround(b0
, b1
, kp
);
412 i_nround(b1
, b0
, kp
); i_nround(b0
, b1
, kp
);
415 i_nround(b1
, b0
, kp
); i_nround(b0
, b1
, kp
);
416 i_nround(b1
, b0
, kp
); i_nround(b0
, b1
, kp
);
417 i_nround(b1
, b0
, kp
); i_nround(b0
, b1
, kp
);
418 i_nround(b1
, b0
, kp
); i_nround(b0
, b1
, kp
);
419 i_nround(b1
, b0
, kp
); i_lround(b0
, b1
, kp
);
424 out_blk
[0] = b0
[0]; out_blk
[1] = b0
[1];
425 out_blk
[2] = b0
[2]; out_blk
[3] = b0
[3];