chiark / gitweb /
Add some more vectors, and a whinge about how Skipjack test vectors are.
[catacomb] / md4.c
1 /* -*-c-*-
2  *
3  * $Id: md4.c,v 1.3 2000/06/17 11:31:43 mdw Exp $
4  *
5  * The MD4 message digest function
6  *
7  * (c) 1999 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
30 /*----- Revision history --------------------------------------------------* 
31  *
32  * $Log: md4.c,v $
33  * Revision 1.3  2000/06/17 11:31:43  mdw
34  * Portability fix for broken compilers.
35  *
36  * Revision 1.2  1999/12/10 23:20:03  mdw
37  * New hash interface requirements.
38  *
39  * Revision 1.1  1999/09/03 08:41:12  mdw
40  * Initial import.
41  *
42  */
43
44 /*----- Header files ------------------------------------------------------*/
45
46 #include <mLib/bits.h>
47
48 #include "ghash.h"
49 #include "ghash-def.h"
50 #include "hash.h"
51 #include "md4.h"
52
53 /*----- Main code ---------------------------------------------------------*/
54
55 /* --- @md4_compress@ --- *
56  *
57  * Arguments:   @md4_ctx *ctx@ = pointer to context block
58  *              @const void *sbuf@ = pointer to buffer of appropriate size
59  *
60  * Returns:     ---
61  *
62  * Use:         RIPEMD-160 compression function.
63  */
64
65 void md4_compress(md4_ctx *ctx, const void *sbuf)
66 {
67   uint32 a, b, c, d;
68   uint32 buf[16];
69
70   /* --- Fetch the chaining variables --- */
71
72   a = ctx->a;
73   b = ctx->b;
74   c = ctx->c;
75   d = ctx->d;
76
77   /* --- Fetch the buffer contents --- */
78
79   {
80     int i;
81     const octet *p;
82
83     for (i = 0, p = sbuf; i < 16; i++, p += 4)
84       buf[i] = LOAD32_L(p);
85   }
86
87   /* --- Definitions for round functions --- */
88
89 #define F(x, y, z) (((x) & (y)) | (~(x) & (z)))
90 #define G(x, y, z) (((x) & (y)) | ((x) & (z)) | ((y) & (z)))
91 #define H(x, y, z) ((x) ^ (y) ^ (z))
92
93 #define T(w, x, y, z, i, r, k, f) do {                                  \
94   uint32 _t = w + f(x, y, z) + buf[i] + k;                              \
95   w = ROL32(_t, r);                                                     \
96 } while (0)
97
98 #define FF(w, x, y, z, i, r) T(w, x, y, z, i, r, 0x00000000, F)
99 #define GG(w, x, y, z, i, r) T(w, x, y, z, i, r, 0x5a827999, G)
100 #define HH(w, x, y, z, i, r) T(w, x, y, z, i, r, 0x6ed9eba1, H)
101
102   /* --- The main compression function --- */
103
104   FF(a, b, c, d,  0,  3);
105   FF(d, a, b, c,  1,  7);
106   FF(c, d, a, b,  2, 11);
107   FF(b, c, d, a,  3, 19);
108   FF(a, b, c, d,  4,  3);
109   FF(d, a, b, c,  5,  7);
110   FF(c, d, a, b,  6, 11);
111   FF(b, c, d, a,  7, 19);
112   FF(a, b, c, d,  8,  3);
113   FF(d, a, b, c,  9,  7);
114   FF(c, d, a, b, 10, 11);
115   FF(b, c, d, a, 11, 19);
116   FF(a, b, c, d, 12,  3);
117   FF(d, a, b, c, 13,  7);
118   FF(c, d, a, b, 14, 11);
119   FF(b, c, d, a, 15, 19);
120
121   GG(a, b, c, d,  0,  3);
122   GG(d, a, b, c,  4,  5);
123   GG(c, d, a, b,  8,  9);
124   GG(b, c, d, a, 12, 13);
125   GG(a, b, c, d,  1,  3);
126   GG(d, a, b, c,  5,  5);
127   GG(c, d, a, b,  9,  9);
128   GG(b, c, d, a, 13, 13);
129   GG(a, b, c, d,  2,  3);
130   GG(d, a, b, c,  6,  5);
131   GG(c, d, a, b, 10,  9);
132   GG(b, c, d, a, 14, 13);
133   GG(a, b, c, d,  3,  3);
134   GG(d, a, b, c,  7,  5);
135   GG(c, d, a, b, 11,  9);
136   GG(b, c, d, a, 15, 13);
137
138   HH(a, b, c, d,  0,  3);
139   HH(d, a, b, c,  8,  9);
140   HH(c, d, a, b,  4, 11);
141   HH(b, c, d, a, 12, 15);
142   HH(a, b, c, d,  2,  3);
143   HH(d, a, b, c, 10,  9);
144   HH(c, d, a, b,  6, 11);
145   HH(b, c, d, a, 14, 15);
146   HH(a, b, c, d,  1,  3);
147   HH(d, a, b, c,  9,  9);
148   HH(c, d, a, b,  5, 11);
149   HH(b, c, d, a, 13, 15);
150   HH(a, b, c, d,  3,  3);
151   HH(d, a, b, c, 11,  9);
152   HH(c, d, a, b,  7, 11);
153   HH(b, c, d, a, 15, 15);
154
155   /* --- Update the chaining variables --- */
156
157   ctx->a += a;
158   ctx->b += b;
159   ctx->c += c;
160   ctx->d += d;
161 }
162
163 /* --- @md4_init@ --- *
164  *
165  * Arguments:   @md4_ctx *ctx@ = pointer to context block to initialize
166  *
167  * Returns:     ---
168  *
169  * Use:         Initializes a context block ready for hashing.
170  */
171
172 void md4_init(md4_ctx *ctx)
173 {
174   ctx->a = 0x67452301;
175   ctx->b = 0xefcdab89;
176   ctx->c = 0x98badcfe;
177   ctx->d = 0x10325476;
178   ctx->off = 0;
179   ctx->nl = ctx->nh = 0;
180 }
181
182 /* --- @md4_set@ --- *
183  *
184  * Arguments:   @md4_ctx *ctx@ = pointer to context block
185  *              @const void *buf@ = pointer to state buffer
186  *              @unsigned long count@ = current count of bytes processed
187  *
188  * Returns:     ---
189  *
190  * Use:         Initializes a context block from a given state.  This is
191  *              useful in cases where the initial hash state is meant to be
192  *              secret, e.g., for NMAC and HMAC support.
193  */
194
195 void md4_set(md4_ctx *ctx, const void *buf, unsigned long count)
196 {
197   const octet *p = buf;
198   ctx->a = LOAD32_L(p +  0);
199   ctx->b = LOAD32_L(p +  4);
200   ctx->c = LOAD32_L(p +  8);
201   ctx->d = LOAD32_L(p + 12);
202   ctx->off = 0;
203   ctx->nl = U32(count);
204   ctx->nh = U32(((count & ~MASK32) >> 16) >> 16);
205 }
206
207 /* --- @md4_hash@ --- *
208  *
209  * Arguments:   @md4_ctx *ctx@ = pointer to context block
210  *              @const void *buf@ = buffer of data to hash
211  *              @size_t sz@ = size of buffer to hash
212  *
213  * Returns:     ---
214  *
215  * Use:         Hashes a buffer of data.  The buffer may be of any size and
216  *              alignment.
217  */
218
219 void md4_hash(md4_ctx *ctx, const void *buf, size_t sz)
220 {
221   HASH_BUFFER(MD4, md4, ctx, buf, sz);
222 }
223
224 /* --- @md4_done@ --- *
225  *
226  * Arguments:   @md4_ctx *ctx@ = pointer to context block
227  *              @void *hash@ = pointer to output buffer
228  *
229  * Returns:     ---
230  *
231  * Use:         Returns the hash of the data read so far.
232  */
233
234 void md4_done(md4_ctx *ctx, void *hash)
235 {
236   octet *p = hash;
237   HASH_MD5STRENGTH(MD4, md4, ctx);
238   STORE32_L(p +  0, ctx->a);
239   STORE32_L(p +  4, ctx->b);
240   STORE32_L(p +  8, ctx->c);
241   STORE32_L(p + 12, ctx->d);
242 }
243
244 /* --- @md4_state@ --- *
245  *
246  * Arguments:   @md4_ctx *ctx@ = pointer to context
247  *              @void *state@ = pointer to buffer for current state
248  *
249  * Returns:     Number of bytes written to the hash function so far.
250  *
251  * Use:         Returns the current state of the hash function such that
252  *              it can be passed to @md4_set@.
253  */
254
255 unsigned long md4_state(md4_ctx *ctx, void *state)
256 {
257   octet *p = state;
258   STORE32_L(p +  0, ctx->a);
259   STORE32_L(p +  4, ctx->b);
260   STORE32_L(p +  8, ctx->c);
261   STORE32_L(p + 12, ctx->d);
262   return (ctx->nl | ((ctx->nh >> 16) >> 16));
263 }
264
265 /* --- Generic interface --- */
266
267 GHASH_DEF(MD4, md4)
268
269 /* --- Test rig --- */
270
271 HASH_TEST(MD4, md4)
272
273 /*----- That's all, folks -------------------------------------------------*/