mirror of
https://github.com/zerotier/zssp.git
synced 2026-05-22 16:28:40 -07:00
cargo clippy
This commit is contained in:
@@ -26,7 +26,7 @@ pub fn respond_to_challenge_in_place(
|
||||
challenge: &[u8; CHALLENGE_SIZE],
|
||||
pre_response: &mut [u8; CHALLENGE_SIZE],
|
||||
) {
|
||||
if &challenge[POW_START..] == &pre_response[POW_START..] {
|
||||
if challenge[POW_START..] == pre_response[POW_START..] {
|
||||
pre_response.copy_from_slice(challenge);
|
||||
let mut pow = rng.next_u64();
|
||||
let mut work_buf = [0u8; SHA512_HASH_SIZE];
|
||||
@@ -79,7 +79,6 @@ impl ChallengeContext {
|
||||
hasher.write(&c.to_be_bytes());
|
||||
addr.hash(&mut hasher);
|
||||
hasher.write(&self.salt);
|
||||
drop(hasher);
|
||||
|
||||
let mut mac = [0u8; SHA512_HASH_SIZE];
|
||||
hash.finish_and_reset(&mut mac);
|
||||
|
||||
@@ -31,7 +31,11 @@ pub struct IndexedBinaryHeap<T, P> {
|
||||
data: Vec<(T, P, usize)>,
|
||||
map: Vec<(usize, u64)>,
|
||||
}
|
||||
|
||||
impl<T, P: Ord> Default for IndexedBinaryHeap<T, P> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
impl<T, P: Ord> IndexedBinaryHeap<T, P> {
|
||||
/// Create a new, empty binary heap.
|
||||
pub fn new() -> Self {
|
||||
|
||||
@@ -432,7 +432,7 @@ pub(crate) fn received_x1_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
return Err(fault!(InvalidPacket, true));
|
||||
}
|
||||
|
||||
if &n[AES_GCM_NONCE_SIZE - 8..] != &x1[x1.len() - 8..] {
|
||||
if n[AES_GCM_NONCE_SIZE - 8..] != x1[x1.len() - 8..] {
|
||||
return Err(fault!(FailedAuth, true));
|
||||
}
|
||||
let hmac = &mut C::Hmac::new();
|
||||
@@ -591,7 +591,7 @@ pub(crate) fn received_x2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
return Err(fault!(UnknownLocalKeyId, true, session));
|
||||
}
|
||||
let (_, c) = from_nonce(n);
|
||||
if c >= COUNTER_WINDOW_MAX_SKIP_AHEAD || &n[AES_GCM_NONCE_SIZE - 3..] != &x2[x2.len() - 3..] {
|
||||
if c >= COUNTER_WINDOW_MAX_SKIP_AHEAD || n[AES_GCM_NONCE_SIZE - 3..] != x2[x2.len() - 3..] {
|
||||
return Err(fault!(FailedAuth, true, session));
|
||||
}
|
||||
|
||||
@@ -690,7 +690,7 @@ pub(crate) fn received_x2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
let tag = noise.encrypt_and_hash_in_place(hash, to_nonce(PACKET_TYPE_HANDSHAKE_COMPLETION, 0), &mut x3[i..]);
|
||||
x3.extend(tag);
|
||||
|
||||
let new_ratchet_state = create_ratchet_state(hmac, &mut noise, chain_len);
|
||||
let new_ratchet_state = create_ratchet_state(hmac, &noise, chain_len);
|
||||
|
||||
let (ratchet_to_preserve, ratchet_to_delete) = if ratchet_i == 1 {
|
||||
(Some(&state.ratchet_state1), state.ratchet_state2.as_ref())
|
||||
@@ -859,7 +859,7 @@ pub(crate) fn received_x3_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
let RatchetStates { state1, state2 } = rss.unwrap_or_default();
|
||||
let mut should_warn_missing_ratchet = false;
|
||||
|
||||
if (&zeta.ratchet_state != &state1) & (Some(&zeta.ratchet_state) != state2.as_ref()) {
|
||||
if (zeta.ratchet_state != state1) & (Some(&zeta.ratchet_state) != state2.as_ref()) {
|
||||
if !responder_disallows_downgrade && zeta.ratchet_state.fingerprint().is_none() {
|
||||
should_warn_missing_ratchet = true;
|
||||
} else {
|
||||
@@ -873,7 +873,7 @@ pub(crate) fn received_x3_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
let mut noise_kk_ss = Zeroizing::new([0u8; P384_ECDH_SHARED_SECRET_SIZE]);
|
||||
ctx.s_secret.agree(&s_remote, &mut noise_kk_ss);
|
||||
|
||||
let new_ratchet_state = create_ratchet_state(hmac, &mut noise, zeta.ratchet_state.chain_len);
|
||||
let new_ratchet_state = create_ratchet_state(hmac, &noise, zeta.ratchet_state.chain_len);
|
||||
let mut nk_recv = Zeroizing::new([0u8; HASHLEN]);
|
||||
let mut nk_send = Zeroizing::new([0u8; HASHLEN]);
|
||||
noise.split(hmac, &mut nk_send, &mut nk_recv);
|
||||
|
||||
@@ -199,7 +199,7 @@ impl<C: CryptoLayer> Context<C> {
|
||||
/// * `remote_address` - Whatever the remote address is, as long as you can Hash it
|
||||
/// * `incoming_fragment_buf` - Buffer containing incoming wire packet (the context takes ownership)
|
||||
/// * `output_buffer` - Buffer to receive decrypted and authenticated object data
|
||||
pub fn receive<'a, App: ApplicationLayer<C>>(
|
||||
pub fn receive<App: ApplicationLayer<C>>(
|
||||
&self,
|
||||
mut app: App,
|
||||
mut send_unassociated_reply: impl Sender,
|
||||
|
||||
@@ -201,7 +201,7 @@ impl<C: CryptoLayer> Context<C> {
|
||||
let (p, _) = from_nonce(&pn);
|
||||
let ret = match p {
|
||||
PACKET_TYPE_DATA => {
|
||||
received_payload_in_place::<C, App>(
|
||||
received_payload_in_place::<C>(
|
||||
&mut zeta,
|
||||
kid_recv,
|
||||
to_aes_nonce(&pn),
|
||||
@@ -291,7 +291,7 @@ impl<C: CryptoLayer> Context<C> {
|
||||
}
|
||||
PACKET_TYPE_SESSION_REJECTED => {
|
||||
log!(app, ReceivedRawD);
|
||||
received_d_trans::<C, App>(&mut zeta, kid_recv, to_aes_nonce(&pn), assembled_packet)?;
|
||||
received_d_trans::<C>(&mut zeta, kid_recv, to_aes_nonce(&pn), assembled_packet)?;
|
||||
log!(app, DIsAuthClosedSession(&session));
|
||||
SessionEvent::Rejected
|
||||
}
|
||||
@@ -432,7 +432,7 @@ impl<C: CryptoLayer> Context<C> {
|
||||
{
|
||||
if let Some(Some(session)) = ctx.session_map.borrow_mut().get(&kid_recv).map(|r| r.upgrade()) {
|
||||
let mut zeta = session.0.borrow_mut();
|
||||
respond_to_challenge::<C, App>(
|
||||
respond_to_challenge::<C>(
|
||||
&mut zeta,
|
||||
&ctx.rng,
|
||||
&assembled_packet[KID_SIZE..].try_into().unwrap(),
|
||||
|
||||
@@ -29,9 +29,9 @@ impl AesGcmAead for AesGcmCrate {
|
||||
buffer: &mut [u8],
|
||||
) -> [u8; AES_GCM_TAG_SIZE] {
|
||||
let key = Key::<Aes256Gcm>::from_slice(key);
|
||||
let mut cipher = Aes256Gcm::new(&key);
|
||||
let mut cipher = Aes256Gcm::new(key);
|
||||
cipher
|
||||
.encrypt_in_place_detached(&Nonce::from_slice(iv), aad.unwrap_or(&[]), buffer)
|
||||
.encrypt_in_place_detached(Nonce::from_slice(iv), aad.unwrap_or(&[]), buffer)
|
||||
.unwrap()
|
||||
.try_into()
|
||||
.unwrap()
|
||||
@@ -45,13 +45,13 @@ impl AesGcmAead for AesGcmCrate {
|
||||
tag: &[u8; AES_GCM_TAG_SIZE],
|
||||
) -> bool {
|
||||
let key = Key::<Aes256Gcm>::from_slice(key);
|
||||
let mut cipher = Aes256Gcm::new(&key);
|
||||
let mut cipher = Aes256Gcm::new(key);
|
||||
cipher
|
||||
.decrypt_in_place_detached(
|
||||
&Nonce::from_slice(iv),
|
||||
Nonce::from_slice(iv),
|
||||
aad.unwrap_or(&[]),
|
||||
buffer,
|
||||
&Tag::from_slice(tag),
|
||||
Tag::from_slice(tag),
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
+18
-18
@@ -295,7 +295,7 @@ pub(crate) fn trans_to_a1<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
) -> Result<Arc<Session<C>>, OpenError> {
|
||||
let ratchet_states = app
|
||||
.restore_by_identity(&s_remote, &session_data)
|
||||
.map_err(|e| OpenError::StorageError(e))?;
|
||||
.map_err(OpenError::StorageError)?;
|
||||
let RatchetStates { state1, state2 } = ratchet_states.unwrap_or_default();
|
||||
|
||||
let mut session_map = ctx.session_map.borrow_mut();
|
||||
@@ -338,7 +338,7 @@ pub(crate) fn trans_to_a1<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
Ok(session)
|
||||
}
|
||||
/// Corresponds to Algorithm 13 found in Section 5.
|
||||
pub(crate) fn respond_to_challenge<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
pub(crate) fn respond_to_challenge<C: CryptoLayer>(
|
||||
zeta: &mut Zeta<C>,
|
||||
rng: &RefCell<C::Rng>,
|
||||
challenge: &[u8; CHALLENGE_SIZE],
|
||||
@@ -368,7 +368,7 @@ pub(crate) fn received_x1_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
if !(HANDSHAKE_HELLO_MIN_SIZE..=HANDSHAKE_HELLO_MAX_SIZE).contains(&x1.len()) {
|
||||
return Err(byzantine_fault!(InvalidPacket, true));
|
||||
}
|
||||
if &n[AES_GCM_NONCE_SIZE - 8..] != &x1[x1.len() - 8..] {
|
||||
if n[AES_GCM_NONCE_SIZE - 8..] != x1[x1.len() - 8..] {
|
||||
return Err(byzantine_fault!(FailedAuth, true));
|
||||
}
|
||||
let mut noise = SymmetricState::<C>::initialize(PROTOCOL_NAME_NOISE_XK);
|
||||
@@ -499,7 +499,7 @@ pub(crate) fn received_x2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
return Err(byzantine_fault!(UnknownLocalKeyId, true));
|
||||
}
|
||||
let (_, c) = from_nonce(&n);
|
||||
if c >= COUNTER_WINDOW_MAX_SKIP_AHEAD || &n[AES_GCM_NONCE_SIZE - 3..] != &x2[x2.len() - 3..] {
|
||||
if c >= COUNTER_WINDOW_MAX_SKIP_AHEAD || n[AES_GCM_NONCE_SIZE - 3..] != x2[x2.len() - 3..] {
|
||||
return Err(byzantine_fault!(FailedAuth, true));
|
||||
}
|
||||
let mut should_warn_missing_ratchet = false;
|
||||
@@ -538,7 +538,7 @@ pub(crate) fn received_x2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
// Check for which ratchet key Bob wants to use.
|
||||
let test_ratchet_key = |ratchet_key| -> Option<(NonZeroU32, SymmetricState<C>)> {
|
||||
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(ratchet_key);
|
||||
// Process message pattern 2 payload.
|
||||
@@ -639,7 +639,7 @@ pub(crate) fn received_x2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
}
|
||||
result.map(|_| should_warn_missing_ratchet)
|
||||
}
|
||||
fn send_control<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
fn send_control<C: CryptoLayer>(
|
||||
zeta: &mut Zeta<C>,
|
||||
packet_type: u8,
|
||||
mut payload: Vec<u8>,
|
||||
@@ -726,7 +726,7 @@ pub(crate) fn received_x3_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
let RatchetStates { state1, state2 } = rss.unwrap_or_default();
|
||||
let mut should_warn_missing_ratchet = false;
|
||||
|
||||
if (&zeta.ratchet_state != &state1) & (Some(&zeta.ratchet_state) != state2.as_ref()) {
|
||||
if (zeta.ratchet_state != state1) & (Some(&zeta.ratchet_state) != state2.as_ref()) {
|
||||
if !responder_disallows_downgrade && zeta.ratchet_state.fingerprint().is_none() {
|
||||
should_warn_missing_ratchet = true;
|
||||
} else {
|
||||
@@ -879,7 +879,7 @@ pub(crate) fn received_c1_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
}
|
||||
}
|
||||
let c2 = Vec::new();
|
||||
if !send_control::<C, App>(zeta, PACKET_TYPE_ACK, c2, send) {
|
||||
if !send_control::<C>(zeta, PACKET_TYPE_ACK, c2, send) {
|
||||
return Err(byzantine_fault!(OutOfSequence, true));
|
||||
}
|
||||
|
||||
@@ -926,7 +926,7 @@ pub(crate) fn received_c2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
}
|
||||
/// Corresponds to the trivial Transition Algorithm described for processing D packets found in
|
||||
/// Section 4.3.
|
||||
pub(crate) fn received_d_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
pub(crate) fn received_d_trans<C: CryptoLayer>(
|
||||
zeta: &mut Zeta<C>,
|
||||
kid: NonZeroU32,
|
||||
n: [u8; AES_GCM_NONCE_SIZE],
|
||||
@@ -993,7 +993,7 @@ pub(crate) fn service<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
}
|
||||
};
|
||||
|
||||
send_control::<C, App>(zeta, p, control_payload, send);
|
||||
send_control::<C>(zeta, p, control_payload, send);
|
||||
}
|
||||
}
|
||||
/// Corresponds to the timeout timer Transition Algorithm described in Section 4.1 - Definition 3.
|
||||
@@ -1013,7 +1013,7 @@ fn timeout_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
} else {
|
||||
log!(app, TimeoutX3(session));
|
||||
}
|
||||
let new_kid_recv = remap(session, &zeta, &ctx.rng, &ctx.session_map);
|
||||
let new_kid_recv = remap(session, zeta, &ctx.rng, &ctx.session_map);
|
||||
|
||||
let a1 = create_a1_state::<C, App>(
|
||||
&ctx.rng,
|
||||
@@ -1039,7 +1039,7 @@ fn timeout_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
ZetaAutomata::S2 => {
|
||||
// Corresponds to Transition Algorithm 6 found in Section 4.3.
|
||||
log!(app, StartedRekeyingSentK1(session));
|
||||
let new_kid_recv = remap(session, &zeta, &ctx.rng, &ctx.session_map);
|
||||
let new_kid_recv = remap(session, zeta, &ctx.rng, &ctx.session_map);
|
||||
// -> s
|
||||
// <- s
|
||||
// ...
|
||||
@@ -1067,7 +1067,7 @@ fn timeout_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
zeta.resend_timer = current_time + C::SETTINGS.resend_time as i64;
|
||||
zeta.beta = ZetaAutomata::R1 { noise, e_secret, k1: k1.clone() };
|
||||
|
||||
send_control::<C, App>(zeta, PACKET_TYPE_REKEY_INIT, k1, send);
|
||||
send_control::<C>(zeta, PACKET_TYPE_REKEY_INIT, k1, send);
|
||||
}
|
||||
ZetaAutomata::S1 { .. } => {
|
||||
log!(app, TimeoutKeyConfirm(session));
|
||||
@@ -1166,10 +1166,10 @@ pub(crate) fn received_k1_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
// Process message pattern 2 ee token.
|
||||
noise.mix_dh(&e_secret, &e_remote);
|
||||
// Process message pattern 2 se token.
|
||||
noise.mix_dh(&s_secret, &e_remote);
|
||||
noise.mix_dh(s_secret, &e_remote);
|
||||
// Process message pattern 2 payload.
|
||||
let i = k2.len();
|
||||
let new_kid_recv = remap(session, &zeta, rng, session_map);
|
||||
let new_kid_recv = remap(session, zeta, rng, session_map);
|
||||
k2.extend(&new_kid_recv.get().to_be_bytes());
|
||||
noise.encrypt_and_hash_in_place(to_nonce(PACKET_TYPE_REKEY_COMPLETE, 0), i, &mut k2);
|
||||
|
||||
@@ -1206,7 +1206,7 @@ pub(crate) fn received_k1_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
zeta.resend_timer = current_time + C::SETTINGS.resend_time as i64;
|
||||
zeta.beta = ZetaAutomata::R2 { k2: k2.clone() };
|
||||
|
||||
send_control::<C, App>(zeta, PACKET_TYPE_REKEY_COMPLETE, k2, send);
|
||||
send_control::<C>(zeta, PACKET_TYPE_REKEY_COMPLETE, k2, send);
|
||||
Ok(())
|
||||
})();
|
||||
if matches!(result, Err(ReceiveError::ByzantineFault { .. })) {
|
||||
@@ -1306,7 +1306,7 @@ pub(crate) fn received_k2_trans<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
zeta.beta = ZetaAutomata::S1;
|
||||
|
||||
let c1 = Vec::new();
|
||||
send_control::<C, App>(zeta, PACKET_TYPE_KEY_CONFIRM, c1, send);
|
||||
send_control::<C>(zeta, PACKET_TYPE_KEY_CONFIRM, c1, send);
|
||||
Ok(())
|
||||
} else {
|
||||
unreachable!()
|
||||
@@ -1354,7 +1354,7 @@ pub(crate) fn send_payload<C: CryptoLayer>(
|
||||
}
|
||||
}
|
||||
/// Corresponds to Algorithm 10 found in Section 4.3.
|
||||
pub(crate) fn received_payload_in_place<C: CryptoLayer, App: ApplicationLayer<C>>(
|
||||
pub(crate) fn received_payload_in_place<C: CryptoLayer>(
|
||||
zeta: &mut Zeta<C>,
|
||||
kid: NonZeroU32,
|
||||
n: [u8; AES_GCM_NONCE_SIZE],
|
||||
|
||||
Reference in New Issue
Block a user