Skip to content

Commit

Permalink
Use CryptoRng to seed internal ChaCha12
Browse files Browse the repository at this point in the history
  • Loading branch information
blueluna committed Feb 21, 2025
1 parent cb46609 commit a2679a3
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 88 deletions.
7 changes: 5 additions & 2 deletions examples/apps/src/ble_advertise_multiple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<C, const L2CAP_MTU: usize>(controller: C)
pub async fn run<C, RNG, const L2CAP_MTU: usize>(controller: C, random_generator: &mut RNG)
where
C: Controller
+ for<'t> ControllerCmdSync<LeSetExtAdvData<'t>>
Expand All @@ -20,12 +21,14 @@ where
+ ControllerCmdSync<LeReadNumberOfSupportedAdvSets>
+ for<'t> ControllerCmdSync<LeSetExtAdvEnable<'t>>
+ for<'t> ControllerCmdSync<LeSetExtScanResponseData<'t>>,
RNG: RngCore + CryptoRng,
{
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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,
Expand Down
7 changes: 5 additions & 2 deletions examples/apps/src/ble_bas_central.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,17 +9,19 @@ const CONNECTIONS_MAX: usize = 1;
/// Max number of L2CAP channels.
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
pub async fn run<C, RNG, const L2CAP_MTU: usize>(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).
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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,
Expand Down
9 changes: 5 additions & 4 deletions examples/apps/src/ble_bas_central_sec.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,19 +9,19 @@ const CONNECTIONS_MAX: usize = 1;
/// Max number of L2CAP channels.
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

pub async fn run<C, const L2CAP_MTU: usize>(controller: C, random_seed: &[u8; 32])
pub async fn run<C, RNG, const L2CAP_MTU: usize>(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).
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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,
Expand Down
7 changes: 5 additions & 2 deletions examples/apps/src/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,17 +29,19 @@ struct BatteryService {
}

/// Run the BLE stack.
pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
pub async fn run<C, RNG, const L2CAP_MTU: usize>(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).
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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();
Expand Down
9 changes: 5 additions & 4 deletions examples/apps/src/ble_bas_peripheral_sec.rs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -28,19 +29,19 @@ struct BatteryService {
}

/// Run the BLE stack.
pub async fn run<C, const L2CAP_MTU: usize>(controller: C, random_seed: &[u8; 32])
pub async fn run<C, RNG, const L2CAP_MTU: usize>(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).
let address: Address = Address::random([0xff, 0x8f, 0x1a, 0x05, 0xe4, 0xff]);
info!("Our address = {}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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();
Expand Down
7 changes: 5 additions & 2 deletions examples/apps/src/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -8,17 +9,19 @@ const CONNECTIONS_MAX: usize = 1;
/// Max number of L2CAP channels.
const L2CAP_CHANNELS_MAX: usize = 3; // Signal + att + CoC

pub async fn run<C, const L2CAP_MTU: usize>(controller: C)
pub async fn run<C, RNG, const L2CAP_MTU: usize>(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).
let address: Address = Address::random([0xff, 0x8f, 0x1b, 0x05, 0xe4, 0xff]);
info!("Our address = {:?}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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,
Expand Down
7 changes: 5 additions & 2 deletions examples/apps/src/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<C, const L2CAP_MTU: usize>(controller: C)
pub async fn run<C, RNG, const L2CAP_MTU: usize>(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<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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,
Expand Down
7 changes: 5 additions & 2 deletions examples/apps/src/ble_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<C, const L2CAP_MTU: usize>(controller: C)
pub async fn run<C, RNG, const L2CAP_MTU: usize>(controller: C, random_generator: &mut RNG)
where
C: Controller + ControllerCmdSync<LeSetScanParams>,
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).
Expand All @@ -21,7 +23,8 @@ where
info!("Our address = {:?}", address);

let mut resources: HostResources<CONNECTIONS_MAX, L2CAP_CHANNELS_MAX, L2CAP_MTU> = 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, ..
Expand Down
1 change: 1 addition & 0 deletions examples/esp32/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions examples/esp32/src/bin/ble_bas_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand All @@ -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;
}
4 changes: 1 addition & 3 deletions examples/esp32/src/bin/ble_bas_central_sec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}
10 changes: 3 additions & 7 deletions examples/esp32/src/bin/ble_bas_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand All @@ -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;
}
4 changes: 1 addition & 3 deletions examples/esp32/src/bin/ble_bas_peripheral_sec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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;
}
10 changes: 3 additions & 7 deletions examples/esp32/src/bin/ble_l2cap_central.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand All @@ -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;
}
10 changes: 3 additions & 7 deletions examples/esp32/src/bin/ble_l2cap_peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand All @@ -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;
}
10 changes: 3 additions & 7 deletions examples/esp32/src/bin/ble_scanner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
{
Expand All @@ -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;
}
Loading

0 comments on commit a2679a3

Please sign in to comment.