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