Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lib lpc55 rng seed #1820

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions app/lpc55xpresso/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,11 @@ task-slots = ["gpio_driver", "syscon_driver"]
[tasks.rng_driver]
name = "drv-lpc55-rng"
priority = 3
max-sizes = {flash = 16384, ram = 4096}
uses = ["rng", "pmc"]
start = true
stacksize = 2200
stacksize = 3504
task-slots = ["syscon_driver"]
extern-regions = ["dice_certs", "dice_rng"]

[tasks.pong]
name = "task-pong"
Expand Down
9 changes: 9 additions & 0 deletions app/oxide-rot-1/app-dev.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ stacksize = 12304
start = true
extern-regions = ["dice_alias", "dice_certs"]

[tasks.rng_driver]
name = "drv-lpc55-rng"
priority = 6
uses = ["rng", "pmc"]
start = true
stacksize = 4200
task-slots = ["syscon_driver"]
extern-regions = ["dice_certs", "dice_rng"]

[signing.certs]
signing-certs = ["../../support/fake_certs/fake_certificate.der.crt"]
root-certs = ["../../support/fake_certs/fake_certificate.der.crt"]
Expand Down
4 changes: 2 additions & 2 deletions app/rot-carrier/app.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,11 @@ pins = [
[tasks.rng_driver]
name = "drv-lpc55-rng"
priority = 5
max-sizes = {flash = 16384, ram = 4096}
uses = ["rng", "pmc"]
start = true
stacksize = 2200
stacksize = 4200
task-slots = ["syscon_driver"]
extern-regions = ["dice_certs", "dice_rng"]

[tasks.sprot]
name = "drv-lpc55-sprot-server"
Expand Down
16 changes: 16 additions & 0 deletions chips/lpc55/memory.toml
Original file line number Diff line number Diff line change
Expand Up @@ -156,3 +156,19 @@ size = 0x800
read = true
write = true
execute = false

[[dice_rng]]
name = "a"
address =0x40101a00
size = 0x100
read = true
write = true
execute = false

[[dice_rng]]
name = "b"
address =0x40101a00
size = 0x100
read = true
write = true
execute = false
20 changes: 18 additions & 2 deletions drv/lpc55-rng/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,37 @@ edition = "2021"

[dependencies]
cfg-if = { workspace = true }
hubpack.workspace = true
idol-runtime = { workspace = true }
lpc55-pac = { workspace = true }
lpc55-pac.workspace = true
num-traits = { workspace = true }
rand_chacha = { workspace = true }
rand_core = { workspace = true }
serde.workspace = true
sha3.workspace = true
zerocopy = { workspace = true }
zeroize.workspace = true

drv-lpc55-syscon-api = { path = "../lpc55-syscon-api" }
drv-rng-api = { path = "../rng-api" }
lib-dice.path = "../../lib/dice"
lib-lpc55-rng.path = "../../lib/lpc55-rng"
mutable-statics.path = "../../lib/mutable-statics"
ringbuf.path = "../../lib/ringbuf"
stage0-handoff = { path = "../../lib/stage0-handoff", optional = true }
userlib = { path = "../../sys/userlib", features = ["panic-messages"] }

[build-dependencies]
idol = { workspace = true }
anyhow.workspace = true
build-util.path = "../../build/util"
cfg-if.workspace = true
idol.workspace = true
indexmap = { workspace = true, optional = true }
serde.workspace = true

[features]
default = ["dice-seed"]
dice-seed = ["indexmap", "stage0-handoff"]
no-ipc-counters = ["idol/no-counters"]

# This section is here to discourage RLS/rust-analyzer from doing test builds,
Expand Down
72 changes: 66 additions & 6 deletions drv/lpc55-rng/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,75 @@
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
use anyhow::{anyhow, Result};
use idol::{server::ServerStyle, CounterSettings};

cfg_if::cfg_if! {
if #[cfg(feature = "dice-seed")] {
mod data_region {
include!("src/data-region.rs");
}
use anyhow::Context;
use data_region::DataRegion;
use indexmap::IndexMap;
use std::{fs::File, io::Write};

const CFG_SRC: &str = "rng-config.rs";
}
}

#[cfg(feature = "dice-seed")]
fn extern_region_to_cfg<W: Write>(
out: &mut W,
data_regions: &IndexMap<String, DataRegion>,
name: &str,
) -> Result<()> {
let region = data_regions.get(name).ok_or_else(|| {
anyhow::anyhow!(format!("external region not found: {}", name))
})?;

Ok(writeln!(
out,
r##"pub const {}_REGION: DataRegion = DataRegion {{
address: {:#x},
size: {:#x},
}};"##,
name.to_uppercase(),
region.address,
region.size
)?)
}

#[cfg(feature = "dice-seed")]
fn extern_regions_to_cfg(path: &str) -> Result<()> {
let out_dir = build_util::out_dir();
let dest_path = out_dir.join(path);
let mut out =
File::create(dest_path).context(format!("creating {}", path))?;

let data_regions = build_util::task_extern_regions::<DataRegion>()?;
if data_regions.is_empty() {
return Err(anyhow!("no data regions found"));
}

writeln!(out, "use crate::data_region::DataRegion;\n\n")?;

extern_region_to_cfg(&mut out, &data_regions, "dice_certs")?;
extern_region_to_cfg(&mut out, &data_regions, "dice_rng")
}

fn main() -> Result<()> {
idol::Generator::new()
.with_counters(
idol::CounterSettings::default().with_server_counters(false),
)
.with_counters(CounterSettings::default().with_server_counters(false))
.build_server_support(
"../../idl/rng.idol",
"server_stub.rs",
idol::server::ServerStyle::InOrder,
)?;
ServerStyle::InOrder,
)
.map_err(|e| anyhow!(e))?;

#[cfg(feature = "dice-seed")]
extern_regions_to_cfg(CFG_SRC)?;

Ok(())
}
54 changes: 54 additions & 0 deletions drv/lpc55-rng/src/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

use crate::data_region::DataRegion;
use hubpack::SerializedSize;
use serde::Deserialize;
use stage0_handoff::{HandoffData, HandoffDataLoadError};

pub enum HandoffDataRegion {
DiceCerts,
DiceRng,
}

pub const DICE_CERTS: HandoffDataRegion = HandoffDataRegion::DiceCerts;
pub const DICE_RNG: HandoffDataRegion = HandoffDataRegion::DiceRng;

// This file is generated by the crate build.rs.
mod build {
include!(concat!(env!("OUT_DIR"), "/rng-config.rs"));
}

use build::{DICE_CERTS_REGION, DICE_RNG_REGION};

impl HandoffDataRegion {
pub fn data_region(&self) -> DataRegion {
match self {
Self::DiceCerts => DICE_CERTS_REGION,
Self::DiceRng => DICE_RNG_REGION,
}
}

/// Load a type implementing HandoffData (and others) from a config::DataRegion.
/// Errors will be reported in the ringbuf and will return None.
#[inline(always)]
pub fn load_data<
T: for<'a> Deserialize<'a> + HandoffData + SerializedSize,
>(
&self,
) -> Result<T, HandoffDataLoadError> {
use core::slice;

let region = self.data_region();
// Safety: This memory is setup by code executed before hubris and
// exposed using the kernel `extern-regions` mechanism. The safety of
// this code is an extension of our trust in the hubris pre-main, kernel,
// and build process.
let data = unsafe {
slice::from_raw_parts(region.address as *mut u8, region.size)
};

T::load_from_addr(data)
}
}
10 changes: 10 additions & 0 deletions drv/lpc55-rng/src/data-region.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.

#[derive(serde::Deserialize, Default, Debug)]
#[serde(rename_all = "kebab-case")]
pub struct DataRegion {
pub address: usize,
pub size: usize,
}
Loading