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