#include <freeradius-devel/ident.h>
RCSID("$Id$")
#include <freeradius-devel/autoconf.h>
#include "../include/md4.h"
void fr_md4_calc(output, input, inlen)
unsigned char *output;
const unsigned char *input;
unsigned int inlen;
{
FR_MD4_CTX context;
fr_MD4Init(&context);
fr_MD4Update(&context, input, inlen);
fr_MD4Final(output, &context);
}
#ifndef WITH_OPENSSL_MD4
#ifndef WORDS_BIGENDIAN
#define htole32_4(buf)
#define htole32_14(buf)
#define htole32_16(buf)
#else
#define htole32(x) \
(((((uint32_t)x) & 0xff000000) >> 24) | \
((((uint32_t)x) & 0x00ff0000) >> 8) | \
((((uint32_t)x) & 0x0000ff00) << 8) | \
((((uint32_t)x) & 0x000000ff) << 24))
#define htole32_4(buf) do { \
(buf)[ 0] = htole32((buf)[ 0]); \
(buf)[ 1] = htole32((buf)[ 1]); \
(buf)[ 2] = htole32((buf)[ 2]); \
(buf)[ 3] = htole32((buf)[ 3]); \
} while (0)
#define htole32_14(buf) do { \
(buf)[ 0] = htole32((buf)[ 0]); \
(buf)[ 1] = htole32((buf)[ 1]); \
(buf)[ 2] = htole32((buf)[ 2]); \
(buf)[ 3] = htole32((buf)[ 3]); \
(buf)[ 4] = htole32((buf)[ 4]); \
(buf)[ 5] = htole32((buf)[ 5]); \
(buf)[ 6] = htole32((buf)[ 6]); \
(buf)[ 7] = htole32((buf)[ 7]); \
(buf)[ 8] = htole32((buf)[ 8]); \
(buf)[ 9] = htole32((buf)[ 9]); \
(buf)[10] = htole32((buf)[10]); \
(buf)[11] = htole32((buf)[11]); \
(buf)[12] = htole32((buf)[12]); \
(buf)[13] = htole32((buf)[13]); \
} while (0)
#define htole32_16(buf) do { \
(buf)[ 0] = htole32((buf)[ 0]); \
(buf)[ 1] = htole32((buf)[ 1]); \
(buf)[ 2] = htole32((buf)[ 2]); \
(buf)[ 3] = htole32((buf)[ 3]); \
(buf)[ 4] = htole32((buf)[ 4]); \
(buf)[ 5] = htole32((buf)[ 5]); \
(buf)[ 6] = htole32((buf)[ 6]); \
(buf)[ 7] = htole32((buf)[ 7]); \
(buf)[ 8] = htole32((buf)[ 8]); \
(buf)[ 9] = htole32((buf)[ 9]); \
(buf)[10] = htole32((buf)[10]); \
(buf)[11] = htole32((buf)[11]); \
(buf)[12] = htole32((buf)[12]); \
(buf)[13] = htole32((buf)[13]); \
(buf)[14] = htole32((buf)[14]); \
(buf)[15] = htole32((buf)[15]); \
} while (0)
#endif
void
fr_MD4Init(FR_MD4_CTX *ctx)
{
ctx->count[0] = 0;
ctx->count[1] = 0;
ctx->state[0] = 0x67452301;
ctx->state[1] = 0xefcdab89;
ctx->state[2] = 0x98badcfe;
ctx->state[3] = 0x10325476;
}
void
fr_MD4Update(FR_MD4_CTX *ctx, const unsigned char *buf, size_t len)
{
uint32_t count;
count = (uint32_t)((ctx->count[0] >> 3) & 0x3f);
if ((ctx->count[0] += ((uint32_t)len << 3)) < (uint32_t)len) {
ctx->count[1]++;
}
ctx->count[1] += ((uint32_t)len >> 29);
if (count) {
unsigned char *p = (unsigned char *)ctx->buffer + count;
count = MD4_BLOCK_LENGTH - count;
if (len < count) {
memcpy(p, buf, len);
return;
}
memcpy(p, buf, count);
htole32_16((uint32_t *)ctx->buffer);
fr_MD4Transform(ctx->state, ctx->buffer);
buf += count;
len -= count;
}
while (len >= MD4_BLOCK_LENGTH) {
memcpy(ctx->buffer, buf, MD4_BLOCK_LENGTH);
htole32_16((uint32_t *)ctx->buffer);
fr_MD4Transform(ctx->state, ctx->buffer);
buf += MD4_BLOCK_LENGTH;
len -= MD4_BLOCK_LENGTH;
}
memcpy(ctx->buffer, buf, len);
}
void
fr_MD4Final(unsigned char digest[MD4_DIGEST_LENGTH], FR_MD4_CTX *ctx)
{
uint32_t count;
unsigned char *p;
count = (uint32_t)(ctx->count[0] >> 3) & 0x3f;
p = ctx->buffer + count;
*p++ = 0x80;
count = 64 - 1 - count;
if (count < 8) {
memset(p, 0, count);
htole32_16((uint32_t *)ctx->buffer);
fr_MD4Transform(ctx->state, ctx->buffer);
memset(ctx->buffer, 0, 56);
} else {
memset(p, 0, count - 8);
}
htole32_14((uint32_t *)ctx->buffer);
((uint32_t *)ctx->buffer)[14] = ctx->count[0];
((uint32_t *)ctx->buffer)[15] = ctx->count[1];
fr_MD4Transform(ctx->state, ctx->buffer);
htole32_4(ctx->state);
memcpy(digest, ctx->state, MD4_DIGEST_LENGTH);
memset(ctx, 0, sizeof(*ctx));
}
#define F1(x, y, z) (z ^ (x & (y ^ z)))
#define F2(x, y, z) ((x & y) | (x & z) | (y & z))
#define F3(x, y, z) (x ^ y ^ z)
#define MD4STEP(f, w, x, y, z, data, s) \
( w += f(x, y, z) + data, w = w<<s | w>>(32-s) )
void
fr_MD4Transform(uint32_t buf[4], const unsigned char inc[MD4_BLOCK_LENGTH])
{
uint32_t a, b, c, d;
const uint32_t *in = (const uint32_t *)inc;
a = buf[0];
b = buf[1];
c = buf[2];
d = buf[3];
MD4STEP(F1, a, b, c, d, in[ 0], 3);
MD4STEP(F1, d, a, b, c, in[ 1], 7);
MD4STEP(F1, c, d, a, b, in[ 2], 11);
MD4STEP(F1, b, c, d, a, in[ 3], 19);
MD4STEP(F1, a, b, c, d, in[ 4], 3);
MD4STEP(F1, d, a, b, c, in[ 5], 7);
MD4STEP(F1, c, d, a, b, in[ 6], 11);
MD4STEP(F1, b, c, d, a, in[ 7], 19);
MD4STEP(F1, a, b, c, d, in[ 8], 3);
MD4STEP(F1, d, a, b, c, in[ 9], 7);
MD4STEP(F1, c, d, a, b, in[10], 11);
MD4STEP(F1, b, c, d, a, in[11], 19);
MD4STEP(F1, a, b, c, d, in[12], 3);
MD4STEP(F1, d, a, b, c, in[13], 7);
MD4STEP(F1, c, d, a, b, in[14], 11);
MD4STEP(F1, b, c, d, a, in[15], 19);
MD4STEP(F2, a, b, c, d, in[ 0] + 0x5a827999, 3);
MD4STEP(F2, d, a, b, c, in[ 4] + 0x5a827999, 5);
MD4STEP(F2, c, d, a, b, in[ 8] + 0x5a827999, 9);
MD4STEP(F2, b, c, d, a, in[12] + 0x5a827999, 13);
MD4STEP(F2, a, b, c, d, in[ 1] + 0x5a827999, 3);
MD4STEP(F2, d, a, b, c, in[ 5] + 0x5a827999, 5);
MD4STEP(F2, c, d, a, b, in[ 9] + 0x5a827999, 9);
MD4STEP(F2, b, c, d, a, in[13] + 0x5a827999, 13);
MD4STEP(F2, a, b, c, d, in[ 2] + 0x5a827999, 3);
MD4STEP(F2, d, a, b, c, in[ 6] + 0x5a827999, 5);
MD4STEP(F2, c, d, a, b, in[10] + 0x5a827999, 9);
MD4STEP(F2, b, c, d, a, in[14] + 0x5a827999, 13);
MD4STEP(F2, a, b, c, d, in[ 3] + 0x5a827999, 3);
MD4STEP(F2, d, a, b, c, in[ 7] + 0x5a827999, 5);
MD4STEP(F2, c, d, a, b, in[11] + 0x5a827999, 9);
MD4STEP(F2, b, c, d, a, in[15] + 0x5a827999, 13);
MD4STEP(F3, a, b, c, d, in[ 0] + 0x6ed9eba1, 3);
MD4STEP(F3, d, a, b, c, in[ 8] + 0x6ed9eba1, 9);
MD4STEP(F3, c, d, a, b, in[ 4] + 0x6ed9eba1, 11);
MD4STEP(F3, b, c, d, a, in[12] + 0x6ed9eba1, 15);
MD4STEP(F3, a, b, c, d, in[ 2] + 0x6ed9eba1, 3);
MD4STEP(F3, d, a, b, c, in[10] + 0x6ed9eba1, 9);
MD4STEP(F3, c, d, a, b, in[ 6] + 0x6ed9eba1, 11);
MD4STEP(F3, b, c, d, a, in[14] + 0x6ed9eba1, 15);
MD4STEP(F3, a, b, c, d, in[ 1] + 0x6ed9eba1, 3);
MD4STEP(F3, d, a, b, c, in[ 9] + 0x6ed9eba1, 9);
MD4STEP(F3, c, d, a, b, in[ 5] + 0x6ed9eba1, 11);
MD4STEP(F3, b, c, d, a, in[13] + 0x6ed9eba1, 15);
MD4STEP(F3, a, b, c, d, in[ 3] + 0x6ed9eba1, 3);
MD4STEP(F3, d, a, b, c, in[11] + 0x6ed9eba1, 9);
MD4STEP(F3, c, d, a, b, in[ 7] + 0x6ed9eba1, 11);
MD4STEP(F3, b, c, d, a, in[15] + 0x6ed9eba1, 15);
buf[0] += a;
buf[1] += b;
buf[2] += c;
buf[3] += d;
}
#endif