1*a466cc55SCy Schubert /*
2*a466cc55SCy Schubert * Copyright (C) 2004, 2005, 2007, 2009 Internet Systems Consortium, Inc. ("ISC")
3*a466cc55SCy Schubert * Copyright (C) 2000, 2001 Internet Software Consortium.
4*a466cc55SCy Schubert *
5*a466cc55SCy Schubert * Permission to use, copy, modify, and/or distribute this software for any
6*a466cc55SCy Schubert * purpose with or without fee is hereby granted, provided that the above
7*a466cc55SCy Schubert * copyright notice and this permission notice appear in all copies.
8*a466cc55SCy Schubert *
9*a466cc55SCy Schubert * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10*a466cc55SCy Schubert * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11*a466cc55SCy Schubert * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12*a466cc55SCy Schubert * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13*a466cc55SCy Schubert * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14*a466cc55SCy Schubert * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15*a466cc55SCy Schubert * PERFORMANCE OF THIS SOFTWARE.
16*a466cc55SCy Schubert */
17*a466cc55SCy Schubert
18*a466cc55SCy Schubert /* $Id: md5.c,v 1.16 2009/02/06 23:47:42 tbox Exp $ */
19*a466cc55SCy Schubert
20*a466cc55SCy Schubert /*! \file
21*a466cc55SCy Schubert * This code implements the MD5 message-digest algorithm.
22*a466cc55SCy Schubert * The algorithm is due to Ron Rivest. This code was
23*a466cc55SCy Schubert * written by Colin Plumb in 1993, no copyright is claimed.
24*a466cc55SCy Schubert * This code is in the public domain; do with it what you wish.
25*a466cc55SCy Schubert *
26*a466cc55SCy Schubert * Equivalent code is available from RSA Data Security, Inc.
27*a466cc55SCy Schubert * This code has been tested against that, and is equivalent,
28*a466cc55SCy Schubert * except that you don't need to include two pages of legalese
29*a466cc55SCy Schubert * with every copy.
30*a466cc55SCy Schubert *
31*a466cc55SCy Schubert * To compute the message digest of a chunk of bytes, declare an
32*a466cc55SCy Schubert * MD5Context structure, pass it to MD5Init, call MD5Update as
33*a466cc55SCy Schubert * needed on buffers full of bytes, and then call MD5Final, which
34*a466cc55SCy Schubert * will fill a supplied 16-byte array with the digest.
35*a466cc55SCy Schubert */
36*a466cc55SCy Schubert
37*a466cc55SCy Schubert #include "config.h"
38*a466cc55SCy Schubert
39*a466cc55SCy Schubert #include <isc/assertions.h>
40*a466cc55SCy Schubert #include <isc/md5.h>
41*a466cc55SCy Schubert #include <isc/platform.h>
42*a466cc55SCy Schubert #include <isc/string.h>
43*a466cc55SCy Schubert #include <isc/types.h>
44*a466cc55SCy Schubert #include <isc/util.h>
45*a466cc55SCy Schubert
46*a466cc55SCy Schubert #ifdef ISC_PLATFORM_OPENSSLHASH
47*a466cc55SCy Schubert
48*a466cc55SCy Schubert void
isc_md5_init(isc_md5_t * ctx)49*a466cc55SCy Schubert isc_md5_init(isc_md5_t *ctx) {
50*a466cc55SCy Schubert EVP_DigestInit(ctx, EVP_md5());
51*a466cc55SCy Schubert }
52*a466cc55SCy Schubert
53*a466cc55SCy Schubert void
isc_md5_invalidate(isc_md5_t * ctx)54*a466cc55SCy Schubert isc_md5_invalidate(isc_md5_t *ctx) {
55*a466cc55SCy Schubert EVP_MD_CTX_cleanup(ctx);
56*a466cc55SCy Schubert }
57*a466cc55SCy Schubert
58*a466cc55SCy Schubert void
isc_md5_update(isc_md5_t * ctx,const unsigned char * buf,unsigned int len)59*a466cc55SCy Schubert isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
60*a466cc55SCy Schubert EVP_DigestUpdate(ctx, (const void *) buf, (size_t) len);
61*a466cc55SCy Schubert }
62*a466cc55SCy Schubert
63*a466cc55SCy Schubert void
isc_md5_final(isc_md5_t * ctx,unsigned char * digest)64*a466cc55SCy Schubert isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
65*a466cc55SCy Schubert EVP_DigestFinal(ctx, digest, NULL);
66*a466cc55SCy Schubert }
67*a466cc55SCy Schubert
68*a466cc55SCy Schubert #else
69*a466cc55SCy Schubert
70*a466cc55SCy Schubert static void
byteSwap(isc_uint32_t * buf,unsigned words)71*a466cc55SCy Schubert byteSwap(isc_uint32_t *buf, unsigned words)
72*a466cc55SCy Schubert {
73*a466cc55SCy Schubert unsigned char *p = (unsigned char *)buf;
74*a466cc55SCy Schubert
75*a466cc55SCy Schubert do {
76*a466cc55SCy Schubert *buf++ = (isc_uint32_t)((unsigned)p[3] << 8 | p[2]) << 16 |
77*a466cc55SCy Schubert ((unsigned)p[1] << 8 | p[0]);
78*a466cc55SCy Schubert p += 4;
79*a466cc55SCy Schubert } while (--words);
80*a466cc55SCy Schubert }
81*a466cc55SCy Schubert
82*a466cc55SCy Schubert /*!
83*a466cc55SCy Schubert * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
84*a466cc55SCy Schubert * initialization constants.
85*a466cc55SCy Schubert */
86*a466cc55SCy Schubert void
isc_md5_init(isc_md5_t * ctx)87*a466cc55SCy Schubert isc_md5_init(isc_md5_t *ctx) {
88*a466cc55SCy Schubert ctx->buf[0] = 0x67452301;
89*a466cc55SCy Schubert ctx->buf[1] = 0xefcdab89;
90*a466cc55SCy Schubert ctx->buf[2] = 0x98badcfe;
91*a466cc55SCy Schubert ctx->buf[3] = 0x10325476;
92*a466cc55SCy Schubert
93*a466cc55SCy Schubert ctx->bytes[0] = 0;
94*a466cc55SCy Schubert ctx->bytes[1] = 0;
95*a466cc55SCy Schubert }
96*a466cc55SCy Schubert
97*a466cc55SCy Schubert void
isc_md5_invalidate(isc_md5_t * ctx)98*a466cc55SCy Schubert isc_md5_invalidate(isc_md5_t *ctx) {
99*a466cc55SCy Schubert memset(ctx, 0, sizeof(isc_md5_t));
100*a466cc55SCy Schubert }
101*a466cc55SCy Schubert
102*a466cc55SCy Schubert /*@{*/
103*a466cc55SCy Schubert /*! The four core functions - F1 is optimized somewhat */
104*a466cc55SCy Schubert
105*a466cc55SCy Schubert /* #define F1(x, y, z) (x & y | ~x & z) */
106*a466cc55SCy Schubert #define F1(x, y, z) (z ^ (x & (y ^ z)))
107*a466cc55SCy Schubert #define F2(x, y, z) F1(z, x, y)
108*a466cc55SCy Schubert #define F3(x, y, z) (x ^ y ^ z)
109*a466cc55SCy Schubert #define F4(x, y, z) (y ^ (x | ~z))
110*a466cc55SCy Schubert /*@}*/
111*a466cc55SCy Schubert
112*a466cc55SCy Schubert /*! This is the central step in the MD5 algorithm. */
113*a466cc55SCy Schubert #define MD5STEP(f,w,x,y,z,in,s) \
114*a466cc55SCy Schubert (w += f(x,y,z) + in, w = (w<<s | w>>(32-s)) + x)
115*a466cc55SCy Schubert
116*a466cc55SCy Schubert /*!
117*a466cc55SCy Schubert * The core of the MD5 algorithm, this alters an existing MD5 hash to
118*a466cc55SCy Schubert * reflect the addition of 16 longwords of new data. MD5Update blocks
119*a466cc55SCy Schubert * the data and converts bytes into longwords for this routine.
120*a466cc55SCy Schubert */
121*a466cc55SCy Schubert static void
transform(isc_uint32_t buf[4],isc_uint32_t const in[16])122*a466cc55SCy Schubert transform(isc_uint32_t buf[4], isc_uint32_t const in[16]) {
123*a466cc55SCy Schubert register isc_uint32_t a, b, c, d;
124*a466cc55SCy Schubert
125*a466cc55SCy Schubert a = buf[0];
126*a466cc55SCy Schubert b = buf[1];
127*a466cc55SCy Schubert c = buf[2];
128*a466cc55SCy Schubert d = buf[3];
129*a466cc55SCy Schubert
130*a466cc55SCy Schubert MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
131*a466cc55SCy Schubert MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
132*a466cc55SCy Schubert MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
133*a466cc55SCy Schubert MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
134*a466cc55SCy Schubert MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
135*a466cc55SCy Schubert MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
136*a466cc55SCy Schubert MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
137*a466cc55SCy Schubert MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
138*a466cc55SCy Schubert MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
139*a466cc55SCy Schubert MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
140*a466cc55SCy Schubert MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
141*a466cc55SCy Schubert MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
142*a466cc55SCy Schubert MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
143*a466cc55SCy Schubert MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
144*a466cc55SCy Schubert MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
145*a466cc55SCy Schubert MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
146*a466cc55SCy Schubert
147*a466cc55SCy Schubert MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
148*a466cc55SCy Schubert MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
149*a466cc55SCy Schubert MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
150*a466cc55SCy Schubert MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
151*a466cc55SCy Schubert MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
152*a466cc55SCy Schubert MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
153*a466cc55SCy Schubert MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
154*a466cc55SCy Schubert MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
155*a466cc55SCy Schubert MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
156*a466cc55SCy Schubert MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
157*a466cc55SCy Schubert MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
158*a466cc55SCy Schubert MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
159*a466cc55SCy Schubert MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
160*a466cc55SCy Schubert MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
161*a466cc55SCy Schubert MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
162*a466cc55SCy Schubert MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
163*a466cc55SCy Schubert
164*a466cc55SCy Schubert MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
165*a466cc55SCy Schubert MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
166*a466cc55SCy Schubert MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
167*a466cc55SCy Schubert MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
168*a466cc55SCy Schubert MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
169*a466cc55SCy Schubert MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
170*a466cc55SCy Schubert MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
171*a466cc55SCy Schubert MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
172*a466cc55SCy Schubert MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
173*a466cc55SCy Schubert MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
174*a466cc55SCy Schubert MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
175*a466cc55SCy Schubert MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
176*a466cc55SCy Schubert MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
177*a466cc55SCy Schubert MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
178*a466cc55SCy Schubert MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
179*a466cc55SCy Schubert MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
180*a466cc55SCy Schubert
181*a466cc55SCy Schubert MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
182*a466cc55SCy Schubert MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
183*a466cc55SCy Schubert MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
184*a466cc55SCy Schubert MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
185*a466cc55SCy Schubert MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
186*a466cc55SCy Schubert MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
187*a466cc55SCy Schubert MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
188*a466cc55SCy Schubert MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
189*a466cc55SCy Schubert MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
190*a466cc55SCy Schubert MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
191*a466cc55SCy Schubert MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
192*a466cc55SCy Schubert MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
193*a466cc55SCy Schubert MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
194*a466cc55SCy Schubert MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
195*a466cc55SCy Schubert MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
196*a466cc55SCy Schubert MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
197*a466cc55SCy Schubert
198*a466cc55SCy Schubert buf[0] += a;
199*a466cc55SCy Schubert buf[1] += b;
200*a466cc55SCy Schubert buf[2] += c;
201*a466cc55SCy Schubert buf[3] += d;
202*a466cc55SCy Schubert }
203*a466cc55SCy Schubert
204*a466cc55SCy Schubert /*!
205*a466cc55SCy Schubert * Update context to reflect the concatenation of another buffer full
206*a466cc55SCy Schubert * of bytes.
207*a466cc55SCy Schubert */
208*a466cc55SCy Schubert void
isc_md5_update(isc_md5_t * ctx,const unsigned char * buf,unsigned int len)209*a466cc55SCy Schubert isc_md5_update(isc_md5_t *ctx, const unsigned char *buf, unsigned int len) {
210*a466cc55SCy Schubert isc_uint32_t t;
211*a466cc55SCy Schubert
212*a466cc55SCy Schubert /* Update byte count */
213*a466cc55SCy Schubert
214*a466cc55SCy Schubert t = ctx->bytes[0];
215*a466cc55SCy Schubert if ((ctx->bytes[0] = t + len) < t)
216*a466cc55SCy Schubert ctx->bytes[1]++; /* Carry from low to high */
217*a466cc55SCy Schubert
218*a466cc55SCy Schubert t = 64 - (t & 0x3f); /* Space available in ctx->in (at least 1) */
219*a466cc55SCy Schubert if (t > len) {
220*a466cc55SCy Schubert memcpy((unsigned char *)ctx->in + 64 - t, buf, len);
221*a466cc55SCy Schubert return;
222*a466cc55SCy Schubert }
223*a466cc55SCy Schubert /* First chunk is an odd size */
224*a466cc55SCy Schubert memcpy((unsigned char *)ctx->in + 64 - t, buf, t);
225*a466cc55SCy Schubert byteSwap(ctx->in, 16);
226*a466cc55SCy Schubert transform(ctx->buf, ctx->in);
227*a466cc55SCy Schubert buf += t;
228*a466cc55SCy Schubert len -= t;
229*a466cc55SCy Schubert
230*a466cc55SCy Schubert /* Process data in 64-byte chunks */
231*a466cc55SCy Schubert while (len >= 64) {
232*a466cc55SCy Schubert memcpy(ctx->in, buf, 64);
233*a466cc55SCy Schubert byteSwap(ctx->in, 16);
234*a466cc55SCy Schubert transform(ctx->buf, ctx->in);
235*a466cc55SCy Schubert buf += 64;
236*a466cc55SCy Schubert len -= 64;
237*a466cc55SCy Schubert }
238*a466cc55SCy Schubert
239*a466cc55SCy Schubert /* Handle any remaining bytes of data. */
240*a466cc55SCy Schubert memcpy(ctx->in, buf, len);
241*a466cc55SCy Schubert }
242*a466cc55SCy Schubert
243*a466cc55SCy Schubert /*!
244*a466cc55SCy Schubert * Final wrapup - pad to 64-byte boundary with the bit pattern
245*a466cc55SCy Schubert * 1 0* (64-bit count of bits processed, MSB-first)
246*a466cc55SCy Schubert */
247*a466cc55SCy Schubert void
isc_md5_final(isc_md5_t * ctx,unsigned char * digest)248*a466cc55SCy Schubert isc_md5_final(isc_md5_t *ctx, unsigned char *digest) {
249*a466cc55SCy Schubert int count = ctx->bytes[0] & 0x3f; /* Number of bytes in ctx->in */
250*a466cc55SCy Schubert unsigned char *p = (unsigned char *)ctx->in + count;
251*a466cc55SCy Schubert
252*a466cc55SCy Schubert /* Set the first char of padding to 0x80. There is always room. */
253*a466cc55SCy Schubert *p++ = 0x80;
254*a466cc55SCy Schubert
255*a466cc55SCy Schubert /* Bytes of padding needed to make 56 bytes (-8..55) */
256*a466cc55SCy Schubert count = 56 - 1 - count;
257*a466cc55SCy Schubert
258*a466cc55SCy Schubert if (count < 0) { /* Padding forces an extra block */
259*a466cc55SCy Schubert memset(p, 0, count + 8);
260*a466cc55SCy Schubert byteSwap(ctx->in, 16);
261*a466cc55SCy Schubert transform(ctx->buf, ctx->in);
262*a466cc55SCy Schubert p = (unsigned char *)ctx->in;
263*a466cc55SCy Schubert count = 56;
264*a466cc55SCy Schubert }
265*a466cc55SCy Schubert memset(p, 0, count);
266*a466cc55SCy Schubert byteSwap(ctx->in, 14);
267*a466cc55SCy Schubert
268*a466cc55SCy Schubert /* Append length in bits and transform */
269*a466cc55SCy Schubert ctx->in[14] = ctx->bytes[0] << 3;
270*a466cc55SCy Schubert ctx->in[15] = ctx->bytes[1] << 3 | ctx->bytes[0] >> 29;
271*a466cc55SCy Schubert transform(ctx->buf, ctx->in);
272*a466cc55SCy Schubert
273*a466cc55SCy Schubert byteSwap(ctx->buf, 4);
274*a466cc55SCy Schubert memcpy(digest, ctx->buf, 16);
275*a466cc55SCy Schubert memset(ctx, 0, sizeof(isc_md5_t)); /* In case it's sensitive */
276*a466cc55SCy Schubert }
277*a466cc55SCy Schubert #endif
278