Skip to content

Commit 9a26b8b

Browse files
clundin25jhand2
authored andcommitted
Use a chunk iterator instead of a while loop
Currently `rand_bytes` is only used on a buffer < than 48 bytes. If used with a buffer larger than 48 bytes a panic is introduced to the Caliptra runtime due to bounds checking. This change uses a chunks iterator instead. Each chunk is guaranteed to be 48 bytes except for the last chunk which contains the remainder.
1 parent 9ea287b commit 9a26b8b

File tree

1 file changed

+2
-5
lines changed

1 file changed

+2
-5
lines changed

runtime/src/dpe_crypto.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,13 @@ impl<'a> Crypto for DpeCrypto<'a> {
102102
type PrivKey = KeyId;
103103

104104
fn rand_bytes(&mut self, dst: &mut [u8]) -> Result<(), CryptoError> {
105-
let mut curr_idx = 0;
106-
while curr_idx < dst.len() {
105+
for chunk in dst.chunks_mut(48) {
107106
let trng_bytes = <[u8; 48]>::from(
108107
self.trng
109108
.generate()
110109
.map_err(|e| CryptoError::CryptoLibError(u32::from(e)))?,
111110
);
112-
let bytes_to_write = min(dst.len() - curr_idx, trng_bytes.len());
113-
dst[curr_idx..curr_idx + bytes_to_write].copy_from_slice(&trng_bytes[..bytes_to_write]);
114-
curr_idx += bytes_to_write;
111+
chunk.copy_from_slice(&trng_bytes[..chunk.len()])
115112
}
116113
Ok(())
117114
}

0 commit comments

Comments
 (0)