Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import https://github.com/vks/aesrng #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: rust
services: docker
sudo: required

matrix:
include:
- rust: 1.27.0
- rust: stable
- rust: nightly

script:
- cargo test --all ; ./test_aesni.sh

cache: cargo
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[workspace]
members = [
"aes-rng",
]
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ All algorithms are split into separate crates and implemented using
do not require the standard library (i.e. `no_std` capable) and can
be easily used for bare-metal programming.

## Warnings

Crates in this repository have not yet received any formal cryptographic and
security reviews.

**USE AT YOUR OWN RISK.**

## Crates
| Name | Crates.io | Documentation |
| ---- | :--------:| :------------:|
| `aes-rng` | [![crates.io](https://img.shields.io/crates/v/aes-rng.svg)](https://crates.io/crates/aes-rng) | [![Documentation](https://docs.rs/aes-rng/badge.svg)](https://docs.rs/aes-rng) |

### Minimum Rust version
All crates in this repository support Rust 1.27 or higher. In future minimum
supported Rust version can be changed, but it will be done with the minor
version bump.

## License

All crates licensed under either of
Expand Down
26 changes: 26 additions & 0 deletions aes-rng/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "aes-rng"
version = "0.1.0"
authors = ["RustCrypto Developers"]
license = "MIT/Apache-2.0"
description = "AES-NI random-number generator"
documentation = "https://docs.rs/aes-rng"
repository = "https://github.com/RustCrypto/stream-ciphers"
keywords = ["crypto", "stream-cipher"]
categories = ["cryptography"]

[badges]
travis-ci = { repository = "RustCrypto/stream-ciphers" }

[[bench]]
harness = false
name = "rngs"

[dependencies]
rand_core = "0.2"

[dev-dependencies]
criterion = "0.2"
itertools = "0.7"
xoshiro = "0.0.3"
rand = "0.5"
85 changes: 85 additions & 0 deletions aes-rng/benches/rngs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
extern crate aes_rng;
extern crate rand;
extern crate xoshiro;

#[macro_use]
extern crate criterion;

use rand::{RngCore, FromEntropy, SeedableRng};
use criterion::{Criterion, Fun};

fn fill(c: &mut Criterion) {
const BUF_SIZE: usize = 1024 * 1024 * 100;
let fill_aes = {
let mut rng = aes_rng::AesRng::from_seed([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
]);
let mut buf = vec![0; BUF_SIZE];

Fun::new("aes", move |b, _| b.iter(|| rng.fill_bytes(&mut buf)))
};
let fill_aescore = {
let mut rng = aes_rng::AesCore::from_seed([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
]);
let mut buf = vec![0; BUF_SIZE];

Fun::new("aescore", move |b, _| b.iter(|| rng.fill(&mut buf)))
};
let fill_xoshiro =
{
let mut rng = xoshiro::Xoshiro128StarStar::from_seed_u64(1);
let mut buf = vec![0; BUF_SIZE];

Fun::new("xoshiro", move |b, _| b.iter(|| rng.fill_bytes(&mut buf)))
};
let fill_std = {
let mut rng = rand::StdRng::from_entropy();
let mut buf = vec![0; BUF_SIZE];

Fun::new("std", move |b, _| b.iter(|| rng.fill_bytes(&mut buf)))
};
c.bench_functions("fill", vec![fill_aes, fill_aescore, fill_xoshiro, fill_std], ());
}

fn next_u64(c: &mut Criterion) {
let next_aes = {
let mut rng = aes_rng::AesRng::from_seed([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
]);
Fun::new("aes", move |b, _| b.iter(|| rng.next_u64()))
};
let next_xoshiro =
{
let mut rng = xoshiro::Xoshiro128StarStar::from_seed_u64(1);
Fun::new("xoshiro", move |b, _| b.iter(|| rng.next_u64()))
};
let next_std = {
let mut rng = rand::StdRng::from_entropy();
Fun::new("std", move |b, _| b.iter(|| rng.next_u64()))
};
c.bench_functions("next_u64", vec![next_aes, next_xoshiro, next_std], ());
}

fn new(c: &mut Criterion) {
let new_aes = Fun::new("aes", |b, _| b.iter(|| aes_rng::AesRng::from_seed([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
])));
let new_aescore = Fun::new("aescore", |b, _| b.iter(|| aes_rng::AesCore::from_seed([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
])));
let new_xoshiro = Fun::new("xoshiro", |b, _| b.iter(|| xoshiro::Xoshiro128StarStar::from_seed_u64(1)));
let new_std = Fun::new("std", |b, _| b.iter(|| rand::StdRng::from_seed([
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15,
])));
c.bench_functions("new", vec![new_aes, new_aescore, new_xoshiro, new_std], ());
}

criterion_group!(benches, fill, next_u64, new);
criterion_main!(benches);
44 changes: 44 additions & 0 deletions aes-rng/src/byte_slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/// Trait for casting types to byte slices.
pub trait AsByteSliceMut {
/// Return a mutable reference to self as a byte slice
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8];

/// Call `to_le` on each element (i.e. byte-swap on Big Endian platforms).
fn to_le(&mut self);
}

impl AsByteSliceMut for [u8] {
#[inline]
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
self
}

#[inline]
fn to_le(&mut self) {}
}

macro_rules! impl_as_byte_slice {
($t:ty) => {
impl AsByteSliceMut for [$t] {
#[inline]
fn as_byte_slice_mut<'a>(&'a mut self) -> &'a mut [u8] {
unsafe {
::std::slice::from_raw_parts_mut(&mut self[0]
as *mut $t
as *mut u8,
self.len() * ::std::mem::size_of::<$t>()
)
}
}

#[inline]
fn to_le(&mut self) {
for x in self {
*x = x.to_le();
}
}
}
}
}

impl_as_byte_slice!(u32);
Loading