forked from microsoft/SymCrypt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aes-xmm.c
1779 lines (1577 loc) · 72.6 KB
/
aes-xmm.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
//
// aes-xmm.c code for AES implementation
//
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//
// All XMM code for AES operations
// Requires compiler support for ssse3, aesni and pclmulqdq
//
#include "precomp.h"
#if SYMCRYPT_CPU_X86 | SYMCRYPT_CPU_AMD64
#include "xtsaes_definitions.h"
#include "ghash_definitions.h"
VOID
SYMCRYPT_CALL
SymCryptAes4SboxXmm( _In_reads_(4) PCBYTE pIn, _Out_writes_(4) PBYTE pOut )
{
__m128i x;
x = _mm_set1_epi32( *(int *) pIn );
x = _mm_aeskeygenassist_si128( x, 0 );
// Could use _mm_storeu_si32( pOut, x ) but it is missing from some headers and _mm_store_ss will be as fast
_mm_store_ss( (float *) pOut, _mm_castsi128_ps(x) );
}
VOID
SYMCRYPT_CALL
SymCryptAesCreateDecryptionRoundKeyXmm(
_In_reads_(16) PCBYTE pEncryptionRoundKey,
_Out_writes_(16) PBYTE pDecryptionRoundKey )
{
//
// On x86 our key structure is only 4-aligned (the best we can do) so we use unaligned load/stores.
// On Amd64 our round keys are aligned, but recent CPUs have fast unaligned load/store if the address is
// actually aligned properly.
//
_mm_storeu_si128( (__m128i *) pDecryptionRoundKey, _mm_aesimc_si128( _mm_loadu_si128( (__m128i *)pEncryptionRoundKey ) ) );
}
//
// The latency of AES instruction has increased up to 8 cycles in Ivy Bridge,
// and back to 7 in Haswell.
// We use 8-parallel code to expose the maximum parallelism to the CPU.
// On x86 it will introduce some register spilling, but the load/stores
// should be able to hide behind the AES instruction latencies.
// Silvermont x86 CPUs has AES-NI with latency = 8 and throughput = 5, so there
// the CPU parallelism is low.
// For things like BitLocker that is fine, but other uses, such as GCM & AES_CTR_DRBG
// use odd sizes.
// We try to do 5-8 blocks in 8-parallel code, 2-4 blocks in 4-parallel code, and
// 1 block in 1-parallel code.
// This is a compromise; the big cores can do 8 parallel in about the time of a 4-parallel,
// but Silvermont cannot and would pay a big price on small requests if we only use 8-parallel.
// Doing only 8-parallel and then 1-parallel would penalize the big cores a lot.
//
// We used to have 7-parallel code, but common request sizes are not multiples of 7
// blocks so we end up doing a lot of extra work. This is especially expensive on
// Silvermont where the extra work isn't hidden in the latencies.
//
#define AES_ENCRYPT_1( pExpandedKey, c0 ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = &pExpandedKey->RoundKey[0]; \
keyLimit = pExpandedKey->lastEncRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
c0 = _mm_xor_si128( c0, roundkey ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesenc_si128( c0, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesenc_si128( c0, roundkey ); \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesenc_si128( c0, roundkey ); \
} while( keyPtr < keyLimit ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
\
c0 = _mm_aesenclast_si128( c0, roundkey ); \
};
// Perform AES encryption without the first round key and with a specified last round key
//
// For algorithms where performance is dominated by a chain of dependent AES rounds (i.e. CBC encryption, CCM, CMAC)
// we can gain a reasonable performance uplift by computing (last round key ^ next plaintext block ^ first round key)
// off the critical path and using this computed value in place of last round key in AESENCLAST instructions.
#define AES_ENCRYPT_1_CHAIN( pExpandedKey, cipherState, mergedLastRoundKey ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = &pExpandedKey->RoundKey[1]; \
keyLimit = pExpandedKey->lastEncRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
cipherState = _mm_aesenc_si128( cipherState, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
cipherState = _mm_aesenc_si128( cipherState, roundkey ); \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
cipherState = _mm_aesenc_si128( cipherState, roundkey ); \
} while( keyPtr < keyLimit ); \
\
cipherState = _mm_aesenclast_si128( cipherState, mergedLastRoundKey ); \
};
#define AES_ENCRYPT_4( pExpandedKey, c0, c1, c2, c3 ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = &pExpandedKey->RoundKey[0]; \
keyLimit = pExpandedKey->lastEncRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
c0 = _mm_xor_si128( c0, roundkey ); \
c1 = _mm_xor_si128( c1, roundkey ); \
c2 = _mm_xor_si128( c2, roundkey ); \
c3 = _mm_xor_si128( c3, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesenc_si128( c0, roundkey ); \
c1 = _mm_aesenc_si128( c1, roundkey ); \
c2 = _mm_aesenc_si128( c2, roundkey ); \
c3 = _mm_aesenc_si128( c3, roundkey ); \
} while( keyPtr < keyLimit ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
\
c0 = _mm_aesenclast_si128( c0, roundkey ); \
c1 = _mm_aesenclast_si128( c1, roundkey ); \
c2 = _mm_aesenclast_si128( c2, roundkey ); \
c3 = _mm_aesenclast_si128( c3, roundkey ); \
};
#define AES_ENCRYPT_8( pExpandedKey, c0, c1, c2, c3, c4, c5, c6, c7 ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = &pExpandedKey->RoundKey[0]; \
keyLimit = pExpandedKey->lastEncRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
c0 = _mm_xor_si128( c0, roundkey ); \
c1 = _mm_xor_si128( c1, roundkey ); \
c2 = _mm_xor_si128( c2, roundkey ); \
c3 = _mm_xor_si128( c3, roundkey ); \
c4 = _mm_xor_si128( c4, roundkey ); \
c5 = _mm_xor_si128( c5, roundkey ); \
c6 = _mm_xor_si128( c6, roundkey ); \
c7 = _mm_xor_si128( c7, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesenc_si128( c0, roundkey ); \
c1 = _mm_aesenc_si128( c1, roundkey ); \
c2 = _mm_aesenc_si128( c2, roundkey ); \
c3 = _mm_aesenc_si128( c3, roundkey ); \
c4 = _mm_aesenc_si128( c4, roundkey ); \
c5 = _mm_aesenc_si128( c5, roundkey ); \
c6 = _mm_aesenc_si128( c6, roundkey ); \
c7 = _mm_aesenc_si128( c7, roundkey ); \
} while( keyPtr < keyLimit ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
\
c0 = _mm_aesenclast_si128( c0, roundkey ); \
c1 = _mm_aesenclast_si128( c1, roundkey ); \
c2 = _mm_aesenclast_si128( c2, roundkey ); \
c3 = _mm_aesenclast_si128( c3, roundkey ); \
c4 = _mm_aesenclast_si128( c4, roundkey ); \
c5 = _mm_aesenclast_si128( c5, roundkey ); \
c6 = _mm_aesenclast_si128( c6, roundkey ); \
c7 = _mm_aesenclast_si128( c7, roundkey ); \
};
#define AES_DECRYPT_1( pExpandedKey, c0 ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = pExpandedKey->lastEncRoundKey; \
keyLimit = pExpandedKey->lastDecRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
c0 = _mm_xor_si128( c0, roundkey ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesdec_si128( c0, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesdec_si128( c0, roundkey ); \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesdec_si128( c0, roundkey ); \
} while( keyPtr < keyLimit ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
\
c0 = _mm_aesdeclast_si128( c0, roundkey ); \
};
#define AES_DECRYPT_4( pExpandedKey, c0, c1, c2, c3 ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = pExpandedKey->lastEncRoundKey; \
keyLimit = pExpandedKey->lastDecRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
c0 = _mm_xor_si128( c0, roundkey ); \
c1 = _mm_xor_si128( c1, roundkey ); \
c2 = _mm_xor_si128( c2, roundkey ); \
c3 = _mm_xor_si128( c3, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesdec_si128( c0, roundkey ); \
c1 = _mm_aesdec_si128( c1, roundkey ); \
c2 = _mm_aesdec_si128( c2, roundkey ); \
c3 = _mm_aesdec_si128( c3, roundkey ); \
} while( keyPtr < keyLimit ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
\
c0 = _mm_aesdeclast_si128( c0, roundkey ); \
c1 = _mm_aesdeclast_si128( c1, roundkey ); \
c2 = _mm_aesdeclast_si128( c2, roundkey ); \
c3 = _mm_aesdeclast_si128( c3, roundkey ); \
};
#define AES_DECRYPT_8( pExpandedKey, c0, c1, c2, c3, c4, c5, c6, c7 ) \
{ \
const BYTE (*keyPtr)[4][4]; \
const BYTE (*keyLimit)[4][4]; \
__m128i roundkey; \
\
keyPtr = pExpandedKey->lastEncRoundKey; \
keyLimit = pExpandedKey->lastDecRoundKey; \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
\
c0 = _mm_xor_si128( c0, roundkey ); \
c1 = _mm_xor_si128( c1, roundkey ); \
c2 = _mm_xor_si128( c2, roundkey ); \
c3 = _mm_xor_si128( c3, roundkey ); \
c4 = _mm_xor_si128( c4, roundkey ); \
c5 = _mm_xor_si128( c5, roundkey ); \
c6 = _mm_xor_si128( c6, roundkey ); \
c7 = _mm_xor_si128( c7, roundkey ); \
\
do \
{ \
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
keyPtr ++; \
c0 = _mm_aesdec_si128( c0, roundkey ); \
c1 = _mm_aesdec_si128( c1, roundkey ); \
c2 = _mm_aesdec_si128( c2, roundkey ); \
c3 = _mm_aesdec_si128( c3, roundkey ); \
c4 = _mm_aesdec_si128( c4, roundkey ); \
c5 = _mm_aesdec_si128( c5, roundkey ); \
c6 = _mm_aesdec_si128( c6, roundkey ); \
c7 = _mm_aesdec_si128( c7, roundkey ); \
} while( keyPtr < keyLimit ); \
\
roundkey = _mm_loadu_si128( (__m128i *) keyPtr ); \
\
c0 = _mm_aesdeclast_si128( c0, roundkey ); \
c1 = _mm_aesdeclast_si128( c1, roundkey ); \
c2 = _mm_aesdeclast_si128( c2, roundkey ); \
c3 = _mm_aesdeclast_si128( c3, roundkey ); \
c4 = _mm_aesdeclast_si128( c4, roundkey ); \
c5 = _mm_aesdeclast_si128( c5, roundkey ); \
c6 = _mm_aesdeclast_si128( c6, roundkey ); \
c7 = _mm_aesdeclast_si128( c7, roundkey ); \
};
//
// The EncryptXmm code is tested through the CFB mode encryption which has no further optimizations.
//
VOID
SYMCRYPT_CALL
SymCryptAesEncryptXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_In_reads_( SYMCRYPT_AES_BLOCK_SIZE ) PCBYTE pbSrc,
_Out_writes_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbDst )
{
__m128i c;
c = _mm_loadu_si128( ( __m128i * ) pbSrc);
AES_ENCRYPT_1( pExpandedKey, c );
_mm_storeu_si128( (__m128i *) pbDst, c );
}
//
// The DecryptXmm code is tested through the EcbDecrypt calls which has no further optimizations.
//
VOID
SYMCRYPT_CALL
SymCryptAesDecryptXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_In_reads_( SYMCRYPT_AES_BLOCK_SIZE ) PCBYTE pbSrc,
_Out_writes_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbDst )
{
__m128i c;
c = _mm_loadu_si128( ( __m128i * ) pbSrc);
AES_DECRYPT_1( pExpandedKey, c );
_mm_storeu_si128( (__m128i *) pbDst, c );
}
// Disable warnings and VC++ runtime checks for use of uninitialized values (by design)
#pragma warning(push)
#pragma warning( disable: 6001 4701 )
#pragma runtime_checks( "u", off )
VOID
SYMCRYPT_CALL
SymCryptAesEcbEncryptXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
__m128i c0, c1, c2, c3, c4, c5, c6, c7;
while( cbData >= 8 * SYMCRYPT_AES_BLOCK_SIZE )
{
c0 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 0 ));
c1 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 16 ));
c2 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 32 ));
c3 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 48 ));
c4 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 64 ));
c5 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 80 ));
c6 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 96 ));
c7 = _mm_loadu_si128( ( __m128i * ) (pbSrc +112 ));
AES_ENCRYPT_8( pExpandedKey, c0, c1, c2, c3, c4, c5, c6, c7 );
_mm_storeu_si128( (__m128i *) (pbDst + 0 ), c0 );
_mm_storeu_si128( (__m128i *) (pbDst + 16 ), c1 );
_mm_storeu_si128( (__m128i *) (pbDst + 32 ), c2 );
_mm_storeu_si128( (__m128i *) (pbDst + 48 ), c3 );
_mm_storeu_si128( (__m128i *) (pbDst + 64 ), c4 );
_mm_storeu_si128( (__m128i *) (pbDst + 80 ), c5 );
_mm_storeu_si128( (__m128i *) (pbDst + 96 ), c6 );
_mm_storeu_si128( (__m128i *) (pbDst +112 ), c7 );
pbSrc += 8 * SYMCRYPT_AES_BLOCK_SIZE;
pbDst += 8 * SYMCRYPT_AES_BLOCK_SIZE;
cbData -= 8 * SYMCRYPT_AES_BLOCK_SIZE;
}
if( cbData < 16 )
{
return;
}
c0 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 0 ));
if( cbData >= 32 )
{
c1 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 16 ));
if( cbData >= 48 )
{
c2 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 32 ));
if( cbData >= 64 )
{
c3 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 48 ));
if( cbData >= 80 )
{
c4 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 64 ));
if( cbData >= 96 )
{
c5 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 80 ));
if( cbData >= 112 )
{
c6 = _mm_loadu_si128( ( __m128i * ) (pbSrc + 96 ));
}
}
}
}
}
}
if( cbData >= 5 * SYMCRYPT_AES_BLOCK_SIZE )
{
AES_ENCRYPT_8( pExpandedKey, c0, c1, c2, c3, c4, c5, c6, c7 );
}
else if( cbData >= 2 * SYMCRYPT_AES_BLOCK_SIZE )
{
AES_ENCRYPT_4( pExpandedKey, c0, c1, c2, c3 );
}
else
{
AES_ENCRYPT_1( pExpandedKey, c0 );
}
_mm_storeu_si128( (__m128i *) (pbDst + 0 ), c0 );
if( cbData >= 32 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 16 ), c1 );
if( cbData >= 48 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 32 ), c2 );
if( cbData >= 64 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 48 ), c3 );
if( cbData >= 80 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 64 ), c4 );
if( cbData >= 96 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 80 ), c5 );
if( cbData >= 112 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 96 ), c6 );
}
}
}
}
}
}
}
#pragma runtime_checks( "u", restore )
#pragma warning( pop )
VOID
SYMCRYPT_CALL
SymCryptAesCbcEncryptXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_Inout_updates_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbChainingValue,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
__m128i c = _mm_loadu_si128( (__m128i *) pbChainingValue );
__m128i rk0 = _mm_loadu_si128( (__m128i *) &pExpandedKey->RoundKey[0] );
__m128i rkLast = _mm_loadu_si128( (__m128i *) pExpandedKey->lastEncRoundKey );
__m128i d;
if (cbData < SYMCRYPT_AES_BLOCK_SIZE)
return;
// This algorithm is dominated by chain of dependent AES rounds, so we want to avoid XOR
// instructions on the critical path where possible
// We can compute (last round key ^ next plaintext block ^ first round key) off the critical
// path and use this with AES_ENCRYPT_1_CHAIN so that only AES instructions write to c in
// the main loop
d = _mm_xor_si128( _mm_loadu_si128( (__m128i *) pbSrc ), rk0 );
c = _mm_xor_si128( c, d );
pbSrc += SYMCRYPT_AES_BLOCK_SIZE;
cbData -= SYMCRYPT_AES_BLOCK_SIZE;
while( cbData >= SYMCRYPT_AES_BLOCK_SIZE )
{
d = _mm_xor_si128( _mm_loadu_si128( (__m128i *) pbSrc ), rk0 );
AES_ENCRYPT_1_CHAIN( pExpandedKey, c, _mm_xor_si128(d, rkLast ) );
_mm_storeu_si128( (__m128i *) pbDst, _mm_xor_si128(c, d) );
pbSrc += SYMCRYPT_AES_BLOCK_SIZE;
pbDst += SYMCRYPT_AES_BLOCK_SIZE;
cbData -= SYMCRYPT_AES_BLOCK_SIZE;
}
AES_ENCRYPT_1_CHAIN( pExpandedKey, c, rkLast );
_mm_storeu_si128( (__m128i *) pbDst, c );
_mm_storeu_si128( (__m128i *) pbChainingValue, c );
}
// Disable warnings and VC++ runtime checks for use of uninitialized values (by design)
#pragma warning(push)
#pragma warning( disable: 6001 4701 )
#pragma runtime_checks( "u", off )
VOID
SYMCRYPT_CALL
SymCryptAesCbcDecryptXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_Inout_updates_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbChainingValue,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
__m128i chain;
__m128i c0, c1, c2, c3, c4, c5, c6, c7;
__m128i d0, d1, d2, d3, d4, d5, d6, d7;
if( cbData < SYMCRYPT_AES_BLOCK_SIZE )
{
return;
}
chain = _mm_loadu_si128( (__m128i *) pbChainingValue );
//
// First we do all multiples of 8 blocks
//
while( cbData >= 8 * SYMCRYPT_AES_BLOCK_SIZE )
{
d0 = c0 = _mm_loadu_si128( (__m128i *) (pbSrc + 0 * SYMCRYPT_AES_BLOCK_SIZE ) );
d1 = c1 = _mm_loadu_si128( (__m128i *) (pbSrc + 1 * SYMCRYPT_AES_BLOCK_SIZE ) );
d2 = c2 = _mm_loadu_si128( (__m128i *) (pbSrc + 2 * SYMCRYPT_AES_BLOCK_SIZE ) );
d3 = c3 = _mm_loadu_si128( (__m128i *) (pbSrc + 3 * SYMCRYPT_AES_BLOCK_SIZE ) );
d4 = c4 = _mm_loadu_si128( (__m128i *) (pbSrc + 4 * SYMCRYPT_AES_BLOCK_SIZE ) );
d5 = c5 = _mm_loadu_si128( (__m128i *) (pbSrc + 5 * SYMCRYPT_AES_BLOCK_SIZE ) );
d6 = c6 = _mm_loadu_si128( (__m128i *) (pbSrc + 6 * SYMCRYPT_AES_BLOCK_SIZE ) );
d7 = c7 = _mm_loadu_si128( (__m128i *) (pbSrc + 7 * SYMCRYPT_AES_BLOCK_SIZE ) );
AES_DECRYPT_8( pExpandedKey, c0, c1, c2, c3, c4, c5, c6, c7 );
c0 = _mm_xor_si128( c0, chain );
c1 = _mm_xor_si128( c1, d0 );
c2 = _mm_xor_si128( c2, d1 );
c3 = _mm_xor_si128( c3, d2 );
c4 = _mm_xor_si128( c4, d3 );
c5 = _mm_xor_si128( c5, d4 );
c6 = _mm_xor_si128( c6, d5 );
c7 = _mm_xor_si128( c7, d6 );
chain = d7;
_mm_storeu_si128( (__m128i *) (pbDst + 0 * SYMCRYPT_AES_BLOCK_SIZE ), c0 );
_mm_storeu_si128( (__m128i *) (pbDst + 1 * SYMCRYPT_AES_BLOCK_SIZE ), c1 );
_mm_storeu_si128( (__m128i *) (pbDst + 2 * SYMCRYPT_AES_BLOCK_SIZE ), c2 );
_mm_storeu_si128( (__m128i *) (pbDst + 3 * SYMCRYPT_AES_BLOCK_SIZE ), c3 );
_mm_storeu_si128( (__m128i *) (pbDst + 4 * SYMCRYPT_AES_BLOCK_SIZE ), c4 );
_mm_storeu_si128( (__m128i *) (pbDst + 5 * SYMCRYPT_AES_BLOCK_SIZE ), c5 );
_mm_storeu_si128( (__m128i *) (pbDst + 6 * SYMCRYPT_AES_BLOCK_SIZE ), c6 );
_mm_storeu_si128( (__m128i *) (pbDst + 7 * SYMCRYPT_AES_BLOCK_SIZE ), c7 );
pbSrc += 8 * SYMCRYPT_AES_BLOCK_SIZE;
pbDst += 8 * SYMCRYPT_AES_BLOCK_SIZE;
cbData -= 8 * SYMCRYPT_AES_BLOCK_SIZE;
}
if( cbData >= 16 )
{
//
// There is remaining work to be done
//
d0 = c0 = _mm_loadu_si128( (__m128i *) (pbSrc + 0 * SYMCRYPT_AES_BLOCK_SIZE ) );
if( cbData >= 32 )
{
d1 = c1 = _mm_loadu_si128( (__m128i *) (pbSrc + 1 * SYMCRYPT_AES_BLOCK_SIZE ) );
if( cbData >= 48 )
{
d2 = c2 = _mm_loadu_si128( (__m128i *) (pbSrc + 2 * SYMCRYPT_AES_BLOCK_SIZE ) );
if( cbData >= 64 )
{
d3 = c3 = _mm_loadu_si128( (__m128i *) (pbSrc + 3 * SYMCRYPT_AES_BLOCK_SIZE ) );
if( cbData >= 80 )
{
d4 = c4 = _mm_loadu_si128( (__m128i *) (pbSrc + 4 * SYMCRYPT_AES_BLOCK_SIZE ) );
if( cbData >= 96 )
{
d5 = c5 = _mm_loadu_si128( (__m128i *) (pbSrc + 5 * SYMCRYPT_AES_BLOCK_SIZE ) );
if( cbData >= 112 )
{
d6 = c6 = _mm_loadu_si128( (__m128i *) (pbSrc + 6 * SYMCRYPT_AES_BLOCK_SIZE ) );
}
}
}
}
}
}
//
// Decrypt 1, 4, or 8 blocks in AES-CBC mode. This might decrypt uninitialized registers,
// but those will not be used when we store the results.
//
if( cbData > 4 * SYMCRYPT_AES_BLOCK_SIZE )
{
AES_DECRYPT_8( pExpandedKey, c0, c1, c2, c3, c4, c5, c6, c7 );
c0 = _mm_xor_si128( c0, chain );
c1 = _mm_xor_si128( c1, d0 );
c2 = _mm_xor_si128( c2, d1 );
c3 = _mm_xor_si128( c3, d2 );
c4 = _mm_xor_si128( c4, d3 );
c5 = _mm_xor_si128( c5, d4 );
c6 = _mm_xor_si128( c6, d5 );
}
else if( cbData > SYMCRYPT_AES_BLOCK_SIZE )
{
AES_DECRYPT_4( pExpandedKey, c0, c1, c2, c3 );
c0 = _mm_xor_si128( c0, chain );
c1 = _mm_xor_si128( c1, d0 );
c2 = _mm_xor_si128( c2, d1 );
c3 = _mm_xor_si128( c3, d2 );
} else
{
AES_DECRYPT_1( pExpandedKey, c0 );
c0 = _mm_xor_si128( c0, chain );
}
chain = _mm_loadu_si128( (__m128i *) (pbSrc + cbData - SYMCRYPT_AES_BLOCK_SIZE ) );
_mm_storeu_si128( (__m128i *) (pbDst + 0 * SYMCRYPT_AES_BLOCK_SIZE ), c0 );
if( cbData >= 32 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 1 * SYMCRYPT_AES_BLOCK_SIZE ), c1 );
if( cbData >= 48 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 2 * SYMCRYPT_AES_BLOCK_SIZE ), c2 );
if( cbData >= 64 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 3 * SYMCRYPT_AES_BLOCK_SIZE ), c3 );
if( cbData >= 80 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 4 * SYMCRYPT_AES_BLOCK_SIZE ), c4 );
if( cbData >= 96 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 5 * SYMCRYPT_AES_BLOCK_SIZE ), c5 );
if( cbData >= 112 )
{
_mm_storeu_si128( (__m128i *) (pbDst + 6 * SYMCRYPT_AES_BLOCK_SIZE ), c6 );
}
}
}
}
}
}
}
_mm_storeu_si128( (__m128i *) pbChainingValue, chain );
return;
}
#pragma runtime_checks( "u", restore )
#pragma warning( pop )
VOID
SYMCRYPT_CALL
SymCryptAesCbcMacXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_Inout_updates_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbChainingValue,
_In_reads_( cbData ) PCBYTE pbData,
SIZE_T cbData )
{
__m128i c = _mm_loadu_si128( (__m128i *) pbChainingValue );
__m128i rk0 = _mm_loadu_si128( (__m128i *) &pExpandedKey->RoundKey[0] );
__m128i rkLast = _mm_loadu_si128( (__m128i *) pExpandedKey->lastEncRoundKey );
__m128i d, rk0AndLast;
if (cbData < SYMCRYPT_AES_BLOCK_SIZE)
return;
// This algorithm is dominated by chain of dependent AES rounds, so we want to avoid XOR
// instructions on the critical path where possible
// We can compute (last round key ^ next plaintext block ^ first round key) off the critical
// path and use this with AES_ENCRYPT_1_CHAIN so that only AES instructions write to c in
// the main loop
d = _mm_xor_si128( _mm_loadu_si128( (__m128i *) pbData ), rk0 );
c = _mm_xor_si128( c, d );
pbData += SYMCRYPT_AES_BLOCK_SIZE;
cbData -= SYMCRYPT_AES_BLOCK_SIZE;
// As we don't compute ciphertext here, we only need to XOR rk0 and rkLast once
rk0AndLast = _mm_xor_si128( rk0, rkLast );
while( cbData >= SYMCRYPT_AES_BLOCK_SIZE )
{
d = _mm_xor_si128( _mm_loadu_si128( (__m128i *) pbData ), rk0AndLast );
AES_ENCRYPT_1_CHAIN( pExpandedKey, c, d );
pbData += SYMCRYPT_AES_BLOCK_SIZE;
cbData -= SYMCRYPT_AES_BLOCK_SIZE;
}
AES_ENCRYPT_1_CHAIN( pExpandedKey, c, rkLast );
_mm_storeu_si128( (__m128i *) pbChainingValue, c );
}
#pragma warning(push)
#pragma warning( disable:4701 ) // "Use of uninitialized variable"
#pragma runtime_checks( "u", off )
#define SYMCRYPT_AesCtrMsbXxXmm SymCryptAesCtrMsb64Xmm
#define MM_ADD_EPIXX _mm_add_epi64
#define MM_SUB_EPIXX _mm_sub_epi64
#include "aes-pattern.c"
#undef MM_SUB_EPIXX
#undef MM_ADD_EPIXX
#undef SYMCRYPT_AesCtrMsbXxXmm
#define SYMCRYPT_AesCtrMsbXxXmm SymCryptAesCtrMsb32Xmm
#define MM_ADD_EPIXX _mm_add_epi32
#define MM_SUB_EPIXX _mm_sub_epi32
#include "aes-pattern.c"
#undef MM_SUB_EPIXX
#undef MM_ADD_EPIXX
#undef SYMCRYPT_AesCtrMsbXxXmm
#pragma runtime_checks( "u", restore )
#pragma warning(pop)
/*
if( cbData >= 16 )
{
if( cbData >= 32 )
{
if( cbData >= 48 )
{
if( cbData >= 64 )
{
if( cbData >= 80 )
{
if( cbData >= 96 )
{
if( cbData >= 112 )
{
}
}
}
}
}
}
}
*/
VOID
SYMCRYPT_CALL
SymCryptXtsAesEncryptDataUnitXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_In_reads_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbTweakBlock,
_Out_writes_( SYMCRYPT_AES_BLOCK_SIZE*16 ) PBYTE pbScratch,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,
SIZE_T cbData )
{
__m128i t0;
__m128i c0, c1, c2, c3, c4, c5, c6, c7;
__m128i roundkey, firstRoundKey, lastRoundKey;
__m128i XTS_ALPHA_MASK = _mm_set_epi32( 1, 1, 1, 0x87 );
SYMCRYPT_GF128_ELEMENT* tweakBuffer = (SYMCRYPT_GF128_ELEMENT*) pbScratch;
const BYTE (*keyPtr)[4][4];
const BYTE (*keyLimit)[4][4] = pExpandedKey->lastEncRoundKey;
UINT64 lastTweakLow, lastTweakHigh;
int aesEncryptXtsLoop;
SIZE_T cbDataMain; // number of bytes to handle in the main loop
SIZE_T cbDataTail; // number of bytes to handle in the tail loop
SYMCRYPT_ASSERT(cbData >= SYMCRYPT_AES_BLOCK_SIZE);
// To simplify logic and unusual size processing, we handle all
// data not a multiple of 8 blocks in the tail loop
cbDataTail = cbData & ((8*SYMCRYPT_AES_BLOCK_SIZE)-1);
// Additionally, so that ciphertext stealing logic does not rely on
// reading back from the destination buffer, when we have a non-zero
// tail, we ensure that we handle at least 1 whole block in the tail
//
// Note that our caller has ensured we have at least 1 whole block
// to process, this is checked in debug build
// This means that cbDataTail is in [1,15] at this point iff there are
// at least 8 whole blocks to process; so the below does not cause
// cbDataTail or cbDataMain to exceed cbData
cbDataTail += ((cbDataTail > 0) && (cbDataTail < SYMCRYPT_AES_BLOCK_SIZE)) ? (8*SYMCRYPT_AES_BLOCK_SIZE) : 0;
cbDataMain = cbData - cbDataTail;
SYMCRYPT_ASSERT(cbDataMain <= cbData);
SYMCRYPT_ASSERT(cbDataTail <= cbData);
SYMCRYPT_ASSERT((cbDataMain & ((8*SYMCRYPT_AES_BLOCK_SIZE)-1)) == 0);
c0 = _mm_loadu_si128( (__m128i *) pbTweakBlock );
XTS_MUL_ALPHA( c0, c1 );
XTS_MUL_ALPHA( c1, c2 );
XTS_MUL_ALPHA( c2, c3 );
XTS_MUL_ALPHA4( c0, c4 );
XTS_MUL_ALPHA ( c4, c5 );
XTS_MUL_ALPHA ( c5, c6 );
XTS_MUL_ALPHA ( c6, c7 );
tweakBuffer[0].m128i = c0;
tweakBuffer[1].m128i = c1;
tweakBuffer[2].m128i = c2;
tweakBuffer[3].m128i = c3;
tweakBuffer[4].m128i = c4;
tweakBuffer[5].m128i = c5;
tweakBuffer[6].m128i = c6;
tweakBuffer[7].m128i = c7;
lastTweakLow = tweakBuffer[7].ull[0];
lastTweakHigh = tweakBuffer[7].ull[1];
firstRoundKey = _mm_loadu_si128( (__m128i *) &pExpandedKey->RoundKey[0] );
lastRoundKey = _mm_loadu_si128( (__m128i *) pExpandedKey->lastEncRoundKey );
while( cbDataMain > 0 )
{
// At loop entry, tweakBuffer[0-7] are tweakValues for the next 8 blocks
c0 = _mm_xor_si128( tweakBuffer[0].m128i, firstRoundKey );
c1 = _mm_xor_si128( tweakBuffer[1].m128i, firstRoundKey );
c2 = _mm_xor_si128( tweakBuffer[2].m128i, firstRoundKey );
c3 = _mm_xor_si128( tweakBuffer[3].m128i, firstRoundKey );
c4 = _mm_xor_si128( tweakBuffer[4].m128i, firstRoundKey );
c5 = _mm_xor_si128( tweakBuffer[5].m128i, firstRoundKey );
c6 = _mm_xor_si128( tweakBuffer[6].m128i, firstRoundKey );
c7 = _mm_xor_si128( tweakBuffer[7].m128i, firstRoundKey );
c0 = _mm_xor_si128( c0, _mm_loadu_si128( ( __m128i * ) (pbSrc + 0) ) );
c1 = _mm_xor_si128( c1, _mm_loadu_si128( ( __m128i * ) (pbSrc + 16) ) );
c2 = _mm_xor_si128( c2, _mm_loadu_si128( ( __m128i * ) (pbSrc + 32) ) );
c3 = _mm_xor_si128( c3, _mm_loadu_si128( ( __m128i * ) (pbSrc + 48) ) );
c4 = _mm_xor_si128( c4, _mm_loadu_si128( ( __m128i * ) (pbSrc + 64) ) );
c5 = _mm_xor_si128( c5, _mm_loadu_si128( ( __m128i * ) (pbSrc + 80) ) );
c6 = _mm_xor_si128( c6, _mm_loadu_si128( ( __m128i * ) (pbSrc + 96) ) );
c7 = _mm_xor_si128( c7, _mm_loadu_si128( ( __m128i * ) (pbSrc + 112) ) );
keyPtr = &pExpandedKey->RoundKey[1];
// Do 8 full rounds (AES-128|AES-192|AES-256) with stitched XTS (peformed in scalar registers)
for( aesEncryptXtsLoop = 0; aesEncryptXtsLoop < 8; aesEncryptXtsLoop++ )
{
roundkey = _mm_loadu_si128( (__m128i *) keyPtr );
keyPtr ++;
c0 = _mm_aesenc_si128( c0, roundkey );
c1 = _mm_aesenc_si128( c1, roundkey );
c2 = _mm_aesenc_si128( c2, roundkey );
c3 = _mm_aesenc_si128( c3, roundkey );
c4 = _mm_aesenc_si128( c4, roundkey );
c5 = _mm_aesenc_si128( c5, roundkey );
c6 = _mm_aesenc_si128( c6, roundkey );
c7 = _mm_aesenc_si128( c7, roundkey );
// Prepare tweakBuffer[8-15] with tweak^lastRoundKey
tweakBuffer[ 8+aesEncryptXtsLoop ].m128i = _mm_xor_si128( tweakBuffer[ aesEncryptXtsLoop ].m128i, lastRoundKey );
// Prepare tweakBuffer[0-7] with tweaks for next 8 blocks
XTS_MUL_ALPHA_Scalar( lastTweakLow, lastTweakHigh );
tweakBuffer[ aesEncryptXtsLoop ].ull[0] = lastTweakLow;
tweakBuffer[ aesEncryptXtsLoop ].ull[1] = lastTweakHigh;
}
do
{
roundkey = _mm_loadu_si128( (__m128i *) keyPtr );
keyPtr ++;
c0 = _mm_aesenc_si128( c0, roundkey );
c1 = _mm_aesenc_si128( c1, roundkey );
c2 = _mm_aesenc_si128( c2, roundkey );
c3 = _mm_aesenc_si128( c3, roundkey );
c4 = _mm_aesenc_si128( c4, roundkey );
c5 = _mm_aesenc_si128( c5, roundkey );
c6 = _mm_aesenc_si128( c6, roundkey );
c7 = _mm_aesenc_si128( c7, roundkey );
} while( keyPtr < keyLimit );
_mm_storeu_si128( (__m128i *) (pbDst + 0), _mm_aesenclast_si128( c0, tweakBuffer[ 8].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 16), _mm_aesenclast_si128( c1, tweakBuffer[ 9].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 32), _mm_aesenclast_si128( c2, tweakBuffer[10].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 48), _mm_aesenclast_si128( c3, tweakBuffer[11].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 64), _mm_aesenclast_si128( c4, tweakBuffer[12].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 80), _mm_aesenclast_si128( c5, tweakBuffer[13].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 96), _mm_aesenclast_si128( c6, tweakBuffer[14].m128i ) );
_mm_storeu_si128( (__m128i *) (pbDst + 112), _mm_aesenclast_si128( c7, tweakBuffer[15].m128i ) );
pbSrc += 8 * SYMCRYPT_AES_BLOCK_SIZE;
pbDst += 8 * SYMCRYPT_AES_BLOCK_SIZE;
cbDataMain -= 8 * SYMCRYPT_AES_BLOCK_SIZE;
}
if( cbDataTail == 0 )
{
return; // <-- expected case; early return here
}
// Rare case, with data unit length not being multiple of 128 bytes, handle the tail one block at a time
t0 = tweakBuffer[0].m128i;
while( cbDataTail >= 2*SYMCRYPT_AES_BLOCK_SIZE )
{
c0 = _mm_xor_si128( _mm_loadu_si128( ( __m128i * ) pbSrc ), t0 );
pbSrc += SYMCRYPT_AES_BLOCK_SIZE;
AES_ENCRYPT_1( pExpandedKey, c0 );
_mm_storeu_si128( (__m128i *) pbDst, _mm_xor_si128( c0, t0 ) );
pbDst += SYMCRYPT_AES_BLOCK_SIZE;
XTS_MUL_ALPHA( t0, t0 );
cbDataTail -= SYMCRYPT_AES_BLOCK_SIZE;
}
if( cbDataTail > SYMCRYPT_AES_BLOCK_SIZE )
{
// Ciphertext stealing encryption
//
// +--------------+
// | |
// | V
// +-----------------+ | +-----+-----------+
// | P_m-1 | | | P_m |++++CP+++++|
// +-----------------+ | +-----+-----------+
// | | |
// enc_m-1 | enc_m
// | | |
// V | V
// +-----+-----------+ | +-----------------+
// | C_m |++++CP+++++|--+ | C_m-1 |
// +-----+-----------+ +-----------------+
// | /
// +---------------- / --+
// / |
// | V
// +-----------------+ | +-----+
// | C_m-1 |<-+ | C_m |
// +-----------------+ +-----+
// Encrypt penultimate plaintext block into tweakBuffer[0]
c0 = _mm_xor_si128( _mm_loadu_si128( (__m128i *) pbSrc ), t0 );
AES_ENCRYPT_1( pExpandedKey, c0 );
tweakBuffer[0].m128i = _mm_xor_si128( c0, t0 );
cbDataTail -= SYMCRYPT_AES_BLOCK_SIZE;
// Copy tweakBuffer[0] to tweakBuffer[1]
tweakBuffer[1].m128i = tweakBuffer[0].m128i;
// Copy final plaintext bytes to prefix of tweakBuffer[0] - we must read before writing to support in-place encryption
memcpy( &tweakBuffer[0].ul[0], pbSrc + SYMCRYPT_AES_BLOCK_SIZE, cbDataTail );
// Copy prefix of tweakBuffer[1] to the right place in the destination buffer
memcpy( pbDst + SYMCRYPT_AES_BLOCK_SIZE, &tweakBuffer[1].ul[0], cbDataTail );
// Do final tweak update
XTS_MUL_ALPHA( t0, t0 );
// Load updated tweakBuffer[0] into c0
c0 = tweakBuffer[0].m128i;
} else {
// Just load final plaintext block into c0
c0 = _mm_loadu_si128( (__m128i*) pbSrc );
}
// Final full block encryption
c0 = _mm_xor_si128( c0, t0 );
AES_ENCRYPT_1( pExpandedKey, c0 );
_mm_storeu_si128( (__m128i *) pbDst, _mm_xor_si128( c0, t0 ) );
}
VOID
SYMCRYPT_CALL
SymCryptXtsAesDecryptDataUnitXmm(
_In_ PCSYMCRYPT_AES_EXPANDED_KEY pExpandedKey,
_In_reads_( SYMCRYPT_AES_BLOCK_SIZE ) PBYTE pbTweakBlock,
_Out_writes_( SYMCRYPT_AES_BLOCK_SIZE*16 ) PBYTE pbScratch,
_In_reads_( cbData ) PCBYTE pbSrc,
_Out_writes_( cbData ) PBYTE pbDst,