VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 16 months ago

Copyright year updates by scm.

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

© 2023 Oracle
ContactPrivacy policyTerms of Use