Skip to main content

s2n_quic_dc/
random.rs

1// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use rand::rand_core::TryRng;
5
6pub use s2n_quic_core::random::*;
7
8struct AwsLc;
9
10impl TryRng for AwsLc {
11    type Error = core::convert::Infallible;
12
13    #[inline]
14    #[expect(
15        clippy::unwrap_used,
16        clippy::unwrap_in_result,
17        reason = "aws-lc-rs random fill only fails on internal RNG failure, which is not a recoverable condition"
18    )]
19    fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
20        let mut v = [0; 4];
21        aws_lc_rs::rand::fill(&mut v).unwrap();
22        Ok(u32::from_ne_bytes(v))
23    }
24
25    #[inline]
26    #[expect(
27        clippy::unwrap_used,
28        clippy::unwrap_in_result,
29        reason = "aws-lc-rs random fill only fails on internal RNG failure, which is not a recoverable condition"
30    )]
31    fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
32        let mut v = [0; 8];
33        aws_lc_rs::rand::fill(&mut v).unwrap();
34        Ok(u64::from_ne_bytes(v))
35    }
36
37    #[inline]
38    #[expect(
39        clippy::unwrap_used,
40        clippy::unwrap_in_result,
41        reason = "aws-lc-rs random fill only fails on internal RNG failure, which is not a recoverable condition"
42    )]
43    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
44        aws_lc_rs::rand::fill(dest).unwrap();
45        Ok(())
46    }
47}
48
49pub struct Random(s2n_quic::provider::random::Random<AwsLc>);
50
51impl Default for Random {
52    #[inline]
53    fn default() -> Self {
54        Self(s2n_quic::provider::random::Random::new(AwsLc, AwsLc))
55    }
56}
57
58impl Generator for Random {
59    #[inline]
60    fn public_random_fill(&mut self, dest: &mut [u8]) {
61        self.0.public_random_fill(dest);
62    }
63
64    #[inline]
65    fn private_random_fill(&mut self, dest: &mut [u8]) {
66        self.0.private_random_fill(dest);
67    }
68}