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