Skip to content

Commit

Permalink
fix documentation and misc stuff (#91)
Browse files Browse the repository at this point in the history
- ran `cargo clippy`, apply all fixes
- ran `cargo fmt`

HandshakeState:
- fix documentation of `write_message`
- move updating of `self.my_turn` from {_(*) -> \1}_message
  • Loading branch information
fogti authored Jun 12, 2020
1 parent 703939a commit e04fc34
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 26 deletions.
8 changes: 1 addition & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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
Expand Down Expand Up @@ -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"),
}
}
}
Expand Down
17 changes: 6 additions & 11 deletions src/handshakestate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,20 +195,20 @@ 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.
///
/// # Errors
///
/// 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<usize, Error> {
pub fn write_message(&mut self, payload: &[u8], message: &mut [u8]) -> Result<usize, Error> {
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) => {
Expand Down Expand Up @@ -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)
}

Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 2 additions & 6 deletions src/resolvers/default.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use aes_gcm;
use blake2::{Blake2b, Blake2s};
#[cfg(feature = "xchachapoly")]
use chacha20poly1305::XChaCha20Poly1305;
Expand Down Expand Up @@ -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(|_| ())
}
}
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 0 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<(), ()>;
}

Expand All @@ -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,
Expand Down

0 comments on commit e04fc34

Please sign in to comment.