35682d2f |
1 | /* -*-c-*- |
2 | * |
35682d2f |
3 | * Build precomputed tables for the Square block cipher |
4 | * |
5 | * (c) 2000 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
35682d2f |
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. |
45c0fd36 |
16 | * |
35682d2f |
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. |
45c0fd36 |
21 | * |
35682d2f |
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 | |
35682d2f |
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 |
ba74e11e |
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. |
35682d2f |
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 |
45c0fd36 |
85 | * algorithm. |
35682d2f |
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 | #ifndef CATACOMB_SQUARE_TAB_H\n\ |
232 | #define CATACOMB_SQUARE_TAB_H\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 | #define SQUARE_S { \\\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 | #define SQUARE_SI { \\\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 | #define SQUARE_T { \\\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\ |
282 | \\\n\ |
283 | { ", stdout); |
284 | } else if (i % 4 == 3) |
45c0fd36 |
285 | fputs(", \\\n ", stdout); |
35682d2f |
286 | else |
287 | fputs(", ", stdout); |
288 | } |
45c0fd36 |
289 | } |
35682d2f |
290 | |
291 | fputs("\ |
292 | #define SQUARE_TI { \\\n\ |
293 | { ", stdout); |
294 | for (j = 0; j < 4; j++) { |
295 | for (i = 0; i < 256; i++) { |
296 | printf("0x%08x", ti[j][i]); |
297 | if (i == 255) { |
298 | if (j == 3) |
299 | fputs(" } \\\n}\n\n", stdout); |
300 | else |
301 | fputs(" }, \\\n\ |
302 | \\\n\ |
303 | { ", stdout); |
304 | } else if (i % 4 == 3) |
45c0fd36 |
305 | fputs(", \\\n ", stdout); |
35682d2f |
306 | else |
307 | fputs(", ", stdout); |
308 | } |
309 | } |
310 | |
311 | /* --- Write out the big u tables --- */ |
312 | |
313 | ubox(); |
314 | fputs("\ |
315 | /* --- The key schedule tables --- */\n\ |
316 | \n\ |
317 | #define SQUARE_U { \\\n\ |
318 | { ", stdout); |
319 | for (j = 0; j < 4; j++) { |
320 | for (i = 0; i < 256; i++) { |
321 | printf("0x%08x", u[j][i]); |
322 | if (i == 255) { |
323 | if (j == 3) |
324 | fputs(" } \\\n}\n\n", stdout); |
325 | else |
326 | fputs(" }, \\\n\ |
327 | \\\n\ |
328 | { ", stdout); |
329 | } else if (i % 4 == 3) |
45c0fd36 |
330 | fputs(", \\\n ", stdout); |
35682d2f |
331 | else |
332 | fputs(", ", stdout); |
333 | } |
45c0fd36 |
334 | } |
35682d2f |
335 | |
336 | /* --- Round constants --- */ |
337 | |
338 | rcon(); |
339 | fputs("\ |
340 | /* --- The round constants --- */\n\ |
341 | \n\ |
342 | #define SQUARE_RCON { \\\n\ |
343 | ", stdout); |
344 | for (i = 0; i < sizeof(rc); i++) { |
345 | printf("0x%02x", rc[i]); |
346 | if (i == sizeof(rc) - 1) |
347 | fputs(" \\\n}\n\n", stdout); |
348 | else if (i % 8 == 7) |
349 | fputs(", \\\n ", stdout); |
350 | else |
351 | fputs(", ", stdout); |
45c0fd36 |
352 | } |
35682d2f |
353 | |
354 | /* --- Done --- */ |
355 | |
356 | puts("#endif"); |
357 | |
358 | if (fclose(stdout)) { |
359 | fprintf(stderr, "error writing data\n"); |
360 | exit(EXIT_FAILURE); |
361 | } |
362 | |
363 | return (0); |
364 | } |
365 | |
366 | /*----- That's all, folks -------------------------------------------------*/ |