-
Notifications
You must be signed in to change notification settings - Fork 185
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
flihp
wants to merge
16
commits into
oxidecomputer:master
Choose a base branch
from
flihp:lib-lpc55-rng-seed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Lib lpc55 rng seed #1820
Changes from 1 commit
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5a6ce9e
lpc55-rng: Move Lpc55Core and Lpc55Rng to a library.
flihp 6551ef3
lpc55-rng: Combine Lpc55Core and Lpc55Rng.
flihp 6427006
lpc55-rng: cleanup some nonsense
flihp dd1dbc0
lpc55-rng: Streamline the `fill` loop & increase the read buffer size.
flihp f76e271
lpc55-rng: Make ReseedingRng generic over the reseeder.
flihp c16168f
lpc55-rng: Include 32 bytes from the last PRNG instance when reseeding.
flihp 67828cf
lpc55-rng: Include DICE derived seed in initial PRNG seed.
flihp 0c8a4be
lpc55-rng: Include SN from platform id cert in initial PRNG seed.
flihp 6036d0e
oxide-rot-1: Add rng task to app-dev.toml.
flihp bce3889
Pass lpc55 PMC & RNG devices as parameters to Lpc55Rng 'new'.
flihp c29dffa
Panic if RNG is powered off after Lpc55Rng instantiation.
flihp cf2704f
Make safety comment on code loading data from handoff region true.
flihp bf092d3
Undo increased stack usage from cf2704fae6.
flihp 11f962a
Create inputs to `Lpc55Rng::new` in dedicated scope.
flihp de27347
Restructure the construction of the server.
flihp b43295c
Use `mutable-static` to create hash instance.
flihp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ use idol_runtime::{ClientError, NotificationHandler, RequestError}; | |
use lib_dice::{persistid_cert_tmpl::SUBJECT_CN_LENGTH, RngSeed, SeedBuf}; | ||
use lib_lpc55_rng::Lpc55Rng; | ||
use lpc55_pac::Peripherals; | ||
use mutable_statics::mutable_statics; | ||
use rand_chacha::ChaCha20Rng; | ||
use rand_core::{impls, Error, RngCore, SeedableRng}; | ||
use ringbuf::ringbuf; | ||
|
@@ -53,12 +54,12 @@ enum Trace { | |
ringbuf!(Trace, 16, Trace::None); | ||
|
||
// low-budget rand::rngs::adapter::ReseedingRng w/o fork stuff | ||
struct ReseedingRng<T: SeedableRng, R: RngCore, H: Digest> { | ||
struct ReseedingRng<T: SeedableRng, R: RngCore, H: Digest + 'static> { | ||
inner: T, | ||
reseeder: R, | ||
threshold: usize, | ||
bytes_until_reseed: usize, | ||
mixer: H, | ||
mixer: &'static mut H, | ||
} | ||
|
||
impl<T, R, H> ReseedingRng<T, R, H> | ||
|
@@ -73,28 +74,28 @@ where | |
mut reseeder: R, | ||
pid: Option<&[u8; SUBJECT_CN_LENGTH]>, | ||
threshold: usize, | ||
mixer: &'static mut H, | ||
) -> Result<Self, Error> { | ||
let threshold = if threshold == 0 { | ||
usize::MAX | ||
} else { | ||
threshold | ||
}; | ||
|
||
let mut mixer = H::default(); | ||
if let Some(seed) = seed { | ||
// mix platform unique seed derived by measured boot | ||
Digest::update(&mut mixer, seed.as_bytes()); | ||
Digest::update(mixer, seed.as_bytes()); | ||
} | ||
|
||
if let Some(pid) = pid { | ||
// mix in unique platform id | ||
Digest::update(&mut mixer, pid); | ||
Digest::update(mixer, pid); | ||
} | ||
|
||
// w/ 32 bytes from HRNG | ||
let mut buf = Zeroizing::new(T::Seed::default()); | ||
reseeder.try_fill_bytes(buf.as_mut())?; | ||
Digest::update(&mut mixer, buf.as_ref()); | ||
Digest::update(mixer, buf.as_ref()); | ||
|
||
// create initial instance of the SeedableRng from the seed | ||
let inner = T::from_seed(mixer.finalize_fixed_reset().into()); | ||
|
@@ -144,11 +145,11 @@ where | |
|
||
// mix 32 bytes from current PRNG instance | ||
self.inner.try_fill_bytes(buf.as_mut())?; | ||
Digest::update(&mut self.mixer, buf.as_mut()); | ||
Digest::update(self.mixer, buf.as_mut()); | ||
|
||
// w/ 32 bytes from HRNG | ||
self.reseeder.try_fill_bytes(buf.as_mut())?; | ||
Digest::update(&mut self.mixer, buf.as_mut()); | ||
Digest::update(self.mixer, buf.as_mut()); | ||
|
||
// seed new RNG instance & reset mixer | ||
self.inner = | ||
|
@@ -163,7 +164,7 @@ where | |
} | ||
} | ||
|
||
struct Lpc55RngServer<T: SeedableRng, R: RngCore, H: Digest>( | ||
struct Lpc55RngServer<T: SeedableRng, R: RngCore, H: Digest + 'static>( | ||
ReseedingRng<T, R, H>, | ||
); | ||
|
||
|
@@ -283,13 +284,22 @@ fn main() -> ! { | |
) | ||
}; | ||
|
||
let mixer = mutable_statics! { | ||
static mut MIXER: [Sha3_256; 1] = [Sha3_256::new; _]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the change I had in mind -- I'm surprised it didn't reduce stack usage more! Oh well. It's still an improvement. |
||
}; | ||
|
||
let reseeding_rng: ReseedingRng<ChaCha20Rng, Lpc55Rng, Sha3_256> = { | ||
let seed = get_dice_seed(); | ||
let pid = get_seed_personalization(); | ||
let threshold = 0x100000; // 1 MiB | ||
|
||
ReseedingRng::new(seed.as_ref(), rng, pid.as_ref(), threshold) | ||
.unwrap_lite() | ||
ReseedingRng::new( | ||
seed.as_ref(), | ||
rng, | ||
pid.as_ref(), | ||
threshold, | ||
&mut mixer[0], | ||
) | ||
.unwrap_lite() | ||
}; | ||
|
||
let mut server = Lpc55RngServer(reseeding_rng); | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI, you might see lighter stack usage here by using one of the
finalize_into
operations, instead of returning the array. It might also not change anything. Hard to tell.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This approach seems to be thwarted by
T::from_seed
taking ownership of the seed. If we keep aGenericArray
in theReseedingRng
to hold the digest we end up having to clone it intry_fill_bytes
. If we keep a[u8; 32]
in theReseedingRng
the compiler will do the copy for us. This ends up taking up more stack not less. I didn't commit this experiment to this PR branch but put a wip here if you think it could be made to work.