mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
fix corner case on non-word aligned buffers
This commit is contained in:
parent
c98865d2e3
commit
fdb613cfb3
1 changed files with 4 additions and 1 deletions
|
|
@ -151,10 +151,12 @@ impl Trng {
|
||||||
use rand_chacha::rand_core::RngCore;
|
use rand_chacha::rand_core::RngCore;
|
||||||
use rand_chacha::rand_core::SeedableRng;
|
use rand_chacha::rand_core::SeedableRng;
|
||||||
|
|
||||||
|
// for very short runs, fill directly from the HW TRNG
|
||||||
if dest.len() < 64 {
|
if dest.len() < 64 {
|
||||||
self.fill_bytes_via_next(dest);
|
self.fill_bytes_via_next(dest);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
// larger runs, seed a ChaCha8 and use its results to fill the buffer
|
||||||
let mut seed = [0u8; 32];
|
let mut seed = [0u8; 32];
|
||||||
self.fill_bytes_via_next(&mut seed);
|
self.fill_bytes_via_next(&mut seed);
|
||||||
let mut cstrng = ChaCha8Rng::from_seed(seed);
|
let mut cstrng = ChaCha8Rng::from_seed(seed);
|
||||||
|
|
@ -181,7 +183,8 @@ impl RngCore for Trng {
|
||||||
let buf = &mut api_buf.data;
|
let buf = &mut api_buf.data;
|
||||||
let mut filled = 0;
|
let mut filled = 0;
|
||||||
while filled < dest.len() {
|
while filled < dest.len() {
|
||||||
let fetch_len = ((dest.len() - filled) / size_of::<u32>()).min(buf.len());
|
let fetch_len = ((((dest.len() + 3) & !3) - filled) / size_of::<u32>()).min(buf.len());
|
||||||
|
log::debug!("fetching {} bytes; filled {} so far", fetch_len * 4, filled);
|
||||||
self.fill_buf(&mut buf[..fetch_len]).expect("couldn't fill temp buffer");
|
self.fill_buf(&mut buf[..fetch_len]).expect("couldn't fill temp buffer");
|
||||||
for &w in buf[..fetch_len].iter() {
|
for &w in buf[..fetch_len].iter() {
|
||||||
let bytes = w.to_le_bytes();
|
let bytes = w.to_le_bytes();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue