VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/alt-sha256.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 Author Date Id Revision
File size: 25.0 KB
Line 
1/* $Id: alt-sha256.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - SHA-256 and SHA-224 hash functions, Alternative Implementation.
4 */
5
6/*
7 * Copyright (C) 2009-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
38/*********************************************************************************************************************************
39* Defined Constants And Macros *
40*********************************************************************************************************************************/
41/** The SHA-256 block size (in bytes). */
42#define RTSHA256_BLOCK_SIZE 64U
43
44/** Enables the unrolled code. */
45#define RTSHA256_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 RTSHA256ALTPRIVATECTX
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[64];
66 /** The message length (in bytes). */
67 uint64_t cbMessage;
68 /** The 8 hash values. */
69 uint32_t auH[8];
70} RTSHA256ALTPRIVATECTX;
71
72#define RT_SHA256_PRIVATE_ALT_CONTEXT
73#include <iprt/sha.h>
74
75
76AssertCompile(RT_SIZEOFMEMB(RTSHA256CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA256CONTEXT, AltPrivate));
77AssertCompileMemberSize(RTSHA256ALTPRIVATECTX, auH, RTSHA256_HASH_SIZE);
78
79
80/*********************************************************************************************************************************
81* Global Variables *
82*********************************************************************************************************************************/
83#ifndef RTSHA256_UNROLLED
84/** The K constants */
85static uint32_t const g_auKs[] =
86{
87 UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
88 UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5),
89 UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
90 UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174),
91 UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
92 UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da),
93 UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
94 UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967),
95 UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
96 UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85),
97 UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
98 UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070),
99 UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
100 UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3),
101 UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
102 UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2),
103};
104#endif /* !RTSHA256_UNROLLED */
105
106
107
108RTDECL(void) RTSha256Init(PRTSHA256CONTEXT pCtx)
109{
110 pCtx->AltPrivate.cbMessage = 0;
111 pCtx->AltPrivate.auH[0] = UINT32_C(0x6a09e667);
112 pCtx->AltPrivate.auH[1] = UINT32_C(0xbb67ae85);
113 pCtx->AltPrivate.auH[2] = UINT32_C(0x3c6ef372);
114 pCtx->AltPrivate.auH[3] = UINT32_C(0xa54ff53a);
115 pCtx->AltPrivate.auH[4] = UINT32_C(0x510e527f);
116 pCtx->AltPrivate.auH[5] = UINT32_C(0x9b05688c);
117 pCtx->AltPrivate.auH[6] = UINT32_C(0x1f83d9ab);
118 pCtx->AltPrivate.auH[7] = UINT32_C(0x5be0cd19);
119}
120RT_EXPORT_SYMBOL(RTSha256Init);
121
122
123/** Function 4.2. */
124DECL_FORCE_INLINE(uint32_t) rtSha256Ch(uint32_t uX, uint32_t uY, uint32_t uZ)
125{
126#if 1
127 /* Optimization that saves one operation and probably a temporary variable. */
128 uint32_t uResult = uY;
129 uResult ^= uZ;
130 uResult &= uX;
131 uResult ^= uZ;
132 return uResult;
133#else
134 /* The original. */
135 uint32_t uResult = uX & uY;
136 uResult ^= ~uX & uZ;
137 return uResult;
138#endif
139}
140
141
142/** Function 4.3. */
143DECL_FORCE_INLINE(uint32_t) rtSha256Maj(uint32_t uX, uint32_t uY, uint32_t uZ)
144{
145#if 1
146 /* Optimization that save one operation and probably a temporary variable. */
147 uint32_t uResult = uY;
148 uResult ^= uZ;
149 uResult &= uX;
150 uResult ^= uY & uZ;
151 return uResult;
152#else
153 /* The original. */
154 uint32_t uResult = uX & uY;
155 uResult ^= uX & uZ;
156 uResult ^= uY & uZ;
157 return uResult;
158#endif
159}
160
161
162/** Function 4.4. */
163DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma0(uint32_t uX)
164{
165 uint32_t uResult = uX = ASMRotateRightU32(uX, 2);
166 uX = ASMRotateRightU32(uX, 13 - 2);
167 uResult ^= uX;
168 uX = ASMRotateRightU32(uX, 22 - 13);
169 uResult ^= uX;
170 return uResult;
171}
172
173
174/** Function 4.5. */
175DECL_FORCE_INLINE(uint32_t) rtSha256CapitalSigma1(uint32_t uX)
176{
177 uint32_t uResult = uX = ASMRotateRightU32(uX, 6);
178 uX = ASMRotateRightU32(uX, 11 - 6);
179 uResult ^= uX;
180 uX = ASMRotateRightU32(uX, 25 - 11);
181 uResult ^= uX;
182 return uResult;
183}
184
185
186/** Function 4.6. */
187DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma0(uint32_t uX)
188{
189 uint32_t uResult = uX >> 3;
190 uX = ASMRotateRightU32(uX, 7);
191 uResult ^= uX;
192 uX = ASMRotateRightU32(uX, 18 - 7);
193 uResult ^= uX;
194 return uResult;
195}
196
197
198/** Function 4.7. */
199DECL_FORCE_INLINE(uint32_t) rtSha256SmallSigma1(uint32_t uX)
200{
201 uint32_t uResult = uX >> 10;
202 uX = ASMRotateRightU32(uX, 17);
203 uResult ^= uX;
204 uX = ASMRotateRightU32(uX, 19 - 17);
205 uResult ^= uX;
206 return uResult;
207}
208
209
210/**
211 * Initializes the auW array from the specfied input block.
212 *
213 * @param pCtx The SHA-256 context.
214 * @param pbBlock The block. Must be arch-bit-width aligned.
215 */
216DECLINLINE(void) rtSha256BlockInit(PRTSHA256CONTEXT pCtx, uint8_t const *pbBlock)
217{
218#ifdef RTSHA256_UNROLLED
219 /* Copy and byte-swap the block. Initializing the rest of the Ws are done
220 in the processing loop. */
221# ifdef RT_LITTLE_ENDIAN
222# if 0 /* Just an idea... very little gain as this isn't the expensive code. */
223 __m128i const uBSwapConst = { 3, 2, 1, 0, 7, 6, 5, 4, 11, 10, 9, 8, 15, 14, 13, 12 };
224 __m128i const *puSrc = (__m128i const *)pbBlock;
225 __m128i *puDst = (__m128i *)&pCtx->AltPrivate.auW[0];
226
227 _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
228 _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
229 _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
230 _mm_storeu_si128(puDst, _mm_shuffle_epi8(_mm_loadu_si128(puSrc), uBSwapConst)); puDst++; puSrc++;
231
232# elif ARCH_BITS == 64
233 uint64_t const *puSrc = (uint64_t const *)pbBlock;
234 uint64_t *puW = (uint64_t *)&pCtx->AltPrivate.auW[0];
235 Assert(!((uintptr_t)puSrc & 7));
236 Assert(!((uintptr_t)puW & 7));
237
238 /* b0 b1 b2 b3 b4 b5 b6 b7 --bwap--> b7 b6 b5 b4 b3 b2 b1 b0 --ror--> b3 b2 b1 b0 b7 b6 b5 b4; */
239 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
240 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
241 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
242 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
243
244 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
245 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
246 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
247 *puW++ = ASMRotateRightU64(ASMByteSwapU64(*puSrc++), 32);
248
249# else
250 uint32_t const *puSrc = (uint32_t const *)pbBlock;
251 uint32_t *puW = &pCtx->AltPrivate.auW[0];
252 Assert(!((uintptr_t)puSrc & 3));
253 Assert(!((uintptr_t)puW & 3));
254
255 *puW++ = ASMByteSwapU32(*puSrc++);
256 *puW++ = ASMByteSwapU32(*puSrc++);
257 *puW++ = ASMByteSwapU32(*puSrc++);
258 *puW++ = ASMByteSwapU32(*puSrc++);
259
260 *puW++ = ASMByteSwapU32(*puSrc++);
261 *puW++ = ASMByteSwapU32(*puSrc++);
262 *puW++ = ASMByteSwapU32(*puSrc++);
263 *puW++ = ASMByteSwapU32(*puSrc++);
264
265 *puW++ = ASMByteSwapU32(*puSrc++);
266 *puW++ = ASMByteSwapU32(*puSrc++);
267 *puW++ = ASMByteSwapU32(*puSrc++);
268 *puW++ = ASMByteSwapU32(*puSrc++);
269
270 *puW++ = ASMByteSwapU32(*puSrc++);
271 *puW++ = ASMByteSwapU32(*puSrc++);
272 *puW++ = ASMByteSwapU32(*puSrc++);
273 *puW++ = ASMByteSwapU32(*puSrc++);
274# endif
275# else /* RT_BIG_ENDIAN */
276 memcpy(&pCtx->AltPrivate.auW[0], pbBlock, RTSHA256_BLOCK_SIZE);
277# endif /* RT_BIG_ENDIAN */
278
279#else /* !RTSHA256_UNROLLED */
280 uint32_t const *pu32Block = (uint32_t const *)pbBlock;
281 Assert(!((uintptr_t)pu32Block & 3));
282
283 unsigned iWord;
284 for (iWord = 0; iWord < 16; iWord++)
285 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pu32Block[iWord]);
286
287 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
288 {
289 uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
290 u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
291 u32 += pCtx->AltPrivate.auW[iWord - 7];
292 u32 += pCtx->AltPrivate.auW[iWord - 16];
293 pCtx->AltPrivate.auW[iWord] = u32;
294 }
295#endif /* !RTSHA256_UNROLLED */
296}
297
298
299/**
300 * Initializes the auW array from data buffered in the first part of the array.
301 *
302 * @param pCtx The SHA-256 context.
303 */
304DECLINLINE(void) rtSha256BlockInitBuffered(PRTSHA256CONTEXT pCtx)
305{
306#ifdef RTSHA256_UNROLLED
307 /* Do the byte swap if necessary. Initializing the rest of the Ws are done
308 in the processing loop. */
309# ifdef RT_LITTLE_ENDIAN
310# if ARCH_BITS == 64
311 uint64_t *puW = (uint64_t *)&pCtx->AltPrivate.auW[0];
312 Assert(!((uintptr_t)puW & 7));
313 /* b0 b1 b2 b3 b4 b5 b6 b7 --bwap--> b7 b6 b5 b4 b3 b2 b1 b0 --ror--> b3 b2 b1 b0 b7 b6 b5 b4; */
314 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
315 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
316 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
317 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
318
319 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
320 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
321 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
322 *puW = ASMRotateRightU64(ASMByteSwapU64(*puW), 32); puW++;
323
324# else
325 uint32_t *puW = &pCtx->AltPrivate.auW[0];
326 Assert(!((uintptr_t)puW & 3));
327
328 *puW = ASMByteSwapU32(*puW); puW++;
329 *puW = ASMByteSwapU32(*puW); puW++;
330 *puW = ASMByteSwapU32(*puW); puW++;
331 *puW = ASMByteSwapU32(*puW); puW++;
332
333 *puW = ASMByteSwapU32(*puW); puW++;
334 *puW = ASMByteSwapU32(*puW); puW++;
335 *puW = ASMByteSwapU32(*puW); puW++;
336 *puW = ASMByteSwapU32(*puW); puW++;
337
338 *puW = ASMByteSwapU32(*puW); puW++;
339 *puW = ASMByteSwapU32(*puW); puW++;
340 *puW = ASMByteSwapU32(*puW); puW++;
341 *puW = ASMByteSwapU32(*puW); puW++;
342
343 *puW = ASMByteSwapU32(*puW); puW++;
344 *puW = ASMByteSwapU32(*puW); puW++;
345 *puW = ASMByteSwapU32(*puW); puW++;
346 *puW = ASMByteSwapU32(*puW); puW++;
347# endif
348# endif
349
350#else /* !RTSHA256_UNROLLED */
351 unsigned iWord;
352 for (iWord = 0; iWord < 16; iWord++)
353 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U32(pCtx->AltPrivate.auW[iWord]);
354
355 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
356 {
357 uint32_t u32 = rtSha256SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
358 u32 += rtSha256SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
359 u32 += pCtx->AltPrivate.auW[iWord - 7];
360 u32 += pCtx->AltPrivate.auW[iWord - 16];
361 pCtx->AltPrivate.auW[iWord] = u32;
362 }
363#endif /* !RTSHA256_UNROLLED */
364}
365
366
367/**
368 * Process the current block.
369 *
370 * Requires one of the rtSha256BlockInit functions to be called first.
371 *
372 * @param pCtx The SHA-256 context.
373 */
374static void rtSha256BlockProcess(PRTSHA256CONTEXT pCtx)
375{
376 uint32_t uA = pCtx->AltPrivate.auH[0];
377 uint32_t uB = pCtx->AltPrivate.auH[1];
378 uint32_t uC = pCtx->AltPrivate.auH[2];
379 uint32_t uD = pCtx->AltPrivate.auH[3];
380 uint32_t uE = pCtx->AltPrivate.auH[4];
381 uint32_t uF = pCtx->AltPrivate.auH[5];
382 uint32_t uG = pCtx->AltPrivate.auH[6];
383 uint32_t uH = pCtx->AltPrivate.auH[7];
384
385#ifdef RTSHA256_UNROLLED
386 uint32_t *puW = &pCtx->AltPrivate.auW[0];
387# define RTSHA256_BODY(a_iWord, a_uK, a_uA, a_uB, a_uC, a_uD, a_uE, a_uF, a_uG, a_uH) \
388 do { \
389 if ((a_iWord) < 16) \
390 a_uH += *puW++; \
391 else \
392 { \
393 uint32_t u32 = puW[-16]; \
394 u32 += rtSha256SmallSigma0(puW[-15]); \
395 u32 += puW[-7]; \
396 u32 += rtSha256SmallSigma1(puW[-2]); \
397 if (a_iWord < 64-2) *puW++ = u32; else puW++; \
398 a_uH += u32; \
399 } \
400 \
401 a_uH += rtSha256CapitalSigma1(a_uE); \
402 a_uH += a_uK; \
403 a_uH += rtSha256Ch(a_uE, a_uF, a_uG); \
404 a_uD += a_uH; \
405 \
406 a_uH += rtSha256CapitalSigma0(a_uA); \
407 a_uH += rtSha256Maj(a_uA, a_uB, a_uC); \
408 } while (0)
409# define RTSHA256_EIGHT(a_uK0, a_uK1, a_uK2, a_uK3, a_uK4, a_uK5, a_uK6, a_uK7, a_iFirst) \
410 do { \
411 RTSHA256_BODY(a_iFirst + 0, a_uK0, uA, uB, uC, uD, uE, uF, uG, uH); \
412 RTSHA256_BODY(a_iFirst + 1, a_uK1, uH, uA, uB, uC, uD, uE, uF, uG); \
413 RTSHA256_BODY(a_iFirst + 2, a_uK2, uG, uH, uA, uB, uC, uD, uE, uF); \
414 RTSHA256_BODY(a_iFirst + 3, a_uK3, uF, uG, uH, uA, uB, uC, uD, uE); \
415 RTSHA256_BODY(a_iFirst + 4, a_uK4, uE, uF, uG, uH, uA, uB, uC, uD); \
416 RTSHA256_BODY(a_iFirst + 5, a_uK5, uD, uE, uF, uG, uH, uA, uB, uC); \
417 RTSHA256_BODY(a_iFirst + 6, a_uK6, uC, uD, uE, uF, uG, uH, uA, uB); \
418 RTSHA256_BODY(a_iFirst + 7, a_uK7, uB, uC, uD, uE, uF, uG, uH, uA); \
419 } while (0)
420 RTSHA256_EIGHT(UINT32_C(0x428a2f98), UINT32_C(0x71374491), UINT32_C(0xb5c0fbcf), UINT32_C(0xe9b5dba5),
421 UINT32_C(0x3956c25b), UINT32_C(0x59f111f1), UINT32_C(0x923f82a4), UINT32_C(0xab1c5ed5), 0);
422 RTSHA256_EIGHT(UINT32_C(0xd807aa98), UINT32_C(0x12835b01), UINT32_C(0x243185be), UINT32_C(0x550c7dc3),
423 UINT32_C(0x72be5d74), UINT32_C(0x80deb1fe), UINT32_C(0x9bdc06a7), UINT32_C(0xc19bf174), 8);
424 RTSHA256_EIGHT(UINT32_C(0xe49b69c1), UINT32_C(0xefbe4786), UINT32_C(0x0fc19dc6), UINT32_C(0x240ca1cc),
425 UINT32_C(0x2de92c6f), UINT32_C(0x4a7484aa), UINT32_C(0x5cb0a9dc), UINT32_C(0x76f988da), 16);
426 RTSHA256_EIGHT(UINT32_C(0x983e5152), UINT32_C(0xa831c66d), UINT32_C(0xb00327c8), UINT32_C(0xbf597fc7),
427 UINT32_C(0xc6e00bf3), UINT32_C(0xd5a79147), UINT32_C(0x06ca6351), UINT32_C(0x14292967), 24);
428 RTSHA256_EIGHT(UINT32_C(0x27b70a85), UINT32_C(0x2e1b2138), UINT32_C(0x4d2c6dfc), UINT32_C(0x53380d13),
429 UINT32_C(0x650a7354), UINT32_C(0x766a0abb), UINT32_C(0x81c2c92e), UINT32_C(0x92722c85), 32);
430 RTSHA256_EIGHT(UINT32_C(0xa2bfe8a1), UINT32_C(0xa81a664b), UINT32_C(0xc24b8b70), UINT32_C(0xc76c51a3),
431 UINT32_C(0xd192e819), UINT32_C(0xd6990624), UINT32_C(0xf40e3585), UINT32_C(0x106aa070), 40);
432 RTSHA256_EIGHT(UINT32_C(0x19a4c116), UINT32_C(0x1e376c08), UINT32_C(0x2748774c), UINT32_C(0x34b0bcb5),
433 UINT32_C(0x391c0cb3), UINT32_C(0x4ed8aa4a), UINT32_C(0x5b9cca4f), UINT32_C(0x682e6ff3), 48);
434 RTSHA256_EIGHT(UINT32_C(0x748f82ee), UINT32_C(0x78a5636f), UINT32_C(0x84c87814), UINT32_C(0x8cc70208),
435 UINT32_C(0x90befffa), UINT32_C(0xa4506ceb), UINT32_C(0xbef9a3f7), UINT32_C(0xc67178f2), 56);
436
437#else /* !RTSHA256_UNROLLED */
438 for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
439 {
440 uint32_t uT1 = uH;
441 uT1 += rtSha256CapitalSigma1(uE);
442 uT1 += rtSha256Ch(uE, uF, uG);
443 uT1 += g_auKs[iWord];
444 uT1 += pCtx->AltPrivate.auW[iWord];
445
446 uint32_t uT2 = rtSha256CapitalSigma0(uA);
447 uT2 += rtSha256Maj(uA, uB, uC);
448
449 uH = uG;
450 uG = uF;
451 uF = uE;
452 uE = uD + uT1;
453 uD = uC;
454 uC = uB;
455 uB = uA;
456 uA = uT1 + uT2;
457 }
458#endif /* !RTSHA256_UNROLLED */
459
460 pCtx->AltPrivate.auH[0] += uA;
461 pCtx->AltPrivate.auH[1] += uB;
462 pCtx->AltPrivate.auH[2] += uC;
463 pCtx->AltPrivate.auH[3] += uD;
464 pCtx->AltPrivate.auH[4] += uE;
465 pCtx->AltPrivate.auH[5] += uF;
466 pCtx->AltPrivate.auH[6] += uG;
467 pCtx->AltPrivate.auH[7] += uH;
468}
469
470
471RTDECL(void) RTSha256Update(PRTSHA256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
472{
473 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
474 uint8_t const *pbBuf = (uint8_t const *)pvBuf;
475
476 /*
477 * Deal with buffered bytes first.
478 */
479 size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
480 if (cbBuffered)
481 {
482 size_t cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
483 if (cbBuf >= cbMissing)
484 {
485 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
486 pCtx->AltPrivate.cbMessage += cbMissing;
487 pbBuf += cbMissing;
488 cbBuf -= cbMissing;
489
490 rtSha256BlockInitBuffered(pCtx);
491 rtSha256BlockProcess(pCtx);
492 }
493 else
494 {
495 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
496 pCtx->AltPrivate.cbMessage += cbBuf;
497 return;
498 }
499 }
500
501 if (!((uintptr_t)pbBuf & (sizeof(void *) - 1)))
502 {
503 /*
504 * Process full blocks directly from the input buffer.
505 */
506 while (cbBuf >= RTSHA256_BLOCK_SIZE)
507 {
508 rtSha256BlockInit(pCtx, pbBuf);
509 rtSha256BlockProcess(pCtx);
510
511 pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE;
512 pbBuf += RTSHA256_BLOCK_SIZE;
513 cbBuf -= RTSHA256_BLOCK_SIZE;
514 }
515 }
516 else
517 {
518 /*
519 * Unaligned input, so buffer it.
520 */
521 while (cbBuf >= RTSHA256_BLOCK_SIZE)
522 {
523 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA256_BLOCK_SIZE);
524 rtSha256BlockInitBuffered(pCtx);
525 rtSha256BlockProcess(pCtx);
526
527 pCtx->AltPrivate.cbMessage += RTSHA256_BLOCK_SIZE;
528 pbBuf += RTSHA256_BLOCK_SIZE;
529 cbBuf -= RTSHA256_BLOCK_SIZE;
530 }
531 }
532
533 /*
534 * Stash any remaining bytes into the context buffer.
535 */
536 if (cbBuf > 0)
537 {
538 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
539 pCtx->AltPrivate.cbMessage += cbBuf;
540 }
541}
542RT_EXPORT_SYMBOL(RTSha256Update);
543
544
545/**
546 * Internal worker for RTSha256Final and RTSha224Final that finalizes the
547 * computation but does not copy out the hash value.
548 *
549 * @param pCtx The SHA-256 context.
550 */
551static void rtSha256FinalInternal(PRTSHA256CONTEXT pCtx)
552{
553 Assert(pCtx->AltPrivate.cbMessage < UINT64_MAX / 8);
554
555 /*
556 * Complete the message by adding a single bit (0x80), padding till
557 * the next 448-bit boundrary, the add the message length.
558 */
559 uint64_t const cMessageBits = pCtx->AltPrivate.cbMessage * 8;
560
561 unsigned cbMissing = RTSHA256_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U));
562 static uint8_t const s_abSingleBitAndSomePadding[12] = { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, };
563 if (cbMissing < 1U + 8U)
564 /* Less than 64+8 bits left in the current block, force a new block. */
565 RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
566 else
567 RTSha256Update(pCtx, &s_abSingleBitAndSomePadding, 1);
568
569 unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage & (RTSHA256_BLOCK_SIZE - 1U);
570 cbMissing = RTSHA256_BLOCK_SIZE - cbBuffered;
571 Assert(cbMissing >= 8);
572 memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 8);
573
574 *(uint64_t *)&pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits);
575
576 /*
577 * Process the last buffered block constructed/completed above.
578 */
579 rtSha256BlockInitBuffered(pCtx);
580 rtSha256BlockProcess(pCtx);
581
582 /*
583 * Convert the byte order of the hash words and we're done.
584 */
585 pCtx->AltPrivate.auH[0] = RT_H2BE_U32(pCtx->AltPrivate.auH[0]);
586 pCtx->AltPrivate.auH[1] = RT_H2BE_U32(pCtx->AltPrivate.auH[1]);
587 pCtx->AltPrivate.auH[2] = RT_H2BE_U32(pCtx->AltPrivate.auH[2]);
588 pCtx->AltPrivate.auH[3] = RT_H2BE_U32(pCtx->AltPrivate.auH[3]);
589 pCtx->AltPrivate.auH[4] = RT_H2BE_U32(pCtx->AltPrivate.auH[4]);
590 pCtx->AltPrivate.auH[5] = RT_H2BE_U32(pCtx->AltPrivate.auH[5]);
591 pCtx->AltPrivate.auH[6] = RT_H2BE_U32(pCtx->AltPrivate.auH[6]);
592 pCtx->AltPrivate.auH[7] = RT_H2BE_U32(pCtx->AltPrivate.auH[7]);
593
594 RT_ZERO(pCtx->AltPrivate.auW);
595 pCtx->AltPrivate.cbMessage = UINT64_MAX;
596}
597RT_EXPORT_SYMBOL(RTSha256Final);
598
599
600RTDECL(void) RTSha256Final(PRTSHA256CONTEXT pCtx, uint8_t pabDigest[RTSHA256_HASH_SIZE])
601{
602 rtSha256FinalInternal(pCtx);
603 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA256_HASH_SIZE);
604 RT_ZERO(pCtx->AltPrivate.auH);
605}
606RT_EXPORT_SYMBOL(RTSha256Final);
607
608
609RTDECL(void) RTSha256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA256_HASH_SIZE])
610{
611 RTSHA256CONTEXT Ctx;
612 RTSha256Init(&Ctx);
613 RTSha256Update(&Ctx, pvBuf, cbBuf);
614 RTSha256Final(&Ctx, pabDigest);
615}
616RT_EXPORT_SYMBOL(RTSha256);
617
618
619RTDECL(bool) RTSha256Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA256_HASH_SIZE])
620{
621 RTSHA256CONTEXT Ctx;
622 RTSha256Init(&Ctx);
623 RTSha256Update(&Ctx, pvBuf, cbBuf);
624 rtSha256FinalInternal(&Ctx);
625
626 bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA256_HASH_SIZE) == 0;
627
628 RT_ZERO(Ctx.AltPrivate.auH);
629 return fRet;
630}
631RT_EXPORT_SYMBOL(RTSha256Check);
632
633
634
635/*
636 * SHA-224 is just SHA-256 with different initial values an a truncated result.
637 */
638
639RTDECL(void) RTSha224Init(PRTSHA224CONTEXT pCtx)
640{
641 pCtx->AltPrivate.cbMessage = 0;
642 pCtx->AltPrivate.auH[0] = UINT32_C(0xc1059ed8);
643 pCtx->AltPrivate.auH[1] = UINT32_C(0x367cd507);
644 pCtx->AltPrivate.auH[2] = UINT32_C(0x3070dd17);
645 pCtx->AltPrivate.auH[3] = UINT32_C(0xf70e5939);
646 pCtx->AltPrivate.auH[4] = UINT32_C(0xffc00b31);
647 pCtx->AltPrivate.auH[5] = UINT32_C(0x68581511);
648 pCtx->AltPrivate.auH[6] = UINT32_C(0x64f98fa7);
649 pCtx->AltPrivate.auH[7] = UINT32_C(0xbefa4fa4);
650}
651RT_EXPORT_SYMBOL(RTSha224Init);
652
653
654RTDECL(void) RTSha224Update(PRTSHA224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
655{
656 RTSha256Update(pCtx, pvBuf, cbBuf);
657}
658RT_EXPORT_SYMBOL(RTSha224Update);
659
660
661RTDECL(void) RTSha224Final(PRTSHA224CONTEXT pCtx, uint8_t pabDigest[RTSHA224_HASH_SIZE])
662{
663 rtSha256FinalInternal(pCtx);
664 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA224_HASH_SIZE);
665 RT_ZERO(pCtx->AltPrivate.auH);
666}
667RT_EXPORT_SYMBOL(RTSha224Final);
668
669
670RTDECL(void) RTSha224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA224_HASH_SIZE])
671{
672 RTSHA224CONTEXT Ctx;
673 RTSha224Init(&Ctx);
674 RTSha224Update(&Ctx, pvBuf, cbBuf);
675 RTSha224Final(&Ctx, pabDigest);
676}
677RT_EXPORT_SYMBOL(RTSha224);
678
679
680RTDECL(bool) RTSha224Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA224_HASH_SIZE])
681{
682 RTSHA224CONTEXT Ctx;
683 RTSha224Init(&Ctx);
684 RTSha224Update(&Ctx, pvBuf, cbBuf);
685 rtSha256FinalInternal(&Ctx);
686
687 bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA224_HASH_SIZE) == 0;
688
689 RT_ZERO(Ctx.AltPrivate.auH);
690 return fRet;
691}
692RT_EXPORT_SYMBOL(RTSha224Check);
693
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