|
9 | 9 | use core::fmt::Debug; |
10 | 10 |
|
11 | 11 | use rand_core::{ |
| 12 | + block::{BlockRng, CryptoGenerator, Generator}, |
12 | 13 | CryptoRng, RngCore, SeedableRng, |
13 | | - block::{BlockRng, BlockRngCore, CryptoBlockRng}, |
14 | 14 | }; |
15 | 15 |
|
16 | 16 | #[cfg(feature = "zeroize")] |
17 | 17 | use zeroize::{Zeroize, ZeroizeOnDrop}; |
18 | 18 |
|
19 | 19 | use crate::{ |
20 | | - ChaChaCore, R8, R12, R20, Rounds, backends, |
| 20 | + backends, |
21 | 21 | variants::{Legacy, Variant}, |
| 22 | + ChaChaCore, Rounds, R12, R20, R8, |
22 | 23 | }; |
23 | 24 |
|
24 | 25 | use cfg_if::cfg_if; |
@@ -146,35 +147,6 @@ pub type StreamId = U32x2; |
146 | 147 | /// The arrays should be in little endian order. |
147 | 148 | pub type BlockPos = U32x2; |
148 | 149 |
|
149 | | -/// The results buffer that zeroizes on drop when the `zeroize` feature is enabled. |
150 | | -#[derive(Clone)] |
151 | | -pub struct BlockRngResults([u32; BUFFER_SIZE]); |
152 | | - |
153 | | -impl AsRef<[u32]> for BlockRngResults { |
154 | | - fn as_ref(&self) -> &[u32] { |
155 | | - &self.0 |
156 | | - } |
157 | | -} |
158 | | - |
159 | | -impl AsMut<[u32]> for BlockRngResults { |
160 | | - fn as_mut(&mut self) -> &mut [u32] { |
161 | | - &mut self.0 |
162 | | - } |
163 | | -} |
164 | | - |
165 | | -impl Default for BlockRngResults { |
166 | | - fn default() -> Self { |
167 | | - Self([0u32; BUFFER_SIZE]) |
168 | | - } |
169 | | -} |
170 | | - |
171 | | -#[cfg(feature = "zeroize")] |
172 | | -impl Drop for BlockRngResults { |
173 | | - fn drop(&mut self) { |
174 | | - self.0.zeroize(); |
175 | | - } |
176 | | -} |
177 | | - |
178 | 150 | const BUFFER_SIZE: usize = 64; |
179 | 151 |
|
180 | 152 | // NB. this must remain consistent with some currently hard-coded numbers in this module |
@@ -323,18 +295,18 @@ macro_rules! impl_chacha_rng { |
323 | 295 | impl RngCore for $ChaChaXRng { |
324 | 296 | #[inline] |
325 | 297 | fn next_u32(&mut self) -> u32 { |
326 | | - self.core.next_u32() |
| 298 | + self.core.next_word() |
327 | 299 | } |
328 | 300 | #[inline] |
329 | 301 | fn next_u64(&mut self) -> u64 { |
330 | | - self.core.next_u64() |
| 302 | + self.core.next_u64_from_u32() |
331 | 303 | } |
332 | 304 | #[inline] |
333 | 305 | fn fill_bytes(&mut self, dest: &mut [u8]) { |
334 | 306 | self.core.fill_bytes(dest) |
335 | 307 | } |
336 | 308 | } |
337 | | - impl CryptoBlockRng for $ChaChaXCore {} |
| 309 | + impl CryptoGenerator for $ChaChaXCore {} |
338 | 310 | impl CryptoRng for $ChaChaXRng {} |
339 | 311 |
|
340 | 312 | #[cfg(feature = "zeroize")] |
@@ -535,13 +507,17 @@ macro_rules! impl_chacha_rng { |
535 | 507 | } |
536 | 508 | } |
537 | 509 |
|
538 | | - impl BlockRngCore for $ChaChaXCore { |
539 | | - type Item = u32; |
540 | | - type Results = BlockRngResults; |
| 510 | + impl Generator for $ChaChaXCore { |
| 511 | + type Output = [u32; BUFFER_SIZE]; |
541 | 512 |
|
542 | 513 | #[inline] |
543 | | - fn generate(&mut self, r: &mut Self::Results) { |
544 | | - self.0.generate(&mut r.0); |
| 514 | + fn generate(&mut self, r: &mut Self::Output) { |
| 515 | + self.0.generate(r); |
| 516 | + } |
| 517 | + |
| 518 | + #[cfg(feature = "zeroize")] |
| 519 | + fn drop(&mut self, output: &mut Self::Output) { |
| 520 | + output.zeroize(); |
545 | 521 | } |
546 | 522 | } |
547 | 523 | }; |
@@ -936,8 +912,8 @@ pub(crate) mod tests { |
936 | 912 | /// Because this test uses `rand_chacha v0.3.1` which uses a 64-bit counter, these |
937 | 913 | /// test results should be accurate up to `block_pos = 2^32 - 1`. |
938 | 914 | fn test_fill_bytes_v2() { |
939 | | - use rand_chacha::ChaCha20Rng as TesterRng; |
940 | 915 | use rand_chacha::rand_core::{RngCore, SeedableRng}; |
| 916 | + use rand_chacha::ChaCha20Rng as TesterRng; |
941 | 917 |
|
942 | 918 | let mut rng = ChaChaRng::from_seed([0u8; 32]); |
943 | 919 | let mut tester_rng = TesterRng::from_seed([0u8; 32]); |
|
0 commit comments