1a4e54154Smrg/*
2a4e54154Smrg * This code implements the MD5 message-digest algorithm.
3a4e54154Smrg * The algorithm is due to Ron Rivest.	This code was
4a4e54154Smrg * written by Colin Plumb in 1993, no copyright is claimed.
5a4e54154Smrg * This code is in the public domain; do with it what you wish.
6a4e54154Smrg *
7a4e54154Smrg * Equivalent code is available from RSA Data Security, Inc.
8a4e54154Smrg * This code has been tested against that, and is equivalent,
9a4e54154Smrg * except that you don't need to include two pages of legalese
10a4e54154Smrg * with every copy.
11a4e54154Smrg *
12a4e54154Smrg * To compute the message digest of a chunk of bytes, declare an
13a4e54154Smrg * MD5Context structure, pass it to MD5Init, call MD5Update as
14a4e54154Smrg * needed on buffers full of bytes, and then call MD5Final, which
15a4e54154Smrg * will fill a supplied 16-byte array with the digest.
16a4e54154Smrg */
17a4e54154Smrg#include "fcint.h"
18a4e54154Smrg
19a4e54154Smrgstruct MD5Context {
20a4e54154Smrg        FcChar32 buf[4];
21a4e54154Smrg        FcChar32 bits[2];
22a4e54154Smrg        unsigned char in[64];
23a4e54154Smrg};
24a4e54154Smrg
25a4e54154Smrgstatic void MD5Init(struct MD5Context *ctx);
26a4e54154Smrgstatic void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len);
27a4e54154Smrgstatic void MD5Final(unsigned char digest[16], struct MD5Context *ctx);
28a4e54154Smrgstatic void MD5Transform(FcChar32 buf[4], FcChar32 in[16]);
29a4e54154Smrg
30a4e54154Smrg#ifndef WORDS_BIGENDIAN
31a4e54154Smrg#define byteReverse(buf, len)	/* Nothing */
32a4e54154Smrg#else
33a4e54154Smrg/*
34a4e54154Smrg * Note: this code is harmless on little-endian machines.
35a4e54154Smrg */
36a4e54154Smrgvoid byteReverse(unsigned char *buf, unsigned longs)
37a4e54154Smrg{
38a4e54154Smrg    FcChar32 t;
39a4e54154Smrg    do {
40a4e54154Smrg	t = (FcChar32) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
41a4e54154Smrg	    ((unsigned) buf[1] << 8 | buf[0]);
42a4e54154Smrg	*(FcChar32 *) buf = t;
43a4e54154Smrg	buf += 4;
44a4e54154Smrg    } while (--longs);
45a4e54154Smrg}
46a4e54154Smrg#endif
47a4e54154Smrg
48a4e54154Smrg/*
49a4e54154Smrg * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
50a4e54154Smrg * initialization constants.
51a4e54154Smrg */
52a4e54154Smrgstatic void MD5Init(struct MD5Context *ctx)
53a4e54154Smrg{
54a4e54154Smrg    ctx->buf[0] = 0x67452301;
55a4e54154Smrg    ctx->buf[1] = 0xefcdab89;
56a4e54154Smrg    ctx->buf[2] = 0x98badcfe;
57a4e54154Smrg    ctx->buf[3] = 0x10325476;
58a4e54154Smrg
59a4e54154Smrg    ctx->bits[0] = 0;
60a4e54154Smrg    ctx->bits[1] = 0;
61a4e54154Smrg}
62a4e54154Smrg
63a4e54154Smrg/*
64a4e54154Smrg * Update context to reflect the concatenation of another buffer full
65a4e54154Smrg * of bytes.
66a4e54154Smrg */
67a4e54154Smrgstatic void MD5Update(struct MD5Context *ctx, const unsigned char *buf, unsigned len)
68a4e54154Smrg{
69a4e54154Smrg    FcChar32 t;
70a4e54154Smrg
71a4e54154Smrg    /* Update bitcount */
72a4e54154Smrg
73a4e54154Smrg    t = ctx->bits[0];
74a4e54154Smrg    if ((ctx->bits[0] = t + ((FcChar32) len << 3)) < t)
75a4e54154Smrg	ctx->bits[1]++; 	/* Carry from low to high */
76a4e54154Smrg    ctx->bits[1] += len >> 29;
77a4e54154Smrg
78a4e54154Smrg    t = (t >> 3) & 0x3f;	/* Bytes already in shsInfo->data */
79a4e54154Smrg
80a4e54154Smrg    /* Handle any leading odd-sized chunks */
81a4e54154Smrg
82a4e54154Smrg    if (t) {
83a4e54154Smrg	unsigned char *p = (unsigned char *) ctx->in + t;
84a4e54154Smrg
85a4e54154Smrg	t = 64 - t;
86a4e54154Smrg	if (len < t) {
87a4e54154Smrg	    memcpy(p, buf, len);
88a4e54154Smrg	    return;
89a4e54154Smrg	}
90a4e54154Smrg	memcpy(p, buf, t);
91a4e54154Smrg	byteReverse(ctx->in, 16);
92a4e54154Smrg	MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
93a4e54154Smrg	buf += t;
94a4e54154Smrg	len -= t;
95a4e54154Smrg    }
96a4e54154Smrg    /* Process data in 64-byte chunks */
97a4e54154Smrg
98a4e54154Smrg    while (len >= 64) {
99a4e54154Smrg	memcpy(ctx->in, buf, 64);
100a4e54154Smrg	byteReverse(ctx->in, 16);
101a4e54154Smrg	MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
102a4e54154Smrg	buf += 64;
103a4e54154Smrg	len -= 64;
104a4e54154Smrg    }
105a4e54154Smrg
106a4e54154Smrg    /* Handle any remaining bytes of data. */
107a4e54154Smrg
108a4e54154Smrg    memcpy(ctx->in, buf, len);
109a4e54154Smrg}
110a4e54154Smrg
111a4e54154Smrg/*
112a4e54154Smrg * Final wrapup - pad to 64-byte boundary with the bit pattern
113a4e54154Smrg * 1 0* (64-bit count of bits processed, MSB-first)
114a4e54154Smrg */
115a4e54154Smrgstatic void MD5Final(unsigned char digest[16], struct MD5Context *ctx)
116a4e54154Smrg{
117a4e54154Smrg    unsigned count;
118a4e54154Smrg    unsigned char *p;
119a4e54154Smrg
120a4e54154Smrg    /* Compute number of bytes mod 64 */
121a4e54154Smrg    count = (ctx->bits[0] >> 3) & 0x3F;
122a4e54154Smrg
123a4e54154Smrg    /* Set the first char of padding to 0x80.  This is safe since there is
124a4e54154Smrg       always at least one byte free */
125a4e54154Smrg    p = ctx->in + count;
126a4e54154Smrg    *p++ = 0x80;
127a4e54154Smrg
128a4e54154Smrg    /* Bytes of padding needed to make 64 bytes */
129a4e54154Smrg    count = 64 - 1 - count;
130a4e54154Smrg
131a4e54154Smrg    /* Pad out to 56 mod 64 */
132a4e54154Smrg    if (count < 8) {
133a4e54154Smrg	/* Two lots of padding:  Pad the first block to 64 bytes */
134a4e54154Smrg	memset(p, 0, count);
135a4e54154Smrg	byteReverse(ctx->in, 16);
136a4e54154Smrg	MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
137a4e54154Smrg
138a4e54154Smrg	/* Now fill the next block with 56 bytes */
139a4e54154Smrg	memset(ctx->in, 0, 56);
140a4e54154Smrg    } else {
141a4e54154Smrg	/* Pad block to 56 bytes */
142a4e54154Smrg	memset(p, 0, count - 8);
143a4e54154Smrg    }
144a4e54154Smrg    byteReverse(ctx->in, 14);
145a4e54154Smrg
146a4e54154Smrg    /* Append length in bits and transform */
147a4e54154Smrg    ((FcChar32 *) ctx->in)[14] = ctx->bits[0];
148a4e54154Smrg    ((FcChar32 *) ctx->in)[15] = ctx->bits[1];
149a4e54154Smrg
150a4e54154Smrg    MD5Transform(ctx->buf, (FcChar32 *) ctx->in);
151a4e54154Smrg    byteReverse((unsigned char *) ctx->buf, 4);
152a4e54154Smrg    memcpy(digest, ctx->buf, 16);
153a4e54154Smrg    memset(ctx, 0, sizeof(*ctx));        /* In case it's sensitive */
154a4e54154Smrg}
155a4e54154Smrg
156a4e54154Smrg
157a4e54154Smrg/* The four core functions - F1 is optimized somewhat */
158a4e54154Smrg
159a4e54154Smrg/* #define F1(x, y, z) (x & y | ~x & z) */
160a4e54154Smrg#define F1(x, y, z) (z ^ (x & (y ^ z)))
161a4e54154Smrg#define F2(x, y, z) F1(z, x, y)
162a4e54154Smrg#define F3(x, y, z) (x ^ y ^ z)
163a4e54154Smrg#define F4(x, y, z) (y ^ (x | ~z))
164a4e54154Smrg
165a4e54154Smrg/* This is the central step in the MD5 algorithm. */
166a4e54154Smrg#define MD5STEP(f, w, x, y, z, data, s) \
167a4e54154Smrg	( w += f(x, y, z) + data,  w = w<<s | w>>(32-s),  w += x )
168a4e54154Smrg
169a4e54154Smrg/*
170a4e54154Smrg * The core of the MD5 algorithm, this alters an existing MD5 hash to
171a4e54154Smrg * reflect the addition of 16 longwords of new data.  MD5Update blocks
172a4e54154Smrg * the data and converts bytes into longwords for this routine.
173a4e54154Smrg */
174a4e54154Smrgstatic void MD5Transform(FcChar32 buf[4], FcChar32 in[16])
175a4e54154Smrg{
176a4e54154Smrg    register FcChar32 a, b, c, d;
177a4e54154Smrg
178a4e54154Smrg    a = buf[0];
179a4e54154Smrg    b = buf[1];
180a4e54154Smrg    c = buf[2];
181a4e54154Smrg    d = buf[3];
182a4e54154Smrg
183a4e54154Smrg    MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
184a4e54154Smrg    MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
185a4e54154Smrg    MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
186a4e54154Smrg    MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
187a4e54154Smrg    MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
188a4e54154Smrg    MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
189a4e54154Smrg    MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
190a4e54154Smrg    MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
191a4e54154Smrg    MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
192a4e54154Smrg    MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
193a4e54154Smrg    MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
194a4e54154Smrg    MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
195a4e54154Smrg    MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
196a4e54154Smrg    MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
197a4e54154Smrg    MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
198a4e54154Smrg    MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
199a4e54154Smrg
200a4e54154Smrg    MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
201a4e54154Smrg    MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
202a4e54154Smrg    MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
203a4e54154Smrg    MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
204a4e54154Smrg    MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
205a4e54154Smrg    MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
206a4e54154Smrg    MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
207a4e54154Smrg    MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
208a4e54154Smrg    MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
209a4e54154Smrg    MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
210a4e54154Smrg    MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
211a4e54154Smrg    MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
212a4e54154Smrg    MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
213a4e54154Smrg    MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
214a4e54154Smrg    MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
215a4e54154Smrg    MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
216a4e54154Smrg
217a4e54154Smrg    MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
218a4e54154Smrg    MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
219a4e54154Smrg    MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
220a4e54154Smrg    MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
221a4e54154Smrg    MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
222a4e54154Smrg    MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
223a4e54154Smrg    MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
224a4e54154Smrg    MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
225a4e54154Smrg    MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
226a4e54154Smrg    MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
227a4e54154Smrg    MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
228a4e54154Smrg    MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
229a4e54154Smrg    MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
230a4e54154Smrg    MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
231a4e54154Smrg    MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
232a4e54154Smrg    MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
233a4e54154Smrg
234a4e54154Smrg    MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
235a4e54154Smrg    MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
236a4e54154Smrg    MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
237a4e54154Smrg    MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
238a4e54154Smrg    MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
239a4e54154Smrg    MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
240a4e54154Smrg    MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
241a4e54154Smrg    MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
242a4e54154Smrg    MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
243a4e54154Smrg    MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
244a4e54154Smrg    MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
245a4e54154Smrg    MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
246a4e54154Smrg    MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
247a4e54154Smrg    MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
248a4e54154Smrg    MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
249a4e54154Smrg    MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
250a4e54154Smrg
251a4e54154Smrg    buf[0] += a;
252a4e54154Smrg    buf[1] += b;
253a4e54154Smrg    buf[2] += c;
254a4e54154Smrg    buf[3] += d;
255a4e54154Smrg}
256