From 7f78deebe59114c855c033f298a0fb03b867d272 Mon Sep 17 00:00:00 2001 From: Monica Moniot Date: Tue, 8 Aug 2023 11:30:15 -0400 Subject: [PATCH] improved oncecell usage --- crypto-glue/build.rs | 6 +++++- crypto-glue/src/p384.rs | 8 ++------ crypto-glue/src/random.rs | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/crypto-glue/build.rs b/crypto-glue/build.rs index 444b5e4..7651429 100644 --- a/crypto-glue/build.rs +++ b/crypto-glue/build.rs @@ -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; diff --git a/crypto-glue/src/p384.rs b/crypto-glue/src/p384.rs index e59ee75..1dc0026 100644 --- a/crypto-glue/src/p384.rs +++ b/crypto-glue/src/p384.rs @@ -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 = Lazy::new(||unsafe { OSSLGroup::p384() }); +static GROUP_P384: Lazy = + 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 { diff --git a/crypto-glue/src/random.rs b/crypto-glue/src/random.rs index dfb3d8c..bdc5ceb 100644 --- a/crypto-glue/src/random.rs +++ b/crypto-glue/src/random.rs @@ -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> = Mutex::new(Lazy::new(|| Xoshiro256StarStar::from_rng(SecureRandom).unwrap())); +pub static GLOBAL_XORSHIFT: Mutex> = + 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).