Merge pull request #747 from betrusted-io/chaffing

AES Chaffing
This commit is contained in:
bunnie 2025-12-01 21:45:36 +03:00 committed by GitHub
commit 4cd712b2b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
13 changed files with 1410 additions and 23 deletions

5
Cargo.lock generated
View file

@ -22,7 +22,11 @@ dependencies = [
name = "aes" name = "aes"
version = "0.8.4" version = "0.8.4"
dependencies = [ dependencies = [
"bao1x-hal",
"cipher", "cipher",
"hex 0.4.3",
"rand_chacha 0.3.1",
"utralib 0.1.26",
"zeroize", "zeroize",
] ]
@ -7536,7 +7540,6 @@ dependencies = [
name = "xous-kernel" name = "xous-kernel"
version = "0.9.38" version = "0.9.38"
dependencies = [ dependencies = [
"aes",
"armv7", "armv7",
"bao1x-hal", "bao1x-hal",
"bitflags 1.3.2", "bitflags 1.3.2",

View file

@ -18,8 +18,6 @@ utralib = { version = "0.1.26", optional = true, default-features = false }
rand_chacha = { version = "0.3.1", optional = true, default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false }
bao1x-hal = { path = "../libs/bao1x-hal", optional = true, default-features = false } bao1x-hal = { path = "../libs/bao1x-hal", optional = true, default-features = false }
loader = { path = "../loader", optional = true, features = ["swap"] } loader = { path = "../loader", optional = true, features = ["swap"] }
aes = { path = "../services/aes", default-features = false, optional = true }
[target.'cfg(any(windows,unix))'.dev-dependencies] [target.'cfg(any(windows,unix))'.dev-dependencies]
xous-kernel = { package = "xous", version = "0.9.67", features = [ xous-kernel = { package = "xous", version = "0.9.67", features = [
"forget-memory-messages", "forget-memory-messages",
@ -46,7 +44,7 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(ci)', 'cfg(baremetal)'] }
[features] [features]
vexii-test = [] # integration test for vexii, not a production configuration vexii-test = [] # integration test for vexii, not a production configuration
bao1x = ["utralib/bao1x", "rand_chacha", "raw-trng", "aes/bao1x"] bao1x = ["utralib/bao1x", "rand_chacha", "raw-trng"]
verilator-only = [] verilator-only = []
board-baosec = ["bao1x-hal/kernel-baosec", "memmap-flash", "swap"] board-baosec = ["bao1x-hal/kernel-baosec", "memmap-flash", "swap"]
board-baosor = ["bao1x-hal/kernel-baosor"] board-baosor = ["bao1x-hal/kernel-baosor"]

View file

@ -40,7 +40,9 @@ xous-pl230 = { path = "../libs/xous-pl230", optional = true, features = [
aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ aes-gcm-siv = { version = "0.11.1", default-features = false, features = [
"aes", "aes",
], optional = true } ], optional = true }
aes = { path = "../services/aes", default-features = false, optional = true } aes = { path = "../services/aes", default-features = false, optional = true, features = [
"chaffing",
] }
crc = { version = "1.8.1", default-features = false } crc = { version = "1.8.1", default-features = false }
rand_chacha = { version = "0.3.1", optional = true, default-features = false } rand_chacha = { version = "0.3.1", optional = true, default-features = false }
pio-proc = { version = "0.2.2", optional = true } pio-proc = { version = "0.2.2", optional = true }
@ -103,9 +105,14 @@ board-baosec = [
"sha2-bao1x", "sha2-bao1x",
"digest", "digest",
"ux-api/loader-baosec", "ux-api/loader-baosec",
"aes/loader-baosec",
] ]
board-baosor = ["bao1x-hal/loader-baosor"] board-baosor = ["bao1x-hal/loader-baosor"]
board-dabao = ["bao1x-hal/loader-dabao", "bao1x-hal/board-dabao"] board-dabao = [
"bao1x-hal/loader-dabao",
"bao1x-hal/board-dabao",
"aes/loader-dabao",
]
atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"] atsama5d27 = ["utralib/atsama5d27", "armv7", "dep:atsama5d27"]
# precursor flags # precursor flags

View file

@ -18,7 +18,7 @@ pub struct EnvHeader {
count: u16, count: u16,
} }
#[derive(Debug)] #[derive(Debug, Clone)]
pub struct EnvVariables { pub struct EnvVariables {
count: usize, count: usize,
flattened_vars: Vec<u8>, flattened_vars: Vec<u8>,

View file

@ -61,7 +61,6 @@
// -[ ] libs/precursor/hal/src/board/precursors.rs - PDDB_LEN is shortened for vexii-test target // -[ ] libs/precursor/hal/src/board/precursors.rs - PDDB_LEN is shortened for vexii-test target
extern crate alloc; extern crate alloc;
use alloc::vec::Vec;
#[macro_use] #[macro_use]
mod args; mod args;
@ -433,14 +432,11 @@ fn boot_sequence(
env_variables.add_var("UUID", &hex_uuid); env_variables.add_var("UUID", &hex_uuid);
} }
let mut env_header = crate::env::EnvHeader::default();
let env = env_header.to_bytes(&env_variables);
// cold boot path // cold boot path
println!("No suspend marker found, doing a cold boot!"); println!("No suspend marker found, doing a cold boot!");
clear_ram(&mut cfg); clear_ram(&mut cfg);
phase_1(&mut cfg, detached_app); phase_1(&mut cfg, detached_app);
phase_2(&mut cfg, &env); phase_2(&mut cfg, env_variables);
#[cfg(any(feature = "debug-print", feature = "swap"))] #[cfg(any(feature = "debug-print", feature = "swap"))]
if VDBG || SDBG { if VDBG || SDBG {
check_load(&mut cfg); check_load(&mut cfg);

View file

@ -3,13 +3,13 @@ use xous::arch::{SWAP_CFG_VADDR, SWAP_COUNT_VADDR, SWAP_PT_VADDR};
#[cfg(feature = "swap")] #[cfg(feature = "swap")]
use crate::swap::*; use crate::swap::*;
use crate::*; use crate::{env::EnvVariables, *};
/// Phase 2 bootloader /// Phase 2 bootloader
/// ///
/// Set up all the page tables, allocating new root page tables for SATPs and corresponding /// Set up all the page tables, allocating new root page tables for SATPs and corresponding
/// sub-pages starting from the base of previously copied process data. /// sub-pages starting from the base of previously copied process data.
pub fn phase_2(cfg: &mut BootConfig, env: &Vec<u8>) { pub fn phase_2(cfg: &mut BootConfig, env_variables: EnvVariables) {
let args = cfg.args; let args = cfg.args;
// This is the offset in RAM where programs are loaded from. // This is the offset in RAM where programs are loaded from.
@ -36,18 +36,38 @@ pub fn phase_2(cfg: &mut BootConfig, env: &Vec<u8>) {
#[cfg(feature = "atsama5d27")] #[cfg(feature = "atsama5d27")]
let mut kernel_irq_sp = 0; let mut kernel_irq_sp = 0;
#[cfg(feature = "bao1x")]
let mut trng = bao1x_hal::sce::trng::Trng::new(utralib::generated::HW_TRNG_BASE as usize);
#[cfg(feature = "bao1x")]
trng.setup_raw_generation(32); // this is safe to call multiple times to affirm the TRNG state
for tag in args.iter() { for tag in args.iter() {
let mut env_header = crate::env::EnvHeader::default();
let mut pid_env = env_variables.clone();
#[cfg(feature = "bao1x")]
{
let mut seed = [0u8; 32];
for chunk in seed.chunks_mut(4) {
chunk.copy_from_slice(&trng.get_u32().unwrap().to_ne_bytes());
}
let seed_hex = hex::encode(&seed);
pid_env.add_var("SEED", &seed_hex);
}
let env = env_header.to_bytes(&pid_env);
if tag.name == u32::from_le_bytes(*b"IniE") { if tag.name == u32::from_le_bytes(*b"IniE") {
let inie = MiniElf::new(&tag); let inie = MiniElf::new(&tag);
println!("\n\nCopying IniE program into memory"); println!("\n\nCopying IniE program into memory");
let allocated = inie.load(cfg, process_offset, pid, env, IniType::IniE); let allocated = inie.load(cfg, process_offset, pid, &env, IniType::IniE);
println!("IniE Allocated {:x}", allocated); println!("IniE Allocated {:x}", allocated);
process_offset -= allocated; process_offset -= allocated;
pid += 1; pid += 1;
} else if tag.name == u32::from_le_bytes(*b"IniF") { } else if tag.name == u32::from_le_bytes(*b"IniF") {
let inif = MiniElf::new(&tag); let inif = MiniElf::new(&tag);
println!("\n\nMapping IniF program into memory"); println!("\n\nMapping IniF program into memory");
let allocated = inif.load(cfg, process_offset, pid, env, IniType::IniF); let allocated = inif.load(cfg, process_offset, pid, &env, IniType::IniF);
println!("IniF Allocated {:x}", allocated); println!("IniF Allocated {:x}", allocated);
process_offset -= allocated; process_offset -= allocated;
pid += 1; pid += 1;
@ -56,7 +76,7 @@ pub fn phase_2(cfg: &mut BootConfig, env: &Vec<u8>) {
{ {
let inis = MiniElf::new(&tag); let inis = MiniElf::new(&tag);
println!("\n\nMapping IniS program into memory"); println!("\n\nMapping IniS program into memory");
let allocated = inis.load(cfg, process_offset, pid, env, IniType::IniS); let allocated = inis.load(cfg, process_offset, pid, &env, IniType::IniS);
println!("IniS Allocated {:x}", allocated); println!("IniS Allocated {:x}", allocated);
process_offset -= allocated; process_offset -= allocated;
pid += 1; pid += 1;

View file

@ -9,15 +9,24 @@ description = "AES library for Xous"
[dependencies] [dependencies]
cipher = "0.4.2" cipher = "0.4.2"
zeroize = { version = "1.6.0", optional = true, default-features = false } zeroize = { version = "1.6.0", optional = true, default-features = false }
rand_chacha = { version = "0.3.1", optional = true, default-features = false }
hex = { version = "0.4.3", optional = true, default-features = false, features = [
"alloc",
] }
utralib = { version = "0.1.26", optional = true, default-features = false }
bao1x-hal = { path = "../../libs/bao1x-hal", default-features = false, optional = true }
[lints.rust.unexpected_cfgs] [lints.rust.unexpected_cfgs]
level = "warn" level = "warn"
check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)"] check-cfg = ["cfg(aes_compact)", "cfg(aes_force_soft)"]
[features] [features]
hazmat = [] # Expose cryptographically hazardous APIs hazmat = [] # Expose cryptographically hazardous APIs
bao1x = [] bao1x = []
vexii-test = [] vexii-test = []
precursor = [] precursor = []
renode = [] renode = []
chaffing = ["rand_chacha", "hex"]
loader-baosec = ["utralib/bao1x", "bao1x-hal/loader-baosec"]
loader-dabao = ["utralib/bao1x", "bao1x-hal/loader-dabao"]
default = [] default = []

View file

@ -1,4 +1,4 @@
#![no_std] #![cfg_attr(target_os = "none", no_std)]
/* /*
Soft AES implementations vendored in from https://github.com/RustCrypto/block-ciphers.git Soft AES implementations vendored in from https://github.com/RustCrypto/block-ciphers.git
@ -54,13 +54,16 @@ pub use soft::Aes256Soft as Aes256;
))] ))]
pub use vex::{Aes128, Aes256}; pub use vex::{Aes128, Aes256};
#[cfg(any(feature = "vexii-test", feature = "bao1x"))] #[cfg(all(any(feature = "vexii-test", feature = "bao1x"), not(feature = "chaffing")))]
mod zkn; mod zkn;
#[cfg(all(any(feature = "vexii-test", feature = "bao1x"), feature = "chaffing"))]
mod zkn_chaff;
#[cfg(any(feature = "vexii-test", feature = "bao1x"))] #[cfg(any(feature = "vexii-test", feature = "bao1x"))]
// pub use soft::Aes128Soft as Aes128;
pub use zkn::Aes128; pub use zkn::Aes128;
#[cfg(any(feature = "vexii-test", feature = "bao1x"))] #[cfg(any(feature = "vexii-test", feature = "bao1x"))]
pub use zkn::Aes256; pub use zkn::Aes256;
#[cfg(all(any(feature = "vexii-test", feature = "bao1x"), feature = "chaffing"))]
use zkn_chaff as zkn;
/// Size of an AES block (128-bits; 16-bytes) /// Size of an AES block (128-bits; 16-bytes)
pub const BLOCK_SIZE: usize = 16; pub const BLOCK_SIZE: usize = 16;

File diff suppressed because it is too large Load diff

View file

@ -30,7 +30,9 @@ rand = { version = "0.8.5", features = ["getrandom"], optional = true }
rand_chacha = { version = "0.3.1", optional = true } rand_chacha = { version = "0.3.1", optional = true }
rand_core = "0.6.4" rand_core = "0.6.4"
aes = { path = "../aes", default-features = false, optional = true } aes = { path = "../aes", default-features = false, optional = true, features = [
"chaffing",
] }
# time testing # time testing
chrono = "0.4.33" chrono = "0.4.33"

View file

@ -168,6 +168,10 @@ with vex bao1x CPU (400MHz)
hardware 4.65µs/block enc+dec AES256 hardware 4.65µs/block enc+dec AES256
software 45.79µs/block enc+dec AES256 software 45.79µs/block enc+dec AES256
with vex bao1x CPU (350MHz)
hardware 5.36µs/block enc+dec AES256
software 52.45µs/block enc+dec AES256
hardware - with chaffing - 10.27µs/block enc+dec AES256
*/ */
pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) { pub fn benchmark_thread(sid0: usize, sid1: usize, sid2: usize, sid3: usize) {
let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32); let sid = xous::SID::from_u32(sid0 as u32, sid1 as u32, sid2 as u32, sid3 as u32);

View file

@ -121,6 +121,10 @@ impl<'a> ShellCmdApi<'a> for Test {
iox.set_gpio_pin_value(IoxPort::PF, 6, IoxValue::Low); iox.set_gpio_pin_value(IoxPort::PF, 6, IoxValue::Low);
log::info!("sent shutdown to axp2101"); log::info!("sent shutdown to axp2101");
} }
"seed" => {
let (_, value) = std::env::vars().find(|(key, _value)| key == "SEED").unwrap();
log::info!("Seed: {:?}", value);
}
"keepon" => { "keepon" => {
todo!("Fix this to use DCDC2 for keepon (as per baosec v2)"); todo!("Fix this to use DCDC2 for keepon (as per baosec v2)");
} }

View file

@ -21,7 +21,9 @@ loader = { path = "../../loader", features = ["swap"], optional = true }
aes-gcm-siv = { version = "0.11.1", default-features = false, features = [ aes-gcm-siv = { version = "0.11.1", default-features = false, features = [
"aes", "aes",
] } ] }
aes = { path = "../aes", default-features = false, optional = true } aes = { path = "../aes", default-features = false, optional = true, features = [
"chaffing",
] }
bao1x-hal = { path = "../../libs/bao1x-hal", optional = true, default-features = false } bao1x-hal = { path = "../../libs/bao1x-hal", optional = true, default-features = false }
precursor-hal = { path = "../../libs/precursor-hal", optional = true, default-features = false } precursor-hal = { path = "../../libs/precursor-hal", optional = true, default-features = false }
bao1x-api = { path = "../../libs/bao1x-api", optional = true, default-features = false } bao1x-api = { path = "../../libs/bao1x-api", optional = true, default-features = false }