1use embedded_cal::{
6 Cal,
7 plumbing::{
8 Plumbing,
9 ec::Ec,
10 hash::{Hash, Sha2Short},
11 },
12};
13
14pub struct WithRng<C, R> {
20 pub cal: C,
21 pub rng: R,
22}
23
24#[cfg(feature = "new_from_sys")]
25pub type WithSysRng<C> = WithRng<C, rand::rngs::StdRng>;
26
27impl<C, R> WithRng<C, R> {
28 pub fn new(cal: C, rng: R) -> Self {
29 WithRng { cal, rng }
30 }
31}
32
33#[cfg(feature = "new_from_sys")]
34impl<C> WithRng<C, rand::rngs::StdRng> {
35 pub fn new_from_sys(cal: C) -> Self {
43 WithRng {
44 cal,
45 rng: rand::make_rng(),
46 }
47 }
48}
49
50impl<C: Cal, R> Cal for WithRng<C, R> {
51 type DhProvider = C::DhProvider;
52
53 type AeadProvider = C::AeadProvider;
54
55 type HashProvider = C::HashProvider;
56
57 type HmacProvider = C::HmacProvider;
58
59 fn dh(&mut self) -> &mut Self::DhProvider {
60 self.cal.dh()
61 }
62
63 fn aead(&mut self) -> &mut Self::AeadProvider {
64 self.cal.aead()
65 }
66
67 fn hash(&mut self) -> &mut Self::HashProvider {
68 self.cal.hash()
69 }
70
71 fn hmac(&mut self) -> &mut Self::HmacProvider {
72 self.cal.hmac()
73 }
74}
75
76impl<C: Sha2Short, R> Sha2Short for WithRng<C, R> {
77 const SUPPORTED: bool = C::SUPPORTED;
78
79 const SEND_PADDING: bool = C::SEND_PADDING;
80
81 const FIRST_CHUNK_SIZE: usize = C::FIRST_CHUNK_SIZE;
82
83 const UPDATE_MULTICHUNK: bool = C::UPDATE_MULTICHUNK;
84
85 type State = C::State;
86
87 fn init(&mut self, variant: embedded_cal::plumbing::hash::Sha2ShortVariant) -> Self::State {
88 self.cal.init(variant)
89 }
90
91 fn update(&mut self, instance: &mut Self::State, data: &[u8]) {
92 self.cal.update(instance, data)
93 }
94
95 fn finalize(&mut self, instance: Self::State, last_chunk: &[u8], target: &mut [u8]) {
96 self.cal.finalize(instance, last_chunk, target)
97 }
98}
99
100impl<C: Hash, R> Hash for WithRng<C, R> {}
101
102impl<C: Ec, R> Ec for WithRng<C, R> {
103 const MAX_SCALAR_LENGTH: usize = C::MAX_SCALAR_LENGTH;
104
105 type PrimitivesP256 = C::PrimitivesP256;
106
107 type PrimitivesX25519 = C::PrimitivesX25519;
108
109 type PrimitivesX448 = C::PrimitivesX448;
110
111 fn p256(&mut self) -> &mut Self::PrimitivesP256 {
112 self.cal.p256()
113 }
114
115 fn x25519(&mut self) -> &mut Self::PrimitivesX25519 {
116 self.cal.x25519()
117 }
118
119 fn x448(&mut self) -> &mut Self::PrimitivesX448 {
120 self.cal.x448()
121 }
122}
123
124impl<C: Plumbing, R> Plumbing for WithRng<C, R> {}
125
126impl<C, R: rand_core::TryRng> rand_core::TryRng for WithRng<C, R> {
127 type Error = R::Error;
128
129 fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
130 self.rng.try_next_u32()
131 }
132
133 fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
134 self.rng.try_next_u64()
135 }
136
137 fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
138 self.rng.try_fill_bytes(dst)
139 }
140}
141
142impl<C, R: rand_core::CryptoRng> rand_core::TryCryptoRng for WithRng<C, R> {}