Skip to content

Commit 791a171

Browse files
committed
lpc55-rng: Streamline the fill loop & increase the read buffer size.
The `ReseedingRng` can handle us pulling more than 4 bytes out at a time.
1 parent 6de98ff commit 791a171

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

drv/lpc55-rng/src/main.rs

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#![no_std]
1010
#![no_main]
1111

12-
use core::{mem::size_of, usize};
12+
use core::{cmp, usize};
1313
use drv_lpc55_syscon_api::Syscon;
1414
use drv_rng_api::RngError;
1515
use idol_runtime::{ClientError, NotificationHandler, RequestError};
@@ -89,24 +89,19 @@ impl idl::InOrderRngImpl for Lpc55RngServer {
8989
dest: idol_runtime::Leased<idol_runtime::W, [u8]>,
9090
) -> Result<usize, RequestError<RngError>> {
9191
let mut cnt = 0;
92-
const STEP: usize = size_of::<u32>();
93-
let mut buf = [0u8; STEP];
94-
// fill in multiples of STEP / RNG register size
95-
for _ in 0..(dest.len() / STEP) {
96-
self.0.try_fill_bytes(&mut buf).map_err(RngError::from)?;
97-
dest.write_range(cnt..cnt + STEP, &buf)
92+
let mut buf = [0u8; 32];
93+
while cnt < dest.len() {
94+
let len = cmp::min(buf.len(), dest.len() - cnt);
95+
96+
self.0
97+
.try_fill_bytes(&mut buf[..len])
98+
.map_err(RngError::from)?;
99+
dest.write_range(cnt..cnt + len, &buf[..len])
98100
.map_err(|_| RequestError::Fail(ClientError::WentAway))?;
99-
cnt += STEP;
100-
}
101-
// fill in remaining
102-
let remain = dest.len() - cnt;
103-
assert!(remain < STEP);
104-
if remain > 0 {
105-
self.0.try_fill_bytes(&mut buf).map_err(RngError::from)?;
106-
dest.write_range(dest.len() - remain..dest.len(), &buf)
107-
.map_err(|_| RequestError::Fail(ClientError::WentAway))?;
108-
cnt += remain;
101+
102+
cnt += len;
109103
}
104+
110105
Ok(cnt)
111106
}
112107
}

0 commit comments

Comments
 (0)