VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/alt-md4.cpp@ 87203

Last change on this file since 87203 was 82968, checked in by vboxsync, 5 years ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 11.0 KB
Line 
1/* $Id: alt-md4.cpp 82968 2020-02-04 10:35:17Z vboxsync $ */
2/** @file
3 * IPRT - Message-Digest Algorithm 4, Alternative Implementation.
4 */
5
6/*
7 * Copyright (C) 2009-2020 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
28/*********************************************************************************************************************************
29* Defined Constants And Macros *
30*********************************************************************************************************************************/
31/** The MD4 block size in bytes. */
32#define RTMD4_BLOCK_SIZE 64
33/** The MD4 block size in bits. */
34#define RTMD4_BLOCK_SIZE_IN_BITS (RTMD4_BLOCK_SIZE * 8)
35
36
37/*********************************************************************************************************************************
38* Header Files *
39*********************************************************************************************************************************/
40#include "internal/iprt.h"
41#include <iprt/types.h>
42
43
44/** Our private context structure. */
45typedef struct RTMD4ALTPRIVATECTX
46{
47 uint32_t uA;
48 uint32_t uB;
49 uint32_t uC;
50 uint32_t uD;
51 /** Message length in bits. */
52 uint64_t cTotalBits;
53 /** Input buffer. cTotalBits indicates how much is present. */
54 union
55 {
56 uint8_t abBuffer[RTMD4_BLOCK_SIZE];
57 uint32_t aX[RTMD4_BLOCK_SIZE / 4];
58 } u;
59} RTMD4ALTPRIVATECTX;
60
61
62#define RT_MD4_PRIVATE_ALT_CONTEXT
63#include <iprt/md4.h>
64
65#include <iprt/asm.h>
66#include <iprt/assert.h>
67#include <iprt/string.h>
68
69AssertCompile(RT_SIZEOFMEMB(RTMD4CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTMD4CONTEXT, AltPrivate));
70
71
72/*********************************************************************************************************************************
73* Global Variables *
74*********************************************************************************************************************************/
75/** MD4 padding. */
76static const uint8_t g_abMd4Padding[RTMD4_BLOCK_SIZE] =
77{
78 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
79 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
81 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
82};
83
84
85
86
87RTDECL(void) RTMd4Init(PRTMD4CONTEXT pCtx)
88{
89 pCtx->AltPrivate.uA = UINT32_C(0x67452301);
90 pCtx->AltPrivate.uB = UINT32_C(0xefcdab89);
91 pCtx->AltPrivate.uC = UINT32_C(0x98badcfe);
92 pCtx->AltPrivate.uD = UINT32_C(0x10325476);
93 pCtx->AltPrivate.cTotalBits = 0;
94 RT_ZERO(pCtx->AltPrivate.u);
95}
96RT_EXPORT_SYMBOL(RTMd4Init);
97
98
99DECLINLINE(uint32_t) rtMd4FuncF(uint32_t uX, uint32_t uY, uint32_t uZ)
100{
101 return (uX & uY) | (~uX & uZ);
102}
103
104DECLINLINE(uint32_t) rtMd4FuncG(uint32_t uX, uint32_t uY, uint32_t uZ)
105{
106 return (uX & (uY | uZ)) | (uY & uZ);
107}
108
109DECLINLINE(uint32_t) rtMd4FuncH(uint32_t uX, uint32_t uY, uint32_t uZ)
110{
111 return uX ^ uY ^ uZ;
112}
113
114
115/**
116 * Process the current block.
117 *
118 * Requires one of the rtMD4BlockInit functions to be called first.
119 *
120 * @param pCtx The MD4 context.
121 */
122DECLINLINE(void) rtMD4BlockProcess(PRTMD4CONTEXT pCtx)
123{
124#ifdef RT_BIG_ENDIAN
125 /* Convert the X array to little endian. */
126 for (uint32_t i = 0; i < RT_ELEMENTS(pCtx->AltPrivate.u.aX); i++)
127 pCtx->AltPrivate.u.aX[i] = RT_BSWAP_U32(pCtx->AltPrivate.u.aX[i]);
128#endif
129
130 /* Instead of saving A, B, C, D we copy them into variables and work on those. */
131 uint32_t A = pCtx->AltPrivate.uA;
132 uint32_t B = pCtx->AltPrivate.uB;
133 uint32_t C = pCtx->AltPrivate.uC;
134 uint32_t D = pCtx->AltPrivate.uD;
135
136 /* Round #1: */
137#define ABCD_K_S(a,b,c,d, k, s) ASMRotateLeftU32(a + rtMd4FuncF(b, c, d) + pCtx->AltPrivate.u.aX[k], s)
138 A = ABCD_K_S(A,B,C,D, 0, 3); D = ABCD_K_S(D,A,B,C, 1, 7); C = ABCD_K_S(C,D,A,B, 2, 11); B = ABCD_K_S(B,C,D,A, 3, 19);
139 A = ABCD_K_S(A,B,C,D, 4, 3); D = ABCD_K_S(D,A,B,C, 5, 7); C = ABCD_K_S(C,D,A,B, 6, 11); B = ABCD_K_S(B,C,D,A, 7, 19);
140 A = ABCD_K_S(A,B,C,D, 8, 3); D = ABCD_K_S(D,A,B,C, 9, 7); C = ABCD_K_S(C,D,A,B, 10, 11); B = ABCD_K_S(B,C,D,A, 11, 19);
141 A = ABCD_K_S(A,B,C,D, 12, 3); D = ABCD_K_S(D,A,B,C, 13, 7); C = ABCD_K_S(C,D,A,B, 14, 11); B = ABCD_K_S(B,C,D,A, 15, 19);
142#undef ABCD_K_S
143
144 /* Round #2: */
145#define ABCD_K_S(a,b,c,d, k, s) ASMRotateLeftU32(a + rtMd4FuncG(b, c, d) + pCtx->AltPrivate.u.aX[k] + UINT32_C(0x5a827999), s)
146 A = ABCD_K_S(A,B,C,D, 0, 3); D = ABCD_K_S(D,A,B,C, 4, 5); C = ABCD_K_S(C,D,A,B, 8, 9); B = ABCD_K_S(B,C,D,A, 12, 13);
147 A = ABCD_K_S(A,B,C,D, 1, 3); D = ABCD_K_S(D,A,B,C, 5, 5); C = ABCD_K_S(C,D,A,B, 9, 9); B = ABCD_K_S(B,C,D,A, 13, 13);
148 A = ABCD_K_S(A,B,C,D, 2, 3); D = ABCD_K_S(D,A,B,C, 6, 5); C = ABCD_K_S(C,D,A,B, 10, 9); B = ABCD_K_S(B,C,D,A, 14, 13);
149 A = ABCD_K_S(A,B,C,D, 3, 3); D = ABCD_K_S(D,A,B,C, 7, 5); C = ABCD_K_S(C,D,A,B, 11, 9); B = ABCD_K_S(B,C,D,A, 15, 13);
150#undef ABCD_K_S
151
152 /* Round #3: */
153#define ABCD_K_S(a,b,c,d, k, s) ASMRotateLeftU32(a + rtMd4FuncH(b, c, d) + pCtx->AltPrivate.u.aX[k] + UINT32_C(0x6ed9eba1), s)
154 A = ABCD_K_S(A,B,C,D, 0, 3); D = ABCD_K_S(D,A,B,C, 8, 9); C = ABCD_K_S(C,D,A,B, 4, 11); B = ABCD_K_S(B,C,D,A, 12, 15);
155 A = ABCD_K_S(A,B,C,D, 2, 3); D = ABCD_K_S(D,A,B,C, 10, 9); C = ABCD_K_S(C,D,A,B, 6, 11); B = ABCD_K_S(B,C,D,A, 14, 15);
156 A = ABCD_K_S(A,B,C,D, 1, 3); D = ABCD_K_S(D,A,B,C, 9, 9); C = ABCD_K_S(C,D,A,B, 5, 11); B = ABCD_K_S(B,C,D,A, 13, 15);
157 A = ABCD_K_S(A,B,C,D, 3, 3); D = ABCD_K_S(D,A,B,C, 11, 9); C = ABCD_K_S(C,D,A,B, 7, 11); B = ABCD_K_S(B,C,D,A, 15, 15);
158#undef ABCD_K_S
159
160 /* Perform the additions. */
161 pCtx->AltPrivate.uA += A;
162 pCtx->AltPrivate.uB += B;
163 pCtx->AltPrivate.uC += C;
164 pCtx->AltPrivate.uD += D;
165}
166
167
168RTDECL(void) RTMd4Update(PRTMD4CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
169{
170 uint8_t const *pbBuf = (uint8_t const *)pvBuf;
171
172 /*
173 * Deal with buffered bytes first.
174 */
175 if (pCtx->AltPrivate.cTotalBits & (RTMD4_BLOCK_SIZE_IN_BITS - 1))
176 {
177 uint8_t cbBuffered = (pCtx->AltPrivate.cTotalBits >> 3) & (RTMD4_BLOCK_SIZE - 1);
178 uint8_t cbMissing = RTMD4_BLOCK_SIZE - cbBuffered;
179 if (cbBuf >= cbMissing)
180 {
181 memcpy(&pCtx->AltPrivate.u.abBuffer[cbBuffered], pbBuf, cbMissing);
182 pCtx->AltPrivate.cTotalBits += cbMissing << 3;
183 pbBuf += cbMissing;
184 cbBuf -= cbMissing;
185
186 rtMD4BlockProcess(pCtx);
187 }
188 else
189 {
190 memcpy(&pCtx->AltPrivate.u.abBuffer[cbBuffered], pbBuf, cbBuf);
191 pCtx->AltPrivate.cTotalBits += cbBuf << 3;
192 return;
193 }
194 }
195
196 /*
197 * Process full blocks directly from the input buffer.
198 */
199 while (cbBuf >= RTMD4_BLOCK_SIZE)
200 {
201 memcpy(&pCtx->AltPrivate.u.abBuffer[0], pbBuf, RTMD4_BLOCK_SIZE);
202 rtMD4BlockProcess(pCtx);
203
204 pbBuf += RTMD4_BLOCK_SIZE;
205 cbBuf -= RTMD4_BLOCK_SIZE;
206 pCtx->AltPrivate.cTotalBits += RTMD4_BLOCK_SIZE_IN_BITS;
207 }
208
209 /*
210 * Stash any remaining bytes into the context buffer.
211 */
212 if (cbBuf > 0)
213 {
214 memcpy(&pCtx->AltPrivate.u.abBuffer[0], pbBuf, cbBuf);
215 pCtx->AltPrivate.cTotalBits += cbBuf << 3;
216 }
217}
218RT_EXPORT_SYMBOL(RTMd4Update);
219
220
221RTDECL(void) RTMd4Final(PRTMD4CONTEXT pCtx, uint8_t pabDigest[RTMD4_HASH_SIZE])
222{
223 uint64_t const cTotalBits = pCtx->AltPrivate.cTotalBits;
224
225 /*
226 * Pad input to block size minus sizeof(cTotalBits).
227 */
228 uint8_t cbMissing = RTMD4_BLOCK_SIZE - ((cTotalBits >> 3) & (RTMD4_BLOCK_SIZE - 1));
229 uint8_t cbPadding = cbMissing + (cbMissing > 8 ? 0 : RTMD4_BLOCK_SIZE) - 8;
230 Assert(cbPadding > 0 && cbPadding <= sizeof(g_abMd4Padding));
231 RTMd4Update(pCtx, g_abMd4Padding, cbPadding);
232 Assert(((pCtx->AltPrivate.cTotalBits >> 3) & (RTMD4_BLOCK_SIZE - 1)) == RTMD4_BLOCK_SIZE - 8);
233
234 /*
235 * Encode the total bitcount at the end of the buffer and do the final round.
236 */
237 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 8] = (uint8_t)(cTotalBits );
238 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 7] = (uint8_t)(cTotalBits >> 8);
239 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 6] = (uint8_t)(cTotalBits >> 16);
240 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 5] = (uint8_t)(cTotalBits >> 24);
241 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 4] = (uint8_t)(cTotalBits >> 32);
242 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 3] = (uint8_t)(cTotalBits >> 40);
243 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 2] = (uint8_t)(cTotalBits >> 48);
244 pCtx->AltPrivate.u.abBuffer[RTMD4_BLOCK_SIZE - 1] = (uint8_t)(cTotalBits >> 56);
245 rtMD4BlockProcess(pCtx);
246
247 /*
248 * Done. Just encode the digest.
249 */
250 pabDigest[ 0] = (uint8_t)(pCtx->AltPrivate.uA );
251 pabDigest[ 1] = (uint8_t)(pCtx->AltPrivate.uA >> 8);
252 pabDigest[ 2] = (uint8_t)(pCtx->AltPrivate.uA >> 16);
253 pabDigest[ 3] = (uint8_t)(pCtx->AltPrivate.uA >> 24);
254 pabDigest[ 4] = (uint8_t)(pCtx->AltPrivate.uB );
255 pabDigest[ 5] = (uint8_t)(pCtx->AltPrivate.uB >> 8);
256 pabDigest[ 6] = (uint8_t)(pCtx->AltPrivate.uB >> 16);
257 pabDigest[ 7] = (uint8_t)(pCtx->AltPrivate.uB >> 24);
258 pabDigest[ 8] = (uint8_t)(pCtx->AltPrivate.uC );
259 pabDigest[ 9] = (uint8_t)(pCtx->AltPrivate.uC >> 8);
260 pabDigest[10] = (uint8_t)(pCtx->AltPrivate.uC >> 16);
261 pabDigest[11] = (uint8_t)(pCtx->AltPrivate.uC >> 24);
262 pabDigest[12] = (uint8_t)(pCtx->AltPrivate.uD );
263 pabDigest[13] = (uint8_t)(pCtx->AltPrivate.uD >> 8);
264 pabDigest[14] = (uint8_t)(pCtx->AltPrivate.uD >> 16);
265 pabDigest[15] = (uint8_t)(pCtx->AltPrivate.uD >> 24);
266
267 /*
268 * Nuke the state.
269 */
270 RT_ZERO(pCtx->AltPrivate);
271}
272RT_EXPORT_SYMBOL(RTMd4Final);
273
274
275RTDECL(void) RTMd4(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTMD4_HASH_SIZE])
276{
277 RTMD4CONTEXT Ctx;
278 RTMd4Init(&Ctx);
279 RTMd4Update(&Ctx, pvBuf, cbBuf);
280 RTMd4Final(&Ctx, pabDigest);
281}
282RT_EXPORT_SYMBOL(RTMd4);
283
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