3bef8c14 |
1 | /* -*-c-*- |
3bef8c14 |
2 | * |
3 | * Generate the MARS S-box table |
4 | * |
5 | * (c) 2001 Straylight/Edgeware |
6 | */ |
7 | |
45c0fd36 |
8 | /*----- Licensing notice --------------------------------------------------* |
3bef8c14 |
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 | * |
3bef8c14 |
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 | * |
3bef8c14 |
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 | |
3bef8c14 |
28 | /*----- Header files ------------------------------------------------------*/ |
29 | |
30 | #include <assert.h> |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <string.h> |
34 | |
35 | #include <mLib/bits.h> |
36 | |
37 | #include "sha.h" |
38 | |
39 | /*----- SHA-1 (quick version) ---------------------------------------------*/ |
40 | |
41 | /* --- @sha_compress@ --- * |
42 | * |
43 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
44 | * @const void *sbuf@ = pointer to buffer of appropriate size |
45 | * |
46 | * Returns: --- |
47 | * |
48 | * Use: SHA-1 compression function. |
49 | */ |
50 | |
51 | void sha_compress(sha_ctx *ctx, const void *sbuf) |
52 | { |
53 | uint32 a, b, c, d, e; |
54 | uint32 buf[80]; |
55 | |
56 | /* --- Fetch the chaining variables --- */ |
57 | |
58 | a = ctx->a; |
59 | b = ctx->b; |
60 | c = ctx->c; |
61 | d = ctx->d; |
62 | e = ctx->e; |
63 | |
64 | /* --- Fetch and expand the buffer contents --- */ |
65 | |
66 | { |
67 | int i; |
68 | const octet *p; |
69 | |
70 | for (i = 0, p = sbuf; i < 16; i++, p += 4) |
71 | buf[i] = LOAD32(p); |
72 | for (i = 16; i < 80; i++) { |
73 | uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16]; |
74 | buf[i] = ROL32(x, 1); |
75 | } |
76 | } |
77 | |
78 | /* --- Definitions for round functions --- */ |
79 | |
80 | #define F(x, y, z) (((x) & (y)) | (~(x) & (z))) |
81 | #define G(x, y, z) ((x) ^ (y) ^ (z)) |
82 | #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z))) |
83 | |
84 | #define T(v, w, x, y, z, i, f, k) do { \ |
85 | uint32 _x; \ |
86 | z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \ |
87 | w = ROR32(w, 2); \ |
88 | _x = v; v = z; z = y; y = x; x = w; w = _x; \ |
89 | } while (0) |
90 | |
91 | #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999) |
92 | #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1) |
93 | #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc) |
94 | #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6) |
95 | |
96 | /* --- The main compression function --- */ |
97 | |
98 | { |
99 | unsigned i; |
100 | for (i = 0; i < 20; i++) |
101 | FF(a, b, c, d, e, i); |
102 | for (i = 20; i < 40; i++) |
103 | GG(a, b, c, d, e, i); |
104 | for (i = 40; i < 60; i++) |
105 | HH(a, b, c, d, e, i); |
106 | for (i = 60; i < 80; i++) |
107 | II(a, b, c, d, e, i); |
108 | } |
109 | |
110 | ctx->a += a; |
111 | ctx->b += b; |
112 | ctx->c += c; |
113 | ctx->d += d; |
114 | ctx->e += e; |
115 | } |
116 | |
117 | /* --- @sha_init@ --- * |
118 | * |
119 | * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize |
120 | * |
121 | * Returns: --- |
122 | * |
123 | * Use: Initializes a context block ready for hashing. |
124 | */ |
125 | |
126 | void sha_init(sha_ctx *ctx) |
127 | { |
128 | ctx->a = 0x67452301; |
129 | ctx->b = 0xefcdab89; |
130 | ctx->c = 0x98badcfe; |
131 | ctx->d = 0x10325476; |
132 | ctx->e = 0xc3d2e1f0; |
133 | ctx->off = 0; |
134 | ctx->nl = ctx->nh = 0; |
135 | } |
136 | |
137 | /* --- @sha_hash@ --- * |
138 | * |
139 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
140 | * @const void *buf@ = buffer of data to hash |
141 | * @size_t sz@ = size of buffer to hash |
142 | * |
143 | * Returns: --- |
144 | * |
145 | * Use: Hashes a buffer of data. The buffer may be of any size and |
146 | * alignment. |
147 | */ |
148 | |
149 | void sha_hash(sha_ctx *ctx, const void *buf, size_t sz) |
150 | { |
151 | sha_ctx *_bctx = (ctx); |
152 | size_t _bsz = (sz); |
153 | const octet *_bbuf = (octet *) (buf); |
154 | |
155 | { |
156 | uint32 _l = ((uint32) ((_bsz) & MASK32)); |
157 | uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16; |
158 | _bctx->nh += _h; |
159 | _bctx->nl += _l; |
160 | if (_bctx->nl < _l || _bctx->nl & ~MASK32) |
161 | _bctx->nh++; |
162 | } |
163 | if (_bctx->off + _bsz < SHA_BUFSZ) { |
164 | memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz); |
165 | _bctx->off += _bsz; |
166 | } else { |
167 | if (_bctx->off) { |
168 | size_t s = SHA_BUFSZ - _bctx->off; |
169 | memcpy(_bctx->buf + _bctx->off, _bbuf, s); |
170 | sha_compress(_bctx, _bctx->buf); |
171 | _bsz -= s; |
172 | _bbuf += s; |
173 | } |
174 | while (_bsz >= SHA_BUFSZ) { |
175 | sha_compress(_bctx, _bbuf); |
176 | _bsz -= SHA_BUFSZ; |
177 | _bbuf += SHA_BUFSZ; |
178 | } |
179 | if (_bsz) |
180 | memcpy(_bctx->buf, _bbuf, _bsz); |
181 | _bctx->off = _bsz; |
182 | } |
183 | } |
184 | |
185 | /* --- @sha_done@ --- * |
186 | * |
187 | * Arguments: @sha_ctx *ctx@ = pointer to context block |
188 | * @void *hash@ = pointer to output buffer |
189 | * |
190 | * Returns: --- |
191 | * |
192 | * Use: Returns the hash of the data read so far. |
193 | */ |
194 | |
195 | void sha_done(sha_ctx *ctx, void *hash) |
196 | { |
197 | octet *p = hash; |
198 | { |
199 | sha_ctx *_pctx = (ctx); |
200 | _pctx->buf[_pctx->off] = 0x80; |
201 | _pctx->off++; |
202 | if (_pctx->off > SHA_BUFSZ - 8) { |
203 | if (_pctx->off < SHA_BUFSZ) |
204 | memset(_pctx->buf + _pctx->off, 0, SHA_BUFSZ - _pctx->off); |
205 | sha_compress(_pctx, _pctx->buf); |
206 | memset(_pctx->buf, 0, SHA_BUFSZ - 8); |
207 | } else |
208 | memset(_pctx->buf + _pctx->off, 0, SHA_BUFSZ - _pctx->off - 8); |
209 | } |
210 | STORE32(ctx->buf + SHA_BUFSZ - 8, (ctx->nl >> 29) | (ctx->nh << 3)); |
211 | STORE32(ctx->buf + SHA_BUFSZ - 4, ctx->nl << 3); |
212 | sha_compress(ctx, ctx->buf); |
213 | STORE32(p + 0, ctx->a); |
214 | STORE32(p + 4, ctx->b); |
215 | STORE32(p + 8, ctx->c); |
216 | STORE32(p + 12, ctx->d); |
217 | STORE32(p + 16, ctx->e); |
218 | } |
219 | |
220 | /*----- Main code ---------------------------------------------------------*/ |
221 | |
222 | static void mks(uint32 c3, uint32 *s) |
223 | { |
224 | octet ibuf[16], obuf[20]; |
225 | sha_ctx h; |
226 | unsigned i, j; |
227 | |
228 | STORE32_L(ibuf + 4, 0xb7e15162); |
229 | STORE32_L(ibuf + 8, 0x243f6a88); |
230 | STORE32_L(ibuf + 12, c3); |
231 | |
232 | for (i = 0; i < 510; i += 5) { |
233 | STORE32_L(ibuf, i); |
234 | sha_init(&h); |
235 | sha_hash(&h, ibuf, sizeof(ibuf)); |
236 | sha_done(&h, obuf); |
237 | for (j = 0; j < 5; j++) |
238 | *s++ = LOAD32_L(obuf + (4 * j)); |
239 | } |
240 | STORE32_L(ibuf, i); |
241 | sha_init(&h); |
242 | sha_hash(&h, ibuf, sizeof(ibuf)); |
243 | sha_done(&h, obuf); |
244 | for (j = 0; i < 512; j++, i++) |
245 | *s++ = LOAD32_L(obuf + (4 * j)); |
246 | } |
247 | |
248 | static void fix(uint32 *s) |
249 | { |
250 | unsigned i, j, n; |
251 | uint32 d; |
252 | |
253 | for (i = 0; i < 512; i++) { |
254 | for (j = i & ~255u; j < ((i + 255) & ~255u); j++) { |
255 | if (i == j) |
256 | continue; |
257 | d = s[i] ^ s[j]; |
258 | n = 0; |
259 | if (!(d & 0xff000000)) n++; |
260 | if (!(d & 0x00ff0000)) n++; |
261 | if (!(d & 0x0000ff00)) n++; |
262 | if (!(d & 0x000000ff)) n++; |
263 | if (n >= 2) { |
264 | s[i] = U32(s[i] * 3); |
265 | goto fixed; |
266 | } |
267 | } |
268 | fixed:; |
269 | } |
270 | } |
271 | |
272 | int main(void) |
273 | { |
274 | uint32 s[512]; |
275 | unsigned i; |
276 | |
277 | mks(0x02917d59, s); |
278 | fix(s); |
279 | |
280 | puts("\ |
281 | /* -*-c-*-\n\ |
282 | *\n\ |
283 | * MARS tables [generated]\n\ |
284 | */\n\ |
285 | \n\ |
e5b61a8d |
286 | #include <mLib/bits.h>\n\ |
3bef8c14 |
287 | "); |
288 | |
289 | fputs("\ |
290 | /* --- The S-box --- */\n\ |
291 | \n\ |
e5b61a8d |
292 | const uint32 mars_s[] = {\n\ |
3bef8c14 |
293 | ", stdout); |
294 | for (i = 0; i < 512; i++) { |
295 | printf("0x%08lx", (unsigned long)s[i]); |
296 | if (i == 511) |
e5b61a8d |
297 | fputs("\n};\n", stdout); |
3bef8c14 |
298 | else if (i % 4 == 3) |
e5b61a8d |
299 | fputs(",\n ", stdout); |
3bef8c14 |
300 | else |
301 | fputs(", ", stdout); |
302 | } |
303 | |
3bef8c14 |
304 | if (fclose(stdout)) { |
305 | fprintf(stderr, "error writing data\n"); |
306 | exit(EXIT_FAILURE); |
307 | } |
308 | |
45c0fd36 |
309 | return (0); |
3bef8c14 |
310 | } |
311 | |
312 | /*----- That's all, folks -------------------------------------------------*/ |