From 0a1979c12855fa9d033e7d59c28ee0974286009a Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Mon, 16 Oct 2023 13:27:58 +0200 Subject: [PATCH 1/2] add an opt-in less-safe-getrandom-custom feature This Cargo feature treats a user-provided `getrandom` implementation as a secure random number generator (`SecureRandom`). The feature only has effect on targets not supported by `getrandom`. I agree to license my contributions to each file under the terms given at the top of each file I changed. --- Cargo.toml | 1 + src/lib.rs | 8 ++++++++ src/rand.rs | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index 0297fb68a7..1b4a2dd533 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -197,6 +197,7 @@ cc = { version = "1.0.83", default-features = false } default = ["alloc", "dev_urandom_fallback"] alloc = [] dev_urandom_fallback = [] +less-safe-getrandom-custom = ["getrandom/custom"] slow_tests = [] std = ["alloc"] unstable-testing-arm-no-hw = [] diff --git a/src/lib.rs b/src/lib.rs index c7ff22efeb..ba0081160c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,6 +22,14 @@ //! Description //! alloc (default) //! Enable features that require use of the heap, RSA in particular. +//! less-safe-getrandom-custom +//! Treat a user-provided ("custom") getrandom +//! implementation as a secure random number generator (see +//! SecureRandom). Only has effect on targets +//! not supported by getrandom. +//! See +//! register_custom_getrandom +//! for details. //! std //! Enable features that use libstd, in particular //! std::error::Error integration. Implies `alloc`. diff --git a/src/rand.rs b/src/rand.rs index 603b01450b..bd22440729 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -125,6 +125,10 @@ impl crate::sealed::Sealed for SystemRandom {} // system's) CSPRNG. Avoid using it on targets where it uses the `rdrand` // implementation. #[cfg(any( + // NOTE `getrandom`'s (v0.2.10) docs state that a custom implementation will + // NOT override the implementation of supported targets, like the ones + // listed below. + feature = "less-safe-getrandom-custom", target_os = "aix", target_os = "android", target_os = "dragonfly", From 2756989abd60318b87d8657b2c8a4d3a17fc2d44 Mon Sep 17 00:00:00 2001 From: Jorge Aparicio Date: Fri, 3 Nov 2023 14:02:10 +0100 Subject: [PATCH 2/2] rename feature and have it apply only when `target_os = "none"` --- Cargo.toml | 2 +- src/lib.rs | 16 +++++++++------- src/rand.rs | 5 +---- 3 files changed, 11 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b4a2dd533..32dc5edd0c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -197,7 +197,7 @@ cc = { version = "1.0.83", default-features = false } default = ["alloc", "dev_urandom_fallback"] alloc = [] dev_urandom_fallback = [] -less-safe-getrandom-custom = ["getrandom/custom"] +less-safe-getrandom-custom-or-rdrand = [] slow_tests = [] std = ["alloc"] unstable-testing-arm-no-hw = [] diff --git a/src/lib.rs b/src/lib.rs index ba0081160c..8f0b4e49d7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,14 +22,16 @@ //! Description //! alloc (default) //! Enable features that require use of the heap, RSA in particular. -//! less-safe-getrandom-custom -//! Treat a user-provided ("custom") getrandom -//! implementation as a secure random number generator (see -//! SecureRandom). Only has effect on targets -//! not supported by getrandom. -//! See +//! less-safe-getrandom-custom-or-rdrand +//! Treat user-provided ("custom") and RDRAND-based getrandom +//! implementations as secure random number generators (see +//! SecureRandom). This feature only works with +//! os = "none" targets. See +//! //! register_custom_getrandom -//! for details. +//! and +//! RDRAND on x86 +//! for additional details. //! std //! Enable features that use libstd, in particular //! std::error::Error integration. Implies `alloc`. diff --git a/src/rand.rs b/src/rand.rs index bd22440729..809791c79c 100644 --- a/src/rand.rs +++ b/src/rand.rs @@ -125,10 +125,7 @@ impl crate::sealed::Sealed for SystemRandom {} // system's) CSPRNG. Avoid using it on targets where it uses the `rdrand` // implementation. #[cfg(any( - // NOTE `getrandom`'s (v0.2.10) docs state that a custom implementation will - // NOT override the implementation of supported targets, like the ones - // listed below. - feature = "less-safe-getrandom-custom", + all(feature = "less-safe-getrandom-custom-or-rdrand", target_os = "none"), target_os = "aix", target_os = "android", target_os = "dragonfly",