chiark / gitweb /
rand/rand.c: Mix the pool key in `rand_gate' and `rand_stretch'.
[catacomb] / symm / square-mktab.c
1 /* -*-c-*-
2  *
3  * Build precomputed tables for the Square block cipher
4  *
5  * (c) 2000 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
12  * Catacomb is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU Library General Public License as
14  * published by the Free Software Foundation; either version 2 of the
15  * License, or (at your option) any later version.
16  *
17  * Catacomb is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU Library General Public License for more details.
21  *
22  * You should have received a copy of the GNU Library General Public
23  * License along with Catacomb; if not, write to the Free
24  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33
34 #include <mLib/bits.h>
35
36 /*----- Magic variables ---------------------------------------------------*/
37
38 static octet s[256], si[256];
39 static uint32 t[4][256], ti[4][256];
40 static uint32 u[4][256];
41 static octet rc[32];
42
43 /*----- Main code ---------------------------------------------------------*/
44
45 /* --- @mul@ --- *
46  *
47  * Arguments:   @unsigned x, y@ = polynomials over %$\gf{2^8}$%
48  *              @unsigned m@ = modulus
49  *
50  * Returns:     The product of two polynomials.
51  *
52  * Use:         Computes a product of polynomials, quite slowly.
53  */
54
55 static unsigned mul(unsigned x, unsigned y, unsigned m)
56 {
57   unsigned a = 0;
58   unsigned i;
59
60   for (i = 0; i < 8; i++) {
61     if (y & 1)
62       a ^= x;
63     y >>= 1;
64     x <<= 1;
65     if (x & 0x100)
66       x ^= m;
67   }
68
69   return (a);
70 }
71
72 /* --- @sbox@ --- *
73  *
74  * Build the S-box.
75  *
76  * This is built from inversion in the multiplicative group of
77  * %$\gf{2^8}[x]/(p(x))$%, where %$p(x) = x^8+x^7+x^6+x^5+x^4+x^2+1$%,
78  * followed by an affine transformation treating inputs as vectors over
79  * %$\gf{2}$%.  The result is a horrible function.
80  *
81  * The inversion is done slightly sneakily, by building log and antilog
82  * tables.  Let %$a$% be an element of the finite field.  If the inverse of
83  * %$a$% is %$a^{-1}$%, then %$\log a a^{-1} = 0$%.  Hence
84  * %$\log a = -\log a^{-1}$%.  This saves fiddling about with Euclidean
85  * algorithm.
86  */
87
88 #define S_MOD 0x1f5
89
90 static void sbox(void)
91 {
92   octet log[256], alog[256];
93   unsigned x;
94   unsigned i;
95   unsigned g;
96
97   /* --- Find a suitable generator, and build log tables --- */
98
99   log[0] = 0;
100   for (g = 2; g < 256; g++) {
101     x = 1;
102     for (i = 0; i < 256; i++) {
103       log[x] = i;
104       alog[i] = x;
105       x = mul(x, g, S_MOD);
106       if (x == 1 && i != 254)
107         goto again;
108     }
109     goto done;
110   again:;
111   }
112   fprintf(stderr, "couldn't find generator\n");
113   exit(EXIT_FAILURE);
114 done:;
115
116   /* --- Now grind through and do the affine transform --- *
117    *
118    * The matrix multiply is an AND and a parity op.  The add is an XOR.
119    */
120
121   for (i = 0; i < 256; i++) {
122     unsigned j;
123     octet m[] = { 0xd6, 0x7b, 0x3d, 0x1f, 0x0f, 0x05, 0x03, 0x01 };
124     unsigned v = i ? alog[255 - log[i]] : 0;
125
126     assert(i == 0 || mul(i, v, S_MOD) == 1);
127
128     x = 0;
129     for (j = 0; j < 8; j++) {
130       unsigned r;
131       r = v & m[j];
132       r = (r >> 4) ^ r;
133       r = (r >> 2) ^ r;
134       r = (r >> 1) ^ r;
135       x = (x << 1) | (r & 1);
136     }
137     x ^= 0xb1;
138     s[i] = x;
139     si[x] = i;
140   }
141 }
142
143 /* --- @tbox@ --- *
144  *
145  * Construct the t tables for doing the round function efficiently.
146  */
147
148 static void tbox(void)
149 {
150   unsigned i;
151
152   for (i = 0; i < 256; i++) {
153     uint32 a, b, c, d;
154     uint32 w;
155
156     /* --- Build a forwards t-box entry --- */
157
158     a = s[i];
159     b = a << 1; if (b & 0x100) b ^= S_MOD;
160     c = a ^ b;
161     w = (b << 0) | (a << 8) | (a << 16) | (c << 24);
162     t[0][i] = w;
163     t[1][i] = ROL32(w, 8);
164     t[2][i] = ROL32(w, 16);
165     t[3][i] = ROL32(w, 24);
166
167     /* --- Build a backwards t-box entry --- */
168
169     a = mul(si[i], 0x0e, S_MOD);
170     b = mul(si[i], 0x09, S_MOD);
171     c = mul(si[i], 0x0d, S_MOD);
172     d = mul(si[i], 0x0b, S_MOD);
173     w = (a << 0) | (b << 8) | (c << 16) | (d << 24);
174     ti[0][i] = w;
175     ti[1][i] = ROL32(w, 8);
176     ti[2][i] = ROL32(w, 16);
177     ti[3][i] = ROL32(w, 24);
178   }
179 }
180
181 /* --- @ubox@ --- *
182  *
183  * Construct the tables for performing the key schedule.
184  */
185
186 static void ubox(void)
187 {
188   unsigned i;
189
190   for (i = 0; i < 256; i++) {
191     uint32 a, b, c;
192     uint32 w;
193     a = i;
194     b = a << 1; if (b & 0x100) b ^= S_MOD;
195     c = a ^ b;
196     w = (b << 0) | (a << 8) | (a << 16) | (c << 24);
197     u[0][i] = w;
198     u[1][i] = ROL32(w, 8);
199     u[2][i] = ROL32(w, 16);
200     u[3][i] = ROL32(w, 24);
201   }
202 }
203
204 /* --- Round constants --- */
205
206 void rcon(void)
207 {
208   unsigned r = 1;
209   int i;
210
211   for (i = 0; i < sizeof(rc); i++) {
212     rc[i] = r;
213     r <<= 1;
214     if (r & 0x100)
215       r ^= S_MOD;
216   }
217 }
218
219 /* --- @main@ --- */
220
221 int main(void)
222 {
223   int i, j;
224
225   puts("\
226 /* -*-c-*-\n\
227  *\n\
228  * Square tables [generated]\n\
229  */\n\
230 \n\
231 #include <mLib/bits.h>\n\
232 \n\
233 ");
234
235   /* --- Write out the S-box --- */
236
237   sbox();
238   fputs("\
239 /* --- The byte substitution and its inverse --- */\n\
240 \n\
241 const octet square_s[256] = {\n\
242   ", stdout);
243   for (i = 0; i < 256; i++) {
244     printf("0x%02x", s[i]);
245     if (i == 255)
246       fputs("\n};\n\n", stdout);
247     else if (i % 8 == 7)
248       fputs(",\n  ", stdout);
249     else
250       fputs(", ", stdout);
251   }
252
253   fputs("\
254 const octet square_si[256] = {\n\
255   ", stdout);
256   for (i = 0; i < 256; i++) {
257     printf("0x%02x", si[i]);
258     if (i == 255)
259       fputs("\n};\n\n", stdout);
260     else if (i % 8 == 7)
261       fputs(",\n  ", stdout);
262     else
263       fputs(", ", stdout);
264   }
265
266   /* --- Write out the big t tables --- */
267
268   tbox();
269   fputs("\
270 /* --- The big round tables --- */\n\
271 \n\
272 const uint32 square_t[4][256] = {\n\
273   { ", stdout);
274   for (j = 0; j < 4; j++) {
275     for (i = 0; i < 256; i++) {
276       printf("0x%08x", t[j][i]);
277       if (i == 255) {
278         if (j == 3)
279           fputs(" }\n};\n\n", stdout);
280         else
281           fputs(" },\n\n  { ", stdout);
282       } else if (i % 4 == 3)
283         fputs(",\n    ", stdout);
284       else
285         fputs(", ", stdout);
286     }
287   }
288
289   fputs("\
290 const uint32 square_ti[4][256] = {\n\
291   { ", stdout);
292   for (j = 0; j < 4; j++) {
293     for (i = 0; i < 256; i++) {
294       printf("0x%08x", ti[j][i]);
295       if (i == 255) {
296         if (j == 3)
297           fputs(" }\n};\n\n", stdout);
298         else
299           fputs(" },\n\n  { ", stdout);
300       } else if (i % 4 == 3)
301         fputs(",\n    ", stdout);
302       else
303         fputs(", ", stdout);
304     }
305   }
306
307   /* --- Write out the big u tables --- */
308
309   ubox();
310   fputs("\
311 /* --- The key schedule tables --- */\n\
312 \n\
313 const uint32 square_u[4][256] = {\n\
314   { ", stdout);
315   for (j = 0; j < 4; j++) {
316     for (i = 0; i < 256; i++) {
317       printf("0x%08x", u[j][i]);
318       if (i == 255) {
319         if (j == 3)
320           fputs(" }\n};\n\n", stdout);
321         else
322           fputs(" },\n\n  { ", stdout);
323       } else if (i % 4 == 3)
324         fputs(",\n    ", stdout);
325       else
326         fputs(", ", stdout);
327     }
328   }
329
330   /* --- Round constants --- */
331
332   rcon();
333   fputs("\
334 /* --- The round constants --- */\n\
335 \n\
336 const octet square_rcon[32] = {\n\
337   ", stdout);
338   for (i = 0; i < sizeof(rc); i++) {
339     printf("0x%02x", rc[i]);
340     if (i == sizeof(rc) - 1)
341       fputs("\n};\n", stdout);
342     else if (i % 8 == 7)
343       fputs(",\n  ", stdout);
344     else
345       fputs(", ", stdout);
346   }
347
348   /* --- Done --- */
349
350   if (fclose(stdout)) {
351     fprintf(stderr, "error writing data\n");
352     exit(EXIT_FAILURE);
353   }
354
355   return (0);
356 }
357
358 /*----- That's all, folks -------------------------------------------------*/