1
/* $Id: alt-sha512.cpp $ */
3
* IPRT - SHA-512 and SHA-384 hash functions, Alternative Implementation.
7
* Copyright (C) 2009-2014 Oracle Corporation
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.
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.
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.
28
/*******************************************************************************
29
* Defined Constants And Macros *
30
*******************************************************************************/
31
/** The SHA-512 block size (in bytes). */
32
#define RTSHA512_BLOCK_SIZE 128U
34
/** Enables the unrolled code. */
35
#define RTSHA512_UNROLLED 1
38
/*******************************************************************************
40
*******************************************************************************/
41
#include "internal/iprt.h"
42
#include <iprt/types.h>
43
#include <iprt/assert.h>
45
#include <iprt/string.h>
48
/** Our private context structure. */
49
typedef struct RTSHA512ALTPRIVATECTX
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. */
56
/** The message length (in bytes). */
58
/** The 8 hash values. */
60
} RTSHA512ALTPRIVATECTX;
62
#define RT_SHA512_PRIVATE_ALT_CONTEXT
66
AssertCompile(RT_SIZEOFMEMB(RTSHA512CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA512CONTEXT, AltPrivate));
67
AssertCompileMemberSize(RTSHA512ALTPRIVATECTX, auH, RTSHA512_HASH_SIZE);
70
/*******************************************************************************
72
*******************************************************************************/
73
#ifndef RTSHA512_UNROLLED
74
/** The K constants. */
75
static uint64_t const g_auKs[] =
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),
98
#endif /* !RTSHA512_UNROLLED */
102
RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx)
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);
115
RT_EXPORT_SYMBOL(RTSha512Init);
119
DECL_FORCE_INLINE(uint64_t) rtSha512Ch(uint64_t uX, uint64_t uY, uint64_t uZ)
122
/* Optimization that saves one operation and probably a temporary variable. */
123
uint64_t uResult = uY;
130
uint64_t uResult = uX & uY;
138
DECL_FORCE_INLINE(uint64_t) rtSha512Maj(uint64_t uX, uint64_t uY, uint64_t uZ)
141
/* Optimization that save one operation and probably a temporary variable. */
142
uint64_t uResult = uY;
149
uint64_t uResult = uX & uY;
157
/** Function 4.10. */
158
DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma0(uint64_t uX)
160
uint64_t uResult = uX = ASMRotateRightU64(uX, 28);
161
uX = ASMRotateRightU64(uX, 34 - 28);
163
uX = ASMRotateRightU64(uX, 39 - 34);
169
/** Function 4.11. */
170
DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma1(uint64_t uX)
172
uint64_t uResult = uX = ASMRotateRightU64(uX, 14);
173
uX = ASMRotateRightU64(uX, 18 - 14);
175
uX = ASMRotateRightU64(uX, 41 - 18);
181
/** Function 4.12. */
182
DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma0(uint64_t uX)
184
uint64_t uResult = uX >> 7;
185
uX = ASMRotateRightU64(uX, 1);
187
uX = ASMRotateRightU64(uX, 8 - 1);
193
/** Function 4.13. */
194
DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma1(uint64_t uX)
196
uint64_t uResult = uX >> 6;
197
uX = ASMRotateRightU64(uX, 19);
199
uX = ASMRotateRightU64(uX, 61 - 19);
206
* Initializes the auW array from the specfied input block.
208
* @param pCtx The SHA-512 context.
209
* @param pbBlock The block. Must be 64-bit aligned.
211
DECLINLINE(void) rtSha512BlockInit(PRTSHA512CONTEXT pCtx, uint8_t const *pbBlock)
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));
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++);
227
*puW++ = ASMByteSwapU64(*puSrc++);
228
*puW++ = ASMByteSwapU64(*puSrc++);
229
*puW++ = ASMByteSwapU64(*puSrc++);
230
*puW++ = ASMByteSwapU64(*puSrc++);
232
*puW++ = ASMByteSwapU64(*puSrc++);
233
*puW++ = ASMByteSwapU64(*puSrc++);
234
*puW++ = ASMByteSwapU64(*puSrc++);
235
*puW++ = ASMByteSwapU64(*puSrc++);
237
*puW++ = ASMByteSwapU64(*puSrc++);
238
*puW++ = ASMByteSwapU64(*puSrc++);
239
*puW++ = ASMByteSwapU64(*puSrc++);
240
*puW++ = ASMByteSwapU64(*puSrc++);
242
memcpy(puW, puSrc, RTSHA512_BLOCK_SIZE);
245
#else /* !RTSHA512_UNROLLED */
247
uint64_t const *pu32Block = (uint64_t const *)pbBlock;
248
Assert(!((uintptr_t)pu32Block & 3));
251
for (iWord = 0; iWord < 16; iWord++)
252
pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pu32Block[iWord]);
254
for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
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;
262
#endif /* !RTSHA512_UNROLLED */
267
* Initializes the auW array from data buffered in the first part of the array.
269
* @param pCtx The SHA-512 context.
271
DECLINLINE(void) rtSha512BlockInitBuffered(PRTSHA512CONTEXT pCtx)
273
#ifdef RTSHA512_UNROLLED
274
uint64_t *puW = &pCtx->AltPrivate.auW[0];
275
Assert(!((uintptr_t)puW & 7));
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++;
285
*puW = ASMByteSwapU64(*puW); puW++;
286
*puW = ASMByteSwapU64(*puW); puW++;
287
*puW = ASMByteSwapU64(*puW); puW++;
288
*puW = ASMByteSwapU64(*puW); puW++;
290
*puW = ASMByteSwapU64(*puW); puW++;
291
*puW = ASMByteSwapU64(*puW); puW++;
292
*puW = ASMByteSwapU64(*puW); puW++;
293
*puW = ASMByteSwapU64(*puW); puW++;
295
*puW = ASMByteSwapU64(*puW); puW++;
296
*puW = ASMByteSwapU64(*puW); puW++;
297
*puW = ASMByteSwapU64(*puW); puW++;
298
*puW = ASMByteSwapU64(*puW); puW++;
301
#else /* !RTSHA512_UNROLLED */
304
for (iWord = 0; iWord < 16; iWord++)
305
pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pCtx->AltPrivate.auW[iWord]);
307
for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
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;
315
#endif /* !RTSHA512_UNROLLED */
320
* Process the current block.
322
* Requires one of the rtSha512BlockInit functions to be called first.
324
* @param pCtx The SHA-512 context.
326
static void rtSha512BlockProcess(PRTSHA512CONTEXT pCtx)
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];
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) \
341
if ((a_iWord) < 16) \
345
uint64_t u64 = puW[-16]; \
346
u64 += rtSha512SmallSigma0(puW[-15]); \
348
u64 += rtSha512SmallSigma1(puW[-2]); \
349
if (a_iWord < 80-2) *puW++ = u64; else puW++; \
353
a_uH += rtSha512CapitalSigma1(a_uE); \
355
a_uH += rtSha512Ch(a_uE, a_uF, a_uG); \
358
a_uH += rtSha512CapitalSigma0(a_uA); \
359
a_uH += rtSha512Maj(a_uA, a_uB, a_uC); \
361
# define RTSHA512_EIGHT(a_uK0, a_uK1, a_uK2, a_uK3, a_uK4, a_uK5, a_uK6, a_uK7, a_iFirst) \
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); \
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),
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),
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),
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),
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),
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),
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),
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),
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),
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),
403
for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
406
uT1 += rtSha512CapitalSigma1(uE);
407
uT1 += rtSha512Ch(uE, uF, uG);
408
uT1 += g_auKs[iWord];
409
uT1 += pCtx->AltPrivate.auW[iWord];
411
uint64_t uT2 = rtSha512CapitalSigma0(uA);
412
uT2 += rtSha512Maj(uA, uB, uC);
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;
436
RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
438
Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
439
uint8_t const *pbBuf = (uint8_t const *)pvBuf;
442
* Deal with buffered bytes first.
444
size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
447
size_t cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
448
if (cbBuf >= cbMissing)
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++;
457
rtSha512BlockInitBuffered(pCtx);
458
rtSha512BlockProcess(pCtx);
462
memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
463
pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
468
if (!((uintptr_t)pbBuf & 7))
471
* Process full blocks directly from the input buffer.
473
while (cbBuf >= RTSHA512_BLOCK_SIZE)
475
rtSha512BlockInit(pCtx, pbBuf);
476
rtSha512BlockProcess(pCtx);
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;
488
* Unaligned input, so buffer it.
490
while (cbBuf >= RTSHA512_BLOCK_SIZE)
492
memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA512_BLOCK_SIZE);
493
rtSha512BlockInitBuffered(pCtx);
494
rtSha512BlockProcess(pCtx);
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;
505
* Stash any remaining bytes into the context buffer.
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++;
515
RT_EXPORT_SYMBOL(RTSha512Update);
519
* Internal worker for RTSha512Final and RTSha384Final that finalizes the
520
* computation but does not copy out the hash value.
522
* @param pCtx The SHA-512 context.
524
static void rtSha512FinalInternal(PRTSHA512CONTEXT pCtx)
526
Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
529
* Complete the message by adding a single bit (0x80), padding till
530
* the next 448-bit boundrary, the add the message length.
532
RTUINT128U cMessageBits = pCtx->AltPrivate.cbMessage;
533
cMessageBits.s.Hi <<= 3;
534
cMessageBits.s.Hi |= cMessageBits.s.Lo >> 61;
535
cMessageBits.s.Lo <<= 3;
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));
544
RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, 1);
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);
551
pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits.s.Hi);
552
pCtx->AltPrivate.auW[15] = RT_H2BE_U64(cMessageBits.s.Lo);
555
* Process the last buffered block constructed/completed above.
557
rtSha512BlockInitBuffered(pCtx);
558
rtSha512BlockProcess(pCtx);
561
* Convert the byte order of the hash words and we're done.
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]);
572
RT_ZERO(pCtx->AltPrivate.auW);
573
pCtx->AltPrivate.cbMessage.s.Lo = UINT64_MAX;
574
pCtx->AltPrivate.cbMessage.s.Hi = UINT64_MAX;
576
RT_EXPORT_SYMBOL(RTSha512Final);
579
RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE])
581
rtSha512FinalInternal(pCtx);
582
memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512_HASH_SIZE);
583
RT_ZERO(pCtx->AltPrivate.auH);
585
RT_EXPORT_SYMBOL(RTSha512Final);
588
RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE])
592
RTSha512Update(&Ctx, pvBuf, cbBuf);
593
RTSha512Final(&Ctx, pabDigest);
595
RT_EXPORT_SYMBOL(RTSha512);
598
RTDECL(bool) RTSha512Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512_HASH_SIZE])
602
RTSha512Update(&Ctx, pvBuf, cbBuf);
603
rtSha512FinalInternal(&Ctx);
605
bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512_HASH_SIZE) == 0;
607
RT_ZERO(Ctx.AltPrivate.auH);
610
RT_EXPORT_SYMBOL(RTSha512Check);
615
* SHA-384 is just SHA-512 with different initial values an a truncated result.
618
RTDECL(void) RTSha384Init(PRTSHA384CONTEXT pCtx)
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);
631
RT_EXPORT_SYMBOL(RTSha384Init);
634
RTDECL(void) RTSha384Update(PRTSHA384CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
636
RTSha512Update(pCtx, pvBuf, cbBuf);
638
RT_EXPORT_SYMBOL(RTSha384Update);
641
RTDECL(void) RTSha384Final(PRTSHA384CONTEXT pCtx, uint8_t pabDigest[RTSHA384_HASH_SIZE])
643
rtSha512FinalInternal(pCtx);
644
memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA384_HASH_SIZE);
645
RT_ZERO(pCtx->AltPrivate.auH);
647
RT_EXPORT_SYMBOL(RTSha384Final);
650
RTDECL(void) RTSha384(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA384_HASH_SIZE])
654
RTSha384Update(&Ctx, pvBuf, cbBuf);
655
RTSha384Final(&Ctx, pabDigest);
657
RT_EXPORT_SYMBOL(RTSha384);
660
RTDECL(bool) RTSha384Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA384_HASH_SIZE])
664
RTSha384Update(&Ctx, pvBuf, cbBuf);
665
rtSha512FinalInternal(&Ctx);
667
bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA384_HASH_SIZE) == 0;
669
RT_ZERO(Ctx.AltPrivate.auH);
672
RT_EXPORT_SYMBOL(RTSha384Check);
676
* SHA-512/224 is just SHA-512 with different initial values an a truncated result.
679
RTDECL(void) RTSha512t224Init(PRTSHA512T224CONTEXT pCtx)
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);
692
RT_EXPORT_SYMBOL(RTSha512t224Init);
695
RTDECL(void) RTSha512t224Update(PRTSHA512T224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
697
RTSha512Update(pCtx, pvBuf, cbBuf);
699
RT_EXPORT_SYMBOL(RTSha512t224Update);
702
RTDECL(void) RTSha512t224Final(PRTSHA512T224CONTEXT pCtx, uint8_t pabDigest[RTSHA512T224_HASH_SIZE])
704
rtSha512FinalInternal(pCtx);
705
memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512T224_HASH_SIZE);
706
RT_ZERO(pCtx->AltPrivate.auH);
708
RT_EXPORT_SYMBOL(RTSha512t224Final);
711
RTDECL(void) RTSha512t224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512T224_HASH_SIZE])
713
RTSHA512T224CONTEXT Ctx;
714
RTSha512t224Init(&Ctx);
715
RTSha512t224Update(&Ctx, pvBuf, cbBuf);
716
RTSha512t224Final(&Ctx, pabDigest);
718
RT_EXPORT_SYMBOL(RTSha512t224);
721
RTDECL(bool) RTSha512t224Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512T224_HASH_SIZE])
723
RTSHA512T224CONTEXT Ctx;
724
RTSha512t224Init(&Ctx);
725
RTSha512t224Update(&Ctx, pvBuf, cbBuf);
726
rtSha512FinalInternal(&Ctx);
728
bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512T224_HASH_SIZE) == 0;
730
RT_ZERO(Ctx.AltPrivate.auH);
733
RT_EXPORT_SYMBOL(RTSha512t224Check);
737
* SHA-512/256 is just SHA-512 with different initial values an a truncated result.
740
RTDECL(void) RTSha512t256Init(PRTSHA512T256CONTEXT pCtx)
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);
753
RT_EXPORT_SYMBOL(RTSha512t256Init);
756
RTDECL(void) RTSha512t256Update(PRTSHA512T256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
758
RTSha512Update(pCtx, pvBuf, cbBuf);
760
RT_EXPORT_SYMBOL(RTSha512t256Update);
763
RTDECL(void) RTSha512t256Final(PRTSHA512T256CONTEXT pCtx, uint8_t pabDigest[RTSHA512T256_HASH_SIZE])
765
rtSha512FinalInternal(pCtx);
766
memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512T256_HASH_SIZE);
767
RT_ZERO(pCtx->AltPrivate.auH);
769
RT_EXPORT_SYMBOL(RTSha512t256Final);
772
RTDECL(void) RTSha512t256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512T256_HASH_SIZE])
774
RTSHA512T256CONTEXT Ctx;
775
RTSha512t256Init(&Ctx);
776
RTSha512t256Update(&Ctx, pvBuf, cbBuf);
777
RTSha512t256Final(&Ctx, pabDigest);
779
RT_EXPORT_SYMBOL(RTSha512t256);
782
RTDECL(bool) RTSha512t256Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512T256_HASH_SIZE])
784
RTSHA512T256CONTEXT Ctx;
785
RTSha512t256Init(&Ctx);
786
RTSha512t256Update(&Ctx, pvBuf, cbBuf);
787
rtSha512FinalInternal(&Ctx);
789
bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512T256_HASH_SIZE) == 0;
791
RT_ZERO(Ctx.AltPrivate.auH);
794
RT_EXPORT_SYMBOL(RTSha512t256Check);