3 * The MD4 message digest function
5 * (c) 1999 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 ------------------------------------------------------*/
30 #include <mLib/bits.h>
33 #include "ghash-def.h"
37 /*----- Main code ---------------------------------------------------------*/
39 /* --- @md4_compress@ --- *
41 * Arguments: @md4_ctx *ctx@ = pointer to context block
42 * @const void *sbuf@ = pointer to buffer of appropriate size
46 * Use: RIPEMD-160 compression function.
49 void md4_compress(md4_ctx *ctx, const void *sbuf)
54 /* --- Fetch the chaining variables --- */
61 /* --- Fetch the buffer contents --- */
67 for (i = 0, p = sbuf; i < 16; i++, p += 4)
71 /* --- Definitions for round functions --- */
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))
77 #define T(w, x, y, z, i, r, k, f) do { \
78 uint32 _t = w + f(x, y, z) + buf[i] + k; \
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)
86 /* --- The main compression function --- */
90 FF(c, d, a, b, 2, 11);
91 FF(b, c, d, a, 3, 19);
94 FF(c, d, a, b, 6, 11);
95 FF(b, c, d, a, 7, 19);
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);
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);
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);
139 /* --- Update the chaining variables --- */
147 /* --- @md4_init@ --- *
149 * Arguments: @md4_ctx *ctx@ = pointer to context block to initialize
153 * Use: Initializes a context block ready for hashing.
156 void md4_init(md4_ctx *ctx)
163 ctx->nl = ctx->nh = 0;
166 /* --- @md4_set@ --- *
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
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.
179 void md4_set(md4_ctx *ctx, const void *buf, unsigned long count)
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);
187 ctx->nl = U32(count);
188 ctx->nh = U32(((count & ~(unsigned long)MASK32) >> 16) >> 16);
191 /* --- @md4_hash@ --- *
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
199 * Use: Hashes a buffer of data. The buffer may be of any size and
203 void md4_hash(md4_ctx *ctx, const void *buf, size_t sz)
205 HASH_BUFFER(MD4, md4, ctx, buf, sz);
208 /* --- @md4_done@ --- *
210 * Arguments: @md4_ctx *ctx@ = pointer to context block
211 * @void *hash@ = pointer to output buffer
215 * Use: Returns the hash of the data read so far.
218 void md4_done(md4_ctx *ctx, void *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);
228 /* --- @md4_state@ --- *
230 * Arguments: @md4_ctx *ctx@ = pointer to context
231 * @void *state@ = pointer to buffer for current state
233 * Returns: Number of bytes written to the hash function so far.
235 * Use: Returns the current state of the hash function such that
236 * it can be passed to @md4_set@.
239 unsigned long md4_state(md4_ctx *ctx, void *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));
249 /* --- Generic interface --- */
253 /* --- Test rig --- */
257 /*----- That's all, folks -------------------------------------------------*/