From 48f3f0731155b4d563541450f44c28e3dcbf8b73 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 16:41:27 +0800 Subject: [PATCH 01/13] get rid of the `getrandom` requirement --- Cargo.lock | 2 +- Cargo.toml | 6 ++-- src/kx.rs | 4 +-- src/lib.rs | 88 +++++++++++++++++++++++++++++++++++++++++++++-------- src/sign.rs | 4 ++- 5 files changed, 86 insertions(+), 18 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3e1d73e..1ce9bd0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -684,7 +684,7 @@ checksum = "5ede67b28608b4c60685c7d54122d4400d90f62b40caee7700e700380a390fa8" [[package]] name = "rustls-rustcrypto" -version = "0.0.1-alpha" +version = "0.0.2-alpha" dependencies = [ "aead", "aes-gcm", diff --git a/Cargo.toml b/Cargo.toml index 6ff6103..4a36402 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,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"] } @@ -45,7 +45,7 @@ x25519-dalek = { version = "2", default-features = false } 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"] @@ -57,3 +57,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 = ["alloc", "rand_core/getrandom"] \ No newline at end of file diff --git a/src/kx.rs b/src/kx.rs index 66341f2..31317ff 100644 --- a/src/kx.rs +++ b/src/kx.rs @@ -14,7 +14,7 @@ impl crypto::SupportedKxGroup for X25519 { } fn start(&self) -> Result, rustls::Error> { - let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(rand_core::OsRng); + let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(crate::Provider); let pub_key = (&priv_key).into(); Ok(Box::new(X25519KeyExchange { priv_key, pub_key })) } @@ -60,7 +60,7 @@ macro_rules! impl_kx { } fn start(&self) -> Result, rustls::Error> { - let priv_key = $secret::random(&mut rand_core::OsRng); + let priv_key = $secret::random(&mut crate::Provider); let pub_key: $public_key = (&priv_key).into(); Ok(Box::new([<$name KeyExchange>] { priv_key, diff --git a/src/lib.rs b/src/lib.rs index 1b108b8..741778c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,9 +38,12 @@ compile_error!("Rustls currently does not support alloc-less environments"); #[cfg(feature = "alloc")] extern crate alloc; +use core::{cell::OnceCell, fmt::Debug}; + #[cfg(feature = "alloc")] -use alloc::sync::Arc; +use alloc::{boxed::Box, sync::Arc}; +use rand_core::{CryptoRng, RngCore}; use rustls::crypto::{ CipherSuiteCommon, CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom, }; @@ -49,7 +52,7 @@ use rustls::{CipherSuite, SupportedCipherSuite, Tls13CipherSuite}; #[cfg(feature = "tls12")] use rustls::SignatureScheme; -#[derive(Debug)] +#[derive(Debug, Clone)] pub struct Provider; pub fn provider() -> CryptoProvider { @@ -62,15 +65,76 @@ pub fn provider() -> CryptoProvider { } } +// TODO: switch to ThinBox once it is available +#[cfg(feature = "alloc")] +static mut RNG: OnceCell> = OnceCell::new(); + +#[cfg(feature = "alloc")] +fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { + #[cfg(feature = "getrandom")] + #[allow(static_mut_refs)] + // SAFETY: we only init the randomness source if the once cell was not initialized + unsafe { + // TODO: Add unlikely(...) later when it is stabilized for faster speculative branch + if RNG.get().is_none() { + // This would either set the randomness source or panic, in other word, infallible + init_randomness_source(Box::new(rand_core::OsRng)); + } + } + + // SAFETY: If randomness source is not already set, the whole program panics due to the unwrap + // UNSAFETY: If you have a memory corruption (whether stack or heap or not), this could + #[allow(static_mut_refs)] + unsafe { + RNG.get_mut().expect("RNG was not set").as_mut() + } +} + +// Initialize an RNG source, and panic if it was already set, which would only happen if two threads set the data at the same time. +// This ensures the user would have to decide on the RNG source at the very beginning, likely the first function call in main and find way to provide entropy themselves +// TIP: Use Box::from_raw to prevent having to do real heap allocation if you can assume your program to be single-threaded and your put RNG state as a global variable +#[cfg(feature = "alloc")] +pub fn init_randomness_source(rng: Box) { + // SAFETY: If randomness source is already set, the whole program panics + #[allow(static_mut_refs)] + unsafe { + if RNG.set(rng).is_err() { + panic!("RNG was set twice") + } + } +} + +#[cfg(feature = "alloc")] impl SecureRandom for Provider { fn fill(&self, bytes: &mut [u8]) -> Result<(), GetRandomFailed> { - use rand_core::RngCore; - rand_core::OsRng + get_rng_danger() .try_fill_bytes(bytes) .map_err(|_| GetRandomFailed) } } +#[cfg(feature = "alloc")] +impl RngCore for Provider { + fn next_u32(&mut self) -> u32 { + get_rng_danger().next_u32() + } + + fn next_u64(&mut self) -> u64 { + get_rng_danger().next_u64() + } + + fn fill_bytes(&mut self, dest: &mut [u8]) { + get_rng_danger().fill_bytes(dest) + } + + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { + get_rng_danger().try_fill_bytes(dest) + } +} + +#[cfg(feature = "alloc")] +impl CryptoRng for Provider {} + impl KeyProvider for Provider { fn load_private_key( &self, @@ -81,7 +145,7 @@ 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, @@ -89,7 +153,7 @@ const TLS12_ECDSA_SCHEMES: [SignatureScheme; 4] = [ ]; #[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, @@ -190,21 +254,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 { @@ -230,7 +294,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 = @@ -245,13 +309,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 static ALL_CIPHER_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!( SupportedCipherSuite, if cfg!(feature = "tls12") { TLS12_SUITES diff --git a/src/sign.rs b/src/sign.rs index e6109f5..b523299 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -2,6 +2,8 @@ use alloc::{sync::Arc, vec::Vec}; use core::marker::PhantomData; +use crate::Provider; + use self::ecdsa::{EcdsaSigningKeyP256, EcdsaSigningKeyP384}; use self::eddsa::Ed25519SigningKey; use self::rsa::RsaSigningKey; @@ -29,7 +31,7 @@ where { fn sign(&self, message: &[u8]) -> Result, Error> { self.key - .try_sign_with_rng(&mut rand_core::OsRng, message) + .try_sign_with_rng(&mut Provider, message) .map_err(|_| rustls::Error::General("signing failed".into())) .map(|sig: S| sig.to_vec()) } From 6ed88f0c355da9fb1bba8c615899fb863aa16f6c Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:04:36 +0800 Subject: [PATCH 02/13] add a simple function to combine both provider and init steps --- Cargo.toml | 1 - src/lib.rs | 6 ++++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 4a36402..b914648 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/src/lib.rs b/src/lib.rs index 741778c..ba06a8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -65,6 +65,12 @@ pub fn provider() -> CryptoProvider { } } +#[cfg(feature = "alloc")] +pub fn provider_and_init_rng(rng: Box) -> CryptoProvider { + init_randomness_source(rng); + provider() +} + // TODO: switch to ThinBox once it is available #[cfg(feature = "alloc")] static mut RNG: OnceCell> = OnceCell::new(); From 3205704331db4eb1c9180404e55f2fedd663f5d0 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:08:56 +0800 Subject: [PATCH 03/13] update comments --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ba06a8d..af78f15 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -89,7 +89,7 @@ fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { } // SAFETY: If randomness source is not already set, the whole program panics due to the unwrap - // UNSAFETY: If you have a memory corruption (whether stack or heap or not), this could + // UNSAFETY: If you have a memory corruption (whether stack or heap or not), this assumption could be violated #[allow(static_mut_refs)] unsafe { RNG.get_mut().expect("RNG was not set").as_mut() @@ -98,7 +98,7 @@ fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { // Initialize an RNG source, and panic if it was already set, which would only happen if two threads set the data at the same time. // This ensures the user would have to decide on the RNG source at the very beginning, likely the first function call in main and find way to provide entropy themselves -// TIP: Use Box::from_raw to prevent having to do real heap allocation if you can assume your program to be single-threaded and your put RNG state as a global variable +// TIP: Use Box::from_raw to prevent having to do real heap allocation if you can assume your program to be single-threaded and you put RNG state as a global variable, which is usually useful for MCUs #[cfg(feature = "alloc")] pub fn init_randomness_source(rng: Box) { // SAFETY: If randomness source is already set, the whole program panics From 87ef834af4f60297a15963bdf4a44ca1d93dd355 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:48:57 +0800 Subject: [PATCH 04/13] do it in a thread safe way --- Cargo.lock | 361 ++++++++++++++++++++++++++++++++++++++++++++++++++--- Cargo.toml | 6 +- src/lib.rs | 47 +++---- 3 files changed, 366 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1ce9bd0..5c1b0f1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,6 +37,35 @@ dependencies = [ "subtle", ] +[[package]] +name = "aho-corasick" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" +dependencies = [ + "memchr", +] + +[[package]] +name = "atomic-polyfill" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" +dependencies = [ + "critical-section", +] + +[[package]] +name = "atomic_once_cell" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6a674c439c37e76547f5757a4758b3540be48ee4384ea3f31c022b730d30202" +dependencies = [ + "atomic-polyfill", + "crossbeam-utils", + "loom", +] + [[package]] name = "autocfg" version = "1.0.1" @@ -132,6 +161,18 @@ dependencies = [ "libc", ] +[[package]] +name = "critical-section" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f64009896348fc5af4222e9cf7d7d82a95a256c634ebcf61c53e4ea461422242" + +[[package]] +name = "crossbeam-utils" +version = "0.8.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" + [[package]] name = "crypto-bigint" version = "0.5.5" @@ -289,6 +330,19 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1676f435fc1dadde4d03e43f5d62b259e1ce5f40bd4ffb21db2b42ebe59c1382" +[[package]] +name = "generator" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dbb949699c3e4df3a183b1d2142cb24277057055ed23c68ed58894f76c517223" +dependencies = [ + "cfg-if", + "libc", + "log", + "rustversion", + "windows", +] + [[package]] name = "generic-array" version = "0.14.7" @@ -386,6 +440,44 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" +[[package]] +name = "loom" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" +dependencies = [ + "cfg-if", + "generator", + "scoped-tls", + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "matchers" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" +dependencies = [ + "regex-automata 0.1.10", +] + +[[package]] +name = "memchr" +version = "2.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" + +[[package]] +name = "nu-ansi-term" +version = "0.46.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" +dependencies = [ + "overload", + "winapi", +] + [[package]] name = "num-bigint-dig" version = "0.8.4" @@ -445,6 +537,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "overload" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" + [[package]] name = "p256" version = "0.13.2" @@ -484,6 +582,12 @@ dependencies = [ "base64ct", ] +[[package]] +name = "pin-project-lite" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" + [[package]] name = "pkcs1" version = "0.7.5" @@ -607,6 +711,50 @@ dependencies = [ "getrandom", ] +[[package]] +name = "regex" +version = "1.10.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" +dependencies = [ + "aho-corasick", + "memchr", + "regex-automata 0.4.7", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-automata" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" +dependencies = [ + "regex-syntax 0.6.29", +] + +[[package]] +name = "regex-automata" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax 0.8.4", +] + +[[package]] +name = "regex-syntax" +version = "0.6.29" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" + +[[package]] +name = "regex-syntax" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" + [[package]] name = "rfc6979" version = "0.4.0" @@ -688,13 +836,13 @@ version = "0.0.2-alpha" dependencies = [ "aead", "aes-gcm", + "atomic_once_cell", "chacha20poly1305", "crypto-common", "der", "digest", "ecdsa", "ed25519-dalek", - "getrandom", "hmac", "p256", "p384", @@ -722,6 +870,18 @@ dependencies = [ "untrusted", ] +[[package]] +name = "rustversion" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" + +[[package]] +name = "scoped-tls" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" + [[package]] name = "sec1" version = "0.7.3" @@ -773,6 +933,15 @@ dependencies = [ "digest", ] +[[package]] +name = "sharded-slab" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" +dependencies = [ + "lazy_static", +] + [[package]] name = "signature" version = "2.2.0" @@ -828,6 +997,65 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "thread_local" +version = "1.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" +dependencies = [ + "cfg-if", + "once_cell", +] + +[[package]] +name = "tracing" +version = "0.1.40" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" +dependencies = [ + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" +dependencies = [ + "once_cell", + "valuable", +] + +[[package]] +name = "tracing-log" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" +dependencies = [ + "log", + "once_cell", + "tracing-core", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" +dependencies = [ + "matchers", + "nu-ansi-term", + "once_cell", + "regex", + "sharded-slab", + "smallvec", + "thread_local", + "tracing", + "tracing-core", + "tracing-log", +] + [[package]] name = "typenum" version = "1.17.0" @@ -856,6 +1084,12 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "version_check" version = "0.9.4" @@ -868,6 +1102,92 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] +name = "windows" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" +dependencies = [ + "windows-core", + "windows-targets", +] + +[[package]] +name = "windows-core" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-result", + "windows-strings", + "windows-targets", +] + +[[package]] +name = "windows-implement" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.58.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-result" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-strings" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" +dependencies = [ + "windows-result", + "windows-targets", +] + [[package]] name = "windows-sys" version = "0.52.0" @@ -879,13 +1199,14 @@ dependencies = [ [[package]] name = "windows-targets" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dd37b7e5ab9018759f893a1952c9420d060016fc19a472b4bb20d1bdd694d1b" +checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973" dependencies = [ "windows_aarch64_gnullvm", "windows_aarch64_msvc", "windows_i686_gnu", + "windows_i686_gnullvm", "windows_i686_msvc", "windows_x86_64_gnu", "windows_x86_64_gnullvm", @@ -894,45 +1215,51 @@ dependencies = [ [[package]] name = "windows_aarch64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bcf46cf4c365c6f2d1cc93ce535f2c8b244591df96ceee75d8e83deb70a9cac9" +checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3" [[package]] name = "windows_aarch64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "da9f259dd3bcf6990b55bffd094c4f7235817ba4ceebde8e6d11cd0c5633b675" +checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469" [[package]] name = "windows_i686_gnu" -version = "0.52.4" +version = "0.52.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b" + +[[package]] +name = "windows_i686_gnullvm" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b474d8268f99e0995f25b9f095bc7434632601028cf86590aea5c8a5cb7801d3" +checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66" [[package]] name = "windows_i686_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1515e9a29e5bed743cb4415a9ecf5dfca648ce85ee42e15873c3cd8610ff8e02" +checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66" [[package]] name = "windows_x86_64_gnu" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5eee091590e89cc02ad514ffe3ead9eb6b660aedca2183455434b93546371a03" +checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78" [[package]] name = "windows_x86_64_gnullvm" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77ca79f2451b49fa9e2af39f0747fe999fcda4f5e241b2898624dca97a1f2177" +checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d" [[package]] name = "windows_x86_64_msvc" -version = "0.52.4" +version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "32b752e52a2da0ddfbdbcc6fceadfeede4c939ed16d13e648833a61dfb611ed8" +checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" [[package]] name = "x25519-dalek" diff --git a/Cargo.toml b/Cargo.toml index b914648..30a8517 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,6 +19,7 @@ rust-version = "1.75" [dependencies] aead = { version = "0.5.2", default-features = false } aes-gcm = { version = "0.10.3", default-features = false, features = ["aes", "alloc"] } +atomic_once_cell = "0.1.6" chacha20poly1305 = { version = "0.10.1", default-features = false } crypto-common = { version = "0.1.6", default-features = false } der = { version = "0.7.9", default-features = false } @@ -40,9 +41,6 @@ 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", "getrandom"] logging = ["rustls/logging"] @@ -57,4 +55,4 @@ std = ["alloc", "webpki/std", "pki-types/std", "rustls/std", "ed25519-dalek/std" alloc = ["webpki/alloc", "pki-types/alloc", "aead/alloc", "ed25519-dalek/alloc"] zeroize = ["ed25519-dalek/zeroize", "x25519-dalek/zeroize"] -getrandom = ["alloc", "rand_core/getrandom"] \ No newline at end of file +getrandom = ["rand_core/getrandom"] diff --git a/src/lib.rs b/src/lib.rs index af78f15..9c439ed 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,10 +38,13 @@ compile_error!("Rustls currently does not support alloc-less environments"); #[cfg(feature = "alloc")] extern crate alloc; -use core::{cell::OnceCell, fmt::Debug}; +use core::fmt::Debug; #[cfg(feature = "alloc")] -use alloc::{boxed::Box, sync::Arc}; +use atomic_once_cell::AtomicOnceCell; + +#[cfg(feature = "alloc")] +use alloc::sync::Arc; use rand_core::{CryptoRng, RngCore}; use rustls::crypto::{ @@ -65,52 +68,44 @@ pub fn provider() -> CryptoProvider { } } -#[cfg(feature = "alloc")] -pub fn provider_and_init_rng(rng: Box) -> CryptoProvider { - init_randomness_source(rng); +pub fn provider_and_init_rng(rng: &'static mut (dyn RngCore + Send + Sync)) -> CryptoProvider { + unsafe { + init_randomness_source(rng); + } provider() } // TODO: switch to ThinBox once it is available -#[cfg(feature = "alloc")] -static mut RNG: OnceCell> = OnceCell::new(); +static mut RNG: AtomicOnceCell<&'static mut (dyn RngCore + Send + Sync)> = AtomicOnceCell::new(); + -#[cfg(feature = "alloc")] fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { #[cfg(feature = "getrandom")] - #[allow(static_mut_refs)] // SAFETY: we only init the randomness source if the once cell was not initialized unsafe { - // TODO: Add unlikely(...) later when it is stabilized for faster speculative branch - if RNG.get().is_none() { - // This would either set the randomness source or panic, in other word, infallible - init_randomness_source(Box::new(rand_core::OsRng)); - } + static mut OS_RNG: &'static mut (dyn RngCore + Send + Sync) = &mut rand_core::OsRng; + init_randomness_source(OS_RNG); } // SAFETY: If randomness source is not already set, the whole program panics due to the unwrap // UNSAFETY: If you have a memory corruption (whether stack or heap or not), this assumption could be violated #[allow(static_mut_refs)] unsafe { - RNG.get_mut().expect("RNG was not set").as_mut() + RNG.get_mut().expect("RNG was not set") } } -// Initialize an RNG source, and panic if it was already set, which would only happen if two threads set the data at the same time. +// Initialize an RNG source, and panic if was already set when it think it is unset, which would only happen if two threads set the data at the same time. // This ensures the user would have to decide on the RNG source at the very beginning, likely the first function call in main and find way to provide entropy themselves -// TIP: Use Box::from_raw to prevent having to do real heap allocation if you can assume your program to be single-threaded and you put RNG state as a global variable, which is usually useful for MCUs -#[cfg(feature = "alloc")] -pub fn init_randomness_source(rng: Box) { +// TIP: you can put your RNG state as a global variable, which is usually useful for MCUs +pub unsafe fn init_randomness_source(rng: &'static mut (dyn RngCore + Send + Sync)) { // SAFETY: If randomness source is already set, the whole program panics #[allow(static_mut_refs)] unsafe { - if RNG.set(rng).is_err() { - panic!("RNG was set twice") - } + let _ = RNG.set(rng); } } -#[cfg(feature = "alloc")] impl SecureRandom for Provider { fn fill(&self, bytes: &mut [u8]) -> Result<(), GetRandomFailed> { get_rng_danger() @@ -119,7 +114,6 @@ impl SecureRandom for Provider { } } -#[cfg(feature = "alloc")] impl RngCore for Provider { fn next_u32(&mut self) -> u32 { get_rng_danger().next_u32() @@ -138,7 +132,6 @@ impl RngCore for Provider { } } -#[cfg(feature = "alloc")] impl CryptoRng for Provider {} impl KeyProvider for Provider { @@ -211,7 +204,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, @@ -321,7 +314,7 @@ pub const TLS13_SUITES: &[SupportedCipherSuite] = misc::const_concat_slices!( &[TLS13_CHACHA20_POLY1305_SHA256] ); -pub 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 From c433975a5c7c7b4b93d6270be20de317cdbbae8d Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 17:50:27 +0800 Subject: [PATCH 05/13] remove comment about thin box --- src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9c439ed..f1350d8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -75,7 +75,6 @@ pub fn provider_and_init_rng(rng: &'static mut (dyn RngCore + Send + Sync)) -> C provider() } -// TODO: switch to ThinBox once it is available static mut RNG: AtomicOnceCell<&'static mut (dyn RngCore + Send + Sync)> = AtomicOnceCell::new(); From 96554315cc92bcf19429ad21a0b20f5bd20ba9d1 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:16:14 +0800 Subject: [PATCH 06/13] separate AtomicOnceCell into its own feature --- Cargo.toml | 5 +++-- src/lib.rs | 17 +++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 30a8517..8b90856 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ rust-version = "1.75" [dependencies] aead = { version = "0.5.2", default-features = false } aes-gcm = { version = "0.10.3", default-features = false, features = ["aes", "alloc"] } -atomic_once_cell = "0.1.6" +atomic_once_cell = { version = "0.1.6", optional = true } chacha20poly1305 = { version = "0.10.1", default-features = false } crypto-common = { version = "0.1.6", default-features = false } der = { version = "0.7.9", default-features = false } @@ -42,7 +42,7 @@ webpki = { package = "rustls-webpki", version = "0.102.0", default-features = fa x25519-dalek = { version = "2", default-features = false } [features] -default = ["std", "tls12", "zeroize", "getrandom"] +default = ["std", "tls12", "zeroize", "getrandom", "atomic"] logging = ["rustls/logging"] tls12 = ["rustls/tls12"] @@ -56,3 +56,4 @@ alloc = ["webpki/alloc", "pki-types/alloc", "aead/alloc", "ed25519-dalek/alloc"] zeroize = ["ed25519-dalek/zeroize", "x25519-dalek/zeroize"] getrandom = ["rand_core/getrandom"] +atomic = ["dep:atomic_once_cell"] \ No newline at end of file diff --git a/src/lib.rs b/src/lib.rs index f1350d8..3e5421c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,8 +40,11 @@ extern crate alloc; use core::fmt::Debug; -#[cfg(feature = "alloc")] -use atomic_once_cell::AtomicOnceCell; +#[cfg(not(feature = "atomic"))] +use core::cell::OnceCell; + +#[cfg(feature = "atomic")] +use atomic_once_cell::AtomicOnceCell as OnceCell; #[cfg(feature = "alloc")] use alloc::sync::Arc; @@ -75,8 +78,9 @@ pub fn provider_and_init_rng(rng: &'static mut (dyn RngCore + Send + Sync)) -> C provider() } -static mut RNG: AtomicOnceCell<&'static mut (dyn RngCore + Send + Sync)> = AtomicOnceCell::new(); - +// The global RNG cell that points to a user-defined, custom global RNG state. +// Technically speaking, we want something similar to a lazy cell, except the user can customize the closure +static mut RNG: OnceCell<&'static mut (dyn RngCore + Send + Sync)> = OnceCell::new(); fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { #[cfg(feature = "getrandom")] @@ -94,11 +98,12 @@ fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { } } -// Initialize an RNG source, and panic if was already set when it think it is unset, which would only happen if two threads set the data at the same time. +// Initialize an RNG source, and panic if was already set when it think it is unset, which would only happen if two threads set the data at the same time, otherwise a no-op if it was already set. // This ensures the user would have to decide on the RNG source at the very beginning, likely the first function call in main and find way to provide entropy themselves // TIP: you can put your RNG state as a global variable, which is usually useful for MCUs pub unsafe fn init_randomness_source(rng: &'static mut (dyn RngCore + Send + Sync)) { - // SAFETY: If randomness source is already set, the whole program panics + // SAFETY (under "atomic" assumption): If the randomness source is already set in progress when it is trying to set the value, either one can safely commit the write or the whole program panic + // DANGER (without "atomic" assumption): this operation can be racy if any two asymmetric cores access the same memory region at the same time without prior cache invalidation knowledge #[allow(static_mut_refs)] unsafe { let _ = RNG.set(rng); From 8b5bfa2145317395dc6353dc4a5d6e363deb7645 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:25:39 +0800 Subject: [PATCH 07/13] raising MSRV --- .github/workflows/rustls-rustcrypto.yml | 8 ++++---- Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rustls-rustcrypto.yml b/.github/workflows/rustls-rustcrypto.yml index 37e374a..5286b92 100644 --- a/.github/workflows/rustls-rustcrypto.yml +++ b/.github/workflows/rustls-rustcrypto.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: - toolchain: 1.75.0 + toolchain: 1.78.0 components: clippy - run: cargo clippy --all --all-features -- -D warnings @@ -51,7 +51,7 @@ jobs: strategy: matrix: rust: - - 1.75.0 # MSRV + - 1.78.0 # MSRV - stable target: - armv7a-none-eabi @@ -69,7 +69,7 @@ jobs: strategy: matrix: toolchain: - - 1.75.0 # MSRV + - 1.78.0 # MSRV - stable runs-on: ubuntu-latest steps: @@ -87,7 +87,7 @@ jobs: matrix: include: - target: powerpc-unknown-linux-gnu - rust: 1.75.0 # MSRV + rust: 1.78.0 # MSRV - target: powerpc-unknown-linux-gnu rust: stable runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 8b90856..7fd18bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/RustCrypto/rustls-rustcrypto" categories = ["cryptography", "no-std"] keywords = ["rustls", "tls"] edition = "2021" -rust-version = "1.75" +rust-version = "1.78" # Ensure all dependencies + feats are mapped to crate features for correct usage # default features often have std breaking no_std and potentially other unwanted From 7876446146ed66a3ebc55c6429946be0ecaba18a Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:31:07 +0800 Subject: [PATCH 08/13] fix rustfmt --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3e5421c..82a1881 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -78,8 +78,8 @@ pub fn provider_and_init_rng(rng: &'static mut (dyn RngCore + Send + Sync)) -> C provider() } -// The global RNG cell that points to a user-defined, custom global RNG state. -// Technically speaking, we want something similar to a lazy cell, except the user can customize the closure +// The global RNG cell that points to a user-defined, custom global RNG state. +// Technically speaking, we want something similar to a lazy cell, except the user can customize the closure static mut RNG: OnceCell<&'static mut (dyn RngCore + Send + Sync)> = OnceCell::new(); fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { From db4c27d2ddcee51760018dfaab4c3e32a3736800 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 18:32:27 +0800 Subject: [PATCH 09/13] fix it so that clippy won't nag --- src/lib.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 82a1881..6f07a4d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -101,13 +101,11 @@ fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { // Initialize an RNG source, and panic if was already set when it think it is unset, which would only happen if two threads set the data at the same time, otherwise a no-op if it was already set. // This ensures the user would have to decide on the RNG source at the very beginning, likely the first function call in main and find way to provide entropy themselves // TIP: you can put your RNG state as a global variable, which is usually useful for MCUs +// SAFETY (under "atomic" assumption): If the randomness source is already set in progress when it is trying to set the value, either one can safely commit the write or the whole program panic +// DANGER (without "atomic" assumption): this operation can be racy if any two asymmetric cores access the same memory region at the same time without prior cache invalidation knowledge +#[allow(static_mut_refs)] pub unsafe fn init_randomness_source(rng: &'static mut (dyn RngCore + Send + Sync)) { - // SAFETY (under "atomic" assumption): If the randomness source is already set in progress when it is trying to set the value, either one can safely commit the write or the whole program panic - // DANGER (without "atomic" assumption): this operation can be racy if any two asymmetric cores access the same memory region at the same time without prior cache invalidation knowledge - #[allow(static_mut_refs)] - unsafe { - let _ = RNG.set(rng); - } + let _ = RNG.set(rng); } impl SecureRandom for Provider { From 327bc3d0fc45520f1e192ec5e0f8ff4fbcf3b48e Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 23:55:09 +0800 Subject: [PATCH 10/13] remove once cell requirement, put the responsibility back when provider was created --- Cargo.lock | 321 -------------------------------------------------- Cargo.toml | 6 +- src/kx.rs | 4 +- src/lib.rs | 112 ++++++++---------- src/sign.rs | 4 +- src/verify.rs | 2 +- 6 files changed, 53 insertions(+), 396 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5c1b0f1..6db4472 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -37,35 +37,6 @@ dependencies = [ "subtle", ] -[[package]] -name = "aho-corasick" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916" -dependencies = [ - "memchr", -] - -[[package]] -name = "atomic-polyfill" -version = "1.0.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8cf2bce30dfe09ef0bfaef228b9d414faaf7e563035494d7fe092dba54b300f4" -dependencies = [ - "critical-section", -] - -[[package]] -name = "atomic_once_cell" -version = "0.1.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e6a674c439c37e76547f5757a4758b3540be48ee4384ea3f31c022b730d30202" -dependencies = [ - "atomic-polyfill", - "crossbeam-utils", - "loom", -] - [[package]] name = "autocfg" version = "1.0.1" @@ -161,18 +132,6 @@ dependencies = [ "libc", ] -[[package]] -name = "critical-section" -version = "1.1.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f64009896348fc5af4222e9cf7d7d82a95a256c634ebcf61c53e4ea461422242" - -[[package]] -name = "crossbeam-utils" -version = "0.8.20" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "22ec99545bb0ed0ea7bb9b8e1e9122ea386ff8a48c0922e43f36d45ab09e0e80" - [[package]] name = "crypto-bigint" version = "0.5.5" @@ -330,19 +289,6 @@ version = "0.2.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1676f435fc1dadde4d03e43f5d62b259e1ce5f40bd4ffb21db2b42ebe59c1382" -[[package]] -name = "generator" -version = "0.8.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dbb949699c3e4df3a183b1d2142cb24277057055ed23c68ed58894f76c517223" -dependencies = [ - "cfg-if", - "libc", - "log", - "rustversion", - "windows", -] - [[package]] name = "generic-array" version = "0.14.7" @@ -440,44 +386,6 @@ version = "0.4.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" -[[package]] -name = "loom" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "419e0dc8046cb947daa77eb95ae174acfbddb7673b4151f56d1eed8e93fbfaca" -dependencies = [ - "cfg-if", - "generator", - "scoped-tls", - "tracing", - "tracing-subscriber", -] - -[[package]] -name = "matchers" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8263075bb86c5a1b1427b5ae862e8889656f126e9f77c484496e8b47cf5c5558" -dependencies = [ - "regex-automata 0.1.10", -] - -[[package]] -name = "memchr" -version = "2.7.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" - -[[package]] -name = "nu-ansi-term" -version = "0.46.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77a8165726e8236064dbb45459242600304b42a5ea24ee2948e18e023bf7ba84" -dependencies = [ - "overload", - "winapi", -] - [[package]] name = "num-bigint-dig" version = "0.8.4" @@ -537,12 +445,6 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" -[[package]] -name = "overload" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39" - [[package]] name = "p256" version = "0.13.2" @@ -582,12 +484,6 @@ dependencies = [ "base64ct", ] -[[package]] -name = "pin-project-lite" -version = "0.2.14" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "bda66fc9667c18cb2758a2ac84d1167245054bcf85d5d1aaa6923f45801bdd02" - [[package]] name = "pkcs1" version = "0.7.5" @@ -711,50 +607,6 @@ dependencies = [ "getrandom", ] -[[package]] -name = "regex" -version = "1.10.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4219d74c6b67a3654a9fbebc4b419e22126d13d2f3c4a07ee0cb61ff79a79619" -dependencies = [ - "aho-corasick", - "memchr", - "regex-automata 0.4.7", - "regex-syntax 0.8.4", -] - -[[package]] -name = "regex-automata" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" -dependencies = [ - "regex-syntax 0.6.29", -] - -[[package]] -name = "regex-automata" -version = "0.4.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "38caf58cc5ef2fed281f89292ef23f6365465ed9a41b7a7754eb4e26496c92df" -dependencies = [ - "aho-corasick", - "memchr", - "regex-syntax 0.8.4", -] - -[[package]] -name = "regex-syntax" -version = "0.6.29" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1" - -[[package]] -name = "regex-syntax" -version = "0.8.4" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" - [[package]] name = "rfc6979" version = "0.4.0" @@ -836,7 +688,6 @@ version = "0.0.2-alpha" dependencies = [ "aead", "aes-gcm", - "atomic_once_cell", "chacha20poly1305", "crypto-common", "der", @@ -870,18 +721,6 @@ dependencies = [ "untrusted", ] -[[package]] -name = "rustversion" -version = "1.0.17" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "955d28af4278de8121b7ebeb796b6a45735dc01436d898801014aced2773a3d6" - -[[package]] -name = "scoped-tls" -version = "1.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e1cf6437eb19a8f4a6cc0f7dca544973b0b78843adbfeb3683d1a94a0024a294" - [[package]] name = "sec1" version = "0.7.3" @@ -933,15 +772,6 @@ dependencies = [ "digest", ] -[[package]] -name = "sharded-slab" -version = "0.1.7" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f40ca3c46823713e0d4209592e8d6e826aa57e928f09752619fc696c499637f6" -dependencies = [ - "lazy_static", -] - [[package]] name = "signature" version = "2.2.0" @@ -997,65 +827,6 @@ dependencies = [ "unicode-ident", ] -[[package]] -name = "thread_local" -version = "1.1.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8b9ef9bad013ada3808854ceac7b46812a6465ba368859a37e2100283d2d719c" -dependencies = [ - "cfg-if", - "once_cell", -] - -[[package]] -name = "tracing" -version = "0.1.40" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3523ab5a71916ccf420eebdf5521fcef02141234bbc0b8a49f2fdc4544364ef" -dependencies = [ - "pin-project-lite", - "tracing-core", -] - -[[package]] -name = "tracing-core" -version = "0.1.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c06d3da6113f116aaee68e4d601191614c9053067f9ab7f6edbcb161237daa54" -dependencies = [ - "once_cell", - "valuable", -] - -[[package]] -name = "tracing-log" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee855f1f400bd0e5c02d150ae5de3840039a3f54b025156404e34c23c03f47c3" -dependencies = [ - "log", - "once_cell", - "tracing-core", -] - -[[package]] -name = "tracing-subscriber" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad0f048c97dbd9faa9b7df56362b8ebcaa52adb06b498c050d2f4e32f90a7a8b" -dependencies = [ - "matchers", - "nu-ansi-term", - "once_cell", - "regex", - "sharded-slab", - "smallvec", - "thread_local", - "tracing", - "tracing-core", - "tracing-log", -] - [[package]] name = "typenum" version = "1.17.0" @@ -1084,12 +855,6 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" -[[package]] -name = "valuable" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" - [[package]] name = "version_check" version = "0.9.4" @@ -1102,92 +867,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "winapi" -version = "0.3.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" -dependencies = [ - "winapi-i686-pc-windows-gnu", - "winapi-x86_64-pc-windows-gnu", -] - -[[package]] -name = "winapi-i686-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" - -[[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" - -[[package]] -name = "windows" -version = "0.58.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dd04d41d93c4992d421894c18c8b43496aa748dd4c081bac0dc93eb0489272b6" -dependencies = [ - "windows-core", - "windows-targets", -] - -[[package]] -name = "windows-core" -version = "0.58.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ba6d44ec8c2591c134257ce647b7ea6b20335bf6379a27dac5f1641fcf59f99" -dependencies = [ - "windows-implement", - "windows-interface", - "windows-result", - "windows-strings", - "windows-targets", -] - -[[package]] -name = "windows-implement" -version = "0.58.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2bbd5b46c938e506ecbce286b6628a02171d56153ba733b6c741fc627ec9579b" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "windows-interface" -version = "0.58.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "053c4c462dc91d3b1504c6fe5a726dd15e216ba718e84a0e46a88fbe5ded3515" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] - -[[package]] -name = "windows-result" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e" -dependencies = [ - "windows-targets", -] - -[[package]] -name = "windows-strings" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd9b125c486025df0eabcb585e62173c6c9eddcec5d117d3b6e8c30e2ee4d10" -dependencies = [ - "windows-result", - "windows-targets", -] - [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index 7fd18bd..4edcd55 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,6 @@ rust-version = "1.78" [dependencies] aead = { version = "0.5.2", default-features = false } aes-gcm = { version = "0.10.3", default-features = false, features = ["aes", "alloc"] } -atomic_once_cell = { version = "0.1.6", optional = true } chacha20poly1305 = { version = "0.10.1", default-features = false } crypto-common = { version = "0.1.6", default-features = false } der = { version = "0.7.9", default-features = false } @@ -42,7 +41,7 @@ webpki = { package = "rustls-webpki", version = "0.102.0", default-features = fa x25519-dalek = { version = "2", default-features = false } [features] -default = ["std", "tls12", "zeroize", "getrandom", "atomic"] +default = ["std", "tls12", "zeroize", "getrandom"] logging = ["rustls/logging"] tls12 = ["rustls/tls12"] @@ -55,5 +54,4 @@ std = ["alloc", "webpki/std", "pki-types/std", "rustls/std", "ed25519-dalek/std" alloc = ["webpki/alloc", "pki-types/alloc", "aead/alloc", "ed25519-dalek/alloc"] zeroize = ["ed25519-dalek/zeroize", "x25519-dalek/zeroize"] -getrandom = ["rand_core/getrandom"] -atomic = ["dep:atomic_once_cell"] \ No newline at end of file +getrandom = ["rand_core/getrandom"] \ No newline at end of file diff --git a/src/kx.rs b/src/kx.rs index 31317ff..b1fa7f0 100644 --- a/src/kx.rs +++ b/src/kx.rs @@ -14,7 +14,7 @@ impl crypto::SupportedKxGroup for X25519 { } fn start(&self) -> Result, rustls::Error> { - let priv_key = x25519_dalek::EphemeralSecret::random_from_rng(crate::Provider); + 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 })) } @@ -60,7 +60,7 @@ macro_rules! impl_kx { } fn start(&self) -> Result, rustls::Error> { - let priv_key = $secret::random(&mut crate::Provider); + let priv_key = $secret::random(&mut crate::CryptoProviderRng); let pub_key: $public_key = (&priv_key).into(); Ok(Box::new([<$name KeyExchange>] { priv_key, diff --git a/src/lib.rs b/src/lib.rs index 6f07a4d..374c22a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,102 +40,84 @@ extern crate alloc; use core::fmt::Debug; -#[cfg(not(feature = "atomic"))] -use core::cell::OnceCell; - -#[cfg(feature = "atomic")] -use atomic_once_cell::AtomicOnceCell as OnceCell; +use core::num::NonZeroU32; #[cfg(feature = "alloc")] use alloc::sync::Arc; use rand_core::{CryptoRng, RngCore}; -use rustls::crypto::{ - CipherSuiteCommon, CryptoProvider, GetRandomFailed, KeyProvider, SecureRandom, -}; +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 struct Provider; +pub(crate) struct CryptoProviderRng; -pub fn provider() -> 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, - key_provider: &Provider, +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) } -} -pub fn provider_and_init_rng(rng: &'static mut (dyn RngCore + Send + Sync)) -> CryptoProvider { - unsafe { - init_randomness_source(rng); + fn next_u64(&mut self) -> u64 { + let mut limbs: [u8; 8] = [0; 8]; + self.fill_bytes(&mut limbs); + u64::from_ne_bytes(limbs) } - provider() -} - -// The global RNG cell that points to a user-defined, custom global RNG state. -// Technically speaking, we want something similar to a lazy cell, except the user can customize the closure -static mut RNG: OnceCell<&'static mut (dyn RngCore + Send + Sync)> = OnceCell::new(); -fn get_rng_danger() -> &'static mut (dyn RngCore + Send + Sync) { - #[cfg(feature = "getrandom")] - // SAFETY: we only init the randomness source if the once cell was not initialized - unsafe { - static mut OS_RNG: &'static mut (dyn RngCore + Send + Sync) = &mut rand_core::OsRng; - init_randomness_source(OS_RNG); + fn fill_bytes(&mut self, dest: &mut [u8]) { + self.try_fill_bytes(dest).unwrap() } - // SAFETY: If randomness source is not already set, the whole program panics due to the unwrap - // UNSAFETY: If you have a memory corruption (whether stack or heap or not), this assumption could be violated - #[allow(static_mut_refs)] - unsafe { - RNG.get_mut().expect("RNG was not set") + fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { + CryptoProvider::get_default() + .unwrap() + .secure_random + .fill(dest) + .map_err(|_| unsafe { NonZeroU32::new_unchecked(1).into() }) } } -// Initialize an RNG source, and panic if was already set when it think it is unset, which would only happen if two threads set the data at the same time, otherwise a no-op if it was already set. -// This ensures the user would have to decide on the RNG source at the very beginning, likely the first function call in main and find way to provide entropy themselves -// TIP: you can put your RNG state as a global variable, which is usually useful for MCUs -// SAFETY (under "atomic" assumption): If the randomness source is already set in progress when it is trying to set the value, either one can safely commit the write or the whole program panic -// DANGER (without "atomic" assumption): this operation can be racy if any two asymmetric cores access the same memory region at the same time without prior cache invalidation knowledge -#[allow(static_mut_refs)] -pub unsafe fn init_randomness_source(rng: &'static mut (dyn RngCore + Send + Sync)) { - let _ = RNG.set(rng); -} +impl CryptoRng for CryptoProviderRng {} + +#[derive(Debug)] +#[cfg(feature = "getrandom")] +struct OsRngSecureRandom; -impl SecureRandom for Provider { - fn fill(&self, bytes: &mut [u8]) -> Result<(), GetRandomFailed> { - get_rng_danger() - .try_fill_bytes(bytes) +#[cfg(feature = "getrandom")] +impl SecureRandom for OsRngSecureRandom { + fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> { + use rand_core::RngCore; + rand_core::OsRng + .try_fill_bytes(buf) .map_err(|_| GetRandomFailed) } } -impl RngCore for Provider { - fn next_u32(&mut self) -> u32 { - get_rng_danger().next_u32() - } - - fn next_u64(&mut self) -> u64 { - get_rng_danger().next_u64() - } +#[derive(Debug, Clone)] +pub struct Provider; - fn fill_bytes(&mut self, dest: &mut [u8]) { - get_rng_danger().fill_bytes(dest) - } +#[cfg(feature = "getrandom")] +pub fn provider() -> CryptoProvider { + provider_with_rng(&OsRngSecureRandom) +} - fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> { - get_rng_danger().try_fill_bytes(dest) +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: rng, + key_provider: &Provider, } } -impl CryptoRng for Provider {} - impl KeyProvider for Provider { fn load_private_key( &self, diff --git a/src/sign.rs b/src/sign.rs index b523299..deadef4 100644 --- a/src/sign.rs +++ b/src/sign.rs @@ -2,8 +2,6 @@ use alloc::{sync::Arc, vec::Vec}; use core::marker::PhantomData; -use crate::Provider; - use self::ecdsa::{EcdsaSigningKeyP256, EcdsaSigningKeyP384}; use self::eddsa::Ed25519SigningKey; use self::rsa::RsaSigningKey; @@ -31,7 +29,7 @@ where { fn sign(&self, message: &[u8]) -> Result, Error> { self.key - .try_sign_with_rng(&mut Provider, message) + .try_sign_with_rng(&mut crate::CryptoProviderRng, message) .map_err(|_| rustls::Error::General("signing failed".into())) .map(|sig: S| sig.to_vec()) } diff --git a/src/verify.rs b/src/verify.rs index 2043b64..513587c 100644 --- a/src/verify.rs +++ b/src/verify.rs @@ -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, From e41807d15c995af5bc787b249f4cb5f9f1f1558e Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Wed, 25 Sep 2024 23:57:38 +0800 Subject: [PATCH 11/13] Revert "raising MSRV" This reverts commit 8b5bfa2145317395dc6353dc4a5d6e363deb7645. --- .github/workflows/rustls-rustcrypto.yml | 8 ++++---- Cargo.toml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/.github/workflows/rustls-rustcrypto.yml b/.github/workflows/rustls-rustcrypto.yml index 5286b92..37e374a 100644 --- a/.github/workflows/rustls-rustcrypto.yml +++ b/.github/workflows/rustls-rustcrypto.yml @@ -23,7 +23,7 @@ jobs: - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@master with: - toolchain: 1.78.0 + toolchain: 1.75.0 components: clippy - run: cargo clippy --all --all-features -- -D warnings @@ -51,7 +51,7 @@ jobs: strategy: matrix: rust: - - 1.78.0 # MSRV + - 1.75.0 # MSRV - stable target: - armv7a-none-eabi @@ -69,7 +69,7 @@ jobs: strategy: matrix: toolchain: - - 1.78.0 # MSRV + - 1.75.0 # MSRV - stable runs-on: ubuntu-latest steps: @@ -87,7 +87,7 @@ jobs: matrix: include: - target: powerpc-unknown-linux-gnu - rust: 1.78.0 # MSRV + rust: 1.75.0 # MSRV - target: powerpc-unknown-linux-gnu rust: stable runs-on: ubuntu-latest diff --git a/Cargo.toml b/Cargo.toml index 4edcd55..8796370 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/RustCrypto/rustls-rustcrypto" categories = ["cryptography", "no-std"] keywords = ["rustls", "tls"] edition = "2021" -rust-version = "1.78" +rust-version = "1.75" # Ensure all dependencies + feats are mapped to crate features for correct usage # default features often have std breaking no_std and potentially other unwanted From e641e7d1822c65c069ec763aedb3163ebf80d67c Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Thu, 26 Sep 2024 00:00:59 +0800 Subject: [PATCH 12/13] don't let clippy nag again --- src/lib.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 374c22a..5bc98f8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,12 +72,12 @@ impl RngCore for CryptoProviderRng { } fn fill_bytes(&mut self, dest: &mut [u8]) { - self.try_fill_bytes(dest).unwrap() + 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() - .unwrap() + .expect("provider should be set") .secure_random .fill(dest) .map_err(|_| unsafe { NonZeroU32::new_unchecked(1).into() }) @@ -93,7 +93,6 @@ struct OsRngSecureRandom; #[cfg(feature = "getrandom")] impl SecureRandom for OsRngSecureRandom { fn fill(&self, buf: &mut [u8]) -> Result<(), GetRandomFailed> { - use rand_core::RngCore; rand_core::OsRng .try_fill_bytes(buf) .map_err(|_| GetRandomFailed) From 640a9ef6cf996f6a6fc5fbccc476dc2caec99eb5 Mon Sep 17 00:00:00 2001 From: Steve Fan <29133953+stevefan1999-personal@users.noreply.github.com> Date: Thu, 26 Sep 2024 00:08:05 +0800 Subject: [PATCH 13/13] run rustfmt --- src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 5bc98f8..4468d46 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -72,7 +72,8 @@ impl RngCore for CryptoProviderRng { } fn fill_bytes(&mut self, dest: &mut [u8]) { - self.try_fill_bytes(dest).expect("random bytes should be filled") + self.try_fill_bytes(dest) + .expect("random bytes should be filled") } fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {