10 RCSID(
"$Id: 2ff492e5b5a6635b6b44b6d38437b7cba0164d3d $")
16 #include <freeradius-devel/md4.h>
33 #ifndef HAVE_OPENSSL_EVP_H
52 #ifndef WORDS_BIGENDIAN
53 # define htole32_4(buf)
54 # define htole32_14(buf)
55 # define htole32_16(buf)
60 (((((uint32_t)x) & 0xff000000) >> 24) |\
61 ((((uint32_t)x) & 0x00ff0000) >> 8) |\
62 ((((uint32_t)x) & 0x0000ff00) << 8) |\
63 ((((uint32_t)x) & 0x000000ff) << 24))
65 # define htole32_4(buf) do {\
66 (buf)[0] = htole32((buf)[0]);\
67 (buf)[1] = htole32((buf)[1]);\
68 (buf)[2] = htole32((buf)[2]);\
69 (buf)[3] = htole32((buf)[3]);\
72 # define htole32_14(buf) do {\
73 (buf)[0] = htole32((buf)[0]);\
74 (buf)[1] = htole32((buf)[1]);\
75 (buf)[2] = htole32((buf)[2]);\
76 (buf)[3] = htole32((buf)[3]);\
77 (buf)[4] = htole32((buf)[4]);\
78 (buf)[5] = htole32((buf)[5]);\
79 (buf)[6] = htole32((buf)[6]);\
80 (buf)[7] = htole32((buf)[7]);\
81 (buf)[8] = htole32((buf)[8]);\
82 (buf)[9] = htole32((buf)[9]);\
83 (buf)[10] = htole32((buf)[10]);\
84 (buf)[11] = htole32((buf)[11]);\
85 (buf)[12] = htole32((buf)[12]);\
86 (buf)[13] = htole32((buf)[13]);\
89 # define htole32_16(buf) do {\
90 (buf)[0] = htole32((buf)[0]);\
91 (buf)[1] = htole32((buf)[1]);\
92 (buf)[2] = htole32((buf)[2]);\
93 (buf)[3] = htole32((buf)[3]);\
94 (buf)[4] = htole32((buf)[4]);\
95 (buf)[5] = htole32((buf)[5]);\
96 (buf)[6] = htole32((buf)[6]);\
97 (buf)[7] = htole32((buf)[7]);\
98 (buf)[8] = htole32((buf)[8]);\
99 (buf)[9] = htole32((buf)[9]);\
100 (buf)[10] = htole32((buf)[10]);\
101 (buf)[11] = htole32((buf)[11]);\
102 (buf)[12] = htole32((buf)[12]);\
103 (buf)[13] = htole32((buf)[13]);\
104 (buf)[14] = htole32((buf)[14]);\
105 (buf)[15] = htole32((buf)[15]);\
119 ctx->
state[0] = 0x67452301;
120 ctx->
state[1] = 0xefcdab89;
121 ctx->
state[2] = 0x98badcfe;
122 ctx->
state[3] = 0x10325476;
136 count = (uint32_t)((ctx->
count[0] >> 3) & 0x3f);
140 if ((ctx->
count[0] += ((uint32_t)inlen << 3)) < (uint32_t)inlen) {
144 ctx->
count[1] += ((uint32_t)inlen >> 29);
148 unsigned char *p = (
unsigned char *)ctx->
buffer + count;
152 memcpy(p, in, inlen);
155 memcpy(p, in, count);
172 memcpy(ctx->
buffer, in, inlen);
189 count = (uint32_t)(ctx->
count[0] >> 3) & 0x3f;
199 count = 64 - 1 - count;
209 memset(ctx->
buffer, 0, 56);
212 memset(p, 0, count - 8);
222 memcpy(out, ctx->
state, MD4_DIGEST_LENGTH);
223 memset(ctx, 0,
sizeof(*ctx));
227 #define F1(x, y, z) (z ^ (x & (y ^ z)))
228 #define F2(x, y, z) ((x & y) | (x & z) | (y & z))
229 #define F3(x, y, z) (x ^ y ^ z)
232 #define MD4STEP(f, w, x, y, z, data, s) (w += f(x, y, z) + data, w = w << s | w >> (32 - s))
246 uint32_t
const *in = (uint32_t
const *)block;
270 MD4STEP(
F2, a, b, c, d, in[ 0] + 0x5a827999, 3);
271 MD4STEP(
F2, d, a, b, c, in[ 4] + 0x5a827999, 5);
272 MD4STEP(
F2, c, d, a, b, in[ 8] + 0x5a827999, 9);
273 MD4STEP(
F2, b, c, d, a, in[12] + 0x5a827999, 13);
274 MD4STEP(
F2, a, b, c, d, in[ 1] + 0x5a827999, 3);
275 MD4STEP(
F2, d, a, b, c, in[ 5] + 0x5a827999, 5);
276 MD4STEP(
F2, c, d, a, b, in[ 9] + 0x5a827999, 9);
277 MD4STEP(
F2, b, c, d, a, in[13] + 0x5a827999, 13);
278 MD4STEP(
F2, a, b, c, d, in[ 2] + 0x5a827999, 3);
279 MD4STEP(
F2, d, a, b, c, in[ 6] + 0x5a827999, 5);
280 MD4STEP(
F2, c, d, a, b, in[10] + 0x5a827999, 9);
281 MD4STEP(
F2, b, c, d, a, in[14] + 0x5a827999, 13);
282 MD4STEP(
F2, a, b, c, d, in[ 3] + 0x5a827999, 3);
283 MD4STEP(
F2, d, a, b, c, in[ 7] + 0x5a827999, 5);
284 MD4STEP(
F2, c, d, a, b, in[11] + 0x5a827999, 9);
285 MD4STEP(
F2, b, c, d, a, in[15] + 0x5a827999, 13);
287 MD4STEP(
F3, a, b, c, d, in[ 0] + 0x6ed9eba1, 3);
288 MD4STEP(
F3, d, a, b, c, in[ 8] + 0x6ed9eba1, 9);
289 MD4STEP(
F3, c, d, a, b, in[ 4] + 0x6ed9eba1, 11);
290 MD4STEP(
F3, b, c, d, a, in[12] + 0x6ed9eba1, 15);
291 MD4STEP(
F3, a, b, c, d, in[ 2] + 0x6ed9eba1, 3);
292 MD4STEP(
F3, d, a, b, c, in[10] + 0x6ed9eba1, 9);
293 MD4STEP(
F3, c, d, a, b, in[ 6] + 0x6ed9eba1, 11);
294 MD4STEP(
F3, b, c, d, a, in[14] + 0x6ed9eba1, 15);
295 MD4STEP(
F3, a, b, c, d, in[ 1] + 0x6ed9eba1, 3);
296 MD4STEP(
F3, d, a, b, c, in[ 9] + 0x6ed9eba1, 9);
297 MD4STEP(
F3, c, d, a, b, in[ 5] + 0x6ed9eba1, 11);
298 MD4STEP(
F3, b, c, d, a, in[13] + 0x6ed9eba1, 15);
299 MD4STEP(
F3, a, b, c, d, in[ 3] + 0x6ed9eba1, 3);
300 MD4STEP(
F3, d, a, b, c, in[11] + 0x6ed9eba1, 9);
301 MD4STEP(
F3, c, d, a, b, in[ 7] + 0x6ed9eba1, 11);
302 MD4STEP(
F3, b, c, d, a, in[15] + 0x6ed9eba1, 15);
void fr_md4_update(FR_MD4_CTX *ctx, uint8_t const *in, size_t inlen)
Feed additional data into the MD4 hashing function.
#define MD4STEP(f, w, x, y, z, data, s)
uint32_t count[2]
Number of bits, mod 2^64.
void fr_md4_calc(uint8_t out[MD4_DIGEST_LENGTH], uint8_t const *in, size_t inlen)
Calculate the MD4 hash of the contents of a buffer.
void fr_md4_init(FR_MD4_CTX *ctx)
Initialise a new MD4 context.
void fr_md4_final(uint8_t out[MD4_DIGEST_LENGTH], FR_MD4_CTX *ctx)
Finalise the MD4 context and write out the hash.
void void MD4_DIGEST_LENGTH
void fr_md4_transform(uint32_t state[4], uint8_t const block[MD4_BLOCK_LENGTH])
The core of the MD4 algorithm.
uint8_t buffer[MD4_BLOCK_LENGTH]
Input buffer.