3 * $Id: mars-mktab.c,v 1.2 2004/04/08 01:36:15 mdw Exp $
5 * Generate the MARS S-box table
7 * (c) 2001 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Header files ------------------------------------------------------*/
37 #include <mLib/bits.h>
41 /*----- SHA-1 (quick version) ---------------------------------------------*/
43 /* --- @sha_compress@ --- *
45 * Arguments: @sha_ctx *ctx@ = pointer to context block
46 * @const void *sbuf@ = pointer to buffer of appropriate size
50 * Use: SHA-1 compression function.
53 void sha_compress(sha_ctx *ctx, const void *sbuf)
58 /* --- Fetch the chaining variables --- */
66 /* --- Fetch and expand the buffer contents --- */
72 for (i = 0, p = sbuf; i < 16; i++, p += 4)
74 for (i = 16; i < 80; i++) {
75 uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16];
80 /* --- Definitions for round functions --- */
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)))
86 #define T(v, w, x, y, z, i, f, k) do { \
88 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
90 _x = v; v = z; z = y; y = x; x = w; w = _x; \
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)
98 /* --- The main compression function --- */
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);
119 /* --- @sha_init@ --- *
121 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
125 * Use: Initializes a context block ready for hashing.
128 void sha_init(sha_ctx *ctx)
136 ctx->nl = ctx->nh = 0;
139 /* --- @sha_hash@ --- *
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
147 * Use: Hashes a buffer of data. The buffer may be of any size and
151 void sha_hash(sha_ctx *ctx, const void *buf, size_t sz)
153 sha_ctx *_bctx = (ctx);
155 const octet *_bbuf = (octet *) (buf);
158 uint32 _l = ((uint32) ((_bsz) & MASK32));
159 uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16;
162 if (_bctx->nl < _l || _bctx->nl & ~MASK32)
165 if (_bctx->off + _bsz < SHA_BUFSZ) {
166 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz);
170 size_t s = SHA_BUFSZ - _bctx->off;
171 memcpy(_bctx->buf + _bctx->off, _bbuf, s);
172 sha_compress(_bctx, _bctx->buf);
176 while (_bsz >= SHA_BUFSZ) {
177 sha_compress(_bctx, _bbuf);
182 memcpy(_bctx->buf, _bbuf, _bsz);
187 /* --- @sha_done@ --- *
189 * Arguments: @sha_ctx *ctx@ = pointer to context block
190 * @void *hash@ = pointer to output buffer
194 * Use: Returns the hash of the data read so far.
197 void sha_done(sha_ctx *ctx, void *hash)
201 sha_ctx *_pctx = (ctx);
202 _pctx->buf[_pctx->off] = 0x80;
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);
210 memset(_pctx->buf + _pctx->off, 0, SHA_BUFSZ - _pctx->off - 8);
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);
222 /*----- Main code ---------------------------------------------------------*/
224 static void mks(uint32 c3, uint32 *s)
226 octet ibuf[16], obuf[20];
230 STORE32_L(ibuf + 4, 0xb7e15162);
231 STORE32_L(ibuf + 8, 0x243f6a88);
232 STORE32_L(ibuf + 12, c3);
234 for (i = 0; i < 510; i += 5) {
237 sha_hash(&h, ibuf, sizeof(ibuf));
239 for (j = 0; j < 5; j++)
240 *s++ = LOAD32_L(obuf + (4 * j));
244 sha_hash(&h, ibuf, sizeof(ibuf));
246 for (j = 0; i < 512; j++, i++)
247 *s++ = LOAD32_L(obuf + (4 * j));
250 static void fix(uint32 *s)
255 for (i = 0; i < 512; i++) {
256 for (j = i & ~255u; j < ((i + 255) & ~255u); j++) {
261 if (!(d & 0xff000000)) n++;
262 if (!(d & 0x00ff0000)) n++;
263 if (!(d & 0x0000ff00)) n++;
264 if (!(d & 0x000000ff)) n++;
266 s[i] = U32(s[i] * 3);
285 * MARS tables [generated]\n\
288 #ifndef CATACOMB_MARS_TAB_H\n\
289 #define CATACOMB_MARS_TAB_H\n\
293 /* --- The S-box --- */\n\
295 #define MARS_S { \\\n\
297 for (i = 0; i < 512; i++) {
298 printf("0x%08lx", (unsigned long)s[i]);
300 fputs(" \\\n}\n\n", stdout);
302 fputs(", \\\n ", stdout);
309 if (fclose(stdout)) {
310 fprintf(stderr, "error writing data\n");
317 /*----- That's all, folks -------------------------------------------------*/