00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "postgres.h"
00044
00045 #include <sys/param.h>
00046
00047 #include "px.h"
00048 #include "rijndael.h"
00049
00050 #define PRE_CALC_TABLES
00051 #define LARGE_TABLES
00052
00053 static void gen_tabs(void);
00054
00055
00056
00057
00058
00059 #define rotr(x,n) (((x) >> ((int)(n))) | ((x) << (32 - (int)(n))))
00060 #define rotl(x,n) (((x) << ((int)(n))) | ((x) >> (32 - (int)(n))))
00061
00062
00063
00064 #define bswap(x) ((rotl((x), 8) & 0x00ff00ff) | (rotr((x), 8) & 0xff00ff00))
00065
00066
00067
00068 #define byte(x,n) ((u1byte)((x) >> (8 * (n))))
00069
00070 #ifdef WORDS_BIGENDIAN
00071 #define io_swap(x) bswap(x)
00072 #else
00073 #define io_swap(x) (x)
00074 #endif
00075
00076 #ifdef PRINT_TABS
00077 #undef PRE_CALC_TABLES
00078 #endif
00079
00080 #ifdef PRE_CALC_TABLES
00081
00082 #include "rijndael.tbl"
00083 #define tab_gen 1
00084 #else
00085
00086 static u1byte pow_tab[256];
00087 static u1byte log_tab[256];
00088 static u1byte sbx_tab[256];
00089 static u1byte isb_tab[256];
00090 static u4byte rco_tab[10];
00091 static u4byte ft_tab[4][256];
00092 static u4byte it_tab[4][256];
00093
00094 #ifdef LARGE_TABLES
00095 static u4byte fl_tab[4][256];
00096 static u4byte il_tab[4][256];
00097 #endif
00098
00099 static u4byte tab_gen = 0;
00100 #endif
00101
00102 #define ff_mult(a,b) ((a) && (b) ? pow_tab[(log_tab[a] + log_tab[b]) % 255] : 0)
00103
00104 #define f_rn(bo, bi, n, k) \
00105 (bo)[n] = ft_tab[0][byte((bi)[n],0)] ^ \
00106 ft_tab[1][byte((bi)[((n) + 1) & 3],1)] ^ \
00107 ft_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
00108 ft_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
00109
00110 #define i_rn(bo, bi, n, k) \
00111 (bo)[n] = it_tab[0][byte((bi)[n],0)] ^ \
00112 it_tab[1][byte((bi)[((n) + 3) & 3],1)] ^ \
00113 it_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
00114 it_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
00115
00116 #ifdef LARGE_TABLES
00117
00118 #define ls_box(x) \
00119 ( fl_tab[0][byte(x, 0)] ^ \
00120 fl_tab[1][byte(x, 1)] ^ \
00121 fl_tab[2][byte(x, 2)] ^ \
00122 fl_tab[3][byte(x, 3)] )
00123
00124 #define f_rl(bo, bi, n, k) \
00125 (bo)[n] = fl_tab[0][byte((bi)[n],0)] ^ \
00126 fl_tab[1][byte((bi)[((n) + 1) & 3],1)] ^ \
00127 fl_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
00128 fl_tab[3][byte((bi)[((n) + 3) & 3],3)] ^ *((k) + (n))
00129
00130 #define i_rl(bo, bi, n, k) \
00131 (bo)[n] = il_tab[0][byte((bi)[n],0)] ^ \
00132 il_tab[1][byte((bi)[((n) + 3) & 3],1)] ^ \
00133 il_tab[2][byte((bi)[((n) + 2) & 3],2)] ^ \
00134 il_tab[3][byte((bi)[((n) + 1) & 3],3)] ^ *((k) + (n))
00135 #else
00136
00137 #define ls_box(x) \
00138 ((u4byte)sbx_tab[byte(x, 0)] << 0) ^ \
00139 ((u4byte)sbx_tab[byte(x, 1)] << 8) ^ \
00140 ((u4byte)sbx_tab[byte(x, 2)] << 16) ^ \
00141 ((u4byte)sbx_tab[byte(x, 3)] << 24)
00142
00143 #define f_rl(bo, bi, n, k) \
00144 (bo)[n] = (u4byte)sbx_tab[byte((bi)[n],0)] ^ \
00145 rotl(((u4byte)sbx_tab[byte((bi)[((n) + 1) & 3],1)]), 8) ^ \
00146 rotl(((u4byte)sbx_tab[byte((bi)[((n) + 2) & 3],2)]), 16) ^ \
00147 rotl(((u4byte)sbx_tab[byte((bi)[((n) + 3) & 3],3)]), 24) ^ *((k) + (n))
00148
00149 #define i_rl(bo, bi, n, k) \
00150 (bo)[n] = (u4byte)isb_tab[byte((bi)[n],0)] ^ \
00151 rotl(((u4byte)isb_tab[byte((bi)[((n) + 3) & 3],1)]), 8) ^ \
00152 rotl(((u4byte)isb_tab[byte((bi)[((n) + 2) & 3],2)]), 16) ^ \
00153 rotl(((u4byte)isb_tab[byte((bi)[((n) + 1) & 3],3)]), 24) ^ *((k) + (n))
00154 #endif
00155
00156 static void
00157 gen_tabs(void)
00158 {
00159 #ifndef PRE_CALC_TABLES
00160 u4byte i,
00161 t;
00162 u1byte p,
00163 q;
00164
00165
00166
00167
00168
00169 for (i = 0, p = 1; i < 256; ++i)
00170 {
00171 pow_tab[i] = (u1byte) p;
00172 log_tab[p] = (u1byte) i;
00173
00174 p = p ^ (p << 1) ^ (p & 0x80 ? 0x01b : 0);
00175 }
00176
00177 log_tab[1] = 0;
00178 p = 1;
00179
00180 for (i = 0; i < 10; ++i)
00181 {
00182 rco_tab[i] = p;
00183
00184 p = (p << 1) ^ (p & 0x80 ? 0x1b : 0);
00185 }
00186
00187
00188
00189
00190
00191
00192
00193 for (i = 0; i < 256; ++i)
00194 {
00195 p = (i ? pow_tab[255 - log_tab[i]] : 0);
00196 q = p;
00197 q = (q >> 7) | (q << 1);
00198 p ^= q;
00199 q = (q >> 7) | (q << 1);
00200 p ^= q;
00201 q = (q >> 7) | (q << 1);
00202 p ^= q;
00203 q = (q >> 7) | (q << 1);
00204 p ^= q ^ 0x63;
00205 sbx_tab[i] = (u1byte) p;
00206 isb_tab[p] = (u1byte) i;
00207 }
00208
00209 for (i = 0; i < 256; ++i)
00210 {
00211 p = sbx_tab[i];
00212
00213 #ifdef LARGE_TABLES
00214
00215 t = p;
00216 fl_tab[0][i] = t;
00217 fl_tab[1][i] = rotl(t, 8);
00218 fl_tab[2][i] = rotl(t, 16);
00219 fl_tab[3][i] = rotl(t, 24);
00220 #endif
00221 t = ((u4byte) ff_mult(2, p)) |
00222 ((u4byte) p << 8) |
00223 ((u4byte) p << 16) |
00224 ((u4byte) ff_mult(3, p) << 24);
00225
00226 ft_tab[0][i] = t;
00227 ft_tab[1][i] = rotl(t, 8);
00228 ft_tab[2][i] = rotl(t, 16);
00229 ft_tab[3][i] = rotl(t, 24);
00230
00231 p = isb_tab[i];
00232
00233 #ifdef LARGE_TABLES
00234
00235 t = p;
00236 il_tab[0][i] = t;
00237 il_tab[1][i] = rotl(t, 8);
00238 il_tab[2][i] = rotl(t, 16);
00239 il_tab[3][i] = rotl(t, 24);
00240 #endif
00241 t = ((u4byte) ff_mult(14, p)) |
00242 ((u4byte) ff_mult(9, p) << 8) |
00243 ((u4byte) ff_mult(13, p) << 16) |
00244 ((u4byte) ff_mult(11, p) << 24);
00245
00246 it_tab[0][i] = t;
00247 it_tab[1][i] = rotl(t, 8);
00248 it_tab[2][i] = rotl(t, 16);
00249 it_tab[3][i] = rotl(t, 24);
00250 }
00251
00252 tab_gen = 1;
00253 #endif
00254 }
00255
00256
00257 #define star_x(x) (((x) & 0x7f7f7f7f) << 1) ^ ((((x) & 0x80808080) >> 7) * 0x1b)
00258
00259 #define imix_col(y,x) \
00260 do { \
00261 u = star_x(x); \
00262 v = star_x(u); \
00263 w = star_x(v); \
00264 t = w ^ (x); \
00265 (y) = u ^ v ^ w; \
00266 (y) ^= rotr(u ^ t, 8) ^ \
00267 rotr(v ^ t, 16) ^ \
00268 rotr(t,24); \
00269 } while (0)
00270
00271
00272
00273 #define loop4(i) \
00274 do { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
00275 t ^= e_key[4 * i]; e_key[4 * i + 4] = t; \
00276 t ^= e_key[4 * i + 1]; e_key[4 * i + 5] = t; \
00277 t ^= e_key[4 * i + 2]; e_key[4 * i + 6] = t; \
00278 t ^= e_key[4 * i + 3]; e_key[4 * i + 7] = t; \
00279 } while (0)
00280
00281 #define loop6(i) \
00282 do { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
00283 t ^= e_key[6 * (i)]; e_key[6 * (i) + 6] = t; \
00284 t ^= e_key[6 * (i) + 1]; e_key[6 * (i) + 7] = t; \
00285 t ^= e_key[6 * (i) + 2]; e_key[6 * (i) + 8] = t; \
00286 t ^= e_key[6 * (i) + 3]; e_key[6 * (i) + 9] = t; \
00287 t ^= e_key[6 * (i) + 4]; e_key[6 * (i) + 10] = t; \
00288 t ^= e_key[6 * (i) + 5]; e_key[6 * (i) + 11] = t; \
00289 } while (0)
00290
00291 #define loop8(i) \
00292 do { t = ls_box(rotr(t, 8)) ^ rco_tab[i]; \
00293 t ^= e_key[8 * (i)]; e_key[8 * (i) + 8] = t; \
00294 t ^= e_key[8 * (i) + 1]; e_key[8 * (i) + 9] = t; \
00295 t ^= e_key[8 * (i) + 2]; e_key[8 * (i) + 10] = t; \
00296 t ^= e_key[8 * (i) + 3]; e_key[8 * (i) + 11] = t; \
00297 t = e_key[8 * (i) + 4] ^ ls_box(t); \
00298 e_key[8 * (i) + 12] = t; \
00299 t ^= e_key[8 * (i) + 5]; e_key[8 * (i) + 13] = t; \
00300 t ^= e_key[8 * (i) + 6]; e_key[8 * (i) + 14] = t; \
00301 t ^= e_key[8 * (i) + 7]; e_key[8 * (i) + 15] = t; \
00302 } while (0)
00303
00304 rijndael_ctx *
00305 rijndael_set_key(rijndael_ctx *ctx, const u4byte *in_key, const u4byte key_len,
00306 int encrypt)
00307 {
00308 u4byte i,
00309 t,
00310 u,
00311 v,
00312 w;
00313 u4byte *e_key = ctx->e_key;
00314 u4byte *d_key = ctx->d_key;
00315
00316 ctx->decrypt = !encrypt;
00317
00318 if (!tab_gen)
00319 gen_tabs();
00320
00321 ctx->k_len = (key_len + 31) / 32;
00322
00323 e_key[0] = io_swap(in_key[0]);
00324 e_key[1] = io_swap(in_key[1]);
00325 e_key[2] = io_swap(in_key[2]);
00326 e_key[3] = io_swap(in_key[3]);
00327
00328 switch (ctx->k_len)
00329 {
00330 case 4:
00331 t = e_key[3];
00332 for (i = 0; i < 10; ++i)
00333 loop4(i);
00334 break;
00335
00336 case 6:
00337 e_key[4] = io_swap(in_key[4]);
00338 t = e_key[5] = io_swap(in_key[5]);
00339 for (i = 0; i < 8; ++i)
00340 loop6(i);
00341 break;
00342
00343 case 8:
00344 e_key[4] = io_swap(in_key[4]);
00345 e_key[5] = io_swap(in_key[5]);
00346 e_key[6] = io_swap(in_key[6]);
00347 t = e_key[7] = io_swap(in_key[7]);
00348 for (i = 0; i < 7; ++i)
00349 loop8(i);
00350 break;
00351 }
00352
00353 if (!encrypt)
00354 {
00355 d_key[0] = e_key[0];
00356 d_key[1] = e_key[1];
00357 d_key[2] = e_key[2];
00358 d_key[3] = e_key[3];
00359
00360 for (i = 4; i < 4 * ctx->k_len + 24; ++i)
00361 imix_col(d_key[i], e_key[i]);
00362 }
00363
00364 return ctx;
00365 }
00366
00367
00368
00369 #define f_nround(bo, bi, k) \
00370 do { \
00371 f_rn(bo, bi, 0, k); \
00372 f_rn(bo, bi, 1, k); \
00373 f_rn(bo, bi, 2, k); \
00374 f_rn(bo, bi, 3, k); \
00375 k += 4; \
00376 } while (0)
00377
00378 #define f_lround(bo, bi, k) \
00379 do { \
00380 f_rl(bo, bi, 0, k); \
00381 f_rl(bo, bi, 1, k); \
00382 f_rl(bo, bi, 2, k); \
00383 f_rl(bo, bi, 3, k); \
00384 } while (0)
00385
00386 void
00387 rijndael_encrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
00388 {
00389 u4byte k_len = ctx->k_len;
00390 u4byte *e_key = ctx->e_key;
00391 u4byte b0[4],
00392 b1[4],
00393 *kp;
00394
00395 b0[0] = io_swap(in_blk[0]) ^ e_key[0];
00396 b0[1] = io_swap(in_blk[1]) ^ e_key[1];
00397 b0[2] = io_swap(in_blk[2]) ^ e_key[2];
00398 b0[3] = io_swap(in_blk[3]) ^ e_key[3];
00399
00400 kp = e_key + 4;
00401
00402 if (k_len > 6)
00403 {
00404 f_nround(b1, b0, kp);
00405 f_nround(b0, b1, kp);
00406 }
00407
00408 if (k_len > 4)
00409 {
00410 f_nround(b1, b0, kp);
00411 f_nround(b0, b1, kp);
00412 }
00413
00414 f_nround(b1, b0, kp);
00415 f_nround(b0, b1, kp);
00416 f_nround(b1, b0, kp);
00417 f_nround(b0, b1, kp);
00418 f_nround(b1, b0, kp);
00419 f_nround(b0, b1, kp);
00420 f_nround(b1, b0, kp);
00421 f_nround(b0, b1, kp);
00422 f_nround(b1, b0, kp);
00423 f_lround(b0, b1, kp);
00424
00425 out_blk[0] = io_swap(b0[0]);
00426 out_blk[1] = io_swap(b0[1]);
00427 out_blk[2] = io_swap(b0[2]);
00428 out_blk[3] = io_swap(b0[3]);
00429 }
00430
00431
00432
00433 #define i_nround(bo, bi, k) \
00434 do { \
00435 i_rn(bo, bi, 0, k); \
00436 i_rn(bo, bi, 1, k); \
00437 i_rn(bo, bi, 2, k); \
00438 i_rn(bo, bi, 3, k); \
00439 k -= 4; \
00440 } while (0)
00441
00442 #define i_lround(bo, bi, k) \
00443 do { \
00444 i_rl(bo, bi, 0, k); \
00445 i_rl(bo, bi, 1, k); \
00446 i_rl(bo, bi, 2, k); \
00447 i_rl(bo, bi, 3, k); \
00448 } while (0)
00449
00450 void
00451 rijndael_decrypt(rijndael_ctx *ctx, const u4byte *in_blk, u4byte *out_blk)
00452 {
00453 u4byte b0[4],
00454 b1[4],
00455 *kp;
00456 u4byte k_len = ctx->k_len;
00457 u4byte *e_key = ctx->e_key;
00458 u4byte *d_key = ctx->d_key;
00459
00460 b0[0] = io_swap(in_blk[0]) ^ e_key[4 * k_len + 24];
00461 b0[1] = io_swap(in_blk[1]) ^ e_key[4 * k_len + 25];
00462 b0[2] = io_swap(in_blk[2]) ^ e_key[4 * k_len + 26];
00463 b0[3] = io_swap(in_blk[3]) ^ e_key[4 * k_len + 27];
00464
00465 kp = d_key + 4 * (k_len + 5);
00466
00467 if (k_len > 6)
00468 {
00469 i_nround(b1, b0, kp);
00470 i_nround(b0, b1, kp);
00471 }
00472
00473 if (k_len > 4)
00474 {
00475 i_nround(b1, b0, kp);
00476 i_nround(b0, b1, kp);
00477 }
00478
00479 i_nround(b1, b0, kp);
00480 i_nround(b0, b1, kp);
00481 i_nround(b1, b0, kp);
00482 i_nround(b0, b1, kp);
00483 i_nround(b1, b0, kp);
00484 i_nround(b0, b1, kp);
00485 i_nround(b1, b0, kp);
00486 i_nround(b0, b1, kp);
00487 i_nround(b1, b0, kp);
00488 i_lround(b0, b1, kp);
00489
00490 out_blk[0] = io_swap(b0[0]);
00491 out_blk[1] = io_swap(b0[1]);
00492 out_blk[2] = io_swap(b0[2]);
00493 out_blk[3] = io_swap(b0[3]);
00494 }
00495
00496
00497
00498
00499
00500
00501
00502
00503 void
00504 aes_set_key(rijndael_ctx *ctx, const uint8 *key, unsigned keybits, int enc)
00505 {
00506 uint32 *k;
00507
00508 k = (uint32 *) key;
00509 rijndael_set_key(ctx, k, keybits, enc);
00510 }
00511
00512 void
00513 aes_ecb_encrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
00514 {
00515 unsigned bs = 16;
00516 uint32 *d;
00517
00518 while (len >= bs)
00519 {
00520 d = (uint32 *) data;
00521 rijndael_encrypt(ctx, d, d);
00522
00523 len -= bs;
00524 data += bs;
00525 }
00526 }
00527
00528 void
00529 aes_ecb_decrypt(rijndael_ctx *ctx, uint8 *data, unsigned len)
00530 {
00531 unsigned bs = 16;
00532 uint32 *d;
00533
00534 while (len >= bs)
00535 {
00536 d = (uint32 *) data;
00537 rijndael_decrypt(ctx, d, d);
00538
00539 len -= bs;
00540 data += bs;
00541 }
00542 }
00543
00544 void
00545 aes_cbc_encrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
00546 {
00547 uint32 *iv = (uint32 *) iva;
00548 uint32 *d = (uint32 *) data;
00549 unsigned bs = 16;
00550
00551 while (len >= bs)
00552 {
00553 d[0] ^= iv[0];
00554 d[1] ^= iv[1];
00555 d[2] ^= iv[2];
00556 d[3] ^= iv[3];
00557
00558 rijndael_encrypt(ctx, d, d);
00559
00560 iv = d;
00561 d += bs / 4;
00562 len -= bs;
00563 }
00564 }
00565
00566 void
00567 aes_cbc_decrypt(rijndael_ctx *ctx, uint8 *iva, uint8 *data, unsigned len)
00568 {
00569 uint32 *d = (uint32 *) data;
00570 unsigned bs = 16;
00571 uint32 buf[4],
00572 iv[4];
00573
00574 memcpy(iv, iva, bs);
00575 while (len >= bs)
00576 {
00577 buf[0] = d[0];
00578 buf[1] = d[1];
00579 buf[2] = d[2];
00580 buf[3] = d[3];
00581
00582 rijndael_decrypt(ctx, buf, d);
00583
00584 d[0] ^= iv[0];
00585 d[1] ^= iv[1];
00586 d[2] ^= iv[2];
00587 d[3] ^= iv[3];
00588
00589 iv[0] = buf[0];
00590 iv[1] = buf[1];
00591 iv[2] = buf[2];
00592 iv[3] = buf[3];
00593 d += 4;
00594 len -= bs;
00595 }
00596 }
00597
00598
00599
00600
00601
00602
00603
00604
00605 #ifdef PRINT_TABS
00606
00607 static void
00608 show256u8(char *name, uint8 *data)
00609 {
00610 int i;
00611
00612 printf("static const u1byte %s[256] = {\n ", name);
00613 for (i = 0; i < 256;)
00614 {
00615 printf("%u", pow_tab[i++]);
00616 if (i < 256)
00617 printf(i % 16 ? ", " : ",\n ");
00618 }
00619 printf("\n};\n\n");
00620 }
00621
00622
00623 static void
00624 show4x256u32(char *name, uint32 data[4][256])
00625 {
00626 int i,
00627 j;
00628
00629 printf("static const u4byte %s[4][256] = {\n{\n ", name);
00630 for (i = 0; i < 4; i++)
00631 {
00632 for (j = 0; j < 256;)
00633 {
00634 printf("0x%08x", data[i][j]);
00635 j++;
00636 if (j < 256)
00637 printf(j % 4 ? ", " : ",\n ");
00638 }
00639 printf(i < 3 ? "\n}, {\n " : "\n}\n");
00640 }
00641 printf("};\n\n");
00642 }
00643
00644 int
00645 main()
00646 {
00647 int i;
00648 char *hdr = "/* Generated by rijndael.c */\n\n";
00649
00650 gen_tabs();
00651
00652 printf(hdr);
00653 show256u8("pow_tab", pow_tab);
00654 show256u8("log_tab", log_tab);
00655 show256u8("sbx_tab", sbx_tab);
00656 show256u8("isb_tab", isb_tab);
00657
00658 show4x256u32("ft_tab", ft_tab);
00659 show4x256u32("it_tab", it_tab);
00660 #ifdef LARGE_TABLES
00661 show4x256u32("fl_tab", fl_tab);
00662 show4x256u32("il_tab", il_tab);
00663 #endif
00664 printf("static const u4byte rco_tab[10] = {\n ");
00665 for (i = 0; i < 10; i++)
00666 {
00667 printf("0x%08x", rco_tab[i]);
00668 if (i < 9)
00669 printf(", ");
00670 if (i == 4)
00671 printf("\n ");
00672 }
00673 printf("\n};\n\n");
00674 return 0;
00675 }
00676
00677 #endif