VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/md5.cpp@ 47428

Last change on this file since 47428 was 45356, checked in by vboxsync, 12 years ago

tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1/* $Id: md5.cpp 45356 2013-04-05 07:01:27Z vboxsync $ */
2/** @file
3 * IPRT - MD5 message digest functions.
4 */
5
6/*
7 * Copyright (C) 2006-2013 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27/* The code is virtually unchanged from the original version (see copyright
28 * notice below). Most changes are related to the function names and data
29 * types - in order to fit the code in the IPRT naming style. */
30
31/*
32 * This code implements the MD5 message-digest algorithm.
33 * The algorithm is due to Ron Rivest. This code was
34 * written by Colin Plumb in 1993, no copyright is claimed.
35 * This code is in the public domain; do with it what you wish.
36 *
37 * Equivalent code is available from RSA Data Security, Inc.
38 * This code has been tested against that, and is equivalent,
39 * except that you don't need to include two pages of legalese
40 * with every copy.
41 *
42 * To compute the message digest of a chunk of bytes, declare an
43 * RTMD5CONTEXT structure, pass it to MD5Init, call MD5Update as
44 * needed on buffers full of bytes, and then call MD5Final, which
45 * will fill a supplied 16-byte array with the digest.
46 */
47
48/*******************************************************************************
49* Header Files *
50*******************************************************************************/
51#include <iprt/md5.h>
52#include "internal/iprt.h"
53
54#include <iprt/string.h> /* for memcpy() */
55#if defined(RT_BIG_ENDIAN)
56# include <iprt/asm.h> /* RT_LE2H_U32 uses ASMByteSwapU32. */
57#endif
58
59
60/*******************************************************************************
61* Defined Constants And Macros *
62*******************************************************************************/
63/* The four core functions - F1 is optimized somewhat */
64#if 1
65/* #define F1(x, y, z) (x & y | ~x & z) */
66# define F1(x, y, z) (z ^ (x & (y ^ z)))
67# define F2(x, y, z) F1(z, x, y)
68# define F3(x, y, z) (x ^ y ^ z)
69# define F4(x, y, z) (y ^ (x | ~z))
70#else /* gcc 4.0.1 (x86) benefits from the explicitness of F1() here. */
71DECL_FORCE_INLINE(uint32_t) F1(uint32_t x, uint32_t y, uint32_t z)
72{
73 register uint32_t r = y ^ z;
74 r &= x;
75 r ^= z;
76 return r;
77}
78# define F2(x, y, z) F1(z, x, y)
79DECL_FORCE_INLINE(uint32_t) F3(uint32_t x, uint32_t y, uint32_t z)
80{
81 register uint32_t r = x ^ y;
82 r ^= z;
83 return r;
84}
85DECL_FORCE_INLINE(uint32_t) F4(uint32_t x, uint32_t y, uint32_t z)
86{
87 register uint32_t r = ~z;
88 r |= x;
89 r ^= y;
90 return r;
91}
92#endif
93
94/* This is the central step in the MD5 algorithm. */
95#define MD5STEP(f, w, x, y, z, data, s) \
96 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
97
98
99/**
100 * The core of the MD5 algorithm, this alters an existing MD5 hash to reflect
101 * the addition of 16 longwords of new data. RTMd5Update blocks the data and
102 * converts bytes into longwords for this routine.
103 */
104static void rtMd5Transform(uint32_t buf[4], uint32_t const in[16])
105{
106 uint32_t a, b, c, d;
107
108 a = buf[0];
109 b = buf[1];
110 c = buf[2];
111 d = buf[3];
112
113 /* fn, w, x, y, z, data, s) */
114 MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478, 7);
115 MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
116 MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
117 MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
118 MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf, 7);
119 MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
120 MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
121 MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
122 MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8, 7);
123 MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
124 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
125 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
126 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
127 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
128 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
129 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
130
131 MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562, 5);
132 MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340, 9);
133 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
134 MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
135 MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d, 5);
136 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
137 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
138 MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
139 MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6, 5);
140 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
141 MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
142 MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
143 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
144 MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8, 9);
145 MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
146 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
147
148 MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942, 4);
149 MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
150 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
151 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
152 MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44, 4);
153 MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
154 MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
155 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
156 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
157 MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
158 MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
159 MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
160 MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039, 4);
161 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
162 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
163 MD5STEP(F3, b, c, d, a, in[ 2] + 0xc4ac5665, 23);
164
165 MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244, 6);
166 MD5STEP(F4, d, a, b, c, in[ 7] + 0x432aff97, 10);
167 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
168 MD5STEP(F4, b, c, d, a, in[ 5] + 0xfc93a039, 21);
169 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
170 MD5STEP(F4, d, a, b, c, in[ 3] + 0x8f0ccc92, 10);
171 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
172 MD5STEP(F4, b, c, d, a, in[ 1] + 0x85845dd1, 21);
173 MD5STEP(F4, a, b, c, d, in[ 8] + 0x6fa87e4f, 6);
174 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
175 MD5STEP(F4, c, d, a, b, in[ 6] + 0xa3014314, 15);
176 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
177 MD5STEP(F4, a, b, c, d, in[ 4] + 0xf7537e82, 6);
178 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
179 MD5STEP(F4, c, d, a, b, in[ 2] + 0x2ad7d2bb, 15);
180 MD5STEP(F4, b, c, d, a, in[ 9] + 0xeb86d391, 21);
181
182 buf[0] += a;
183 buf[1] += b;
184 buf[2] += c;
185 buf[3] += d;
186}
187
188
189#ifdef RT_BIG_ENDIAN
190/*
191 * Note: this code is harmless on little-endian machines.
192 */
193static void rtMd5ByteReverse(uint32_t *buf, unsigned int longs)
194{
195 uint32_t t;
196 do
197 {
198 t = *buf;
199 t = RT_LE2H_U32(t);
200 *buf = t;
201 buf++;
202 } while (--longs);
203}
204#else /* little endian - do nothing */
205# define rtMd5ByteReverse(buf, len) do { /* Nothing */ } while (0)
206#endif
207
208
209
210/*
211 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
212 * initialization constants.
213 */
214RTDECL(void) RTMd5Init(PRTMD5CONTEXT ctx)
215{
216 ctx->buf[0] = 0x67452301;
217 ctx->buf[1] = 0xefcdab89;
218 ctx->buf[2] = 0x98badcfe;
219 ctx->buf[3] = 0x10325476;
220
221 ctx->bits[0] = 0;
222 ctx->bits[1] = 0;
223}
224RT_EXPORT_SYMBOL(RTMd5Init);
225
226
227/*
228 * Update context to reflect the concatenation of another buffer full
229 * of bytes.
230 */
231RTDECL(void) RTMd5Update(PRTMD5CONTEXT ctx, const void *pvBuf, size_t len)
232{
233 const uint8_t *buf = (const uint8_t *)pvBuf;
234 uint32_t t;
235
236 /* Update bitcount */
237 t = ctx->bits[0];
238 if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
239 ctx->bits[1]++; /* Carry from low to high */
240 ctx->bits[1] += (uint32_t)(len >> 29);
241
242 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
243
244 /* Handle any leading odd-sized chunks */
245 if (t)
246 {
247 uint8_t *p = (uint8_t *) ctx->in + t;
248
249 t = 64 - t;
250 if (len < t)
251 {
252 memcpy(p, buf, len);
253 return;
254 }
255 memcpy(p, buf, t);
256 rtMd5ByteReverse(ctx->in, 16);
257 rtMd5Transform(ctx->buf, ctx->in);
258 buf += t;
259 len -= t;
260 }
261
262 /* Process data in 64-byte chunks */
263#ifndef RT_BIG_ENDIAN
264 if (!((uintptr_t)buf & 0x3))
265 {
266 while (len >= 64) {
267 rtMd5Transform(ctx->buf, (uint32_t const *)buf);
268 buf += 64;
269 len -= 64;
270 }
271 }
272 else
273#endif
274 {
275 while (len >= 64) {
276 memcpy(ctx->in, buf, 64);
277 rtMd5ByteReverse(ctx->in, 16);
278 rtMd5Transform(ctx->buf, ctx->in);
279 buf += 64;
280 len -= 64;
281 }
282 }
283
284 /* Handle any remaining bytes of data */
285 memcpy(ctx->in, buf, len);
286}
287RT_EXPORT_SYMBOL(RTMd5Update);
288
289
290/*
291 * Final wrapup - pad to 64-byte boundary with the bit pattern
292 * 1 0* (64-bit count of bits processed, MSB-first)
293 */
294RTDECL(void) RTMd5Final(uint8_t digest[16], PRTMD5CONTEXT ctx)
295{
296 unsigned int count;
297 uint8_t *p;
298
299 /* Compute number of bytes mod 64 */
300 count = (ctx->bits[0] >> 3) & 0x3F;
301
302 /* Set the first char of padding to 0x80. This is safe since there is
303 always at least one byte free */
304 p = (uint8_t *)ctx->in + count;
305 *p++ = 0x80;
306
307 /* Bytes of padding needed to make 64 bytes */
308 count = 64 - 1 - count;
309
310 /* Pad out to 56 mod 64 */
311 if (count < 8)
312 {
313 /* Two lots of padding: Pad the first block to 64 bytes */
314 memset(p, 0, count);
315 rtMd5ByteReverse(ctx->in, 16);
316 rtMd5Transform(ctx->buf, ctx->in);
317
318 /* Now fill the next block with 56 bytes */
319 memset(ctx->in, 0, 56);
320 }
321 else
322 {
323 /* Pad block to 56 bytes */
324 memset(p, 0, count - 8);
325 }
326 rtMd5ByteReverse(ctx->in, 14);
327
328 /* Append length in bits and transform */
329 ctx->in[14] = ctx->bits[0];
330 ctx->in[15] = ctx->bits[1];
331
332 rtMd5Transform(ctx->buf, ctx->in);
333 rtMd5ByteReverse(ctx->buf, 4);
334 memcpy(digest, ctx->buf, 16);
335 memset(ctx, 0, sizeof(*ctx)); /* In case it's sensitive */
336}
337RT_EXPORT_SYMBOL(RTMd5Final);
338
339
340RTDECL(void) RTMd5(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTMD5HASHSIZE])
341{
342#if 0
343 RTMD5CONTEXT Ctx[2];
344 PRTMD5CONTEXT const pCtx = RT_ALIGN_PT(&Ctx[0], 64, PRTMD5CONTEXT);
345#else
346 RTMD5CONTEXT Ctx;
347 PRTMD5CONTEXT const pCtx = &Ctx;
348#endif
349
350 RTMd5Init(pCtx);
351 for (;;)
352 {
353 uint32_t cb = (uint32_t)RT_MIN(cbBuf, _2M);
354 RTMd5Update(pCtx, pvBuf, cb);
355 if (cb == cbBuf)
356 break;
357 cbBuf -= cb;
358 pvBuf = (uint8_t const *)pvBuf + cb;
359 }
360 RTMd5Final(pabDigest, pCtx);
361}
362RT_EXPORT_SYMBOL(RTMd5);
363
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette