Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aes-prng"
version = "0.2.2"
version = "0.2.3"
edition = "2021"
license = "Apache-2.0"
authors = [
Expand All @@ -25,12 +25,12 @@ categories = ["cryptography"]
[dependencies]
aes = "0.8"
byteorder = "1.4"
rand = { version = "0.9", features = ["std", "std_rng"] }
rand = { version = "0.10", features = ["std", "std_rng"] }

[dev-dependencies]
criterion = "0.5"
getrandom = "0.3"
rand_chacha = "0.9"
rand_chacha = "0.10"

[[bench]]
name = "rng"
Expand Down
6 changes: 3 additions & 3 deletions benches/rng.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use aes_prng::{AesRng, SEED_SIZE};
use criterion::{criterion_group, criterion_main, Bencher, BenchmarkId, Criterion};
use rand::{RngCore, SeedableRng};
use rand::{Rng, SeedableRng};
use rand_chacha::{ChaCha12Rng, ChaCha20Rng, ChaCha8Rng};

fn rng_fill(c: &mut Criterion) {
fn rng_bencher<T: RngCore>(b: &mut Bencher, rng: &mut T, buffer_size_kb: usize) {
fn rng_bencher<T: Rng>(b: &mut Bencher, rng: &mut T, buffer_size_kb: usize) {
let buffer_size = buffer_size_kb * 1024;
let mut output = vec![0u8; buffer_size];
b.iter(|| {
Expand Down Expand Up @@ -37,7 +37,7 @@ fn rng_fill(c: &mut Criterion) {
}

fn rng_next64(c: &mut Criterion) {
fn rng_bencher<T: RngCore>(b: &mut Bencher, rng: &mut T, n: usize) {
fn rng_bencher<T: Rng>(b: &mut Bencher, rng: &mut T, n: usize) {
b.iter(|| {
for _ in 0..n {
let _ = rng.next_u64();
Expand Down
20 changes: 12 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ use aes::cipher::generic_array::{typenum::U16, typenum::U8, GenericArray};
use aes::cipher::{BlockEncrypt, KeyInit};
use aes::Aes128;
use byteorder::{ByteOrder, LittleEndian};
use rand::{CryptoRng, RngCore, SeedableRng};
use rand::{Rng, SeedableRng, TryCryptoRng, TryRng};
use std::mem;
use std::slice;

Expand Down Expand Up @@ -221,32 +221,34 @@ impl AesRng {
}
}

impl RngCore for AesRng {
impl TryRng for AesRng {
type Error = core::convert::Infallible;

/// fetches 32 bits of randomness
fn next_u32(&mut self) -> u32 {
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
let u32_size = mem::size_of::<u32>();
if self.state.used_bytes >= STATE_SIZE - u32_size {
self.next();
}
let used_bytes = self.state.used_bytes;
self.state.used_bytes += u32_size; // update number of used bytes
let blocks_bytes = self.state.as_mut_bytes();
LittleEndian::read_u32(&blocks_bytes[used_bytes..used_bytes + u32_size])
Ok(LittleEndian::read_u32(&blocks_bytes[used_bytes..used_bytes + u32_size]))
}

/// fetches 64 bits of randomness
fn next_u64(&mut self) -> u64 {
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
let u64_size = mem::size_of::<u64>();
if self.state.used_bytes >= STATE_SIZE - u64_size {
self.next();
}
let used_bytes = self.state.used_bytes;
self.state.used_bytes += u64_size; // update number of used bytes
LittleEndian::read_u64(&self.state.as_mut_bytes()[used_bytes..used_bytes + u64_size])
Ok(LittleEndian::read_u64(&self.state.as_mut_bytes()[used_bytes..used_bytes + u64_size]))
}

/// Fills in an array of bytes with randomness
fn fill_bytes(&mut self, dest: &mut [u8]) {
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), Self::Error> {
let mut read_len = STATE_SIZE - self.state.used_bytes;
let mut dest_start = 0;

Expand All @@ -266,10 +268,12 @@ impl RngCore for AesRng {
dest[dest_start..dest_len]
.copy_from_slice(&self.state.as_mut_bytes()[src_start..src_start + remainder]);
self.state.used_bytes += remainder;

Ok(())
}
}

impl CryptoRng for AesRng {}
impl TryCryptoRng for AesRng {}

#[cfg(test)]
mod tests {
Expand Down