Skip to content

Commit f102c4f

Browse files
authored
aes: consolidate backend configuration under aes_backend (#541)
Consolidates the previous `cfg` options `aes_force_soft`, `aes_avx256`, and `aes_avx512` as `aes_backend` values: - `avx256` - `avx512` - `soft` The `aes_compact` option has been retained as-is so it can be used in conjunction with the other options, though perhaps we can find some other solution for it. This does have the effect of preventing `avx256` and `avx512` from being simultaneously enabled, although I'm not sure that would ever actually make sense?
1 parent f468230 commit f102c4f

File tree

5 files changed

+75
-71
lines changed

5 files changed

+75
-71
lines changed

.github/workflows/aes.yml

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,17 @@ jobs:
4949
cargo build --target ${{ matrix.target }}
5050
cargo build --target ${{ matrix.target }} --features hazmat
5151
- env:
52-
RUSTFLAGS: "-Dwarnings --cfg aes_force_soft"
52+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="soft"'
5353
run: |
5454
cargo build --target ${{ matrix.target }}
5555
cargo build --target ${{ matrix.target }} --features hazmat
5656
- env:
57-
RUSTFLAGS: "-Dwarnings --cfg aes_compact"
57+
RUSTFLAGS: '-Dwarnings --cfg aes_compact'
5858
run: |
5959
cargo build --target ${{ matrix.target }}
6060
cargo build --target ${{ matrix.target }} --features hazmat
6161
- env:
62-
RUSTFLAGS: "-Dwarnings --cfg aes_force_soft --cfg aes_compact"
62+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="soft" --cfg aes_compact'
6363
run: |
6464
cargo build --target ${{ matrix.target }}
6565
cargo build --target ${{ matrix.target }} --features hazmat
@@ -112,7 +112,7 @@ jobs:
112112
include:
113113
- target: x86_64-unknown-linux-gnu
114114
rust: stable
115-
RUSTFLAGS: "-Dwarnings --cfg aes_avx256"
115+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="avx256"'
116116
env:
117117
CARGO_INCREMENTAL: 0
118118
RUSTFLAGS: ${{ matrix.RUSTFLAGS }}
@@ -150,10 +150,7 @@ jobs:
150150
include:
151151
- target: x86_64-unknown-linux-gnu
152152
rust: stable
153-
RUSTFLAGS: "-Dwarnings --cfg aes_avx512"
154-
- target: x86_64-unknown-linux-gnu
155-
rust: stable
156-
RUSTFLAGS: "-Dwarnings --cfg aes_avx256 --cfg aes_avx512"
153+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="avx512"'
157154
env:
158155
CARGO_INCREMENTAL: 0
159156
RUSTFLAGS: ${{ matrix.RUSTFLAGS }}
@@ -214,11 +211,11 @@ jobs:
214211
- run: cargo test --target ${{ matrix.target }} --features hazmat
215212
- run: cargo test --target ${{ matrix.target }} --all-features
216213

217-
# Tests for the portable software backend (i.e. `aes_force_soft`-only)
214+
# Tests for the portable software backend (i.e. `aes_backend="soft"`-only)
218215
soft:
219216
runs-on: ubuntu-latest
220217
env:
221-
RUSTFLAGS: "-Dwarnings --cfg aes_force_soft"
218+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="soft"'
222219
strategy:
223220
matrix:
224221
include:
@@ -280,17 +277,17 @@ jobs:
280277
cross test --package aes --target ${{ matrix.target }}
281278
cross test --package aes --target ${{ matrix.target }} --features hazmat
282279
- env:
283-
RUSTFLAGS: "-Dwarnings --cfg aes_force_soft"
280+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="soft"'
284281
run: |
285282
cross test --package aes --target ${{ matrix.target }}
286283
cross test --package aes --target ${{ matrix.target }} --features hazmat
287284
- env:
288-
RUSTFLAGS: "-Dwarnings --cfg aes_compact"
285+
RUSTFLAGS: '-Dwarnings --cfg aes_compact'
289286
run: |
290287
cross test --package aes --target ${{ matrix.target }}
291288
cross test --package aes --target ${{ matrix.target }} --features hazmat
292289
- env:
293-
RUSTFLAGS: "-Dwarnings --cfg aes_force_soft --cfg aes_compact"
290+
RUSTFLAGS: '-Dwarnings --cfg aes_backend="soft" --cfg aes_compact'
294291
run: |
295292
cross test --package aes --target ${{ matrix.target }}
296293
cross test --package aes --target ${{ matrix.target }} --features hazmat
@@ -322,7 +319,7 @@ jobs:
322319

323320
clippy:
324321
env:
325-
RUSTFLAGS: "-Dwarnings --cfg aes_compact"
322+
RUSTFLAGS: '-Dwarnings --cfg aes_compact'
326323
runs-on: ubuntu-latest
327324
steps:
328325
- uses: actions/checkout@v4

aes/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ hazmat = [] # Expose cryptographically hazardous APIs
3131
level = "warn"
3232
check-cfg = [
3333
'cfg(aes_compact)',
34-
'cfg(aes_force_soft)',
35-
'cfg(aes_avx256)',
36-
'cfg(aes_avx512)',
34+
'cfg(aes_backend, values("soft", "avx256", "avx512"))',
3735
'cfg(cpubits, values("16", "32", "64"))'
3836
]
3937

aes/src/hazmat.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,18 @@ pub use crate::Block;
1717
/// Eight 128-bit AES blocks
1818
pub type Block8 = cipher::array::Array<Block, cipher::consts::U8>;
1919

20-
#[cfg(all(target_arch = "aarch64", not(aes_force_soft)))]
20+
#[cfg(all(target_arch = "aarch64", not(aes_backend = "soft")))]
2121
use crate::armv8::hazmat as intrinsics;
2222

23-
#[cfg(all(any(target_arch = "x86_64", target_arch = "x86"), not(aes_force_soft)))]
23+
#[cfg(all(
24+
any(target_arch = "x86", target_arch = "x86_64"),
25+
not(aes_backend = "soft")
26+
))]
2427
use crate::x86::ni::hazmat as intrinsics;
2528

2629
#[cfg(all(
2730
any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"),
28-
not(aes_force_soft)
31+
not(aes_backend = "soft")
2932
))]
3033
cpufeatures::new!(aes_intrinsics, "aes");
3134

@@ -35,7 +38,7 @@ macro_rules! if_intrinsics_available {
3538
($body:expr) => {{
3639
#[cfg(all(
3740
any(target_arch = "x86", target_arch = "x86_64", target_arch = "aarch64"),
38-
not(aes_force_soft)
41+
not(aes_backend = "soft")
3942
))]
4043
if aes_intrinsics::get() {
4144
unsafe { $body }

aes/src/lib.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
//! will ensure that AESNI and VAES are always used.
4747
//!
4848
//! Note: Enabling VAES256 or VAES512 still requires specifying `--cfg
49-
//! aes_avx256` or `--cfg aes_avx512` explicitly.
49+
//! aes_backend = "avx256"` or `--cfg aes_backend = "avx512"` explicitly.
5050
//!
5151
//! Programs built in this manner will crash with an illegal instruction on
5252
//! CPUs which do not have AES-NI and VAES enabled.
@@ -102,7 +102,10 @@
102102
//!
103103
//! You can modify crate using the following configuration flags:
104104
//!
105-
//! - `aes_force_soft`: force software implementation.
105+
//! - `aes_backend`: explicitly select one of the following backends:
106+
//! `"soft"`: force software backend
107+
//! `"avx256"`: force AVX2 backend
108+
//! `"avx512"`: force AVX-512 backend
106109
//! - `aes_compact`: reduce code size at the cost of slower performance
107110
//! (affects only software backend).
108111
//!
@@ -130,13 +133,13 @@ mod macros;
130133
mod soft;
131134

132135
cpubits::cfg_if! {
133-
if #[cfg(all(target_arch = "aarch64", not(aes_force_soft)))] {
136+
if #[cfg(all(target_arch = "aarch64", not(aes_backend = "soft")))] {
134137
mod armv8;
135138
mod autodetect;
136139
pub use autodetect::*;
137140
} else if #[cfg(all(
138141
any(target_arch = "x86", target_arch = "x86_64"),
139-
not(aes_force_soft)
142+
not(aes_backend = "soft")
140143
))] {
141144
mod x86;
142145
mod autodetect;
@@ -188,7 +191,10 @@ mod tests {
188191
test_for(soft::Aes256Enc::new(&key_256));
189192
test_for(soft::Aes256Dec::new(&key_256));
190193

191-
#[cfg(all(any(target_arch = "x86", target_arch = "x86_64"), not(aes_force_soft)))]
194+
#[cfg(all(
195+
any(target_arch = "x86", target_arch = "x86_64"),
196+
not(aes_backend = "soft")
197+
))]
192198
{
193199
use super::x86;
194200

@@ -206,7 +212,7 @@ mod tests {
206212
}
207213
}
208214

209-
#[cfg(all(target_arch = "aarch64", not(aes_force_soft)))]
215+
#[cfg(all(target_arch = "aarch64", not(aes_backend = "soft")))]
210216
{
211217
use super::armv8;
212218

0 commit comments

Comments
 (0)