VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/alt-sha512.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: 29.5 KB
Line 
1/* $Id: alt-sha512.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * IPRT - SHA-512 and SHA-384 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-512 block size (in bytes). */
42#define RTSHA512_BLOCK_SIZE 128U
43
44/** Enables the unrolled code. */
45#define RTSHA512_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 RTSHA512ALTPRIVATECTX
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 uint64_t auW[80];
66 /** The message length (in bytes). */
67 RTUINT128U cbMessage;
68 /** The 8 hash values. */
69 uint64_t auH[8];
70} RTSHA512ALTPRIVATECTX;
71
72#define RT_SHA512_PRIVATE_ALT_CONTEXT
73#include <iprt/sha.h>
74
75
76AssertCompile(RT_SIZEOFMEMB(RTSHA512CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA512CONTEXT, AltPrivate));
77AssertCompileMemberSize(RTSHA512ALTPRIVATECTX, auH, RTSHA512_HASH_SIZE);
78
79
80/*********************************************************************************************************************************
81* Global Variables *
82*********************************************************************************************************************************/
83#ifndef RTSHA512_UNROLLED
84/** The K constants. */
85static uint64_t const g_auKs[] =
86{
87 UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
88 UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118),
89 UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2),
90 UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694),
91 UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
92 UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5),
93 UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4),
94 UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70),
95 UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df),
96 UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
97 UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30),
98 UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8),
99 UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8),
100 UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3),
101 UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
102 UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b),
103 UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178),
104 UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b),
105 UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c),
106 UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817),
107};
108#endif /* !RTSHA512_UNROLLED */
109
110
111
112RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx)
113{
114 pCtx->AltPrivate.cbMessage.s.Lo = 0;
115 pCtx->AltPrivate.cbMessage.s.Hi = 0;
116 pCtx->AltPrivate.auH[0] = UINT64_C(0x6a09e667f3bcc908);
117 pCtx->AltPrivate.auH[1] = UINT64_C(0xbb67ae8584caa73b);
118 pCtx->AltPrivate.auH[2] = UINT64_C(0x3c6ef372fe94f82b);
119 pCtx->AltPrivate.auH[3] = UINT64_C(0xa54ff53a5f1d36f1);
120 pCtx->AltPrivate.auH[4] = UINT64_C(0x510e527fade682d1);
121 pCtx->AltPrivate.auH[5] = UINT64_C(0x9b05688c2b3e6c1f);
122 pCtx->AltPrivate.auH[6] = UINT64_C(0x1f83d9abfb41bd6b);
123 pCtx->AltPrivate.auH[7] = UINT64_C(0x5be0cd19137e2179);
124}
125RT_EXPORT_SYMBOL(RTSha512Init);
126
127
128/** Function 4.8. */
129DECL_FORCE_INLINE(uint64_t) rtSha512Ch(uint64_t uX, uint64_t uY, uint64_t uZ)
130{
131#if 1
132 /* Optimization that saves one operation and probably a temporary variable. */
133 uint64_t uResult = uY;
134 uResult ^= uZ;
135 uResult &= uX;
136 uResult ^= uZ;
137 return uResult;
138#else
139 /* The original. */
140 uint64_t uResult = uX & uY;
141 uResult ^= ~uX & uZ;
142 return uResult;
143#endif
144}
145
146
147/** Function 4.9. */
148DECL_FORCE_INLINE(uint64_t) rtSha512Maj(uint64_t uX, uint64_t uY, uint64_t uZ)
149{
150#if 1
151 /* Optimization that save one operation and probably a temporary variable. */
152 uint64_t uResult = uY;
153 uResult ^= uZ;
154 uResult &= uX;
155 uResult ^= uY & uZ;
156 return uResult;
157#else
158 /* The original. */
159 uint64_t uResult = uX & uY;
160 uResult ^= uX & uZ;
161 uResult ^= uY & uZ;
162 return uResult;
163#endif
164}
165
166
167/** Function 4.10. */
168DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma0(uint64_t uX)
169{
170 uint64_t uResult = uX = ASMRotateRightU64(uX, 28);
171 uX = ASMRotateRightU64(uX, 34 - 28);
172 uResult ^= uX;
173 uX = ASMRotateRightU64(uX, 39 - 34);
174 uResult ^= uX;
175 return uResult;
176}
177
178
179/** Function 4.11. */
180DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma1(uint64_t uX)
181{
182 uint64_t uResult = uX = ASMRotateRightU64(uX, 14);
183 uX = ASMRotateRightU64(uX, 18 - 14);
184 uResult ^= uX;
185 uX = ASMRotateRightU64(uX, 41 - 18);
186 uResult ^= uX;
187 return uResult;
188}
189
190
191/** Function 4.12. */
192DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma0(uint64_t uX)
193{
194 uint64_t uResult = uX >> 7;
195 uX = ASMRotateRightU64(uX, 1);
196 uResult ^= uX;
197 uX = ASMRotateRightU64(uX, 8 - 1);
198 uResult ^= uX;
199 return uResult;
200}
201
202
203/** Function 4.13. */
204DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma1(uint64_t uX)
205{
206 uint64_t uResult = uX >> 6;
207 uX = ASMRotateRightU64(uX, 19);
208 uResult ^= uX;
209 uX = ASMRotateRightU64(uX, 61 - 19);
210 uResult ^= uX;
211 return uResult;
212}
213
214
215/**
216 * Initializes the auW array from the specfied input block.
217 *
218 * @param pCtx The SHA-512 context.
219 * @param pbBlock The block. Must be 64-bit aligned.
220 */
221DECLINLINE(void) rtSha512BlockInit(PRTSHA512CONTEXT pCtx, uint8_t const *pbBlock)
222{
223#ifdef RTSHA512_UNROLLED
224 uint64_t const *puSrc = (uint64_t const *)pbBlock;
225 uint64_t *puW = &pCtx->AltPrivate.auW[0];
226 Assert(!((uintptr_t)puSrc & 7));
227 Assert(!((uintptr_t)puW & 7));
228
229 /* Copy and byte-swap the block. Initializing the rest of the Ws are done
230 in the processing loop. */
231# ifdef RT_LITTLE_ENDIAN
232 *puW++ = ASMByteSwapU64(*puSrc++);
233 *puW++ = ASMByteSwapU64(*puSrc++);
234 *puW++ = ASMByteSwapU64(*puSrc++);
235 *puW++ = ASMByteSwapU64(*puSrc++);
236
237 *puW++ = ASMByteSwapU64(*puSrc++);
238 *puW++ = ASMByteSwapU64(*puSrc++);
239 *puW++ = ASMByteSwapU64(*puSrc++);
240 *puW++ = ASMByteSwapU64(*puSrc++);
241
242 *puW++ = ASMByteSwapU64(*puSrc++);
243 *puW++ = ASMByteSwapU64(*puSrc++);
244 *puW++ = ASMByteSwapU64(*puSrc++);
245 *puW++ = ASMByteSwapU64(*puSrc++);
246
247 *puW++ = ASMByteSwapU64(*puSrc++);
248 *puW++ = ASMByteSwapU64(*puSrc++);
249 *puW++ = ASMByteSwapU64(*puSrc++);
250 *puW++ = ASMByteSwapU64(*puSrc++);
251# else
252 memcpy(puW, puSrc, RTSHA512_BLOCK_SIZE);
253# endif
254
255#else /* !RTSHA512_UNROLLED */
256
257 uint64_t const *pu32Block = (uint64_t const *)pbBlock;
258 Assert(!((uintptr_t)pu32Block & 3));
259
260 unsigned iWord;
261 for (iWord = 0; iWord < 16; iWord++)
262 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pu32Block[iWord]);
263
264 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
265 {
266 uint64_t u64 = rtSha512SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
267 u64 += rtSha512SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
268 u64 += pCtx->AltPrivate.auW[iWord - 7];
269 u64 += pCtx->AltPrivate.auW[iWord - 16];
270 pCtx->AltPrivate.auW[iWord] = u64;
271 }
272#endif /* !RTSHA512_UNROLLED */
273}
274
275
276/**
277 * Initializes the auW array from data buffered in the first part of the array.
278 *
279 * @param pCtx The SHA-512 context.
280 */
281DECLINLINE(void) rtSha512BlockInitBuffered(PRTSHA512CONTEXT pCtx)
282{
283#ifdef RTSHA512_UNROLLED
284 uint64_t *puW = &pCtx->AltPrivate.auW[0];
285 Assert(!((uintptr_t)puW & 7));
286
287 /* Do the byte swap if necessary. Initializing the rest of the Ws are done
288 in the processing loop. */
289# ifdef RT_LITTLE_ENDIAN
290 *puW = ASMByteSwapU64(*puW); puW++;
291 *puW = ASMByteSwapU64(*puW); puW++;
292 *puW = ASMByteSwapU64(*puW); puW++;
293 *puW = ASMByteSwapU64(*puW); puW++;
294
295 *puW = ASMByteSwapU64(*puW); puW++;
296 *puW = ASMByteSwapU64(*puW); puW++;
297 *puW = ASMByteSwapU64(*puW); puW++;
298 *puW = ASMByteSwapU64(*puW); puW++;
299
300 *puW = ASMByteSwapU64(*puW); puW++;
301 *puW = ASMByteSwapU64(*puW); puW++;
302 *puW = ASMByteSwapU64(*puW); puW++;
303 *puW = ASMByteSwapU64(*puW); puW++;
304
305 *puW = ASMByteSwapU64(*puW); puW++;
306 *puW = ASMByteSwapU64(*puW); puW++;
307 *puW = ASMByteSwapU64(*puW); puW++;
308 *puW = ASMByteSwapU64(*puW); puW++;
309# endif
310
311#else /* !RTSHA512_UNROLLED */
312
313 unsigned iWord;
314 for (iWord = 0; iWord < 16; iWord++)
315 pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pCtx->AltPrivate.auW[iWord]);
316
317 for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
318 {
319 uint64_t u64 = rtSha512SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
320 u64 += rtSha512SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
321 u64 += pCtx->AltPrivate.auW[iWord - 7];
322 u64 += pCtx->AltPrivate.auW[iWord - 16];
323 pCtx->AltPrivate.auW[iWord] = u64;
324 }
325#endif /* !RTSHA512_UNROLLED */
326}
327
328
329/**
330 * Process the current block.
331 *
332 * Requires one of the rtSha512BlockInit functions to be called first.
333 *
334 * @param pCtx The SHA-512 context.
335 */
336static void rtSha512BlockProcess(PRTSHA512CONTEXT pCtx)
337{
338 uint64_t uA = pCtx->AltPrivate.auH[0];
339 uint64_t uB = pCtx->AltPrivate.auH[1];
340 uint64_t uC = pCtx->AltPrivate.auH[2];
341 uint64_t uD = pCtx->AltPrivate.auH[3];
342 uint64_t uE = pCtx->AltPrivate.auH[4];
343 uint64_t uF = pCtx->AltPrivate.auH[5];
344 uint64_t uG = pCtx->AltPrivate.auH[6];
345 uint64_t uH = pCtx->AltPrivate.auH[7];
346
347#ifdef RTSHA512_UNROLLED
348 uint64_t *puW = &pCtx->AltPrivate.auW[0];
349# define RTSHA512_BODY(a_iWord, a_uK, a_uA, a_uB, a_uC, a_uD, a_uE, a_uF, a_uG, a_uH) \
350 do { \
351 if ((a_iWord) < 16) \
352 a_uH += *puW++; \
353 else \
354 { \
355 uint64_t u64 = puW[-16]; \
356 u64 += rtSha512SmallSigma0(puW[-15]); \
357 u64 += puW[-7]; \
358 u64 += rtSha512SmallSigma1(puW[-2]); \
359 if (a_iWord < 80-2) *puW++ = u64; else puW++; \
360 a_uH += u64; \
361 } \
362 \
363 a_uH += rtSha512CapitalSigma1(a_uE); \
364 a_uH += a_uK; \
365 a_uH += rtSha512Ch(a_uE, a_uF, a_uG); \
366 a_uD += a_uH; \
367 \
368 a_uH += rtSha512CapitalSigma0(a_uA); \
369 a_uH += rtSha512Maj(a_uA, a_uB, a_uC); \
370 } while (0)
371# define RTSHA512_EIGHT(a_uK0, a_uK1, a_uK2, a_uK3, a_uK4, a_uK5, a_uK6, a_uK7, a_iFirst) \
372 do { \
373 RTSHA512_BODY(a_iFirst + 0, a_uK0, uA, uB, uC, uD, uE, uF, uG, uH); \
374 RTSHA512_BODY(a_iFirst + 1, a_uK1, uH, uA, uB, uC, uD, uE, uF, uG); \
375 RTSHA512_BODY(a_iFirst + 2, a_uK2, uG, uH, uA, uB, uC, uD, uE, uF); \
376 RTSHA512_BODY(a_iFirst + 3, a_uK3, uF, uG, uH, uA, uB, uC, uD, uE); \
377 RTSHA512_BODY(a_iFirst + 4, a_uK4, uE, uF, uG, uH, uA, uB, uC, uD); \
378 RTSHA512_BODY(a_iFirst + 5, a_uK5, uD, uE, uF, uG, uH, uA, uB, uC); \
379 RTSHA512_BODY(a_iFirst + 6, a_uK6, uC, uD, uE, uF, uG, uH, uA, uB); \
380 RTSHA512_BODY(a_iFirst + 7, a_uK7, uB, uC, uD, uE, uF, uG, uH, uA); \
381 } while (0)
382 RTSHA512_EIGHT(UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
383 UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118),
384 0);
385 RTSHA512_EIGHT(UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2),
386 UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694),
387 8);
388 RTSHA512_EIGHT(UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
389 UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5),
390 16);
391 RTSHA512_EIGHT(UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4),
392 UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70),
393 24);
394 RTSHA512_EIGHT(UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df),
395 UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
396 32);
397 RTSHA512_EIGHT(UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30),
398 UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8),
399 40);
400 RTSHA512_EIGHT(UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8),
401 UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3),
402 48);
403 RTSHA512_EIGHT(UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
404 UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b),
405 56);
406 RTSHA512_EIGHT(UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178),
407 UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b),
408 64);
409 RTSHA512_EIGHT(UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c),
410 UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817),
411 72);
412#else
413 for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
414 {
415 uint64_t uT1 = uH;
416 uT1 += rtSha512CapitalSigma1(uE);
417 uT1 += rtSha512Ch(uE, uF, uG);
418 uT1 += g_auKs[iWord];
419 uT1 += pCtx->AltPrivate.auW[iWord];
420
421 uint64_t uT2 = rtSha512CapitalSigma0(uA);
422 uT2 += rtSha512Maj(uA, uB, uC);
423
424 uH = uG;
425 uG = uF;
426 uF = uE;
427 uE = uD + uT1;
428 uD = uC;
429 uC = uB;
430 uB = uA;
431 uA = uT1 + uT2;
432 }
433#endif
434
435 pCtx->AltPrivate.auH[0] += uA;
436 pCtx->AltPrivate.auH[1] += uB;
437 pCtx->AltPrivate.auH[2] += uC;
438 pCtx->AltPrivate.auH[3] += uD;
439 pCtx->AltPrivate.auH[4] += uE;
440 pCtx->AltPrivate.auH[5] += uF;
441 pCtx->AltPrivate.auH[6] += uG;
442 pCtx->AltPrivate.auH[7] += uH;
443}
444
445
446RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
447{
448 Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
449 uint8_t const *pbBuf = (uint8_t const *)pvBuf;
450
451 /*
452 * Deal with buffered bytes first.
453 */
454 size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
455 if (cbBuffered)
456 {
457 size_t cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
458 if (cbBuf >= cbMissing)
459 {
460 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
461 pCtx->AltPrivate.cbMessage.s.Lo += cbMissing;
462 if (!pCtx->AltPrivate.cbMessage.s.Lo)
463 pCtx->AltPrivate.cbMessage.s.Hi++;
464 pbBuf += cbMissing;
465 cbBuf -= cbMissing;
466
467 rtSha512BlockInitBuffered(pCtx);
468 rtSha512BlockProcess(pCtx);
469 }
470 else
471 {
472 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
473 pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
474 return;
475 }
476 }
477
478 if (!((uintptr_t)pbBuf & 7))
479 {
480 /*
481 * Process full blocks directly from the input buffer.
482 */
483 while (cbBuf >= RTSHA512_BLOCK_SIZE)
484 {
485 rtSha512BlockInit(pCtx, pbBuf);
486 rtSha512BlockProcess(pCtx);
487
488 pCtx->AltPrivate.cbMessage.s.Lo += RTSHA512_BLOCK_SIZE;
489 if (!pCtx->AltPrivate.cbMessage.s.Lo)
490 pCtx->AltPrivate.cbMessage.s.Hi++;
491 pbBuf += RTSHA512_BLOCK_SIZE;
492 cbBuf -= RTSHA512_BLOCK_SIZE;
493 }
494 }
495 else
496 {
497 /*
498 * Unaligned input, so buffer it.
499 */
500 while (cbBuf >= RTSHA512_BLOCK_SIZE)
501 {
502 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA512_BLOCK_SIZE);
503 rtSha512BlockInitBuffered(pCtx);
504 rtSha512BlockProcess(pCtx);
505
506 pCtx->AltPrivate.cbMessage.s.Lo += RTSHA512_BLOCK_SIZE;
507 if (!pCtx->AltPrivate.cbMessage.s.Lo)
508 pCtx->AltPrivate.cbMessage.s.Hi++;
509 pbBuf += RTSHA512_BLOCK_SIZE;
510 cbBuf -= RTSHA512_BLOCK_SIZE;
511 }
512 }
513
514 /*
515 * Stash any remaining bytes into the context buffer.
516 */
517 if (cbBuf > 0)
518 {
519 memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
520 pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
521 if (!pCtx->AltPrivate.cbMessage.s.Lo)
522 pCtx->AltPrivate.cbMessage.s.Hi++;
523 }
524}
525RT_EXPORT_SYMBOL(RTSha512Update);
526
527
528/**
529 * Internal worker for RTSha512Final and RTSha384Final that finalizes the
530 * computation but does not copy out the hash value.
531 *
532 * @param pCtx The SHA-512 context.
533 */
534static void rtSha512FinalInternal(PRTSHA512CONTEXT pCtx)
535{
536 Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
537
538 /*
539 * Complete the message by adding a single bit (0x80), padding till
540 * the next 448-bit boundrary, the add the message length.
541 */
542 RTUINT128U cMessageBits = pCtx->AltPrivate.cbMessage;
543 cMessageBits.s.Hi <<= 3;
544 cMessageBits.s.Hi |= cMessageBits.s.Lo >> 61;
545 cMessageBits.s.Lo <<= 3;
546
547 unsigned cbMissing = RTSHA512_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U));
548 static uint8_t const s_abSingleBitAndSomePadding[20] =
549 { 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
550 if (cbMissing < 1U + 16U)
551 /* Less than 64+16 bits left in the current block, force a new block. */
552 RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
553 else
554 RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, 1);
555
556 unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
557 cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
558 Assert(cbMissing >= 16);
559 memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 16);
560
561 pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits.s.Hi);
562 pCtx->AltPrivate.auW[15] = RT_H2BE_U64(cMessageBits.s.Lo);
563
564 /*
565 * Process the last buffered block constructed/completed above.
566 */
567 rtSha512BlockInitBuffered(pCtx);
568 rtSha512BlockProcess(pCtx);
569
570 /*
571 * Convert the byte order of the hash words and we're done.
572 */
573 pCtx->AltPrivate.auH[0] = RT_H2BE_U64(pCtx->AltPrivate.auH[0]);
574 pCtx->AltPrivate.auH[1] = RT_H2BE_U64(pCtx->AltPrivate.auH[1]);
575 pCtx->AltPrivate.auH[2] = RT_H2BE_U64(pCtx->AltPrivate.auH[2]);
576 pCtx->AltPrivate.auH[3] = RT_H2BE_U64(pCtx->AltPrivate.auH[3]);
577 pCtx->AltPrivate.auH[4] = RT_H2BE_U64(pCtx->AltPrivate.auH[4]);
578 pCtx->AltPrivate.auH[5] = RT_H2BE_U64(pCtx->AltPrivate.auH[5]);
579 pCtx->AltPrivate.auH[6] = RT_H2BE_U64(pCtx->AltPrivate.auH[6]);
580 pCtx->AltPrivate.auH[7] = RT_H2BE_U64(pCtx->AltPrivate.auH[7]);
581
582 RT_ZERO(pCtx->AltPrivate.auW);
583 pCtx->AltPrivate.cbMessage.s.Lo = UINT64_MAX;
584 pCtx->AltPrivate.cbMessage.s.Hi = UINT64_MAX;
585}
586RT_EXPORT_SYMBOL(RTSha512Final);
587
588
589RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE])
590{
591 rtSha512FinalInternal(pCtx);
592 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512_HASH_SIZE);
593 RT_ZERO(pCtx->AltPrivate.auH);
594}
595RT_EXPORT_SYMBOL(RTSha512Final);
596
597
598RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE])
599{
600 RTSHA512CONTEXT Ctx;
601 RTSha512Init(&Ctx);
602 RTSha512Update(&Ctx, pvBuf, cbBuf);
603 RTSha512Final(&Ctx, pabDigest);
604}
605RT_EXPORT_SYMBOL(RTSha512);
606
607
608RTDECL(bool) RTSha512Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512_HASH_SIZE])
609{
610 RTSHA512CONTEXT Ctx;
611 RTSha512Init(&Ctx);
612 RTSha512Update(&Ctx, pvBuf, cbBuf);
613 rtSha512FinalInternal(&Ctx);
614
615 bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512_HASH_SIZE) == 0;
616
617 RT_ZERO(Ctx.AltPrivate.auH);
618 return fRet;
619}
620RT_EXPORT_SYMBOL(RTSha512Check);
621
622
623
624/*
625 * SHA-384 is just SHA-512 with different initial values an a truncated result.
626 */
627
628RTDECL(void) RTSha384Init(PRTSHA384CONTEXT pCtx)
629{
630 pCtx->AltPrivate.cbMessage.s.Lo = 0;
631 pCtx->AltPrivate.cbMessage.s.Hi = 0;
632 pCtx->AltPrivate.auH[0] = UINT64_C(0xcbbb9d5dc1059ed8);
633 pCtx->AltPrivate.auH[1] = UINT64_C(0x629a292a367cd507);
634 pCtx->AltPrivate.auH[2] = UINT64_C(0x9159015a3070dd17);
635 pCtx->AltPrivate.auH[3] = UINT64_C(0x152fecd8f70e5939);
636 pCtx->AltPrivate.auH[4] = UINT64_C(0x67332667ffc00b31);
637 pCtx->AltPrivate.auH[5] = UINT64_C(0x8eb44a8768581511);
638 pCtx->AltPrivate.auH[6] = UINT64_C(0xdb0c2e0d64f98fa7);
639 pCtx->AltPrivate.auH[7] = UINT64_C(0x47b5481dbefa4fa4);
640}
641RT_EXPORT_SYMBOL(RTSha384Init);
642
643
644RTDECL(void) RTSha384Update(PRTSHA384CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
645{
646 RTSha512Update(pCtx, pvBuf, cbBuf);
647}
648RT_EXPORT_SYMBOL(RTSha384Update);
649
650
651RTDECL(void) RTSha384Final(PRTSHA384CONTEXT pCtx, uint8_t pabDigest[RTSHA384_HASH_SIZE])
652{
653 rtSha512FinalInternal(pCtx);
654 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA384_HASH_SIZE);
655 RT_ZERO(pCtx->AltPrivate.auH);
656}
657RT_EXPORT_SYMBOL(RTSha384Final);
658
659
660RTDECL(void) RTSha384(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA384_HASH_SIZE])
661{
662 RTSHA384CONTEXT Ctx;
663 RTSha384Init(&Ctx);
664 RTSha384Update(&Ctx, pvBuf, cbBuf);
665 RTSha384Final(&Ctx, pabDigest);
666}
667RT_EXPORT_SYMBOL(RTSha384);
668
669
670RTDECL(bool) RTSha384Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA384_HASH_SIZE])
671{
672 RTSHA384CONTEXT Ctx;
673 RTSha384Init(&Ctx);
674 RTSha384Update(&Ctx, pvBuf, cbBuf);
675 rtSha512FinalInternal(&Ctx);
676
677 bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA384_HASH_SIZE) == 0;
678
679 RT_ZERO(Ctx.AltPrivate.auH);
680 return fRet;
681}
682RT_EXPORT_SYMBOL(RTSha384Check);
683
684
685/*
686 * SHA-512/224 is just SHA-512 with different initial values an a truncated result.
687 */
688
689RTDECL(void) RTSha512t224Init(PRTSHA512T224CONTEXT pCtx)
690{
691 pCtx->AltPrivate.cbMessage.s.Lo = 0;
692 pCtx->AltPrivate.cbMessage.s.Hi = 0;
693 pCtx->AltPrivate.auH[0] = UINT64_C(0x8c3d37c819544da2);
694 pCtx->AltPrivate.auH[1] = UINT64_C(0x73e1996689dcd4d6);
695 pCtx->AltPrivate.auH[2] = UINT64_C(0x1dfab7ae32ff9c82);
696 pCtx->AltPrivate.auH[3] = UINT64_C(0x679dd514582f9fcf);
697 pCtx->AltPrivate.auH[4] = UINT64_C(0x0f6d2b697bd44da8);
698 pCtx->AltPrivate.auH[5] = UINT64_C(0x77e36f7304c48942);
699 pCtx->AltPrivate.auH[6] = UINT64_C(0x3f9d85a86a1d36c8);
700 pCtx->AltPrivate.auH[7] = UINT64_C(0x1112e6ad91d692a1);
701}
702RT_EXPORT_SYMBOL(RTSha512t224Init);
703
704
705RTDECL(void) RTSha512t224Update(PRTSHA512T224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
706{
707 RTSha512Update(pCtx, pvBuf, cbBuf);
708}
709RT_EXPORT_SYMBOL(RTSha512t224Update);
710
711
712RTDECL(void) RTSha512t224Final(PRTSHA512T224CONTEXT pCtx, uint8_t pabDigest[RTSHA512T224_HASH_SIZE])
713{
714 rtSha512FinalInternal(pCtx);
715 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512T224_HASH_SIZE);
716 RT_ZERO(pCtx->AltPrivate.auH);
717}
718RT_EXPORT_SYMBOL(RTSha512t224Final);
719
720
721RTDECL(void) RTSha512t224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512T224_HASH_SIZE])
722{
723 RTSHA512T224CONTEXT Ctx;
724 RTSha512t224Init(&Ctx);
725 RTSha512t224Update(&Ctx, pvBuf, cbBuf);
726 RTSha512t224Final(&Ctx, pabDigest);
727}
728RT_EXPORT_SYMBOL(RTSha512t224);
729
730
731RTDECL(bool) RTSha512t224Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512T224_HASH_SIZE])
732{
733 RTSHA512T224CONTEXT Ctx;
734 RTSha512t224Init(&Ctx);
735 RTSha512t224Update(&Ctx, pvBuf, cbBuf);
736 rtSha512FinalInternal(&Ctx);
737
738 bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512T224_HASH_SIZE) == 0;
739
740 RT_ZERO(Ctx.AltPrivate.auH);
741 return fRet;
742}
743RT_EXPORT_SYMBOL(RTSha512t224Check);
744
745
746/*
747 * SHA-512/256 is just SHA-512 with different initial values an a truncated result.
748 */
749
750RTDECL(void) RTSha512t256Init(PRTSHA512T256CONTEXT pCtx)
751{
752 pCtx->AltPrivate.cbMessage.s.Lo = 0;
753 pCtx->AltPrivate.cbMessage.s.Hi = 0;
754 pCtx->AltPrivate.auH[0] = UINT64_C(0x22312194fc2bf72c);
755 pCtx->AltPrivate.auH[1] = UINT64_C(0x9f555fa3c84c64c2);
756 pCtx->AltPrivate.auH[2] = UINT64_C(0x2393b86b6f53b151);
757 pCtx->AltPrivate.auH[3] = UINT64_C(0x963877195940eabd);
758 pCtx->AltPrivate.auH[4] = UINT64_C(0x96283ee2a88effe3);
759 pCtx->AltPrivate.auH[5] = UINT64_C(0xbe5e1e2553863992);
760 pCtx->AltPrivate.auH[6] = UINT64_C(0x2b0199fc2c85b8aa);
761 pCtx->AltPrivate.auH[7] = UINT64_C(0x0eb72ddc81c52ca2);
762}
763RT_EXPORT_SYMBOL(RTSha512t256Init);
764
765
766RTDECL(void) RTSha512t256Update(PRTSHA512T256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
767{
768 RTSha512Update(pCtx, pvBuf, cbBuf);
769}
770RT_EXPORT_SYMBOL(RTSha512t256Update);
771
772
773RTDECL(void) RTSha512t256Final(PRTSHA512T256CONTEXT pCtx, uint8_t pabDigest[RTSHA512T256_HASH_SIZE])
774{
775 rtSha512FinalInternal(pCtx);
776 memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512T256_HASH_SIZE);
777 RT_ZERO(pCtx->AltPrivate.auH);
778}
779RT_EXPORT_SYMBOL(RTSha512t256Final);
780
781
782RTDECL(void) RTSha512t256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512T256_HASH_SIZE])
783{
784 RTSHA512T256CONTEXT Ctx;
785 RTSha512t256Init(&Ctx);
786 RTSha512t256Update(&Ctx, pvBuf, cbBuf);
787 RTSha512t256Final(&Ctx, pabDigest);
788}
789RT_EXPORT_SYMBOL(RTSha512t256);
790
791
792RTDECL(bool) RTSha512t256Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512T256_HASH_SIZE])
793{
794 RTSHA512T256CONTEXT Ctx;
795 RTSha512t256Init(&Ctx);
796 RTSha512t256Update(&Ctx, pvBuf, cbBuf);
797 rtSha512FinalInternal(&Ctx);
798
799 bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512T256_HASH_SIZE) == 0;
800
801 RT_ZERO(Ctx.AltPrivate.auH);
802 return fRet;
803}
804RT_EXPORT_SYMBOL(RTSha512t256Check);
805
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use