VirtualBox

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

Last change on this file was 106061, checked in by vboxsync, 6 weeks ago

Copyright year updates by scm.

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