From 1c08e004e5d62e7fad8167009c315785297b1ad8 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 1 Feb 2026 20:58:34 -0700 Subject: [PATCH 1/2] aes: use `cpubits` crate The `cpubits` crate contains a macro for selecting between 32-bit and 64-bit backends, incorporating 64-bit overrides for certain natively 32-bit targets with native ISA support for operations "register pairs", namely ARMv7 and WASM. It also supports overriding the selection with `cfg(cpubits = "...")` This switches the selection of the 32-bit vs 64-bit soft backend, always compiled into the crate as it's used as a fallback implementation if intrinsics aren't available, to using `cpubits`. The crate contains a vendored copy of `cfg-if`, so this also replaces `cfg-if` as a dependency with `cpubits`. --- Cargo.lock | 8 +++++++- aes/Cargo.toml | 10 ++++++++-- aes/src/lib.rs | 4 +--- aes/src/soft.rs | 13 ++++++++++--- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2da1658..06f5d0f9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,8 +6,8 @@ version = 4 name = "aes" version = "0.9.0-rc.3" dependencies = [ - "cfg-if", "cipher", + "cpubits", "cpufeatures", "hex-literal", "zeroize", @@ -102,6 +102,12 @@ dependencies = [ "zeroize", ] +[[package]] +name = "cpubits" +version = "0.1.0-rc.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11a8c0210fa48ba3ea04ac1e9c6e72ae66009db3b1f1745635d4ff2e58eaadd0" + [[package]] name = "cpufeatures" version = "0.2.17" diff --git a/aes/Cargo.toml b/aes/Cargo.toml index ab0d7b7b..63c349c4 100644 --- a/aes/Cargo.toml +++ b/aes/Cargo.toml @@ -13,8 +13,8 @@ keywords = ["crypto", "aes", "rijndael", "block-cipher"] categories = ["cryptography", "no-std"] [dependencies] -cfg-if = "1" cipher = "0.5.0-rc.7" +cpubits = "0.1.0-rc.3" zeroize = { version = "1.5.6", optional = true, default-features = false, features = ["aarch64"] } [target.'cfg(any(target_arch = "aarch64", target_arch = "x86_64", target_arch = "x86"))'.dependencies] @@ -29,7 +29,13 @@ hazmat = [] # Expose cryptographically hazardous APIs [lints.rust.unexpected_cfgs] level = "warn" -check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)", "cfg(aes_avx256)", "cfg(aes_avx512)"] +check-cfg = [ + 'cfg(aes_compact)', + 'cfg(aes_force_soft)', + 'cfg(aes_avx256)', + 'cfg(aes_avx512)', + 'cfg(cpubits, values("16", "32", "64"))' +] [package.metadata.docs.rs] all-features = true diff --git a/aes/src/lib.rs b/aes/src/lib.rs index b2e80a6c..ed562871 100644 --- a/aes/src/lib.rs +++ b/aes/src/lib.rs @@ -129,9 +129,7 @@ pub mod hazmat; mod macros; mod soft; -use cfg_if::cfg_if; - -cfg_if! { +cpubits::cfg_if! { if #[cfg(all(target_arch = "aarch64", not(aes_force_soft)))] { mod armv8; mod autodetect; diff --git a/aes/src/soft.rs b/aes/src/soft.rs index 1f0941cb..658d8ff7 100644 --- a/aes/src/soft.rs +++ b/aes/src/soft.rs @@ -8,9 +8,16 @@ #![deny(unsafe_code)] -#[cfg_attr(not(target_pointer_width = "64"), path = "soft/fixslice32.rs")] -#[cfg_attr(target_pointer_width = "64", path = "soft/fixslice64.rs")] -pub(crate) mod fixslice; +cpubits::cpubits! { + 16 | 32 => { + #[path = "soft/fixslice32.rs"] + pub(crate) mod fixslice; + } + 64 => { + #[path = "soft/fixslice32.rs"] + pub(crate) mod fixslice; + } +} use crate::Block; use cipher::{ From 0577582661335a3e76aaaf3f79acec136a317aa3 Mon Sep 17 00:00:00 2001 From: Tony Arcieri Date: Sun, 1 Feb 2026 21:17:15 -0700 Subject: [PATCH 2/2] Update aes/src/soft.rs Co-authored-by: Artyom Pavlov --- aes/src/soft.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aes/src/soft.rs b/aes/src/soft.rs index 658d8ff7..445adc39 100644 --- a/aes/src/soft.rs +++ b/aes/src/soft.rs @@ -14,7 +14,7 @@ cpubits::cpubits! { pub(crate) mod fixslice; } 64 => { - #[path = "soft/fixslice32.rs"] + #[path = "soft/fixslice64.rs"] pub(crate) mod fixslice; } }