1 /* $OpenBSD: rijndael.c,v 1.6 2000/12/09 18:51:34 markus Exp $ */
3 /* contrib/pgcrypto/rijndael.c */
5 /* This is an independent implementation of the encryption algorithm: */
7 /* RIJNDAEL by Joan Daemen and Vincent Rijmen */
9 /* which is a candidate algorithm in the Advanced Encryption Standard */
10 /* programme of the US National Institute of Standards and Technology. */
12 /* Copyright in this implementation is held by Dr B R Gladman but I */
13 /* hereby give permission for its free direct or derivative use subject */
14 /* to acknowledgment of its origin and compliance with any conditions */
15 /* that the originators of the algorithm place on its exploitation. */
17 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999 */
19 /* Timing data for Rijndael (rijndael.c)
21 Algorithm: rijndael (rijndael.c)
24 Key Setup: 305/1389 cycles (encrypt/decrypt)
25 Encrypt: 374 cycles = 68.4 mbits/sec
26 Decrypt: 352 cycles = 72.7 mbits/sec
27 Mean: 363 cycles = 70.5 mbits/sec
30 Key Setup: 277/1595 cycles (encrypt/decrypt)
31 Encrypt: 439 cycles = 58.3 mbits/sec
32 Decrypt: 425 cycles = 60.2 mbits/sec
33 Mean: 432 cycles = 59.3 mbits/sec
36 Key Setup: 374/1960 cycles (encrypt/decrypt)
37 Encrypt: 502 cycles = 51.0 mbits/sec
38 Decrypt: 498 cycles = 51.4 mbits/sec
39 Mean: 500 cycles = 51.2 mbits/sec
45 #include <sys/param.h>
50 #define PRE_CALC_TABLES
53 static void gen_tabs(void);
55 /* 3. Basic macros for speeding up generic operations */
57 /* Circular rotate of 32 bit values */
59 #define rotr(x,n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
60 #define rotl(x,n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
62 /* Invert byte order in a 32 bit variable */
64 #define bswap(x) ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
66 /* Extract byte from a 32 bit quantity (little endian notation) */
68 #define byte(x,n) ((u1byte)((x) >> (8 * (n))))
70 #ifdef WORDS_BIGENDIAN
71 #define io_swap(x) bswap(x)
73 #define io_swap(x) (x)
77 #undef PRE_CALC_TABLES
80 #ifdef PRE_CALC_TABLES
82 #include "rijndael.tbl"
84 #else /* !PRE_CALC_TABLES */
86 static u1byte pow_tab[256];
87 static u1byte log_tab[256];
88 static u1byte sbx_tab[256];
89 static u1byte isb_tab[256];
90 static u4byte rco_tab[10];
91 static u4byte ft_tab[4][256];
92 static u4byte it_tab[4][256];
95 static u4byte fl_tab[4][256];
96 static u4byte il_tab[4][256];
99 static u4byte tab_gen = 0;
100 #endif /* !PRE_CALC_TABLES */
102 #define ff_mult(a,b) ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
104 #define f_rn(bo, bi, n, k) \
105 (bo)[n] = ft_tab[0][byte((bi)[n],0)] ^ \
106 ft_tab[1][byte((bi)[((n) + 1) & 3],1)] ^ \
107 ft_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
108 ft_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
110 #define i_rn(bo, bi, n, k) \
111 (bo)[n] = it_tab[0][byte((bi)[n],0)] ^ \
112 it_tab[1][byte((bi)[((n) + 3) & 3],1)] ^ \
113 it_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
114 it_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
119 ( fl_tab[0][byte(x, 0)] ^ \
120 fl_tab[1][byte(x, 1)] ^ \
121 fl_tab[2][byte(x, 2)] ^ \
122 fl_tab[3][byte(x, 3)] )
124 #define f_rl(bo, bi, n, k) \
125 (bo)[n] = fl_tab[0][byte((bi)[n],0)] ^ \
126 fl_tab[1][byte((bi)[((n) + 1) & 3],1)] ^ \
127 fl_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
128 fl_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
130 #define i_rl(bo, bi, n, k) \
131 (bo)[n] = il_tab[0][byte((bi)[n],0)] ^ \
132 il_tab[1][byte((bi)[((n) + 3) & 3],1)] ^ \
133 il_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
134 il_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
138 ((u4byte)sbx_tab[byte(x, 0)] << 0) ^ \
139 ((u4byte)sbx_tab[byte(x, 1)] << 8) ^ \
140 ((u4byte)sbx_tab[byte(x, 2)] << 16) ^ \
141 ((u4byte)sbx_tab[byte(x, 3)] << 24)
143 #define f_rl(bo, bi, n, k) \
144 (bo)[n] = (u4byte)sbx_tab[byte((bi)[n],0)] ^ \
145 rotl(((u4byte)sbx_tab[byte((bi)[((n) + 1) & 3],1)]), 8) ^ \
146 rotl(((u4byte)sbx_tab[byte((bi)[((n) + 2) & 3],2)]), 16) ^ \
147 rotl(((u4byte)sbx_tab[byte((bi)[((n) + 3) & 3],3)]), 24) ^ *((k) + (n))
149 #define i_rl(bo, bi, n, k) \
150 (bo)[n] = (u4byte)isb_tab[byte((bi)[n],0)] ^ \
151 rotl(((u4byte)isb_tab[byte((bi)[((n) + 3) & 3],1)]), 8) ^ \
152 rotl(((u4byte)isb_tab[byte((bi)[((n) + 2) & 3],2)]), 16) ^ \
153 rotl(((u4byte)isb_tab[byte((bi)[((n) + 1) & 3],3)]), 24) ^ *((k) + (n))
159 #ifndef PRE_CALC_TABLES
165 /* log and power tables for GF(2**8) finite field with */
166 /* 0x11b as modular polynomial - the simplest prmitive */
167 /* root is 0x11, used here to generate the tables */
169 for (i = 0, p = 1; i < 256; ++i)
171 pow_tab[i] = (u1byte) p;
172 log_tab[p] = (u1byte) i;
174 p = p ^ (p << 1) ^ (p & 0x80 ? 0x01b : 0);
180 for (i = 0; i < 10; ++i)
184 p = (p << 1) ^ (p & 0x80 ? 0x1b : 0);
187 /* note that the affine byte transformation matrix in */
188 /* rijndael specification is in big endian format with */
189 /* bit 0 as the most significant bit. In the remainder */
190 /* of the specification the bits are numbered from the */
191 /* least significant end of a byte. */
193 for (i = 0; i < 256; ++i)
195 p = (i ? pow_tab[255 - log_tab[i]] : 0);
197 q = (q >> 7) | (q << 1);
199 q = (q >> 7) | (q << 1);
201 q = (q >> 7) | (q << 1);
203 q = (q >> 7) | (q << 1);
205 sbx_tab[i] = (u1byte) p;
206 isb_tab[p] = (u1byte) i;
209 for (i = 0; i < 256; ++i)
217 fl_tab[1][i] = rotl(t, 8);
218 fl_tab[2][i] = rotl(t, 16);
219 fl_tab[3][i] = rotl(t, 24);
221 t = ((u4byte) ff_mult(2, p)) |
224 ((u4byte) ff_mult(3, p) << 24);
227 ft_tab[1][i] = rotl(t, 8);
228 ft_tab[2][i] = rotl(t, 16);
229 ft_tab[3][i] = rotl(t, 24);
237 il_tab[1][i] = rotl(t, 8);
238 il_tab[2][i] = rotl(t, 16);
239 il_tab[3][i] = rotl(t, 24);
241 t = ((u4byte) ff_mult(14, p)) |
242 ((u4byte) ff_mult(9, p) << 8) |
243 ((u4byte) ff_mult(13, p) << 16) |
244 ((u4byte) ff_mult(11, p) << 24);
247 it_tab[1][i] = rotl(t, 8);
248 it_tab[2][i] = rotl(t, 16);
249 it_tab[3][i] = rotl(t, 24);
253 #endif /* !PRE_CALC_TABLES */
257 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
259 #define imix_col(y,x) \
266 (y) ^= rotr(u ^ t, 8) ^ \
271 /* initialise the key schedule from the user supplied key */
274 do { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
275 t ^= e_key[4 * i]; e_key[4 * i + 4] = t; \
276 t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t; \
277 t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t; \
278 t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t; \
282 do { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
283 t ^= e_key[6 * (i)]; e_key[6 * (i) + 6] = t; \
284 t ^= e_key[6 * (i) + 1]; e_key[6 * (i) + 7] = t; \
285 t ^= e_key[6 * (i) + 2]; e_key[6 * (i) + 8] = t; \
286 t ^= e_key[6 * (i) + 3]; e_key[6 * (i) + 9] = t; \
287 t ^= e_key[6 * (i) + 4]; e_key[6 * (i) + 10] = t; \
288 t ^= e_key[6 * (i) + 5]; e_key[6 * (i) + 11] = t; \
292 do { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
293 t ^= e_key[8 * (i)]; e_key[8 * (i) + 8] = t; \
294 t ^= e_key[8 * (i) + 1]; e_key[8 * (i) + 9] = t; \
295 t ^= e_key[8 * (i) + 2]; e_key[8 * (i) + 10] = t; \
296 t ^= e_key[8 * (i) + 3]; e_key[8 * (i) + 11] = t; \
297 t = e_key[8 * (i) + 4] ^ ls_box(t); \
298 e_key[8 * (i) + 12] = t; \
299 t ^= e_key[8 * (i) + 5]; e_key[8 * (i) + 13] = t; \
300 t ^= e_key[8 * (i) + 6]; e_key[8 * (i) + 14] = t; \
301 t ^= e_key[8 * (i) + 7]; e_key[8 * (i) + 15] = t; \
305 rijndael_set_key(rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len,
313 u4byte *e_key = ctx->e_key;
314 u4byte *d_key = ctx->d_key;
316 ctx->decrypt = !encrypt;
321 ctx->k_len = (key_len + 31) / 32;
323 e_key[0] = io_swap(in_key[0]);
324 e_key[1] = io_swap(in_key[1]);
325 e_key[2] = io_swap(in_key[2]);
326 e_key[3] = io_swap(in_key[3]);
332 for (i = 0; i < 10; ++i)
337 e_key[4] = io_swap(in_key[4]);
338 t = e_key[5] = io_swap(in_key[5]);
339 for (i = 0; i < 8; ++i)
344 e_key[4] = io_swap(in_key[4]);
345 e_key[5] = io_swap(in_key[5]);
346 e_key[6] = io_swap(in_key[6]);
347 t = e_key[7] = io_swap(in_key[7]);
348 for (i = 0; i < 7; ++i)
360 for (i = 4; i < 4 * ctx->k_len + 24; ++i)
361 imix_col(d_key[i], e_key[i]);
367 /* encrypt a block of text */
369 #define f_nround(bo, bi, k) \
371 f_rn(bo, bi, 0, k); \
372 f_rn(bo, bi, 1, k); \
373 f_rn(bo, bi, 2, k); \
374 f_rn(bo, bi, 3, k); \
378 #define f_lround(bo, bi, k) \
380 f_rl(bo, bi, 0, k); \
381 f_rl(bo, bi, 1, k); \
382 f_rl(bo, bi, 2, k); \
383 f_rl(bo, bi, 3, k); \
387 rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
389 u4byte k_len = ctx->k_len;
390 u4byte *e_key = ctx->e_key;
395 b0[0] = io_swap(in_blk[0]) ^ e_key[0];
396 b0[1] = io_swap(in_blk[1]) ^ e_key[1];
397 b0[2] = io_swap(in_blk[2]) ^ e_key[2];
398 b0[3] = io_swap(in_blk[3]) ^ e_key[3];
404 f_nround(b1, b0, kp);
405 f_nround(b0, b1, kp);
410 f_nround(b1, b0, kp);
411 f_nround(b0, b1, kp);
414 f_nround(b1, b0, kp);
415 f_nround(b0, b1, kp);
416 f_nround(b1, b0, kp);
417 f_nround(b0, b1, kp);
418 f_nround(b1, b0, kp);
419 f_nround(b0, b1, kp);
420 f_nround(b1, b0, kp);
421 f_nround(b0, b1, kp);
422 f_nround(b1, b0, kp);
423 f_lround(b0, b1, kp);
425 out_blk[0] = io_swap(b0[0]);
426 out_blk[1] = io_swap(b0[1]);
427 out_blk[2] = io_swap(b0[2]);
428 out_blk[3] = io_swap(b0[3]);
431 /* decrypt a block of text */
433 #define i_nround(bo, bi, k) \
435 i_rn(bo, bi, 0, k); \
436 i_rn(bo, bi, 1, k); \
437 i_rn(bo, bi, 2, k); \
438 i_rn(bo, bi, 3, k); \
442 #define i_lround(bo, bi, k) \
444 i_rl(bo, bi, 0, k); \
445 i_rl(bo, bi, 1, k); \
446 i_rl(bo, bi, 2, k); \
447 i_rl(bo, bi, 3, k); \
451 rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
456 u4byte k_len = ctx->k_len;
457 u4byte *e_key = ctx->e_key;
458 u4byte *d_key = ctx->d_key;
460 b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
461 b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
462 b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
463 b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];
465 kp = d_key + 4 * (k_len + 5);
469 i_nround(b1, b0, kp);
470 i_nround(b0, b1, kp);
475 i_nround(b1, b0, kp);
476 i_nround(b0, b1, kp);
479 i_nround(b1, b0, kp);
480 i_nround(b0, b1, kp);
481 i_nround(b1, b0, kp);
482 i_nround(b0, b1, kp);
483 i_nround(b1, b0, kp);
484 i_nround(b0, b1, kp);
485 i_nround(b1, b0, kp);
486 i_nround(b0, b1, kp);
487 i_nround(b1, b0, kp);
488 i_lround(b0, b1, kp);
490 out_blk[0] = io_swap(b0[0]);
491 out_blk[1] = io_swap(b0[1]);
492 out_blk[2] = io_swap(b0[2]);
493 out_blk[3] = io_swap(b0[3]);
497 * conventional interface
499 * ATM it hopes all data is 4-byte aligned - which
500 * should be true for PX. -marko
504 aes_set_key(rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
509 rijndael_set_key(ctx, k, keybits, enc);
513 aes_ecb_encrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
521 rijndael_encrypt(ctx, d, d);
529 aes_ecb_decrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
537 rijndael_decrypt(ctx, d, d);
545 aes_cbc_encrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
547 uint32 *iv = (uint32 *) iva;
548 uint32 *d = (uint32 *) data;
558 rijndael_encrypt(ctx, d, d);
567 aes_cbc_decrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
569 uint32 *d = (uint32 *) data;
582 rijndael_decrypt(ctx, buf, d);
599 * pre-calculate tables.
601 * On i386 lifts 17k from .bss to .rodata
602 * and avoids 1k code and setup time.
608 show256u8(char *name, uint8 *data)
612 printf("static const u1byte %s[256] = {\n ", name);
613 for (i = 0; i < 256;)
615 printf("%u", pow_tab[i++]);
617 printf(i % 16 ? ", " : ",\n ");
624 show4x256u32(char *name, uint32 data[4][256])
629 printf("static const u4byte %s[4][256] = {\n{\n ", name);
630 for (i = 0; i < 4; i++)
632 for (j = 0; j < 256;)
634 printf("0x%08x", data[i][j]);
637 printf(j % 4 ? ", " : ",\n ");
639 printf(i < 3 ? "\n}, {\n " : "\n}\n");
648 char *hdr = "/* Generated by rijndael.c */\n\n";
653 show256u8("pow_tab", pow_tab);
654 show256u8("log_tab", log_tab);
655 show256u8("sbx_tab", sbx_tab);
656 show256u8("isb_tab", isb_tab);
658 show4x256u32("ft_tab", ft_tab);
659 show4x256u32("it_tab", it_tab);
661 show4x256u32("fl_tab", fl_tab);
662 show4x256u32("il_tab", il_tab);
664 printf("static const u4byte rco_tab[10] = {\n ");
665 for (i = 0; i < 10; i++)
667 printf("0x%08x", rco_tab[i]);