VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/checksum/alt-sha512.cpp@ 81106

Last change on this file since 81106 was 76553, checked in by vboxsync, 6 years ago

scm --update-copyright-year

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