VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/alt-md5.cpp@ 81106

Last change on this file since 81106 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.2 KB
Line 
1/* $Id: alt-md5.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
2/** @file
3 * IPRT - MD5 message digest functions, alternative implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2019 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/*********************************************************************************************************************************
50* Header Files *
51*********************************************************************************************************************************/
52#include <iprt/md5.h>
53#include "internal/iprt.h"
54
55#include <iprt/string.h> /* for memcpy() */
56#if defined(RT_BIG_ENDIAN)
57# include <iprt/asm.h> /* RT_LE2H_U32 uses ASMByteSwapU32. */
58#endif
59
60
61/*********************************************************************************************************************************
62* Defined Constants And Macros *
63*********************************************************************************************************************************/
64/* The four core functions - F1 is optimized somewhat */
65#if 1
66/* #define F1(x, y, z) (x & y | ~x & z) */
67# define F1(x, y, z) (z ^ (x & (y ^ z)))
68# define F2(x, y, z) F1(z, x, y)
69# define F3(x, y, z) (x ^ y ^ z)
70# define F4(x, y, z) (y ^ (x | ~z))
71#else /* gcc 4.0.1 (x86) benefits from the explicitness of F1() here. */
72DECL_FORCE_INLINE(uint32_t) F1(uint32_t x, uint32_t y, uint32_t z)
73{
74 register uint32_t r = y ^ z;
75 r &= x;
76 r ^= z;
77 return r;
78}
79# define F2(x, y, z) F1(z, x, y)
80DECL_FORCE_INLINE(uint32_t) F3(uint32_t x, uint32_t y, uint32_t z)
81{
82 register uint32_t r = x ^ y;
83 r ^= z;
84 return r;
85}
86DECL_FORCE_INLINE(uint32_t) F4(uint32_t x, uint32_t y, uint32_t z)
87{
88 register uint32_t r = ~z;
89 r |= x;
90 r ^= y;
91 return r;
92}
93#endif
94
95/* This is the central step in the MD5 algorithm. */
96#define MD5STEP(f, w, x, y, z, data, s) \
97 ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
98
99
100/**
101 * The core of the MD5 algorithm, this alters an existing MD5 hash to reflect
102 * the addition of 16 longwords of new data. RTMd5Update blocks the data and
103 * converts bytes into longwords for this routine.
104 */
105static void rtMd5Transform(uint32_t buf[4], uint32_t const in[16])
106{
107 uint32_t a, b, c, d;
108
109 a = buf[0];
110 b = buf[1];
111 c = buf[2];
112 d = buf[3];
113
114 /* fn, w, x, y, z, data, s) */
115 MD5STEP(F1, a, b, c, d, in[ 0] + 0xd76aa478, 7);
116 MD5STEP(F1, d, a, b, c, in[ 1] + 0xe8c7b756, 12);
117 MD5STEP(F1, c, d, a, b, in[ 2] + 0x242070db, 17);
118 MD5STEP(F1, b, c, d, a, in[ 3] + 0xc1bdceee, 22);
119 MD5STEP(F1, a, b, c, d, in[ 4] + 0xf57c0faf, 7);
120 MD5STEP(F1, d, a, b, c, in[ 5] + 0x4787c62a, 12);
121 MD5STEP(F1, c, d, a, b, in[ 6] + 0xa8304613, 17);
122 MD5STEP(F1, b, c, d, a, in[ 7] + 0xfd469501, 22);
123 MD5STEP(F1, a, b, c, d, in[ 8] + 0x698098d8, 7);
124 MD5STEP(F1, d, a, b, c, in[ 9] + 0x8b44f7af, 12);
125 MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
126 MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
127 MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
128 MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
129 MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
130 MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
131
132 MD5STEP(F2, a, b, c, d, in[ 1] + 0xf61e2562, 5);
133 MD5STEP(F2, d, a, b, c, in[ 6] + 0xc040b340, 9);
134 MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
135 MD5STEP(F2, b, c, d, a, in[ 0] + 0xe9b6c7aa, 20);
136 MD5STEP(F2, a, b, c, d, in[ 5] + 0xd62f105d, 5);
137 MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
138 MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
139 MD5STEP(F2, b, c, d, a, in[ 4] + 0xe7d3fbc8, 20);
140 MD5STEP(F2, a, b, c, d, in[ 9] + 0x21e1cde6, 5);
141 MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
142 MD5STEP(F2, c, d, a, b, in[ 3] + 0xf4d50d87, 14);
143 MD5STEP(F2, b, c, d, a, in[ 8] + 0x455a14ed, 20);
144 MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
145 MD5STEP(F2, d, a, b, c, in[ 2] + 0xfcefa3f8, 9);
146 MD5STEP(F2, c, d, a, b, in[ 7] + 0x676f02d9, 14);
147 MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
148
149 MD5STEP(F3, a, b, c, d, in[ 5] + 0xfffa3942, 4);
150 MD5STEP(F3, d, a, b, c, in[ 8] + 0x8771f681, 11);
151 MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
152 MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
153 MD5STEP(F3, a, b, c, d, in[ 1] + 0xa4beea44, 4);
154 MD5STEP(F3, d, a, b, c, in[ 4] + 0x4bdecfa9, 11);
155 MD5STEP(F3, c, d, a, b, in[ 7] + 0xf6bb4b60, 16);
156 MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
157 MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
158 MD5STEP(F3, d, a, b, c, in[ 0] + 0xeaa127fa, 11);
159 MD5STEP(F3, c, d, a, b, in[ 3] + 0xd4ef3085, 16);
160 MD5STEP(F3, b, c, d, a, in[ 6] + 0x04881d05, 23);
161 MD5STEP(F3, a, b, c, d, in[ 9] + 0xd9d4d039, 4);
162 MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
163 MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
164 MD5STEP(F3, b, c, d, a, in[ 2] + 0xc4ac5665, 23);
165
166 MD5STEP(F4, a, b, c, d, in[ 0] + 0xf4292244, 6);
167 MD5STEP(F4, d, a, b, c, in[ 7] + 0x432aff97, 10);
168 MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
169 MD5STEP(F4, b, c, d, a, in[ 5] + 0xfc93a039, 21);
170 MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
171 MD5STEP(F4, d, a, b, c, in[ 3] + 0x8f0ccc92, 10);
172 MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
173 MD5STEP(F4, b, c, d, a, in[ 1] + 0x85845dd1, 21);
174 MD5STEP(F4, a, b, c, d, in[ 8] + 0x6fa87e4f, 6);
175 MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
176 MD5STEP(F4, c, d, a, b, in[ 6] + 0xa3014314, 15);
177 MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
178 MD5STEP(F4, a, b, c, d, in[ 4] + 0xf7537e82, 6);
179 MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
180 MD5STEP(F4, c, d, a, b, in[ 2] + 0x2ad7d2bb, 15);
181 MD5STEP(F4, b, c, d, a, in[ 9] + 0xeb86d391, 21);
182
183 buf[0] += a;
184 buf[1] += b;
185 buf[2] += c;
186 buf[3] += d;
187}
188
189
190#ifdef RT_BIG_ENDIAN
191/*
192 * Note: this code is harmless on little-endian machines.
193 */
194static void rtMd5ByteReverse(uint32_t *buf, unsigned int longs)
195{
196 uint32_t t;
197 do
198 {
199 t = *buf;
200 t = RT_LE2H_U32(t);
201 *buf = t;
202 buf++;
203 } while (--longs);
204}
205#else /* little endian - do nothing */
206# define rtMd5ByteReverse(buf, len) do { /* Nothing */ } while (0)
207#endif
208
209
210
211/*
212 * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
213 * initialization constants.
214 */
215RTDECL(void) RTMd5Init(PRTMD5CONTEXT pCtx)
216{
217 pCtx->AltPrivate.buf[0] = 0x67452301;
218 pCtx->AltPrivate.buf[1] = 0xefcdab89;
219 pCtx->AltPrivate.buf[2] = 0x98badcfe;
220 pCtx->AltPrivate.buf[3] = 0x10325476;
221
222 pCtx->AltPrivate.bits[0] = 0;
223 pCtx->AltPrivate.bits[1] = 0;
224}
225RT_EXPORT_SYMBOL(RTMd5Init);
226
227
228/*
229 * Update context to reflect the concatenation of another buffer full
230 * of bytes.
231 */
232RTDECL(void) RTMd5Update(PRTMD5CONTEXT pCtx, const void *pvBuf, size_t len)
233{
234 const uint8_t *buf = (const uint8_t *)pvBuf;
235 uint32_t t;
236
237 /* Update bitcount */
238 t = pCtx->AltPrivate.bits[0];
239 if ((pCtx->AltPrivate.bits[0] = t + ((uint32_t) len << 3)) < t)
240 pCtx->AltPrivate.bits[1]++; /* Carry from low to high */
241 pCtx->AltPrivate.bits[1] += (uint32_t)(len >> 29);
242
243 t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
244
245 /* Handle any leading odd-sized chunks */
246 if (t)
247 {
248 uint8_t *p = (uint8_t *) pCtx->AltPrivate.in + t;
249
250 t = 64 - t;
251 if (len < t)
252 {
253 memcpy(p, buf, len);
254 return;
255 }
256 memcpy(p, buf, t);
257 rtMd5ByteReverse(pCtx->AltPrivate.in, 16);
258 rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
259 buf += t;
260 len -= t;
261 }
262
263 /* Process data in 64-byte chunks */
264#ifndef RT_BIG_ENDIAN
265 if (!((uintptr_t)buf & 0x3))
266 {
267 while (len >= 64) {
268 rtMd5Transform(pCtx->AltPrivate.buf, (uint32_t const *)buf);
269 buf += 64;
270 len -= 64;
271 }
272 }
273 else
274#endif
275 {
276 while (len >= 64) {
277 memcpy(pCtx->AltPrivate.in, buf, 64);
278 rtMd5ByteReverse(pCtx->AltPrivate.in, 16);
279 rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
280 buf += 64;
281 len -= 64;
282 }
283 }
284
285 /* Handle any remaining bytes of data */
286 memcpy(pCtx->AltPrivate.in, buf, len);
287}
288RT_EXPORT_SYMBOL(RTMd5Update);
289
290
291/*
292 * Final wrapup - pad to 64-byte boundary with the bit pattern
293 * 1 0* (64-bit count of bits processed, MSB-first)
294 */
295RTDECL(void) RTMd5Final(uint8_t digest[16], PRTMD5CONTEXT pCtx)
296{
297 unsigned int count;
298 uint8_t *p;
299
300 /* Compute number of bytes mod 64 */
301 count = (pCtx->AltPrivate.bits[0] >> 3) & 0x3F;
302
303 /* Set the first char of padding to 0x80. This is safe since there is
304 always at least one byte free */
305 p = (uint8_t *)pCtx->AltPrivate.in + count;
306 *p++ = 0x80;
307
308 /* Bytes of padding needed to make 64 bytes */
309 count = 64 - 1 - count;
310
311 /* Pad out to 56 mod 64 */
312 if (count < 8)
313 {
314 /* Two lots of padding: Pad the first block to 64 bytes */
315 memset(p, 0, count);
316 rtMd5ByteReverse(pCtx->AltPrivate.in, 16);
317 rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
318
319 /* Now fill the next block with 56 bytes */
320 memset(pCtx->AltPrivate.in, 0, 56);
321 }
322 else
323 {
324 /* Pad block to 56 bytes */
325 memset(p, 0, count - 8);
326 }
327 rtMd5ByteReverse(pCtx->AltPrivate.in, 14);
328
329 /* Append length in bits and transform */
330 pCtx->AltPrivate.in[14] = pCtx->AltPrivate.bits[0];
331 pCtx->AltPrivate.in[15] = pCtx->AltPrivate.bits[1];
332
333 rtMd5Transform(pCtx->AltPrivate.buf, pCtx->AltPrivate.in);
334 rtMd5ByteReverse(pCtx->AltPrivate.buf, 4);
335 memcpy(digest, pCtx->AltPrivate.buf, 16);
336 memset(pCtx, 0, sizeof(*pCtx)); /* In case it's sensitive */
337}
338RT_EXPORT_SYMBOL(RTMd5Final);
339
340
341RTDECL(void) RTMd5(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTMD5HASHSIZE])
342{
343#if 0
344 RTMD5CONTEXT Ctx[2];
345 PRTMD5CONTEXT const pCtx = RT_ALIGN_PT(&Ctx[0], 64, PRTMD5CONTEXT);
346#else
347 RTMD5CONTEXT Ctx;
348 PRTMD5CONTEXT const pCtx = &Ctx;
349#endif
350
351 RTMd5Init(pCtx);
352 for (;;)
353 {
354 uint32_t cb = (uint32_t)RT_MIN(cbBuf, _2M);
355 RTMd5Update(pCtx, pvBuf, cb);
356 if (cb == cbBuf)
357 break;
358 cbBuf -= cb;
359 pvBuf = (uint8_t const *)pvBuf + cb;
360 }
361 RTMd5Final(pabDigest, pCtx);
362}
363RT_EXPORT_SYMBOL(RTMd5);
364
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