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
|
// Copyright 2016 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Hash code using AES intrinsics.
#include "runtime.h"
uintptr aeshashbody(void*, uintptr, uintptr, Slice)
__asm__(GOSYM_PREFIX "runtime.aeshashbody");
uintptr aeshashbody(void*, uintptr, uintptr, Slice)
__attribute__((no_split_stack));
#if (defined(__i386__) || defined(__x86_64__)) && defined(HAVE_AS_X86_AES)
#include <emmintrin.h>
#include <tmmintrin.h>
#include <wmmintrin.h>
// Force appropriate CPU level. We won't call here unless the CPU
// supports it.
#pragma GCC target("ssse3", "aes")
#ifdef __x86_64__
// aeshashbody implements a hash function using AES instructions
// available in recent x86 processors. Note this is not encryption,
// just hashing.
//
// This is written to produce exactly the same results as the gc
// implementation, not because that matters, but just to ensure that
// this does something reasonable.
uintptr aeshashbody(void* p, uintptr seed, uintptr size, Slice aeskeysched) {
__m128i mseed, mseed2, mseed3, mseed4, mseed5, mseed6, mseed7, mseed8;
__m128i mval, mval2, mval3, mval4, mval5, mval6, mval7, mval8;
// Start with hash seed.
mseed = _mm_cvtsi64_si128(seed);
// Get 16 bits of length.
mseed = _mm_insert_epi16(mseed, size, 4);
// Repeat length 4 times total.
mseed = _mm_shufflehi_epi16(mseed, 0);
// Save unscrambled seed.
mseed2 = mseed;
// XOR in per-process seed.
mseed ^= _mm_loadu_si128(aeskeysched.__values);
// Scramble seed.
mseed = _mm_aesenc_si128(mseed, mseed);
if (size <= 16) {
if (size == 0) {
// Return scrambled input seed.
return _mm_cvtsi128_si64(_mm_aesenc_si128(mseed, mseed));
} else if (size < 16) {
if ((((uintptr)(p) + 16) & 0xff0) != 0) {
static const uint64 masks[32]
__attribute__ ((aligned(16))) =
{
0x0000000000000000, 0x0000000000000000,
0x00000000000000ff, 0x0000000000000000,
0x000000000000ffff, 0x0000000000000000,
0x0000000000ffffff, 0x0000000000000000,
0x00000000ffffffff, 0x0000000000000000,
0x000000ffffffffff, 0x0000000000000000,
0x0000ffffffffffff, 0x0000000000000000,
0x00ffffffffffffff, 0x0000000000000000,
0xffffffffffffffff, 0x0000000000000000,
0xffffffffffffffff, 0x00000000000000ff,
0xffffffffffffffff, 0x000000000000ffff,
0xffffffffffffffff, 0x0000000000ffffff,
0xffffffffffffffff, 0x00000000ffffffff,
0xffffffffffffffff, 0x000000ffffffffff,
0xffffffffffffffff, 0x0000ffffffffffff,
0xffffffffffffffff, 0x00ffffffffffffff
};
// 16 bytes loaded at p won't cross a page
// boundary, so we can load directly.
mval = _mm_loadu_si128(p);
mval &= *(const __m128i*)(&masks[size*2]);
} else {
static const uint64 shifts[32]
__attribute__ ((aligned(16))) =
{
0x0000000000000000, 0x0000000000000000,
0xffffffffffffff0f, 0xffffffffffffffff,
0xffffffffffff0f0e, 0xffffffffffffffff,
0xffffffffff0f0e0d, 0xffffffffffffffff,
0xffffffff0f0e0d0c, 0xffffffffffffffff,
0xffffff0f0e0d0c0b, 0xffffffffffffffff,
0xffff0f0e0d0c0b0a, 0xffffffffffffffff,
0xff0f0e0d0c0b0a09, 0xffffffffffffffff,
0x0f0e0d0c0b0a0908, 0xffffffffffffffff,
0x0e0d0c0b0a090807, 0xffffffffffffff0f,
0x0d0c0b0a09080706, 0xffffffffffff0f0e,
0x0c0b0a0908070605, 0xffffffffff0f0e0d,
0x0b0a090807060504, 0xffffffff0f0e0d0c,
0x0a09080706050403, 0xffffff0f0e0d0c0b,
0x0908070605040302, 0xffff0f0e0d0c0b0a,
0x0807060504030201, 0xff0f0e0d0c0b0a09,
};
// address ends in 1111xxxx. Might be
// up against a page boundary, so load
// ending at last byte. Then shift
// bytes down using pshufb.
mval = _mm_loadu_si128((void*)((char*)p - 16 + size));
mval = _mm_shuffle_epi8(mval, *(const __m128i*)(&shifts[size*2]));
}
} else {
mval = _mm_loadu_si128(p);
}
// XOR data with seed.
mval ^= mseed;
// Scramble combo 3 times.
mval = _mm_aesenc_si128(mval, mval);
mval = _mm_aesenc_si128(mval, mval);
mval = _mm_aesenc_si128(mval, mval);
return _mm_cvtsi128_si64(mval);
} else if (size <= 32) {
// Make second starting seed.
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
// Load data to be hashed.
mval = _mm_loadu_si128(p);
mval2 = _mm_loadu_si128((void*)((char*)p + size - 16));
// XOR with seed.
mval ^= mseed;
mval2 ^= mseed2;
// Scramble 3 times.
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
// Combine results.
mval ^= mval2;
return _mm_cvtsi128_si64(mval);
} else if (size <= 64) {
// Make 3 more starting seeds.
mseed3 = mseed2;
mseed4 = mseed2;
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed3 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 32));
mseed4 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 48));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
mseed3 = _mm_aesenc_si128(mseed3, mseed3);
mseed4 = _mm_aesenc_si128(mseed4, mseed4);
mval = _mm_loadu_si128(p);
mval2 = _mm_loadu_si128((void*)((char*)p + 16));
mval3 = _mm_loadu_si128((void*)((char*)p + size - 32));
mval4 = _mm_loadu_si128((void*)((char*)p + size - 16));
mval ^= mseed;
mval2 ^= mseed2;
mval3 ^= mseed3;
mval4 ^= mseed4;
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval ^= mval3;
mval2 ^= mval4;
mval ^= mval2;
return _mm_cvtsi128_si64(mval);
} else if (size <= 128) {
// Make 7 more starting seeds.
mseed3 = mseed2;
mseed4 = mseed2;
mseed5 = mseed2;
mseed6 = mseed2;
mseed7 = mseed2;
mseed8 = mseed2;
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed3 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 32));
mseed4 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 48));
mseed5 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 64));
mseed6 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 80));
mseed7 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 96));
mseed8 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 112));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
mseed3 = _mm_aesenc_si128(mseed3, mseed3);
mseed4 = _mm_aesenc_si128(mseed4, mseed4);
mseed5 = _mm_aesenc_si128(mseed5, mseed5);
mseed6 = _mm_aesenc_si128(mseed6, mseed6);
mseed7 = _mm_aesenc_si128(mseed7, mseed7);
mseed8 = _mm_aesenc_si128(mseed8, mseed8);
// Load data.
mval = _mm_loadu_si128(p);
mval2 = _mm_loadu_si128((void*)((char*)p + 16));
mval3 = _mm_loadu_si128((void*)((char*)p + 32));
mval4 = _mm_loadu_si128((void*)((char*)p + 48));
mval5 = _mm_loadu_si128((void*)((char*)p + size - 64));
mval6 = _mm_loadu_si128((void*)((char*)p + size - 48));
mval7 = _mm_loadu_si128((void*)((char*)p + size - 32));
mval8 = _mm_loadu_si128((void*)((char*)p + size - 16));
// XOR with seed.
mval ^= mseed;
mval2 ^= mseed2;
mval3 ^= mseed3;
mval4 ^= mseed4;
mval5 ^= mseed5;
mval6 ^= mseed6;
mval7 ^= mseed7;
mval8 ^= mseed8;
// Scramble 3 times.
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
// Combine results.
mval ^= mval5;
mval2 ^= mval6;
mval3 ^= mval7;
mval4 ^= mval8;
mval ^= mval3;
mval2 ^= mval4;
mval ^= mval2;
return _mm_cvtsi128_si64(mval);
} else {
// Make 7 more starting seeds.
mseed3 = mseed2;
mseed4 = mseed2;
mseed5 = mseed2;
mseed6 = mseed2;
mseed7 = mseed2;
mseed8 = mseed2;
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed3 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 32));
mseed4 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 48));
mseed5 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 64));
mseed6 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 80));
mseed7 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 96));
mseed8 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 112));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
mseed3 = _mm_aesenc_si128(mseed3, mseed3);
mseed4 = _mm_aesenc_si128(mseed4, mseed4);
mseed5 = _mm_aesenc_si128(mseed5, mseed5);
mseed6 = _mm_aesenc_si128(mseed6, mseed6);
mseed7 = _mm_aesenc_si128(mseed7, mseed7);
mseed8 = _mm_aesenc_si128(mseed8, mseed8);
// Start with last (possibly overlapping) block.
mval = _mm_loadu_si128((void*)((char*)p + size - 128));
mval2 = _mm_loadu_si128((void*)((char*)p + size - 112));
mval3 = _mm_loadu_si128((void*)((char*)p + size - 96));
mval4 = _mm_loadu_si128((void*)((char*)p + size - 80));
mval5 = _mm_loadu_si128((void*)((char*)p + size - 64));
mval6 = _mm_loadu_si128((void*)((char*)p + size - 48));
mval7 = _mm_loadu_si128((void*)((char*)p + size - 32));
mval8 = _mm_loadu_si128((void*)((char*)p + size - 16));
// XOR in seed.
mval ^= mseed;
mval2 ^= mseed2;
mval3 ^= mseed3;
mval4 ^= mseed4;
mval5 ^= mseed5;
mval6 ^= mseed6;
mval7 ^= mseed7;
mval8 ^= mseed8;
// Compute number of remaining 128-byte blocks.
size--;
size >>= 7;
do {
// Scramble state.
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
// Scramble state, XOR in a block.
mval = _mm_aesenc_si128(mval, _mm_loadu_si128(p));
mval2 = _mm_aesenc_si128(mval2, _mm_loadu_si128((void*)((char*)p + 16)));
mval3 = _mm_aesenc_si128(mval3, _mm_loadu_si128((void*)((char*)p + 32)));
mval4 = _mm_aesenc_si128(mval4, _mm_loadu_si128((void*)((char*)p + 48)));
mval5 = _mm_aesenc_si128(mval5, _mm_loadu_si128((void*)((char*)p + 64)));
mval6 = _mm_aesenc_si128(mval6, _mm_loadu_si128((void*)((char*)p + 80)));
mval7 = _mm_aesenc_si128(mval7, _mm_loadu_si128((void*)((char*)p + 96)));
mval8 = _mm_aesenc_si128(mval8, _mm_loadu_si128((void*)((char*)p + 112)));
p = (void*)((char*)p + 128);
} while (--size > 0);
// 3 more scrambles to finish.
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval5 = _mm_aesenc_si128(mval5, mval5);
mval6 = _mm_aesenc_si128(mval6, mval6);
mval7 = _mm_aesenc_si128(mval7, mval7);
mval8 = _mm_aesenc_si128(mval8, mval8);
mval ^= mval5;
mval2 ^= mval6;
mval3 ^= mval7;
mval4 ^= mval8;
mval ^= mval3;
mval2 ^= mval4;
mval ^= mval2;
return _mm_cvtsi128_si64(mval);
}
}
#else // !defined(__x86_64__)
// The 32-bit version of aeshashbody.
uintptr aeshashbody(void* p, uintptr seed, uintptr size, Slice aeskeysched) {
__m128i mseed, mseed2, mseed3, mseed4;
__m128i mval, mval2, mval3, mval4;
// Start with hash seed.
mseed = _mm_cvtsi32_si128(seed);
// Get 16 bits of length.
mseed = _mm_insert_epi16(mseed, size, 4);
// Replace size with its low 2 bytes repeated 4 times.
mseed = _mm_shufflehi_epi16(mseed, 0);
// Save unscrambled seed.
mseed2 = mseed;
// XOR in per-process seed.
mseed ^= _mm_loadu_si128(aeskeysched.__values);
// Scramble seed.
mseed = _mm_aesenc_si128(mseed, mseed);
if (size <= 16) {
if (size == 0) {
// Return scrambled input seed.
return _mm_cvtsi128_si32(_mm_aesenc_si128(mseed, mseed));
} else if (size < 16) {
if ((((uintptr)(p) + 16) & 0xff0) != 0) {
static const uint64 masks[32]
__attribute__ ((aligned(16))) =
{
0x0000000000000000, 0x0000000000000000,
0x00000000000000ff, 0x0000000000000000,
0x000000000000ffff, 0x0000000000000000,
0x0000000000ffffff, 0x0000000000000000,
0x00000000ffffffff, 0x0000000000000000,
0x000000ffffffffff, 0x0000000000000000,
0x0000ffffffffffff, 0x0000000000000000,
0x00ffffffffffffff, 0x0000000000000000,
0xffffffffffffffff, 0x0000000000000000,
0xffffffffffffffff, 0x00000000000000ff,
0xffffffffffffffff, 0x000000000000ffff,
0xffffffffffffffff, 0x0000000000ffffff,
0xffffffffffffffff, 0x00000000ffffffff,
0xffffffffffffffff, 0x000000ffffffffff,
0xffffffffffffffff, 0x0000ffffffffffff,
0xffffffffffffffff, 0x00ffffffffffffff
};
// 16 bytes loaded at p won't cross a page
// boundary, so we can load it directly.
mval = _mm_loadu_si128(p);
mval &= *(const __m128i*)(&masks[size*2]);
} else {
static const uint64 shifts[32]
__attribute__ ((aligned(16))) =
{
0x0000000000000000, 0x0000000000000000,
0xffffffffffffff0f, 0xffffffffffffffff,
0xffffffffffff0f0e, 0xffffffffffffffff,
0xffffffffff0f0e0d, 0xffffffffffffffff,
0xffffffff0f0e0d0c, 0xffffffffffffffff,
0xffffff0f0e0d0c0b, 0xffffffffffffffff,
0xffff0f0e0d0c0b0a, 0xffffffffffffffff,
0xff0f0e0d0c0b0a09, 0xffffffffffffffff,
0x0f0e0d0c0b0a0908, 0xffffffffffffffff,
0x0e0d0c0b0a090807, 0xffffffffffffff0f,
0x0d0c0b0a09080706, 0xffffffffffff0f0e,
0x0c0b0a0908070605, 0xffffffffff0f0e0d,
0x0b0a090807060504, 0xffffffff0f0e0d0c,
0x0a09080706050403, 0xffffff0f0e0d0c0b,
0x0908070605040302, 0xffff0f0e0d0c0b0a,
0x0807060504030201, 0xff0f0e0d0c0b0a09,
};
// address ends in 1111xxxx. Might be
// up against a page boundary, so load
// ending at last byte. Then shift
// bytes down using pshufb.
mval = _mm_loadu_si128((void*)((char*)p - 16 + size));
mval = _mm_shuffle_epi8(mval, *(const __m128i*)(&shifts[size*2]));
}
} else {
mval = _mm_loadu_si128(p);
}
// Scramble input, XOR in seed.
mval = _mm_aesenc_si128(mval, mseed);
mval = _mm_aesenc_si128(mval, mval);
mval = _mm_aesenc_si128(mval, mval);
return _mm_cvtsi128_si32(mval);
} else if (size <= 32) {
// Make second starting seed.
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
// Load data to be hashed.
mval = _mm_loadu_si128(p);
mval2 = _mm_loadu_si128((void*)((char*)p + size - 16));
// Scramble 3 times.
mval = _mm_aesenc_si128(mval, mseed);
mval2 = _mm_aesenc_si128(mval2, mseed2);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
// Combine results.
mval ^= mval2;
return _mm_cvtsi128_si32(mval);
} else if (size <= 64) {
// Make 3 more starting seeds.
mseed3 = mseed2;
mseed4 = mseed2;
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed3 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 32));
mseed4 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 48));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
mseed3 = _mm_aesenc_si128(mseed3, mseed3);
mseed4 = _mm_aesenc_si128(mseed4, mseed4);
mval = _mm_loadu_si128(p);
mval2 = _mm_loadu_si128((void*)((char*)p + 16));
mval3 = _mm_loadu_si128((void*)((char*)p + size - 32));
mval4 = _mm_loadu_si128((void*)((char*)p + size - 16));
mval = _mm_aesenc_si128(mval, mseed);
mval2 = _mm_aesenc_si128(mval2, mseed2);
mval3 = _mm_aesenc_si128(mval3, mseed3);
mval4 = _mm_aesenc_si128(mval4, mseed4);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval ^= mval3;
mval2 ^= mval4;
mval ^= mval2;
return _mm_cvtsi128_si32(mval);
} else {
// Make 3 more starting seeds.
mseed3 = mseed2;
mseed4 = mseed2;
mseed2 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 16));
mseed3 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 32));
mseed4 ^= _mm_loadu_si128((void*)((char*)aeskeysched.__values + 48));
mseed2 = _mm_aesenc_si128(mseed2, mseed2);
mseed3 = _mm_aesenc_si128(mseed3, mseed3);
mseed4 = _mm_aesenc_si128(mseed4, mseed4);
// Start with last (possibly overlapping) block.
mval = _mm_loadu_si128((void*)((char*)p + size - 64));
mval2 = _mm_loadu_si128((void*)((char*)p + size - 48));
mval3 = _mm_loadu_si128((void*)((char*)p + size - 32));
mval4 = _mm_loadu_si128((void*)((char*)p + size - 16));
// Scramble state once.
mval = _mm_aesenc_si128(mval, mseed);
mval2 = _mm_aesenc_si128(mval2, mseed2);
mval3 = _mm_aesenc_si128(mval3, mseed3);
mval4 = _mm_aesenc_si128(mval4, mseed4);
// Compute number of remaining 64-byte blocks.
size--;
size >>= 6;
do {
// Scramble state, XOR in a block.
mval = _mm_aesenc_si128(mval, _mm_loadu_si128(p));
mval2 = _mm_aesenc_si128(mval2, _mm_loadu_si128((void*)((char*)p + 16)));
mval3 = _mm_aesenc_si128(mval3, _mm_loadu_si128((void*)((char*)p + 32)));
mval4 = _mm_aesenc_si128(mval4, _mm_loadu_si128((void*)((char*)p + 48)));
// Scramble state.
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
p = (void*)((char*)p + 64);
} while (--size > 0);
// 2 more scrambles to finish.
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval = _mm_aesenc_si128(mval, mval);
mval2 = _mm_aesenc_si128(mval2, mval2);
mval3 = _mm_aesenc_si128(mval3, mval3);
mval4 = _mm_aesenc_si128(mval4, mval4);
mval ^= mval3;
mval2 ^= mval4;
mval ^= mval2;
return _mm_cvtsi128_si32(mval);
}
}
#endif // !defined(__x86_64__)
#elif defined(__aarch64__)
// Undefine some identifiers that we pick up from the Go runtime package that
// are used in arm_neon.h.
#undef t1
#undef tx
#undef t2
#undef t3
#undef t4
#undef t5
#include <arm_neon.h>
// Force appropriate CPU level. We won't call here unless the CPU
// supports it.
#pragma GCC target("+crypto")
// The arm64 version of aeshashbody.
uintptr aeshashbody(void* p, uintptr seed, uintptr size, Slice aeskeysched) {
uint8x16_t *pseed;
uint64x2_t vinit64;
uint8x16_t vinit;
uint8x16_t vseed, vseed2, vseed3, vseed4;
uint8x16_t vseed5, vseed6, vseed7, vseed8;
uint8x16_t vval, vval2, vval3, vval4;
uint8x16_t vval5, vval6, vval7, vval8;
uint8x16_t vvalLoop, vvalLoop2, vvalLoop3, vvalLoop4;
uint8x16_t vvalLoop5, vvalLoop6, vvalLoop7, vvalLoop8;
uint8x16x2_t avval2;
uint8x16x3_t avseed3;
pseed = (uint8x16_t*)(aeskeysched.__values);
// Combined hash seed and length.
vinit64 = vdupq_n_u64(0);
vinit64[0] = (uint64)seed;
vinit64[1] = (uint64)size;
vinit = vreinterpretq_u8_u64(vinit64);
// Mix in per-process seed.
vseed = vaeseq_u8(*pseed, vinit);
++pseed;
// Scramble seed.
vseed = vaesmcq_u8(vseed);
if (size <= 16) {
if (size == 0) {
// Return 64 bits of scrambled input seed.
return vreinterpretq_u64_u8(vseed)[0];
} else if (size < 16) {
vval = vreinterpretq_u8_u64(vdupq_n_u64(0));
if ((size & 8) != 0) {
vval = vreinterpretq_u8_u64(vld1q_lane_u64((uint64_t*)(p), vreinterpretq_u64_u8(vval), 0));
p = (void*)((uint64_t*)(p) + 1);
}
if ((size & 4) != 0) {
vval = vreinterpretq_u8_u32(vld1q_lane_u32((uint32_t*)(p), vreinterpretq_u32_u8(vval), 2));
p = (void*)((uint32_t*)(p) + 1);
}
if ((size & 2) != 0) {
vval = vreinterpretq_u8_u16(vld1q_lane_u16((uint16_t*)(p), vreinterpretq_u16_u8(vval), 6));
p = (void*)((uint16_t*)(p) + 1);
}
if ((size & 1) != 0) {
vval = vld1q_lane_u8((uint8*)(p), vval, 14);
}
} else {
vval = *(uint8x16_t*)(p);
}
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval = vaeseq_u8(vval, vseed);
return vreinterpretq_u64_u8(vval)[0];
} else if (size <= 32) {
// Make a second seed.
vseed2 = vaeseq_u8(*pseed, vinit);
vseed2 = vaesmcq_u8(vseed2);
vval = *(uint8x16_t*)(p);
vval2 = *(uint8x16_t*)((char*)(p) + (size - 16));
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vseed2);
vval2 = vaesmcq_u8(vval2);
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vseed2);
vval2 = vaesmcq_u8(vval2);
vval = vaeseq_u8(vval, vseed);
vval2 = vaeseq_u8(vval2, vseed2);
vval ^= vval2;
return vreinterpretq_u64_u8(vval)[0];
} else if (size <= 64) {
avseed3 = vld1q_u8_x3((uint8*)(pseed));
vseed2 = avseed3.val[0];
vseed3 = avseed3.val[1];
vseed4 = avseed3.val[2];
vseed2 = vaeseq_u8(vseed2, vinit);
vseed2 = vaesmcq_u8(vseed2);
vseed3 = vaeseq_u8(vseed3, vinit);
vseed3 = vaesmcq_u8(vseed3);
vseed4 = vaeseq_u8(vseed4, vinit);
vseed4 = vaesmcq_u8(vseed4);
avval2 = vld1q_u8_x2((uint8*)(p));
vval = avval2.val[0];
vval2 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 32));
vval3 = avval2.val[0];
vval4 = avval2.val[1];
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vseed2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vseed3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vseed4);
vval4 = vaesmcq_u8(vval4);
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vseed2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vseed3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vseed4);
vval4 = vaesmcq_u8(vval4);
vval = vaeseq_u8(vval, vseed);
vval2 = vaeseq_u8(vval2, vseed2);
vval3 = vaeseq_u8(vval3, vseed3);
vval4 = vaeseq_u8(vval4, vseed4);
vval ^= vval3;
vval2 ^= vval4;
vval ^= vval2;
return vreinterpretq_u64_u8(vval)[0];
} else if (size <= 128) {
// For some reason vld1q_u8_x4 is missing.
avseed3 = vld1q_u8_x3((uint8*)(pseed));
vseed2 = avseed3.val[0];
vseed3 = avseed3.val[1];
vseed4 = avseed3.val[2];
avseed3 = vld1q_u8_x3((uint8*)(pseed + 3));
vseed5 = avseed3.val[0];
vseed6 = avseed3.val[1];
vseed7 = avseed3.val[2];
vseed8 = *(pseed + 6);
vseed2 = vaeseq_u8(vseed2, vinit);
vseed2 = vaesmcq_u8(vseed2);
vseed3 = vaeseq_u8(vseed3, vinit);
vseed3 = vaesmcq_u8(vseed3);
vseed4 = vaeseq_u8(vseed4, vinit);
vseed4 = vaesmcq_u8(vseed4);
vseed5 = vaeseq_u8(vseed5, vinit);
vseed5 = vaesmcq_u8(vseed5);
vseed6 = vaeseq_u8(vseed6, vinit);
vseed6 = vaesmcq_u8(vseed6);
vseed7 = vaeseq_u8(vseed7, vinit);
vseed7 = vaesmcq_u8(vseed7);
vseed8 = vaeseq_u8(vseed8, vinit);
vseed8 = vaesmcq_u8(vseed8);
avval2 = vld1q_u8_x2((uint8*)(p));
vval = avval2.val[0];
vval2 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + 32);
vval3 = avval2.val[0];
vval4 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 64));
vval5 = avval2.val[0];
vval6 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 32));
vval7 = avval2.val[0];
vval8 = avval2.val[1];
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vseed2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vseed3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vseed4);
vval4 = vaesmcq_u8(vval4);
vval5 = vaeseq_u8(vval5, vseed5);
vval5 = vaesmcq_u8(vval5);
vval6 = vaeseq_u8(vval6, vseed6);
vval6 = vaesmcq_u8(vval6);
vval7 = vaeseq_u8(vval7, vseed7);
vval7 = vaesmcq_u8(vval7);
vval8 = vaeseq_u8(vval8, vseed8);
vval8 = vaesmcq_u8(vval8);
vval = vaeseq_u8(vval, vseed);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vseed2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vseed3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vseed4);
vval4 = vaesmcq_u8(vval4);
vval5 = vaeseq_u8(vval5, vseed5);
vval5 = vaesmcq_u8(vval5);
vval6 = vaeseq_u8(vval6, vseed6);
vval6 = vaesmcq_u8(vval6);
vval7 = vaeseq_u8(vval7, vseed7);
vval7 = vaesmcq_u8(vval7);
vval8 = vaeseq_u8(vval8, vseed8);
vval8 = vaesmcq_u8(vval8);
vval = vaeseq_u8(vval, vseed);
vval2 = vaeseq_u8(vval2, vseed2);
vval3 = vaeseq_u8(vval3, vseed3);
vval4 = vaeseq_u8(vval4, vseed4);
vval5 = vaeseq_u8(vval5, vseed5);
vval6 = vaeseq_u8(vval6, vseed6);
vval7 = vaeseq_u8(vval7, vseed7);
vval8 = vaeseq_u8(vval8, vseed8);
vval ^= vval5;
vval2 ^= vval6;
vval3 ^= vval7;
vval4 ^= vval8;
vval ^= vval3;
vval2 ^= vval4;
vval ^= vval2;
return vreinterpretq_u64_u8(vval)[0];
} else {
// For some reason vld1q_u8_x4 is missing.
avseed3 = vld1q_u8_x3((uint8*)(pseed));
vseed2 = avseed3.val[0];
vseed3 = avseed3.val[1];
vseed4 = avseed3.val[2];
avseed3 = vld1q_u8_x3((uint8*)(pseed + 3));
vseed5 = avseed3.val[0];
vseed6 = avseed3.val[1];
vseed7 = avseed3.val[2];
vseed8 = *(pseed + 6);
vseed2 = vaeseq_u8(vseed2, vinit);
vseed2 = vaesmcq_u8(vseed2);
vseed3 = vaeseq_u8(vseed3, vinit);
vseed3 = vaesmcq_u8(vseed3);
vseed4 = vaeseq_u8(vseed4, vinit);
vseed4 = vaesmcq_u8(vseed4);
vseed5 = vaeseq_u8(vseed5, vinit);
vseed5 = vaesmcq_u8(vseed5);
vseed6 = vaeseq_u8(vseed6, vinit);
vseed6 = vaesmcq_u8(vseed6);
vseed7 = vaeseq_u8(vseed7, vinit);
vseed7 = vaesmcq_u8(vseed7);
vseed8 = vaeseq_u8(vseed8, vinit);
vseed8 = vaesmcq_u8(vseed8);
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 128));
vval = avval2.val[0];
vval2 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 96));
vval3 = avval2.val[0];
vval4 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 64));
vval5 = avval2.val[0];
vval6 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + (size - 32));
vval7 = avval2.val[0];
vval8 = avval2.val[1];
vvalLoop = vseed;
vvalLoop2 = vseed2;
vvalLoop3 = vseed3;
vvalLoop4 = vseed4;
vvalLoop5 = vseed5;
vvalLoop6 = vseed6;
vvalLoop7 = vseed7;
vvalLoop8 = vseed8;
size--;
size >>= 7;
do {
vval = vaeseq_u8(vval, vvalLoop);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vvalLoop2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vvalLoop3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vvalLoop4);
vval4 = vaesmcq_u8(vval4);
vval5 = vaeseq_u8(vval5, vvalLoop5);
vval5 = vaesmcq_u8(vval5);
vval6 = vaeseq_u8(vval6, vvalLoop6);
vval6 = vaesmcq_u8(vval6);
vval7 = vaeseq_u8(vval7, vvalLoop7);
vval7 = vaesmcq_u8(vval7);
vval8 = vaeseq_u8(vval8, vvalLoop8);
vval8 = vaesmcq_u8(vval8);
avval2 = vld1q_u8_x2((uint8*)(p));
vvalLoop = avval2.val[0];
vvalLoop2 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + 32);
vvalLoop3 = avval2.val[0];
vvalLoop4 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + 64);
vvalLoop5 = avval2.val[0];
vvalLoop6 = avval2.val[1];
avval2 = vld1q_u8_x2((uint8*)(p) + 96);
vvalLoop7 = avval2.val[0];
vvalLoop8 = avval2.val[1];
p = (void *)((uint8*)(p) + 128);
vval = vaeseq_u8(vval, vvalLoop);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vvalLoop2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vvalLoop3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vvalLoop4);
vval4 = vaesmcq_u8(vval4);
vval5 = vaeseq_u8(vval5, vvalLoop5);
vval5 = vaesmcq_u8(vval5);
vval6 = vaeseq_u8(vval6, vvalLoop6);
vval6 = vaesmcq_u8(vval6);
vval7 = vaeseq_u8(vval7, vvalLoop7);
vval7 = vaesmcq_u8(vval7);
vval8 = vaeseq_u8(vval8, vvalLoop8);
vval8 = vaesmcq_u8(vval8);
} while (--size > 0);
vval = vaeseq_u8(vval, vvalLoop);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vvalLoop2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vvalLoop3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vvalLoop4);
vval4 = vaesmcq_u8(vval4);
vval5 = vaeseq_u8(vval5, vvalLoop5);
vval5 = vaesmcq_u8(vval5);
vval6 = vaeseq_u8(vval6, vvalLoop6);
vval6 = vaesmcq_u8(vval6);
vval7 = vaeseq_u8(vval7, vvalLoop7);
vval7 = vaesmcq_u8(vval7);
vval8 = vaeseq_u8(vval8, vvalLoop8);
vval8 = vaesmcq_u8(vval8);
vval = vaeseq_u8(vval, vvalLoop);
vval = vaesmcq_u8(vval);
vval2 = vaeseq_u8(vval2, vvalLoop2);
vval2 = vaesmcq_u8(vval2);
vval3 = vaeseq_u8(vval3, vvalLoop3);
vval3 = vaesmcq_u8(vval3);
vval4 = vaeseq_u8(vval4, vvalLoop4);
vval4 = vaesmcq_u8(vval4);
vval5 = vaeseq_u8(vval5, vvalLoop5);
vval5 = vaesmcq_u8(vval5);
vval6 = vaeseq_u8(vval6, vvalLoop6);
vval6 = vaesmcq_u8(vval6);
vval7 = vaeseq_u8(vval7, vvalLoop7);
vval7 = vaesmcq_u8(vval7);
vval8 = vaeseq_u8(vval8, vvalLoop8);
vval8 = vaesmcq_u8(vval8);
vval = vaeseq_u8(vval, vvalLoop);
vval2 = vaeseq_u8(vval2, vvalLoop2);
vval3 = vaeseq_u8(vval3, vvalLoop3);
vval4 = vaeseq_u8(vval4, vvalLoop4);
vval5 = vaeseq_u8(vval5, vvalLoop5);
vval6 = vaeseq_u8(vval6, vvalLoop6);
vval7 = vaeseq_u8(vval7, vvalLoop7);
vval8 = vaeseq_u8(vval8, vvalLoop8);
vval ^= vval5;
vval2 ^= vval6;
vval3 ^= vval7;
vval4 ^= vval8;
vval ^= vval3;
vval2 ^= vval4;
vval ^= vval2;
return vreinterpretq_u64_u8(vval)[0];
}
}
#else // (!defined(__i386__) && !defined(__x86_64__) || !defined(HAVE_AS_X86_AES)) && !defined(__aarch64__)
uintptr aeshashbody(void* p __attribute__((unused)),
uintptr seed __attribute__((unused)),
uintptr size __attribute__((unused)),
Slice aeskeysched __attribute__((unused))) {
// We should never get here on a non-x86, non-arm64 system.
runtime_throw("impossible call to aeshashbody");
}
#endif // !defined(__i386__) && !defined(__x86_64__) || !defined(HAVE_AS_X86_AES)
|