#include <sys/types.h>
#include <sys/systm.h>
#include <libkern/crypto/md5.h>
#define memset(x, y, z) bzero(x, z);
#define memcpy(x, y, z) bcopy(y, x, z)
#if defined(__i386__) || defined(__x86_64__)
#define FETCH_32(p) (*(const u_int32_t *)(p))
#else
#define FETCH_32(p) \
(((u_int32_t)*((const u_int8_t *)(p))) | \
(((u_int32_t)*((const u_int8_t *)(p) + 1)) << 8) | \
(((u_int32_t)*((const u_int8_t *)(p) + 2)) << 16) | \
(((u_int32_t)*((const u_int8_t *)(p) + 3)) << 24))
#endif
static void
Encode(unsigned char *output, u_int32_t *input, unsigned int len)
{
unsigned int i, j;
for (i = 0, j = 0; j < len; i++, j += 4) {
#if defined(__i386__) || defined(__x86_64__)
*(u_int32_t *)(output + j) = input[i];
#else
output[j] = input[i] & 0xff;
output[j + 1] = (input[i] >> 8) & 0xff;
output[j + 2] = (input[i] >> 16) & 0xff;
output[j + 3] = (input[i] >> 24) & 0xff;
#endif
}
}
static unsigned char PADDING[64] = { 0x80, };
#define F(x, y, z) ((((y) ^ (z)) & (x)) ^ (z))
#define G(x, y, z) ((((x) ^ (y)) & (z)) ^ (y))
#define H(x, y, z) ((x) ^ (y) ^ (z))
#define I(x, y, z) (((~(z)) | (x)) ^ (y))
#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32 - (n))))
#define FF(a, b, c, d, x, s, ac) { \
(a) += F((b), (c), (d)) + (x) + (unsigned long long)(ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
#define GG(a, b, c, d, x, s, ac) { \
(a) += G((b), (c), (d)) + (x) + (unsigned long long)(ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
#define HH(a, b, c, d, x, s, ac) { \
(a) += H((b), (c), (d)) + (x) + (unsigned long long)(ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
#define II(a, b, c, d, x, s, ac) { \
(a) += I((b), (c), (d)) + (x) + (unsigned long long)(ac); \
(a) = ROTATE_LEFT((a), (s)); \
(a) += (b); \
}
static void MD5Transform(u_int32_t, u_int32_t, u_int32_t, u_int32_t,
const u_int8_t [64], MD5_CTX *);
void
MD5Init(MD5_CTX *context)
{
context->count[0] = context->count[1] = 0;
context->state[0] = 0x67452301UL;
context->state[1] = 0xefcdab89UL;
context->state[2] = 0x98badcfeUL;
context->state[3] = 0x10325476UL;
}
void
MD5Update(MD5_CTX *context, const void *inpp, unsigned int inputLen)
{
u_int32_t i, index, partLen;
const unsigned char *input = (const unsigned char *)inpp;
index = (context->count[0] >> 3) & 0x3F;
if ((context->count[0] += (inputLen << 3)) < (inputLen << 3))
context->count[1]++;
context->count[1] += (inputLen >> 29);
partLen = 64 - index;
i = 0;
if (inputLen >= partLen) {
if (index != 0) {
memcpy(&context->buffer[index], input, partLen);
MD5Transform(context->state[0], context->state[1],
context->state[2], context->state[3],
context->buffer, context);
i = partLen;
}
for (; i + 63 < inputLen; i += 64)
MD5Transform(context->state[0], context->state[1],
context->state[2], context->state[3],
&input[i], context);
if (inputLen == i)
return;
index = 0;
}
memcpy(&context->buffer[index], &input[i], inputLen - i);
}
void
MD5Final(unsigned char digest[MD5_DIGEST_LENGTH], MD5_CTX *context)
{
unsigned char bits[8];
u_int32_t index = (context->count[0] >> 3) & 0x3f;
Encode(bits, context->count, 8);
MD5Update(context, PADDING, ((index < 56) ? 56 : 120) - index);
MD5Update(context, bits, 8);
Encode(digest, context->state, 16);
memset(context, 0, sizeof (*context));
}
static void
MD5Transform(u_int32_t a, u_int32_t b, u_int32_t c, u_int32_t d,
const u_int8_t block[64], MD5_CTX *context)
{
register u_int32_t x0, x1, x2, x3, x4, x5, x6, x7;
register u_int32_t x8, x9, x10, x11, x12, x13, x14, x15;
x15 = FETCH_32(block + 60);
x14 = FETCH_32(block + 56);
x13 = FETCH_32(block + 52);
x12 = FETCH_32(block + 48);
x11 = FETCH_32(block + 44);
x10 = FETCH_32(block + 40);
x9 = FETCH_32(block + 36);
x8 = FETCH_32(block + 32);
x7 = FETCH_32(block + 28);
x6 = FETCH_32(block + 24);
x5 = FETCH_32(block + 20);
x4 = FETCH_32(block + 16);
x3 = FETCH_32(block + 12);
x2 = FETCH_32(block + 8);
x1 = FETCH_32(block + 4);
x0 = FETCH_32(block + 0);
#define S11 7
#define S12 12
#define S13 17
#define S14 22
FF(a, b, c, d, x0, S11, 0xd76aa478UL);
FF(d, a, b, c, x1, S12, 0xe8c7b756UL);
FF(c, d, a, b, x2, S13, 0x242070dbUL);
FF(b, c, d, a, x3, S14, 0xc1bdceeeUL);
FF(a, b, c, d, x4, S11, 0xf57c0fafUL);
FF(d, a, b, c, x5, S12, 0x4787c62aUL);
FF(c, d, a, b, x6, S13, 0xa8304613UL);
FF(b, c, d, a, x7, S14, 0xfd469501UL);
FF(a, b, c, d, x8, S11, 0x698098d8UL);
FF(d, a, b, c, x9, S12, 0x8b44f7afUL);
FF(c, d, a, b, x10, S13, 0xffff5bb1UL);
FF(b, c, d, a, x11, S14, 0x895cd7beUL);
FF(a, b, c, d, x12, S11, 0x6b901122UL);
FF(d, a, b, c, x13, S12, 0xfd987193UL);
FF(c, d, a, b, x14, S13, 0xa679438eUL);
FF(b, c, d, a, x15, S14, 0x49b40821UL);
#define S21 5
#define S22 9
#define S23 14
#define S24 20
GG(a, b, c, d, x1, S21, 0xf61e2562UL);
GG(d, a, b, c, x6, S22, 0xc040b340UL);
GG(c, d, a, b, x11, S23, 0x265e5a51UL);
GG(b, c, d, a, x0, S24, 0xe9b6c7aaUL);
GG(a, b, c, d, x5, S21, 0xd62f105dUL);
GG(d, a, b, c, x10, S22, 0x02441453UL);
GG(c, d, a, b, x15, S23, 0xd8a1e681UL);
GG(b, c, d, a, x4, S24, 0xe7d3fbc8UL);
GG(a, b, c, d, x9, S21, 0x21e1cde6UL);
GG(d, a, b, c, x14, S22, 0xc33707d6UL);
GG(c, d, a, b, x3, S23, 0xf4d50d87UL);
GG(b, c, d, a, x8, S24, 0x455a14edUL);
GG(a, b, c, d, x13, S21, 0xa9e3e905UL);
GG(d, a, b, c, x2, S22, 0xfcefa3f8UL);
GG(c, d, a, b, x7, S23, 0x676f02d9UL);
GG(b, c, d, a, x12, S24, 0x8d2a4c8aUL);
#define S31 4
#define S32 11
#define S33 16
#define S34 23
HH(a, b, c, d, x5, S31, 0xfffa3942UL);
HH(d, a, b, c, x8, S32, 0x8771f681UL);
HH(c, d, a, b, x11, S33, 0x6d9d6122UL);
HH(b, c, d, a, x14, S34, 0xfde5380cUL);
HH(a, b, c, d, x1, S31, 0xa4beea44UL);
HH(d, a, b, c, x4, S32, 0x4bdecfa9UL);
HH(c, d, a, b, x7, S33, 0xf6bb4b60UL);
HH(b, c, d, a, x10, S34, 0xbebfbc70UL);
HH(a, b, c, d, x13, S31, 0x289b7ec6UL);
HH(d, a, b, c, x0, S32, 0xeaa127faUL);
HH(c, d, a, b, x3, S33, 0xd4ef3085UL);
HH(b, c, d, a, x6, S34, 0x04881d05UL);
HH(a, b, c, d, x9, S31, 0xd9d4d039UL);
HH(d, a, b, c, x12, S32, 0xe6db99e5UL);
HH(c, d, a, b, x15, S33, 0x1fa27cf8UL);
HH(b, c, d, a, x2, S34, 0xc4ac5665UL);
#define S41 6
#define S42 10
#define S43 15
#define S44 21
II(a, b, c, d, x0, S41, 0xf4292244UL);
II(d, a, b, c, x7, S42, 0x432aff97UL);
II(c, d, a, b, x14, S43, 0xab9423a7UL);
II(b, c, d, a, x5, S44, 0xfc93a039UL);
II(a, b, c, d, x12, S41, 0x655b59c3UL);
II(d, a, b, c, x3, S42, 0x8f0ccc92UL);
II(c, d, a, b, x10, S43, 0xffeff47dUL);
II(b, c, d, a, x1, S44, 0x85845dd1UL);
II(a, b, c, d, x8, S41, 0x6fa87e4fUL);
II(d, a, b, c, x15, S42, 0xfe2ce6e0UL);
II(c, d, a, b, x6, S43, 0xa3014314UL);
II(b, c, d, a, x13, S44, 0x4e0811a1UL);
II(a, b, c, d, x4, S41, 0xf7537e82UL);
II(d, a, b, c, x11, S42, 0xbd3af235UL);
II(c, d, a, b, x2, S43, 0x2ad7d2bbUL);
II(b, c, d, a, x9, S44, 0xeb86d391UL);
context->state[0] += a;
context->state[1] += b;
context->state[2] += c;
context->state[3] += d;
x15 = x14 = x13 = x12 = x11 = x10 = x9 = x8 = 0;
x7 = x6 = x5 = x4 = x3 = x2 = x1 = x0 = 0;
}