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