improved oncecell usage

This commit is contained in:
Monica Moniot
2023-08-08 11:30:15 -04:00
parent b79435f16b
commit 7f78deebe5
3 changed files with 10 additions and 10 deletions
+5 -1
View File
@@ -1,4 +1,8 @@
#![allow(clippy::inconsistent_digit_grouping, clippy::uninlined_format_args, clippy::unusual_byte_groupings)]
#![allow(
clippy::inconsistent_digit_grouping,
clippy::uninlined_format_args,
clippy::unusual_byte_groupings
)]
use std::env;
+2 -6
View File
@@ -376,14 +376,10 @@ impl Drop for OSSLKey {
/// OpenSSL wrapper for a EC_GROUP that is used to tell rust that an OpenSSL EC_GROUP is threadsafe.
/// We only ever instantiate one of these with lazy_static. It is never freed.
struct OSSLGroup(*mut ffi::EC_GROUP);
impl OSSLGroup {
unsafe fn p384() -> Self {
Self(cvt_p(ffi::EC_GROUP_new_by_curve_name(ffi::NID_secp384r1)).unwrap())
}
}
unsafe impl Send for OSSLGroup {}
unsafe impl Sync for OSSLGroup {}
static GROUP_P384: Lazy<OSSLGroup> = Lazy::new(||unsafe { OSSLGroup::p384() });
static GROUP_P384: Lazy<OSSLGroup> =
Lazy::new(|| unsafe { OSSLGroup(cvt_p(ffi::EC_GROUP_new_by_curve_name(ffi::NID_secp384r1)).unwrap()) });
impl p384::P384PublicKey for P384PublicKey {
fn from_bytes(raw_key: &[u8; p384::P384_PUBLIC_KEY_SIZE]) -> Option<Self> {
+3 -3
View File
@@ -10,7 +10,7 @@ use std::sync::Mutex;
use libc::c_int;
use once_cell::unsync::Lazy;
use rand_xoshiro::rand_core::{SeedableRng, RngCore, Error, CryptoRng};
use rand_xoshiro::rand_core::{CryptoRng, Error, RngCore, SeedableRng};
use crate::error::cvt;
@@ -23,7 +23,6 @@ pub fn fill_bytes_secure(dest: &mut [u8]) {
}
}
pub fn next_u32_secure() -> u32 {
let mut tmp = [0u8; 4];
fill_bytes_secure(&mut tmp);
@@ -130,7 +129,8 @@ pub use rand_xoshiro::Xoshiro256StarStar;
/// A global Xoshiro256** wrapped in a mutex and a OnceCell.
/// Unsync OnceCell is just a wrapped `Option<>` and is very fast.
/// Also OnceCell is about to be stabilized into Rust std.
pub static GLOBAL_XORSHIFT: Mutex<Lazy<Xoshiro256StarStar>> = Mutex::new(Lazy::new(|| Xoshiro256StarStar::from_rng(SecureRandom).unwrap()));
pub static GLOBAL_XORSHIFT: Mutex<Lazy<Xoshiro256StarStar>> =
Mutex::new(Lazy::new(|| Xoshiro256StarStar::from_rng(SecureRandom).unwrap()));
/// Quickly creates a new Xoshiro256StarStar state that is randomly seeded and fully owned by the
/// caller (does not require dereferencing and locking a global variable).