Skip to content

Commit a49e9e0

Browse files
committed
Switch VAES cfg flags from opt-out to opt-in
1 parent d883f21 commit a49e9e0

File tree

4 files changed

+61
-62
lines changed

4 files changed

+61
-62
lines changed

.github/workflows/aes.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ jobs:
6969
env:
7070
CARGO_INCREMENTAL: 0
7171
RUSTDOCFLAGS: "-C target-feature=+aes,+ssse3"
72-
RUSTFLAGS: "-Dwarnings -C target-feature=+aes,+ssse3 --cfg aes_avx512_disable --cfg aes_avx256_disable"
72+
RUSTFLAGS: "-Dwarnings -C target-feature=+aes,+ssse3"
7373
strategy:
7474
matrix:
7575
include:
@@ -103,7 +103,7 @@ jobs:
103103
runs-on: ubuntu-latest
104104
env:
105105
CARGO_INCREMENTAL: 0
106-
RUSTFLAGS: "-Dwarnings --cfg aes_avx512_disable"
106+
RUSTFLAGS: "-Dwarnings --cfg aes_avx256"
107107
strategy:
108108
matrix:
109109
include:
@@ -140,6 +140,7 @@ jobs:
140140
runs-on: ubuntu-latest
141141
env:
142142
CARGO_INCREMENTAL: 0
143+
RUSTFLAGS: "-Dwarnings --cfg aes_avx512"
143144
strategy:
144145
matrix:
145146
include:

aes/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ hazmat = [] # Expose cryptographically hazardous APIs
3131

3232
[lints.rust.unexpected_cfgs]
3333
level = "warn"
34-
check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)", "cfg(aes_avx256_disable)", "cfg(aes_avx512_disable)"]
34+
check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)", "cfg(aes_avx256)", "cfg(aes_avx512)"]
3535

3636
[package.metadata.docs.rs]
3737
all-features = true

aes/src/lib.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,12 @@
4545
//! used or passing `RUSTFLAGS=-Ctarget-feature=+aes,+avx512f,+ssse3,+vaes`
4646
//! will ensure that AESNI and VAES are always used.
4747
//!
48+
//! Note: Enabling VAES256 or VAES512 still requires specifying `--cfg
49+
//! aes_avx256` or `--cfg aes_avx512` explicitly.
50+
//!
4851
//! Programs built in this manner will crash with an illegal instruction on
4952
//! CPUs which do not have AES-NI and VAES enabled.
5053
//!
51-
//! Note: It is possible to disable the use of AVX512 for the VAES backend
52-
//! and limiting it to AVX (256-bit) by specifying `--cfg aes_avx512_disable`.
53-
//! For CPUs which support VAES but not AVX512, the 256-bit VAES backend will
54-
//! be selected automatically without needing to specify this flag.
55-
//!
5654
//! Note: runtime detection is not possible on SGX targets. Please use the
5755
//! aforementioned `RUSTFLAGS` to leverage AES-NI and VAES on these targets.
5856
//!
@@ -97,19 +95,19 @@
9795
//! }
9896
//! ```
9997
//!
100-
//! For implementation of block cipher modes of operation see
101-
//! [`block-modes`] repository.
98+
//! For implementation of block cipher modes of operation see [`block-modes`]
99+
//! repository.
102100
//!
103101
//! # Configuration Flags
104102
//!
105103
//! You can modify crate using the following configuration flags:
106104
//!
107105
//! - `aes_force_soft`: force software implementation.
108-
//! - `aes_compact`: reduce code size at the cost of slower performance
109-
//! (affects only software backend).
106+
//! - `aes_compact`: reduce code size at the cost of slower performance (affects
107+
//! only software backend).
110108
//!
111-
//! It can be enabled using `RUSTFLAGS` environmental variable
112-
//! (e.g. `RUSTFLAGS="--cfg aes_compact"`) or by modifying `.cargo/config`.
109+
//! It can be enabled using `RUSTFLAGS` environmental variable (e.g.
110+
//! `RUSTFLAGS="--cfg aes_compact"`) or by modifying `.cargo/config`.
113111
//!
114112
//! [AES]: https://en.wikipedia.org/wiki/Advanced_Encryption_Standard
115113
//! [fixslicing]: https://eprint.iacr.org/2020/1123.pdf

aes/src/x86.rs

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
pub(crate) mod ni;
2-
#[cfg(target_arch = "x86_64")]
2+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
33
pub(crate) mod vaes256;
4-
#[cfg(target_arch = "x86_64")]
4+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
55
pub(crate) mod vaes512;
66

77
#[cfg(target_arch = "x86")]
@@ -11,26 +11,24 @@ use core::arch::x86_64 as arch;
1111

1212
use self::arch::*;
1313
use crate::Block;
14+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
15+
use cipher::consts::U64;
1416
use cipher::{
1517
AlgorithmName, BlockCipherDecBackend, BlockCipherDecClosure, BlockCipherDecrypt,
1618
BlockCipherEncBackend, BlockCipherEncClosure, BlockCipherEncrypt, BlockSizeUser, InOut, Key,
1719
KeyInit, KeySizeUser, ParBlocksSizeUser,
1820
consts::{U9, U16, U24, U32},
1921
crypto_common::WeakKeyError,
2022
};
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)))]
2826
use core::cell::OnceCell;
2927
use core::fmt;
3028

31-
#[cfg(target_arch = "x86_64")]
29+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
3230
pub(crate) type Block30 = Array<Block, U30>;
33-
#[cfg(target_arch = "x86_64")]
31+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
3432
pub(crate) type Block64 = Array<Block, U64>;
3533

3634
pub(crate) mod features {
@@ -41,81 +39,81 @@ pub(crate) mod features {
4139
pub(crate) mod aes {
4240
pub use super::features_aes::*;
4341
}
44-
#[cfg(target_arch = "x86_64")]
42+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
4543
pub(crate) mod avx {
4644
pub use super::features_avx::*;
4745
}
48-
#[cfg(target_arch = "x86_64")]
46+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
4947
pub(crate) mod avx512f {
5048
pub use super::features_avx512f::*;
5149
}
52-
#[cfg(target_arch = "x86_64")]
50+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
5351
pub(crate) mod vaes {
5452
pub use super::features_vaes::*;
5553
}
5654
}
5755

5856
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)))]
6058
type Simd256RoundKeys<const ROUNDS: usize> = [__m256i; ROUNDS];
61-
#[cfg(target_arch = "x86_64")]
59+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
6260
type Simd512RoundKeys<const ROUNDS: usize> = [__m512i; ROUNDS];
6361

6462
#[derive(Clone)]
6563
enum Backend {
6664
Ni,
67-
#[cfg(target_arch = "x86_64")]
65+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
6866
Vaes256,
69-
#[cfg(target_arch = "x86_64")]
67+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
7068
Vaes512,
7169
}
7270

7371
#[derive(Clone, Copy)]
7472
struct Features {
75-
#[cfg(target_arch = "x86_64")]
73+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
7674
avx: self::features::avx::InitToken,
77-
#[cfg(target_arch = "x86_64")]
75+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
7876
avx512f: self::features::avx512f::InitToken,
79-
#[cfg(target_arch = "x86_64")]
77+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
8078
vaes: self::features::vaes::InitToken,
8179
}
8280

8381
impl Features {
8482
fn new() -> Self {
8583
Self {
86-
#[cfg(target_arch = "x86_64")]
84+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
8785
avx: self::features::avx::init(),
88-
#[cfg(target_arch = "x86_64")]
86+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
8987
avx512f: self::features::avx512f::init(),
90-
#[cfg(target_arch = "x86_64")]
88+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
9189
vaes: self::features::vaes::init(),
9290
}
9391
}
9492

95-
#[cfg(target_arch = "x86_64")]
93+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
9694
fn has_vaes256(&self) -> bool {
9795
#[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() {
9997
return true;
10098
}
10199
false
102100
}
103101

104-
#[cfg(target_arch = "x86_64")]
102+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
105103
fn has_vaes512(&self) -> bool {
106104
#[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() {
108106
return true;
109107
}
110108
false
111109
}
112110

113111
fn dispatch(&self) -> Backend {
114-
#[cfg(target_arch = "x86_64")]
112+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
115113
if self.has_vaes512() {
116114
return self::Backend::Vaes512;
117115
}
118-
#[cfg(target_arch = "x86_64")]
116+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
119117
if self.has_vaes256() {
120118
return self::Backend::Vaes256;
121119
}
@@ -141,33 +139,35 @@ macro_rules! define_aes_impl {
141139
pub(crate) struct Ni<'a> {
142140
pub(crate) keys: &'a Simd128RoundKeys<$rounds>,
143141
}
144-
#[cfg(target_arch = "x86_64")]
142+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
145143
impl<'a> Ni<'a> {
146144
pub const fn par_blocks(&self) -> usize {
147145
<Self as ParBlocksSizeUser>::ParBlocksSize::USIZE
148146
}
149147
}
150-
#[cfg(target_arch = "x86_64")]
148+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
151149
impl<'a> From<&Vaes256<'a>> for Ni<'a> {
152150
fn from(backend: &Vaes256<'a>) -> Self {
153151
Self { keys: backend.keys }
154152
}
155153
}
156154

155+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
157156
#[derive(Clone)]
158-
#[cfg(target_arch = "x86_64")]
159157
pub(crate) struct Vaes256<'a> {
158+
#[allow(unused)] // TODO: remove once cfg flags are removed
160159
pub(crate) features: Features,
161160
pub(crate) keys: &'a Simd128RoundKeys<$rounds>,
162161
pub(crate) simd_256_keys: OnceCell<Simd256RoundKeys<$rounds>>,
163162
}
164-
#[cfg(target_arch = "x86_64")]
163+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
165164
impl<'a> Vaes256<'a> {
165+
#[allow(unused)] // TODO: remove once cfg flags are removed
166166
pub const fn par_blocks(&self) -> usize {
167167
<Self as ParBlocksSizeUser>::ParBlocksSize::USIZE
168168
}
169169
}
170-
#[cfg(target_arch = "x86_64")]
170+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
171171
impl<'a> From<&Vaes512<'a>> for Vaes256<'a> {
172172
fn from(backend: &Vaes512<'a>) -> Self {
173173
Self {
@@ -178,7 +178,7 @@ macro_rules! define_aes_impl {
178178
}
179179
}
180180

181-
#[cfg(target_arch = "x86_64")]
181+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
182182
pub(crate) struct Vaes512<'a> {
183183
pub(crate) features: Features,
184184
pub(crate) keys: &'a Simd128RoundKeys<$rounds>,
@@ -314,13 +314,13 @@ macro_rules! define_aes_impl {
314314
let keys = &self.keys;
315315
match features.dispatch() {
316316
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)))]
318318
self::Backend::Vaes256 => f.call(&mut $name_backend::Vaes256 {
319319
features,
320320
keys,
321321
simd_256_keys: OnceCell::new(),
322322
}),
323-
#[cfg(target_arch = "x86_64")]
323+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
324324
self::Backend::Vaes512 => f.call(&mut $name_backend::Vaes512 {
325325
features,
326326
keys,
@@ -406,13 +406,13 @@ macro_rules! define_aes_impl {
406406
let keys = &self.keys;
407407
match features.dispatch() {
408408
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)))]
410410
self::Backend::Vaes256 => f.call(&mut $name_backend::Vaes256 {
411411
features,
412412
keys,
413413
simd_256_keys: OnceCell::new(),
414414
}),
415-
#[cfg(target_arch = "x86_64")]
415+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
416416
self::Backend::Vaes512 => f.call(&mut $name_backend::Vaes512 {
417417
features,
418418
keys,
@@ -437,23 +437,23 @@ macro_rules! define_aes_impl {
437437
impl<'a> BlockSizeUser for $name_backend::Ni<'a> {
438438
type BlockSize = U16;
439439
}
440-
#[cfg(target_arch = "x86_64")]
440+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
441441
impl<'a> BlockSizeUser for $name_backend::Vaes256<'a> {
442442
type BlockSize = U16;
443443
}
444-
#[cfg(target_arch = "x86_64")]
444+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
445445
impl<'a> BlockSizeUser for $name_backend::Vaes512<'a> {
446446
type BlockSize = U16;
447447
}
448448

449449
impl<'a> ParBlocksSizeUser for $name_backend::Ni<'a> {
450450
type ParBlocksSize = U9;
451451
}
452-
#[cfg(target_arch = "x86_64")]
452+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
453453
impl<'a> ParBlocksSizeUser for $name_backend::Vaes256<'a> {
454454
type ParBlocksSize = U30;
455455
}
456-
#[cfg(target_arch = "x86_64")]
456+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
457457
impl<'a> ParBlocksSizeUser for $name_backend::Vaes512<'a> {
458458
type ParBlocksSize = U64;
459459
}
@@ -472,7 +472,7 @@ macro_rules! define_aes_impl {
472472
}
473473
}
474474
}
475-
#[cfg(target_arch = "x86_64")]
475+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
476476
impl<'a> BlockCipherEncBackend for $name_backend::Vaes256<'a> {
477477
#[inline]
478478
fn encrypt_block(&self, block: InOut<'_, '_, Block>) {
@@ -514,7 +514,7 @@ macro_rules! define_aes_impl {
514514
}
515515
}
516516
}
517-
#[cfg(target_arch = "x86_64")]
517+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
518518
impl<'a> BlockCipherEncBackend for $name_backend::Vaes512<'a> {
519519
#[inline]
520520
fn encrypt_block(&self, block: InOut<'_, '_, Block>) {
@@ -582,7 +582,7 @@ macro_rules! define_aes_impl {
582582
}
583583
}
584584
}
585-
#[cfg(target_arch = "x86_64")]
585+
#[cfg(all(target_arch = "x86_64", any(aes_avx256, aes_avx512)))]
586586
impl<'a> BlockCipherDecBackend for $name_backend::Vaes256<'a> {
587587
#[inline]
588588
fn decrypt_block(&self, block: InOut<'_, '_, Block>) {
@@ -624,7 +624,7 @@ macro_rules! define_aes_impl {
624624
}
625625
}
626626
}
627-
#[cfg(target_arch = "x86_64")]
627+
#[cfg(all(target_arch = "x86_64", aes_avx512))]
628628
impl<'a> BlockCipherDecBackend for $name_backend::Vaes512<'a> {
629629
#[inline]
630630
fn decrypt_block(&self, block: InOut<'_, '_, Block>) {

0 commit comments

Comments
 (0)