3 * $Id: mars-mktab.c,v 1.1 2001/04/29 18:11:19 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 /*----- Revision history --------------------------------------------------*
32 * $Log: mars-mktab.c,v $
33 * Revision 1.1 2001/04/29 18:11:19 mdw
34 * New block cipher MARS.
38 /*----- Header files ------------------------------------------------------*/
45 #include <mLib/bits.h>
49 /*----- SHA-1 (quick version) ---------------------------------------------*/
51 /* --- @sha_compress@ --- *
53 * Arguments: @sha_ctx *ctx@ = pointer to context block
54 * @const void *sbuf@ = pointer to buffer of appropriate size
58 * Use: SHA-1 compression function.
61 void sha_compress(sha_ctx
*ctx
, const void *sbuf
)
66 /* --- Fetch the chaining variables --- */
74 /* --- Fetch and expand the buffer contents --- */
80 for (i
= 0, p
= sbuf
; i
< 16; i
++, p
+= 4)
82 for (i
= 16; i
< 80; i
++) {
83 uint32 x
= buf
[i
- 3] ^ buf
[i
- 8] ^ buf
[i
- 14] ^ buf
[i
- 16];
88 /* --- Definitions for round functions --- */
90 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
91 #define G(x, y, z) ((x) ^ (y) ^ (z))
92 #define H(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
94 #define T(v, w, x, y, z, i, f, k) do { \
96 z = ROL32(v, 5) + f(w, x, y) + z + buf[i] + k; \
98 _x = v; v = z; z = y; y = x; x = w; w = _x; \
101 #define FF(v, w, x, y, z, i) T(v, w, x, y, z, i, F, 0x5a827999)
102 #define GG(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0x6ed9eba1)
103 #define HH(v, w, x, y, z, i) T(v, w, x, y, z, i, H, 0x8f1bbcdc)
104 #define II(v, w, x, y, z, i) T(v, w, x, y, z, i, G, 0xca62c1d6)
106 /* --- The main compression function --- */
110 for (i
= 0; i
< 20; i
++)
111 FF(a
, b
, c
, d
, e
, i
);
112 for (i
= 20; i
< 40; i
++)
113 GG(a
, b
, c
, d
, e
, i
);
114 for (i
= 40; i
< 60; i
++)
115 HH(a
, b
, c
, d
, e
, i
);
116 for (i
= 60; i
< 80; i
++)
117 II(a
, b
, c
, d
, e
, i
);
127 /* --- @sha_init@ --- *
129 * Arguments: @sha_ctx *ctx@ = pointer to context block to initialize
133 * Use: Initializes a context block ready for hashing.
136 void sha_init(sha_ctx
*ctx
)
144 ctx
->nl
= ctx
->nh
= 0;
147 /* --- @sha_hash@ --- *
149 * Arguments: @sha_ctx *ctx@ = pointer to context block
150 * @const void *buf@ = buffer of data to hash
151 * @size_t sz@ = size of buffer to hash
155 * Use: Hashes a buffer of data. The buffer may be of any size and
159 void sha_hash(sha_ctx
*ctx
, const void *buf
, size_t sz
)
161 sha_ctx
*_bctx
= (ctx
);
163 const octet
*_bbuf
= (octet
*) (buf
);
166 uint32 _l
= ((uint32
) ((_bsz
) & MASK32
));
167 uint32 _h
= ((_bsz
& ~MASK32
) >> 16) >> 16;
170 if (_bctx
->nl
< _l
|| _bctx
->nl
& ~MASK32
)
173 if (_bctx
->off
+ _bsz
< SHA_BUFSZ
) {
174 memcpy(_bctx
->buf
+ _bctx
->off
, _bbuf
, _bsz
);
178 size_t s
= SHA_BUFSZ
- _bctx
->off
;
179 memcpy(_bctx
->buf
+ _bctx
->off
, _bbuf
, s
);
180 sha_compress(_bctx
, _bctx
->buf
);
184 while (_bsz
>= SHA_BUFSZ
) {
185 sha_compress(_bctx
, _bbuf
);
190 memcpy(_bctx
->buf
, _bbuf
, _bsz
);
195 /* --- @sha_done@ --- *
197 * Arguments: @sha_ctx *ctx@ = pointer to context block
198 * @void *hash@ = pointer to output buffer
202 * Use: Returns the hash of the data read so far.
205 void sha_done(sha_ctx
*ctx
, void *hash
)
209 sha_ctx
*_pctx
= (ctx
);
210 _pctx
->buf
[_pctx
->off
] = 0x80;
212 if (_pctx
->off
> SHA_BUFSZ
- 8) {
213 if (_pctx
->off
< SHA_BUFSZ
)
214 memset(_pctx
->buf
+ _pctx
->off
, 0, SHA_BUFSZ
- _pctx
->off
);
215 sha_compress(_pctx
, _pctx
->buf
);
216 memset(_pctx
->buf
, 0, SHA_BUFSZ
- 8);
218 memset(_pctx
->buf
+ _pctx
->off
, 0, SHA_BUFSZ
- _pctx
->off
- 8);
220 STORE32(ctx
->buf
+ SHA_BUFSZ
- 8, (ctx
->nl
>> 29) | (ctx
->nh
<< 3));
221 STORE32(ctx
->buf
+ SHA_BUFSZ
- 4, ctx
->nl
<< 3);
222 sha_compress(ctx
, ctx
->buf
);
223 STORE32(p
+ 0, ctx
->a
);
224 STORE32(p
+ 4, ctx
->b
);
225 STORE32(p
+ 8, ctx
->c
);
226 STORE32(p
+ 12, ctx
->d
);
227 STORE32(p
+ 16, ctx
->e
);
230 /*----- Main code ---------------------------------------------------------*/
232 static void mks(uint32 c3
, uint32
*s
)
234 octet ibuf
[16], obuf
[20];
238 STORE32_L(ibuf
+ 4, 0xb7e15162);
239 STORE32_L(ibuf
+ 8, 0x243f6a88);
240 STORE32_L(ibuf
+ 12, c3
);
242 for (i
= 0; i
< 510; i
+= 5) {
245 sha_hash(&h
, ibuf
, sizeof(ibuf
));
247 for (j
= 0; j
< 5; j
++)
248 *s
++ = LOAD32_L(obuf
+ (4 * j
));
252 sha_hash(&h
, ibuf
, sizeof(ibuf
));
254 for (j
= 0; i
< 512; j
++, i
++)
255 *s
++ = LOAD32_L(obuf
+ (4 * j
));
258 static void fix(uint32
*s
)
263 for (i
= 0; i
< 512; i
++) {
264 for (j
= i
& ~255u; j
< ((i
+ 255) & ~255u); j
++) {
269 if (!(d
& 0xff000000)) n
++;
270 if (!(d
& 0x00ff0000)) n
++;
271 if (!(d
& 0x0000ff00)) n
++;
272 if (!(d
& 0x000000ff)) n
++;
274 s
[i
] = U32(s
[i
] * 3);
293 * MARS tables [generated]\n\
296 #ifndef CATACOMB_MARS_TAB_H\n\
297 #define CATACOMB_MARS_TAB_H\n\
301 /* --- The S-box --- */\n\
303 #define MARS_S { \\\n\
305 for (i
= 0; i
< 512; i
++) {
306 printf("0x%08lx", (unsigned long)s
[i
]);
308 fputs(" \\\n}\n\n", stdout
);
310 fputs(", \\\n ", stdout
);
317 if (fclose(stdout
)) {
318 fprintf(stderr
, "error writing data\n");
325 /*----- That's all, folks -------------------------------------------------*/