From a0f0d9dde9e384f17b1d417f83ffb6c4e69b36bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20B=C3=A5nvik?= Date: Fri, 21 Feb 2025 13:57:05 +0100 Subject: [PATCH] Use CryptoRng to seed internal ChaCha12 --- examples/apps/src/ble_advertise_multiple.rs | 7 ++- examples/apps/src/ble_bas_central.rs | 7 ++- examples/apps/src/ble_bas_central_sec.rs | 9 ++-- examples/apps/src/ble_bas_peripheral.rs | 7 ++- examples/apps/src/ble_bas_peripheral_sec.rs | 9 ++-- examples/apps/src/ble_l2cap_central.rs | 7 ++- examples/apps/src/ble_l2cap_peripheral.rs | 7 ++- examples/apps/src/ble_scanner.rs | 7 ++- examples/esp32/Cargo.lock | 1 + examples/esp32/src/bin/ble_bas_central.rs | 10 ++--- examples/esp32/src/bin/ble_bas_central_sec.rs | 4 +- examples/esp32/src/bin/ble_bas_peripheral.rs | 10 ++--- .../esp32/src/bin/ble_bas_peripheral_sec.rs | 4 +- examples/esp32/src/bin/ble_l2cap_central.rs | 10 ++--- .../esp32/src/bin/ble_l2cap_peripheral.rs | 10 ++--- examples/esp32/src/bin/ble_scanner.rs | 10 ++--- examples/nrf-sdc/.cargo/config.toml | 2 +- examples/nrf-sdc/Cargo.lock | 3 ++ examples/nrf-sdc/Cargo.toml | 2 + examples/nrf-sdc/src/bin/ble_bas_central.rs | 5 ++- .../nrf-sdc/src/bin/ble_bas_central_sec.rs | 7 +-- .../nrf-sdc/src/bin/ble_bas_peripheral.rs | 5 ++- .../nrf-sdc/src/bin/ble_bas_peripheral_sec.rs | 7 +-- examples/nrf-sdc/src/bin/ble_l2cap_central.rs | 5 ++- .../nrf-sdc/src/bin/ble_l2cap_peripheral.rs | 5 ++- examples/nrf-sdc/src/bin/ble_scanner.rs | 5 ++- examples/rp-pico-2-w/Cargo.toml | 2 + .../rp-pico-2-w/src/bin/ble_bas_central.rs | 7 ++- .../rp-pico-2-w/src/bin/ble_bas_peripheral.rs | 7 ++- examples/rp-pico-w/Cargo.lock | 44 +++++++++++++++++++ examples/rp-pico-w/Cargo.toml | 2 + examples/rp-pico-w/src/bin/ble_bas_central.rs | 7 ++- .../rp-pico-w/src/bin/ble_bas_peripheral.rs | 7 ++- examples/serial-hci/Cargo.lock | 1 + .../serial-hci/src/bin/ble_bas_central_sec.rs | 5 +-- .../serial-hci/src/bin/ble_bas_peripheral.rs | 3 +- .../src/bin/ble_bas_peripheral_sec.rs | 5 +-- examples/tests/tests/ble_l2cap_central.rs | 3 +- examples/tests/tests/ble_l2cap_peripheral.rs | 2 +- host/src/channel_manager.rs | 2 +- host/src/connection_manager.rs | 9 +++- host/src/host.rs | 5 ++- host/src/lib.rs | 12 ++--- host/src/security_manager/mod.rs | 10 +---- host/tests/gatt.rs | 4 +- host/tests/gatt_derive.rs | 4 +- host/tests/l2cap.rs | 4 +- 47 files changed, 199 insertions(+), 111 deletions(-) diff --git a/examples/apps/src/ble_advertise_multiple.rs b/examples/apps/src/ble_advertise_multiple.rs index 557d5532..6bc7253c 100644 --- a/examples/apps/src/ble_advertise_multiple.rs +++ b/examples/apps/src/ble_advertise_multiple.rs @@ -2,6 +2,7 @@ use bt_hci::cmd::le::*; use bt_hci::controller::ControllerCmdSync; use embassy_futures::join::join; use embassy_time::{Duration, Instant, Timer}; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -10,7 +11,7 @@ const CONNECTIONS_MAX: usize = 1; /// Max number of L2CAP channels. const L2CAP_CHANNELS_MAX: usize = 2; // Signal + att -pub async fn run(controller: C) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller + for<'t> ControllerCmdSync> @@ -20,12 +21,14 @@ where + ControllerCmdSync + for<'t> ControllerCmdSync> + for<'t> ControllerCmdSync>, + RNG: RngCore + CryptoRng, { let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]); info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources).set_random_address(address); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut peripheral, mut runner, diff --git a/examples/apps/src/ble_bas_central.rs b/examples/apps/src/ble_bas_central.rs index 9b605e18..975a3f0a 100644 --- a/examples/apps/src/ble_bas_central.rs +++ b/examples/apps/src/ble_bas_central.rs @@ -1,5 +1,6 @@ use embassy_futures::join::join; use embassy_time::{Duration, Timer}; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -8,9 +9,10 @@ const CONNECTIONS_MAX: usize = 1; /// Max number of L2CAP channels. const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC -pub async fn run(controller: C) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller, + RNG: RngCore + CryptoRng, { // Using a fixed "random" address can be useful for testing. In real scenarios, one would // use e.g. the MAC 6 byte array as the address (how to get that varies by the platform). @@ -18,7 +20,8 @@ where info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources).set_random_address(address); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut central, mut runner, diff --git a/examples/apps/src/ble_bas_central_sec.rs b/examples/apps/src/ble_bas_central_sec.rs index a335cf7c..c8f650fe 100644 --- a/examples/apps/src/ble_bas_central_sec.rs +++ b/examples/apps/src/ble_bas_central_sec.rs @@ -1,5 +1,6 @@ use embassy_futures::join::join; use embassy_time::{Duration, Timer}; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -8,9 +9,10 @@ const CONNECTIONS_MAX: usize = 1; /// Max number of L2CAP channels. const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC -pub async fn run(controller: C, random_seed: &[u8; 32]) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller, + RNG: RngCore + CryptoRng, { // Using a fixed "random" address can be useful for testing. In real scenarios, one would // use e.g. the MAC 6 byte array as the address (how to get that varies by the platform). @@ -18,9 +20,8 @@ where info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources) - .set_random_address(address) - .set_random_seed(random_seed); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut central, diff --git a/examples/apps/src/ble_bas_peripheral.rs b/examples/apps/src/ble_bas_peripheral.rs index 5e1aae01..9ddf1a51 100644 --- a/examples/apps/src/ble_bas_peripheral.rs +++ b/examples/apps/src/ble_bas_peripheral.rs @@ -1,6 +1,7 @@ use embassy_futures::join::join; use embassy_futures::select::select; use embassy_time::Timer; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -28,9 +29,10 @@ struct BatteryService { } /// Run the BLE stack. -pub async fn run(controller: C) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller, + RNG: RngCore + CryptoRng, { // Using a fixed "random" address can be useful for testing. In real scenarios, one would // use e.g. the MAC 6 byte array as the address (how to get that varies by the platform). @@ -38,7 +40,8 @@ where info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources).set_random_address(address); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut peripheral, runner, .. } = stack.build(); diff --git a/examples/apps/src/ble_bas_peripheral_sec.rs b/examples/apps/src/ble_bas_peripheral_sec.rs index 5fdb2477..01cbc2f2 100644 --- a/examples/apps/src/ble_bas_peripheral_sec.rs +++ b/examples/apps/src/ble_bas_peripheral_sec.rs @@ -1,6 +1,7 @@ use embassy_futures::join::join; use embassy_futures::select::select; use embassy_time::Timer; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -28,9 +29,10 @@ struct BatteryService { } /// Run the BLE stack. -pub async fn run(controller: C, random_seed: &[u8; 32]) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller, + RNG: RngCore + CryptoRng, { // Using a fixed "random" address can be useful for testing. In real scenarios, one would // use e.g. the MAC 6 byte array as the address (how to get that varies by the platform). @@ -38,9 +40,8 @@ where info!("Our address = {}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources) - .set_random_address(address) - .set_random_seed(random_seed); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut peripheral, runner, .. } = stack.build(); diff --git a/examples/apps/src/ble_l2cap_central.rs b/examples/apps/src/ble_l2cap_central.rs index 44858a68..040b9d34 100644 --- a/examples/apps/src/ble_l2cap_central.rs +++ b/examples/apps/src/ble_l2cap_central.rs @@ -1,5 +1,6 @@ use embassy_futures::join::join; use embassy_time::{Duration, Timer}; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -8,9 +9,10 @@ const CONNECTIONS_MAX: usize = 1; /// Max number of L2CAP channels. const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC -pub async fn run(controller: C) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller, + RNG: RngCore + CryptoRng, { // Using a fixed "random" address can be useful for testing. In real scenarios, one would // use e.g. the MAC 6 byte array as the address (how to get that varies by the platform). @@ -18,7 +20,8 @@ where info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources).set_random_address(address); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut central, mut runner, diff --git a/examples/apps/src/ble_l2cap_peripheral.rs b/examples/apps/src/ble_l2cap_peripheral.rs index da173196..9e9d152f 100644 --- a/examples/apps/src/ble_l2cap_peripheral.rs +++ b/examples/apps/src/ble_l2cap_peripheral.rs @@ -1,5 +1,6 @@ use embassy_futures::join::join; use embassy_time::{Duration, Timer}; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections @@ -8,16 +9,18 @@ const CONNECTIONS_MAX: usize = 1; /// Max number of L2CAP channels. const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC -pub async fn run(controller: C) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller, + RNG: RngCore + CryptoRng, { // Hardcoded peripheral address let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]); info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources).set_random_address(address); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { mut peripheral, mut runner, diff --git a/examples/apps/src/ble_scanner.rs b/examples/apps/src/ble_scanner.rs index 9789eba9..31253180 100644 --- a/examples/apps/src/ble_scanner.rs +++ b/examples/apps/src/ble_scanner.rs @@ -4,15 +4,17 @@ use core::cell::RefCell; use embassy_futures::join::join; use embassy_time::{Duration, Timer}; use heapless::Deque; +use rand_core::{CryptoRng, RngCore}; use trouble_host::prelude::*; /// Max number of connections const CONNECTIONS_MAX: usize = 1; const L2CAP_CHANNELS_MAX: usize = 1; -pub async fn run(controller: C) +pub async fn run(controller: C, random_generator: &mut RNG) where C: Controller + ControllerCmdSync, + RNG: RngCore + CryptoRng, { // Using a fixed "random" address can be useful for testing. In real scenarios, one would // use e.g. the MAC 6 byte array as the address (how to get that varies by the platform). @@ -21,7 +23,8 @@ where info!("Our address = {:?}", address); let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller, &mut resources).set_random_address(address); + let stack = trouble_host::new(controller, &mut resources, random_generator); + let stack = stack.set_random_address(address); let Host { central, mut runner, .. diff --git a/examples/esp32/Cargo.lock b/examples/esp32/Cargo.lock index 336a04c1..e6774ac7 100644 --- a/examples/esp32/Cargo.lock +++ b/examples/esp32/Cargo.lock @@ -1527,6 +1527,7 @@ dependencies = [ "embedded-io", "heapless", "log", + "rand_core", "static_cell", "trouble-host", ] diff --git a/examples/esp32/src/bin/ble_bas_central.rs b/examples/esp32/src/bin/ble_bas_central.rs index cc016c95..519f45f1 100644 --- a/examples/esp32/src/bin/ble_bas_central.rs +++ b/examples/esp32/src/bin/ble_bas_central.rs @@ -21,13 +21,9 @@ async fn main(_s: Spawner) { }); esp_alloc::heap_allocator!(72 * 1024); let timg0 = TimerGroup::new(peripherals.TIMG0); + let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let init = esp_wifi::init( - timg0.timer0, - esp_hal::rng::Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); #[cfg(not(feature = "esp32"))] { @@ -43,5 +39,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_bas_central::run::<_, { consts::L2CAP_MTU }>(controller).await; + ble_bas_central::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/esp32/src/bin/ble_bas_central_sec.rs b/examples/esp32/src/bin/ble_bas_central_sec.rs index ef89df2f..e283cd11 100644 --- a/examples/esp32/src/bin/ble_bas_central_sec.rs +++ b/examples/esp32/src/bin/ble_bas_central_sec.rs @@ -23,8 +23,6 @@ async fn main(_s: Spawner) { let timg0 = TimerGroup::new(peripherals.TIMG0); let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let mut random_seed = [0u8; 32]; - rng.read(&mut random_seed); let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); @@ -42,5 +40,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_bas_central_sec::run::<_, { consts::L2CAP_MTU }>(controller, &random_seed).await; + ble_bas_central_sec::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/esp32/src/bin/ble_bas_peripheral.rs b/examples/esp32/src/bin/ble_bas_peripheral.rs index ded57aa5..244ad0ea 100644 --- a/examples/esp32/src/bin/ble_bas_peripheral.rs +++ b/examples/esp32/src/bin/ble_bas_peripheral.rs @@ -21,13 +21,9 @@ async fn main(_s: Spawner) { }); esp_alloc::heap_allocator!(72 * 1024); let timg0 = TimerGroup::new(peripherals.TIMG0); + let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let init = esp_wifi::init( - timg0.timer0, - esp_hal::rng::Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); #[cfg(not(feature = "esp32"))] { @@ -43,5 +39,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_bas_peripheral::run::<_, { consts::L2CAP_MTU }>(controller).await; + ble_bas_peripheral::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/esp32/src/bin/ble_bas_peripheral_sec.rs b/examples/esp32/src/bin/ble_bas_peripheral_sec.rs index a6c65c7f..7cdf4de1 100644 --- a/examples/esp32/src/bin/ble_bas_peripheral_sec.rs +++ b/examples/esp32/src/bin/ble_bas_peripheral_sec.rs @@ -23,8 +23,6 @@ async fn main(_s: Spawner) { let timg0 = TimerGroup::new(peripherals.TIMG0); let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let mut random_seed = [0u8; 32]; - rng.read(&mut random_seed); let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); @@ -42,5 +40,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_bas_peripheral_sec::run::<_, { consts::L2CAP_MTU }>(controller, &random_seed).await; + ble_bas_peripheral_sec::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/esp32/src/bin/ble_l2cap_central.rs b/examples/esp32/src/bin/ble_l2cap_central.rs index 03024fe3..f111e57a 100644 --- a/examples/esp32/src/bin/ble_l2cap_central.rs +++ b/examples/esp32/src/bin/ble_l2cap_central.rs @@ -21,13 +21,9 @@ async fn main(_s: Spawner) { }); esp_alloc::heap_allocator!(72 * 1024); let timg0 = TimerGroup::new(peripherals.TIMG0); + let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let init = esp_wifi::init( - timg0.timer0, - esp_hal::rng::Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); #[cfg(not(feature = "esp32"))] { @@ -43,5 +39,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_l2cap_central::run::<_, { consts::L2CAP_MTU }>(controller).await; + ble_l2cap_central::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/esp32/src/bin/ble_l2cap_peripheral.rs b/examples/esp32/src/bin/ble_l2cap_peripheral.rs index 4a0d19e0..1fa21564 100644 --- a/examples/esp32/src/bin/ble_l2cap_peripheral.rs +++ b/examples/esp32/src/bin/ble_l2cap_peripheral.rs @@ -21,13 +21,9 @@ async fn main(_s: Spawner) { }); esp_alloc::heap_allocator!(72 * 1024); let timg0 = TimerGroup::new(peripherals.TIMG0); + let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let init = esp_wifi::init( - timg0.timer0, - esp_hal::rng::Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); #[cfg(not(feature = "esp32"))] { @@ -43,5 +39,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_l2cap_peripheral::run::<_, { consts::L2CAP_MTU }>(controller).await; + ble_l2cap_peripheral::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/esp32/src/bin/ble_scanner.rs b/examples/esp32/src/bin/ble_scanner.rs index 73d6ed5e..f97d748c 100644 --- a/examples/esp32/src/bin/ble_scanner.rs +++ b/examples/esp32/src/bin/ble_scanner.rs @@ -21,13 +21,9 @@ async fn main(_s: Spawner) { }); esp_alloc::heap_allocator!(72 * 1024); let timg0 = TimerGroup::new(peripherals.TIMG0); + let mut rng = esp_hal::rng::Trng::new(peripherals.RNG, peripherals.ADC1); - let init = esp_wifi::init( - timg0.timer0, - esp_hal::rng::Rng::new(peripherals.RNG), - peripherals.RADIO_CLK, - ) - .unwrap(); + let init = esp_wifi::init(timg0.timer0, rng.rng.clone(), peripherals.RADIO_CLK).unwrap(); #[cfg(not(feature = "esp32"))] { @@ -43,5 +39,5 @@ async fn main(_s: Spawner) { let connector = BleConnector::new(&init, bluetooth); let controller: ExternalController<_, 20> = ExternalController::new(connector); - ble_scanner::run::<_, { consts::L2CAP_MTU }>(controller).await; + ble_scanner::run::<_, _, { consts::L2CAP_MTU }>(controller, &mut rng).await; } diff --git a/examples/nrf-sdc/.cargo/config.toml b/examples/nrf-sdc/.cargo/config.toml index 31442eb6..92871155 100644 --- a/examples/nrf-sdc/.cargo/config.toml +++ b/examples/nrf-sdc/.cargo/config.toml @@ -1,7 +1,7 @@ [target.'cfg(all(target_arch = "arm", target_os = "none"))'] #runner = "probe-rs run --chip nRF52832_xxAA" #runner = "probe-rs run --chip nRF52833_xxAA" -runner = "probe-rs run --chip nRF52840_xxAA" +runner = ["probe-rs", "run", "--log-format", "{t} {L} {f:>10}: {s}", "--chip", "nRF52833_xxAA"] [build] # Pick ONE of these compilation targets diff --git a/examples/nrf-sdc/Cargo.lock b/examples/nrf-sdc/Cargo.lock index b45745e8..628c0365 100644 --- a/examples/nrf-sdc/Cargo.lock +++ b/examples/nrf-sdc/Cargo.lock @@ -1323,6 +1323,7 @@ dependencies = [ "embedded-hal 1.0.0", "embedded-io", "heapless", + "rand_core", "static_cell", "trouble-host", ] @@ -1379,6 +1380,8 @@ dependencies = [ "nrf-sdc", "panic-probe", "rand", + "rand_chacha", + "rand_core", "static_cell", "trouble-example-apps", ] diff --git a/examples/nrf-sdc/Cargo.toml b/examples/nrf-sdc/Cargo.toml index cbead6b9..8c638c52 100644 --- a/examples/nrf-sdc/Cargo.toml +++ b/examples/nrf-sdc/Cargo.toml @@ -25,6 +25,8 @@ cortex-m-rt = "0.7.0" panic-probe = { version = "0.3", features = ["print-defmt"] } rand = { version = "0.8.4", default-features = false } static_cell = "2" +rand_core = { version = "0.6"} +rand_chacha = { version = "0.3", default-features = false } [profile.release] debug = 2 diff --git a/examples/nrf-sdc/src/bin/ble_bas_central.rs b/examples/nrf-sdc/src/bin/ble_bas_central.rs index 2f92719e..1bc8a839 100644 --- a/examples/nrf-sdc/src/bin/ble_bas_central.rs +++ b/examples/nrf-sdc/src/bin/ble_bas_central.rs @@ -7,6 +7,8 @@ use embassy_nrf::peripherals::RNG; use embassy_nrf::{bind_interrupts, rng}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_central; use {defmt_rtt as _, panic_probe as _}; @@ -69,9 +71,10 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<6544>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); - ble_bas_central::run::<_, L2CAP_MTU>(sdc).await; + ble_bas_central::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/nrf-sdc/src/bin/ble_bas_central_sec.rs b/examples/nrf-sdc/src/bin/ble_bas_central_sec.rs index d5f7d4a8..2fc43118 100644 --- a/examples/nrf-sdc/src/bin/ble_bas_central_sec.rs +++ b/examples/nrf-sdc/src/bin/ble_bas_central_sec.rs @@ -7,6 +7,8 @@ use embassy_nrf::peripherals::RNG; use embassy_nrf::{bind_interrupts, rng}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_central_sec; use {defmt_rtt as _, panic_probe as _}; @@ -69,11 +71,10 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); - let mut random_seed = [0u8; 32]; - rng.fill_bytes(&mut random_seed).await; + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<6544>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); - ble_bas_central_sec::run::<_, L2CAP_MTU>(sdc, &random_seed).await; + ble_bas_central_sec::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs b/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs index 01a4586d..f94f45b2 100644 --- a/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs +++ b/examples/nrf-sdc/src/bin/ble_bas_peripheral.rs @@ -7,6 +7,8 @@ use embassy_nrf::peripherals::RNG; use embassy_nrf::{bind_interrupts, rng}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_peripheral; use {defmt_rtt as _, panic_probe as _}; @@ -69,9 +71,10 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<3312>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); - ble_bas_peripheral::run::<_, L2CAP_MTU>(sdc).await; + ble_bas_peripheral::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/nrf-sdc/src/bin/ble_bas_peripheral_sec.rs b/examples/nrf-sdc/src/bin/ble_bas_peripheral_sec.rs index d8a2f6b8..8f069cb1 100644 --- a/examples/nrf-sdc/src/bin/ble_bas_peripheral_sec.rs +++ b/examples/nrf-sdc/src/bin/ble_bas_peripheral_sec.rs @@ -7,6 +7,8 @@ use embassy_nrf::peripherals::RNG; use embassy_nrf::{bind_interrupts, rng}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_peripheral_sec; use {defmt_rtt as _, panic_probe as _}; @@ -69,11 +71,10 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); - let mut random_seed = [0u8; 32]; - rng.fill_bytes(&mut random_seed).await; + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<3312>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); - ble_bas_peripheral_sec::run::<_, L2CAP_MTU>(sdc, &random_seed).await; + ble_bas_peripheral_sec::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/nrf-sdc/src/bin/ble_l2cap_central.rs b/examples/nrf-sdc/src/bin/ble_l2cap_central.rs index 50661827..efd034f3 100644 --- a/examples/nrf-sdc/src/bin/ble_l2cap_central.rs +++ b/examples/nrf-sdc/src/bin/ble_l2cap_central.rs @@ -8,6 +8,8 @@ use embassy_nrf::{bind_interrupts, rng}; use embassy_time::{Duration, Timer}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_l2cap_central; use {defmt_rtt as _, panic_probe as _}; @@ -70,11 +72,12 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<6544>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); Timer::after(Duration::from_millis(200)).await; - ble_l2cap_central::run::<_, L2CAP_MTU>(sdc).await; + ble_l2cap_central::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs b/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs index a102dc18..14c60986 100644 --- a/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs +++ b/examples/nrf-sdc/src/bin/ble_l2cap_peripheral.rs @@ -7,6 +7,8 @@ use embassy_nrf::peripherals::RNG; use embassy_nrf::{bind_interrupts, rng}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_l2cap_peripheral; use {defmt_rtt as _, panic_probe as _}; @@ -68,9 +70,10 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<12848>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); - ble_l2cap_peripheral::run::<_, L2CAP_MTU>(sdc).await; + ble_l2cap_peripheral::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/nrf-sdc/src/bin/ble_scanner.rs b/examples/nrf-sdc/src/bin/ble_scanner.rs index 63c1b4e3..b6434e10 100644 --- a/examples/nrf-sdc/src/bin/ble_scanner.rs +++ b/examples/nrf-sdc/src/bin/ble_scanner.rs @@ -8,6 +8,8 @@ use embassy_nrf::{bind_interrupts, rng}; use embassy_time::{Duration, Timer}; use nrf_sdc::mpsl::MultiprotocolServiceLayer; use nrf_sdc::{self as sdc, mpsl}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_scanner; use {defmt_rtt as _, panic_probe as _}; @@ -64,11 +66,12 @@ async fn main(spawner: Spawner) { ); let mut rng = rng::Rng::new(p.RNG, Irqs); + let mut rng_2 = ChaCha12Rng::from_rng(&mut rng).unwrap(); let mut sdc_mem = sdc::Mem::<2712>::new(); let sdc = unwrap!(build_sdc(sdc_p, &mut rng, mpsl, &mut sdc_mem)); Timer::after(Duration::from_millis(200)).await; - ble_scanner::run::<_, L2CAP_MTU>(sdc).await; + ble_scanner::run::<_, _, L2CAP_MTU>(sdc, &mut rng_2).await; } diff --git a/examples/rp-pico-2-w/Cargo.toml b/examples/rp-pico-2-w/Cargo.toml index 74755627..92d30aeb 100644 --- a/examples/rp-pico-2-w/Cargo.toml +++ b/examples/rp-pico-2-w/Cargo.toml @@ -25,6 +25,8 @@ cortex-m-rt = "0.7" panic-probe = { version = "0.3", features = ["print-defmt"] } static_cell = "2" portable-atomic = { version = "1.5", features = ["critical-section"] } +rand_core = { version = "0.6"} +rand_chacha = { version = "0.3", default-features = false } [build-dependencies] reqwest = { version = "0.12.9", features = ["blocking"]} diff --git a/examples/rp-pico-2-w/src/bin/ble_bas_central.rs b/examples/rp-pico-2-w/src/bin/ble_bas_central.rs index d2f155a8..cfdb3b80 100644 --- a/examples/rp-pico-2-w/src/bin/ble_bas_central.rs +++ b/examples/rp-pico-2-w/src/bin/ble_bas_central.rs @@ -6,9 +6,12 @@ use cyw43_pio::{PioSpi, RM2_CLOCK_DIVIDER}; use defmt::*; use embassy_executor::Spawner; use embassy_rp::bind_interrupts; +use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Level, Output}; use embassy_rp::peripherals::{DMA_CH0, PIO0}; use embassy_rp::pio::{InterruptHandler, Pio}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_central; use {defmt_rtt as _, embassy_time as _, panic_probe as _}; @@ -63,7 +66,9 @@ async fn main(spawner: Spawner) { unwrap!(spawner.spawn(cyw43_task(runner))); control.init(clm).await; + let mut rng = ChaCha12Rng::from_rng(&mut RoscRng).unwrap(); + let controller: ExternalController<_, 10> = ExternalController::new(bt_device); - ble_bas_central::run::<_, 128>(controller).await; + ble_bas_central::run::<_, _, 128>(controller, &mut rng).await; } diff --git a/examples/rp-pico-2-w/src/bin/ble_bas_peripheral.rs b/examples/rp-pico-2-w/src/bin/ble_bas_peripheral.rs index 91e28e3e..3f24fe26 100644 --- a/examples/rp-pico-2-w/src/bin/ble_bas_peripheral.rs +++ b/examples/rp-pico-2-w/src/bin/ble_bas_peripheral.rs @@ -6,9 +6,12 @@ use cyw43_pio::{PioSpi, RM2_CLOCK_DIVIDER}; use defmt::*; use embassy_executor::Spawner; use embassy_rp::bind_interrupts; +use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Level, Output}; use embassy_rp::peripherals::{DMA_CH0, PIO0}; use embassy_rp::pio::{InterruptHandler, Pio}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_peripheral; use {defmt_rtt as _, panic_probe as _}; @@ -63,7 +66,9 @@ async fn main(spawner: Spawner) { unwrap!(spawner.spawn(cyw43_task(runner))); control.init(clm).await; + let mut rng = ChaCha12Rng::from_rng(&mut RoscRng).unwrap(); + let controller: ExternalController<_, 10> = ExternalController::new(bt_device); - ble_bas_peripheral::run::<_, 128>(controller).await; + ble_bas_peripheral::run::<_, _, 128>(controller, &mut rng).await; } diff --git a/examples/rp-pico-w/Cargo.lock b/examples/rp-pico-w/Cargo.lock index 86292e78..549bbd85 100644 --- a/examples/rp-pico-w/Cargo.lock +++ b/examples/rp-pico-w/Cargo.lock @@ -1632,6 +1632,15 @@ dependencies = [ "critical-section", ] +[[package]] +name = "ppv-lite86" +version = "0.2.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77957b295656769bb8ad2b6a6b09d897d94f05c41b069aede1fcdaa675eaea04" +dependencies = [ + "zerocopy", +] + [[package]] name = "precomputed-hash" version = "0.1.1" @@ -1702,6 +1711,16 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + [[package]] name = "rand_core" version = "0.6.4" @@ -2396,6 +2415,7 @@ dependencies = [ "embedded-hal 1.0.0", "embedded-io", "heapless", + "rand_core", "static_cell", "trouble-host", ] @@ -2412,6 +2432,7 @@ dependencies = [ "embedded-io", "futures", "heapless", + "rand_core", "static_cell", "trouble-host-macros", ] @@ -2447,6 +2468,8 @@ dependencies = [ "futures", "panic-probe", "portable-atomic", + "rand_chacha", + "rand_core", "reqwest", "static_cell", "trouble-example-apps", @@ -2821,6 +2844,27 @@ dependencies = [ "synstructure", ] +[[package]] +name = "zerocopy" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b9b4fd18abc82b8136838da5d50bae7bdea537c574d8dc1a34ed098d6c166f0" +dependencies = [ + "byteorder", + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.35" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.93", +] + [[package]] name = "zerofrom" version = "0.1.5" diff --git a/examples/rp-pico-w/Cargo.toml b/examples/rp-pico-w/Cargo.toml index a4d2f181..3af09273 100644 --- a/examples/rp-pico-w/Cargo.toml +++ b/examples/rp-pico-w/Cargo.toml @@ -25,6 +25,8 @@ cortex-m-rt = "0.7.0" panic-probe = { version = "0.3", features = ["print-defmt"] } static_cell = "2" portable-atomic = { version = "1.5", features = ["critical-section"] } +rand_core = { version = "0.6"} +rand_chacha = { version = "0.3", default-features = false } [build-dependencies] reqwest = { version = "0.12.9", features = ["blocking"]} diff --git a/examples/rp-pico-w/src/bin/ble_bas_central.rs b/examples/rp-pico-w/src/bin/ble_bas_central.rs index 3e2cd7f4..e93e7c69 100644 --- a/examples/rp-pico-w/src/bin/ble_bas_central.rs +++ b/examples/rp-pico-w/src/bin/ble_bas_central.rs @@ -6,9 +6,12 @@ use cyw43_pio::PioSpi; use defmt::*; use embassy_executor::Spawner; use embassy_rp::bind_interrupts; +use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Level, Output}; use embassy_rp::peripherals::{DMA_CH0, PIO0}; use embassy_rp::pio::{InterruptHandler, Pio}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_central; use {defmt_rtt as _, embassy_time as _, panic_probe as _}; @@ -63,7 +66,9 @@ async fn main(spawner: Spawner) { unwrap!(spawner.spawn(cyw43_task(runner))); control.init(clm).await; + let mut rng = ChaCha12Rng::from_rng(&mut RoscRng).unwrap(); + let controller: ExternalController<_, 10> = ExternalController::new(bt_device); - ble_bas_central::run::<_, 128>(controller).await; + ble_bas_central::run::<_, _, 128>(controller, &mut rng).await; } diff --git a/examples/rp-pico-w/src/bin/ble_bas_peripheral.rs b/examples/rp-pico-w/src/bin/ble_bas_peripheral.rs index 86084861..fb034ed4 100644 --- a/examples/rp-pico-w/src/bin/ble_bas_peripheral.rs +++ b/examples/rp-pico-w/src/bin/ble_bas_peripheral.rs @@ -6,9 +6,12 @@ use cyw43_pio::PioSpi; use defmt::*; use embassy_executor::Spawner; use embassy_rp::bind_interrupts; +use embassy_rp::clocks::RoscRng; use embassy_rp::gpio::{Level, Output}; use embassy_rp::peripherals::{DMA_CH0, PIO0}; use embassy_rp::pio::{InterruptHandler, Pio}; +use rand_chacha::ChaCha12Rng; +use rand_core::SeedableRng; use static_cell::StaticCell; use trouble_example_apps::ble_bas_peripheral; use {defmt_rtt as _, embassy_time as _, panic_probe as _}; @@ -63,7 +66,9 @@ async fn main(spawner: Spawner) { unwrap!(spawner.spawn(cyw43_task(runner))); control.init(clm).await; + let mut rng = ChaCha12Rng::from_rng(&mut RoscRng).unwrap(); + let controller: ExternalController<_, 10> = ExternalController::new(bt_device); - ble_bas_peripheral::run::<_, 128>(controller).await; + ble_bas_peripheral::run::<_, _, 128>(controller, &mut rng).await; } diff --git a/examples/serial-hci/Cargo.lock b/examples/serial-hci/Cargo.lock index 5a426898..968dc8a1 100644 --- a/examples/serial-hci/Cargo.lock +++ b/examples/serial-hci/Cargo.lock @@ -1197,6 +1197,7 @@ dependencies = [ "embedded-io", "heapless", "log", + "rand_core", "static_cell", "trouble-host", ] diff --git a/examples/serial-hci/src/bin/ble_bas_central_sec.rs b/examples/serial-hci/src/bin/ble_bas_central_sec.rs index 0069c96b..68d5e954 100644 --- a/examples/serial-hci/src/bin/ble_bas_central_sec.rs +++ b/examples/serial-hci/src/bin/ble_bas_central_sec.rs @@ -3,6 +3,7 @@ use bt_hci::controller::ExternalController; use bt_hci::transport::SerialTransport; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use log::*; +use rand::rngs::OsRng; use tokio::time::Duration; use tokio_serial::{DataBits, Parity, SerialStream, StopBits}; use trouble_example_apps::ble_bas_central_sec; @@ -51,7 +52,5 @@ async fn main() { let driver: SerialTransport = SerialTransport::new(reader, writer); let controller: ExternalController<_, 10> = ExternalController::new(driver); - let random_seed = rand::random::<[u8; 32]>(); - - ble_bas_central_sec::run::<_, 128>(controller, &random_seed).await; + ble_bas_central_sec::run::<_, _, 128>(controller, &mut OsRng).await; } diff --git a/examples/serial-hci/src/bin/ble_bas_peripheral.rs b/examples/serial-hci/src/bin/ble_bas_peripheral.rs index 35fe6612..2568f4d7 100644 --- a/examples/serial-hci/src/bin/ble_bas_peripheral.rs +++ b/examples/serial-hci/src/bin/ble_bas_peripheral.rs @@ -3,6 +3,7 @@ use bt_hci::controller::ExternalController; use bt_hci::transport::SerialTransport; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use log::*; +use rand::rngs::OsRng; use tokio::time::Duration; use tokio_serial::{DataBits, Parity, SerialStream, StopBits}; use trouble_example_apps::ble_bas_peripheral; @@ -51,5 +52,5 @@ async fn main() { let driver: SerialTransport = SerialTransport::new(reader, writer); let controller: ExternalController<_, 10> = ExternalController::new(driver); - ble_bas_peripheral::run::<_, 128>(controller).await; + ble_bas_peripheral::run::<_, _, 128>(controller, &mut OsRng).await; } diff --git a/examples/serial-hci/src/bin/ble_bas_peripheral_sec.rs b/examples/serial-hci/src/bin/ble_bas_peripheral_sec.rs index 181912c6..894628ca 100644 --- a/examples/serial-hci/src/bin/ble_bas_peripheral_sec.rs +++ b/examples/serial-hci/src/bin/ble_bas_peripheral_sec.rs @@ -3,6 +3,7 @@ use bt_hci::controller::ExternalController; use bt_hci::transport::SerialTransport; use embassy_sync::blocking_mutex::raw::NoopRawMutex; use log::*; +use rand::rngs::OsRng; use tokio::time::Duration; use tokio_serial::{DataBits, Parity, SerialStream, StopBits}; use trouble_example_apps::ble_bas_peripheral_sec; @@ -51,7 +52,5 @@ async fn main() { let driver: SerialTransport = SerialTransport::new(reader, writer); let controller: ExternalController<_, 10> = ExternalController::new(driver); - let random_seed = rand::random::<[u8; 32]>(); - - ble_bas_peripheral_sec::run::<_, 128>(controller, &random_seed).await; + ble_bas_peripheral_sec::run::<_, _, 128>(controller, &mut OsRng).await; } diff --git a/examples/tests/tests/ble_l2cap_central.rs b/examples/tests/tests/ble_l2cap_central.rs index ccc620c8..eebc2e46 100644 --- a/examples/tests/tests/ble_l2cap_central.rs +++ b/examples/tests/tests/ble_l2cap_central.rs @@ -49,7 +49,8 @@ async fn run_l2cap_central_test(labels: &[(&str, &str)], firmware: &str) { let controller_peripheral = serial::create_controller(&peripheral).await; let mut resources: HostResources<2, 4, 27> = HostResources::new(); - let stack = trouble_host::new(controller_peripheral, &mut resources).set_random_address(peripheral_address); + let stack = trouble_host::new(controller_peripheral, &mut resources, &mut rand::rngs::OsRng) + .set_random_address(peripheral_address); let Host { mut peripheral, mut runner, diff --git a/examples/tests/tests/ble_l2cap_peripheral.rs b/examples/tests/tests/ble_l2cap_peripheral.rs index 4601c0bd..2edd9786 100644 --- a/examples/tests/tests/ble_l2cap_peripheral.rs +++ b/examples/tests/tests/ble_l2cap_peripheral.rs @@ -46,7 +46,7 @@ async fn run_l2cap_peripheral_test(labels: &[(&str, &str)], firmware: &str) { let central = tokio::task::spawn_local(async move { let controller_central = serial::create_controller(¢ral).await; let mut resources: HostResources<2, 4, 27> = HostResources::new(); - let stack = trouble_host::new(controller_central, &mut resources); + let stack = trouble_host::new(controller_central, &mut resources, &mut rand::rngs::OsRng); let Host { mut central, mut runner, diff --git a/host/src/channel_manager.rs b/host/src/channel_manager.rs index 530c46b7..ad2833b5 100644 --- a/host/src/channel_manager.rs +++ b/host/src/channel_manager.rs @@ -1019,7 +1019,7 @@ mod tests { let mut resources: HostResources<2, 2, 27> = HostResources::new(); let ble = MockController::new(); - let builder = crate::new(ble, &mut resources); + let builder = crate::new(ble, &mut resources, &mut rand::rngs::OsRng); let ble = builder.host; let conn = ConnHandle::new(33); diff --git a/host/src/connection_manager.rs b/host/src/connection_manager.rs index 9d8efb26..618b44f1 100644 --- a/host/src/connection_manager.rs +++ b/host/src/connection_manager.rs @@ -83,6 +83,7 @@ impl<'d> ConnectionManager<'d> { events: &'d mut [EventChannel], default_att_mtu: u16, #[cfg(feature = "gatt")] tx_pool: &'d dyn Pool, + _random_seed: [u8; 32], ) -> Self { Self { state: RefCell::new(State { @@ -98,7 +99,7 @@ impl<'d> ConnectionManager<'d> { #[cfg(feature = "gatt")] tx_pool, #[cfg(feature = "security")] - security_manager: SecurityManager::new(), + security_manager: SecurityManager::new(_random_seed), } } @@ -796,10 +797,14 @@ mod tests { const ADDR_2: [u8; 6] = [0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]; fn setup() -> &'static ConnectionManager<'static> { + let _random_seed = [0u8; 32]; let storage = Box::leak(Box::new([ConnectionStorage::DISCONNECTED; 3])); let events = Box::leak(Box::new([EventChannel::NEW; 3])); let pool = Box::leak(Box::new(PacketPool::<27, 8>::new())); - let mgr = ConnectionManager::new(&mut storage[..], &mut events[..], 23, pool); + #[cfg(feature = "gatt")] + let mgr = ConnectionManager::new(&mut storage[..], &mut events[..], 23, pool, _random_seed); + #[cfg(not(feature = "gatt"))] + let mgr = ConnectionManager::new(&mut storage[..], &mut events[..], 23, pool, _random_seed); Box::leak(Box::new(mgr)) } diff --git a/host/src/host.rs b/host/src/host.rs index ef5fa9d4..6e5417cc 100644 --- a/host/src/host.rs +++ b/host/src/host.rs @@ -206,6 +206,7 @@ where channels_rx: &'d mut [PacketChannel<{ config::L2CAP_RX_QUEUE_SIZE }>], sar: &'d mut [SarType], advertise_handles: &'d mut [AdvHandleState], + random_seed: [u8; 32], ) -> Self { Self { address: None, @@ -213,9 +214,9 @@ where metrics: RefCell::new(HostMetrics::default()), controller, #[cfg(feature = "gatt")] - connections: ConnectionManager::new(connections, events, rx_pool.mtu() as u16 - 4, tx_pool), + connections: ConnectionManager::new(connections, events, rx_pool.mtu() as u16 - 4, tx_pool, random_seed), #[cfg(not(feature = "gatt"))] - connections: ConnectionManager::new(connections, events, rx_pool.mtu() as u16 - 4), + connections: ConnectionManager::new(connections, events, rx_pool.mtu() as u16 - 4, random_seed), reassembly: PacketReassembly::new(sar), channels: ChannelManager::new(rx_pool, channels, channels_rx), rx_pool, diff --git a/host/src/lib.rs b/host/src/lib.rs index 5aef490f..3c42253f 100644 --- a/host/src/lib.rs +++ b/host/src/lib.rs @@ -23,6 +23,7 @@ use crate::connection_manager::{ConnectionStorage, EventChannel}; use crate::l2cap::sar::SarType; use crate::packet_pool::PacketPool; use bt_hci::param::{AddrKind, BdAddr}; +use rand_core::{CryptoRng, RngCore}; mod fmt; @@ -401,6 +402,7 @@ impl( controller: C, resources: &'resources mut HostResources, + random_generator: &mut RNG, ) -> Stack<'resources, C> { unsafe fn transmute_slice(x: &mut [T]) -> &'static mut [T] { core::mem::transmute(x) @@ -448,6 +451,8 @@ pub fn new< let sar: &'static mut [Option<(ConnHandle, L2capHeader, AssembledPacket)>] = unsafe { transmute_slice(sar) }; let advertise_handles = &mut *resources.advertise_handles.write([AdvHandleState::None; ADV_SETS]); let advertise_handles: &'static mut [AdvHandleState] = unsafe { transmute_slice(advertise_handles) }; + let mut random_seed = [0u8; 32]; + random_generator.fill_bytes(&mut random_seed); let host: BleHost<'_, C> = BleHost::new( controller, rx_pool, @@ -459,6 +464,7 @@ pub fn new< channels_rx, sar, advertise_handles, + random_seed, ); Stack { host } @@ -490,12 +496,6 @@ impl<'stack, C: Controller> Stack<'stack, C> { self.host.connections.security_manager.set_local_address(address); self } - /// Set the random seed - pub fn set_random_seed(self, random_seed: &[u8; 32]) -> Self { - #[cfg(feature = "security")] - self.host.connections.security_manager.set_random_seed(random_seed); - self - } /// Build the stack. pub fn build(&'stack self) -> Host<'stack, C> { diff --git a/host/src/security_manager/mod.rs b/host/src/security_manager/mod.rs index 690c1236..2c126836 100644 --- a/host/src/security_manager/mod.rs +++ b/host/src/security_manager/mod.rs @@ -293,11 +293,10 @@ enum TimerCommand { } impl SecurityManager { - const NON_SECURE_RANDOM_SEED: [u8; 32] = [0; 32]; /// Create a new SecurityManager - pub(crate) fn new() -> Self { + pub(crate) fn new(random_seed: [u8; 32]) -> Self { Self { - rng: RefCell::new(ChaCha12Rng::from_seed(Self::NON_SECURE_RANDOM_SEED)), + rng: RefCell::new(ChaCha12Rng::from_seed(random_seed)), state: RefCell::new(SecurityManagerData::new()), events: Channel::new(), pairing_state: RefCell::new(PairingData::new()), @@ -310,11 +309,6 @@ impl SecurityManager { self.state.borrow_mut().local_address = Some(address); } - /// Set random seed - pub(crate) fn set_random_seed(&self, random_seed: &[u8; 32]) { - self.rng.replace(ChaCha12Rng::from_seed(*random_seed)); - } - /// Get the long term key from the latests pairing pub(crate) fn get_long_term_key(&self) -> Option<[u8; 16]> { self.pairing_state.borrow().ltk.map(|ltk| ltk.to_le_bytes()) diff --git a/host/tests/gatt.rs b/host/tests/gatt.rs index 7d12d128..5d45aea3 100644 --- a/host/tests/gatt.rs +++ b/host/tests/gatt.rs @@ -32,7 +32,7 @@ async fn gatt_client_server() { let controller_peripheral = common::create_controller(&peripheral).await; let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller_peripheral, &mut resources) + let stack = trouble_host::new(controller_peripheral, &mut resources, &mut rand::rngs::OsRng) .set_random_address(peripheral_address); let Host { mut peripheral, @@ -132,7 +132,7 @@ async fn gatt_client_server() { let central = local.spawn_local(async move { let controller_central = common::create_controller(¢ral).await; let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller_central, &mut resources); + let stack = trouble_host::new(controller_central, &mut resources, &mut rand::rngs::OsRng); let Host { mut central, mut runner, diff --git a/host/tests/gatt_derive.rs b/host/tests/gatt_derive.rs index 301b8202..1ccf1dfb 100644 --- a/host/tests/gatt_derive.rs +++ b/host/tests/gatt_derive.rs @@ -74,7 +74,7 @@ async fn gatt_client_server() { let controller_peripheral = common::create_controller(&peripheral).await; let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller_peripheral, &mut resources) + let stack = trouble_host::new(controller_peripheral, &mut resources, &mut rand::rngs::OsRng) .set_random_address(peripheral_address); let Host { mut peripheral, @@ -163,7 +163,7 @@ async fn gatt_client_server() { let central = local.spawn_local(async move { let controller_central = common::create_controller(¢ral).await; let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller_central, &mut resources); + let stack = trouble_host::new(controller_central, &mut resources, &mut rand::rngs::OsRng); let Host { mut central, mut runner, diff --git a/host/tests/l2cap.rs b/host/tests/l2cap.rs index d9944d63..79b04e89 100644 --- a/host/tests/l2cap.rs +++ b/host/tests/l2cap.rs @@ -27,7 +27,7 @@ async fn l2cap_connection_oriented_channels() { let controller_peripheral = common::create_controller(&peripheral).await; let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller_peripheral, &mut resources) + let stack = trouble_host::new(controller_peripheral, &mut resources, &mut rand::rngs::OsRng) .set_random_address(peripheral_address); let Host { mut peripheral, @@ -94,7 +94,7 @@ async fn l2cap_connection_oriented_channels() { let controller_central = common::create_controller(¢ral).await; let mut resources: HostResources = HostResources::new(); - let stack = trouble_host::new(controller_central, &mut resources); + let stack = trouble_host::new(controller_central, &mut resources, &mut rand::rngs::OsRng); let Host { mut central, mut runner,