1
1
pub ( crate ) mod ni;
2
- #[ cfg( target_arch = "x86_64" ) ]
2
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
3
3
pub ( crate ) mod vaes256;
4
- #[ cfg( target_arch = "x86_64" ) ]
4
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
5
5
pub ( crate ) mod vaes512;
6
6
7
7
#[ cfg( target_arch = "x86" ) ]
@@ -11,26 +11,24 @@ use core::arch::x86_64 as arch;
11
11
12
12
use self :: arch:: * ;
13
13
use crate :: Block ;
14
+ #[ cfg( all( target_arch = "x86_64" , aes_avx512) ) ]
15
+ use cipher:: consts:: U64 ;
14
16
use cipher:: {
15
17
AlgorithmName , BlockCipherDecBackend , BlockCipherDecClosure , BlockCipherDecrypt ,
16
18
BlockCipherEncBackend , BlockCipherEncClosure , BlockCipherEncrypt , BlockSizeUser , InOut , Key ,
17
19
KeyInit , KeySizeUser , ParBlocksSizeUser ,
18
20
consts:: { U9 , U16 , U24 , U32 } ,
19
21
crypto_common:: WeakKeyError ,
20
22
} ;
21
- #[ cfg( target_arch = "x86_64" ) ]
22
- use cipher:: {
23
- Array , InOutBuf ,
24
- consts:: { U30 , U64 } ,
25
- typenum:: Unsigned ,
26
- } ;
27
- #[ cfg( target_arch = "x86_64" ) ]
23
+ #[ cfg( all( target_arch = "x86_64" , any( aes_avx256, aes_avx512) ) ) ]
24
+ use cipher:: { Array , InOutBuf , consts:: U30 , typenum:: Unsigned } ;
25
+ #[ cfg( all( target_arch = "x86_64" , any( aes_avx256, aes_avx512) ) ) ]
28
26
use core:: cell:: OnceCell ;
29
27
use core:: fmt;
30
28
31
- #[ cfg( target_arch = "x86_64" ) ]
29
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
32
30
pub ( crate ) type Block30 = Array < Block , U30 > ;
33
- #[ cfg( target_arch = "x86_64" ) ]
31
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
34
32
pub ( crate ) type Block64 = Array < Block , U64 > ;
35
33
36
34
pub ( crate ) mod features {
@@ -41,81 +39,81 @@ pub(crate) mod features {
41
39
pub ( crate ) mod aes {
42
40
pub use super :: features_aes:: * ;
43
41
}
44
- #[ cfg( target_arch = "x86_64" ) ]
42
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
45
43
pub ( crate ) mod avx {
46
44
pub use super :: features_avx:: * ;
47
45
}
48
- #[ cfg( target_arch = "x86_64" ) ]
46
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
49
47
pub ( crate ) mod avx512f {
50
48
pub use super :: features_avx512f:: * ;
51
49
}
52
- #[ cfg( target_arch = "x86_64" ) ]
50
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
53
51
pub ( crate ) mod vaes {
54
52
pub use super :: features_vaes:: * ;
55
53
}
56
54
}
57
55
58
56
type Simd128RoundKeys < const ROUNDS : usize > = [ __m128i ; ROUNDS ] ;
59
- #[ cfg( target_arch = "x86_64" ) ]
57
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
60
58
type Simd256RoundKeys < const ROUNDS : usize > = [ __m256i ; ROUNDS ] ;
61
- #[ cfg( target_arch = "x86_64" ) ]
59
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
62
60
type Simd512RoundKeys < const ROUNDS : usize > = [ __m512i ; ROUNDS ] ;
63
61
64
62
#[ derive( Clone ) ]
65
63
enum Backend {
66
64
Ni ,
67
- #[ cfg( target_arch = "x86_64" ) ]
65
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
68
66
Vaes256 ,
69
- #[ cfg( target_arch = "x86_64" ) ]
67
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
70
68
Vaes512 ,
71
69
}
72
70
73
71
#[ derive( Clone , Copy ) ]
74
72
struct Features {
75
- #[ cfg( target_arch = "x86_64" ) ]
73
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
76
74
avx : self :: features:: avx:: InitToken ,
77
- #[ cfg( target_arch = "x86_64" ) ]
75
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
78
76
avx512f : self :: features:: avx512f:: InitToken ,
79
- #[ cfg( target_arch = "x86_64" ) ]
77
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
80
78
vaes : self :: features:: vaes:: InitToken ,
81
79
}
82
80
83
81
impl Features {
84
82
fn new ( ) -> Self {
85
83
Self {
86
- #[ cfg( target_arch = "x86_64" ) ]
84
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
87
85
avx : self :: features:: avx:: init ( ) ,
88
- #[ cfg( target_arch = "x86_64" ) ]
86
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
89
87
avx512f : self :: features:: avx512f:: init ( ) ,
90
- #[ cfg( target_arch = "x86_64" ) ]
88
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
91
89
vaes : self :: features:: vaes:: init ( ) ,
92
90
}
93
91
}
94
92
95
- #[ cfg( target_arch = "x86_64" ) ]
93
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
96
94
fn has_vaes256 ( & self ) -> bool {
97
95
#[ cfg( target_arch = "x86_64" ) ]
98
- if self . vaes . get ( ) && self . avx . get ( ) && ! cfg ! ( aes_avx256_disable ) {
96
+ if cfg ! ( aes_avx256 ) && self . vaes . get ( ) && self . avx . get ( ) {
99
97
return true ;
100
98
}
101
99
false
102
100
}
103
101
104
- #[ cfg( target_arch = "x86_64" ) ]
102
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
105
103
fn has_vaes512 ( & self ) -> bool {
106
104
#[ cfg( target_arch = "x86_64" ) ]
107
- if self . vaes . get ( ) && self . avx512f . get ( ) && ! cfg ! ( aes_avx512_disable ) {
105
+ if cfg ! ( aes_avx512 ) && self . vaes . get ( ) && self . avx512f . get ( ) {
108
106
return true ;
109
107
}
110
108
false
111
109
}
112
110
113
111
fn dispatch ( & self ) -> Backend {
114
- #[ cfg( target_arch = "x86_64" ) ]
112
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
115
113
if self . has_vaes512 ( ) {
116
114
return self :: Backend :: Vaes512 ;
117
115
}
118
- #[ cfg( target_arch = "x86_64" ) ]
116
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
119
117
if self . has_vaes256 ( ) {
120
118
return self :: Backend :: Vaes256 ;
121
119
}
@@ -141,33 +139,35 @@ macro_rules! define_aes_impl {
141
139
pub ( crate ) struct Ni <' a> {
142
140
pub ( crate ) keys: & ' a Simd128RoundKeys <$rounds>,
143
141
}
144
- #[ cfg( target_arch = "x86_64" ) ]
142
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
145
143
impl <' a> Ni <' a> {
146
144
pub const fn par_blocks( & self ) -> usize {
147
145
<Self as ParBlocksSizeUser >:: ParBlocksSize :: USIZE
148
146
}
149
147
}
150
- #[ cfg( target_arch = "x86_64" ) ]
148
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
151
149
impl <' a> From <& Vaes256 <' a>> for Ni <' a> {
152
150
fn from( backend: & Vaes256 <' a>) -> Self {
153
151
Self { keys: backend. keys }
154
152
}
155
153
}
156
154
155
+ #[ cfg( all( target_arch = "x86_64" , any( aes_avx256, aes_avx512) ) ) ]
157
156
#[ derive( Clone ) ]
158
- #[ cfg( target_arch = "x86_64" ) ]
159
157
pub ( crate ) struct Vaes256 <' a> {
158
+ #[ allow( unused) ] // TODO: remove once cfg flags are removed
160
159
pub ( crate ) features: Features ,
161
160
pub ( crate ) keys: & ' a Simd128RoundKeys <$rounds>,
162
161
pub ( crate ) simd_256_keys: OnceCell <Simd256RoundKeys <$rounds>>,
163
162
}
164
- #[ cfg( target_arch = "x86_64" ) ]
163
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
165
164
impl <' a> Vaes256 <' a> {
165
+ #[ allow( unused) ] // TODO: remove once cfg flags are removed
166
166
pub const fn par_blocks( & self ) -> usize {
167
167
<Self as ParBlocksSizeUser >:: ParBlocksSize :: USIZE
168
168
}
169
169
}
170
- #[ cfg( target_arch = "x86_64" ) ]
170
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
171
171
impl <' a> From <& Vaes512 <' a>> for Vaes256 <' a> {
172
172
fn from( backend: & Vaes512 <' a>) -> Self {
173
173
Self {
@@ -178,7 +178,7 @@ macro_rules! define_aes_impl {
178
178
}
179
179
}
180
180
181
- #[ cfg( target_arch = "x86_64" ) ]
181
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
182
182
pub ( crate ) struct Vaes512 <' a> {
183
183
pub ( crate ) features: Features ,
184
184
pub ( crate ) keys: & ' a Simd128RoundKeys <$rounds>,
@@ -314,13 +314,13 @@ macro_rules! define_aes_impl {
314
314
let keys = & self . keys;
315
315
match features. dispatch( ) {
316
316
self :: Backend :: Ni => f. call( & mut $name_backend:: Ni { keys } ) ,
317
- #[ cfg( target_arch = "x86_64" ) ]
317
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
318
318
self :: Backend :: Vaes256 => f. call( & mut $name_backend:: Vaes256 {
319
319
features,
320
320
keys,
321
321
simd_256_keys: OnceCell :: new( ) ,
322
322
} ) ,
323
- #[ cfg( target_arch = "x86_64" ) ]
323
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
324
324
self :: Backend :: Vaes512 => f. call( & mut $name_backend:: Vaes512 {
325
325
features,
326
326
keys,
@@ -406,13 +406,13 @@ macro_rules! define_aes_impl {
406
406
let keys = & self . keys;
407
407
match features. dispatch( ) {
408
408
self :: Backend :: Ni => f. call( & mut $name_backend:: Ni { keys } ) ,
409
- #[ cfg( target_arch = "x86_64" ) ]
409
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
410
410
self :: Backend :: Vaes256 => f. call( & mut $name_backend:: Vaes256 {
411
411
features,
412
412
keys,
413
413
simd_256_keys: OnceCell :: new( ) ,
414
414
} ) ,
415
- #[ cfg( target_arch = "x86_64" ) ]
415
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
416
416
self :: Backend :: Vaes512 => f. call( & mut $name_backend:: Vaes512 {
417
417
features,
418
418
keys,
@@ -437,23 +437,23 @@ macro_rules! define_aes_impl {
437
437
impl <' a> BlockSizeUser for $name_backend:: Ni <' a> {
438
438
type BlockSize = U16 ;
439
439
}
440
- #[ cfg( target_arch = "x86_64" ) ]
440
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
441
441
impl <' a> BlockSizeUser for $name_backend:: Vaes256 <' a> {
442
442
type BlockSize = U16 ;
443
443
}
444
- #[ cfg( target_arch = "x86_64" ) ]
444
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
445
445
impl <' a> BlockSizeUser for $name_backend:: Vaes512 <' a> {
446
446
type BlockSize = U16 ;
447
447
}
448
448
449
449
impl <' a> ParBlocksSizeUser for $name_backend:: Ni <' a> {
450
450
type ParBlocksSize = U9 ;
451
451
}
452
- #[ cfg( target_arch = "x86_64" ) ]
452
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
453
453
impl <' a> ParBlocksSizeUser for $name_backend:: Vaes256 <' a> {
454
454
type ParBlocksSize = U30 ;
455
455
}
456
- #[ cfg( target_arch = "x86_64" ) ]
456
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
457
457
impl <' a> ParBlocksSizeUser for $name_backend:: Vaes512 <' a> {
458
458
type ParBlocksSize = U64 ;
459
459
}
@@ -472,7 +472,7 @@ macro_rules! define_aes_impl {
472
472
}
473
473
}
474
474
}
475
- #[ cfg( target_arch = "x86_64" ) ]
475
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
476
476
impl <' a> BlockCipherEncBackend for $name_backend:: Vaes256 <' a> {
477
477
#[ inline]
478
478
fn encrypt_block( & self , block: InOut <' _, ' _, Block >) {
@@ -514,7 +514,7 @@ macro_rules! define_aes_impl {
514
514
}
515
515
}
516
516
}
517
- #[ cfg( target_arch = "x86_64" ) ]
517
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
518
518
impl <' a> BlockCipherEncBackend for $name_backend:: Vaes512 <' a> {
519
519
#[ inline]
520
520
fn encrypt_block( & self , block: InOut <' _, ' _, Block >) {
@@ -582,7 +582,7 @@ macro_rules! define_aes_impl {
582
582
}
583
583
}
584
584
}
585
- #[ cfg( target_arch = "x86_64" ) ]
585
+ #[ cfg( all ( target_arch = "x86_64" , any ( aes_avx256 , aes_avx512 ) ) ) ]
586
586
impl <' a> BlockCipherDecBackend for $name_backend:: Vaes256 <' a> {
587
587
#[ inline]
588
588
fn decrypt_block( & self , block: InOut <' _, ' _, Block >) {
@@ -624,7 +624,7 @@ macro_rules! define_aes_impl {
624
624
}
625
625
}
626
626
}
627
- #[ cfg( target_arch = "x86_64" ) ]
627
+ #[ cfg( all ( target_arch = "x86_64" , aes_avx512 ) ) ]
628
628
impl <' a> BlockCipherDecBackend for $name_backend:: Vaes512 <' a> {
629
629
#[ inline]
630
630
fn decrypt_block( & self , block: InOut <' _, ' _, Block >) {
0 commit comments