1 | /* $Id: alt-sha1.cpp 69111 2017-10-17 14:26:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - SHA-1 hash functions, Alternative Implementation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2017 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 SHA-1 block size (in bytes). */
|
---|
32 | #define RTSHA1_BLOCK_SIZE 64U
|
---|
33 |
|
---|
34 | /** Enables the unrolled code. */
|
---|
35 | #define RTSHA1_UNROLLED 1
|
---|
36 |
|
---|
37 |
|
---|
38 | /*********************************************************************************************************************************
|
---|
39 | * Header Files *
|
---|
40 | *********************************************************************************************************************************/
|
---|
41 | #include "internal/iprt.h"
|
---|
42 | #include <iprt/types.h>
|
---|
43 | #include <iprt/assert.h>
|
---|
44 | #include <iprt/asm.h>
|
---|
45 | #include <iprt/string.h>
|
---|
46 |
|
---|
47 |
|
---|
48 | /** Our private context structure. */
|
---|
49 | typedef struct RTSHA1ALTPRIVATECTX
|
---|
50 | {
|
---|
51 | /** The W array.
|
---|
52 | * Buffering happens in the first 16 words, converted from big endian to host
|
---|
53 | * endian immediately before processing. The amount of buffered data is kept
|
---|
54 | * in the 6 least significant bits of cbMessage. */
|
---|
55 | uint32_t auW[80];
|
---|
56 | /** The message length (in bytes). */
|
---|
57 | uint64_t cbMessage;
|
---|
58 |
|
---|
59 | /** The 5 hash values. */
|
---|
60 | uint32_t auH[5];
|
---|
61 | } RTSHA1ALTPRIVATECTX;
|
---|
62 |
|
---|
63 | #define RT_SHA1_PRIVATE_ALT_CONTEXT
|
---|
64 | #include <iprt/sha.h>
|
---|
65 |
|
---|
66 |
|
---|
67 | AssertCompile(RT_SIZEOFMEMB(RTSHA1CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA1CONTEXT, AltPrivate));
|
---|
68 | AssertCompileMemberSize(RTSHA1ALTPRIVATECTX, auH, RTSHA1_HASH_SIZE);
|
---|
69 |
|
---|
70 |
|
---|
71 |
|
---|
72 |
|
---|
73 | RTDECL(void) RTSha1Init(PRTSHA1CONTEXT pCtx)
|
---|
74 | {
|
---|
75 | pCtx->AltPrivate.cbMessage = 0;
|
---|
76 | pCtx->AltPrivate.auH[0] = UINT32_C(0x67452301);
|
---|
77 | pCtx->AltPrivate.auH[1] = UINT32_C(0xefcdab89);
|
---|
78 | pCtx->AltPrivate.auH[2] = UINT32_C(0x98badcfe);
|
---|
79 | pCtx->AltPrivate.auH[3] = UINT32_C(0x10325476);
|
---|
80 | pCtx->AltPrivate.auH[4] = UINT32_C(0xc3d2e1f0);
|
---|
81 | }
|
---|
82 | RT_EXPORT_SYMBOL(RTSha1Init);
|
---|
83 |
|
---|
84 |
|
---|
85 | /**
|
---|
86 | * Initializes the auW array from the specfied input block.
|
---|
87 | *
|
---|
88 | * @param pCtx The SHA1 context.
|
---|
89 | * @param pbBlock The block. Must be 32-bit aligned.
|
---|
90 | */
|
---|
91 | DECLINLINE(void) rtSha1BlockInit(PRTSHA1CONTEXT pCtx, uint8_t const *pbBlock)
|
---|
92 | {
|
---|
93 | #ifdef RTSHA1_UNROLLED
|
---|
94 | uint32_t const *puSrc = (uint32_t const *)pbBlock;
|
---|
95 | uint32_t *puW = &pCtx->AltPrivate.auW[0];
|
---|
96 | Assert(!((uintptr_t)puSrc & 3));
|
---|
97 | Assert(!((uintptr_t)puW & 3));
|
---|
98 |
|
---|
99 | /* Copy and byte-swap the block. Initializing the rest of the Ws are done
|
---|
100 | in the processing loop. */
|
---|
101 | # ifdef RT_LITTLE_ENDIAN
|
---|
102 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
103 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
104 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
105 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
106 |
|
---|
107 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
108 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
109 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
110 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
111 |
|
---|
112 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
113 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
114 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
115 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
116 |
|
---|
117 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
118 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
119 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
120 | *puW++ = ASMByteSwapU32(*puSrc++);
|
---|
121 | # else
|
---|
122 | memcpy(puW, puSrc, RTSHA1_BLOCK_SIZE);
|
---|
123 | # endif
|
---|
124 |
|
---|
125 | #else /* !RTSHA1_UNROLLED */
|
---|
126 | uint32_t const *pu32Block = (uint32_t const *)pbBlock;
|
---|
127 | Assert(!((uintptr_t)pu32Block & 3));
|
---|
128 |
|
---|
129 | unsigned iWord;
|
---|
130 | for (iWord = 0; iWord < 16; iWord++)
|
---|
131 | pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]);
|
---|
132 |
|
---|
133 | for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
|
---|
134 | {
|
---|
135 | uint32_t u32 = pCtx->AltPrivate.auW[iWord - 16];
|
---|
136 | u32 ^= pCtx->AltPrivate.auW[iWord - 14];
|
---|
137 | u32 ^= pCtx->AltPrivate.auW[iWord - 8];
|
---|
138 | u32 ^= pCtx->AltPrivate.auW[iWord - 3];
|
---|
139 | pCtx->AltPrivate.auW[iWord] = ASMRotateLeftU32(u32, 1);
|
---|
140 | }
|
---|
141 | #endif /* !RTSHA1_UNROLLED */
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * Initializes the auW array from data buffered in the first part of the array.
|
---|
147 | *
|
---|
148 | * @param pCtx The SHA1 context.
|
---|
149 | */
|
---|
150 | DECLINLINE(void) rtSha1BlockInitBuffered(PRTSHA1CONTEXT pCtx)
|
---|
151 | {
|
---|
152 | #ifdef RTSHA1_UNROLLED
|
---|
153 | uint32_t *puW = &pCtx->AltPrivate.auW[0];
|
---|
154 | Assert(!((uintptr_t)puW & 3));
|
---|
155 |
|
---|
156 | /* Do the byte swap if necessary. Initializing the rest of the Ws are done
|
---|
157 | in the processing loop. */
|
---|
158 | # ifdef RT_LITTLE_ENDIAN
|
---|
159 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
160 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
161 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
162 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
163 |
|
---|
164 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
165 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
166 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
167 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
168 |
|
---|
169 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
170 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
171 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
172 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
173 |
|
---|
174 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
175 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
176 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
177 | *puW = ASMByteSwapU32(*puW); puW++;
|
---|
178 | # endif
|
---|
179 |
|
---|
180 | #else /* !RTSHA1_UNROLLED_INIT */
|
---|
181 | unsigned iWord;
|
---|
182 | for (iWord = 0; iWord < 16; iWord++)
|
---|
183 | pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]);
|
---|
184 |
|
---|
185 | for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
|
---|
186 | {
|
---|
187 | uint32_t u32 = pCtx->AltPrivate.auW[iWord - 16];
|
---|
188 | u32 ^= pCtx->AltPrivate.auW[iWord - 14];
|
---|
189 | u32 ^= pCtx->AltPrivate.auW[iWord - 8];
|
---|
190 | u32 ^= pCtx->AltPrivate.auW[iWord - 3];
|
---|
191 | pCtx->AltPrivate.auW[iWord] = ASMRotateLeftU32(u32, 1);
|
---|
192 | }
|
---|
193 | #endif /* !RTSHA1_UNROLLED_INIT */
|
---|
194 | }
|
---|
195 |
|
---|
196 |
|
---|
197 | /** Function 4.1, Ch(x,y,z). */
|
---|
198 | DECL_FORCE_INLINE(uint32_t) rtSha1Ch(uint32_t uX, uint32_t uY, uint32_t uZ)
|
---|
199 | {
|
---|
200 | #if 1
|
---|
201 | /* Optimization that saves one operation and probably a temporary variable. */
|
---|
202 | uint32_t uResult = uY;
|
---|
203 | uResult ^= uZ;
|
---|
204 | uResult &= uX;
|
---|
205 | uResult ^= uZ;
|
---|
206 | return uResult;
|
---|
207 | #else
|
---|
208 | /* The original. */
|
---|
209 | uint32_t uResult = uX & uY;
|
---|
210 | uResult ^= ~uX & uZ;
|
---|
211 | return uResult;
|
---|
212 | #endif
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /** Function 4.1, Parity(x,y,z). */
|
---|
217 | DECL_FORCE_INLINE(uint32_t) rtSha1Parity(uint32_t uX, uint32_t uY, uint32_t uZ)
|
---|
218 | {
|
---|
219 | uint32_t uResult = uX;
|
---|
220 | uResult ^= uY;
|
---|
221 | uResult ^= uZ;
|
---|
222 | return uResult;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /** Function 4.1, Maj(x,y,z). */
|
---|
227 | DECL_FORCE_INLINE(uint32_t) rtSha1Maj(uint32_t uX, uint32_t uY, uint32_t uZ)
|
---|
228 | {
|
---|
229 | #if 1
|
---|
230 | /* Optimization that save one operation and probably a temporary variable. */
|
---|
231 | uint32_t uResult = uY;
|
---|
232 | uResult ^= uZ;
|
---|
233 | uResult &= uX;
|
---|
234 | uResult ^= uY & uZ;
|
---|
235 | return uResult;
|
---|
236 | #else
|
---|
237 | /* The original. */
|
---|
238 | uint32_t uResult = (uX & uY);
|
---|
239 | uResult |= (uX & uZ);
|
---|
240 | uResult |= (uY & uZ);
|
---|
241 | return uResult;
|
---|
242 | #endif
|
---|
243 | }
|
---|
244 |
|
---|
245 |
|
---|
246 | /**
|
---|
247 | * Process the current block.
|
---|
248 | *
|
---|
249 | * Requires one of the rtSha1BlockInit functions to be called first.
|
---|
250 | *
|
---|
251 | * @param pCtx The SHA1 context.
|
---|
252 | */
|
---|
253 | static void rtSha1BlockProcess(PRTSHA1CONTEXT pCtx)
|
---|
254 | {
|
---|
255 | uint32_t uA = pCtx->AltPrivate.auH[0];
|
---|
256 | uint32_t uB = pCtx->AltPrivate.auH[1];
|
---|
257 | uint32_t uC = pCtx->AltPrivate.auH[2];
|
---|
258 | uint32_t uD = pCtx->AltPrivate.auH[3];
|
---|
259 | uint32_t uE = pCtx->AltPrivate.auH[4];
|
---|
260 |
|
---|
261 | #ifdef RTSHA1_UNROLLED
|
---|
262 | /* This fully unrolled version will avoid the variable rotation by
|
---|
263 | embedding it into the loop unrolling. */
|
---|
264 | uint32_t *puW = &pCtx->AltPrivate.auW[0];
|
---|
265 | # define SHA1_BODY(a_iWord, a_uK, a_fnFt, a_uA, a_uB, a_uC, a_uD, a_uE) \
|
---|
266 | do { \
|
---|
267 | if (a_iWord < 16) \
|
---|
268 | a_uE += *puW++; \
|
---|
269 | else \
|
---|
270 | { \
|
---|
271 | uint32_t u32 = puW[-16]; \
|
---|
272 | u32 ^= puW[-14]; \
|
---|
273 | u32 ^= puW[-8]; \
|
---|
274 | u32 ^= puW[-3]; \
|
---|
275 | u32 = ASMRotateLeftU32(u32, 1); \
|
---|
276 | *puW++ = u32; \
|
---|
277 | a_uE += u32; \
|
---|
278 | } \
|
---|
279 | a_uE += (a_uK); \
|
---|
280 | a_uE += ASMRotateLeftU32(a_uA, 5); \
|
---|
281 | a_uE += a_fnFt(a_uB, a_uC, a_uD); \
|
---|
282 | a_uB = ASMRotateLeftU32(a_uB, 30); \
|
---|
283 | } while (0)
|
---|
284 | # define FIVE_ITERATIONS(a_iFirst, a_uK, a_fnFt) \
|
---|
285 | do { \
|
---|
286 | SHA1_BODY(a_iFirst + 0, a_uK, a_fnFt, uA, uB, uC, uD, uE); \
|
---|
287 | SHA1_BODY(a_iFirst + 1, a_uK, a_fnFt, uE, uA, uB, uC, uD); \
|
---|
288 | SHA1_BODY(a_iFirst + 2, a_uK, a_fnFt, uD, uE, uA, uB, uC); \
|
---|
289 | SHA1_BODY(a_iFirst + 3, a_uK, a_fnFt, uC, uD, uE, uA, uB); \
|
---|
290 | SHA1_BODY(a_iFirst + 4, a_uK, a_fnFt, uB, uC, uD, uE, uA); \
|
---|
291 | } while (0)
|
---|
292 | # define TWENTY_ITERATIONS(a_iStart, a_uK, a_fnFt) \
|
---|
293 | do { \
|
---|
294 | FIVE_ITERATIONS(a_iStart + 0, a_uK, a_fnFt); \
|
---|
295 | FIVE_ITERATIONS(a_iStart + 5, a_uK, a_fnFt); \
|
---|
296 | FIVE_ITERATIONS(a_iStart + 10, a_uK, a_fnFt); \
|
---|
297 | FIVE_ITERATIONS(a_iStart + 15, a_uK, a_fnFt); \
|
---|
298 | } while (0)
|
---|
299 |
|
---|
300 | TWENTY_ITERATIONS( 0, UINT32_C(0x5a827999), rtSha1Ch);
|
---|
301 | TWENTY_ITERATIONS(20, UINT32_C(0x6ed9eba1), rtSha1Parity);
|
---|
302 | TWENTY_ITERATIONS(40, UINT32_C(0x8f1bbcdc), rtSha1Maj);
|
---|
303 | TWENTY_ITERATIONS(60, UINT32_C(0xca62c1d6), rtSha1Parity);
|
---|
304 |
|
---|
305 | #elif 1 /* Version avoiding the constant selection. */
|
---|
306 | unsigned iWord = 0;
|
---|
307 | # define TWENTY_ITERATIONS(a_iWordStop, a_uK, a_uExprBCD) \
|
---|
308 | for (; iWord < a_iWordStop; iWord++) \
|
---|
309 | { \
|
---|
310 | uint32_t uTemp = ASMRotateLeftU32(uA, 5); \
|
---|
311 | uTemp += (a_uExprBCD); \
|
---|
312 | uTemp += uE; \
|
---|
313 | uTemp += pCtx->AltPrivate.auW[iWord]; \
|
---|
314 | uTemp += (a_uK); \
|
---|
315 | \
|
---|
316 | uE = uD; \
|
---|
317 | uD = uC; \
|
---|
318 | uC = ASMRotateLeftU32(uB, 30); \
|
---|
319 | uB = uA; \
|
---|
320 | uA = uTemp; \
|
---|
321 | } do { } while (0)
|
---|
322 | TWENTY_ITERATIONS(20, UINT32_C(0x5a827999), rtSha1Ch(uB, uC, uD));
|
---|
323 | TWENTY_ITERATIONS(40, UINT32_C(0x6ed9eba1), rtSha1Parity(uB, uC, uD));
|
---|
324 | TWENTY_ITERATIONS(60, UINT32_C(0x8f1bbcdc), rtSha1Maj(uB, uC, uD));
|
---|
325 | TWENTY_ITERATIONS(80, UINT32_C(0xca62c1d6), rtSha1Parity(uB, uC, uD));
|
---|
326 |
|
---|
327 | #else /* Dead simple implementation. */
|
---|
328 | for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
|
---|
329 | {
|
---|
330 | uint32_t uTemp = ASMRotateLeftU32(uA, 5);
|
---|
331 | uTemp += uE;
|
---|
332 | uTemp += pCtx->AltPrivate.auW[iWord];
|
---|
333 | if (iWord <= 19)
|
---|
334 | {
|
---|
335 | uTemp += (uB & uC) | (~uB & uD);
|
---|
336 | uTemp += UINT32_C(0x5a827999);
|
---|
337 | }
|
---|
338 | else if (iWord <= 39)
|
---|
339 | {
|
---|
340 | uTemp += uB ^ uC ^ uD;
|
---|
341 | uTemp += UINT32_C(0x6ed9eba1);
|
---|
342 | }
|
---|
343 | else if (iWord <= 59)
|
---|
344 | {
|
---|
345 | uTemp += (uB & uC) | (uB & uD) | (uC & uD);
|
---|
346 | uTemp += UINT32_C(0x8f1bbcdc);
|
---|
347 | }
|
---|
348 | else
|
---|
349 | {
|
---|
350 | uTemp += uB ^ uC ^ uD;
|
---|
351 | uTemp += UINT32_C(0xca62c1d6);
|
---|
352 | }
|
---|
353 |
|
---|
354 | uE = uD;
|
---|
355 | uD = uC;
|
---|
356 | uC = ASMRotateLeftU32(uB, 30);
|
---|
357 | uB = uA;
|
---|
358 | uA = uTemp;
|
---|
359 | }
|
---|
360 | #endif
|
---|
361 |
|
---|
362 | pCtx->AltPrivate.auH[0] += uA;
|
---|
363 | pCtx->AltPrivate.auH[1] += uB;
|
---|
364 | pCtx->AltPrivate.auH[2] += uC;
|
---|
365 | pCtx->AltPrivate.auH[3] += uD;
|
---|
366 | pCtx->AltPrivate.auH[4] += uE;
|
---|
367 | }
|
---|
368 |
|
---|
369 |
|
---|
370 | RTDECL(void) RTSha1Update(PRTSHA1CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
|
---|
371 | {
|
---|
372 | Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 2);
|
---|
373 | uint8_t const *pbBuf = (uint8_t const *)pvBuf;
|
---|
374 |
|
---|
375 | /*
|
---|
376 | * Deal with buffered bytes first.
|
---|
377 | */
|
---|
378 | size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA1_BLOCK_SIZE - 1U);
|
---|
379 | if (cbBuffered)
|
---|
380 | {
|
---|
381 | size_t cbMissing = RTSHA1_BLOCK_SIZE - cbBuffered;
|
---|
382 | if (cbBuf >= cbMissing)
|
---|
383 | {
|
---|
384 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
|
---|
385 | pCtx->AltPrivate.cbMessage += cbMissing;
|
---|
386 | pbBuf += cbMissing;
|
---|
387 | cbBuf -= cbMissing;
|
---|
388 |
|
---|
389 | rtSha1BlockInitBuffered(pCtx);
|
---|
390 | rtSha1BlockProcess(pCtx);
|
---|
391 | }
|
---|
392 | else
|
---|
393 | {
|
---|
394 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
|
---|
395 | pCtx->AltPrivate.cbMessage += cbBuf;
|
---|
396 | return;
|
---|
397 | }
|
---|
398 | }
|
---|
399 |
|
---|
400 | if (!((uintptr_t)pbBuf & 3))
|
---|
401 | {
|
---|
402 | /*
|
---|
403 | * Process full blocks directly from the input buffer.
|
---|
404 | */
|
---|
405 | while (cbBuf >= RTSHA1_BLOCK_SIZE)
|
---|
406 | {
|
---|
407 | rtSha1BlockInit(pCtx, pbBuf);
|
---|
408 | rtSha1BlockProcess(pCtx);
|
---|
409 |
|
---|
410 | pCtx->AltPrivate.cbMessage += RTSHA1_BLOCK_SIZE;
|
---|
411 | pbBuf += RTSHA1_BLOCK_SIZE;
|
---|
412 | cbBuf -= RTSHA1_BLOCK_SIZE;
|
---|
413 | }
|
---|
414 | }
|
---|
415 | else
|
---|
416 | {
|
---|
417 | /*
|
---|
418 | * Unaligned input, so buffer it.
|
---|
419 | */
|
---|
420 | while (cbBuf >= RTSHA1_BLOCK_SIZE)
|
---|
421 | {
|
---|
422 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA1_BLOCK_SIZE);
|
---|
423 | rtSha1BlockInitBuffered(pCtx);
|
---|
424 | rtSha1BlockProcess(pCtx);
|
---|
425 |
|
---|
426 | pCtx->AltPrivate.cbMessage += RTSHA1_BLOCK_SIZE;
|
---|
427 | pbBuf += RTSHA1_BLOCK_SIZE;
|
---|
428 | cbBuf -= RTSHA1_BLOCK_SIZE;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | /*
|
---|
433 | * Stash any remaining bytes into the context buffer.
|
---|
434 | */
|
---|
435 | if (cbBuf > 0)
|
---|
436 | {
|
---|
437 | memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
|
---|
438 | pCtx->AltPrivate.cbMessage += cbBuf;
|
---|
439 | }
|
---|
440 | }
|
---|
441 | RT_EXPORT_SYMBOL(RTSha1Update);
|
---|
442 |
|
---|
443 |
|
---|
444 | static void rtSha1FinalInternal(PRTSHA1CONTEXT pCtx)
|
---|
445 | {
|
---|
446 | Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 2);
|
---|
447 |
|
---|
448 | /*
|
---|
449 | * Complete the message by adding a single bit (0x80), padding till
|
---|
450 | * the next 448-bit boundrary, the add the message length.
|
---|
451 | */
|
---|
452 | uint64_t const cMessageBits = pCtx->AltPrivate.cbMessage * 8;
|
---|
453 |
|
---|
454 | unsigned cbMissing = RTSHA1_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA1_BLOCK_SIZE - 1U));
|
---|
455 | static uint8_t const s_abSingleBitAndSomePadding[12] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
|
---|
456 | if (cbMissing < 1U + 8U)
|
---|
457 | /* Less than 64+8 bits left in the current block, force a new block. */
|
---|
458 | RTSha1Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
|
---|
459 | else
|
---|
460 | RTSha1Update(pCtx, &s_abSingleBitAndSomePadding, 1);
|
---|
461 |
|
---|
462 | unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage & (RTSHA1_BLOCK_SIZE - 1U);
|
---|
463 | cbMissing = RTSHA1_BLOCK_SIZE - cbBuffered;
|
---|
464 | Assert(cbMissing >= 8);
|
---|
465 | memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 8);
|
---|
466 |
|
---|
467 | *(uint64_t *)&pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits);
|
---|
468 |
|
---|
469 | /*
|
---|
470 | * Process the last buffered block constructed/completed above.
|
---|
471 | */
|
---|
472 | rtSha1BlockInitBuffered(pCtx);
|
---|
473 | rtSha1BlockProcess(pCtx);
|
---|
474 |
|
---|
475 | /*
|
---|
476 | * Convert the byte order of the hash words and we're done.
|
---|
477 | */
|
---|
478 | pCtx->AltPrivate.auH[0] = RT_H2BE_U32(pCtx->AltPrivate.auH[0]);
|
---|
479 | pCtx->AltPrivate.auH[1] = RT_H2BE_U32(pCtx->AltPrivate.auH[1]);
|
---|
480 | pCtx->AltPrivate.auH[2] = RT_H2BE_U32(pCtx->AltPrivate.auH[2]);
|
---|
481 | pCtx->AltPrivate.auH[3] = RT_H2BE_U32(pCtx->AltPrivate.auH[3]);
|
---|
482 | pCtx->AltPrivate.auH[4] = RT_H2BE_U32(pCtx->AltPrivate.auH[4]);
|
---|
483 | }
|
---|
484 |
|
---|
485 |
|
---|
486 | DECLINLINE(void) rtSha1WipeCtx(PRTSHA1CONTEXT pCtx)
|
---|
487 | {
|
---|
488 | RT_ZERO(pCtx->AltPrivate);
|
---|
489 | pCtx->AltPrivate.cbMessage = UINT64_MAX;
|
---|
490 | }
|
---|
491 |
|
---|
492 |
|
---|
493 | RTDECL(void) RTSha1Final(PRTSHA1CONTEXT pCtx, uint8_t pabDigest[RTSHA1_HASH_SIZE])
|
---|
494 | {
|
---|
495 | rtSha1FinalInternal(pCtx);
|
---|
496 | memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA1_HASH_SIZE);
|
---|
497 | rtSha1WipeCtx(pCtx);
|
---|
498 | }
|
---|
499 | RT_EXPORT_SYMBOL(RTSha1Final);
|
---|
500 |
|
---|
501 |
|
---|
502 | RTDECL(void) RTSha1(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA1_HASH_SIZE])
|
---|
503 | {
|
---|
504 | RTSHA1CONTEXT Ctx;
|
---|
505 | RTSha1Init(&Ctx);
|
---|
506 | RTSha1Update(&Ctx, pvBuf, cbBuf);
|
---|
507 | RTSha1Final(&Ctx, pabDigest);
|
---|
508 | }
|
---|
509 | RT_EXPORT_SYMBOL(RTSha1);
|
---|
510 |
|
---|
511 |
|
---|
512 | RTDECL(bool) RTSha1Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA1_HASH_SIZE])
|
---|
513 | {
|
---|
514 | RTSHA1CONTEXT Ctx;
|
---|
515 | RTSha1Init(&Ctx);
|
---|
516 | RTSha1Update(&Ctx, pvBuf, cbBuf);
|
---|
517 | rtSha1FinalInternal(&Ctx);
|
---|
518 |
|
---|
519 | bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA1_HASH_SIZE) == 0;
|
---|
520 |
|
---|
521 | rtSha1WipeCtx(&Ctx);
|
---|
522 | return fRet;
|
---|
523 | }
|
---|
524 | RT_EXPORT_SYMBOL(RTSha1Check);
|
---|
525 |
|
---|