mirror of
https://github.com/zerotier/zssp.git
synced 2026-05-22 16:28:40 -07:00
cargo clippy
This commit is contained in:
@@ -323,7 +323,7 @@ pub trait Sender {
|
||||
/// Send the given fragment on this interface and then return whether or not an error occured.
|
||||
///
|
||||
/// If `true` is returned then sending is cancelled and this instance of `Sender` is dropped.
|
||||
fn send_frag<'a>(&'a mut self, frag: &mut [u8]) -> bool;
|
||||
fn send_frag(&mut self, frag: &mut [u8]) -> bool;
|
||||
}
|
||||
|
||||
/// A trait to genericize the process of borrowing the resources necessary to repeatedly
|
||||
@@ -353,7 +353,7 @@ pub trait SendTo<Crypto: CryptoLayer> {
|
||||
}
|
||||
|
||||
impl<F: FnMut(&mut [u8]) -> bool> Sender for F {
|
||||
fn send_frag<'a>(&'a mut self, frag: &mut [u8]) -> bool {
|
||||
fn send_frag(&mut self, frag: &mut [u8]) -> bool {
|
||||
self(frag)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -93,7 +93,7 @@ impl<Crypto: CryptoLayer> UnassociatedFragCache<Crypto> {
|
||||
} else if self.map[idx1].key == key {
|
||||
idx1
|
||||
} else if self.map[idx0].key == 0 || self.map[idx1].key == 0 {
|
||||
if (fragment_count as usize) > self.frags_unused_size {
|
||||
if fragment_count > self.frags_unused_size {
|
||||
// There are not enough free fragment slots so attempt to expire a bunch of entries.
|
||||
let _ = self.check_for_expiry_inner(Crypto::SETTINGS.resend_time as i64, current_time);
|
||||
}
|
||||
@@ -117,7 +117,7 @@ impl<Crypto: CryptoLayer> UnassociatedFragCache<Crypto> {
|
||||
let mut new_expiry = None;
|
||||
if self.map[idx].key == 0 {
|
||||
// This is a new entry so initialize it.
|
||||
if (fragment_count as usize) <= self.frags_unused_size {
|
||||
if fragment_count <= self.frags_unused_size {
|
||||
new_expiry = Some(current_time + Crypto::SETTINGS.fragment_assembly_timeout as i64);
|
||||
let entry = &mut self.map[idx];
|
||||
entry.key = key;
|
||||
@@ -148,7 +148,7 @@ impl<Crypto: CryptoLayer> UnassociatedFragCache<Crypto> {
|
||||
entry.packet_size = new_size;
|
||||
entry.fragment_have |= got;
|
||||
|
||||
let frag_idx = (entry.frags_idx as usize + fragment_no as usize) % self.frags.len();
|
||||
let frag_idx = (entry.frags_idx as usize + fragment_no) % self.frags.len();
|
||||
self.frags[frag_idx].write(fragment);
|
||||
|
||||
if entry.fragment_have == 1u64.wrapping_shl(fragment_count as u32) - 1 {
|
||||
@@ -183,7 +183,7 @@ impl<Crypto: CryptoLayer> UnassociatedFragCache<Crypto> {
|
||||
return expiry;
|
||||
}
|
||||
}
|
||||
return i64::MAX;
|
||||
i64::MAX
|
||||
}
|
||||
|
||||
fn invalidate<const DROP: bool>(&mut self, idx: usize) {
|
||||
|
||||
@@ -53,8 +53,8 @@ impl<Fragment, const MAX_FRAGMENTS: usize> Fragged<Fragment, MAX_FRAGMENTS> {
|
||||
if got & self.have == 0 && self.count == fragment_count as u32 {
|
||||
self.have |= got;
|
||||
unsafe {
|
||||
self.frags.get_unchecked_mut(fragment_no as usize).write(fragment);
|
||||
if self.have == 1u64.wrapping_shl(self.count as u32) - 1 {
|
||||
self.frags.get_unchecked_mut(fragment_no).write(fragment);
|
||||
if self.have == 1u64.wrapping_shl(self.count) - 1 {
|
||||
self.have = 0;
|
||||
self.count = 0;
|
||||
self.nonce = u64::MAX;
|
||||
|
||||
@@ -61,7 +61,7 @@ impl<Application: CryptoLayer> UnassociatedHandshakeCache<Application> {
|
||||
cache.expiries[idx] = expiry;
|
||||
cache.handshakes[idx] = Some(state);
|
||||
self.has_pending.store(true, Ordering::Release);
|
||||
return Some(expiry);
|
||||
Some(expiry)
|
||||
}
|
||||
pub(crate) fn remove(&self, local_id: NonZeroU32) -> bool {
|
||||
let mut cache = self.cache.write().unwrap();
|
||||
|
||||
@@ -94,8 +94,8 @@ pub(crate) const LABEL_RATCHET_STATE: &[u8; 4] = b"ASKR";
|
||||
pub(crate) const LABEL_HEADER_KEY: &[u8; 4] = b"ASKH";
|
||||
pub(crate) const LABEL_KEX_KEY: &[u8; 4] = b"ASKK";
|
||||
|
||||
pub(crate) const EXPIRE_AFTER_USES: u64 = 1 << 32 - 1;
|
||||
pub(crate) const THREAD_SAFE_COUNTER_HARD_EXPIRE: u64 = u64::MAX - 1 << 16;
|
||||
pub(crate) const EXPIRE_AFTER_USES: u64 = (1 << 32) - 1;
|
||||
pub(crate) const THREAD_SAFE_COUNTER_HARD_EXPIRE: u64 = u64::MAX - (1 << 16);
|
||||
/// Determines the number of counters a session will remember. If a counter arrives over
|
||||
/// this amount out of order relative to other received counters, it is likely to be
|
||||
/// rejected on the basis that the session can't remember if this counter was replayed.
|
||||
@@ -137,7 +137,7 @@ pub(crate) const HANDSHAKE_RESPONSE_SIZE: usize =
|
||||
P384_PUBLIC_KEY_SIZE + KYBER_CIPHERTEXT_SIZE + AES_GCM_TAG_SIZE + KID_SIZE + AES_GCM_TAG_SIZE;
|
||||
pub(crate) const HEADERED_HANDSHAKE_RESPONSE_SIZE: usize = HANDSHAKE_RESPONSE_SIZE + HEADER_SIZE;
|
||||
|
||||
pub(crate) const HANDSHAKE_COMPLETION_MIN_SIZE: usize = P384_PUBLIC_KEY_SIZE + AES_GCM_TAG_SIZE + 0 + AES_GCM_TAG_SIZE;
|
||||
pub(crate) const HANDSHAKE_COMPLETION_MIN_SIZE: usize = P384_PUBLIC_KEY_SIZE + AES_GCM_TAG_SIZE + AES_GCM_TAG_SIZE;
|
||||
pub(crate) const HANDSHAKE_COMPLETION_MAX_SIZE: usize = HANDSHAKE_COMPLETION_MIN_SIZE + IDENTITY_MAX_SIZE;
|
||||
|
||||
pub(crate) const HEADERED_HANDSHAKE_COMPLETION_MAX_SIZE: usize = HANDSHAKE_COMPLETION_MAX_SIZE + HEADER_SIZE;
|
||||
|
||||
@@ -19,7 +19,7 @@ impl<Crypto: CryptoLayer> Clone for SymmetricState<Crypto> {
|
||||
Self {
|
||||
k: self.k.clone(),
|
||||
ck: self.ck.clone(),
|
||||
h: self.h.clone(),
|
||||
h: self.h,
|
||||
_app: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
+12
-12
@@ -184,12 +184,12 @@ impl<Crypto: CryptoLayer> SymmetricState<Crypto> {
|
||||
}
|
||||
fn mix_dh(&mut self, hmac: &mut Crypto::Hmac, secret: &Crypto::KeyPair, remote: &Crypto::PublicKey) {
|
||||
let mut ecdh_secret = Zeroizing::new([0u8; P384_ECDH_SHARED_SECRET_SIZE]);
|
||||
secret.agree(&remote, &mut ecdh_secret);
|
||||
secret.agree(remote, &mut ecdh_secret);
|
||||
self.mix_key(hmac, ecdh_secret.as_ref());
|
||||
}
|
||||
fn mix_dh_no_init(&mut self, hmac: &mut Crypto::Hmac, secret: &Crypto::KeyPair, remote: &Crypto::PublicKey) {
|
||||
let mut ecdh_secret = Zeroizing::new([0u8; P384_ECDH_SHARED_SECRET_SIZE]);
|
||||
secret.agree(&remote, &mut ecdh_secret);
|
||||
secret.agree(remote, &mut ecdh_secret);
|
||||
self.mix_key_no_init(hmac, ecdh_secret.as_ref());
|
||||
}
|
||||
}
|
||||
@@ -262,7 +262,7 @@ fn remap<Crypto: CryptoLayer>(
|
||||
let weak = if let Some(Some(weak)) = state.key_ref(true).recv.kid.as_ref().map(|kid| session_map.remove(kid)) {
|
||||
weak
|
||||
} else {
|
||||
Arc::downgrade(&session)
|
||||
Arc::downgrade(session)
|
||||
};
|
||||
let new_kid_recv = gen_kid(session_map.deref(), ctx.rng.lock().unwrap().deref_mut());
|
||||
session_map.insert(new_kid_recv, weak);
|
||||
@@ -332,7 +332,7 @@ pub(crate) fn trans_to_a1<Crypto: CryptoLayer, App: ApplicationLayer<Crypto>>(
|
||||
) -> Result<(Arc<Session<Crypto>>, Option<i64>), OpenError> {
|
||||
let RatchetStates { state1, state2 } = app
|
||||
.restore_by_identity(&s_remote, &session_data)
|
||||
.map_err(|e| OpenError::StorageError(e))?
|
||||
.map_err(OpenError::StorageError)?
|
||||
.unwrap_or_default();
|
||||
|
||||
let mut session_queue = ctx.session_queue.lock().unwrap();
|
||||
@@ -450,7 +450,7 @@ pub(crate) fn received_x1_trans<Crypto: CryptoLayer, App: ApplicationLayer<Crypt
|
||||
i = j;
|
||||
// Process message pattern 1 e token.
|
||||
let e_remote = noise
|
||||
.read_e_no_init(hash, hmac, &mut i, &x1)
|
||||
.read_e_no_init(hash, hmac, &mut i, x1)
|
||||
.ok_or_else(|| fault!(FailedAuth, true))?;
|
||||
// Process message pattern 1 es token.
|
||||
noise.mix_dh(hmac, &ctx.s_secret, &e_remote);
|
||||
@@ -609,7 +609,7 @@ pub(crate) fn received_x2_trans<Crypto: CryptoLayer, App: ApplicationLayer<Crypt
|
||||
let mut i = 0;
|
||||
// Process message pattern 2 e token.
|
||||
let e_remote = noise
|
||||
.read_e_no_init(hash, hmac, &mut i, &x2)
|
||||
.read_e_no_init(hash, hmac, &mut i, x2)
|
||||
.ok_or_else(|| fault!(FailedAuth, true, session))?;
|
||||
// Process message pattern 2 ee token.
|
||||
noise.mix_dh(hmac, &a1.e_secret, &e_remote);
|
||||
@@ -643,7 +643,7 @@ pub(crate) fn received_x2_trans<Crypto: CryptoLayer, App: ApplicationLayer<Crypt
|
||||
// Check for which ratchet key Bob wants to use.
|
||||
let mut test_ratchet_key = |ratchet_key| -> Option<(NonZeroU32, SymmetricState<Crypto>)> {
|
||||
let mut noise = noise.clone();
|
||||
let mut payload = payload.clone();
|
||||
let mut payload = payload;
|
||||
// Process message pattern 2 psk token.
|
||||
noise.mix_key_and_hash(hash, hmac, ratchet_key);
|
||||
// Process message pattern 2 payload.
|
||||
@@ -774,7 +774,7 @@ fn send_control<Crypto: CryptoLayer, const CAP: usize>(
|
||||
mut payload: ArrayVec<u8, CAP>,
|
||||
send: impl FnOnce(&mut [u8], Option<&Crypto::PrpEnc>),
|
||||
) -> Result<(), bool> {
|
||||
if let Some((c, _)) = get_counter(session, &state) {
|
||||
if let Some((c, _)) = get_counter(session, state) {
|
||||
if let (Some(kek), Some(kid)) = (state.key_ref(false).send.kek.as_ref(), state.key_ref(false).send.kid) {
|
||||
let nonce = to_nonce(packet_type, c);
|
||||
let tag = Crypto::Aead::encrypt_in_place(kek, &nonce, &[], &mut payload[HEADER_SIZE..]);
|
||||
@@ -1386,7 +1386,7 @@ pub(crate) fn received_k1_trans<Crypto: CryptoLayer, App: ApplicationLayer<Crypt
|
||||
noise.mix_key_and_hash_no_init(hash, hmac, state.ratchet_state1.key.as_ref());
|
||||
// Process message pattern 1 e token.
|
||||
let e_remote = noise
|
||||
.read_e_no_init(hash, hmac, &mut i, &k1)
|
||||
.read_e_no_init(hash, hmac, &mut i, k1)
|
||||
.ok_or_else(|| fault!(FailedAuth, true, session, true))?;
|
||||
// Process message pattern 1 es token.
|
||||
noise.mix_dh_no_init(hmac, &ctx.s_secret, &e_remote);
|
||||
@@ -1520,7 +1520,7 @@ pub(crate) fn received_k2_trans<Crypto: CryptoLayer, App: ApplicationLayer<Crypt
|
||||
let hmac = &mut Crypto::Hmac::new();
|
||||
// Process message pattern 2 e token.
|
||||
let e_remote = noise
|
||||
.read_e_no_init(hash, hmac, &mut i, &k2)
|
||||
.read_e_no_init(hash, hmac, &mut i, k2)
|
||||
.ok_or_else(|| fault!(FailedAuth, true, session, true))?;
|
||||
// Process message pattern 2 ee token.
|
||||
noise.mix_dh_no_init(hmac, e_secret, &e_remote);
|
||||
@@ -1584,14 +1584,14 @@ pub(crate) fn received_k2_trans<Crypto: CryptoLayer, App: ApplicationLayer<Crypt
|
||||
|
||||
let mut c1 = ArrayVec::<u8, HEADERED_KEY_CONFIRMATION_SIZE>::new();
|
||||
c1.extend([0u8; HEADER_SIZE]);
|
||||
match send_control(&session, &state, PACKET_TYPE_KEY_CONFIRM, c1, send) {
|
||||
match send_control(session, &state, PACKET_TYPE_KEY_CONFIRM, c1, send) {
|
||||
Ok(()) => Ok(reduced_service_time),
|
||||
Err(false) => Err(fault!(OutOfSequence, true, session)),
|
||||
Err(true) => Err(fault!(ExpiredCounter, true, session, true)),
|
||||
}
|
||||
} else {
|
||||
// Some rekey packet may have arrived extremely delayed.
|
||||
return Err(fault!(OutOfSequence, false, session));
|
||||
Err(fault!(OutOfSequence, false, session))
|
||||
}
|
||||
})();
|
||||
|
||||
|
||||
@@ -483,7 +483,7 @@ impl<Crypto: CryptoLayer> Context<Crypto> {
|
||||
// This can occur naturally because either Bob's incoming_sessions cache got
|
||||
// full so Alice's incoming session was dropped, or the session this packet
|
||||
// was for was dropped by the application.
|
||||
return Err(fault!(UnknownLocalKeyId, false));
|
||||
Err(fault!(UnknownLocalKeyId, false))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -83,7 +83,6 @@ pub(crate) const LABEL_RATCHET_STATE: &[u8; 4] = b"ASKR";
|
||||
pub(crate) const LABEL_HEADER_KEY: &[u8; 4] = b"ASKH";
|
||||
pub(crate) const LABEL_KEX_KEY: &[u8; 4] = b"ASKK";
|
||||
|
||||
//pub(crate) const EXPIRE_AFTER_USES: u64 = (1 << 32) - 1;
|
||||
pub(crate) const HARD_EXPIRATION: u64 = u64::MAX;
|
||||
/// Determines the number of counters a session will remember. If a counter arrives over
|
||||
/// this amount out of order relative to other received counters, it is likely to be
|
||||
|
||||
Reference in New Issue
Block a user