From 5307403ff0edc77a4e37ab5c6605d8b6a6da2fa5 Mon Sep 17 00:00:00 2001 From: Erik Zscheile Date: Wed, 10 Jun 2020 21:58:19 +0200 Subject: [PATCH] fix documentation and misc stuff - ran `cargo clippy`, apply all fixes - ran `cargo fmt` HandshakeState: - fix documentation of `write_message` - move updating of `self.my_turn` from {_(*) -> \1}_message --- src/error.rs | 8 +------- src/handshakestate.rs | 17 ++++++----------- src/resolvers/default.rs | 8 ++------ src/types.rs | 2 -- 4 files changed, 9 insertions(+), 26 deletions(-) diff --git a/src/error.rs b/src/error.rs index 59860e8d..fbecbf58 100644 --- a/src/error.rs +++ b/src/error.rs @@ -5,6 +5,7 @@ use std::fmt; /// All errors in snow will include an `ErrorKind`. #[allow(missing_docs)] #[derive(Debug)] +#[non_exhaustive] pub enum Error { /// The noise pattern failed to parse. Pattern(PatternProblem), @@ -30,12 +31,6 @@ pub enum Error { /// Key-encapsulation failed #[cfg(feature = "hfs")] Kem, - - /// This enum may grow additional variants, so this makes sure clients - /// don't count on exhaustive matching. (Otherwise, adding a new variant - /// could break existing code.) - #[doc(hidden)] - __Nonexhaustive, } /// The various stages of initialization used to help identify @@ -130,7 +125,6 @@ impl fmt::Display for Error { Error::Decrypt => write!(f, "decrypt error"), #[cfg(feature = "hfs")] Error::Kem => write!(f, "kem error"), - Error::__Nonexhaustive => write!(f, "Nonexhaustive"), } } } diff --git a/src/handshakestate.rs b/src/handshakestate.rs index 98cbfbaf..0fb78db7 100644 --- a/src/handshakestate.rs +++ b/src/handshakestate.rs @@ -195,7 +195,7 @@ impl HandshakeState { } /// Construct a message from `payload` (and pending handshake tokens if in handshake state), - /// and writes it to the `output` buffer. + /// and writes it to the `message` buffer. /// /// Returns the size of the written payload. /// @@ -203,12 +203,12 @@ impl HandshakeState { /// /// Will result in `Error::Input` if the size of the output exceeds the max message /// length in the Noise Protocol (65535 bytes). - #[must_use] - pub fn write_message(&mut self, message: &[u8], payload: &mut [u8]) -> Result { + pub fn write_message(&mut self, payload: &[u8], message: &mut [u8]) -> Result { let checkpoint = self.symmetricstate.checkpoint(); - match self._write_message(message, payload) { + match self._write_message(payload, message) { Ok(res) => { self.pattern_position += 1; + self.my_turn = false; Ok(res) }, Err(err) => { @@ -317,7 +317,6 @@ impl HandshakeState { if self.pattern_position == (self.message_patterns.len() - 1) { self.symmetricstate.split(&mut self.cipherstates.0, &mut self.cipherstates.1); } - self.my_turn = false; Ok(byte_index) } @@ -338,6 +337,7 @@ impl HandshakeState { match self._read_message(message, payload) { Ok(res) => { self.pattern_position += 1; + self.my_turn = true; Ok(res) }, Err(err) => { @@ -447,7 +447,6 @@ impl HandshakeState { } self.symmetricstate.decrypt_and_mix_hash(ptr, payload).map_err(|_| Error::Decrypt)?; - self.my_turn = true; if last { self.symmetricstate.split(&mut self.cipherstates.0, &mut self.cipherstates.1); } @@ -463,7 +462,6 @@ impl HandshakeState { /// # Errors /// /// Will result in `Error::Input` if the PSK is not the right length or the location is out of bounds. - #[must_use] pub fn set_psk(&mut self, location: usize, key: &[u8]) -> Result<(), Error> { if key.len() != PSKLEN || self.psks.len() <= location { bail!(Error::Input); @@ -516,10 +514,7 @@ impl HandshakeState { pub fn dangerously_get_raw_split(&mut self) -> ([u8; CIPHERKEYLEN], [u8; CIPHERKEYLEN]) { let mut output = ([0u8; MAXHASHLEN], [0u8; MAXHASHLEN]); self.symmetricstate.split_raw(&mut output.0, &mut output.1); - ( - output.0[..CIPHERKEYLEN].try_into().unwrap(), - output.1[..CIPHERKEYLEN].try_into().unwrap() - ) + (output.0[..CIPHERKEYLEN].try_into().unwrap(), output.1[..CIPHERKEYLEN].try_into().unwrap()) } /// Convert this `HandshakeState` into a `TransportState` with an internally stored nonce. diff --git a/src/resolvers/default.rs b/src/resolvers/default.rs index b431f4ef..5c363591 100644 --- a/src/resolvers/default.rs +++ b/src/resolvers/default.rs @@ -1,4 +1,3 @@ -use aes_gcm; use blake2::{Blake2b, Blake2s}; #[cfg(feature = "xchachapoly")] use chacha20poly1305::XChaCha20Poly1305; @@ -211,7 +210,7 @@ impl Cipher for CipherAesGcm { &mut out[..message_len], ciphertext[message_len..].into(), ) - .and_then(|_| Ok(message_len)) + .map(|_| message_len) .map_err(|_| ()) } } @@ -524,12 +523,9 @@ impl Kem for Kyber1024 { #[cfg(test)] mod tests { - - use hex; - use self::hex::FromHex; use super::*; - use crate::types::*; + use hex; #[test] fn test_sha256() { diff --git a/src/types.rs b/src/types.rs index e986b27a..1725a8b9 100644 --- a/src/types.rs +++ b/src/types.rs @@ -30,7 +30,6 @@ pub trait Dh: Send + Sync { fn privkey(&self) -> &[u8]; /// Calculate a Diffie-Hellman exchange. - #[must_use] fn dh(&self, pubkey: &[u8], out: &mut [u8]) -> Result<(), ()>; } @@ -45,7 +44,6 @@ pub trait Cipher: Send + Sync { /// Encrypt (with associated data) a given plaintext. fn encrypt(&self, nonce: u64, authtext: &[u8], plaintext: &[u8], out: &mut [u8]) -> usize; - #[must_use] /// Decrypt (with associated data) a given ciphertext. fn decrypt( &self,