-
Notifications
You must be signed in to change notification settings - Fork 1
/
camellia_simd128_with_aes_instruction_set.c
2164 lines (1904 loc) · 63.7 KB
/
camellia_simd128_with_aes_instruction_set.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
/*
* Copyright (C) 2020,2022-2023 Jussi Kivilinna <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
/*
* SSE/AVX/NEON implementation of Camellia cipher, using AES-NI/ARMv8-CE for
* sbox calculations. This implementation takes 16 input blocks and process
* them in parallel. Vectorized key setup is also available at the end of
* file.
*
* This work was originally presented in Master's Thesis,
* "Block Ciphers: Fast Implementations on x86-64 Architecture" (pages 42-50)
* http://urn.fi/URN:NBN:fi:oulu-201305311409
*/
#include <stdint.h>
#include "camellia_simd.h"
#if defined(__powerpc__) && defined(__VSX__) && defined(__CRYPTO__) && \
(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
/**********************************************************************
AT&T x86 asm to intrinsics conversion macros (PowerPC VSX+crypto)
**********************************************************************/
#include <altivec.h>
typedef vector signed char int8x16_t;
typedef vector unsigned char uint8x16_t;
typedef vector unsigned short uint16x8_t;
typedef vector unsigned int uint32x4_t;
typedef vector unsigned long long uint64x2_t;
typedef uint64x2_t __m128i;
#define vec_bswap(a) ((__m128i)vec_reve((uint8x16_t)a))
#define vpand128(a, b, o) (o = vec_and(b, a))
#define vpandn128(a, b, o) (o = vec_andc(a, b))
#define vpxor128(a, b, o) (o = vec_xor(b, a))
#define vpor128(a, b, o) (o = vec_or(b, a))
#define vpsrlb128(s, a, o) ({ o = (__m128i)((uint8x16_t)a >> s); })
#define vpsllb128(s, a, o) ({ o = (__m128i)((uint8x16_t)a << s); })
#define vpsrlw128(s, a, o) ({ o = (__m128i)((uint16x8_t)a >> s); })
#define vpsllw128(s, a, o) ({ o = (__m128i)((uint16x8_t)a << s); })
#define vpsrld128(s, a, o) ({ o = (__m128i)((uint32x4_t)a >> s); })
#define vpslld128(s, a, o) ({ o = (__m128i)((uint32x4_t)a << s); })
#define vpsrlq128(s, a, o) ({ o = (__m128i)((uint64x2_t)a >> s); })
#define vpsllq128(s, a, o) ({ o = (__m128i)((uint64x2_t)a << s); })
#define vpsrldq128(s, a, o) ({ uint64x2_t __tmp = { 0, 0 }; \
o = (__m128i)vec_sld((uint8x16_t)__tmp, \
(uint8x16_t)a, (16 - (s)) & 15);})
#define vpslldq128(s, a, o) ({ uint64x2_t __tmp = { 0, 0 }; \
o = (__m128i)vec_sld((uint8x16_t)a, \
(uint8x16_t)__tmp, (s) & 15);})
#define if_vpsrlb128(...) __VA_ARGS__
#define if_not_vpsrlb128(...) /*_*/
#define vpsrl_byte_128(s, a, o) vpsrlb128(s, a, o)
#define vpsll_byte_128(s, a, o) vpsllb128(s, a, o)
#define vpaddb128(a, b, o) (o = (__m128i)vec_add((uint8x16_t)b, (uint8x16_t)a))
#define vpcmpgtb128(a, b, o) (o = (__m128i)vec_cmpgt((int8x16_t)b, (int8x16_t)a))
#define vpabsb128(a, o) (o = (__m128i)vec_abs((int8x16_t)a))
#define vpshufd128_0x4e(a, o) (o = (__m128i)vec_reve((uint64x2_t)a))
#define vpshufd128_0x1b(a, o) (o = (__m128i)vec_reve((uint32x4_t)a))
#define vpshufb128(m, a, o) \
({ uint64x2_t __tmpz = { 0, 0 }; \
o = (__m128i)vec_perm((uint8x16_t)a, (uint8x16_t)__tmpz, (uint8x16_t)m); })
#define vpunpckhdq128(a, b, o) (o = (__m128i)vec_mergel((uint32x4_t)b, (uint32x4_t)a))
#define vpunpckldq128(a, b, o) (o = (__m128i)vec_mergeh((uint32x4_t)b, (uint32x4_t)a))
#define vpunpckhqdq128(a, b, o) (o = (__m128i)vec_mergel((uint64x2_t)b, (uint64x2_t)a))
#define vpunpcklqdq128(a, b, o) (o = (__m128i)vec_mergeh((uint64x2_t)b, (uint64x2_t)a))
#define vmovdqa128(a, o) (o = a)
#define vmovd128(a, o) ({ uint32x4_t __tmp = { (a), 0, 0, 0 }; \
o = (__m128i)(__tmp); })
#define vmovq128(a, o) ({ uint64x2_t __tmp = { (a), 0 }; \
o = (__m128i)(__tmp); })
#define vmovdqa128_memld(a, o) (o = *(const __m128i *)(a))
#define vmovdqa128_memst(a, o) (*(__m128i *)(o) = (a))
#define vpshufb128_amemld(m, a, o) vpshufb128(*(const __m128i *)(m), a, o)
/* Following operations may have unaligned memory input */
#define vmovdqu128_memld(a, o) (o = (__m128i)vec_xl(0, (const uint8_t *)(a)))
#define vpxor128_memld(a, b, o) vpxor128(b, (__m128i)vec_xl(0, (const uint8_t *)(a)), o)
/* Following operations may have unaligned memory output */
#define vmovdqu128_memst(a, o) vec_xst((uint8x16_t)(a), 0, (uint8_t *)(o))
#define vmovq128_memst(a, o) (((uint64_unaligned_t *)(o))[0] = ((__m128i)(a))[0])
/* PowerPC AES encrypt last round => ShiftRows + SubBytes + XOR round key */
static const uint8x16_t shift_row =
{ 0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11 };
#define vaesenclast128(a, b, o) \
({ uint64x2_t __tmp = (__m128i)vec_sbox_be((uint8x16_t)(b)); \
vpshufb128(shift_row, __tmp, __tmp); \
vpxor128(a, __tmp, o); })
/* Macros for exposing SubBytes from PowerPC crypto instructions. */
#define aes_subbytes(a, o) \
(o = (__m128i)vec_sbox_be((uint8x16_t)(a)))
#define aes_subbytes_and_shuf_and_xor(zero, a, o) \
vaesenclast128((zero), (a), (o))
/*#define aes_load_inv_shufmask(shufmask_reg) \
load_frequent_const(inv_shift_row, (shufmask_reg))*/
#define aes_inv_shuf(shufmask_reg, a, o) \
vpshufb128(shufmask_reg, (a), (o))
#define if_aes_subbytes(...) __VA_ARGS__
#define if_not_aes_subbytes(...) /*_*/
#define memory_barrier_with_vec(a) __asm__("" : "+wa"(a) :: "memory")
#endif /* __powerpc__ */
#ifdef __ARM_NEON
/**********************************************************************
AT&T x86 asm to intrinsics conversion macros (ARMv8-CE)
**********************************************************************/
#include <arm_neon.h>
#define __m128i uint64x2_t
#define vpand128(a, b, o) (o = vandq_u64(b, a))
#define vpandn128(a, b, o) (o = vbicq_u64(a, b))
#define vpxor128(a, b, o) (o = veorq_u64(b, a))
#define vpor128(a, b, o) (o = vorrq_u64(b, a))
#define vpsrlb128(s, a, o) (o = (__m128i)vshrq_n_u8((uint8x16_t)a, s))
#define vpsllb128(s, a, o) (o = (__m128i)vshlq_n_u8((uint8x16_t)a, s))
#define vpsrlw128(s, a, o) (o = (__m128i)vshrq_n_u16((uint16x8_t)a, s))
#define vpsllw128(s, a, o) (o = (__m128i)vshlq_n_u16((uint16x8_t)a, s))
#define vpsrld128(s, a, o) (o = (__m128i)vshrq_n_u32((uint32x4_t)a, s))
#define vpslld128(s, a, o) (o = (__m128i)vshlq_n_u32((uint32x4_t)a, s))
#define vpsrlq128(s, a, o) (o = (__m128i)vshrq_n_u64(a, s))
#define vpsllq128(s, a, o) (o = (__m128i)vshlq_n_u64(a, s))
#define vpsrldq128(s, a, o) ({ uint64x2_t __tmp = { 0, 0 }; \
o = (__m128i)vextq_u8((uint8x16_t)a, \
(uint8x16_t)__tmp, (s) & 15);})
#define vpslldq128(s, a, o) ({ uint64x2_t __tmp = { 0, 0 }; \
o = (__m128i)vextq_u8((uint8x16_t)__tmp, \
(uint8x16_t)a, (16 - (s)) & 15);})
#define if_vpsrlb128(...) __VA_ARGS__
#define if_not_vpsrlb128(...) /*_*/
#define vpsrl_byte_128(s, a, o) vpsrlb128(s, a, o)
#define vpsll_byte_128(s, a, o) vpsllb128(s, a, o)
#define vpaddb128(a, b, o) (o = (__m128i)vaddq_u8((uint8x16_t)b, (uint8x16_t)a))
#define vpcmpgtb128(a, b, o) (o = (__m128i)vcgtq_s8((int8x16_t)b, (int8x16_t)a))
#define vpabsb128(a, o) (o = (__m128i)vabsq_s8((int8x16_t)a))
#define vpshufd128_0x4e(a, o) (o = (__m128i)vextq_u8((uint8x16_t)a, (uint8x16_t)a, 8))
#define vpshufd128_0x1b(a, o) (o = (__m128i)vrev64q_u32((uint32x4_t)vextq_u8((uint8x16_t)a, (uint8x16_t)a, 8)))
#define vpshufb128(m, a, o) (o = (__m128i)vqtbl1q_u8((uint8x16_t)a, (uint8x16_t)m))
#define vpunpckhdq128(a, b, o) (o = (__m128i)vzip2q_u32((uint32x4_t)b, (uint32x4_t)a))
#define vpunpckldq128(a, b, o) (o = (__m128i)vzip1q_u32((uint32x4_t)b, (uint32x4_t)a))
#define vpunpckhqdq128(a, b, o) (o = (__m128i)vzip2q_u64(b, a))
#define vpunpcklqdq128(a, b, o) (o = (__m128i)vzip1q_u64(b, a))
/* CE AES encrypt last round => ShiftRows + SubBytes + XOR round key */
#define vaesenclast128(a, b, o) (o = (__m128i)vaeseq_u8((uint8x16_t)b, (uint8x16_t)a))
#define vmovdqa128(a, o) (o = a)
#define vmovd128(a, o) ({ uint32x4_t __tmp = { a, 0, 0, 0 }; o = (__m128i)__tmp; })
#define vmovq128(a, o) ({ uint64x2_t __tmp = { a, 0 }; o = (__m128i)__tmp; })
#define vmovdqa128_memld(a, o) (o = (*(const __m128i *)(a)))
#define vmovdqa128_memst(a, o) (*(__m128i *)(o) = (a))
#define vpshufb128_amemld(m, a, o) vpshufb128(*(const __m128i *)(m), a, o)
/* Following operations may have unaligned memory input */
#define vmovdqu128_memld(a, o) (o = (__m128i)vld1q_u8((const uint8_t *)(a)))
#define vpxor128_memld(a, b, o) vpxor128(b, (__m128i)vld1q_u8((const uint8_t *)(a)), o)
/* Following operations may have unaligned memory output */
#define vmovdqu128_memst(a, o) vst1q_u8((uint8_t *)(o), (uint8x16_t)a)
#define vmovq128_memst(a, o) (((uint64_unaligned_t *)(o))[0] = (a)[0])
/* Macros for exposing SubBytes from Crypto-Extension instruction set. */
#define aes_subbytes_and_shuf_and_xor(zero, a, o) \
vaesenclast128(zero, a, o)
#define aes_load_inv_shufmask(shufmask_reg) \
load_frequent_const(inv_shift_row, shufmask_reg)
#define aes_inv_shuf(shufmask_reg, a, o) \
vpshufb128(shufmask_reg, a, o)
#define if_aes_subbytes(...) /*_*/
#define if_not_aes_subbytes(...) __VA_ARGS__
#define memory_barrier_with_vec(a) __asm__("" : "+w"(a) :: "memory")
#endif /* __ARM_NEON */
#if defined(__x86_64__) || defined(__i386__)
/**********************************************************************
AT&T x86 asm to intrinsics conversion macros
**********************************************************************/
#include <x86intrin.h>
#define vpand128(a, b, o) (o = _mm_and_si128(b, a))
#define vpandn128(a, b, o) (o = _mm_andnot_si128(b, a))
#define vpxor128(a, b, o) (o = _mm_xor_si128(b, a))
#define vpor128(a, b, o) (o = _mm_or_si128(b, a))
#define vpsrlw128(s, a, o) (o = _mm_srli_epi16(a, s))
#define vpsllw128(s, a, o) (o = _mm_slli_epi16(a, s))
#define vpsrld128(s, a, o) (o = _mm_srli_epi32(a, s))
#define vpslld128(s, a, o) (o = _mm_slli_epi32(a, s))
#define vpsrlq128(s, a, o) (o = _mm_srli_epi64(a, s))
#define vpsllq128(s, a, o) (o = _mm_slli_epi64(a, s))
#define vpsrldq128(s, a, o) (o = _mm_srli_si128(a, s))
#define vpslldq128(s, a, o) (o = _mm_slli_si128(a, s))
#define if_vpsrlb128(...) /*_*/
#define if_not_vpsrlb128(...) __VA_ARGS__
#define vpsrl_byte_128(s, a, o) vpsrld128(s, a, o)
#define vpsll_byte_128(s, a, o) vpslld128(s, a, o)
#define vpaddb128(a, b, o) (o = _mm_add_epi8(b, a))
#define vpcmpgtb128(a, b, o) (o = _mm_cmpgt_epi8(b, a))
#define vpabsb128(a, o) (o = _mm_abs_epi8(a))
#define vpshufd128_0x1b(a, o) (o = _mm_shuffle_epi32(a, 0x1b))
#define vpshufd128_0x4e(a, o) (o = _mm_shuffle_epi32(a, 0x4e))
#define vpshufb128(m, a, o) (o = _mm_shuffle_epi8(a, m))
#define vpunpckhdq128(a, b, o) (o = _mm_unpackhi_epi32(b, a))
#define vpunpckldq128(a, b, o) (o = _mm_unpacklo_epi32(b, a))
#define vpunpckhqdq128(a, b, o) (o = _mm_unpackhi_epi64(b, a))
#define vpunpcklqdq128(a, b, o) (o = _mm_unpacklo_epi64(b, a))
/* AES-NI encrypt last round => ShiftRows + SubBytes + XOR round key */
#define vaesenclast128(a, b, o) (o = _mm_aesenclast_si128(b, a))
#define vmovdqa128(a, o) (o = a)
#define vmovd128(a, o) (o = _mm_set_epi32(0, 0, 0, a))
#define vmovq128(a, o) (o = _mm_set_epi64x(0, a))
#define vmovdqa128_memld(a, o) (o = (*(const __m128i *)(a)))
#define vmovdqa128_memst(a, o) (*(__m128i *)(o) = (a))
#define vpshufb128_amemld(m, a, o) vpshufb128(*(const __m128i *)(m), a, o)
/* Following operations may have unaligned memory input */
#define vmovdqu128_memld(a, o) (o = _mm_loadu_si128((const __m128i *)(a)))
#define vpxor128_memld(a, b, o) \
vpxor128(b, _mm_loadu_si128((const __m128i *)(a)), o)
/* Following operations may have unaligned memory output */
#define vmovdqu128_memst(a, o) _mm_storeu_si128((__m128i *)(o), a)
#define vmovq128_memst(a, o) _mm_storel_epi64((__m128i *)(o), a)
/* Macros for exposing SubBytes from AES-NI instruction set. */
#define aes_subbytes_and_shuf_and_xor(zero, a, o) \
vaesenclast128(zero, a, o)
#define aes_load_inv_shufmask(shufmask_reg) \
load_frequent_const(inv_shift_row, shufmask_reg)
#define aes_inv_shuf(shufmask_reg, a, o) \
vpshufb128(shufmask_reg, a, o)
#define if_aes_subbytes(...) /*_*/
#define if_not_aes_subbytes(...) __VA_ARGS__
#define memory_barrier_with_vec(a) __asm__("" : "+x"(a) :: "memory")
#endif /* defined(__x86_64__) || defined(__i386__) */
/**********************************************************************
helper macros
**********************************************************************/
#define filter_8bit(x, lo_t, hi_t, mask4bit, tmp0) \
vpand128(x, mask4bit, tmp0); \
if_vpsrlb128(vpsrlb128(4, x, x)); \
if_not_vpsrlb128(vpandn128(x, mask4bit, x)); \
if_not_vpsrlb128(vpsrld128(4, x, x)); \
\
vpshufb128(tmp0, lo_t, tmp0); \
vpshufb128(x, hi_t, x); \
vpxor128(tmp0, x, x);
#define transpose_4x4(x0, x1, x2, x3, t1, t2) \
vpunpckhdq128(x1, x0, t2); \
vpunpckldq128(x1, x0, x0); \
\
vpunpckldq128(x3, x2, t1); \
vpunpckhdq128(x3, x2, x2); \
\
vpunpckhqdq128(t1, x0, x1); \
vpunpcklqdq128(t1, x0, x0); \
\
vpunpckhqdq128(x2, t2, x3); \
vpunpcklqdq128(x2, t2, x2);
#define load_zero(o) vmovq128(0, o)
#define load_frequent_const(constant, o) vmovdqa128(constant ## _stack, o)
#define prepare_frequent_const(constant) \
vmovdqa128_memld(&(constant), constant ## _stack); \
memory_barrier_with_vec(constant ## _stack)
#define prepare_frequent_constants() \
prepare_frequent_const(inv_shift_row); \
prepare_frequent_const(pack_bswap); \
prepare_frequent_const(shufb_16x16b); \
prepare_frequent_const(mask_0f); \
prepare_frequent_const(pre_tf_lo_s1); \
prepare_frequent_const(pre_tf_hi_s1); \
prepare_frequent_const(pre_tf_lo_s4); \
prepare_frequent_const(pre_tf_hi_s4); \
prepare_frequent_const(post_tf_lo_s1); \
prepare_frequent_const(post_tf_hi_s1); \
prepare_frequent_const(post_tf_lo_s3); \
prepare_frequent_const(post_tf_hi_s3); \
prepare_frequent_const(post_tf_lo_s2); \
prepare_frequent_const(post_tf_hi_s2)
#define frequent_constants_declare \
__m128i inv_shift_row_stack; \
__m128i pack_bswap_stack; \
__m128i shufb_16x16b_stack; \
__m128i mask_0f_stack; \
__m128i pre_tf_lo_s1_stack; \
__m128i pre_tf_hi_s1_stack; \
__m128i pre_tf_lo_s4_stack; \
__m128i pre_tf_hi_s4_stack; \
__m128i post_tf_lo_s1_stack; \
__m128i post_tf_hi_s1_stack; \
__m128i post_tf_lo_s3_stack; \
__m128i post_tf_hi_s3_stack; \
__m128i post_tf_lo_s2_stack; \
__m128i post_tf_hi_s2_stack
/**********************************************************************
16-way camellia macros
**********************************************************************/
/*
* IN:
* x0..x7: byte-sliced AB state
* mem_cd: register pointer storing CD state
* key: index for key material
* OUT:
* x0..x7: new byte-sliced CD state
*/
#define roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, t0, t1, t2, t3, t4, t5, t6, \
t7, mem_cd, key) \
/* \
* S-function with AES subbytes \
*/ \
if_not_aes_subbytes(aes_load_inv_shufmask(t4);) \
load_frequent_const(mask_0f, t7); \
load_frequent_const(pre_tf_lo_s1, t0); \
load_frequent_const(pre_tf_hi_s1, t1); \
\
/* AES inverse shift rows */ \
if_not_aes_subbytes( \
aes_inv_shuf(t4, x0, x0); \
aes_inv_shuf(t4, x7, x7); \
aes_inv_shuf(t4, x1, x1); \
aes_inv_shuf(t4, x4, x4); \
aes_inv_shuf(t4, x2, x2); \
aes_inv_shuf(t4, x5, x5); \
aes_inv_shuf(t4, x3, x3); \
aes_inv_shuf(t4, x6, x6); \
) \
\
/* prefilter sboxes 1, 2 and 3 */ \
load_frequent_const(pre_tf_lo_s4, t2); \
load_frequent_const(pre_tf_hi_s4, t3); \
filter_8bit(x0, t0, t1, t7, t6); \
filter_8bit(x7, t0, t1, t7, t6); \
filter_8bit(x1, t0, t1, t7, t6); \
filter_8bit(x4, t0, t1, t7, t6); \
filter_8bit(x2, t0, t1, t7, t6); \
filter_8bit(x5, t0, t1, t7, t6); \
\
/* prefilter sbox 4 */ \
if_not_aes_subbytes(load_zero(t4);) \
filter_8bit(x3, t2, t3, t7, t6); \
filter_8bit(x6, t2, t3, t7, t6); \
\
/* AES subbytes + AES shift rows */ \
load_frequent_const(post_tf_lo_s1, t0); \
load_frequent_const(post_tf_hi_s1, t1); \
if_not_aes_subbytes( \
aes_subbytes_and_shuf_and_xor(t4, x0, x0); \
aes_subbytes_and_shuf_and_xor(t4, x7, x7); \
aes_subbytes_and_shuf_and_xor(t4, x1, x1); \
aes_subbytes_and_shuf_and_xor(t4, x4, x4); \
aes_subbytes_and_shuf_and_xor(t4, x2, x2); \
aes_subbytes_and_shuf_and_xor(t4, x5, x5); \
aes_subbytes_and_shuf_and_xor(t4, x3, x3); \
aes_subbytes_and_shuf_and_xor(t4, x6, x6); \
) \
if_aes_subbytes( \
aes_subbytes(x0, x0); \
aes_subbytes(x7, x7); \
aes_subbytes(x1, x1); \
aes_subbytes(x4, x4); \
aes_subbytes(x2, x2); \
aes_subbytes(x5, x5); \
aes_subbytes(x3, x3); \
aes_subbytes(x6, x6); \
) \
\
/* postfilter sboxes 1 and 4 */ \
load_frequent_const(post_tf_lo_s3, t2); \
load_frequent_const(post_tf_hi_s3, t3); \
filter_8bit(x0, t0, t1, t7, t6); \
filter_8bit(x7, t0, t1, t7, t6); \
filter_8bit(x3, t0, t1, t7, t6); \
filter_8bit(x6, t0, t1, t7, t6); \
\
/* postfilter sbox 3 */ \
load_frequent_const(post_tf_lo_s2, t4); \
load_frequent_const(post_tf_hi_s2, t5); \
filter_8bit(x2, t2, t3, t7, t6); \
filter_8bit(x5, t2, t3, t7, t6); \
\
vmovq128((key), t0); \
\
/* postfilter sbox 2 */ \
filter_8bit(x1, t4, t5, t7, t2); \
filter_8bit(x4, t4, t5, t7, t2); \
\
/* P-function */ \
vpxor128(x5, x0, x0); \
vpxor128(x6, x1, x1); \
vpxor128(x7, x2, x2); \
vpxor128(x4, x3, x3); \
\
vpxor128(x2, x4, x4); \
vpxor128(x3, x5, x5); \
vpxor128(x0, x6, x6); \
vpxor128(x1, x7, x7); \
\
vpxor128(x7, x0, x0); \
vpxor128(x4, x1, x1); \
vpxor128(x5, x2, x2); \
vpxor128(x6, x3, x3); \
\
vpxor128(x3, x4, x4); \
vpxor128(x0, x5, x5); \
vpxor128(x1, x6, x6); \
vpxor128(x2, x7, x7); /* note: high and low parts swapped */ \
\
/* Add key material and result to CD (x becomes new CD) */ \
\
vpshufb128(bcast[7], t0, t7); \
vpshufb128(bcast[6], t0, t6); \
vpshufb128(bcast[5], t0, t5); \
vpshufb128(bcast[4], t0, t4); \
vpshufb128(bcast[3], t0, t3); \
vpshufb128(bcast[2], t0, t2); \
vpshufb128(bcast[1], t0, t1); \
\
vpxor128(t3, x4, x4); \
vpxor128(mem_cd[0], x4, x4); \
\
load_zero(t3); \
vpshufb128(t3, t0, t0); \
\
vpxor128(t2, x5, x5); \
vpxor128(mem_cd[1], x5, x5); \
\
vpxor128(t1, x6, x6); \
vpxor128(mem_cd[2], x6, x6); \
\
vpxor128(t0, x7, x7); \
vpxor128(mem_cd[3], x7, x7); \
\
vpxor128(t7, x0, x0); \
vpxor128(mem_cd[4], x0, x0); \
\
vpxor128(t6, x1, x1); \
vpxor128(mem_cd[5], x1, x1); \
\
vpxor128(t5, x2, x2); \
vpxor128(mem_cd[6], x2, x2); \
\
vpxor128(t4, x3, x3); \
vpxor128(mem_cd[7], x3, x3);
/*
* IN/OUT:
* x0..x7: byte-sliced AB state preloaded
* mem_ab: byte-sliced AB state in memory
* mem_cb: byte-sliced CD state in memory
*/
#define two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, i, dir, store_ab) \
roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_cd, ctx->key_table[(i)]); \
\
vmovdqa128(x4, mem_cd[0]); \
vmovdqa128(x5, mem_cd[1]); \
vmovdqa128(x6, mem_cd[2]); \
vmovdqa128(x7, mem_cd[3]); \
vmovdqa128(x0, mem_cd[4]); \
vmovdqa128(x1, mem_cd[5]); \
vmovdqa128(x2, mem_cd[6]); \
vmovdqa128(x3, mem_cd[7]); \
\
roundsm16(x4, x5, x6, x7, x0, x1, x2, x3, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, ctx->key_table[(i) + (dir)]); \
\
store_ab(x0, x1, x2, x3, x4, x5, x6, x7, mem_ab);
#define dummy_store(x0, x1, x2, x3, x4, x5, x6, x7, mem_ab) /* do nothing */
#define store_ab_state(x0, x1, x2, x3, x4, x5, x6, x7, mem_ab) \
/* Store new AB state */ \
vmovdqa128(x0, mem_ab[0]); \
vmovdqa128(x1, mem_ab[1]); \
vmovdqa128(x2, mem_ab[2]); \
vmovdqa128(x3, mem_ab[3]); \
vmovdqa128(x4, mem_ab[4]); \
vmovdqa128(x5, mem_ab[5]); \
vmovdqa128(x6, mem_ab[6]); \
vmovdqa128(x7, mem_ab[7]);
#define enc_rounds16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, i) \
two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, (i) + 2, 1, store_ab_state); \
two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, (i) + 4, 1, store_ab_state); \
two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, (i) + 6, 1, dummy_store);
#define dec_rounds16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, i) \
two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, (i) + 7, -1, store_ab_state); \
two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, (i) + 5, -1, store_ab_state); \
two_roundsm16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd, (i) + 3, -1, dummy_store);
#define LE64_LO32(x) ((x) & 0xffffffffU)
#define LE64_HI32(x) ((x >> 32) & 0xffffffffU)
/*
* IN:
* v0..3: byte-sliced 32-bit integers
* OUT:
* v0..3: (IN <<< 1)
*/
#define rol32_1_16(v0, v1, v2, v3, t0, t1, t2, zero) \
if_vpsrlb128(vpsrlb128(7, v0, t0)); \
if_not_vpsrlb128(vpcmpgtb128(v0, zero, t0)); \
vpaddb128(v0, v0, v0); \
if_not_vpsrlb128(vpabsb128(t0, t0)); \
\
if_vpsrlb128(vpsrlb128(7, v1, t1)); \
if_not_vpsrlb128(vpcmpgtb128(v1, zero, t1)); \
vpaddb128(v1, v1, v1); \
if_not_vpsrlb128(vpabsb128(t1, t1)); \
\
if_vpsrlb128(vpsrlb128(7, v2, t2)); \
if_not_vpsrlb128(vpcmpgtb128(v2, zero, t2)); \
vpaddb128(v2, v2, v2); \
if_not_vpsrlb128(vpabsb128(t2, t2)); \
\
vpor128(t0, v1, v1); \
\
if_vpsrlb128(vpsrlb128(7, v3, t0)); \
if_not_vpsrlb128(vpcmpgtb128(v3, zero, t0)); \
vpaddb128(v3, v3, v3); \
if_not_vpsrlb128(vpabsb128(t0, t0)); \
\
vpor128(t1, v2, v2); \
vpor128(t2, v3, v3); \
vpor128(t0, v0, v0);
/*
* IN:
* r: byte-sliced AB state in memory
* l: byte-sliced CD state in memory
* OUT:
* x0..x7: new byte-sliced CD state
*/
#define fls16(l, l0, l1, l2, l3, l4, l5, l6, l7, r, t0, t1, t2, t3, tt0, \
tt1, tt2, tt3, kl, kr) \
/* \
* t0 = kll; \
* t0 &= ll; \
* lr ^= rol32(t0, 1); \
*/ \
load_zero(tt0); \
vmovd128(LE64_LO32(*(kl)), t0); \
vpshufb128(tt0, t0, t3); \
vpshufb128(bcast[1], t0, t2); \
vpshufb128(bcast[2], t0, t1); \
vpshufb128(bcast[3], t0, t0); \
\
vpand128(l0, t0, t0); \
vpand128(l1, t1, t1); \
vpand128(l2, t2, t2); \
vpand128(l3, t3, t3); \
\
rol32_1_16(t3, t2, t1, t0, tt1, tt2, tt3, tt0); \
\
vpxor128(l4, t0, l4); \
vmovdqa128(l4, l[4]); \
vpxor128(l5, t1, l5); \
vmovdqa128(l5, l[5]); \
vpxor128(l6, t2, l6); \
vmovdqa128(l6, l[6]); \
vpxor128(l7, t3, l7); \
vmovdqa128(l7, l[7]); \
\
/* \
* t2 = krr; \
* t2 |= rr; \
* rl ^= t2; \
*/ \
\
vmovd128(LE64_HI32(*(kr)), t0); \
vpshufb128(tt0, t0, t3); \
vpshufb128(bcast[1], t0, t2); \
vpshufb128(bcast[2], t0, t1); \
vpshufb128(bcast[3], t0, t0); \
\
vpor128(r[4], t0, t0); \
vpor128(r[5], t1, t1); \
vpor128(r[6], t2, t2); \
vpor128(r[7], t3, t3); \
\
vpxor128(r[0], t0, t0); \
vpxor128(r[1], t1, t1); \
vpxor128(r[2], t2, t2); \
vpxor128(r[3], t3, t3); \
vmovdqa128(t0, r[0]); \
vmovdqa128(t1, r[1]); \
vmovdqa128(t2, r[2]); \
vmovdqa128(t3, r[3]); \
\
/* \
* t2 = krl; \
* t2 &= rl; \
* rr ^= rol32(t2, 1); \
*/ \
vmovd128(LE64_LO32(*(kr)), t0); \
vpshufb128(tt0, t0, t3); \
vpshufb128(bcast[1], t0, t2); \
vpshufb128(bcast[2], t0, t1); \
vpshufb128(bcast[3], t0, t0); \
\
vpand128(r[0], t0, t0); \
vpand128(r[1], t1, t1); \
vpand128(r[2], t2, t2); \
vpand128(r[3], t3, t3); \
\
rol32_1_16(t3, t2, t1, t0, tt1, tt2, tt3, tt0); \
\
vpxor128(r[4], t0, t0); \
vpxor128(r[5], t1, t1); \
vpxor128(r[6], t2, t2); \
vpxor128(r[7], t3, t3); \
vmovdqa128(t0, r[4]); \
vmovdqa128(t1, r[5]); \
vmovdqa128(t2, r[6]); \
vmovdqa128(t3, r[7]); \
\
/* \
* t0 = klr; \
* t0 |= lr; \
* ll ^= t0; \
*/ \
\
vmovd128(LE64_HI32(*(kl)), t0); \
vpshufb128(tt0, t0, t3); \
vpshufb128(bcast[1], t0, t2); \
vpshufb128(bcast[2], t0, t1); \
vpshufb128(bcast[3], t0, t0); \
\
vpor128(l4, t0, t0); \
vpor128(l5, t1, t1); \
vpor128(l6, t2, t2); \
vpor128(l7, t3, t3); \
\
vpxor128(l0, t0, l0); \
vmovdqa128(l0, l[0]); \
vpxor128(l1, t1, l1); \
vmovdqa128(l1, l[1]); \
vpxor128(l2, t2, l2); \
vmovdqa128(l2, l[2]); \
vpxor128(l3, t3, l3); \
vmovdqa128(l3, l[3]);
#define byteslice_16x16b_fast(a0, b0, c0, d0, a1, b1, c1, d1, a2, b2, c2, d2, \
a3, b3, c3, d3, st0, st1) \
vmovdqa128(d2, st0); \
vmovdqa128(d3, st1); \
transpose_4x4(a0, a1, a2, a3, d2, d3); \
transpose_4x4(b0, b1, b2, b3, d2, d3); \
vmovdqa128(st0, d2); \
vmovdqa128(st1, d3); \
\
vmovdqa128(a0, st0); \
vmovdqa128(a1, st1); \
transpose_4x4(c0, c1, c2, c3, a0, a1); \
transpose_4x4(d0, d1, d2, d3, a0, a1); \
\
vmovdqa128(shufb_16x16b_stack, a0); \
vmovdqa128(st1, a1); \
vpshufb128(a0, a2, a2); \
vpshufb128(a0, a3, a3); \
vpshufb128(a0, b0, b0); \
vpshufb128(a0, b1, b1); \
vpshufb128(a0, b2, b2); \
vpshufb128(a0, b3, b3); \
vpshufb128(a0, a1, a1); \
vpshufb128(a0, c0, c0); \
vpshufb128(a0, c1, c1); \
vpshufb128(a0, c2, c2); \
vpshufb128(a0, c3, c3); \
vpshufb128(a0, d0, d0); \
vpshufb128(a0, d1, d1); \
vpshufb128(a0, d2, d2); \
vpshufb128(a0, d3, d3); \
vmovdqa128(d3, st1); \
vmovdqa128(st0, d3); \
vpshufb128(a0, d3, a0); \
vmovdqa128(d2, st0); \
\
transpose_4x4(a0, b0, c0, d0, d2, d3); \
transpose_4x4(a1, b1, c1, d1, d2, d3); \
vmovdqa128(st0, d2); \
vmovdqa128(st1, d3); \
\
vmovdqa128(b0, st0); \
vmovdqa128(b1, st1); \
transpose_4x4(a2, b2, c2, d2, b0, b1); \
transpose_4x4(a3, b3, c3, d3, b0, b1); \
vmovdqa128(st0, b0); \
vmovdqa128(st1, b1); \
/* does not adjust output bytes inside vectors */
/* load blocks to registers and apply pre-whitening */
#define inpack16_pre(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, rio, key) \
vmovq128((key), x0); \
vpshufb128(pack_bswap_stack, x0, x0); \
\
vpxor128_memld((rio) + 0 * 16, x0, y7); \
vpxor128_memld((rio) + 1 * 16, x0, y6); \
vpxor128_memld((rio) + 2 * 16, x0, y5); \
vpxor128_memld((rio) + 3 * 16, x0, y4); \
vpxor128_memld((rio) + 4 * 16, x0, y3); \
vpxor128_memld((rio) + 5 * 16, x0, y2); \
vpxor128_memld((rio) + 6 * 16, x0, y1); \
vpxor128_memld((rio) + 7 * 16, x0, y0); \
vpxor128_memld((rio) + 8 * 16, x0, x7); \
vpxor128_memld((rio) + 9 * 16, x0, x6); \
vpxor128_memld((rio) + 10 * 16, x0, x5); \
vpxor128_memld((rio) + 11 * 16, x0, x4); \
vpxor128_memld((rio) + 12 * 16, x0, x3); \
vpxor128_memld((rio) + 13 * 16, x0, x2); \
vpxor128_memld((rio) + 14 * 16, x0, x1); \
vpxor128_memld((rio) + 15 * 16, x0, x0);
/* byteslice pre-whitened blocks and store to temporary memory */
#define inpack16_post(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, mem_ab, mem_cd) \
byteslice_16x16b_fast(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, \
y4, y5, y6, y7, mem_ab[0], mem_cd[0]); \
\
vmovdqa128(x0, mem_ab[0]); \
vmovdqa128(x1, mem_ab[1]); \
vmovdqa128(x2, mem_ab[2]); \
vmovdqa128(x3, mem_ab[3]); \
vmovdqa128(x4, mem_ab[4]); \
vmovdqa128(x5, mem_ab[5]); \
vmovdqa128(x6, mem_ab[6]); \
vmovdqa128(x7, mem_ab[7]); \
vmovdqa128(y0, mem_cd[0]); \
vmovdqa128(y1, mem_cd[1]); \
vmovdqa128(y2, mem_cd[2]); \
vmovdqa128(y3, mem_cd[3]); \
vmovdqa128(y4, mem_cd[4]); \
vmovdqa128(y5, mem_cd[5]); \
vmovdqa128(y6, mem_cd[6]); \
vmovdqa128(y7, mem_cd[7]);
/* de-byteslice, apply post-whitening and store blocks */
#define outunpack16(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, \
y5, y6, y7, key, stack_tmp0, stack_tmp1) \
byteslice_16x16b_fast(y0, y4, x0, x4, y1, y5, x1, x5, y2, y6, x2, x6, \
y3, y7, x3, x7, stack_tmp0, stack_tmp1); \
\
vmovdqa128(x0, stack_tmp0); \
\
vmovq128((key), x0); \
vpshufb128(pack_bswap_stack, x0, x0); \
\
vpxor128(x0, y7, y7); \
vpxor128(x0, y6, y6); \
vpxor128(x0, y5, y5); \
vpxor128(x0, y4, y4); \
vpxor128(x0, y3, y3); \
vpxor128(x0, y2, y2); \
vpxor128(x0, y1, y1); \
vpxor128(x0, y0, y0); \
vpxor128(x0, x7, x7); \
vpxor128(x0, x6, x6); \
vpxor128(x0, x5, x5); \
vpxor128(x0, x4, x4); \
vpxor128(x0, x3, x3); \
vpxor128(x0, x2, x2); \
vpxor128(x0, x1, x1); \
vpxor128(stack_tmp0, x0, x0);
#define write_output(x0, x1, x2, x3, x4, x5, x6, x7, y0, y1, y2, y3, y4, y5, \
y6, y7, rio) \
vmovdqu128_memst(x0, (rio) + 0 * 16); \
vmovdqu128_memst(x1, (rio) + 1 * 16); \
vmovdqu128_memst(x2, (rio) + 2 * 16); \
vmovdqu128_memst(x3, (rio) + 3 * 16); \
vmovdqu128_memst(x4, (rio) + 4 * 16); \
vmovdqu128_memst(x5, (rio) + 5 * 16); \
vmovdqu128_memst(x6, (rio) + 6 * 16); \
vmovdqu128_memst(x7, (rio) + 7 * 16); \
vmovdqu128_memst(y0, (rio) + 8 * 16); \
vmovdqu128_memst(y1, (rio) + 9 * 16); \
vmovdqu128_memst(y2, (rio) + 10 * 16); \
vmovdqu128_memst(y3, (rio) + 11 * 16); \
vmovdqu128_memst(y4, (rio) + 12 * 16); \
vmovdqu128_memst(y5, (rio) + 13 * 16); \
vmovdqu128_memst(y6, (rio) + 14 * 16); \
vmovdqu128_memst(y7, (rio) + 15 * 16);
/**********************************************************************
macros for defining constant vectors
**********************************************************************/
#define SWAP_LE64(x) (x)
#define M128I_BYTE(a0, a1, a2, a3, a4, a5, a6, a7, b0, b1, b2, b3, b4, b5, b6, b7) \
{ \
SWAP_LE64((((a0) & 0xffULL) << 0) | \
(((a1) & 0xffULL) << 8) | \
(((a2) & 0xffULL) << 16) | \
(((a3) & 0xffULL) << 24) | \
(((a4) & 0xffULL) << 32) | \
(((a5) & 0xffULL) << 40) | \
(((a6) & 0xffULL) << 48) | \
(((a7) & 0xffULL) << 56)), \
SWAP_LE64((((b0) & 0xffULL) << 0) | \
(((b1) & 0xffULL) << 8) | \
(((b2) & 0xffULL) << 16) | \
(((b3) & 0xffULL) << 24) | \
(((b4) & 0xffULL) << 32) | \
(((b5) & 0xffULL) << 40) | \
(((b6) & 0xffULL) << 48) | \
(((b7) & 0xffULL) << 56)) \
}
#define M128I_U32(a0, a1, b0, b1) \
{ \
SWAP_LE64((((a0) & 0xffffffffULL) << 0) | \
(((a1) & 0xffffffffULL) << 32)), \
SWAP_LE64((((b0) & 0xffffffffULL) << 0) | \
(((b1) & 0xffffffffULL) << 32)) \
}
#define M128I_REP16(x) { (0x0101010101010101ULL * (x)), (0x0101010101010101ULL * (x)) }
#define SHUFB_BYTES(idx) \
(((0 + (idx)) << 0) | ((4 + (idx)) << 8) | \
((8 + (idx)) << 16) | ((12 + (idx)) << 24))
typedef uint64_t uint64_unaligned_t __attribute__((aligned(1), may_alias));
static const __m128i shufb_16x16b =
M128I_U32(SHUFB_BYTES(0), SHUFB_BYTES(1), SHUFB_BYTES(2), SHUFB_BYTES(3));
static const __m128i pack_bswap =
M128I_U32(0x00010203, 0x04050607, 0x0f0f0f0f, 0x0f0f0f0f);
static const __m128i bcast[8] =
{
M128I_REP16(0), M128I_REP16(1), M128I_REP16(2), M128I_REP16(3),
M128I_REP16(4), M128I_REP16(5), M128I_REP16(6), M128I_REP16(7)
};
/*
* pre-SubByte transform
*
* pre-lookup for sbox1, sbox2, sbox3:
* swap_bitendianness(
* isom_map_camellia_to_aes(
* camellia_f(
* swap_bitendianess(in)
* )
* )
* )
*
* (note: '⊕ 0xc5' inside camellia_f())
*/
static const __m128i pre_tf_lo_s1 =
M128I_BYTE(0x45, 0xe8, 0x40, 0xed, 0x2e, 0x83, 0x2b, 0x86,
0x4b, 0xe6, 0x4e, 0xe3, 0x20, 0x8d, 0x25, 0x88);
static const __m128i pre_tf_hi_s1 =
M128I_BYTE(0x00, 0x51, 0xf1, 0xa0, 0x8a, 0xdb, 0x7b, 0x2a,
0x09, 0x58, 0xf8, 0xa9, 0x83, 0xd2, 0x72, 0x23);
/*
* pre-SubByte transform
*
* pre-lookup for sbox4:
* swap_bitendianness(
* isom_map_camellia_to_aes(
* camellia_f(
* swap_bitendianess(in <<< 1)
* )
* )
* )
*
* (note: '⊕ 0xc5' inside camellia_f())
*/
static const __m128i pre_tf_lo_s4 =
M128I_BYTE(0x45, 0x40, 0x2e, 0x2b, 0x4b, 0x4e, 0x20, 0x25,
0x14, 0x11, 0x7f, 0x7a, 0x1a, 0x1f, 0x71, 0x74);
static const __m128i pre_tf_hi_s4 =
M128I_BYTE(0x00, 0xf1, 0x8a, 0x7b, 0x09, 0xf8, 0x83, 0x72,
0xad, 0x5c, 0x27, 0xd6, 0xa4, 0x55, 0x2e, 0xdf);
/*
* post-SubByte transform
*
* post-lookup for sbox1, sbox4:
* swap_bitendianness(
* camellia_h(
* isom_map_aes_to_camellia(
* swap_bitendianness(
* aes_inverse_affine_transform(in)
* )
* )
* )
* )
*
* (note: '⊕ 0x6e' inside camellia_h())
*/
static const __m128i post_tf_lo_s1 =
M128I_BYTE(0x3c, 0xcc, 0xcf, 0x3f, 0x32, 0xc2, 0xc1, 0x31,
0xdc, 0x2c, 0x2f, 0xdf, 0xd2, 0x22, 0x21, 0xd1);
static const __m128i post_tf_hi_s1 =
M128I_BYTE(0x00, 0xf9, 0x86, 0x7f, 0xd7, 0x2e, 0x51, 0xa8,
0xa4, 0x5d, 0x22, 0xdb, 0x73, 0x8a, 0xf5, 0x0c);
/*
* post-SubByte transform
*
* post-lookup for sbox2:
* swap_bitendianness(
* camellia_h(
* isom_map_aes_to_camellia(
* swap_bitendianness(
* aes_inverse_affine_transform(in)
* )
* )
* )
* ) <<< 1
*
* (note: '⊕ 0x6e' inside camellia_h())
*/
static const __m128i post_tf_lo_s2 =
M128I_BYTE(0x78, 0x99, 0x9f, 0x7e, 0x64, 0x85, 0x83, 0x62,
0xb9, 0x58, 0x5e, 0xbf, 0xa5, 0x44, 0x42, 0xa3);
static const __m128i post_tf_hi_s2 =
M128I_BYTE(0x00, 0xf3, 0x0d, 0xfe, 0xaf, 0x5c, 0xa2, 0x51,
0x49, 0xba, 0x44, 0xb7, 0xe6, 0x15, 0xeb, 0x18);
/*
* post-SubByte transform
*
* post-lookup for sbox3:
* swap_bitendianness(
* camellia_h(
* isom_map_aes_to_camellia(
* swap_bitendianness(
* aes_inverse_affine_transform(in)
* )
* )
* )