-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsha512.c
More file actions
1736 lines (1393 loc) · 60.7 KB
/
Copy pathsha512.c
File metadata and controls
1736 lines (1393 loc) · 60.7 KB
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
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* sha512.c
*
* Copyright (C) 2006-2016 wolfSSL Inc.
*
* This file is part of wolfSSL.
*
* wolfSSL is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfSSL is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <wolfssl/wolfcrypt/settings.h>
#include <wolfssl/wolfcrypt/sha512.h>
#ifdef WOLFSSL_SHA512
#ifdef HAVE_FIPS
int wc_InitSha512(Sha512* sha)
{
return InitSha512_fips(sha);
}
int wc_Sha512Update(Sha512* sha, const byte* data, word32 len)
{
return Sha512Update_fips(sha, data, len);
}
int wc_Sha512Final(Sha512* sha, byte* out)
{
return Sha512Final_fips(sha, out);
}
#if defined(WOLFSSL_SHA384) || defined(HAVE_AESGCM)
int wc_InitSha384(Sha384* sha)
{
return InitSha384_fips(sha);
}
int wc_Sha384Update(Sha384* sha, const byte* data, word32 len)
{
return Sha384Update_fips(sha, data, len);
}
int wc_Sha384Final(Sha384* sha, byte* out)
{
return Sha384Final_fips(sha, out);
}
#endif /* WOLFSSL_SHA384 */
#else /* else build without using fips */
#include <wolfssl/wolfcrypt/logging.h>
#include <wolfssl/wolfcrypt/error-crypt.h>
#ifdef NO_INLINE
#include <wolfssl/wolfcrypt/misc.h>
#else
#define WOLFSSL_MISC_INCLUDED
#include <wolfcrypt/src/misc.c>
#endif
#ifndef WOLFSSL_HAVE_MIN
#define WOLFSSL_HAVE_MIN
static INLINE word32 min(word32 a, word32 b)
{
return a > b ? b : a;
}
#endif /* WOLFSSL_HAVE_MIN */
#if defined(USE_INTEL_SPEEDUP)
#define HAVE_INTEL_AVX1
#define HAVE_INTEL_AVX2
#endif
#if defined(HAVE_INTEL_AVX1)
/* #define DEBUG_XMM */
#endif
#if defined(HAVE_INTEL_AVX2)
#define HAVE_INTEL_RORX
/* #define DEBUG_YMM */
#endif
/*****
Intel AVX1/AVX2 Macro Control Structure
#if defined(HAVE_INteL_SPEEDUP)
#define HAVE_INTEL_AVX1
#define HAVE_INTEL_AVX2
#endif
int InitSha512(Sha512* sha512) {
Save/Recover XMM, YMM
...
Check Intel AVX cpuid flags
}
#if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
Transform_AVX1() ; # Function prototype
Transform_AVX2() ; #
#endif
_Transform() { # Native Transform Function body
}
int Sha512Update() {
Save/Recover XMM, YMM
...
}
int Sha512Final() {
Save/Recover XMM, YMM
...
}
#if defined(HAVE_INTEL_AVX1)
XMM Instructions/INLINE asm Definitions
#endif
#if defined(HAVE_INTEL_AVX2)
YMM Instructions/INLINE asm Definitions
#endif
#if defnied(HAVE_INTEL_AVX1)
int Transform_AVX1() {
Stitched Message Sched/Round
}
#endif
#if defnied(HAVE_INTEL_AVX2)
int Transform_AVX2() {
Stitched Message Sched/Round
}
#endif
*/
#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
/* Each platform needs to query info type 1 from cpuid to see if aesni is
* supported. Also, let's setup a macro for proper linkage w/o ABI conflicts
*/
#ifndef _MSC_VER
#define cpuid(reg, leaf, sub)\
__asm__ __volatile__ ("cpuid":\
"=a" (reg[0]), "=b" (reg[1]), "=c" (reg[2]), "=d" (reg[3]) :\
"a" (leaf), "c"(sub));
#define XASM_LINK(f) asm(f)
#else
#include <intrin.h>
#define cpuid(a,b) __cpuid((int*)a,b)
#define XASM_LINK(f)
#endif /* _MSC_VER */
#define EAX 0
#define EBX 1
#define ECX 2
#define EDX 3
#define CPUID_AVX1 0x1
#define CPUID_AVX2 0x2
#define CPUID_RDRAND 0x4
#define CPUID_RDSEED 0x8
#define CPUID_BMI2 0x10 /* MULX, RORX */
#define IS_INTEL_AVX1 (cpuid_flags&CPUID_AVX1)
#define IS_INTEL_AVX2 (cpuid_flags&CPUID_AVX2)
#define IS_INTEL_BMI2 (cpuid_flags&CPUID_BMI2)
#define IS_INTEL_RDRAND (cpuid_flags&CPUID_RDRAND)
#define IS_INTEL_RDSEED (cpuid_flags&CPUID_RDSEED)
static word32 cpuid_check = 0 ;
static word32 cpuid_flags = 0 ;
static word32 cpuid_flag(word32 leaf, word32 sub, word32 num, word32 bit) {
int got_intel_cpu=0;
unsigned int reg[5];
reg[4] = '\0' ;
cpuid(reg, 0, 0);
if(XMEMCMP((char *)&(reg[EBX]), "Genu", 4) == 0 &&
XMEMCMP((char *)&(reg[EDX]), "ineI", 4) == 0 &&
XMEMCMP((char *)&(reg[ECX]), "ntel", 4) == 0) {
got_intel_cpu = 1;
}
if (got_intel_cpu) {
cpuid(reg, leaf, sub);
return((reg[num]>>bit)&0x1) ;
}
return 0 ;
}
#define CHECK_SHA512 0x1
#define CHECK_SHA384 0x2
static int set_cpuid_flags(int sha) {
if((cpuid_check & sha) ==0) {
if(cpuid_flag(1, 0, ECX, 28)){ cpuid_flags |= CPUID_AVX1 ;}
if(cpuid_flag(7, 0, EBX, 5)){ cpuid_flags |= CPUID_AVX2 ; }
if(cpuid_flag(7, 0, EBX, 8)) { cpuid_flags |= CPUID_BMI2 ; }
if(cpuid_flag(1, 0, ECX, 30)){ cpuid_flags |= CPUID_RDRAND ; }
if(cpuid_flag(7, 0, EBX, 18)){ cpuid_flags |= CPUID_RDSEED ; }
cpuid_check |= sha ;
return 0 ;
}
return 1 ;
}
/* #if defined(HAVE_INTEL_AVX1/2) at the tail of sha512 */
#if defined(HAVE_INTEL_AVX1)
static int Transform_AVX1(Sha512 *sha512) ;
#endif
#if defined(HAVE_INTEL_AVX2)
static int Transform_AVX2(Sha512 *sha512) ;
#if defined(HAVE_INTEL_AVX1) && defined(HAVE_INTEL_AVX2) && defined(HAVE_INTEL_RORX)
static int Transform_AVX1_RORX(Sha512 *sha512) ;
#endif
#endif
static int _Transform(Sha512 *sha512) ;
static int (*Transform_p)(Sha512* sha512) = _Transform ;
#define Transform(sha512) (*Transform_p)(sha512)
static void set_Transform(void) {
if(set_cpuid_flags(CHECK_SHA512)) return ;
#if defined(HAVE_INTEL_AVX2)
if(IS_INTEL_AVX2 && IS_INTEL_BMI2){
Transform_p = Transform_AVX1_RORX; return ;
Transform_p = Transform_AVX2 ;
/* for avoiding warning,"not used" */
}
#endif
#if defined(HAVE_INTEL_AVX1)
Transform_p = ((IS_INTEL_AVX1) ? Transform_AVX1 : _Transform) ; return ;
#endif
Transform_p = _Transform ; return ;
}
#else
#define Transform(sha512) _Transform(sha512)
#endif
/* Dummy for saving MM_REGs on behalf of Transform */
/* #if defined(HAVE_INTEL_AVX2)
#define SAVE_XMM_YMM __asm__ volatile("orq %%r8, %%r8":::\
"%ymm0","%ymm1","%ymm2","%ymm3","%ymm4","%ymm5","%ymm6","%ymm7","%ymm8","%ymm9","%ymm10","%ymm11",\
"%ymm12","%ymm13","%ymm14","%ymm15")
*/
#if defined(HAVE_INTEL_AVX1)
#define SAVE_XMM_YMM __asm__ volatile("orq %%r8, %%r8":::\
"xmm0","xmm1","xmm2","xmm3","xmm4","xmm5","xmm6","xmm7","xmm8","xmm9","xmm10","xmm11","xmm12","xmm13","xmm14","xmm15")
#else
#define SAVE_XMM_YMM
#endif
#if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
#include <string.h>
#endif /* defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2) */
#if defined(HAVE_INTEL_RORX)
#define ROTR(func, bits, x) \
word64 func(word64 x) { word64 ret ;\
__asm__ ("rorx $"#bits", %1, %0\n\t":"=r"(ret):"r"(x):) ;\
return ret ;\
}
static INLINE ROTR(rotrFixed64_28, 28, x)
static INLINE ROTR(rotrFixed64_34, 34, x)
static INLINE ROTR(rotrFixed64_39, 39, x)
static INLINE ROTR(rotrFixed64_14, 14, x)
static INLINE ROTR(rotrFixed64_18, 18, x)
static INLINE ROTR(rotrFixed64_41, 41, x)
#define S0_RORX(x) (rotrFixed64_28(x)^rotrFixed64_34(x)^rotrFixed64_39(x))
#define S1_RORX(x) (rotrFixed64_14(x)^rotrFixed64_18(x)^rotrFixed64_41(x))
#endif
#if defined(HAVE_BYTEREVERSE64) && !defined(HAVE_INTEL_AVX1) && !defined(HAVE_INTEL_AVX2)
#define ByteReverseWords64(out, in, size) ByteReverseWords64_1(out, size)
#define ByteReverseWords64_1(buf, size)\
{ unsigned int i ;\
for(i=0; i< size/sizeof(word64); i++){\
__asm__ volatile("bswapq %0":"+r"(buf[i])::) ;\
}\
}
#endif
int wc_InitSha512(Sha512* sha512)
{
sha512->digest[0] = W64LIT(0x6a09e667f3bcc908);
sha512->digest[1] = W64LIT(0xbb67ae8584caa73b);
sha512->digest[2] = W64LIT(0x3c6ef372fe94f82b);
sha512->digest[3] = W64LIT(0xa54ff53a5f1d36f1);
sha512->digest[4] = W64LIT(0x510e527fade682d1);
sha512->digest[5] = W64LIT(0x9b05688c2b3e6c1f);
sha512->digest[6] = W64LIT(0x1f83d9abfb41bd6b);
sha512->digest[7] = W64LIT(0x5be0cd19137e2179);
sha512->buffLen = 0;
sha512->loLen = 0;
sha512->hiLen = 0;
#if defined(HAVE_INTEL_AVX1)|| defined(HAVE_INTEL_AVX2)
set_Transform() ; /* choose best Transform function under this runtime environment */
#endif
return 0 ;
}
static const word64 K512[80] = {
W64LIT(0x428a2f98d728ae22), W64LIT(0x7137449123ef65cd),
W64LIT(0xb5c0fbcfec4d3b2f), W64LIT(0xe9b5dba58189dbbc),
W64LIT(0x3956c25bf348b538), W64LIT(0x59f111f1b605d019),
W64LIT(0x923f82a4af194f9b), W64LIT(0xab1c5ed5da6d8118),
W64LIT(0xd807aa98a3030242), W64LIT(0x12835b0145706fbe),
W64LIT(0x243185be4ee4b28c), W64LIT(0x550c7dc3d5ffb4e2),
W64LIT(0x72be5d74f27b896f), W64LIT(0x80deb1fe3b1696b1),
W64LIT(0x9bdc06a725c71235), W64LIT(0xc19bf174cf692694),
W64LIT(0xe49b69c19ef14ad2), W64LIT(0xefbe4786384f25e3),
W64LIT(0x0fc19dc68b8cd5b5), W64LIT(0x240ca1cc77ac9c65),
W64LIT(0x2de92c6f592b0275), W64LIT(0x4a7484aa6ea6e483),
W64LIT(0x5cb0a9dcbd41fbd4), W64LIT(0x76f988da831153b5),
W64LIT(0x983e5152ee66dfab), W64LIT(0xa831c66d2db43210),
W64LIT(0xb00327c898fb213f), W64LIT(0xbf597fc7beef0ee4),
W64LIT(0xc6e00bf33da88fc2), W64LIT(0xd5a79147930aa725),
W64LIT(0x06ca6351e003826f), W64LIT(0x142929670a0e6e70),
W64LIT(0x27b70a8546d22ffc), W64LIT(0x2e1b21385c26c926),
W64LIT(0x4d2c6dfc5ac42aed), W64LIT(0x53380d139d95b3df),
W64LIT(0x650a73548baf63de), W64LIT(0x766a0abb3c77b2a8),
W64LIT(0x81c2c92e47edaee6), W64LIT(0x92722c851482353b),
W64LIT(0xa2bfe8a14cf10364), W64LIT(0xa81a664bbc423001),
W64LIT(0xc24b8b70d0f89791), W64LIT(0xc76c51a30654be30),
W64LIT(0xd192e819d6ef5218), W64LIT(0xd69906245565a910),
W64LIT(0xf40e35855771202a), W64LIT(0x106aa07032bbd1b8),
W64LIT(0x19a4c116b8d2d0c8), W64LIT(0x1e376c085141ab53),
W64LIT(0x2748774cdf8eeb99), W64LIT(0x34b0bcb5e19b48a8),
W64LIT(0x391c0cb3c5c95a63), W64LIT(0x4ed8aa4ae3418acb),
W64LIT(0x5b9cca4f7763e373), W64LIT(0x682e6ff3d6b2b8a3),
W64LIT(0x748f82ee5defb2fc), W64LIT(0x78a5636f43172f60),
W64LIT(0x84c87814a1f0ab72), W64LIT(0x8cc702081a6439ec),
W64LIT(0x90befffa23631e28), W64LIT(0xa4506cebde82bde9),
W64LIT(0xbef9a3f7b2c67915), W64LIT(0xc67178f2e372532b),
W64LIT(0xca273eceea26619c), W64LIT(0xd186b8c721c0c207),
W64LIT(0xeada7dd6cde0eb1e), W64LIT(0xf57d4f7fee6ed178),
W64LIT(0x06f067aa72176fba), W64LIT(0x0a637dc5a2c898a6),
W64LIT(0x113f9804bef90dae), W64LIT(0x1b710b35131c471b),
W64LIT(0x28db77f523047d84), W64LIT(0x32caab7b40c72493),
W64LIT(0x3c9ebe0a15c9bebc), W64LIT(0x431d67c49c100d4c),
W64LIT(0x4cc5d4becb3e42b6), W64LIT(0x597f299cfc657e2a),
W64LIT(0x5fcb6fab3ad6faec), W64LIT(0x6c44198c4a475817)
};
#define blk0(i) (W[i] = sha512->buffer[i])
#define blk2(i) (W[i&15]+=s1(W[(i-2)&15])+W[(i-7)&15]+s0(W[(i-15)&15]))
#define Ch(x,y,z) (z^(x&(y^z)))
#define Maj(x,y,z) ((x&y)|(z&(x|y)))
#define a(i) T[(0-i)&7]
#define b(i) T[(1-i)&7]
#define c(i) T[(2-i)&7]
#define d(i) T[(3-i)&7]
#define e(i) T[(4-i)&7]
#define f(i) T[(5-i)&7]
#define g(i) T[(6-i)&7]
#define h(i) T[(7-i)&7]
#define S0(x) (rotrFixed64(x,28)^rotrFixed64(x,34)^rotrFixed64(x,39))
#define S1(x) (rotrFixed64(x,14)^rotrFixed64(x,18)^rotrFixed64(x,41))
#define s0(x) (rotrFixed64(x,1)^rotrFixed64(x,8)^(x>>7))
#define s1(x) (rotrFixed64(x,19)^rotrFixed64(x,61)^(x>>6))
#define R(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk0(i));\
d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
#define blk384(i) (W[i] = sha384->buffer[i])
#define R2(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j]+(j?blk2(i):blk384(i));\
d(i)+=h(i);h(i)+=S0(a(i))+Maj(a(i),b(i),c(i))
static int _Transform(Sha512* sha512)
{
const word64* K = K512;
word32 j;
word64 T[8];
#ifdef WOLFSSL_SMALL_STACK
word64* W;
W = (word64*) XMALLOC(sizeof(word64) * 16, NULL, DYNAMIC_TYPE_TMP_BUFFER);
if (W == NULL)
return MEMORY_E;
#else
word64 W[16];
#endif
/* Copy digest to working vars */
XMEMCPY(T, sha512->digest, sizeof(T));
#ifdef USE_SLOW_SHA2
/* over twice as small, but 50% slower */
/* 80 operations, not unrolled */
for (j = 0; j < 80; j += 16) {
int m;
for (m = 0; m < 16; m++) { /* braces needed here for macros {} */
R(m);
}
}
#else
/* 80 operations, partially loop unrolled */
for (j = 0; j < 80; j += 16) {
R( 0); R( 1); R( 2); R( 3);
R( 4); R( 5); R( 6); R( 7);
R( 8); R( 9); R(10); R(11);
R(12); R(13); R(14); R(15);
}
#endif /* USE_SLOW_SHA2 */
/* Add the working vars back into digest */
sha512->digest[0] += a(0);
sha512->digest[1] += b(0);
sha512->digest[2] += c(0);
sha512->digest[3] += d(0);
sha512->digest[4] += e(0);
sha512->digest[5] += f(0);
sha512->digest[6] += g(0);
sha512->digest[7] += h(0);
/* Wipe variables */
ForceZero(W, sizeof(word64) * 16);
ForceZero(T, sizeof(T));
#ifdef WOLFSSL_SMALL_STACK
XFREE(W, NULL, DYNAMIC_TYPE_TMP_BUFFER);
#endif
return 0;
}
static INLINE void AddLength(Sha512* sha512, word32 len)
{
word32 tmp = sha512->loLen;
if ( (sha512->loLen += len) < tmp)
sha512->hiLen++; /* carry low to high */
}
int wc_Sha512Update(Sha512* sha512, const byte* data, word32 len)
{
/* do block size increments */
byte* local = (byte*)sha512->buffer;
SAVE_XMM_YMM ; /* for Intel AVX */
while (len) {
word32 add = min(len, SHA512_BLOCK_SIZE - sha512->buffLen);
XMEMCPY(&local[sha512->buffLen], data, add);
sha512->buffLen += add;
data += add;
len -= add;
if (sha512->buffLen == SHA512_BLOCK_SIZE) {
int ret;
#if defined(LITTLE_ENDIAN_ORDER)
#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
if(!IS_INTEL_AVX1 && !IS_INTEL_AVX2)
#endif
ByteReverseWords64(sha512->buffer, sha512->buffer,
SHA512_BLOCK_SIZE);
#endif
ret = Transform(sha512);
if (ret != 0)
return ret;
AddLength(sha512, SHA512_BLOCK_SIZE);
sha512->buffLen = 0;
}
}
return 0;
}
int wc_Sha512Final(Sha512* sha512, byte* hash)
{
byte* local = (byte*)sha512->buffer;
int ret;
SAVE_XMM_YMM ; /* for Intel AVX */
AddLength(sha512, sha512->buffLen); /* before adding pads */
local[sha512->buffLen++] = 0x80; /* add 1 */
/* pad with zeros */
if (sha512->buffLen > SHA512_PAD_SIZE) {
XMEMSET(&local[sha512->buffLen], 0, SHA512_BLOCK_SIZE -sha512->buffLen);
sha512->buffLen += SHA512_BLOCK_SIZE - sha512->buffLen;
#if defined(LITTLE_ENDIAN_ORDER)
#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
if(!IS_INTEL_AVX1 && !IS_INTEL_AVX2)
#endif
ByteReverseWords64(sha512->buffer,sha512->buffer,SHA512_BLOCK_SIZE);
#endif
ret = Transform(sha512);
if (ret != 0)
return ret;
sha512->buffLen = 0;
}
XMEMSET(&local[sha512->buffLen], 0, SHA512_PAD_SIZE - sha512->buffLen);
/* put lengths in bits */
sha512->hiLen = (sha512->loLen >> (8*sizeof(sha512->loLen) - 3)) +
(sha512->hiLen << 3);
sha512->loLen = sha512->loLen << 3;
/* store lengths */
#if defined(LITTLE_ENDIAN_ORDER)
#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
if(!IS_INTEL_AVX1 && !IS_INTEL_AVX2)
#endif
ByteReverseWords64(sha512->buffer, sha512->buffer, SHA512_PAD_SIZE);
#endif
/* ! length ordering dependent on digest endian type ! */
sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2] = sha512->hiLen;
sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 1] = sha512->loLen;
#if defined(HAVE_INTEL_AVX1) || defined(HAVE_INTEL_AVX2)
if(IS_INTEL_AVX1 || IS_INTEL_AVX2)
ByteReverseWords64(&(sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
&(sha512->buffer[SHA512_BLOCK_SIZE / sizeof(word64) - 2]),
SHA512_BLOCK_SIZE - SHA512_PAD_SIZE);
#endif
ret = Transform(sha512);
if (ret != 0)
return ret;
#ifdef LITTLE_ENDIAN_ORDER
ByteReverseWords64(sha512->digest, sha512->digest, SHA512_DIGEST_SIZE);
#endif
XMEMCPY(hash, sha512->digest, SHA512_DIGEST_SIZE);
return wc_InitSha512(sha512); /* reset state */
}
#if defined(HAVE_INTEL_AVX1)
#define Rx_1(i) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j] + W_X[i] ;
#define Rx_2(i) d(i)+=h(i);
#define Rx_3(i) h(i)+=S0(a(i))+Maj(a(i),b(i),c(i));
#if defined(HAVE_INTEL_RORX)
#define Rx_RORX_1(i) h(i)+=S1_RORX(e(i))+Ch(e(i),f(i),g(i))+K[i+j] + W_X[i] ;
#define Rx_RORX_2(i) d(i)+=h(i);
#define Rx_RORX_3(i) h(i)+=S0_RORX(a(i))+Maj(a(i),b(i),c(i));
#endif
#endif
#if defined(HAVE_INTEL_AVX2)
#define Ry_1(i, w) h(i)+=S1(e(i))+Ch(e(i),f(i),g(i))+K[i+j] + w ;
#define Ry_2(i, w) d(i)+=h(i);
#define Ry_3(i, w) h(i)+=S0(a(i))+Maj(a(i),b(i),c(i));
#endif
#if defined(HAVE_INTEL_AVX1) /* INLINE Assember for Intel AVX1 instructions */
#if defined(DEBUG_XMM)
#define SAVE_REG(i) __asm__ volatile("vmovdqu %%xmm"#i", %0 \n\t":"=m"(reg[i][0])::XMM_REGs);
#define RECV_REG(i) __asm__ volatile("vmovdqu %0, %%xmm"#i" \n\t"::"m"(reg[i][0]):XMM_REGs);
#define _DUMP_REG(REG, name)\
{ word64 buf[16] ;word64 reg[16][2];int k ;\
SAVE_REG(0); SAVE_REG(1); SAVE_REG(2); SAVE_REG(3); SAVE_REG(4); \
SAVE_REG(5); SAVE_REG(6); SAVE_REG(7);SAVE_REG(8); SAVE_REG(9); SAVE_REG(10);\
SAVE_REG(11); SAVE_REG(12); SAVE_REG(13); SAVE_REG(14); SAVE_REG(15); \
__asm__ volatile("vmovdqu %%"#REG", %0 \n\t":"=m"(buf[0])::XMM_REGs);\
printf(" "#name":\t") ; for(k=0; k<2; k++) printf("%016lx.", (word64)(buf[k])); printf("\n") ; \
RECV_REG(0); RECV_REG(1); RECV_REG(2); RECV_REG(3); RECV_REG(4);\
RECV_REG(5); RECV_REG(6); RECV_REG(7); RECV_REG(8); RECV_REG(9);\
RECV_REG(10); RECV_REG(11); RECV_REG(12); RECV_REG(13); RECV_REG(14); RECV_REG(15);\
}
#define DUMP_REG(REG) _DUMP_REG(REG, #REG)
#define PRINTF(fmt, ...)
#else
#define DUMP_REG(REG)
#define PRINTF(fmt, ...)
#endif
#define _MOVE_to_REG(xymm, mem) __asm__ volatile("vmovdqu %0, %%"#xymm" "\
:: "m"(mem):XMM_REGs) ;
#define _MOVE_to_MEM(mem,i, xymm) __asm__ volatile("vmovdqu %%"#xymm", %0" :\
"=m"(mem[i]),"=m"(mem[i+1]),"=m"(mem[i+2]),"=m"(mem[i+3])::XMM_REGs) ;
#define _MOVE(dest, src) __asm__ volatile("vmovdqu %%"#src", %%"\
#dest" ":::XMM_REGs) ;
#define _S_TEMP(dest, src, bits, temp) __asm__ volatile("vpsrlq $"#bits", %%"\
#src", %%"#dest"\n\tvpsllq $64-"#bits", %%"#src", %%"#temp"\n\tvpor %%"\
#temp",%%"#dest", %%"#dest" ":::XMM_REGs) ;
#define _AVX1_R(dest, src, bits) __asm__ volatile("vpsrlq $"#bits", %%"\
#src", %%"#dest" ":::XMM_REGs) ;
#define _XOR(dest, src1, src2) __asm__ volatile("vpxor %%"#src1", %%"\
#src2", %%"#dest" ":::XMM_REGs) ;
#define _OR(dest, src1, src2) __asm__ volatile("vpor %%"#src1", %%"\
#src2", %%"#dest" ":::XMM_REGs) ;
#define _ADD(dest, src1, src2) __asm__ volatile("vpaddq %%"#src1", %%"\
#src2", %%"#dest" ":::XMM_REGs) ;
#define _ADD_MEM(dest, src1, mem) __asm__ volatile("vpaddq %0, %%"#src1", %%"\
#dest" "::"m"(mem):XMM_REGs) ;
#define MOVE_to_REG(xymm, mem) _MOVE_to_REG(xymm, mem)
#define MOVE_to_MEM(mem, i, xymm) _MOVE_to_MEM(mem, i, xymm)
#define MOVE(dest, src) _MOVE(dest, src)
#define XOR(dest, src1, src2) _XOR(dest, src1, src2)
#define OR(dest, src1, src2) _OR(dest, src1, src2)
#define ADD(dest, src1, src2) _ADD(dest, src1, src2)
#define S_TMP(dest, src, bits, temp) _S_TEMP(dest, src, bits, temp);
#define AVX1_S(dest, src, bits) S_TMP(dest, src, bits, S_TEMP)
#define AVX1_R(dest, src, bits) _AVX1_R(dest, src, bits)
#define Init_Mask(mask) \
__asm__ volatile("vmovdqu %0, %%xmm1\n\t"::"m"(mask):"%xmm1") ;
#define _W_from_buff1(w, buff, xmm) \
/* X0..3(xmm4..7), W[0..15] = sha512->buffer[0.15]; */\
__asm__ volatile("vmovdqu %1, %%"#xmm"\n\t"\
"vpshufb %%xmm1, %%"#xmm", %%"#xmm"\n\t"\
"vmovdqu %%"#xmm", %0"\
:"=m"(w): "m"(buff):"%xmm0") ;
#define W_from_buff1(w, buff, xmm) _W_from_buff1(w, buff, xmm)
#define W_from_buff(w, buff)\
Init_Mask(mBYTE_FLIP_MASK[0]) ;\
W_from_buff1(w[0], buff[0], W_0);\
W_from_buff1(w[2], buff[2], W_2);\
W_from_buff1(w[4], buff[4], W_4);\
W_from_buff1(w[6], buff[6], W_6);\
W_from_buff1(w[8], buff[8], W_8);\
W_from_buff1(w[10],buff[10],W_10);\
W_from_buff1(w[12],buff[12],W_12);\
W_from_buff1(w[14],buff[14],W_14);
static word64 mBYTE_FLIP_MASK[] = { 0x0001020304050607, 0x08090a0b0c0d0e0f } ;
#define W_I_15 xmm14
#define W_I_7 xmm11
#define W_I_2 xmm13
#define W_I xmm12
#define G_TEMP xmm0
#define S_TEMP xmm1
#define XMM_TEMP0 xmm2
#define W_0 xmm12
#define W_2 xmm3
#define W_4 xmm4
#define W_6 xmm5
#define W_8 xmm6
#define W_10 xmm7
#define W_12 xmm8
#define W_14 xmm9
#define XMM_REGs
#define s0_1(dest, src) AVX1_S(dest, src, 1);
#define s0_2(dest, src) AVX1_S(G_TEMP, src, 8); XOR(dest, G_TEMP, dest) ;
#define s0_3(dest, src) AVX1_R(G_TEMP, src, 7); XOR(dest, G_TEMP, dest) ;
#define s1_1(dest, src) AVX1_S(dest, src, 19);
#define s1_2(dest, src) AVX1_S(G_TEMP, src, 61); XOR(dest, G_TEMP, dest) ;
#define s1_3(dest, src) AVX1_R(G_TEMP, src, 6); XOR(dest, G_TEMP, dest) ;
#define s0_(dest, src) s0_1(dest, src) ; s0_2(dest, src) ; s0_3(dest, src)
#define s1_(dest, src) s1_1(dest, src) ; s1_2(dest, src) ; s1_3(dest, src)
#define Block_xx_1(i) \
MOVE_to_REG(W_I_15, W_X[(i-15)&15]) ;\
MOVE_to_REG(W_I_7, W_X[(i- 7)&15]) ;\
#define Block_xx_2(i) \
MOVE_to_REG(W_I_2, W_X[(i- 2)&15]) ;\
MOVE_to_REG(W_I, W_X[(i)]) ;\
#define Block_xx_3(i) \
s0_ (XMM_TEMP0, W_I_15) ;\
#define Block_xx_4(i) \
ADD(W_I, W_I, XMM_TEMP0) ;\
ADD(W_I, W_I, W_I_7) ;\
#define Block_xx_5(i) \
s1_ (XMM_TEMP0, W_I_2) ;\
#define Block_xx_6(i) \
ADD(W_I, W_I, XMM_TEMP0) ;\
MOVE_to_MEM(W_X,i, W_I) ;\
if(i==0)\
MOVE_to_MEM(W_X,16, W_I) ;\
#define Block_xx_7(i) \
MOVE_to_REG(W_I_15, W_X[(i-15)&15]) ;\
MOVE_to_REG(W_I_7, W_X[(i- 7)&15]) ;\
#define Block_xx_8(i) \
MOVE_to_REG(W_I_2, W_X[(i- 2)&15]) ;\
MOVE_to_REG(W_I, W_X[(i)]) ;\
#define Block_xx_9(i) \
s0_ (XMM_TEMP0, W_I_15) ;\
#define Block_xx_10(i) \
ADD(W_I, W_I, XMM_TEMP0) ;\
ADD(W_I, W_I, W_I_7) ;\
#define Block_xx_11(i) \
s1_ (XMM_TEMP0, W_I_2) ;\
#define Block_xx_12(i) \
ADD(W_I, W_I, XMM_TEMP0) ;\
MOVE_to_MEM(W_X,i, W_I) ;\
if((i)==0)\
MOVE_to_MEM(W_X,16, W_I) ;\
static INLINE void Block_0_1(word64 *W_X) { Block_xx_1(0) ; }
static INLINE void Block_0_2(word64 *W_X) { Block_xx_2(0) ; }
static INLINE void Block_0_3(void) { Block_xx_3(0) ; }
static INLINE void Block_0_4(void) { Block_xx_4(0) ; }
static INLINE void Block_0_5(void) { Block_xx_5(0) ; }
static INLINE void Block_0_6(word64 *W_X) { Block_xx_6(0) ; }
static INLINE void Block_0_7(word64 *W_X) { Block_xx_7(2) ; }
static INLINE void Block_0_8(word64 *W_X) { Block_xx_8(2) ; }
static INLINE void Block_0_9(void) { Block_xx_9(2) ; }
static INLINE void Block_0_10(void){ Block_xx_10(2) ; }
static INLINE void Block_0_11(void){ Block_xx_11(2) ; }
static INLINE void Block_0_12(word64 *W_X){ Block_xx_12(2) ; }
static INLINE void Block_4_1(word64 *W_X) { Block_xx_1(4) ; }
static INLINE void Block_4_2(word64 *W_X) { Block_xx_2(4) ; }
static INLINE void Block_4_3(void) { Block_xx_3(4) ; }
static INLINE void Block_4_4(void) { Block_xx_4(4) ; }
static INLINE void Block_4_5(void) { Block_xx_5(4) ; }
static INLINE void Block_4_6(word64 *W_X) { Block_xx_6(4) ; }
static INLINE void Block_4_7(word64 *W_X) { Block_xx_7(6) ; }
static INLINE void Block_4_8(word64 *W_X) { Block_xx_8(6) ; }
static INLINE void Block_4_9(void) { Block_xx_9(6) ; }
static INLINE void Block_4_10(void){ Block_xx_10(6) ; }
static INLINE void Block_4_11(void){ Block_xx_11(6) ; }
static INLINE void Block_4_12(word64 *W_X){ Block_xx_12(6) ; }
static INLINE void Block_8_1(word64 *W_X) { Block_xx_1(8) ; }
static INLINE void Block_8_2(word64 *W_X) { Block_xx_2(8) ; }
static INLINE void Block_8_3(void) { Block_xx_3(8) ; }
static INLINE void Block_8_4(void) { Block_xx_4(8) ; }
static INLINE void Block_8_5(void) { Block_xx_5(8) ; }
static INLINE void Block_8_6(word64 *W_X) { Block_xx_6(8) ; }
static INLINE void Block_8_7(word64 *W_X) { Block_xx_7(10) ; }
static INLINE void Block_8_8(word64 *W_X) { Block_xx_8(10) ; }
static INLINE void Block_8_9(void) { Block_xx_9(10) ; }
static INLINE void Block_8_10(void){ Block_xx_10(10) ; }
static INLINE void Block_8_11(void){ Block_xx_11(10) ; }
static INLINE void Block_8_12(word64 *W_X){ Block_xx_12(10) ; }
static INLINE void Block_12_1(word64 *W_X) { Block_xx_1(12) ; }
static INLINE void Block_12_2(word64 *W_X) { Block_xx_2(12) ; }
static INLINE void Block_12_3(void) { Block_xx_3(12) ; }
static INLINE void Block_12_4(void) { Block_xx_4(12) ; }
static INLINE void Block_12_5(void) { Block_xx_5(12) ; }
static INLINE void Block_12_6(word64 *W_X) { Block_xx_6(12) ; }
static INLINE void Block_12_7(word64 *W_X) { Block_xx_7(14) ; }
static INLINE void Block_12_8(word64 *W_X) { Block_xx_8(14) ; }
static INLINE void Block_12_9(void) { Block_xx_9(14) ; }
static INLINE void Block_12_10(void){ Block_xx_10(14) ; }
static INLINE void Block_12_11(void){ Block_xx_11(14) ; }
static INLINE void Block_12_12(word64 *W_X){ Block_xx_12(14) ; }
#endif
#if defined(HAVE_INTEL_AVX2)
static const unsigned long mBYTE_FLIP_MASK_Y[] =
{ 0x0001020304050607, 0x08090a0b0c0d0e0f, 0x0001020304050607, 0x08090a0b0c0d0e0f } ;
#define W_from_buff_Y(buff)\
{ /* X0..3(ymm9..12), W_X[0..15] = sha512->buffer[0.15]; */\
__asm__ volatile("vmovdqu %0, %%ymm8\n\t"::"m"(mBYTE_FLIP_MASK_Y[0]):YMM_REGs) ;\
__asm__ volatile("vmovdqu %0, %%ymm12\n\t"\
"vmovdqu %1, %%ymm4\n\t"\
"vpshufb %%ymm8, %%ymm12, %%ymm12\n\t"\
"vpshufb %%ymm8, %%ymm4, %%ymm4\n\t"\
:: "m"(buff[0]), "m"(buff[4]):YMM_REGs) ;\
__asm__ volatile("vmovdqu %0, %%ymm5\n\t"\
"vmovdqu %1, %%ymm6\n\t"\
"vpshufb %%ymm8, %%ymm5, %%ymm5\n\t"\
"vpshufb %%ymm8, %%ymm6, %%ymm6\n\t"\
:: "m"(buff[8]), "m"(buff[12]):YMM_REGs) ;\
}
#if defined(DEBUG_YMM)
#define SAVE_REG_Y(i) __asm__ volatile("vmovdqu %%ymm"#i", %0 \n\t":"=m"(reg[i-4][0])::YMM_REGs);
#define RECV_REG_Y(i) __asm__ volatile("vmovdqu %0, %%ymm"#i" \n\t"::"m"(reg[i-4][0]):YMM_REGs);
#define _DUMP_REG_Y(REG, name)\
{ word64 buf[16] ;word64 reg[16][2];int k ;\
SAVE_REG_Y(4); SAVE_REG_Y(5); SAVE_REG_Y(6); SAVE_REG_Y(7); \
SAVE_REG_Y(8); SAVE_REG_Y(9); SAVE_REG_Y(10); SAVE_REG_Y(11); SAVE_REG_Y(12);\
SAVE_REG_Y(13); SAVE_REG_Y(14); SAVE_REG_Y(15); \
__asm__ volatile("vmovdqu %%"#REG", %0 \n\t":"=m"(buf[0])::YMM_REGs);\
printf(" "#name":\t") ; for(k=0; k<4; k++) printf("%016lx.", (word64)buf[k]) ; printf("\n") ; \
RECV_REG_Y(4); RECV_REG_Y(5); RECV_REG_Y(6); RECV_REG_Y(7); \
RECV_REG_Y(8); RECV_REG_Y(9); RECV_REG_Y(10); RECV_REG_Y(11); RECV_REG_Y(12); \
RECV_REG_Y(13); RECV_REG_Y(14); RECV_REG_Y(15);\
}
#define DUMP_REG_Y(REG) _DUMP_REG_Y(REG, #REG)
#define DUMP_REG2_Y(REG) _DUMP_REG_Y(REG, #REG)
#define PRINTF_Y(fmt, ...)
#else
#define DUMP_REG_Y(REG)
#define DUMP_REG2_Y(REG)
#define PRINTF_Y(fmt, ...)
#endif
#define _MOVE_to_REGy(ymm, mem) __asm__ volatile("vmovdqu %0, %%"#ymm" "\
:: "m"(mem):YMM_REGs) ;
#define _MOVE_to_MEMy(mem,i, ymm) __asm__ volatile("vmovdqu %%"#ymm", %0" \
: "=m"(mem[i]),"=m"(mem[i+1]),"=m"(mem[i+2]),"=m"(mem[i+3])::YMM_REGs) ;
#define _MOVE_128y(ymm0, ymm1, ymm2, map) __asm__ volatile("vperm2i128 $"\
#map", %%"#ymm2", %%"#ymm1", %%"#ymm0" ":::YMM_REGs) ;
#define _S_TEMPy(dest, src, bits, temp) \
__asm__ volatile("vpsrlq $"#bits", %%"#src", %%"#dest"\n\tvpsllq $64-"#bits\
", %%"#src", %%"#temp"\n\tvpor %%"#temp",%%"#dest", %%"#dest" ":::YMM_REGs) ;
#define _AVX2_R(dest, src, bits) __asm__ volatile("vpsrlq $"#bits", %%"\
#src", %%"#dest" ":::YMM_REGs) ;
#define _XORy(dest, src1, src2) __asm__ volatile("vpxor %%"#src1", %%"\
#src2", %%"#dest" ":::YMM_REGs) ;
#define _ADDy(dest, src1, src2) __asm__ volatile("vpaddq %%"#src1", %%"\
#src2", %%"#dest" ":::YMM_REGs) ;
#define _BLENDy(map, dest, src1, src2) __asm__ volatile("vpblendd $"#map", %%"\
#src1", %%"#src2", %%"#dest" ":::YMM_REGs) ;
#define _BLENDQy(map, dest, src1, src2) __asm__ volatile("vblendpd $"#map", %%"\
#src1", %%"#src2", %%"#dest" ":::YMM_REGs) ;
#define _PERMQy(map, dest, src) __asm__ volatile("vpermq $"#map", %%"\
#src", %%"#dest" ":::YMM_REGs) ;
#define MOVE_to_REGy(ymm, mem) _MOVE_to_REGy(ymm, mem)
#define MOVE_to_MEMy(mem, i, ymm) _MOVE_to_MEMy(mem, i, ymm)
#define MOVE_128y(ymm0, ymm1, ymm2, map) _MOVE_128y(ymm0, ymm1, ymm2, map)
#define XORy(dest, src1, src2) _XORy(dest, src1, src2)
#define ADDy(dest, src1, src2) _ADDy(dest, src1, src2)
#define BLENDy(map, dest, src1, src2) _BLENDy(map, dest, src1, src2)
#define BLENDQy(map, dest, src1, src2) _BLENDQy(map, dest, src1, src2)
#define PERMQy(map, dest, src) _PERMQy(map, dest, src)
#define S_TMPy(dest, src, bits, temp) _S_TEMPy(dest, src, bits, temp);
#define AVX2_S(dest, src, bits) S_TMPy(dest, src, bits, S_TEMPy)
#define AVX2_R(dest, src, bits) _AVX2_R(dest, src, bits)
#define FEEDBACK1_to_W_I_2(w_i_2, w_i) MOVE_128y(YMM_TEMP0, w_i, w_i, 0x08) ;\
BLENDy(0xf0, w_i_2, YMM_TEMP0, w_i_2) ;
#define MOVE_W_to_W_I_15(w_i_15, w_0, w_4) BLENDQy(0x1, w_i_15, w_4, w_0) ;\
PERMQy(0x39, w_i_15, w_i_15) ;
#define MOVE_W_to_W_I_7(w_i_7, w_8, w_12) BLENDQy(0x1, w_i_7, w_12, w_8) ;\
PERMQy(0x39, w_i_7, w_i_7) ;
#define MOVE_W_to_W_I_2(w_i_2, w_12) BLENDQy(0xc, w_i_2, w_12, w_i_2) ;\
PERMQy(0x0e, w_i_2, w_i_2) ;
#define W_I_16y ymm8
#define W_I_15y ymm9
#define W_I_7y ymm10
#define W_I_2y ymm11
#define W_Iy ymm12
#define G_TEMPy ymm13
#define S_TEMPy ymm14
#define YMM_TEMP0 ymm15
#define YMM_TEMP0x xmm15
#define W_I_TEMPy ymm7
#define W_K_TEMPy ymm15
#define W_K_TEMPx xmm15
#define W_0y ymm12
#define W_4y ymm4
#define W_8y ymm5
#define W_12y ymm6
#define YMM_REGs
/* Registers are saved in Sha512Update/Final */
/* "%ymm7","%ymm8","%ymm9","%ymm10","%ymm11","%ymm12","%ymm13","%ymm14","%ymm15"*/
#define MOVE_15_to_16(w_i_16, w_i_15, w_i_7)\
__asm__ volatile("vperm2i128 $0x01, %%"#w_i_15", %%"#w_i_15", %%"#w_i_15" ":::YMM_REGs) ;\
__asm__ volatile("vpblendd $0x08, %%"#w_i_15", %%"#w_i_7", %%"#w_i_16" ":::YMM_REGs) ;\
__asm__ volatile("vperm2i128 $0x01, %%"#w_i_7", %%"#w_i_7", %%"#w_i_15" ":::YMM_REGs) ;\
__asm__ volatile("vpblendd $0x80, %%"#w_i_15", %%"#w_i_16", %%"#w_i_16" ":::YMM_REGs) ;\
__asm__ volatile("vpshufd $0x93, %%"#w_i_16", %%"#w_i_16" ":::YMM_REGs) ;\
#define MOVE_7_to_15(w_i_15, w_i_7)\
__asm__ volatile("vmovdqu %%"#w_i_7", %%"#w_i_15" ":::YMM_REGs) ;\
#define MOVE_I_to_7(w_i_7, w_i)\
__asm__ volatile("vperm2i128 $0x01, %%"#w_i", %%"#w_i", %%"#w_i_7" ":::YMM_REGs) ;\
__asm__ volatile("vpblendd $0x01, %%"#w_i_7", %%"#w_i", %%"#w_i_7" ":::YMM_REGs) ;\
__asm__ volatile("vpshufd $0x39, %%"#w_i_7", %%"#w_i_7" ":::YMM_REGs) ;\
#define MOVE_I_to_2(w_i_2, w_i)\
__asm__ volatile("vperm2i128 $0x01, %%"#w_i", %%"#w_i", %%"#w_i_2" ":::YMM_REGs) ;\
__asm__ volatile("vpshufd $0x0e, %%"#w_i_2", %%"#w_i_2" ":::YMM_REGs) ;\
#endif
/*** Transform Body ***/
#if defined(HAVE_INTEL_AVX1)
static int Transform_AVX1(Sha512* sha512)
{
const word64* K = K512;
word64 W_X[16+4];
word32 j;
word64 T[8];
/* Copy digest to working vars */
XMEMCPY(T, sha512->digest, sizeof(T));
W_from_buff(W_X, sha512->buffer) ;
for (j = 0; j < 80; j += 16) {
Rx_1( 0); Block_0_1(W_X); Rx_2( 0); Block_0_2(W_X); Rx_3( 0); Block_0_3();
Rx_1( 1); Block_0_4(); Rx_2( 1); Block_0_5(); Rx_3( 1); Block_0_6(W_X);
Rx_1( 2); Block_0_7(W_X); Rx_2( 2); Block_0_8(W_X); Rx_3( 2); Block_0_9();