From 75a9373fea60294fa4d7ea36bbaf5ef38c900f64 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 7 Sep 2025 11:50:12 +0800 Subject: [PATCH] fix AES dependencies so that the correct acceleration is enabled This relies on a trick I was not aware of previously, which is that transitive dependencies absorb the additive set of features enabled within a crate. see https://doc.rust-lang.org/cargo/reference/features.html#feature-unification for the full description. but basically by referring to the feature required in "aes" despite not being used in the crate but pulled in by aes-gcm-siv, i can ensure that the transitive dependency is in fact used. I think this was always there but previously I just didn't understand it correctly or didn't use it right when I tried to deploy it, but this time I think I got it right. --- kernel/Cargo.toml | 10 ++++++++-- loader/Cargo.toml | 5 ++++- services/aes/Cargo.toml | 9 ++++----- services/aes/src/lib.rs | 21 +++++++++++++++++---- services/bao-console/Cargo.toml | 3 ++- services/keystore/Cargo.toml | 4 ++-- services/pddb/Cargo.toml | 8 +++++--- services/root-keys/Cargo.toml | 9 +++++++-- services/shellchat/Cargo.toml | 4 ++-- services/xous-swapper/Cargo.toml | 8 +++++++- 10 files changed, 58 insertions(+), 23 deletions(-) diff --git a/kernel/Cargo.toml b/kernel/Cargo.toml index cb7b4a61c..48e2acd9f 100644 --- a/kernel/Cargo.toml +++ b/kernel/Cargo.toml @@ -18,6 +18,7 @@ utralib = { version = "0.1.25", optional = true, default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false } cramium-hal = { path = "../libs/cramium-hal", optional = true, default-features = false } loader = { path = "../loader", optional = true, features = ["swap"] } +aes = { path = "../services/aes", default-features = false, optional = true } [target.'cfg(any(windows,unix))'.dev-dependencies] xous-kernel = { package = "xous", version = "0.9.66", features = [ @@ -44,8 +45,13 @@ critical-section = "1.1.1" unexpected_cfgs = { level = "warn", check-cfg = ['cfg(ci)', 'cfg(baremetal)'] } [features] -vexii-test = [] # integration test for vexii, not a production configuration -cramium-soc = ["utralib/cramium-soc", "rand_chacha", "raw-trng"] +vexii-test = [] # integration test for vexii, not a production configuration +cramium-soc = [ + "utralib/cramium-soc", + "rand_chacha", + "raw-trng", + "aes/cramium-soc", +] verilator-only = [] cramium-fpga = ["utralib/cramium-fpga", "rand_chacha"] board-baosec = ["cramium-hal/kernel-baosec", "memmap-flash", "swap"] diff --git a/loader/Cargo.toml b/loader/Cargo.toml index f12728ca9..e0e7c3c70 100644 --- a/loader/Cargo.toml +++ b/loader/Cargo.toml @@ -36,6 +36,7 @@ xous-pl230 = { path = "../libs/xous-pl230", optional = true, features = [ aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ "aes", ], optional = true } +aes = { path = "../services/aes", default-features = false, optional = true } crc = { version = "1.8.1", default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false } pio-proc = { version = "0.2.2", optional = true } @@ -80,6 +81,8 @@ renode = [ "sha2-loader", ] cramium-soc = [ + "dep:aes", + "aes/cramium-soc", "utralib/cramium-soc", # "cramium-hal/debug-print-usb", "debug-print", @@ -113,7 +116,7 @@ secboot = [] #default = ["debug-print"] # swap flag -swap = ["aes-gcm-siv"] +swap = ["dep:aes-gcm-siv"] # cramium target flags board-bringup = [] diff --git a/services/aes/Cargo.toml b/services/aes/Cargo.toml index 20d28494c..7a3791032 100644 --- a/services/aes/Cargo.toml +++ b/services/aes/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aes" -version = "0.8.3" +version = "0.8.4" authors = ["bunnie "] edition = "2018" description = "AES library for Xous" @@ -18,7 +18,6 @@ check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)"] hazmat = [] # Expose cryptographically hazardous APIs cramium-soc = [] vexii-test = [] -# Note: this default line should always be empty. If it has the 'cramium-soc' feature -# checkd in, then AES acceleration gets disabled for all targets. This hack only exists -# because in the MPW version of the silicon, HW AES in broken on the RV32. -default = ["cramium-soc"] +precursor = [] +renode = [] +default = [] diff --git a/services/aes/src/lib.rs b/services/aes/src/lib.rs index 7cd27b8be..675453cf3 100644 --- a/services/aes/src/lib.rs +++ b/services/aes/src/lib.rs @@ -25,19 +25,32 @@ pub type Block = GenericArray; pub type Block8 = GenericArray; // vex patches -#[cfg(all(target_arch = "riscv32", not(feature = "cramium-soc"), not(feature = "vexii-test")))] +#[cfg(all( + all(target_arch = "riscv32", not(feature = "cramium-soc"), not(feature = "vexii-test")), + any(feature = "precursor", feature = "renode"), +))] mod vex; // Note that we can't use 'feature' flags (for precursor, renode, hosted) because the AES // library is patched into functions that are oblivious to these features. // so this library has to fall back on the legacy method of determining which build target // is being specified. -#[cfg(not(any(all(target_arch = "riscv32", feature = "cramium-soc"), feature = "vexii-test")))] +#[cfg(not(any( + all(target_arch = "riscv32", feature = "cramium-soc"), + feature = "vexii-test", + feature = "precursor", + feature = "renode" +)))] pub use soft::Aes128Soft as Aes128; -#[cfg(not(any(all(target_arch = "riscv32", feature = "cramium-soc"), feature = "vexii-test")))] +#[cfg(not(any( + all(target_arch = "riscv32", feature = "cramium-soc"), + feature = "vexii-test", + feature = "precursor", + feature = "renode" +)))] pub use soft::Aes256Soft as Aes256; #[cfg(all( all(target_arch = "riscv32", not(feature = "cramium-soc"), not(feature = "vexii-test")), - any(target_os = "none", target_os = "xous"), + any(feature = "precursor", feature = "renode"), ))] pub use vex::{Aes128, Aes256}; diff --git a/services/bao-console/Cargo.toml b/services/bao-console/Cargo.toml index 7bad5fa6a..2ac98eeca 100644 --- a/services/bao-console/Cargo.toml +++ b/services/bao-console/Cargo.toml @@ -28,7 +28,7 @@ xous-swapper = { path = "../xous-swapper", optional = true } rand = { version = "0.8.5", features = ["getrandom"], optional = true } rand_chacha = { version = "0.3.1", optional = true } -aes = { path = "../aes", optional = true } +aes = { path = "../aes", default-features = false, optional = true } # aes testing [features] @@ -36,6 +36,7 @@ cramium-soc = [ "utralib/cramium-soc", "modals/cramium-soc", "usb-cramium/cramium-soc", + "aes/cramium-soc", ] board-baosec = [ "modals/board-baosec", diff --git a/services/keystore/Cargo.toml b/services/keystore/Cargo.toml index 69eb3fefe..57599ca34 100644 --- a/services/keystore/Cargo.toml +++ b/services/keystore/Cargo.toml @@ -55,7 +55,7 @@ subtle = { version = "2.4.1", default-features = false } #sha2 = {version = "0.9.5", default-features = false, features = []} sha2 = { version = "0.10.8" } digest = "0.10.7" -aes = { path = "../aes" } +aes = { path = "../aes", default-features = false, optional = true } # engine-25519 = { path = "../engine-25519" } byteorder = "1.4.3" # used by keywrap # hardware acceleration adaptations are inserted into a fork of the main branch. @@ -74,7 +74,7 @@ hkdf = "0.12.4" #features = ["rand_core", "digest"] [features] -cramium-soc = ["utralib/cramium-soc"] +cramium-soc = ["utralib/cramium-soc", "aes/cramium-soc"] board-baosec = [ "utralib/cramium-soc", "ux-api/board-baosec", diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 1bb012a61..e998b71bf 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -23,7 +23,7 @@ rkyv = { version = "0.8.8", default-features = false, features = [ ] } trng = { path = "../trng", optional = true } spinor = { path = "../spinor", optional = true } -aes = { path = "../aes" } +aes = { path = "../aes", default-features = false } root-keys = { path = "../root-keys", optional = true } keystore-api = { path = "../../libs/keystore-api" } cipher = "0.4.2" @@ -91,10 +91,10 @@ gen1 = [ ] gen2 = ["keystore", "keystore-api/gen2"] # gen 1 targets -precursor = ["utralib/precursor", "sha2/precursor", "gen1"] +precursor = ["utralib/precursor", "sha2/precursor", "gen1", "aes/precursor"] vexii-test = ["precursor-hal/vexii-test", "aes/vexii-test"] hosted = ["utralib/hosted", "sha2/hosted", "gen1"] -renode = ["utralib/renode", "sha2/renode", "gen1"] +renode = ["utralib/renode", "sha2/renode", "gen1", "aes/renode"] # gen 2 targets board-baosec = [ @@ -104,6 +104,7 @@ board-baosec = [ "cramium-hal/board-baosec", "modals/board-baosec", "keystore/board-baosec", + "aes/cramium-soc", ] # USB form factor token hosted-baosec = [ "gen2", @@ -117,6 +118,7 @@ board-baosor = [ "cram-hal-service/board-baosor", "cramium-hal/board-baosor", "keystore/board-baosor", + "aes/cramium-soc", ] # Precursor form factor # board-dabao is not a valid target as it lacks external SPI flash diff --git a/services/root-keys/Cargo.toml b/services/root-keys/Cargo.toml index ed7113fc2..c6d0f89ac 100644 --- a/services/root-keys/Cargo.toml +++ b/services/root-keys/Cargo.toml @@ -87,9 +87,14 @@ features = ["rand_core", "digest"] [features] vexii-test = ["aes/vexii-test"] -precursor = ["utralib/precursor", "sha2/precursor", "keystore-api/precursor"] +precursor = [ + "utralib/precursor", + "sha2/precursor", + "keystore-api/precursor", + "aes/precursor", +] hosted = ["utralib/hosted", "sha2/hosted", "keystore-api/hosted"] -renode = ["utralib/renode", "sha2/renode", "keystore-api/renode"] +renode = ["utralib/renode", "sha2/renode", "keystore-api/renode", "aes/renode"] policy-menu = [ "keystore-api/policy-menu", ] # provisions for users to set their password retention policies (on the block for deprecation) (note: to re-enable you need to add "rootkeys menu" to the tokens.rs in the GAM) diff --git a/services/shellchat/Cargo.toml b/services/shellchat/Cargo.toml index dcabb85d4..97b6ed54d 100644 --- a/services/shellchat/Cargo.toml +++ b/services/shellchat/Cargo.toml @@ -123,9 +123,9 @@ features = ["rand_core"] [features] vexii-test = ["aes/vexii-test"] -precursor = ["utralib/precursor", "sha2/precursor"] +precursor = ["utralib/precursor", "sha2/precursor", "aes/precursor"] hosted = ["utralib/hosted", "sha2/hosted", "random-pick"] -renode = ["utralib/renode", "sha2/renode"] +renode = ["utralib/renode", "sha2/renode", "aes/renode"] debugprint = [] spinortest = [ ] # for spinor testing. contra-indicated with PDDB, as it steals memory from the PDDB. diff --git a/services/xous-swapper/Cargo.toml b/services/xous-swapper/Cargo.toml index dd1cf532b..008abc35c 100644 --- a/services/xous-swapper/Cargo.toml +++ b/services/xous-swapper/Cargo.toml @@ -21,6 +21,7 @@ loader = { path = "../../loader", features = ["swap"], optional = true } aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ "aes", ] } +aes = { path = "../aes", default-features = false, optional = true } cramium-hal = { path = "../../libs/cramium-hal", optional = true, default-features = false } precursor-hal = { path = "../../libs/precursor-hal", optional = true, default-features = false } cramium-api = { path = "../../libs/cramium-api", optional = true, default-features = false } @@ -30,7 +31,12 @@ utralib = { version = "0.1.25", optional = true, default-features = false } [target.'cfg(any(windows,unix))'.dependencies] [features] -cramium-soc = ["utralib/cramium-soc", "loader/cramium-soc", "cramium-api/std"] +cramium-soc = [ + "utralib/cramium-soc", + "loader/cramium-soc", + "cramium-api/std", + "aes/cramium-soc", +] cramium-fpga = ["utralib/cramium-fpga"] board-baosec = ["cramium-hal/board-baosec", "cramium-soc"] board-baosor = ["cramium-hal/board-baosor", "cramium-soc"]