md5.c revision 135446
1135446Strhodes/*
2135446Strhodes * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3135446Strhodes * Copyright (C) 2000, 2001  Internet Software Consortium.
4135446Strhodes *
5135446Strhodes * Permission to use, copy, modify, and distribute this software for any
6135446Strhodes * purpose with or without fee is hereby granted, provided that the above
7135446Strhodes * copyright notice and this permission notice appear in all copies.
8135446Strhodes *
9135446Strhodes * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10135446Strhodes * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11135446Strhodes * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12135446Strhodes * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13135446Strhodes * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14135446Strhodes * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15135446Strhodes * PERFORMANCE OF THIS SOFTWARE.
16135446Strhodes */
17135446Strhodes
18135446Strhodes/* $Id: md5.c,v 1.9.206.1 2004/03/06 08:14:32 marka Exp $ */
19135446Strhodes
20135446Strhodes/*
21135446Strhodes * This code implements the MD5 message-digest algorithm.
22135446Strhodes * The algorithm is due to Ron Rivest.  This code was
23135446Strhodes * written by Colin Plumb in 1993, no copyright is claimed.
24135446Strhodes * This code is in the public domain; do with it what you wish.
25135446Strhodes *
26135446Strhodes * Equivalent code is available from RSA Data Security, Inc.
27135446Strhodes * This code has been tested against that, and is equivalent,
28135446Strhodes * except that you don't need to include two pages of legalese
29135446Strhodes * with every copy.
30135446Strhodes *
31135446Strhodes * To compute the message digest of a chunk of bytes, declare an
32135446Strhodes * MD5Context structure, pass it to MD5Init, call MD5Update as
33135446Strhodes * needed on buffers full of bytes, and then call MD5Final, which
34135446Strhodes * will fill a supplied 16-byte array with the digest.
35135446Strhodes */
36135446Strhodes
37135446Strhodes#include "config.h"
38135446Strhodes
39135446Strhodes#include <isc/assertions.h>
40135446Strhodes#include <isc/md5.h>
41135446Strhodes#include <isc/string.h>
42135446Strhodes#include <isc/types.h>
43135446Strhodes#include <isc/util.h>
44135446Strhodes
45135446Strhodesstatic void
46135446StrhodesbyteSwap(isc_uint32_t *buf, unsigned words)
47135446Strhodes{
48135446Strhodes	unsigned char *p = (unsigned char *)buf;
49135446Strhodes
50135446Strhodes	do {
51135446Strhodes		*buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
52135446Strhodes			((unsigned)p[1] << 8 | p[0]);
53135446Strhodes		p += 4;
54135446Strhodes	} while (--words);
55135446Strhodes}
56135446Strhodes
57135446Strhodes/*
58135446Strhodes * Start MD5 accumulation.  Set bit count to 0 and buffer to mysterious
59135446Strhodes * initialization constants.
60135446Strhodes */
61135446Strhodesvoid
62135446Strhodesisc_md5_init(isc_md5_t *ctx) {
63135446Strhodes	ctx->buf[0] = 0x67452301;
64135446Strhodes	ctx->buf[1] = 0xefcdab89;
65135446Strhodes	ctx->buf[2] = 0x98badcfe;
66135446Strhodes	ctx->buf[3] = 0x10325476;
67135446Strhodes
68135446Strhodes	ctx->bytes[0] = 0;
69135446Strhodes	ctx->bytes[1] = 0;
70135446Strhodes}
71135446Strhodes
72135446Strhodesvoid
73135446Strhodesisc_md5_invalidate(isc_md5_t *ctx) {
74135446Strhodes	memset(ctx, 0, sizeof(isc_md5_t));
75135446Strhodes}
76135446Strhodes
77135446Strhodes/* The four core functions - F1 is optimized somewhat */
78135446Strhodes
79135446Strhodes/* #define F1(x, y, z) (x & y | ~x & z) */
80135446Strhodes#define F1(x, y, z) (z ^ (x & (y ^ z)))
81135446Strhodes#define F2(x, y, z) F1(z, x, y)
82135446Strhodes#define F3(x, y, z) (x ^ y ^ z)
83135446Strhodes#define F4(x, y, z) (y ^ (x | ~z))
84135446Strhodes
85135446Strhodes/* This is the central step in the MD5 algorithm. */
86135446Strhodes#define MD5STEP(f,w,x,y,z,in,s) \
87135446Strhodes	 (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
88135446Strhodes
89135446Strhodes/*
90135446Strhodes * The core of the MD5 algorithm, this alters an existing MD5 hash to
91135446Strhodes * reflect the addition of 16 longwords of new data.  MD5Update blocks
92135446Strhodes * the data and converts bytes into longwords for this routine.
93135446Strhodes */
94135446Strhodesstatic void
95135446Strhodestransform(isc_uint32_t buf[4], isc_uint32_t const in[16]) {
96135446Strhodes	register isc_uint32_t a, b, c, d;
97135446Strhodes
98135446Strhodes	a = buf[0];
99135446Strhodes	b = buf[1];
100135446Strhodes	c = buf[2];
101135446Strhodes	d = buf[3];
102135446Strhodes
103135446Strhodes	MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
104135446Strhodes	MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
105135446Strhodes	MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
106135446Strhodes	MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
107135446Strhodes	MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
108135446Strhodes	MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
109135446Strhodes	MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
110135446Strhodes	MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
111135446Strhodes	MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
112135446Strhodes	MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
113135446Strhodes	MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
114135446Strhodes	MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
115135446Strhodes	MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
116135446Strhodes	MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
117135446Strhodes	MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
118135446Strhodes	MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
119135446Strhodes
120135446Strhodes	MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
121135446Strhodes	MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
122135446Strhodes	MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
123135446Strhodes	MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
124135446Strhodes	MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
125135446Strhodes	MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
126135446Strhodes	MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
127135446Strhodes	MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
128135446Strhodes	MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
129135446Strhodes	MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
130135446Strhodes	MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
131135446Strhodes	MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
132135446Strhodes	MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
133135446Strhodes	MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
134135446Strhodes	MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
135135446Strhodes	MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
136135446Strhodes
137135446Strhodes	MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
138135446Strhodes	MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
139135446Strhodes	MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
140135446Strhodes	MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
141135446Strhodes	MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
142135446Strhodes	MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
143135446Strhodes	MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
144135446Strhodes	MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
145135446Strhodes	MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
146135446Strhodes	MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
147135446Strhodes	MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
148135446Strhodes	MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
149135446Strhodes	MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
150135446Strhodes	MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
151135446Strhodes	MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
152135446Strhodes	MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
153135446Strhodes
154135446Strhodes	MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
155135446Strhodes	MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
156135446Strhodes	MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
157135446Strhodes	MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
158135446Strhodes	MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
159135446Strhodes	MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
160135446Strhodes	MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
161135446Strhodes	MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
162135446Strhodes	MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
163135446Strhodes	MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
164135446Strhodes	MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
165135446Strhodes	MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
166135446Strhodes	MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
167135446Strhodes	MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
168135446Strhodes	MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
169135446Strhodes	MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
170135446Strhodes
171135446Strhodes	buf[0] += a;
172135446Strhodes	buf[1] += b;
173135446Strhodes	buf[2] += c;
174135446Strhodes	buf[3] += d;
175135446Strhodes}
176135446Strhodes
177135446Strhodes/*
178135446Strhodes * Update context to reflect the concatenation of another buffer full
179135446Strhodes * of bytes.
180135446Strhodes */
181135446Strhodesvoid
182135446Strhodesisc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
183135446Strhodes	isc_uint32_t t;
184135446Strhodes
185135446Strhodes	/* Update byte count */
186135446Strhodes
187135446Strhodes	t = ctx->bytes[0];
188135446Strhodes	if ((ctx->bytes[0] = t + len) < t)
189135446Strhodes		ctx->bytes[1]++;	/* Carry from low to high */
190135446Strhodes
191135446Strhodes	t = 64 - (t & 0x3f);	/* Space available in ctx->in (at least 1) */
192135446Strhodes	if (t > len) {
193135446Strhodes		memcpy((unsigned char *)ctx->in + 64 - t, buf, len);
194135446Strhodes		return;
195135446Strhodes	}
196135446Strhodes	/* First chunk is an odd size */
197135446Strhodes	memcpy((unsigned char *)ctx->in + 64 - t, buf, t);
198135446Strhodes	byteSwap(ctx->in, 16);
199135446Strhodes	transform(ctx->buf, ctx->in);
200135446Strhodes	buf += t;
201135446Strhodes	len -= t;
202135446Strhodes
203135446Strhodes	/* Process data in 64-byte chunks */
204135446Strhodes	while (len >= 64) {
205135446Strhodes		memcpy(ctx->in, buf, 64);
206135446Strhodes		byteSwap(ctx->in, 16);
207135446Strhodes		transform(ctx->buf, ctx->in);
208135446Strhodes		buf += 64;
209135446Strhodes		len -= 64;
210135446Strhodes	}
211135446Strhodes
212135446Strhodes	/* Handle any remaining bytes of data. */
213135446Strhodes	memcpy(ctx->in, buf, len);
214135446Strhodes}
215135446Strhodes
216135446Strhodes/*
217135446Strhodes * Final wrapup - pad to 64-byte boundary with the bit pattern
218135446Strhodes * 1 0* (64-bit count of bits processed, MSB-first)
219135446Strhodes */
220135446Strhodesvoid
221135446Strhodesisc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
222135446Strhodes	int count = ctx->bytes[0] & 0x3f;    /* Number of bytes in ctx->in */
223135446Strhodes	unsigned char *p = (unsigned char *)ctx->in + count;
224135446Strhodes
225135446Strhodes	/* Set the first char of padding to 0x80.  There is always room. */
226135446Strhodes	*p++ = 0x80;
227135446Strhodes
228135446Strhodes	/* Bytes of padding needed to make 56 bytes (-8..55) */
229135446Strhodes	count = 56 - 1 - count;
230135446Strhodes
231135446Strhodes	if (count < 0) {	/* Padding forces an extra block */
232135446Strhodes		memset(p, 0, count + 8);
233135446Strhodes		byteSwap(ctx->in, 16);
234135446Strhodes		transform(ctx->buf, ctx->in);
235135446Strhodes		p = (unsigned char *)ctx->in;
236135446Strhodes		count = 56;
237135446Strhodes	}
238135446Strhodes	memset(p, 0, count);
239135446Strhodes	byteSwap(ctx->in, 14);
240135446Strhodes
241135446Strhodes	/* Append length in bits and transform */
242135446Strhodes	ctx->in[14] = ctx->bytes[0] << 3;
243135446Strhodes	ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
244135446Strhodes	transform(ctx->buf, ctx->in);
245135446Strhodes
246135446Strhodes	byteSwap(ctx->buf, 4);
247135446Strhodes	memcpy(digest, ctx->buf, 16);
248135446Strhodes	memset(ctx, 0, sizeof(isc_md5_t));	/* In case it's sensitive */
249135446Strhodes}
250