Start background writer during archive recovery. Background writer now performs
[PostgreSQL.git] / contrib / pgcrypto / rijndael.c
bloba0a7d10790c716ce6d5359b369d3a2321d1d7d7a
1 /* $OpenBSD$ */
3 /* $PostgreSQL$ */
5 /* This is an independent implementation of the encryption algorithm: */
6 /* */
7 /* RIJNDAEL by Joan Daemen and Vincent Rijmen */
8 /* */
9 /* which is a candidate algorithm in the Advanced Encryption Standard */
10 /* programme of the US National Institute of Standards and Technology. */
11 /* */
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. */
16 /* */
17 /* Dr Brian Gladman (gladman@seven77.demon.co.uk) 14th January 1999 */
19 /* Timing data for Rijndael (rijndael.c)
21 Algorithm: rijndael (rijndael.c)
23 128 bit key:
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
29 192 bit key:
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
35 256 bit key:
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
43 #include "postgres.h"
45 #include <sys/param.h>
47 #include "px.h"
48 #include "rijndael.h"
50 #define PRE_CALC_TABLES
51 #define LARGE_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)
72 #else
73 #define io_swap(x) (x)
74 #endif
76 #ifdef PRINT_TABS
77 #undef PRE_CALC_TABLES
78 #endif
80 #ifdef PRE_CALC_TABLES
82 #include "rijndael.tbl"
83 #define tab_gen 1
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];
94 #ifdef LARGE_TABLES
95 static u4byte fl_tab[4][256];
96 static u4byte il_tab[4][256];
97 #endif
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))
116 #ifdef LARGE_TABLES
118 #define ls_box(x) \
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))
135 #else
137 #define ls_box(x) \
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))
154 #endif
156 static void
157 gen_tabs(void)
159 #ifndef PRE_CALC_TABLES
160 u4byte i,
162 u1byte p,
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);
177 log_tab[1] = 0;
178 p = 1;
180 for (i = 0; i < 10; ++i)
182 rco_tab[i] = p;
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);
196 q = p;
197 q = (q >> 7) | (q << 1);
198 p ^= q;
199 q = (q >> 7) | (q << 1);
200 p ^= q;
201 q = (q >> 7) | (q << 1);
202 p ^= q;
203 q = (q >> 7) | (q << 1);
204 p ^= q ^ 0x63;
205 sbx_tab[i] = (u1byte) p;
206 isb_tab[p] = (u1byte) i;
209 for (i = 0; i < 256; ++i)
211 p = sbx_tab[i];
213 #ifdef LARGE_TABLES
215 t = p;
216 fl_tab[0][i] = t;
217 fl_tab[1][i] = rotl(t, 8);
218 fl_tab[2][i] = rotl(t, 16);
219 fl_tab[3][i] = rotl(t, 24);
220 #endif
221 t = ((u4byte) ff_mult(2, p)) |
222 ((u4byte) p << 8) |
223 ((u4byte) p << 16) |
224 ((u4byte) ff_mult(3, p) << 24);
226 ft_tab[0][i] = t;
227 ft_tab[1][i] = rotl(t, 8);
228 ft_tab[2][i] = rotl(t, 16);
229 ft_tab[3][i] = rotl(t, 24);
231 p = isb_tab[i];
233 #ifdef LARGE_TABLES
235 t = p;
236 il_tab[0][i] = t;
237 il_tab[1][i] = rotl(t, 8);
238 il_tab[2][i] = rotl(t, 16);
239 il_tab[3][i] = rotl(t, 24);
240 #endif
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);
246 it_tab[0][i] = t;
247 it_tab[1][i] = rotl(t, 8);
248 it_tab[2][i] = rotl(t, 16);
249 it_tab[3][i] = rotl(t, 24);
252 tab_gen = 1;
253 #endif /* !PRE_CALC_TABLES */
257 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
259 #define imix_col(y,x) \
260 do { \
261 u = star_x(x); \
262 v = star_x(u); \
263 w = star_x(v); \
264 t = w ^ (x); \
265 (y) = u ^ v ^ w; \
266 (y) ^= rotr(u ^ t, 8) ^ \
267 rotr(v ^ t, 16) ^ \
268 rotr(t,24); \
269 } while (0)
271 /* initialise the key schedule from the user supplied key */
273 #define loop4(i) \
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; \
279 } while (0)
281 #define loop6(i) \
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; \
289 } while (0)
291 #define loop8(i) \
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; \
302 } while (0)
304 rijndael_ctx *
305 rijndael_set_key(rijndael_ctx * ctx, const u4byte * in_key, const u4byte key_len,
306 int encrypt)
308 u4byte i,
313 u4byte *e_key = ctx->e_key;
314 u4byte *d_key = ctx->d_key;
316 ctx->decrypt = !encrypt;
318 if (!tab_gen)
319 gen_tabs();
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]);
328 switch (ctx->k_len)
330 case 4:
331 t = e_key[3];
332 for (i = 0; i < 10; ++i)
333 loop4(i);
334 break;
336 case 6:
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)
340 loop6(i);
341 break;
343 case 8:
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)
349 loop8(i);
350 break;
353 if (!encrypt)
355 d_key[0] = e_key[0];
356 d_key[1] = e_key[1];
357 d_key[2] = e_key[2];
358 d_key[3] = e_key[3];
360 for (i = 4; i < 4 * ctx->k_len + 24; ++i)
361 imix_col(d_key[i], e_key[i]);
364 return ctx;
367 /* encrypt a block of text */
369 #define f_nround(bo, bi, k) \
370 do { \
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); \
375 k += 4; \
376 } while (0)
378 #define f_lround(bo, bi, k) \
379 do { \
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); \
384 } while (0)
386 void
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;
391 u4byte b0[4],
392 b1[4],
393 *kp;
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];
400 kp = e_key + 4;
402 if (k_len > 6)
404 f_nround(b1, b0, kp);
405 f_nround(b0, b1, kp);
408 if (k_len > 4)
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) \
434 do { \
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); \
439 k -= 4; \
440 } while (0)
442 #define i_lround(bo, bi, k) \
443 do { \
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); \
448 } while (0)
450 void
451 rijndael_decrypt(rijndael_ctx * ctx, const u4byte * in_blk, u4byte * out_blk)
453 u4byte b0[4],
454 b1[4],
455 *kp;
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);
467 if (k_len > 6)
469 i_nround(b1, b0, kp);
470 i_nround(b0, b1, kp);
473 if (k_len > 4)
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
503 void
504 aes_set_key(rijndael_ctx * ctx, const uint8 *key, unsigned keybits, int enc)
506 uint32 *k;
508 k = (uint32 *) key;
509 rijndael_set_key(ctx, k, keybits, enc);
512 void
513 aes_ecb_encrypt(rijndael_ctx * ctx, uint8 *data, unsigned len)
515 unsigned bs = 16;
516 uint32 *d;
518 while (len >= bs)
520 d = (uint32 *) data;
521 rijndael_encrypt(ctx, d, d);
523 len -= bs;
524 data += bs;
528 void
529 aes_ecb_decrypt(rijndael_ctx * ctx, uint8 *data, unsigned len)
531 unsigned bs = 16;
532 uint32 *d;
534 while (len >= bs)
536 d = (uint32 *) data;
537 rijndael_decrypt(ctx, d, d);
539 len -= bs;
540 data += bs;
544 void
545 aes_cbc_encrypt(rijndael_ctx * ctx, uint8 *iva, uint8 *data, unsigned len)
547 uint32 *iv = (uint32 *) iva;
548 uint32 *d = (uint32 *) data;
549 unsigned bs = 16;
551 while (len >= bs)
553 d[0] ^= iv[0];
554 d[1] ^= iv[1];
555 d[2] ^= iv[2];
556 d[3] ^= iv[3];
558 rijndael_encrypt(ctx, d, d);
560 iv = d;
561 d += bs / 4;
562 len -= bs;
566 void
567 aes_cbc_decrypt(rijndael_ctx * ctx, uint8 *iva, uint8 *data, unsigned len)
569 uint32 *d = (uint32 *) data;
570 unsigned bs = 16;
571 uint32 buf[4],
572 iv[4];
574 memcpy(iv, iva, bs);
575 while (len >= bs)
577 buf[0] = d[0];
578 buf[1] = d[1];
579 buf[2] = d[2];
580 buf[3] = d[3];
582 rijndael_decrypt(ctx, buf, d);
584 d[0] ^= iv[0];
585 d[1] ^= iv[1];
586 d[2] ^= iv[2];
587 d[3] ^= iv[3];
589 iv[0] = buf[0];
590 iv[1] = buf[1];
591 iv[2] = buf[2];
592 iv[3] = buf[3];
593 d += 4;
594 len -= bs;
599 * pre-calculate tables.
601 * On i386 lifts 17k from .bss to .rodata
602 * and avoids 1k code and setup time.
603 * -marko
605 #ifdef PRINT_TABS
607 static void
608 show256u8(char *name, uint8 *data)
610 int i;
612 printf("static const u1byte %s[256] = {\n ", name);
613 for (i = 0; i < 256;)
615 printf("%u", pow_tab[i++]);
616 if (i < 256)
617 printf(i % 16 ? ", " : ",\n ");
619 printf("\n};\n\n");
623 static void
624 show4x256u32(char *name, uint32 data[4][256])
626 int i,
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]);
635 j++;
636 if (j < 256)
637 printf(j % 4 ? ", " : ",\n ");
639 printf(i < 3 ? "\n}, {\n " : "\n}\n");
641 printf("};\n\n");
645 main()
647 int i;
648 char *hdr = "/* Generated by rijndael.c */\n\n";
650 gen_tabs();
652 printf(hdr);
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);
660 #ifdef LARGE_TABLES
661 show4x256u32("fl_tab", fl_tab);
662 show4x256u32("il_tab", il_tab);
663 #endif
664 printf("static const u4byte rco_tab[10] = {\n ");
665 for (i = 0; i < 10; i++)
667 printf("0x%08x", rco_tab[i]);
668 if (i < 9)
669 printf(", ");
670 if (i == 4)
671 printf("\n ");
673 printf("\n};\n\n");
674 return 0;
677 #endif