Generate precomputed tables as sources in `precomps/'.
[u/mdw/catacomb] / symm / twofish.c
index 75e7491..05e8fc5 100644 (file)
@@ -34,7 +34,6 @@
 #include "blkc.h"
 #include "gcipher.h"
 #include "twofish.h"
-#include "twofish-tab.h"
 #include "paranoia.h"
 
 /*----- Global variables --------------------------------------------------*/
@@ -43,10 +42,17 @@ const octet twofish_keysz[] = { KSZ_RANGE, TWOFISH_KEYSZ, 0, 32, 1 };
 
 /*----- Important tables --------------------------------------------------*/
 
-static const octet q0[256] = TWOFISH_Q0, q1[256] = TWOFISH_Q1;
-static const uint32 qmds[4][256] = TWOFISH_QMDS;
-static const octet rslog[] = TWOFISH_RSLOG, rsexp[] = TWOFISH_RSEXP;
-static const octet rs[32] = TWOFISH_RS;
+extern const octet twofish_q0[256], twofish_q1[256];
+extern const uint32 twofish_qmds[4][256];
+extern const octet twofish_rslog[], twofish_rsexp[];
+extern const octet twofish_rs[32];
+
+#define Q0 twofish_q0
+#define Q1 twofish_q1
+#define QMDS twofish_qmds
+#define RSLOG twofish_rslog
+#define RSEXP twofish_rsexp
+#define RS twofish_rs
 
 /*----- Key initialization ------------------------------------------------*/
 
@@ -66,18 +72,18 @@ static uint32 h(uint32 x, const uint32 *l, unsigned k)
   /* --- Apply a series of @q@ tables to an integer --- */
 
 # define Q(x, qa, qb, qc, qd)                                          \
-  ((qa[((x) >> 0) & 0xff] <<  0) |                                     \
-   (qb[((x) >> 8) & 0xff] <<  8) |                                     \
+  ((qa[((x) >>  0) & 0xff] <<  0) |                                    \
+   (qb[((x) >>  8) & 0xff] <<  8) |                                    \
    (qc[((x) >> 16) & 0xff] << 16) |                                    \
    (qd[((x) >> 24) & 0xff] << 24))
 
   /* --- Grind through the tables --- */
 
   switch (k) {
-    case 4: x = Q(x, q1, q0, q0, q1) ^ l[3];
-    case 3: x = Q(x, q1, q1, q0, q0) ^ l[2];
-    case 2: x = Q(x, q0, q1, q0, q1) ^ l[1];
-           x = Q(x, q0, q0, q1, q1) ^ l[0];
+    case 4: x = Q(x, Q1, Q0, Q0, Q1) ^ l[3];
+    case 3: x = Q(x, Q1, Q1, Q0, Q0) ^ l[2];
+    case 2: x = Q(x, Q0, Q1, Q0, Q1) ^ l[1];
+           x = Q(x, Q0, Q0, Q1, Q1) ^ l[0];
       break;
   }
 
@@ -85,8 +91,8 @@ static uint32 h(uint32 x, const uint32 *l, unsigned k)
 
   /* --- Apply the MDS matrix --- */
 
-  return (qmds[0][U8(x >>  0)] ^ qmds[1][U8(x >>  8)] ^
-         qmds[2][U8(x >> 16)] ^ qmds[3][U8(x >> 24)]);
+  return (QMDS[0][U8(x >>  0)] ^ QMDS[1][U8(x >>  8)] ^
+         QMDS[2][U8(x >> 16)] ^ QMDS[3][U8(x >> 24)]);
 }
 
 /* --- @twofish_initfk@ --- *
@@ -155,7 +161,7 @@ void twofish_initfk(twofish_ctx *k, const void *buf, size_t sz,
     q = p;
     for (i = 0; i < sz; i++) {
       octet ss[4];
-      const octet *r = rs;
+      const octet *r = RS;
       int j;
 
       /* --- Extract the easy subkeys --- */
@@ -172,7 +178,7 @@ void twofish_initfk(twofish_ctx *k, const void *buf, size_t sz,
 
        for (k = 0; k < 8; k++) {
          unsigned char x = *qq ^ fk->t1[i * 8 + k];
-         if (x) a ^= rsexp[rslog[x] + *r];
+         if (x) a ^= RSEXP[RSLOG[x] + *r];
          qq++;
          r++;
        }
@@ -219,10 +225,10 @@ void twofish_initfk(twofish_ctx *k, const void *buf, size_t sz,
   {
     unsigned i;
     static const octet *q[4][KMAX + 1] = {
-      { q1, q0, q0, q1, q1 },
-      { q0, q0, q1, q1, q0 },
-      { q1, q1, q0, q0, q0 },
-      { q0, q1, q1, q0, q1 }
+      { Q1, Q0, Q0, Q1, Q1 },
+      { Q0, Q0, Q1, Q1, Q0 },
+      { Q1, Q1, Q0, Q0, Q0 },
+      { Q0, Q1, Q1, Q0, Q1 }
     };
 
     for (i = 0; i < 4; i++) {
@@ -244,7 +250,7 @@ void twofish_initfk(twofish_ctx *k, const void *buf, size_t sz,
 
        /* --- Write it in the key schedule --- */
 
-       k->g[i][j] = qmds[i][x];
+       k->g[i][j] = QMDS[i][x];
       }
     }
   }