Skip to content

Commit b199e1d

Browse files
client: Drop once_cell dependency
1 parent 86c2a8f commit b199e1d

File tree

7 files changed

+50
-40
lines changed

7 files changed

+50
-40
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ async fn run() -> oo7::Result<()> {
6363
If your application makes heavy usage of the keyring like a password manager. You could store an instance of the `Keyring` in a `OnceCell`
6464

6565
```rust,ignore
66-
use once_cell::sync::OnceCell;
66+
use std::sync::OnceLock;
6767
use std::collections::HashMap;
6868
69-
static KEYRING: OnceCell<oo7::Keyring> = OnceCell::new();
69+
static KEYRING: OnceLock<oo7::Keyring> = OnceLock::new();
7070
7171
fn main() {
7272
// SOME_RUNTIME could be a tokio/async-std/glib runtime

client/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ hmac = { version = "0.12", optional = true }
4040
pbkdf2 = { version = "0.12", optional = true }
4141
sha2 = { version = "0.10", optional = true }
4242
openssl = { version = "0.10", optional = true }
43-
once_cell = "1.9"
4443
async-fs = { version = "2.1.0", optional = true }
4544
async-lock = { version = "3.2.0", optional = true }
4645
async-io = { version = "2.2.2", optional = true }

client/examples/basic_2.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
use std::collections::HashMap;
1+
use std::{collections::HashMap, sync::OnceLock};
22

3-
use once_cell::sync::OnceCell;
43
use oo7::Keyring;
54

6-
static KEYRING: OnceCell<Keyring> = OnceCell::new();
5+
static KEYRING: OnceLock<Keyring> = OnceLock::new();
76

87
#[tokio::main]
98
async fn main() -> oo7::Result<()> {

client/src/crypto/native.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
use std::ops::{Mul, Rem, Shr};
1+
use std::{
2+
ops::{Mul, Rem, Shr},
3+
sync::OnceLock,
4+
};
25

36
use cipher::{
47
block_padding::Pkcs7, BlockDecryptMut, BlockEncryptMut, BlockSizeUser, IvSizeUser, KeyIvInit,
@@ -7,7 +10,6 @@ use digest::{Mac, OutputSizeUser};
710
use hkdf::Hkdf;
811
use num::{FromPrimitive, Integer, One, Zero};
912
use num_bigint_dig::BigUint;
10-
use once_cell::sync::Lazy;
1113
use sha2::Sha256;
1214
use zeroize::{Zeroize, Zeroizing};
1315

@@ -59,7 +61,11 @@ pub(crate) fn generate_private_key() -> Zeroizing<Vec<u8>> {
5961

6062
pub(crate) fn generate_public_key(private_key: impl AsRef<[u8]>) -> Vec<u8> {
6163
let private_key_uint = BigUint::from_bytes_be(private_key.as_ref());
62-
let public_key_uint = powm(&DH_GENERATOR, private_key_uint, &DH_PRIME);
64+
let dh_generator = {
65+
static DH_GENERATOR: OnceLock<BigUint> = OnceLock::new();
66+
DH_GENERATOR.get_or_init(|| BigUint::from_u64(0x2).unwrap())
67+
};
68+
let public_key_uint = powm(dh_generator, private_key_uint);
6369

6470
public_key_uint.to_bytes_be()
6571
}
@@ -70,7 +76,7 @@ pub(crate) fn generate_aes_key(
7076
) -> Zeroizing<Vec<u8>> {
7177
let server_public_key_uint = BigUint::from_bytes_be(server_public_key.as_ref());
7278
let private_key_uint = BigUint::from_bytes_be(private_key.as_ref());
73-
let common_secret = powm(&server_public_key_uint, private_key_uint, &DH_PRIME);
79+
let common_secret = powm(&server_public_key_uint, private_key_uint);
7480

7581
let mut common_secret_bytes = common_secret.to_bytes_be();
7682
let mut common_secret_padded = vec![0; 128 - common_secret_bytes.len()];
@@ -132,24 +138,27 @@ pub(crate) fn derive_key(
132138
key
133139
}
134140

135-
// for key exchange
136-
static DH_GENERATOR: Lazy<BigUint> = Lazy::new(|| BigUint::from_u64(0x2).unwrap());
137-
static DH_PRIME: Lazy<BigUint> = Lazy::new(|| {
138-
BigUint::from_bytes_be(&[
139-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2,
140-
0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08, 0x8A, 0x67,
141-
0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A, 0x08, 0x79, 0x8E,
142-
0x34, 0x04, 0xDD, 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B, 0x30, 0x2B, 0x0A, 0x6D,
143-
0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51, 0xC2, 0x45, 0xE4, 0x85, 0xB5,
144-
0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9, 0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF,
145-
0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED, 0xEE, 0x38, 0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE,
146-
0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6, 0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81,
147-
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
148-
])
149-
});
150-
151141
/// from https://github.com/plietar/librespot/blob/master/core/src/util/mod.rs#L53
152-
fn powm(base: &BigUint, mut exp: BigUint, modulus: &BigUint) -> BigUint {
142+
fn powm(base: &BigUint, mut exp: BigUint) -> BigUint {
143+
let modulus = {
144+
// for key exchange
145+
static DH_PRIME: OnceLock<BigUint> = OnceLock::new();
146+
DH_PRIME.get_or_init(|| {
147+
BigUint::from_bytes_be(&[
148+
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68,
149+
0xC2, 0x34, 0xC4, 0xC6, 0x62, 0x8B, 0x80, 0xDC, 0x1C, 0xD1, 0x29, 0x02, 0x4E, 0x08,
150+
0x8A, 0x67, 0xCC, 0x74, 0x02, 0x0B, 0xBE, 0xA6, 0x3B, 0x13, 0x9B, 0x22, 0x51, 0x4A,
151+
0x08, 0x79, 0x8E, 0x34, 0x04, 0xDD, 0xEF, 0x95, 0x19, 0xB3, 0xCD, 0x3A, 0x43, 0x1B,
152+
0x30, 0x2B, 0x0A, 0x6D, 0xF2, 0x5F, 0x14, 0x37, 0x4F, 0xE1, 0x35, 0x6D, 0x6D, 0x51,
153+
0xC2, 0x45, 0xE4, 0x85, 0xB5, 0x76, 0x62, 0x5E, 0x7E, 0xC6, 0xF4, 0x4C, 0x42, 0xE9,
154+
0xA6, 0x37, 0xED, 0x6B, 0x0B, 0xFF, 0x5C, 0xB6, 0xF4, 0x06, 0xB7, 0xED, 0xEE, 0x38,
155+
0x6B, 0xFB, 0x5A, 0x89, 0x9F, 0xA5, 0xAE, 0x9F, 0x24, 0x11, 0x7C, 0x4B, 0x1F, 0xE6,
156+
0x49, 0x28, 0x66, 0x51, 0xEC, 0xE6, 0x53, 0x81, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
157+
0xFF, 0xFF,
158+
])
159+
})
160+
};
161+
153162
let mut base = base.clone();
154163
let mut result: BigUint = One::one();
155164

client/src/portal/api/mod.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@
77

88
#[cfg(feature = "async-std")]
99
use std::io;
10-
use std::path::{Path, PathBuf};
10+
use std::{
11+
path::{Path, PathBuf},
12+
sync::OnceLock,
13+
};
1114

1215
#[cfg(feature = "async-std")]
1316
use async_fs as fs;
1417
#[cfg(feature = "async-std")]
1518
use async_fs::unix::OpenOptionsExt;
1619
#[cfg(feature = "async-std")]
1720
use futures_lite::AsyncWriteExt;
18-
use once_cell::sync::Lazy;
1921
use rand::Rng;
2022
use serde::{Deserialize, Serialize};
2123
#[cfg(feature = "tokio")]
@@ -37,9 +39,6 @@ const FILE_HEADER_LEN: usize = FILE_HEADER.len();
3739
const MAJOR_VERSION: u8 = 1;
3840
const MINOR_VERSION: u8 = 0;
3941

40-
pub(super) static GVARIANT_ENCODING: Lazy<zvariant::EncodingContext<byteorder::LE>> =
41-
Lazy::new(|| zvariant::EncodingContext::<byteorder::LE>::new_gvariant(0));
42-
4342
mod attribute_value;
4443
mod encrypted_item;
4544

@@ -53,6 +52,11 @@ use crate::{
5352
AsAttributes, Key,
5453
};
5554

55+
pub(super) fn gvariant_encoding() -> &'static zvariant::EncodingContext<byteorder::LE> {
56+
static ENCODING: OnceLock<zvariant::EncodingContext<byteorder::LE>> = OnceLock::new();
57+
ENCODING.get_or_init(|| zvariant::EncodingContext::<byteorder::LE>::new_gvariant(0))
58+
}
59+
5660
/// Logical contents of a keyring file
5761
#[derive(Deserialize, Serialize, Type, Debug)]
5862
pub struct Keyring {
@@ -215,7 +219,7 @@ impl Keyring {
215219

216220
blob.push(MAJOR_VERSION);
217221
blob.push(MINOR_VERSION);
218-
blob.append(&mut zvariant::to_bytes(*GVARIANT_ENCODING, &self)?);
222+
blob.append(&mut zvariant::to_bytes(*gvariant_encoding(), &self)?);
219223

220224
Ok(blob)
221225
}
@@ -257,7 +261,7 @@ impl TryFrom<&[u8]> for Keyring {
257261
}
258262

259263
if let Some(data) = value.get((FILE_HEADER_LEN + 2)..) {
260-
let keyring: Self = zvariant::from_slice(data, *GVARIANT_ENCODING)?;
264+
let keyring: Self = zvariant::from_slice(data, *gvariant_encoding())?;
261265

262266
if keyring.salt.len() != keyring.salt_size as usize {
263267
Err(Error::SaltSizeMismatch(

client/src/portal/item.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use zbus::zvariant::{self, Type};
55
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
66

77
use super::{
8-
api::{AttributeValue, EncryptedItem, GVARIANT_ENCODING},
8+
api::{gvariant_encoding, AttributeValue, EncryptedItem},
99
Error,
1010
};
1111
use crate::{crypto, AsAttributes, Key};
@@ -105,7 +105,7 @@ impl Item {
105105
pub(crate) fn encrypt(&self, key: &Key) -> Result<EncryptedItem, Error> {
106106
key.check_strength()?;
107107

108-
let decrypted = Zeroizing::new(zvariant::to_bytes(*GVARIANT_ENCODING, &self)?);
108+
let decrypted = Zeroizing::new(zvariant::to_bytes(*gvariant_encoding(), &self)?);
109109

110110
let iv = crypto::generate_iv();
111111

@@ -131,6 +131,6 @@ impl TryFrom<&[u8]> for Item {
131131
type Error = Error;
132132

133133
fn try_from(value: &[u8]) -> Result<Self, Error> {
134-
Ok(zvariant::from_slice(value, *GVARIANT_ENCODING)?)
134+
Ok(zvariant::from_slice(value, *gvariant_encoding())?)
135135
}
136136
}

client/src/portal/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use std::io;
3333
use std::{
3434
collections::HashMap,
3535
path::{Path, PathBuf},
36-
sync::Arc,
36+
sync::{Arc, OnceLock},
3737
};
3838

3939
#[cfg(feature = "async-std")]
@@ -42,7 +42,6 @@ use async_fs as fs;
4242
use async_lock::{Mutex, RwLock};
4343
#[cfg(feature = "async-std")]
4444
use futures_lite::AsyncReadExt;
45-
use once_cell::sync::OnceCell;
4645
#[cfg(feature = "tokio")]
4746
use tokio::{
4847
fs, io,
@@ -82,7 +81,7 @@ pub struct Keyring {
8281
/// Times are stored before reading the file to detect
8382
/// file changes before writing
8483
mtime: Mutex<Option<std::time::SystemTime>>,
85-
key: OnceCell<Key>,
84+
key: OnceLock<Key>,
8685
secret: Arc<Secret>,
8786
}
8887

0 commit comments

Comments
 (0)