Skip to content

Commit

Permalink
crypto: use race::OnceBox for no-std support
Browse files Browse the repository at this point in the history
When built w/ the `std` feature, the `DEFAULT_CRYPTO_PROVIDER` and
assoc. fns use a standard `once_cell::sync::OnceCell`. When built w/o
the `std` feature we use `once_cell::race::OnceBox`, requiring some
minor adjustments to account for the expectation of storing
a `Box<Arc<CryptoProvider>>` in place of a `Arc<CryptoProvider>`.
  • Loading branch information
cpu authored and ctz committed Feb 27, 2024
1 parent 0b5a434 commit 8805236
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions rustls/src/crypto/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ use alloc::sync::Arc;
use alloc::vec::Vec;
use core::fmt::Debug;

#[cfg(not(feature = "std"))]
use once_cell::race::OnceBox;
#[cfg(feature = "std")]
use once_cell::sync::OnceCell;

use pki_types::PrivateKeyDer;
use zeroize::Zeroize;

Expand Down Expand Up @@ -224,10 +228,23 @@ impl CryptoProvider {
/// Call this early in your process to configure which provider is used for
/// the provider. The configuration should happen before any use of
/// [`ClientConfig::builder()`] or [`ServerConfig::builder()`].
#[cfg(feature = "std")]
pub fn install_default(self) -> Result<(), Arc<Self>> {
PROCESS_DEFAULT_PROVIDER.set(Arc::new(self))
}

/// Sets this `CryptoProvider` as the default for this process.
///
/// This can be called successfully at most once in any process execution.
///
/// Call this early in your process to configure which provider is used for
/// the provider. The configuration should happen before any use of
/// [`ClientConfig::builder()`] or [`ServerConfig::builder()`].
#[cfg(not(feature = "std"))]
pub fn install_default(self) -> Result<(), Box<Arc<Self>>> {
PROCESS_DEFAULT_PROVIDER.set(Box::new(Arc::new(self)))
}

/// Returns the default `CryptoProvider` for this process.
///
/// This will be `None` if no default has been set yet.
Expand Down Expand Up @@ -294,7 +311,10 @@ impl CryptoProvider {
}
}

#[cfg(feature = "std")]
static PROCESS_DEFAULT_PROVIDER: OnceCell<Arc<CryptoProvider>> = OnceCell::new();
#[cfg(not(feature = "std"))]
static PROCESS_DEFAULT_PROVIDER: OnceBox<Arc<CryptoProvider>> = OnceBox::new();

/// A source of cryptographically secure randomness.
pub trait SecureRandom: Send + Sync + Debug {
Expand Down

0 comments on commit 8805236

Please sign in to comment.