1N/A/***********************************************************************
1N/A* *
1N/A* This software is part of the ast package *
1N/A* Copyright (c) 1996-2011 AT&T Intellectual Property *
1N/A* and is licensed under the *
1N/A* Common Public License, Version 1.0 *
1N/A* by AT&T Intellectual Property *
1N/A* *
1N/A* A copy of the License is available at *
1N/A* http://www.opensource.org/licenses/cpl1.0.txt *
1N/A* (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
1N/A* *
1N/A* Information and Software Systems Research *
1N/A* AT&T Research *
1N/A* Florham Park NJ *
1N/A* *
1N/A* Glenn Fowler <gsf@research.att.com> *
1N/A* *
1N/A***********************************************************************/
1N/A#pragma prototyped
1N/A
1N/A/*
1N/A * md5
1N/A */
1N/A
1N/A/* Copyright (C) 1991-2, RSA Data Security, Inc. Created 1991. All
1N/A rights reserved.
1N/A
1N/A License to copy and use this software is granted provided that it
1N/A is identified as the "RSA Data Security, Inc. MD5 Message-Digest
1N/A Method" in all material mentioning or referencing this software
1N/A or this function.
1N/A
1N/A License is also granted to make and use derivative works provided
1N/A that such works are identified as "derived from the RSA Data
1N/A Security, Inc. MD5 Message-Digest Method" in all material
1N/A mentioning or referencing the derived work.
1N/A
1N/A RSA Data Security, Inc. makes no representations concerning either
1N/A the merchantability of this software or the suitability of this
1N/A software for any particular purpose. It is provided "as is"
1N/A without express or implied warranty of any kind.
1N/A
1N/A These notices must be retained in any copies of any part of this
1N/A documentation and/or software.
1N/A */
1N/A
1N/A#define md5_description \
1N/A "The RSA Data Security, Inc. MD5 Message-Digest Method, 1991-2, \
1N/A used with permission. The block count is not printed."
1N/A#define md5_options "[+(version)?md5 (RSA Data Security, Inc. MD5 Message-Digest, 1991-2) 1996-02-29]"
1N/A#define md5_match "md5|MD5"
1N/A#define md5_scale 0
1N/A
1N/Atypedef uint32_t UINT4;
1N/A
1N/Atypedef struct Md5_s
1N/A{
1N/A _SUM_PUBLIC_
1N/A _SUM_PRIVATE_
1N/A UINT4 state[4]; /* state (ABCD) */
1N/A UINT4 count[2]; /* # bits handled mod 2^64 (lsb)*/
1N/A unsigned char buffer[64]; /* input buffer */
1N/A unsigned char digest[16]; /* final digest */
1N/A unsigned char digest_sum[16]; /* sum of all digests */
1N/A} Md5_t;
1N/A
1N/Astatic const unsigned char md5_pad[] =
1N/A{
1N/A 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1N/A 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1N/A};
1N/A
1N/A/*
1N/A * encode input into output
1N/A * len must be a multiple of 4
1N/A */
1N/A
1N/Astatic void
1N/Amd5_encode(register unsigned char* output, register UINT4* input, unsigned int len)
1N/A{
1N/A register unsigned int i;
1N/A register unsigned int j;
1N/A
1N/A for (i = j = 0; j < len; i++, j += 4)
1N/A {
1N/A output[j] = (unsigned char)(input[i] & 0xff);
1N/A output[j+1] = (unsigned char)((input[i] >> 8) & 0xff);
1N/A output[j+2] = (unsigned char)((input[i] >> 16) & 0xff);
1N/A output[j+3] = (unsigned char)((input[i] >> 24) & 0xff);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * decode input into output
1N/A * len must be a multiple of 4
1N/A */
1N/A
1N/Astatic void
1N/Amd5_decode(register UINT4* output, register unsigned char* input, unsigned int len)
1N/A{
1N/A unsigned int i;
1N/A unsigned int j;
1N/A
1N/A for (i = j = 0; j < len; i++, j += 4)
1N/A output[i] = ((UINT4)input[j]) |
1N/A (((UINT4)input[j+1]) << 8) |
1N/A (((UINT4)input[j+2]) << 16) |
1N/A (((UINT4)input[j+3]) << 24);
1N/A}
1N/A
1N/Astatic int
1N/Amd5_init(Sum_t* p)
1N/A{
1N/A register Md5_t* context = (Md5_t*)p;
1N/A
1N/A context->count[0] = context->count[1] = 0;
1N/A context->state[0] = 0x67452301;
1N/A context->state[1] = 0xefcdab89;
1N/A context->state[2] = 0x98badcfe;
1N/A context->state[3] = 0x10325476;
1N/A return 0;
1N/A}
1N/A
1N/Astatic Sum_t*
1N/Amd5_open(const Method_t* method, const char* name)
1N/A{
1N/A Md5_t* p;
1N/A
1N/A if (p = newof(0, Md5_t, 1, 0))
1N/A {
1N/A p->method = (Method_t*)method;
1N/A p->name = name;
1N/A md5_init((Sum_t*)p);
1N/A }
1N/A return (Sum_t*)p;
1N/A}
1N/A
1N/A/*
1N/A * basic MD5 step -- transforms buf based on in
1N/A */
1N/A
1N/A#define S11 7
1N/A#define S12 12
1N/A#define S13 17
1N/A#define S14 22
1N/A#define S21 5
1N/A#define S22 9
1N/A#define S23 14
1N/A#define S24 20
1N/A#define S31 4
1N/A#define S32 11
1N/A#define S33 16
1N/A#define S34 23
1N/A#define S41 6
1N/A#define S42 10
1N/A#define S43 15
1N/A#define S44 21
1N/A
1N/A/* F, G, H and I are basic MD5 functions */
1N/A#define F(x, y, z) (((x) & (y)) | ((~x) & (z)))
1N/A#define G(x, y, z) (((x) & (z)) | ((y) & (~z)))
1N/A#define H(x, y, z) ((x) ^ (y) ^ (z))
1N/A#define I(x, y, z) ((y) ^ ((x) | (~z)))
1N/A
1N/A/* ROTATE_LEFT rotates x left n bits */
1N/A#define ROTATE_LEFT(x, n) (((x) << (n)) | ((x) >> (32-(n))))
1N/A
1N/A/* FF, GG, HH, and II transformations for rounds 1, 2, 3, and 4 */
1N/A/* Rotation is separate from addition to prevent recomputation */
1N/A#define FF(a, b, c, d, x, s, ac) { \
1N/A (a) += F ((b), (c), (d)) + (x) + (UINT4)(ac); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A }
1N/A#define GG(a, b, c, d, x, s, ac) { \
1N/A (a) += G ((b), (c), (d)) + (x) + (UINT4)(ac); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A }
1N/A#define HH(a, b, c, d, x, s, ac) { \
1N/A (a) += H ((b), (c), (d)) + (x) + (UINT4)(ac); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A }
1N/A#define II(a, b, c, d, x, s, ac) { \
1N/A (a) += I ((b), (c), (d)) + (x) + (UINT4)(ac); \
1N/A (a) = ROTATE_LEFT ((a), (s)); \
1N/A (a) += (b); \
1N/A }
1N/A
1N/Astatic void
1N/Amd5_transform(UINT4 state[4], unsigned char block[64])
1N/A{
1N/A UINT4 a = state[0];
1N/A UINT4 b = state[1];
1N/A UINT4 c = state[2];
1N/A UINT4 d = state[3];
1N/A UINT4 x[16];
1N/A
1N/A md5_decode(x, block, 64);
1N/A
1N/A /* round 1 */
1N/A FF (a, b, c, d, x[ 0], S11, 0xd76aa478); /* 1 */
1N/A FF (d, a, b, c, x[ 1], S12, 0xe8c7b756); /* 2 */
1N/A FF (c, d, a, b, x[ 2], S13, 0x242070db); /* 3 */
1N/A FF (b, c, d, a, x[ 3], S14, 0xc1bdceee); /* 4 */
1N/A FF (a, b, c, d, x[ 4], S11, 0xf57c0faf); /* 5 */
1N/A FF (d, a, b, c, x[ 5], S12, 0x4787c62a); /* 6 */
1N/A FF (c, d, a, b, x[ 6], S13, 0xa8304613); /* 7 */
1N/A FF (b, c, d, a, x[ 7], S14, 0xfd469501); /* 8 */
1N/A FF (a, b, c, d, x[ 8], S11, 0x698098d8); /* 9 */
1N/A FF (d, a, b, c, x[ 9], S12, 0x8b44f7af); /* 10 */
1N/A FF (c, d, a, b, x[10], S13, 0xffff5bb1); /* 11 */
1N/A FF (b, c, d, a, x[11], S14, 0x895cd7be); /* 12 */
1N/A FF (a, b, c, d, x[12], S11, 0x6b901122); /* 13 */
1N/A FF (d, a, b, c, x[13], S12, 0xfd987193); /* 14 */
1N/A FF (c, d, a, b, x[14], S13, 0xa679438e); /* 15 */
1N/A FF (b, c, d, a, x[15], S14, 0x49b40821); /* 16 */
1N/A
1N/A /* round 2 */
1N/A GG (a, b, c, d, x[ 1], S21, 0xf61e2562); /* 17 */
1N/A GG (d, a, b, c, x[ 6], S22, 0xc040b340); /* 18 */
1N/A GG (c, d, a, b, x[11], S23, 0x265e5a51); /* 19 */
1N/A GG (b, c, d, a, x[ 0], S24, 0xe9b6c7aa); /* 20 */
1N/A GG (a, b, c, d, x[ 5], S21, 0xd62f105d); /* 21 */
1N/A GG (d, a, b, c, x[10], S22, 0x02441453); /* 22 */
1N/A GG (c, d, a, b, x[15], S23, 0xd8a1e681); /* 23 */
1N/A GG (b, c, d, a, x[ 4], S24, 0xe7d3fbc8); /* 24 */
1N/A GG (a, b, c, d, x[ 9], S21, 0x21e1cde6); /* 25 */
1N/A GG (d, a, b, c, x[14], S22, 0xc33707d6); /* 26 */
1N/A GG (c, d, a, b, x[ 3], S23, 0xf4d50d87); /* 27 */
1N/A GG (b, c, d, a, x[ 8], S24, 0x455a14ed); /* 28 */
1N/A GG (a, b, c, d, x[13], S21, 0xa9e3e905); /* 29 */
1N/A GG (d, a, b, c, x[ 2], S22, 0xfcefa3f8); /* 30 */
1N/A GG (c, d, a, b, x[ 7], S23, 0x676f02d9); /* 31 */
1N/A GG (b, c, d, a, x[12], S24, 0x8d2a4c8a); /* 32 */
1N/A
1N/A /* round 3 */
1N/A HH (a, b, c, d, x[ 5], S31, 0xfffa3942); /* 33 */
1N/A HH (d, a, b, c, x[ 8], S32, 0x8771f681); /* 34 */
1N/A HH (c, d, a, b, x[11], S33, 0x6d9d6122); /* 35 */
1N/A HH (b, c, d, a, x[14], S34, 0xfde5380c); /* 36 */
1N/A HH (a, b, c, d, x[ 1], S31, 0xa4beea44); /* 37 */
1N/A HH (d, a, b, c, x[ 4], S32, 0x4bdecfa9); /* 38 */
1N/A HH (c, d, a, b, x[ 7], S33, 0xf6bb4b60); /* 39 */
1N/A HH (b, c, d, a, x[10], S34, 0xbebfbc70); /* 40 */
1N/A HH (a, b, c, d, x[13], S31, 0x289b7ec6); /* 41 */
1N/A HH (d, a, b, c, x[ 0], S32, 0xeaa127fa); /* 42 */
1N/A HH (c, d, a, b, x[ 3], S33, 0xd4ef3085); /* 43 */
1N/A HH (b, c, d, a, x[ 6], S34, 0x04881d05); /* 44 */
1N/A HH (a, b, c, d, x[ 9], S31, 0xd9d4d039); /* 45 */
1N/A HH (d, a, b, c, x[12], S32, 0xe6db99e5); /* 46 */
1N/A HH (c, d, a, b, x[15], S33, 0x1fa27cf8); /* 47 */
1N/A HH (b, c, d, a, x[ 2], S34, 0xc4ac5665); /* 48 */
1N/A
1N/A /* round 4 */
1N/A II (a, b, c, d, x[ 0], S41, 0xf4292244); /* 49 */
1N/A II (d, a, b, c, x[ 7], S42, 0x432aff97); /* 50 */
1N/A II (c, d, a, b, x[14], S43, 0xab9423a7); /* 51 */
1N/A II (b, c, d, a, x[ 5], S44, 0xfc93a039); /* 52 */
1N/A II (a, b, c, d, x[12], S41, 0x655b59c3); /* 53 */
1N/A II (d, a, b, c, x[ 3], S42, 0x8f0ccc92); /* 54 */
1N/A II (c, d, a, b, x[10], S43, 0xffeff47d); /* 55 */
1N/A II (b, c, d, a, x[ 1], S44, 0x85845dd1); /* 56 */
1N/A II (a, b, c, d, x[ 8], S41, 0x6fa87e4f); /* 57 */
1N/A II (d, a, b, c, x[15], S42, 0xfe2ce6e0); /* 58 */
1N/A II (c, d, a, b, x[ 6], S43, 0xa3014314); /* 59 */
1N/A II (b, c, d, a, x[13], S44, 0x4e0811a1); /* 60 */
1N/A II (a, b, c, d, x[ 4], S41, 0xf7537e82); /* 61 */
1N/A II (d, a, b, c, x[11], S42, 0xbd3af235); /* 62 */
1N/A II (c, d, a, b, x[ 2], S43, 0x2ad7d2bb); /* 63 */
1N/A II (b, c, d, a, x[ 9], S44, 0xeb86d391); /* 64 */
1N/A
1N/A state[0] += a;
1N/A state[1] += b;
1N/A state[2] += c;
1N/A state[3] += d;
1N/A}
1N/A
1N/Astatic int
1N/Amd5_block(Sum_t* p, const void* s, size_t inputLen)
1N/A{
1N/A register Md5_t* context = (Md5_t*)p;
1N/A unsigned char* input = (unsigned char*)s;
1N/A unsigned int i;
1N/A unsigned int index;
1N/A unsigned int partLen;
1N/A
1N/A /* compute number of bytes mod 64 */
1N/A index = (unsigned int)((context->count[0] >> 3) & 0x3f);
1N/A
1N/A /* update number of bits */
1N/A if ((context->count[0] += ((UINT4)inputLen << 3)) < ((UINT4)inputLen << 3))
1N/A context->count[1]++;
1N/A context->count[1] += ((UINT4)inputLen >> 29);
1N/A partLen = 64 - index;
1N/A
1N/A /* transform as many times as possible */
1N/A if (inputLen >= partLen)
1N/A {
1N/A memcpy(&context->buffer[index], input, partLen);
1N/A md5_transform(context->state, context->buffer);
1N/A for (i = partLen; i + 63 < inputLen; i += 64)
1N/A md5_transform(context->state, &input[i]);
1N/A index = 0;
1N/A }
1N/A else
1N/A i = 0;
1N/A
1N/A /* buffer remaining input */
1N/A memcpy(&context->buffer[index], &input[i], inputLen - i);
1N/A
1N/A return 0;
1N/A}
1N/A
1N/Astatic int
1N/Amd5_done(Sum_t* p)
1N/A{
1N/A register Md5_t* context = (Md5_t*)p;
1N/A unsigned char bits[8];
1N/A unsigned int index;
1N/A unsigned int padLen;
1N/A
1N/A /* save number of bits */
1N/A md5_encode(bits, context->count, sizeof(bits));
1N/A
1N/A /* pad out to 56 mod 64 */
1N/A index = (unsigned int)((context->count[0] >> 3) & 0x3f);
1N/A padLen = (index < 56) ? (56 - index) : (120 - index);
1N/A md5_block(p, md5_pad, padLen);
1N/A
1N/A /* append length (before padding) */
1N/A md5_block(p, bits, sizeof(bits));
1N/A
1N/A /* store state in digest */
1N/A md5_encode(context->digest, context->state, sizeof(context->digest));
1N/A
1N/A /* accumulate the digests */
1N/A for (index = 0; index < elementsof(context->digest); index++)
1N/A context->digest_sum[index] ^= context->digest[index];
1N/A
1N/A return 0;
1N/A}
1N/A
1N/Astatic int
1N/Amd5_print(Sum_t* p, Sfio_t* sp, register int flags, size_t scale)
1N/A{
1N/A register Md5_t* x = (Md5_t*)p;
1N/A register unsigned char* d;
1N/A register int n;
1N/A
1N/A d = (flags & SUM_TOTAL) ? x->digest_sum : x->digest;
1N/A for (n = 0; n < elementsof(x->digest); n++)
1N/A sfprintf(sp, "%02x", d[n]);
1N/A return 0;
1N/A}
1N/A
1N/Astatic int
1N/Amd5_data(Sum_t* p, Sumdata_t* data)
1N/A{
1N/A register Md5_t* x = (Md5_t*)p;
1N/A
1N/A data->size = elementsof(x->digest);
1N/A data->num = 0;
1N/A data->buf = x->digest;
1N/A return 0;
1N/A}