starnix_crypto/lib.rs
1// Copyright 2025 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use rand::RngCore;
6
7/// Overwrite `buffer` with bytes drawn from a thread-local CSPRNG (backed by ChaCha12).
8pub fn cprng_draw(buffer: &mut [u8]) {
9 rand::rng().fill_bytes(buffer);
10}
11
12#[cfg(test)]
13mod tests {
14 use super::*;
15
16 #[test]
17 fn test_cprng_draw() {
18 let mut buf = [0u8; 32];
19 cprng_draw(&mut buf);
20 // Extremely low probability that 32 random bytes are all zero.
21 assert_ne!(buf, [0u8; 32]);
22 }
23}