-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathfusion.c
2249 lines (2008 loc) · 108 KB
/
fusion.c
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
/*
* This source file is licensed under the Apache License 2.0 *and* the MIT
* License. Please agree to *both* of the licensing terms!
*
*
* `transformH` function is a derivative work of OpenSSL. The original work
* is covered by the following license:
*
* Copyright 2013-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*
*
* All other work, including modifications to the `transformH` function is
* covered by the following MIT license:
*
* Copyright (c) 2020-2022 Fastly, Kazuho Oku
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <immintrin.h>
#include <tmmintrin.h>
#include <nmmintrin.h>
#include <wmmintrin.h>
#include "picotls.h"
#include "picotls/fusion.h"
#if defined(__clang__)
#if __has_feature(address_sanitizer)
#define NO_SANITIZE_ADDRESS __attribute__((no_sanitize("address")))
#endif
#elif __SANITIZE_ADDRESS__ /* gcc */
#define NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
#endif
#ifndef NO_SANITIZE_ADDRESS
#define NO_SANITIZE_ADDRESS
#endif
#ifdef _WINDOWS
#define aligned_alloc(a, s) _aligned_malloc((s), (a))
#define aligned_free(p) _aligned_free(p)
#else
#define aligned_free(p) free(p)
#endif
struct ptls_fusion_aesgcm_context {
ptls_fusion_aesecb_context_t ecb;
size_t capacity;
size_t ghash_cnt;
};
struct ptls_fusion_aesgcm_context128 {
struct ptls_fusion_aesgcm_context super;
struct ptls_fusion_aesgcm_ghash_precompute128 {
__m128i H;
__m128i r;
} ghash[0];
};
struct ptls_fusion_aesgcm_context256 {
struct ptls_fusion_aesgcm_context super;
union ptls_fusion_aesgcm_ghash_precompute256 {
struct {
__m128i H[2];
__m128i r[2];
};
struct {
__m256i Hx2;
__m256i rx2;
};
} ghash[0];
};
struct ctr_context {
ptls_cipher_context_t super;
ptls_fusion_aesecb_context_t fusion;
__m128i bits;
uint8_t is_ready;
};
struct aesgcm_context {
ptls_aead_context_t super;
ptls_fusion_aesgcm_context_t *aesgcm;
/**
* retains the static IV in the upper 96 bits (in little endian)
*/
__m128i static_iv;
};
static const uint64_t poly_[2] __attribute__((aligned(16))) = {1, 0xc200000000000000};
#define poly (*(__m128i *)poly_)
static const uint8_t byteswap_[32] __attribute__((aligned(32))) = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,
15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
#define byteswap128 (*(__m128i *)byteswap_)
#define byteswap256 (*(__m256i *)byteswap_)
static const uint8_t one_[16] __attribute__((aligned(16))) = {1};
#define one8 (*(__m128i *)one_)
static const uint8_t incr128x2_[32] __attribute__((aligned(32))) = {2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2};
#define incr128x2 (*(__m256i *)incr128x2_)
/* This function is covered by the Apache License and the MIT License. The origin is crypto/modes/asm/ghash-x86_64.pl of openssl
* at commit 33388b4. */
static __m128i transformH(__m128i H)
{
// # <<1 twist
// pshufd \$0b11111111,$Hkey,$T2 # broadcast uppermost dword
__m128i t2 = _mm_shuffle_epi32(H, 0xff);
// movdqa $Hkey,$T1
__m128i t1 = H;
// psllq \$1,$Hkey
H = _mm_slli_epi64(H, 1);
// pxor $T3,$T3 #
__m128i t3 = _mm_setzero_si128();
// psrlq \$63,$T1
t1 = _mm_srli_epi64(t1, 63);
// pcmpgtd $T2,$T3 # broadcast carry bit
t3 = _mm_cmplt_epi32(t2, t3);
// pslldq \$8,$T1
t1 = _mm_slli_si128(t1, 8);
// por $T1,$Hkey # H<<=1
H = _mm_or_si128(t1, H);
// # magic reduction
// pand .L0x1c2_polynomial(%rip),$T3
t3 = _mm_and_si128(t3, poly);
// pxor $T3,$Hkey # if(carry) H^=0x1c2_polynomial
H = _mm_xor_si128(t3, H);
return H;
}
// end of Apache License code
static __m128i gfmul(__m128i x, __m128i y)
{
__m128i lo = _mm_clmulepi64_si128(x, y, 0x00);
__m128i hi = _mm_clmulepi64_si128(x, y, 0x11);
__m128i a = _mm_shuffle_epi32(x, 78);
__m128i b = _mm_shuffle_epi32(y, 78);
a = _mm_xor_si128(a, x);
b = _mm_xor_si128(b, y);
a = _mm_clmulepi64_si128(a, b, 0x00);
a = _mm_xor_si128(a, lo);
a = _mm_xor_si128(a, hi);
b = _mm_slli_si128(a, 8);
a = _mm_srli_si128(a, 8);
lo = _mm_xor_si128(lo, b);
hi = _mm_xor_si128(hi, a);
// from https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf
__m128i t = _mm_clmulepi64_si128(lo, poly, 0x10);
lo = _mm_shuffle_epi32(lo, 78);
lo = _mm_xor_si128(lo, t);
t = _mm_clmulepi64_si128(lo, poly, 0x10);
lo = _mm_shuffle_epi32(lo, 78);
lo = _mm_xor_si128(lo, t);
return _mm_xor_si128(hi, lo);
}
static inline __m128i gfmul_do_reduce(__m128i hi, __m128i lo, __m128i mid)
{
mid = _mm_xor_si128(mid, hi);
mid = _mm_xor_si128(mid, lo);
lo = _mm_xor_si128(lo, _mm_slli_si128(mid, 8));
hi = _mm_xor_si128(hi, _mm_srli_si128(mid, 8));
/* fast reduction, using https://crypto.stanford.edu/RealWorldCrypto/slides/gueron.pdf */
__m128i r = _mm_clmulepi64_si128(lo, poly, 0x10);
lo = _mm_shuffle_epi32(lo, 78);
lo = _mm_xor_si128(lo, r);
r = _mm_clmulepi64_si128(lo, poly, 0x10);
lo = _mm_shuffle_epi32(lo, 78);
lo = _mm_xor_si128(lo, r);
lo = _mm_xor_si128(hi, lo);
return lo;
}
struct ptls_fusion_gfmul_state128 {
__m128i hi, lo, mid;
};
#if defined(__GNUC__) && !defined(__clang__)
static inline __m128i xor128(__m128i x, __m128i y)
{
__m128i ret;
__asm__("vpxor %2, %1, %0" : "=x"(ret) : "x"(x), "xm"(y));
return ret;
}
#else
#define xor128 _mm_xor_si128
#endif
static inline void gfmul_do_step128(struct ptls_fusion_gfmul_state128 *gstate, __m128i X,
struct ptls_fusion_aesgcm_ghash_precompute128 *precompute)
{
__m128i t1 = _mm_clmulepi64_si128(precompute->H, X, 0x00);
__m128i t2 = _mm_clmulepi64_si128(precompute->H, X, 0x11);
__m128i t3 = _mm_shuffle_epi32(X, 78);
t3 = _mm_xor_si128(t3, X);
t3 = _mm_clmulepi64_si128(precompute->r, t3, 0x00);
gstate->lo = xor128(gstate->lo, t1);
gstate->hi = xor128(gstate->hi, t2);
gstate->mid = xor128(gstate->mid, t3);
}
#undef xor128
static inline void gfmul_firststep128(struct ptls_fusion_gfmul_state128 *gstate, __m128i X,
struct ptls_fusion_aesgcm_ghash_precompute128 *precompute)
{
X = _mm_shuffle_epi8(X, byteswap128);
X = _mm_xor_si128(gstate->lo, X);
gstate->lo = _mm_setzero_si128();
gstate->hi = _mm_setzero_si128();
gstate->mid = _mm_setzero_si128();
gfmul_do_step128(gstate, X, precompute);
}
static inline void gfmul_nextstep128(struct ptls_fusion_gfmul_state128 *gstate, __m128i X,
struct ptls_fusion_aesgcm_ghash_precompute128 *precompute)
{
X = _mm_shuffle_epi8(X, byteswap128);
gfmul_do_step128(gstate, X, precompute);
}
static inline void gfmul_reduce128(struct ptls_fusion_gfmul_state128 *gstate)
{
gstate->lo = gfmul_do_reduce(gstate->hi, gstate->lo, gstate->mid);
}
static inline __m128i gfmul_get_tag128(struct ptls_fusion_gfmul_state128 *gstate, __m128i ek0)
{
__m128i tag = _mm_shuffle_epi8(gstate->lo, byteswap128);
tag = _mm_xor_si128(tag, ek0);
return tag;
}
struct ptls_fusion_gfmul_state256 {
__m256i hi, lo, mid;
};
static inline void gfmul_do_step256(struct ptls_fusion_gfmul_state256 *gstate, __m256i X,
union ptls_fusion_aesgcm_ghash_precompute256 *precompute)
{
__m256i t = _mm256_clmulepi64_epi128(precompute->Hx2, X, 0x00);
gstate->lo = _mm256_xor_si256(gstate->lo, t);
t = _mm256_clmulepi64_epi128(precompute->Hx2, X, 0x11);
gstate->hi = _mm256_xor_si256(gstate->hi, t);
t = _mm256_shuffle_epi32(X, 78);
t = _mm256_xor_si256(t, X);
t = _mm256_clmulepi64_epi128(precompute->rx2, t, 0x00);
gstate->mid = _mm256_xor_si256(gstate->mid, t);
}
static inline void gfmul_firststep256(struct ptls_fusion_gfmul_state256 *gstate, __m256i X, int half,
union ptls_fusion_aesgcm_ghash_precompute256 *precompute)
{
X = _mm256_shuffle_epi8(X, byteswap256);
X = _mm256_xor_si256(gstate->lo, X);
if (half)
X = _mm256_permute2f128_si256(X, X, 0x08);
gstate->lo = _mm256_setzero_si256();
gstate->hi = _mm256_setzero_si256();
gstate->mid = _mm256_setzero_si256();
gfmul_do_step256(gstate, X, precompute);
}
static inline void gfmul_nextstep256(struct ptls_fusion_gfmul_state256 *gstate, __m256i X,
union ptls_fusion_aesgcm_ghash_precompute256 *precompute)
{
X = _mm256_shuffle_epi8(X, byteswap256);
gfmul_do_step256(gstate, X, precompute);
}
static inline void gfmul_reduce256(struct ptls_fusion_gfmul_state256 *gstate)
{
#define XOR_256TO128(y) _mm_xor_si128(_mm256_castsi256_si128(y), _mm256_extractf128_si256((y), 1))
__m128i hi = XOR_256TO128(gstate->hi);
__m128i lo = XOR_256TO128(gstate->lo);
__m128i mid = XOR_256TO128(gstate->mid);
#undef XOR_256TO128
lo = gfmul_do_reduce(hi, lo, mid);
gstate->lo = _mm256_castsi128_si256(lo);
}
static inline __m128i gfmul_get_tag256(struct ptls_fusion_gfmul_state256 *gstate, __m128i ek0)
{
__m128i tag = _mm_shuffle_epi8(_mm256_castsi256_si128(gstate->lo), byteswap128);
tag = _mm_xor_si128(tag, ek0);
return tag;
}
static inline __m128i aesecb_encrypt(ptls_fusion_aesecb_context_t *ctx, __m128i v)
{
#define ROUNDKEY(i) (ctx->aesni256 ? _mm256_castsi256_si128(ctx->keys.m256[i]) : ctx->keys.m128[i])
v = _mm_xor_si128(v, ROUNDKEY(0));
for (size_t i = 1; i < ctx->rounds; ++i)
v = _mm_aesenc_si128(v, ROUNDKEY(i));
v = _mm_aesenclast_si128(v, ROUNDKEY(ctx->rounds));
return v;
#undef ROUNDKEY
}
// 32-bytes of 0xff followed by 31-bytes of 0x00
static const uint8_t loadn_mask[63] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
static const uint8_t loadn_shuffle[31] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, // first 16 bytes map to byte offsets
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80}; // latter 15 bytes map to zero
NO_SANITIZE_ADDRESS
static inline __m128i loadn_end_of_page(const void *p, size_t l)
{
uintptr_t shift = (uintptr_t)p & 15;
__m128i pattern = _mm_loadu_si128((const __m128i *)(loadn_shuffle + shift));
return _mm_shuffle_epi8(_mm_load_si128((const __m128i *)((uintptr_t)p - shift)), pattern);
}
NO_SANITIZE_ADDRESS
static inline __m128i loadn128(const void *p, size_t l)
{
__m128i v, mask = _mm_loadu_si128((__m128i *)(loadn_mask + 32 - l));
uintptr_t mod4k = (uintptr_t)p % 4096;
if (PTLS_LIKELY(mod4k <= 4096 - 16) || mod4k + l > 4096) {
v = _mm_loadu_si128(p);
} else {
v = loadn_end_of_page(p, l);
}
v = _mm_and_si128(v, mask);
return v;
}
NO_SANITIZE_ADDRESS
static inline __m256i loadn256(const void *p, size_t l)
{
__m256i v, mask = _mm256_loadu_si256((__m256i *)(loadn_mask + 32 - l));
uintptr_t mod4k = (uintptr_t)p % 4096;
if (PTLS_LIKELY(mod4k < 4096 - 32) || mod4k + l > 4096) {
v = _mm256_loadu_si256(p);
} else if (l > 16) {
__m128i first16 = _mm_loadu_si128(p), second16 = loadn128((uint8_t *)p + 16, l - 16);
v = _mm256_permute2f128_si256(_mm256_castsi128_si256(first16), _mm256_castsi128_si256(second16), 0x20);
} else if (l == 16) {
v = _mm256_castsi128_si256(_mm_loadu_si128(p));
} else {
v = _mm256_castsi128_si256(loadn_end_of_page(p, l));
}
v = _mm256_and_si256(v, mask);
return v;
}
static inline void storen128(void *_p, size_t l, __m128i v)
{
uint8_t buf[16], *p = _p;
*(__m128i *)buf = v;
for (size_t i = 0; i != l; ++i)
p[i] = buf[i];
}
void ptls_fusion_aesgcm_encrypt(ptls_fusion_aesgcm_context_t *_ctx, void *output, const void *input, size_t inlen, __m128i ctr,
const void *_aad, size_t aadlen, ptls_aead_supplementary_encryption_t *supp)
{
/* init the bits (we can always run in full), but use the last slot for calculating ek0, if possible */
#define AESECB6_INIT() \
do { \
ctr = _mm_add_epi64(ctr, one8); \
bits0 = _mm_shuffle_epi8(ctr, byteswap128); \
ctr = _mm_add_epi64(ctr, one8); \
bits1 = _mm_shuffle_epi8(ctr, byteswap128); \
ctr = _mm_add_epi64(ctr, one8); \
bits2 = _mm_shuffle_epi8(ctr, byteswap128); \
ctr = _mm_add_epi64(ctr, one8); \
bits3 = _mm_shuffle_epi8(ctr, byteswap128); \
ctr = _mm_add_epi64(ctr, one8); \
bits4 = _mm_shuffle_epi8(ctr, byteswap128); \
if (PTLS_LIKELY(srclen > 16 * 5)) { \
ctr = _mm_add_epi64(ctr, one8); \
bits5 = _mm_shuffle_epi8(ctr, byteswap128); \
} else { \
if ((state & STATE_EK0_BEEN_FED) == 0) { \
bits5 = ek0; \
state |= STATE_EK0_BEEN_FED; \
} \
if ((state & STATE_SUPP_USED) != 0 && srclen <= 16 * 4 && (const __m128i *)supp->input + 1 <= dst_ghash) { \
bits4 = _mm_loadu_si128(supp->input); \
bits4keys = ((struct ctr_context *)supp->ctx)->fusion.keys.m128; \
state |= STATE_SUPP_IN_PROCESS; \
} \
} \
__m128i k = ctx->super.ecb.keys.m128[0]; \
bits0 = _mm_xor_si128(bits0, k); \
bits1 = _mm_xor_si128(bits1, k); \
bits2 = _mm_xor_si128(bits2, k); \
bits3 = _mm_xor_si128(bits3, k); \
bits4 = _mm_xor_si128(bits4, bits4keys[0]); \
bits5 = _mm_xor_si128(bits5, k); \
} while (0)
/* aes block update */
#define AESECB6_UPDATE(i) \
do { \
__m128i k = ctx->super.ecb.keys.m128[i]; \
bits0 = _mm_aesenc_si128(bits0, k); \
bits1 = _mm_aesenc_si128(bits1, k); \
bits2 = _mm_aesenc_si128(bits2, k); \
bits3 = _mm_aesenc_si128(bits3, k); \
bits4 = _mm_aesenc_si128(bits4, bits4keys[i]); \
bits5 = _mm_aesenc_si128(bits5, k); \
} while (0)
/* aesenclast */
#define AESECB6_FINAL(i) \
do { \
__m128i k = ctx->super.ecb.keys.m128[i]; \
bits0 = _mm_aesenclast_si128(bits0, k); \
bits1 = _mm_aesenclast_si128(bits1, k); \
bits2 = _mm_aesenclast_si128(bits2, k); \
bits3 = _mm_aesenclast_si128(bits3, k); \
bits4 = _mm_aesenclast_si128(bits4, bits4keys[i]); \
bits5 = _mm_aesenclast_si128(bits5, k); \
} while (0)
struct ptls_fusion_aesgcm_context128 *ctx = (void *)_ctx;
__m128i ek0, bits0, bits1, bits2, bits3, bits4, bits5 = _mm_setzero_si128();
const __m128i *bits4keys = ctx->super.ecb.keys.m128; /* is changed to supp->ctx.keys when calcurating suppout */
struct ptls_fusion_gfmul_state128 gstate = {0};
__m128i gdatabuf[6];
__m128i ac = _mm_shuffle_epi8(_mm_set_epi32(0, (int)aadlen * 8, 0, (int)inlen * 8), byteswap128);
// src and dst are updated after the chunk is processed
const __m128i *src = input;
__m128i *dst = output;
size_t srclen = inlen;
// aad and src_ghash are updated before the chunk is processed (i.e., when the pointers are fed indo the processor)
const __m128i *aad = _aad, *dst_ghash = dst;
size_t dst_ghashlen = srclen;
struct ptls_fusion_aesgcm_ghash_precompute128 *ghash_precompute = ctx->ghash + (aadlen + 15) / 16 + (srclen + 15) / 16 + 1;
#define STATE_EK0_BEEN_FED 0x3
#define STATE_EK0_INCOMPLETE 0x2
#define STATE_EK0_READY() ((state & STATE_EK0_BEEN_FED) == 0x1)
#define STATE_SUPP_USED 0x4
#define STATE_SUPP_IN_PROCESS 0x8
int32_t state = supp != NULL ? STATE_SUPP_USED : 0;
/* build counter */
ctr = _mm_insert_epi32(ctr, 1, 0);
ek0 = _mm_shuffle_epi8(ctr, byteswap128);
/* start preparing AES */
AESECB6_INIT();
AESECB6_UPDATE(1);
/* build first ghash data (only AAD can be fed at this point, as this would be calculated alongside the first AES block) */
const __m128i *gdata = gdatabuf; // points to the elements fed into GHASH
size_t gdata_cnt = 0;
if (PTLS_LIKELY(aadlen != 0)) {
while (gdata_cnt < 6) {
if (PTLS_LIKELY(aadlen < 16)) {
if (aadlen != 0) {
gdatabuf[gdata_cnt++] = loadn128(aad, aadlen);
aadlen = 0;
}
goto MainLoop;
}
gdatabuf[gdata_cnt++] = _mm_loadu_si128(aad++);
aadlen -= 16;
}
}
/* the main loop */
MainLoop:
while (1) {
/* run AES and multiplication in parallel */
size_t i;
for (i = 2; i < gdata_cnt + 2; ++i) {
AESECB6_UPDATE(i);
gfmul_nextstep128(&gstate, _mm_loadu_si128(gdata++), --ghash_precompute);
}
for (; i < ctx->super.ecb.rounds; ++i)
AESECB6_UPDATE(i);
AESECB6_FINAL(i);
/* apply the bit stream to src and write to dest */
if (PTLS_LIKELY(srclen >= 6 * 16)) {
#define APPLY(i) _mm_storeu_si128(dst + i, _mm_xor_si128(_mm_loadu_si128(src + i), bits##i))
APPLY(0);
APPLY(1);
APPLY(2);
APPLY(3);
APPLY(4);
APPLY(5);
#undef APPLY
dst += 6;
src += 6;
srclen -= 6 * 16;
} else {
if ((state & STATE_EK0_BEEN_FED) == STATE_EK0_BEEN_FED) {
ek0 = bits5;
state &= ~STATE_EK0_INCOMPLETE;
}
if ((state & STATE_SUPP_IN_PROCESS) != 0) {
_mm_storeu_si128((__m128i *)supp->output, bits4);
state &= ~(STATE_SUPP_USED | STATE_SUPP_IN_PROCESS);
}
if (srclen != 0) {
#define APPLY(i) \
do { \
if (PTLS_LIKELY(srclen >= 16)) { \
_mm_storeu_si128(dst++, _mm_xor_si128(_mm_loadu_si128(src++), bits##i)); \
srclen -= 16; \
} else if (PTLS_LIKELY(srclen != 0)) { \
bits0 = bits##i; \
goto ApplyRemainder; \
} else { \
goto ApplyEnd; \
} \
} while (0)
APPLY(0);
APPLY(1);
APPLY(2);
APPLY(3);
APPLY(4);
APPLY(5);
#undef APPLY
goto ApplyEnd;
ApplyRemainder:
storen128(dst, srclen, _mm_xor_si128(loadn128(src, srclen), bits0));
dst = (__m128i *)((uint8_t *)dst + srclen);
srclen = 0;
ApplyEnd:;
}
}
/* next block AES starts here */
AESECB6_INIT();
AESECB6_UPDATE(1);
/* setup gdata */
if (PTLS_UNLIKELY(aadlen != 0)) {
gdata_cnt = 0;
while (gdata_cnt < 6) {
if (aadlen < 16) {
if (aadlen != 0) {
gdatabuf[gdata_cnt++] = loadn128(aad, aadlen);
aadlen = 0;
}
goto GdataFillDST;
}
gdatabuf[gdata_cnt++] = _mm_loadu_si128(aad++);
aadlen -= 16;
}
gdata = gdatabuf;
} else if (PTLS_LIKELY(dst_ghashlen >= 6 * 16)) {
gdata = dst_ghash;
gdata_cnt = 6;
dst_ghash += 6;
dst_ghashlen -= 96;
} else {
gdata_cnt = 0;
GdataFillDST:
while (gdata_cnt < 6) {
if (dst_ghashlen < 16) {
if (dst_ghashlen != 0) {
gdatabuf[gdata_cnt++] = loadn128(dst_ghash, dst_ghashlen);
dst_ghashlen = 0;
}
if (gdata_cnt < 6)
goto Finish;
break;
}
gdatabuf[gdata_cnt++] = _mm_loadu_si128(dst_ghash++);
dst_ghashlen -= 16;
}
gdata = gdatabuf;
}
}
Finish:
gdatabuf[gdata_cnt++] = ac;
/* We have complete set of data to be fed into GHASH. Let's finish the remaining calculation.
* Note that by now, all AES operations for payload encryption and ek0 are complete. This is is because it is necessary for GCM
* to process at least the same amount of data (i.e. payload-blocks + AC), and because AES is at least one 96-byte block ahead.
*/
assert(STATE_EK0_READY());
for (size_t i = 0; i < gdata_cnt; ++i)
gfmul_nextstep128(&gstate, gdatabuf[i], --ghash_precompute);
gfmul_reduce128(&gstate);
_mm_storeu_si128(dst, gfmul_get_tag128(&gstate, ek0));
/* Finish the calculation of supplemental vector. Done at the very last, because the sample might cover the GCM tag. */
if ((state & STATE_SUPP_USED) != 0) {
size_t i;
if ((state & STATE_SUPP_IN_PROCESS) == 0) {
bits4keys = ((struct ctr_context *)supp->ctx)->fusion.keys.m128;
bits4 = _mm_xor_si128(_mm_loadu_si128(supp->input), bits4keys[0]);
i = 1;
} else {
i = 2;
}
do {
bits4 = _mm_aesenc_si128(bits4, bits4keys[i++]);
} while (i != ctx->super.ecb.rounds);
bits4 = _mm_aesenclast_si128(bits4, bits4keys[i]);
_mm_storeu_si128((__m128i *)supp->output, bits4);
}
#undef AESECB6_INIT
#undef AESECB6_UPDATE
#undef AESECB6_FINAL
#undef STATE_EK0_BEEN_FOUND
#undef STATE_EK0_READY
#undef STATE_SUPP_IN_PROCESS
}
int ptls_fusion_aesgcm_decrypt(ptls_fusion_aesgcm_context_t *_ctx, void *output, const void *input, size_t inlen, __m128i ctr,
const void *_aad, size_t aadlen, const void *tag)
{
struct ptls_fusion_aesgcm_context128 *ctx = (void *)_ctx;
__m128i ek0 = _mm_setzero_si128(), bits0, bits1 = _mm_setzero_si128(), bits2 = _mm_setzero_si128(), bits3 = _mm_setzero_si128(),
bits4 = _mm_setzero_si128(), bits5 = _mm_setzero_si128();
struct ptls_fusion_gfmul_state128 gstate = {0};
__m128i gdatabuf[6];
__m128i ac = _mm_shuffle_epi8(_mm_set_epi32(0, (int)aadlen * 8, 0, (int)inlen * 8), byteswap128);
struct ptls_fusion_aesgcm_ghash_precompute128 *ghash_precompute = ctx->ghash + (aadlen + 15) / 16 + (inlen + 15) / 16 + 1;
const __m128i *gdata; // points to the elements fed into GHASH
size_t gdata_cnt;
const __m128i *src_ghash = input, *src_aes = input, *aad = _aad;
__m128i *dst = output;
size_t nondata_aes_cnt = 0, src_ghashlen = inlen, src_aeslen = inlen;
/* schedule ek0 and suppkey */
ctr = _mm_add_epi64(ctr, one8);
bits0 = _mm_xor_si128(_mm_shuffle_epi8(ctr, byteswap128), ctx->super.ecb.keys.m128[0]);
++nondata_aes_cnt;
#define STATE_IS_FIRST_RUN 0x1
#define STATE_GHASH_HAS_MORE 0x2
int state = STATE_IS_FIRST_RUN | STATE_GHASH_HAS_MORE;
/* the main loop */
while (1) {
/* setup gdata */
if (PTLS_UNLIKELY(aadlen != 0)) {
gdata = gdatabuf;
gdata_cnt = 0;
while (gdata_cnt < 6) {
if (aadlen < 16) {
if (aadlen != 0) {
gdatabuf[gdata_cnt++] = loadn128(aad, aadlen);
aadlen = 0;
++nondata_aes_cnt;
}
goto GdataFillSrc;
}
gdatabuf[gdata_cnt++] = _mm_loadu_si128(aad++);
aadlen -= 16;
++nondata_aes_cnt;
}
} else if (PTLS_LIKELY(src_ghashlen >= 6 * 16)) {
gdata = src_ghash;
gdata_cnt = 6;
src_ghash += 6;
src_ghashlen -= 6 * 16;
} else {
gdata = gdatabuf;
gdata_cnt = 0;
GdataFillSrc:
while (gdata_cnt < 6) {
if (src_ghashlen < 16) {
if (src_ghashlen != 0) {
gdatabuf[gdata_cnt++] = loadn128(src_ghash, src_ghashlen);
src_ghash = (__m128i *)((uint8_t *)src_ghash + src_ghashlen);
src_ghashlen = 0;
}
if (gdata_cnt < 6 && (state & STATE_GHASH_HAS_MORE) != 0) {
gdatabuf[gdata_cnt++] = ac;
state &= ~STATE_GHASH_HAS_MORE;
}
break;
}
gdatabuf[gdata_cnt++] = _mm_loadu_si128(src_ghash++);
src_ghashlen -= 16;
}
}
/* setup aes bits */
if (PTLS_LIKELY(nondata_aes_cnt == 0))
goto InitAllBits;
switch (nondata_aes_cnt) {
#define INIT_BITS(n, keys) \
case n: \
ctr = _mm_add_epi64(ctr, one8); \
bits##n = _mm_xor_si128(_mm_shuffle_epi8(ctr, byteswap128), keys.m128[0]);
InitAllBits:
INIT_BITS(0, ctx->super.ecb.keys);
INIT_BITS(1, ctx->super.ecb.keys);
INIT_BITS(2, ctx->super.ecb.keys);
INIT_BITS(3, ctx->super.ecb.keys);
INIT_BITS(4, ctx->super.ecb.keys);
INIT_BITS(5, ctx->super.ecb.keys);
#undef INIT_BITS
}
{ /* run aes and ghash */
#define AESECB6_UPDATE(i) \
do { \
__m128i k = ctx->super.ecb.keys.m128[i]; \
bits0 = _mm_aesenc_si128(bits0, k); \
bits1 = _mm_aesenc_si128(bits1, k); \
bits2 = _mm_aesenc_si128(bits2, k); \
bits3 = _mm_aesenc_si128(bits3, k); \
bits4 = _mm_aesenc_si128(bits4, k); \
bits5 = _mm_aesenc_si128(bits5, k); \
} while (0)
size_t aesi;
for (aesi = 1; aesi <= gdata_cnt; ++aesi) {
AESECB6_UPDATE(aesi);
gfmul_nextstep128(&gstate, _mm_loadu_si128(gdata++), --ghash_precompute);
}
for (; aesi < ctx->super.ecb.rounds; ++aesi)
AESECB6_UPDATE(aesi);
__m128i k = ctx->super.ecb.keys.m128[aesi];
bits0 = _mm_aesenclast_si128(bits0, k);
bits1 = _mm_aesenclast_si128(bits1, k);
bits2 = _mm_aesenclast_si128(bits2, k);
bits3 = _mm_aesenclast_si128(bits3, k);
bits4 = _mm_aesenclast_si128(bits4, k);
bits5 = _mm_aesenclast_si128(bits5, k);
#undef AESECB6_UPDATE
}
/* apply aes bits */
if (PTLS_LIKELY(nondata_aes_cnt == 0 && src_aeslen >= 6 * 16)) {
#define APPLY(i) _mm_storeu_si128(dst + i, _mm_xor_si128(_mm_loadu_si128(src_aes + i), bits##i))
APPLY(0);
APPLY(1);
APPLY(2);
APPLY(3);
APPLY(4);
APPLY(5);
#undef APPLY
dst += 6;
src_aes += 6;
src_aeslen -= 6 * 16;
} else {
if ((state & STATE_IS_FIRST_RUN) != 0) {
ek0 = bits0;
state &= ~STATE_IS_FIRST_RUN;
}
switch (nondata_aes_cnt) {
#define APPLY(i) \
case i: \
if (PTLS_LIKELY(src_aeslen > 16)) { \
_mm_storeu_si128(dst++, _mm_xor_si128(_mm_loadu_si128(src_aes++), bits##i)); \
src_aeslen -= 16; \
} else { \
bits0 = bits##i; \
goto Finish; \
}
APPLY(0);
APPLY(1);
APPLY(2);
APPLY(3);
APPLY(4);
APPLY(5);
#undef APPLY
}
nondata_aes_cnt = 0;
}
}
Finish:
if (src_aeslen == 16) {
_mm_storeu_si128(dst, _mm_xor_si128(_mm_loadu_si128(src_aes), bits0));
} else if (src_aeslen != 0) {
storen128(dst, src_aeslen, _mm_xor_si128(loadn128(src_aes, src_aeslen), bits0));
}
assert((state & STATE_IS_FIRST_RUN) == 0);
/* the only case where AES operation is complete and GHASH is not is when the application of AC is remaining */
if ((state & STATE_GHASH_HAS_MORE) != 0) {
assert(ghash_precompute - 1 == ctx->ghash);
gfmul_nextstep128(&gstate, ac, --ghash_precompute);
}
gfmul_reduce128(&gstate);
__m128i calctag = gfmul_get_tag128(&gstate, ek0);
return _mm_movemask_epi8(_mm_cmpeq_epi8(calctag, _mm_loadu_si128(tag))) == 0xffff;
#undef STATE_IS_FIRST_RUN
#undef STATE_GHASH_HAS_MORE
}
static __m128i expand_key(__m128i key, __m128i temp)
{
key = _mm_xor_si128(key, _mm_slli_si128(key, 4));
key = _mm_xor_si128(key, _mm_slli_si128(key, 4));
key = _mm_xor_si128(key, _mm_slli_si128(key, 4));
key = _mm_xor_si128(key, temp);
return key;
}
void ptls_fusion_aesecb_init(ptls_fusion_aesecb_context_t *ctx, int is_enc, const void *key, size_t key_size, int aesni256)
{
assert(is_enc && "decryption is not supported (yet)");
size_t i = 0;
switch (key_size) {
case 16: /* AES128 */
ctx->rounds = 10;
break;
case 32: /* AES256 */
ctx->rounds = 14;
break;
default:
assert(!"invalid key size; AES128 / AES256 are supported");
break;
}
ctx->aesni256 = aesni256;
/* load and expand keys using keys.m128 */
ctx->keys.m128[i++] = _mm_loadu_si128((__m128i *)key);
if (key_size == 32)
ctx->keys.m128[i++] = _mm_loadu_si128((__m128i *)key + 1);
while (1) {
#define EXPAND(R) \
{ \
ctx->keys.m128[i] = \
expand_key(ctx->keys.m128[i - key_size / 16], \
_mm_shuffle_epi32(_mm_aeskeygenassist_si128(ctx->keys.m128[i - 1], R), _MM_SHUFFLE(3, 3, 3, 3))); \
if (i == ctx->rounds) \
break; \
++i; \
if (key_size > 24) { \
ctx->keys.m128[i] = \
expand_key(ctx->keys.m128[i - key_size / 16], \
_mm_shuffle_epi32(_mm_aeskeygenassist_si128(ctx->keys.m128[i - 1], R), _MM_SHUFFLE(2, 2, 2, 2))); \
++i; \
} \
}
EXPAND(0x1);
EXPAND(0x2);
EXPAND(0x4);
EXPAND(0x8);
EXPAND(0x10);
EXPAND(0x20);
EXPAND(0x40);
EXPAND(0x80);
EXPAND(0x1b);
EXPAND(0x36);
#undef EXPAND
}
/* convert to keys.m256 if aesni256 is used */
if (ctx->aesni256) {
size_t i = ctx->rounds;
do {
ctx->keys.m256[i] = _mm256_broadcastsi128_si256(ctx->keys.m128[i]);
} while (i-- != 0);
}
}
void ptls_fusion_aesecb_dispose(ptls_fusion_aesecb_context_t *ctx)
{
ptls_clear_memory(ctx, sizeof(*ctx));
}
void ptls_fusion_aesecb_encrypt(ptls_fusion_aesecb_context_t *ctx, void *dst, const void *src)
{
__m128i v = _mm_loadu_si128(src);
v = aesecb_encrypt(ctx, v);
_mm_storeu_si128(dst, v);
}
/**
* returns the number of ghash entries that is required to handle an AEAD block of given size
*/
static size_t aesgcm_calc_ghash_cnt(size_t capacity)
{
// round-up by block size, add to handle worst split of the size between AAD and payload, plus context to hash AC
return (capacity + 15) / 16 + 2;
}
static void setup_one_ghash_entry(ptls_fusion_aesgcm_context_t *ctx)
{
__m128i *H, *r, *Hprev, H0;
if (ctx->ecb.aesni256) {
struct ptls_fusion_aesgcm_context256 *ctx256 = (void *)ctx;
#define GET_SLOT(i, mem) (&ctx256->ghash[(i) / 2].mem[(i) % 2 == 0])
H = GET_SLOT(ctx->ghash_cnt, H);
r = GET_SLOT(ctx->ghash_cnt, r);
Hprev = ctx->ghash_cnt == 0 ? NULL : GET_SLOT(ctx->ghash_cnt - 1, H);
#undef GET_SLOT
H0 = ctx256->ghash[0].H[1];
} else {
struct ptls_fusion_aesgcm_context128 *ctx128 = (void *)ctx;
H = &ctx128->ghash[ctx->ghash_cnt].H;
r = &ctx128->ghash[ctx->ghash_cnt].r;
Hprev = ctx->ghash_cnt == 0 ? NULL : &ctx128->ghash[ctx->ghash_cnt - 1].H;
H0 = ctx128->ghash[0].H;
}
if (Hprev != NULL)
*H = gfmul(*Hprev, H0);
*r = _mm_shuffle_epi32(*H, 78);
*r = _mm_xor_si128(*r, *H);
++ctx->ghash_cnt;
}
static size_t calc_aesgcm_context_size(size_t *ghash_cnt, int aesni256)
{
size_t sz;
if (aesni256) {
if (*ghash_cnt % 2 != 0)
++*ghash_cnt;
sz = offsetof(struct ptls_fusion_aesgcm_context256, ghash) +
sizeof(union ptls_fusion_aesgcm_ghash_precompute256) * *ghash_cnt / 2;
} else {
sz = offsetof(struct ptls_fusion_aesgcm_context128, ghash) +
sizeof(struct ptls_fusion_aesgcm_ghash_precompute128) * *ghash_cnt;
}
return sz;
}
static ptls_fusion_aesgcm_context_t *new_aesgcm(const void *key, size_t key_size, size_t capacity, int aesni256)
{
ptls_fusion_aesgcm_context_t *ctx;
size_t ghash_cnt = aesgcm_calc_ghash_cnt(capacity), ctx_size = calc_aesgcm_context_size(&ghash_cnt, aesni256);
if ((ctx = aligned_alloc(32, ctx_size)) == NULL)
return NULL;
ptls_fusion_aesecb_init(&ctx->ecb, 1, key, key_size, aesni256);
ctx->capacity = capacity;
__m128i H0 = aesecb_encrypt(&ctx->ecb, _mm_setzero_si128());
H0 = _mm_shuffle_epi8(H0, byteswap128);
H0 = transformH(H0);
if (ctx->ecb.aesni256) {
((struct ptls_fusion_aesgcm_context256 *)ctx)->ghash[0].H[1] = H0;