3 * Generate the MARS S-box table
5 * (c) 2001 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
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.
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.
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,
28 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/bits.h>
39 /*----- SHA-1 (quick version) ---------------------------------------------*/
41 /* --- @sha_compress@ --- *
43 * Arguments: @sha_ctx *ctx@ = pointer to context block
44 * @const void *sbuf@ = pointer to buffer of appropriate size
48 * Use: SHA-1 compression function.
51 void sha_compress(sha_ctx *ctx, const void *sbuf)
56 /* --- Fetch the chaining variables --- */
64 /* --- Fetch and expand the buffer contents --- */
70 for (i = 0, p = sbuf; i < 16; i++, p += 4)
72 for (i = 16; i < 80; i++) {
73 uint32 x = buf[i - 3] ^ buf[i - 8] ^ buf[i - 14] ^ buf[i - 16];
78 /* --- Definitions for round functions --- */
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)))
84 #define T(v, w, x, y, z, i, f, k) do { \
86 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
88 _x = v; v = z; z = y; y = x; x = w; w = _x; \
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)
96 /* --- The main compression function --- */
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);
117 /* --- @sha_init@ --- *
119 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
123 * Use: Initializes a context block ready for hashing.
126 void sha_init(sha_ctx *ctx)
134 ctx->nl = ctx->nh = 0;
137 /* --- @sha_hash@ --- *
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
145 * Use: Hashes a buffer of data. The buffer may be of any size and
149 void sha_hash(sha_ctx *ctx, const void *buf, size_t sz)
151 sha_ctx *_bctx = (ctx);
153 const octet *_bbuf = (octet *) (buf);
156 uint32 _l = ((uint32) ((_bsz) & MASK32));
157 uint32 _h = ((_bsz & ~MASK32) >> 16) >> 16;
160 if (_bctx->nl < _l || _bctx->nl & ~MASK32)
163 if (_bctx->off + _bsz < SHA_BUFSZ) {
164 memcpy(_bctx->buf + _bctx->off, _bbuf, _bsz);
168 size_t s = SHA_BUFSZ - _bctx->off;
169 memcpy(_bctx->buf + _bctx->off, _bbuf, s);
170 sha_compress(_bctx, _bctx->buf);
174 while (_bsz >= SHA_BUFSZ) {
175 sha_compress(_bctx, _bbuf);
180 memcpy(_bctx->buf, _bbuf, _bsz);
185 /* --- @sha_done@ --- *
187 * Arguments: @sha_ctx *ctx@ = pointer to context block
188 * @void *hash@ = pointer to output buffer
192 * Use: Returns the hash of the data read so far.
195 void sha_done(sha_ctx *ctx, void *hash)
199 sha_ctx *_pctx = (ctx);
200 _pctx->buf[_pctx->off] = 0x80;
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);
208 memset(_pctx->buf + _pctx->off, 0, SHA_BUFSZ - _pctx->off - 8);
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);
220 /*----- Main code ---------------------------------------------------------*/
222 static void mks(uint32 c3, uint32 *s)
224 octet ibuf[16], obuf[20];
228 STORE32_L(ibuf + 4, 0xb7e15162);
229 STORE32_L(ibuf + 8, 0x243f6a88);
230 STORE32_L(ibuf + 12, c3);
232 for (i = 0; i < 510; i += 5) {
235 sha_hash(&h, ibuf, sizeof(ibuf));
237 for (j = 0; j < 5; j++)
238 *s++ = LOAD32_L(obuf + (4 * j));
242 sha_hash(&h, ibuf, sizeof(ibuf));
244 for (j = 0; i < 512; j++, i++)
245 *s++ = LOAD32_L(obuf + (4 * j));
248 static void fix(uint32 *s)
253 for (i = 0; i < 512; i++) {
254 for (j = i & ~255u; j < ((i + 255) & ~255u); j++) {
259 if (!(d & 0xff000000)) n++;
260 if (!(d & 0x00ff0000)) n++;
261 if (!(d & 0x0000ff00)) n++;
262 if (!(d & 0x000000ff)) n++;
264 s[i] = U32(s[i] * 3);
283 * MARS tables [generated]\n\
286 #ifndef CATACOMB_MARS_TAB_H\n\
287 #define CATACOMB_MARS_TAB_H\n\
291 /* --- The S-box --- */\n\
293 #define MARS_S { \\\n\
295 for (i = 0; i < 512; i++) {
296 printf("0x%08lx", (unsigned long)s[i]);
298 fputs(" \\\n}\n\n", stdout);
300 fputs(", \\\n ", stdout);
307 if (fclose(stdout)) {
308 fprintf(stderr, "error writing data\n");
315 /*----- That's all, folks -------------------------------------------------*/