Skip to content

Commit

Permalink
Merge pull request #48 from japaric/rust-crypto-no-std
Browse files Browse the repository at this point in the history
no-std-ify hpke-rs-rust-crypto
  • Loading branch information
franziskuskiefer authored Nov 26, 2023
2 parents 65df1f4 + de6030d commit 3e7e671
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
17 changes: 9 additions & 8 deletions rust_crypto_provider/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,20 @@ repository = "https://github.com/franziskuskiefer/hpke-rs"
hpke-rs-crypto = { version = "0.1.3", path = "../traits" }
# Rust crypto
hkdf = { version = "0.12" }
sha2 = { version = "0.10" }
p256 = { version = "0.13", features = ["arithmetic", "ecdh"] }
p384 = { version = "0.13" }
x25519-dalek-ng = { version = "1.1" }
chacha20poly1305 = { version = "0.10" }
aes-gcm = { version = "0.10" }
sha2 = { version = "0.10", default-features = false }
p256 = { version = "0.13", features = ["arithmetic", "ecdh"], default-features = false }
p384 = { version = "0.13", default-features = false }
x25519-dalek-ng = { version = "1.1", default-features = false, features = ["u64_backend"] }
chacha20poly1305 = { version = "0.10", default-features = false, features = ["alloc"] }
aes-gcm = { version = "0.10", default-features = false, features = ["aes"] }
# Randomness
rand = { version = "0.8" }
rand_core = { version = "0.6" }
getrandom = { version = "0.2", features = ["js"] }
rand_chacha = { version = "0.3" }
rand_chacha = { version = "0.3", default-features = false }

[dev-dependencies]
criterion = { version = "0.5", features = ["html_reports"] }
rand = { version = "0.8" }

[features]
deterministic-prng = [] # ⚠️ FOR TESTING ONLY.
Expand Down
36 changes: 16 additions & 20 deletions rust_crypto_provider/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![doc = include_str!("../Readme.md")]

use std::{fmt::Display, sync::RwLock};
use core::fmt::Display;

use hpke_rs_crypto::{
error::Error,
Expand All @@ -11,7 +11,7 @@ use p256::{
elliptic_curve::{ecdh::diffie_hellman, sec1::ToEncodedPoint},
PublicKey, SecretKey,
};
use rand::SeedableRng;
use rand_core::SeedableRng;
use x25519_dalek_ng::{PublicKey as X25519PublicKey, StaticSecret as X25519StaticSecret};

mod aead;
Expand All @@ -25,7 +25,7 @@ pub struct HpkeRustCrypto {}

/// The PRNG for the Rust Crypto Provider.
pub struct HpkeRustCryptoPrng {
rng: RwLock<rand_chacha::ChaCha20Rng>,
rng: rand_chacha::ChaCha20Rng,
#[cfg(feature = "deterministic-prng")]
fake_rng: Vec<u8>,
}
Expand Down Expand Up @@ -107,7 +107,7 @@ impl HpkeCrypto for HpkeRustCrypto {
}

fn kem_key_gen(alg: KemAlgorithm, prng: &mut Self::HpkePrng) -> Result<Vec<u8>, Error> {
let mut rng = prng.rng.write().unwrap();
let rng = &mut prng.rng;
match alg {
KemAlgorithm::DhKem25519 => Ok(X25519StaticSecret::new(&mut *rng).to_bytes().to_vec()),
KemAlgorithm::DhKemP256 => {
Expand Down Expand Up @@ -165,12 +165,12 @@ impl HpkeCrypto for HpkeRustCrypto {
rand_chacha::ChaCha20Rng::from_entropy().fill_bytes(&mut fake_rng);
HpkeRustCryptoPrng {
fake_rng,
rng: RwLock::new(rand_chacha::ChaCha20Rng::from_entropy()),
rng: rand_chacha::ChaCha20Rng::from_entropy(),
}
}
#[cfg(not(feature = "deterministic-prng"))]
HpkeRustCryptoPrng {
rng: RwLock::new(rand_chacha::ChaCha20Rng::from_entropy()),
rng: rand_chacha::ChaCha20Rng::from_entropy(),
}
}

Expand Down Expand Up @@ -200,34 +200,30 @@ impl HpkeCrypto for HpkeRustCrypto {

impl RngCore for HpkeRustCryptoPrng {
fn next_u32(&mut self) -> u32 {
let mut rng = self.rng.write().unwrap();
rng.next_u32()
self.rng.next_u32()
}

fn next_u64(&mut self) -> u64 {
let mut rng = self.rng.write().unwrap();
rng.next_u64()
self.rng.next_u64()
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
let mut rng = self.rng.write().unwrap();
rng.fill_bytes(dest)
self.rng.fill_bytes(dest);
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
let mut rng = self.rng.write().unwrap();
rng.try_fill_bytes(dest)
fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.rng.try_fill_bytes(dest)
}
}

impl CryptoRng for HpkeRustCryptoPrng {}

impl HpkeTestRng for HpkeRustCryptoPrng {
#[cfg(feature = "deterministic-prng")]
fn try_fill_test_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
fn try_fill_test_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
// Here we fake our randomness for testing.
if dest.len() > self.fake_rng.len() {
return Err(rand::Error::new(Error::InsufficientRandomness));
return Err(rand_core::Error::new(Error::InsufficientRandomness));
}
dest.clone_from_slice(&self.fake_rng.split_off(self.fake_rng.len() - dest.len()));
Ok(())
Expand All @@ -238,16 +234,16 @@ impl HpkeTestRng for HpkeRustCryptoPrng {
self.fake_rng = seed.to_vec();
}
#[cfg(not(feature = "deterministic-prng"))]
fn try_fill_test_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
self.rng.write().unwrap().try_fill_bytes(dest)
fn try_fill_test_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
self.rng.try_fill_bytes(dest)
}

#[cfg(not(feature = "deterministic-prng"))]
fn seed(&mut self, _: &[u8]) {}
}

impl Display for HpkeRustCrypto {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", Self::name())
}
}

0 comments on commit 3e7e671

Please sign in to comment.