summaryrefslogtreecommitdiffstats
path: root/src/VBox/Runtime/common/checksum/alt-sha512.cpp
blob: 166889eb9ff0719d6790a5ba8bd9d8be8ec00fc7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
/* $Id: alt-sha512.cpp $ */
/** @file
 * IPRT - SHA-512 and SHA-384 hash functions, Alternative Implementation.
 */

/*
 * Copyright (C) 2009-2019 Oracle Corporation
 *
 * This file is part of VirtualBox Open Source Edition (OSE), as
 * available from http://www.virtualbox.org. This file is free software;
 * you can redistribute it and/or modify it under the terms of the GNU
 * General Public License (GPL) as published by the Free Software
 * Foundation, in version 2 as it comes in the "COPYING" file of the
 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
 *
 * The contents of this file may alternatively be used under the terms
 * of the Common Development and Distribution License Version 1.0
 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
 * VirtualBox OSE distribution, in which case the provisions of the
 * CDDL are applicable instead of those of the GPL.
 *
 * You may elect to license modified versions of this file under the
 * terms and conditions of either the GPL or the CDDL or both.
 */


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/
/** The SHA-512 block size (in bytes). */
#define RTSHA512_BLOCK_SIZE   128U

/** Enables the unrolled code. */
#define RTSHA512_UNROLLED 1


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include "internal/iprt.h"
#include <iprt/types.h>
#include <iprt/assert.h>
#include <iprt/asm.h>
#include <iprt/string.h>


/** Our private context structure. */
typedef struct RTSHA512ALTPRIVATECTX
{
    /** The W array.
     * Buffering happens in the first 16 words, converted from big endian to host
     * endian immediately before processing.  The amount of buffered data is kept
     * in the 6 least significant bits of cbMessage. */
    uint64_t    auW[80];
    /** The message length (in bytes). */
    RTUINT128U  cbMessage;
    /** The 8 hash values. */
    uint64_t    auH[8];
} RTSHA512ALTPRIVATECTX;

#define RT_SHA512_PRIVATE_ALT_CONTEXT
#include <iprt/sha.h>


AssertCompile(RT_SIZEOFMEMB(RTSHA512CONTEXT, abPadding) >= RT_SIZEOFMEMB(RTSHA512CONTEXT, AltPrivate));
AssertCompileMemberSize(RTSHA512ALTPRIVATECTX, auH, RTSHA512_HASH_SIZE);


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
#ifndef RTSHA512_UNROLLED
/** The K constants. */
static uint64_t const g_auKs[] =
{
    UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
    UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118),
    UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2),
    UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694),
    UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
    UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5),
    UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4),
    UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70),
    UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df),
    UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
    UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30),
    UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8),
    UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8),
    UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3),
    UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
    UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b),
    UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178),
    UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b),
    UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c),
    UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817),
};
#endif /* !RTSHA512_UNROLLED */



RTDECL(void) RTSha512Init(PRTSHA512CONTEXT pCtx)
{
    pCtx->AltPrivate.cbMessage.s.Lo = 0;
    pCtx->AltPrivate.cbMessage.s.Hi = 0;
    pCtx->AltPrivate.auH[0] = UINT64_C(0x6a09e667f3bcc908);
    pCtx->AltPrivate.auH[1] = UINT64_C(0xbb67ae8584caa73b);
    pCtx->AltPrivate.auH[2] = UINT64_C(0x3c6ef372fe94f82b);
    pCtx->AltPrivate.auH[3] = UINT64_C(0xa54ff53a5f1d36f1);
    pCtx->AltPrivate.auH[4] = UINT64_C(0x510e527fade682d1);
    pCtx->AltPrivate.auH[5] = UINT64_C(0x9b05688c2b3e6c1f);
    pCtx->AltPrivate.auH[6] = UINT64_C(0x1f83d9abfb41bd6b);
    pCtx->AltPrivate.auH[7] = UINT64_C(0x5be0cd19137e2179);
}
RT_EXPORT_SYMBOL(RTSha512Init);


/** Function 4.8. */
DECL_FORCE_INLINE(uint64_t) rtSha512Ch(uint64_t uX, uint64_t uY, uint64_t uZ)
{
#if 1
    /* Optimization that saves one operation and probably a temporary variable. */
    uint64_t uResult = uY;
    uResult ^= uZ;
    uResult &= uX;
    uResult ^= uZ;
    return uResult;
#else
    /* The original. */
    uint64_t uResult = uX & uY;
    uResult ^= ~uX & uZ;
    return uResult;
#endif
}


/** Function 4.9. */
DECL_FORCE_INLINE(uint64_t) rtSha512Maj(uint64_t uX, uint64_t uY, uint64_t uZ)
{
#if 1
    /* Optimization that save one operation and probably a temporary variable. */
    uint64_t uResult = uY;
    uResult ^= uZ;
    uResult &= uX;
    uResult ^= uY & uZ;
    return uResult;
#else
    /* The original. */
    uint64_t uResult = uX & uY;
    uResult ^= uX & uZ;
    uResult ^= uY & uZ;
    return uResult;
#endif
}


/** Function 4.10. */
DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma0(uint64_t uX)
{
    uint64_t uResult = uX = ASMRotateRightU64(uX, 28);
    uX = ASMRotateRightU64(uX, 34 - 28);
    uResult ^= uX;
    uX = ASMRotateRightU64(uX, 39 - 34);
    uResult ^= uX;
    return uResult;
}


/** Function 4.11. */
DECL_FORCE_INLINE(uint64_t) rtSha512CapitalSigma1(uint64_t uX)
{
    uint64_t uResult = uX = ASMRotateRightU64(uX, 14);
    uX = ASMRotateRightU64(uX, 18 - 14);
    uResult ^= uX;
    uX = ASMRotateRightU64(uX, 41 - 18);
    uResult ^= uX;
    return uResult;
}


/** Function 4.12. */
DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma0(uint64_t uX)
{
    uint64_t uResult = uX >> 7;
    uX = ASMRotateRightU64(uX, 1);
    uResult ^= uX;
    uX = ASMRotateRightU64(uX, 8 - 1);
    uResult ^= uX;
    return uResult;
}


/** Function 4.13. */
DECL_FORCE_INLINE(uint64_t) rtSha512SmallSigma1(uint64_t uX)
{
    uint64_t uResult = uX >> 6;
    uX = ASMRotateRightU64(uX, 19);
    uResult ^= uX;
    uX = ASMRotateRightU64(uX, 61 - 19);
    uResult ^= uX;
    return uResult;
}


/**
 * Initializes the auW array from the specfied input block.
 *
 * @param   pCtx                The SHA-512 context.
 * @param   pbBlock             The block.  Must be 64-bit aligned.
 */
DECLINLINE(void) rtSha512BlockInit(PRTSHA512CONTEXT pCtx, uint8_t const *pbBlock)
{
#ifdef RTSHA512_UNROLLED
    uint64_t const *puSrc = (uint64_t const *)pbBlock;
    uint64_t       *puW   = &pCtx->AltPrivate.auW[0];
    Assert(!((uintptr_t)puSrc & 7));
    Assert(!((uintptr_t)puW & 7));

    /* Copy and byte-swap the block. Initializing the rest of the Ws are done
       in the processing loop. */
# ifdef RT_LITTLE_ENDIAN
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);

    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);

    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);

    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
    *puW++ = ASMByteSwapU64(*puSrc++);
# else
    memcpy(puW, puSrc, RTSHA512_BLOCK_SIZE);
# endif

#else  /* !RTSHA512_UNROLLED */

    uint64_t const *pu32Block = (uint64_t const *)pbBlock;
    Assert(!((uintptr_t)pu32Block & 3));

    unsigned iWord;
    for (iWord = 0; iWord < 16; iWord++)
        pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pu32Block[iWord]);

    for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
    {
        uint64_t u64 = rtSha512SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
        u64         += rtSha512SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
        u64         += pCtx->AltPrivate.auW[iWord - 7];
        u64         += pCtx->AltPrivate.auW[iWord - 16];
        pCtx->AltPrivate.auW[iWord] = u64;
    }
#endif /* !RTSHA512_UNROLLED */
}


/**
 * Initializes the auW array from data buffered in the first part of the array.
 *
 * @param   pCtx                The SHA-512 context.
 */
DECLINLINE(void) rtSha512BlockInitBuffered(PRTSHA512CONTEXT pCtx)
{
#ifdef RTSHA512_UNROLLED
    uint64_t *puW = &pCtx->AltPrivate.auW[0];
    Assert(!((uintptr_t)puW & 7));

    /* Do the byte swap if necessary. Initializing the rest of the Ws are done
       in the processing loop. */
# ifdef RT_LITTLE_ENDIAN
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;

    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;

    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;

    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
    *puW = ASMByteSwapU64(*puW); puW++;
# endif

#else  /* !RTSHA512_UNROLLED */

    unsigned iWord;
    for (iWord = 0; iWord < 16; iWord++)
        pCtx->AltPrivate.auW[iWord] = RT_BE2H_U64(pCtx->AltPrivate.auW[iWord]);

    for (; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
    {
        uint64_t u64 = rtSha512SmallSigma1(pCtx->AltPrivate.auW[iWord - 2]);
        u64         += rtSha512SmallSigma0(pCtx->AltPrivate.auW[iWord - 15]);
        u64         += pCtx->AltPrivate.auW[iWord - 7];
        u64         += pCtx->AltPrivate.auW[iWord - 16];
        pCtx->AltPrivate.auW[iWord] = u64;
    }
#endif /* !RTSHA512_UNROLLED */
}


/**
 * Process the current block.
 *
 * Requires one of the rtSha512BlockInit functions to be called first.
 *
 * @param   pCtx                The SHA-512 context.
 */
static void rtSha512BlockProcess(PRTSHA512CONTEXT pCtx)
{
    uint64_t uA = pCtx->AltPrivate.auH[0];
    uint64_t uB = pCtx->AltPrivate.auH[1];
    uint64_t uC = pCtx->AltPrivate.auH[2];
    uint64_t uD = pCtx->AltPrivate.auH[3];
    uint64_t uE = pCtx->AltPrivate.auH[4];
    uint64_t uF = pCtx->AltPrivate.auH[5];
    uint64_t uG = pCtx->AltPrivate.auH[6];
    uint64_t uH = pCtx->AltPrivate.auH[7];

#ifdef RTSHA512_UNROLLED
    uint64_t *puW = &pCtx->AltPrivate.auW[0];
# define RTSHA512_BODY(a_iWord, a_uK, a_uA, a_uB, a_uC, a_uD, a_uE, a_uF, a_uG, a_uH) \
        do { \
            if ((a_iWord) < 16) \
                a_uH += *puW++; \
            else \
            { \
                uint64_t u64 = puW[-16]; \
                u64 += rtSha512SmallSigma0(puW[-15]); \
                u64 += puW[-7]; \
                u64 += rtSha512SmallSigma1(puW[-2]); \
                if (a_iWord < 80-2) *puW++ = u64; else puW++; \
                a_uH += u64; \
            } \
            \
            a_uH += rtSha512CapitalSigma1(a_uE); \
            a_uH += a_uK; \
            a_uH += rtSha512Ch(a_uE, a_uF, a_uG); \
            a_uD += a_uH; \
            \
            a_uH += rtSha512CapitalSigma0(a_uA); \
            a_uH += rtSha512Maj(a_uA, a_uB, a_uC); \
        } while (0)
# define RTSHA512_EIGHT(a_uK0, a_uK1, a_uK2, a_uK3, a_uK4, a_uK5, a_uK6, a_uK7, a_iFirst) \
        do { \
            RTSHA512_BODY(a_iFirst + 0, a_uK0, uA, uB, uC, uD, uE, uF, uG, uH); \
            RTSHA512_BODY(a_iFirst + 1, a_uK1, uH, uA, uB, uC, uD, uE, uF, uG); \
            RTSHA512_BODY(a_iFirst + 2, a_uK2, uG, uH, uA, uB, uC, uD, uE, uF); \
            RTSHA512_BODY(a_iFirst + 3, a_uK3, uF, uG, uH, uA, uB, uC, uD, uE); \
            RTSHA512_BODY(a_iFirst + 4, a_uK4, uE, uF, uG, uH, uA, uB, uC, uD); \
            RTSHA512_BODY(a_iFirst + 5, a_uK5, uD, uE, uF, uG, uH, uA, uB, uC); \
            RTSHA512_BODY(a_iFirst + 6, a_uK6, uC, uD, uE, uF, uG, uH, uA, uB); \
            RTSHA512_BODY(a_iFirst + 7, a_uK7, uB, uC, uD, uE, uF, uG, uH, uA); \
        } while (0)
    RTSHA512_EIGHT(UINT64_C(0x428a2f98d728ae22), UINT64_C(0x7137449123ef65cd), UINT64_C(0xb5c0fbcfec4d3b2f), UINT64_C(0xe9b5dba58189dbbc),
                   UINT64_C(0x3956c25bf348b538), UINT64_C(0x59f111f1b605d019), UINT64_C(0x923f82a4af194f9b), UINT64_C(0xab1c5ed5da6d8118),
                   0);
    RTSHA512_EIGHT(UINT64_C(0xd807aa98a3030242), UINT64_C(0x12835b0145706fbe), UINT64_C(0x243185be4ee4b28c), UINT64_C(0x550c7dc3d5ffb4e2),
                   UINT64_C(0x72be5d74f27b896f), UINT64_C(0x80deb1fe3b1696b1), UINT64_C(0x9bdc06a725c71235), UINT64_C(0xc19bf174cf692694),
                   8);
    RTSHA512_EIGHT(UINT64_C(0xe49b69c19ef14ad2), UINT64_C(0xefbe4786384f25e3), UINT64_C(0x0fc19dc68b8cd5b5), UINT64_C(0x240ca1cc77ac9c65),
                   UINT64_C(0x2de92c6f592b0275), UINT64_C(0x4a7484aa6ea6e483), UINT64_C(0x5cb0a9dcbd41fbd4), UINT64_C(0x76f988da831153b5),
                   16);
    RTSHA512_EIGHT(UINT64_C(0x983e5152ee66dfab), UINT64_C(0xa831c66d2db43210), UINT64_C(0xb00327c898fb213f), UINT64_C(0xbf597fc7beef0ee4),
                   UINT64_C(0xc6e00bf33da88fc2), UINT64_C(0xd5a79147930aa725), UINT64_C(0x06ca6351e003826f), UINT64_C(0x142929670a0e6e70),
                   24);
    RTSHA512_EIGHT(UINT64_C(0x27b70a8546d22ffc), UINT64_C(0x2e1b21385c26c926), UINT64_C(0x4d2c6dfc5ac42aed), UINT64_C(0x53380d139d95b3df),
                   UINT64_C(0x650a73548baf63de), UINT64_C(0x766a0abb3c77b2a8), UINT64_C(0x81c2c92e47edaee6), UINT64_C(0x92722c851482353b),
                   32);
    RTSHA512_EIGHT(UINT64_C(0xa2bfe8a14cf10364), UINT64_C(0xa81a664bbc423001), UINT64_C(0xc24b8b70d0f89791), UINT64_C(0xc76c51a30654be30),
                   UINT64_C(0xd192e819d6ef5218), UINT64_C(0xd69906245565a910), UINT64_C(0xf40e35855771202a), UINT64_C(0x106aa07032bbd1b8),
                   40);
    RTSHA512_EIGHT(UINT64_C(0x19a4c116b8d2d0c8), UINT64_C(0x1e376c085141ab53), UINT64_C(0x2748774cdf8eeb99), UINT64_C(0x34b0bcb5e19b48a8),
                   UINT64_C(0x391c0cb3c5c95a63), UINT64_C(0x4ed8aa4ae3418acb), UINT64_C(0x5b9cca4f7763e373), UINT64_C(0x682e6ff3d6b2b8a3),
                   48);
    RTSHA512_EIGHT(UINT64_C(0x748f82ee5defb2fc), UINT64_C(0x78a5636f43172f60), UINT64_C(0x84c87814a1f0ab72), UINT64_C(0x8cc702081a6439ec),
                   UINT64_C(0x90befffa23631e28), UINT64_C(0xa4506cebde82bde9), UINT64_C(0xbef9a3f7b2c67915), UINT64_C(0xc67178f2e372532b),
                   56);
    RTSHA512_EIGHT(UINT64_C(0xca273eceea26619c), UINT64_C(0xd186b8c721c0c207), UINT64_C(0xeada7dd6cde0eb1e), UINT64_C(0xf57d4f7fee6ed178),
                   UINT64_C(0x06f067aa72176fba), UINT64_C(0x0a637dc5a2c898a6), UINT64_C(0x113f9804bef90dae), UINT64_C(0x1b710b35131c471b),
                   64);
    RTSHA512_EIGHT(UINT64_C(0x28db77f523047d84), UINT64_C(0x32caab7b40c72493), UINT64_C(0x3c9ebe0a15c9bebc), UINT64_C(0x431d67c49c100d4c),
                   UINT64_C(0x4cc5d4becb3e42b6), UINT64_C(0x597f299cfc657e2a), UINT64_C(0x5fcb6fab3ad6faec), UINT64_C(0x6c44198c4a475817),
                   72);
#else
    for (unsigned iWord = 0; iWord < RT_ELEMENTS(pCtx->AltPrivate.auW); iWord++)
    {
        uint64_t uT1 = uH;
        uT1 += rtSha512CapitalSigma1(uE);
        uT1 += rtSha512Ch(uE, uF, uG);
        uT1 += g_auKs[iWord];
        uT1 += pCtx->AltPrivate.auW[iWord];

        uint64_t uT2 = rtSha512CapitalSigma0(uA);
        uT2 += rtSha512Maj(uA, uB, uC);

        uH = uG;
        uG = uF;
        uF = uE;
        uE = uD + uT1;
        uD = uC;
        uC = uB;
        uB = uA;
        uA = uT1 + uT2;
    }
#endif

    pCtx->AltPrivate.auH[0] += uA;
    pCtx->AltPrivate.auH[1] += uB;
    pCtx->AltPrivate.auH[2] += uC;
    pCtx->AltPrivate.auH[3] += uD;
    pCtx->AltPrivate.auH[4] += uE;
    pCtx->AltPrivate.auH[5] += uF;
    pCtx->AltPrivate.auH[6] += uG;
    pCtx->AltPrivate.auH[7] += uH;
}


RTDECL(void) RTSha512Update(PRTSHA512CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
{
    Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);
    uint8_t const *pbBuf = (uint8_t const *)pvBuf;

    /*
     * Deal with buffered bytes first.
     */
    size_t cbBuffered = (size_t)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
    if (cbBuffered)
    {
        size_t cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
        if (cbBuf >= cbMissing)
        {
            memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbMissing);
            pCtx->AltPrivate.cbMessage.s.Lo += cbMissing;
            if (!pCtx->AltPrivate.cbMessage.s.Lo)
                pCtx->AltPrivate.cbMessage.s.Hi++;
            pbBuf += cbMissing;
            cbBuf -= cbMissing;

            rtSha512BlockInitBuffered(pCtx);
            rtSha512BlockProcess(pCtx);
        }
        else
        {
            memcpy((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, pbBuf, cbBuf);
            pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
            return;
        }
    }

    if (!((uintptr_t)pbBuf & 7))
    {
        /*
         * Process full blocks directly from the input buffer.
         */
        while (cbBuf >= RTSHA512_BLOCK_SIZE)
        {
            rtSha512BlockInit(pCtx, pbBuf);
            rtSha512BlockProcess(pCtx);

            pCtx->AltPrivate.cbMessage.s.Lo += RTSHA512_BLOCK_SIZE;
            if (!pCtx->AltPrivate.cbMessage.s.Lo)
                pCtx->AltPrivate.cbMessage.s.Hi++;
            pbBuf += RTSHA512_BLOCK_SIZE;
            cbBuf -= RTSHA512_BLOCK_SIZE;
        }
    }
    else
    {
        /*
         * Unaligned input, so buffer it.
         */
        while (cbBuf >= RTSHA512_BLOCK_SIZE)
        {
            memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, RTSHA512_BLOCK_SIZE);
            rtSha512BlockInitBuffered(pCtx);
            rtSha512BlockProcess(pCtx);

            pCtx->AltPrivate.cbMessage.s.Lo += RTSHA512_BLOCK_SIZE;
            if (!pCtx->AltPrivate.cbMessage.s.Lo)
                pCtx->AltPrivate.cbMessage.s.Hi++;
            pbBuf += RTSHA512_BLOCK_SIZE;
            cbBuf -= RTSHA512_BLOCK_SIZE;
        }
    }

    /*
     * Stash any remaining bytes into the context buffer.
     */
    if (cbBuf > 0)
    {
        memcpy((uint8_t *)&pCtx->AltPrivate.auW[0], pbBuf, cbBuf);
        pCtx->AltPrivate.cbMessage.s.Lo += cbBuf;
        if (!pCtx->AltPrivate.cbMessage.s.Lo)
            pCtx->AltPrivate.cbMessage.s.Hi++;
    }
}
RT_EXPORT_SYMBOL(RTSha512Update);


/**
 * Internal worker for RTSha512Final and RTSha384Final that finalizes the
 * computation but does not copy out the hash value.
 *
 * @param   pCtx                The SHA-512 context.
 */
static void rtSha512FinalInternal(PRTSHA512CONTEXT pCtx)
{
    Assert(pCtx->AltPrivate.cbMessage.s.Hi < UINT64_MAX / 8);

    /*
     * Complete the message by adding a single bit (0x80), padding till
     * the next 448-bit boundrary, the add the message length.
     */
    RTUINT128U cMessageBits = pCtx->AltPrivate.cbMessage;
    cMessageBits.s.Hi <<= 3;
    cMessageBits.s.Hi |= cMessageBits.s.Lo >> 61;
    cMessageBits.s.Lo <<= 3;

    unsigned cbMissing = RTSHA512_BLOCK_SIZE - ((unsigned)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U));
    static uint8_t const s_abSingleBitAndSomePadding[20] =
    {   0x80, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,  0, 0, 0, 0,};
    if (cbMissing < 1U + 16U)
        /* Less than 64+16 bits left in the current block, force a new block. */
        RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, sizeof(s_abSingleBitAndSomePadding));
    else
        RTSha512Update(pCtx, &s_abSingleBitAndSomePadding, 1);

    unsigned cbBuffered = (unsigned)pCtx->AltPrivate.cbMessage.s.Lo & (RTSHA512_BLOCK_SIZE - 1U);
    cbMissing = RTSHA512_BLOCK_SIZE - cbBuffered;
    Assert(cbMissing >= 16);
    memset((uint8_t *)&pCtx->AltPrivate.auW[0] + cbBuffered, 0, cbMissing - 16);

    pCtx->AltPrivate.auW[14] = RT_H2BE_U64(cMessageBits.s.Hi);
    pCtx->AltPrivate.auW[15] = RT_H2BE_U64(cMessageBits.s.Lo);

    /*
     * Process the last buffered block constructed/completed above.
     */
    rtSha512BlockInitBuffered(pCtx);
    rtSha512BlockProcess(pCtx);

    /*
     * Convert the byte order of the hash words and we're done.
     */
    pCtx->AltPrivate.auH[0] = RT_H2BE_U64(pCtx->AltPrivate.auH[0]);
    pCtx->AltPrivate.auH[1] = RT_H2BE_U64(pCtx->AltPrivate.auH[1]);
    pCtx->AltPrivate.auH[2] = RT_H2BE_U64(pCtx->AltPrivate.auH[2]);
    pCtx->AltPrivate.auH[3] = RT_H2BE_U64(pCtx->AltPrivate.auH[3]);
    pCtx->AltPrivate.auH[4] = RT_H2BE_U64(pCtx->AltPrivate.auH[4]);
    pCtx->AltPrivate.auH[5] = RT_H2BE_U64(pCtx->AltPrivate.auH[5]);
    pCtx->AltPrivate.auH[6] = RT_H2BE_U64(pCtx->AltPrivate.auH[6]);
    pCtx->AltPrivate.auH[7] = RT_H2BE_U64(pCtx->AltPrivate.auH[7]);

    RT_ZERO(pCtx->AltPrivate.auW);
    pCtx->AltPrivate.cbMessage.s.Lo = UINT64_MAX;
    pCtx->AltPrivate.cbMessage.s.Hi = UINT64_MAX;
}
RT_EXPORT_SYMBOL(RTSha512Final);


RTDECL(void) RTSha512Final(PRTSHA512CONTEXT pCtx, uint8_t pabDigest[RTSHA512_HASH_SIZE])
{
    rtSha512FinalInternal(pCtx);
    memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512_HASH_SIZE);
    RT_ZERO(pCtx->AltPrivate.auH);
}
RT_EXPORT_SYMBOL(RTSha512Final);


RTDECL(void) RTSha512(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512_HASH_SIZE])
{
    RTSHA512CONTEXT Ctx;
    RTSha512Init(&Ctx);
    RTSha512Update(&Ctx, pvBuf, cbBuf);
    RTSha512Final(&Ctx, pabDigest);
}
RT_EXPORT_SYMBOL(RTSha512);


RTDECL(bool) RTSha512Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512_HASH_SIZE])
{
    RTSHA512CONTEXT Ctx;
    RTSha512Init(&Ctx);
    RTSha512Update(&Ctx, pvBuf, cbBuf);
    rtSha512FinalInternal(&Ctx);

    bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512_HASH_SIZE) == 0;

    RT_ZERO(Ctx.AltPrivate.auH);
    return fRet;
}
RT_EXPORT_SYMBOL(RTSha512Check);



/*
 * SHA-384 is just SHA-512 with different initial values an a truncated result.
 */

RTDECL(void) RTSha384Init(PRTSHA384CONTEXT pCtx)
{
    pCtx->AltPrivate.cbMessage.s.Lo = 0;
    pCtx->AltPrivate.cbMessage.s.Hi = 0;
    pCtx->AltPrivate.auH[0] = UINT64_C(0xcbbb9d5dc1059ed8);
    pCtx->AltPrivate.auH[1] = UINT64_C(0x629a292a367cd507);
    pCtx->AltPrivate.auH[2] = UINT64_C(0x9159015a3070dd17);
    pCtx->AltPrivate.auH[3] = UINT64_C(0x152fecd8f70e5939);
    pCtx->AltPrivate.auH[4] = UINT64_C(0x67332667ffc00b31);
    pCtx->AltPrivate.auH[5] = UINT64_C(0x8eb44a8768581511);
    pCtx->AltPrivate.auH[6] = UINT64_C(0xdb0c2e0d64f98fa7);
    pCtx->AltPrivate.auH[7] = UINT64_C(0x47b5481dbefa4fa4);
}
RT_EXPORT_SYMBOL(RTSha384Init);


RTDECL(void) RTSha384Update(PRTSHA384CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
{
    RTSha512Update(pCtx, pvBuf, cbBuf);
}
RT_EXPORT_SYMBOL(RTSha384Update);


RTDECL(void) RTSha384Final(PRTSHA384CONTEXT pCtx, uint8_t pabDigest[RTSHA384_HASH_SIZE])
{
    rtSha512FinalInternal(pCtx);
    memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA384_HASH_SIZE);
    RT_ZERO(pCtx->AltPrivate.auH);
}
RT_EXPORT_SYMBOL(RTSha384Final);


RTDECL(void) RTSha384(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA384_HASH_SIZE])
{
    RTSHA384CONTEXT Ctx;
    RTSha384Init(&Ctx);
    RTSha384Update(&Ctx, pvBuf, cbBuf);
    RTSha384Final(&Ctx, pabDigest);
}
RT_EXPORT_SYMBOL(RTSha384);


RTDECL(bool) RTSha384Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA384_HASH_SIZE])
{
    RTSHA384CONTEXT Ctx;
    RTSha384Init(&Ctx);
    RTSha384Update(&Ctx, pvBuf, cbBuf);
    rtSha512FinalInternal(&Ctx);

    bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA384_HASH_SIZE) == 0;

    RT_ZERO(Ctx.AltPrivate.auH);
    return fRet;
}
RT_EXPORT_SYMBOL(RTSha384Check);


/*
 * SHA-512/224 is just SHA-512 with different initial values an a truncated result.
 */

RTDECL(void) RTSha512t224Init(PRTSHA512T224CONTEXT pCtx)
{
    pCtx->AltPrivate.cbMessage.s.Lo = 0;
    pCtx->AltPrivate.cbMessage.s.Hi = 0;
    pCtx->AltPrivate.auH[0] = UINT64_C(0x8c3d37c819544da2);
    pCtx->AltPrivate.auH[1] = UINT64_C(0x73e1996689dcd4d6);
    pCtx->AltPrivate.auH[2] = UINT64_C(0x1dfab7ae32ff9c82);
    pCtx->AltPrivate.auH[3] = UINT64_C(0x679dd514582f9fcf);
    pCtx->AltPrivate.auH[4] = UINT64_C(0x0f6d2b697bd44da8);
    pCtx->AltPrivate.auH[5] = UINT64_C(0x77e36f7304c48942);
    pCtx->AltPrivate.auH[6] = UINT64_C(0x3f9d85a86a1d36c8);
    pCtx->AltPrivate.auH[7] = UINT64_C(0x1112e6ad91d692a1);
}
RT_EXPORT_SYMBOL(RTSha512t224Init);


RTDECL(void) RTSha512t224Update(PRTSHA512T224CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
{
    RTSha512Update(pCtx, pvBuf, cbBuf);
}
RT_EXPORT_SYMBOL(RTSha512t224Update);


RTDECL(void) RTSha512t224Final(PRTSHA512T224CONTEXT pCtx, uint8_t pabDigest[RTSHA512T224_HASH_SIZE])
{
    rtSha512FinalInternal(pCtx);
    memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512T224_HASH_SIZE);
    RT_ZERO(pCtx->AltPrivate.auH);
}
RT_EXPORT_SYMBOL(RTSha512t224Final);


RTDECL(void) RTSha512t224(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512T224_HASH_SIZE])
{
    RTSHA512T224CONTEXT Ctx;
    RTSha512t224Init(&Ctx);
    RTSha512t224Update(&Ctx, pvBuf, cbBuf);
    RTSha512t224Final(&Ctx, pabDigest);
}
RT_EXPORT_SYMBOL(RTSha512t224);


RTDECL(bool) RTSha512t224Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512T224_HASH_SIZE])
{
    RTSHA512T224CONTEXT Ctx;
    RTSha512t224Init(&Ctx);
    RTSha512t224Update(&Ctx, pvBuf, cbBuf);
    rtSha512FinalInternal(&Ctx);

    bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512T224_HASH_SIZE) == 0;

    RT_ZERO(Ctx.AltPrivate.auH);
    return fRet;
}
RT_EXPORT_SYMBOL(RTSha512t224Check);


/*
 * SHA-512/256 is just SHA-512 with different initial values an a truncated result.
 */

RTDECL(void) RTSha512t256Init(PRTSHA512T256CONTEXT pCtx)
{
    pCtx->AltPrivate.cbMessage.s.Lo = 0;
    pCtx->AltPrivate.cbMessage.s.Hi = 0;
    pCtx->AltPrivate.auH[0] = UINT64_C(0x22312194fc2bf72c);
    pCtx->AltPrivate.auH[1] = UINT64_C(0x9f555fa3c84c64c2);
    pCtx->AltPrivate.auH[2] = UINT64_C(0x2393b86b6f53b151);
    pCtx->AltPrivate.auH[3] = UINT64_C(0x963877195940eabd);
    pCtx->AltPrivate.auH[4] = UINT64_C(0x96283ee2a88effe3);
    pCtx->AltPrivate.auH[5] = UINT64_C(0xbe5e1e2553863992);
    pCtx->AltPrivate.auH[6] = UINT64_C(0x2b0199fc2c85b8aa);
    pCtx->AltPrivate.auH[7] = UINT64_C(0x0eb72ddc81c52ca2);
}
RT_EXPORT_SYMBOL(RTSha512t256Init);


RTDECL(void) RTSha512t256Update(PRTSHA512T256CONTEXT pCtx, const void *pvBuf, size_t cbBuf)
{
    RTSha512Update(pCtx, pvBuf, cbBuf);
}
RT_EXPORT_SYMBOL(RTSha512t256Update);


RTDECL(void) RTSha512t256Final(PRTSHA512T256CONTEXT pCtx, uint8_t pabDigest[RTSHA512T256_HASH_SIZE])
{
    rtSha512FinalInternal(pCtx);
    memcpy(pabDigest, &pCtx->AltPrivate.auH[0], RTSHA512T256_HASH_SIZE);
    RT_ZERO(pCtx->AltPrivate.auH);
}
RT_EXPORT_SYMBOL(RTSha512t256Final);


RTDECL(void) RTSha512t256(const void *pvBuf, size_t cbBuf, uint8_t pabDigest[RTSHA512T256_HASH_SIZE])
{
    RTSHA512T256CONTEXT Ctx;
    RTSha512t256Init(&Ctx);
    RTSha512t256Update(&Ctx, pvBuf, cbBuf);
    RTSha512t256Final(&Ctx, pabDigest);
}
RT_EXPORT_SYMBOL(RTSha512t256);


RTDECL(bool) RTSha512t256Check(const void *pvBuf, size_t cbBuf, uint8_t const pabHash[RTSHA512T256_HASH_SIZE])
{
    RTSHA512T256CONTEXT Ctx;
    RTSha512t256Init(&Ctx);
    RTSha512t256Update(&Ctx, pvBuf, cbBuf);
    rtSha512FinalInternal(&Ctx);

    bool fRet = memcmp(pabHash, &Ctx.AltPrivate.auH[0], RTSHA512T256_HASH_SIZE) == 0;

    RT_ZERO(Ctx.AltPrivate.auH);
    return fRet;
}
RT_EXPORT_SYMBOL(RTSha512t256Check);