|
1 | 1 | // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
2 | 2 | // SPDX-License-Identifier: Apache-2.0 |
3 | 3 |
|
4 | | -use rand::{rngs::ReseedingRng, RngCore}; |
5 | | -use rand_chacha::ChaChaCore; |
| 4 | +use rand::rand_core::TryRng; |
6 | 5 |
|
7 | 6 | pub use s2n_quic_core::random::*; |
8 | 7 |
|
9 | 8 | struct AwsLc; |
10 | 9 |
|
11 | | -impl RngCore for AwsLc { |
| 10 | +impl TryRng for AwsLc { |
| 11 | + type Error = core::convert::Infallible; |
| 12 | + |
12 | 13 | #[inline] |
13 | | - fn next_u32(&mut self) -> u32 { |
| 14 | + fn try_next_u32(&mut self) -> Result<u32, Self::Error> { |
14 | 15 | let mut v = [0; 4]; |
15 | | - self.fill_bytes(&mut v); |
16 | | - u32::from_ne_bytes(v) |
| 16 | + aws_lc_rs::rand::fill(&mut v).unwrap(); |
| 17 | + Ok(u32::from_ne_bytes(v)) |
17 | 18 | } |
18 | 19 |
|
19 | 20 | #[inline] |
20 | | - fn next_u64(&mut self) -> u64 { |
| 21 | + fn try_next_u64(&mut self) -> Result<u64, Self::Error> { |
21 | 22 | let mut v = [0; 8]; |
22 | | - self.fill_bytes(&mut v); |
23 | | - u64::from_ne_bytes(v) |
| 23 | + aws_lc_rs::rand::fill(&mut v).unwrap(); |
| 24 | + Ok(u64::from_ne_bytes(v)) |
24 | 25 | } |
25 | 26 |
|
26 | 27 | #[inline] |
27 | | - fn fill_bytes(&mut self, dest: &mut [u8]) { |
28 | | - aws_lc_rs::rand::fill(dest).unwrap() |
| 28 | + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> { |
| 29 | + aws_lc_rs::rand::fill(dest).unwrap(); |
| 30 | + Ok(()) |
29 | 31 | } |
30 | 32 | } |
31 | 33 |
|
32 | | -pub struct Random { |
33 | | - public: ReseedingRng<ChaChaCore, AwsLc>, |
34 | | - private: ReseedingRng<ChaChaCore, AwsLc>, |
35 | | -} |
| 34 | +pub struct Random(s2n_quic::provider::random::Random<AwsLc>); |
36 | 35 |
|
37 | 36 | impl Default for Random { |
38 | 37 | #[inline] |
39 | 38 | fn default() -> Self { |
40 | | - Self { |
41 | | - public: build_rng(), |
42 | | - private: build_rng(), |
43 | | - } |
| 39 | + Self(s2n_quic::provider::random::Random::new(AwsLc, AwsLc)) |
44 | 40 | } |
45 | 41 | } |
46 | 42 |
|
47 | | -// Constructs a `ReseedingRng` with a ChaCha RNG initially seeded from the OS, |
48 | | -// that will reseed from the OS after RESEED_THRESHOLD is exceeded |
49 | | -fn build_rng() -> ReseedingRng<ChaChaCore, AwsLc> { |
50 | | - // Number of generated bytes after which to reseed the public and private random |
51 | | - // generators. |
52 | | - // |
53 | | - // This value is based on THREAD_RNG_RESEED_THRESHOLD from |
54 | | - // [rand::rngs::thread.rs](https://github.com/rust-random/rand/blob/ef75e56cf5824d33c55622bf84a70ec6e22761ba/src/rngs/thread.rs#L39) |
55 | | - const RESEED_THRESHOLD: u64 = 1024 * 64; |
56 | | - ReseedingRng::<ChaChaCore, AwsLc>::new(RESEED_THRESHOLD, AwsLc) |
57 | | - .unwrap_or_else(|err| panic!("could not initialize random generator: {err}")) |
58 | | -} |
59 | | - |
60 | 43 | impl Generator for Random { |
61 | 44 | #[inline] |
62 | 45 | fn public_random_fill(&mut self, dest: &mut [u8]) { |
63 | | - self.public.fill_bytes(dest); |
| 46 | + self.0.public_random_fill(dest); |
64 | 47 | } |
65 | 48 |
|
66 | 49 | #[inline] |
67 | 50 | fn private_random_fill(&mut self, dest: &mut [u8]) { |
68 | | - self.private.fill_bytes(dest); |
| 51 | + self.0.private_random_fill(dest); |
69 | 52 | } |
70 | 53 | } |
0 commit comments