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

Get rid of the getrandom and OsRng requirement #86

Open
wants to merge 13 commits 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
42 changes: 24 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 4 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ categories = ["cryptography", "no-std"]
keywords = ["rustls", "tls"]
edition = "2021"
rust-version = "1.75"
resolver = "1" # Hack to enable the `custom` feature of `getrandom`

# Ensure all dependencies + feats are mapped to crate features for correct usage
# default features often have std breaking no_std and potentially other unwanted
Expand All @@ -32,7 +31,7 @@ p384 = { version = "0.13.0", default-features = false, features = ["pem", "ecdsa
paste = { version = "1.0.14", default-features = false }
pkcs8 = { version = "0.10.2", default-features = false, features = ["pem", "pkcs5"] }
pki-types = { package = "rustls-pki-types", version = "1.0.1", default-features = false }
rand_core = { version = "0.6.4", default-features = false, features = ["getrandom"] }
rand_core = { version = "0.6.4", default-features = false }
rsa = { version = "0.9.2", default-features = false, features = ["sha2"] }
rustls = { version = "0.23.0", default-features = false }
sec1 = { version = "0.7.3", default-features = false, features = ["pkcs8", "pem"] }
Expand All @@ -41,11 +40,8 @@ signature = { version = "2.1.0", default-features = false }
webpki = { package = "rustls-webpki", version = "0.102.0", default-features = false }
x25519-dalek = { version = "2", default-features = false }

[dev-dependencies]
getrandom = { version = "0.2", features = ["custom"] } # workaround to build on no_std targets

[features]
default = ["std", "tls12", "zeroize"]
default = ["std", "tls12", "zeroize", "getrandom"]
logging = ["rustls/logging"]
tls12 = ["rustls/tls12"]

Expand All @@ -57,3 +53,5 @@ std = ["alloc", "webpki/std", "pki-types/std", "rustls/std", "ed25519-dalek/std"
# TODO: go through all of these to ensure to_vec etc. impls are exposed
alloc = ["webpki/alloc", "pki-types/alloc", "aead/alloc", "ed25519-dalek/alloc"]
zeroize = ["ed25519-dalek/zeroize", "x25519-dalek/zeroize"]

getrandom = ["rand_core/getrandom"]
4 changes: 2 additions & 2 deletions src/kx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl crypto::SupportedKxGroup for X25519 {
}

fn start(&self) -> Result<Box<dyn crypto::ActiveKeyExchange>, rustls::Error> {
let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(rand_core::OsRng);
let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(crate::CryptoProviderRng);
let pub_key = (&priv_key).into();
Ok(Box::new(X25519KeyExchange { priv_key, pub_key }))
}
Expand Down Expand Up @@ -60,7 +60,7 @@ macro_rules! impl_kx {
}

fn start(&self) -> Result<Box<dyn crypto::ActiveKeyExchange>, rustls::Error> {
let priv_key = $secret::random(&mut rand_core::OsRng);
let priv_key = $secret::random(&mut crate::CryptoProviderRng);
let pub_key: $public_key = (&priv_key).into();
Ok(Box::new([<$name KeyExchange>] {
priv_key,
Expand Down
91 changes: 69 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,86 @@ compile_error!("Rustls currently does not support alloc-less environments");
#[cfg(feature = "alloc")]
extern crate alloc;

use core::fmt::Debug;

use core::num::NonZeroU32;

#[cfg(feature = "alloc")]
use alloc::sync::Arc;

use rustls::crypto::{
CipherSuiteCommon, CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom,
};
use rand_core::{CryptoRng, RngCore};
use rustls::crypto::{CipherSuiteCommon, CryptoProvider, KeyProvider, SecureRandom};
use rustls::{CipherSuite, SupportedCipherSuite, Tls13CipherSuite};

#[cfg(feature = "tls12")]
use rustls::SignatureScheme;

#[cfg(feature = "getrandom")]
use rustls::crypto::GetRandomFailed;

#[derive(Debug, Clone)]
pub(crate) struct CryptoProviderRng;

impl RngCore for CryptoProviderRng {
fn next_u32(&mut self) -> u32 {
let mut limbs: [u8; 4] = [0; 4];
self.fill_bytes(&mut limbs);
u32::from_ne_bytes(limbs)
}

fn next_u64(&mut self) -> u64 {
let mut limbs: [u8; 8] = [0; 8];
self.fill_bytes(&mut limbs);
u64::from_ne_bytes(limbs)
}

fn fill_bytes(&mut self, dest: &mut [u8]) {
self.try_fill_bytes(dest)
.expect("random bytes should be filled")
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
CryptoProvider::get_default()
.expect("provider should be set")
.secure_random
.fill(dest)
.map_err(|_| unsafe { NonZeroU32::new_unchecked(1).into() })
}
}

impl CryptoRng for CryptoProviderRng {}

#[derive(Debug)]
#[cfg(feature = "getrandom")]
struct OsRngSecureRandom;

#[cfg(feature = "getrandom")]
impl SecureRandom for OsRngSecureRandom {
fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> {
rand_core::OsRng
.try_fill_bytes(buf)
.map_err(|_| GetRandomFailed)
}
}

#[derive(Debug, Clone)]
pub struct Provider;

#[cfg(feature = "getrandom")]
pub fn provider() -> CryptoProvider {
provider_with_rng(&OsRngSecureRandom)
}

pub fn provider_with_rng(rng: &'static dyn SecureRandom) -> CryptoProvider {
CryptoProvider {
cipher_suites: ALL_CIPHER_SUITES.to_vec(),
kx_groups: kx::ALL_KX_GROUPS.to_vec(),
signature_verification_algorithms: verify::ALGORITHMS,
secure_random: &Provider,
secure_random: rng,
key_provider: &Provider,
}
}

impl SecureRandom for Provider {
fn fill(&self, bytes: &mut [u8]) -> Result<(), GetRandomFailed> {
use rand_core::RngCore;
rand_core::OsRng
.try_fill_bytes(bytes)
.map_err(|_| GetRandomFailed)
}
}

impl KeyProvider for Provider {
fn load_private_key(
&self,
Expand All @@ -81,15 +128,15 @@ impl KeyProvider for Provider {
}

#[cfg(feature = "tls12")]
const TLS12_ECDSA_SCHEMES: [SignatureScheme; 4] = [
pub const TLS12_ECDSA_SCHEMES: [SignatureScheme; 4] = [
SignatureScheme::ECDSA_NISTP256_SHA256,
SignatureScheme::ECDSA_NISTP384_SHA384,
SignatureScheme::ECDSA_NISTP521_SHA512,
SignatureScheme::ED25519,
];

#[cfg(feature = "tls12")]
const TLS12_RSA_SCHEMES: [SignatureScheme; 6] = [
pub const TLS12_RSA_SCHEMES: [SignatureScheme; 6] = [
SignatureScheme::RSA_PKCS1_SHA256,
SignatureScheme::RSA_PKCS1_SHA384,
SignatureScheme::RSA_PKCS1_SHA512,
Expand Down Expand Up @@ -141,7 +188,7 @@ pub const TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
});

#[cfg(feature = "tls12")]
const TLS_ECDHE_ECDSA_SUITES: &[SupportedCipherSuite] = &[
pub const TLS_ECDHE_ECDSA_SUITES: &[SupportedCipherSuite] = &[
TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256,
Expand Down Expand Up @@ -190,21 +237,21 @@ pub const TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
});

#[cfg(feature = "tls12")]
const TLS_ECDHE_RSA_SUITES: &[SupportedCipherSuite] = &[
pub const TLS_ECDHE_RSA_SUITES: &[SupportedCipherSuite] = &[
TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,
TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256,
];

#[cfg(feature = "tls12")]
const TLS12_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!(
pub const TLS12_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!(
SupportedCipherSuite,
TLS_ECDHE_ECDSA_SUITES,
TLS_ECDHE_RSA_SUITES
);

#[cfg(not(feature = "tls12"))]
const TLS12_SUITES: &[SupportedCipherSuite] = &[];
pub const TLS12_SUITES: &[SupportedCipherSuite] = &[];

pub const TLS13_AES_128_GCM_SHA256: SupportedCipherSuite =
SupportedCipherSuite::Tls13(&Tls13CipherSuite {
Expand All @@ -230,7 +277,7 @@ pub const TLS13_AES_256_GCM_SHA384: SupportedCipherSuite =
quic: None,
});

const TLS13_AES_SUITES: &[SupportedCipherSuite] =
pub const TLS13_AES_SUITES: &[SupportedCipherSuite] =
&[TLS13_AES_128_GCM_SHA256, TLS13_AES_256_GCM_SHA384];

pub const TLS13_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
Expand All @@ -245,13 +292,13 @@ pub const TLS13_CHACHA20_POLY1305_SHA256: SupportedCipherSuite =
quic: None,
});

const TLS13_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!(
pub const TLS13_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!(
SupportedCipherSuite,
TLS13_AES_SUITES,
&[TLS13_CHACHA20_POLY1305_SHA256]
);

static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!(
pub const ALL_CIPHER_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!(
SupportedCipherSuite,
if cfg!(feature = "tls12") {
TLS12_SUITES
Expand Down
2 changes: 1 addition & 1 deletion src/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ where
{
fn sign(&self, message: &[u8]) -> Result<Vec<u8>, Error> {
self.key
.try_sign_with_rng(&mut rand_core::OsRng, message)
.try_sign_with_rng(&mut crate::CryptoProviderRng, message)
.map_err(|_| rustls::Error::General("signing failed".into()))
.map(|sig: S| sig.to_vec())
}
Expand Down
2 changes: 1 addition & 1 deletion src/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use self::rsa::{
RSA_PSS_SHA512,
};

pub static ALGORITHMS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
pub const ALGORITHMS: WebPkiSupportedAlgorithms = WebPkiSupportedAlgorithms {
all: &[
ECDSA_P256_SHA256,
ECDSA_P256_SHA384,
Expand Down
Loading