Skip to content

Commit

Permalink
Fix some missing docs and Clippy lints (#102)
Browse files Browse the repository at this point in the history
* Resolve Clippy findings

* Add missing docs on public API
  • Loading branch information
BlackHoleFox authored Dec 12, 2020
1 parent ec32cfa commit 7ccfaf4
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 51 deletions.
12 changes: 2 additions & 10 deletions benches/benches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,6 @@ use snow::{params::*, *};

const MSG_SIZE: usize = 4096;

pub fn copy_memory(data: &[u8], out: &mut [u8]) -> usize {
for count in 0..data.len() {
out[count] = data[count];
}

data.len()
}

fn benchmarks(c: &mut Criterion) {
c.bench(
"builder",
Expand Down Expand Up @@ -92,7 +84,7 @@ fn benchmarks(c: &mut Criterion) {
c.bench(
"transport",
Benchmark::new("AESGCM_SHA256 throughput", |b| {
static PATTERN: &'static str = "Noise_NN_25519_AESGCM_SHA256";
static PATTERN: &str = "Noise_NN_25519_AESGCM_SHA256";

let mut h_i = Builder::new(PATTERN.parse().unwrap()).build_initiator().unwrap();
let mut h_r = Builder::new(PATTERN.parse().unwrap()).build_responder().unwrap();
Expand Down Expand Up @@ -121,7 +113,7 @@ fn benchmarks(c: &mut Criterion) {
c.bench(
"transport",
Benchmark::new("ChaChaPoly_BLAKE2s throughput", |b| {
static PATTERN: &'static str = "Noise_NN_25519_ChaChaPoly_BLAKE2s";
static PATTERN: &str = "Noise_NN_25519_ChaChaPoly_BLAKE2s";

let mut h_i = Builder::new(PATTERN.parse().unwrap()).build_initiator().unwrap();
let mut h_r = Builder::new(PATTERN.parse().unwrap()).build_responder().unwrap();
Expand Down
2 changes: 1 addition & 1 deletion examples/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
net::{TcpListener, TcpStream},
};

static SECRET: &'static [u8] = b"i don't care for fidget spinners";
static SECRET: &[u8] = b"i don't care for fidget spinners";
lazy_static! {
static ref PARAMS: NoiseParams = "Noise_XXpsk3_25519_ChaChaPoly_BLAKE2s".parse().unwrap();
}
Expand Down
4 changes: 2 additions & 2 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ mod tests {
let params: ::std::result::Result<NoiseParams, _> =
"Noise_NK_25519_ChaChaPoly_BLAH256".parse();

if let Ok(_) = params {
if params.is_ok() {
panic!("NoiseParams should have failed");
}
}
Expand All @@ -292,7 +292,7 @@ mod tests {
.local_private_key(&[0u8; 32])
.build_initiator(); // missing remote key, should result in Err

if let Ok(_) = noise {
if noise.is_ok() {
panic!("builder should have failed on build");
}
}
Expand Down
34 changes: 13 additions & 21 deletions src/params/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,40 +127,30 @@ impl HandshakePattern {
///
/// See: http://noiseprotocol.org/noise.html#one-way-patterns
pub fn is_oneway(self) -> bool {
match self {
N | X | K => true,
_ => false,
}
matches!(self, N | X | K)
}

/// Whether this pattern requires a long-term static key.
pub fn needs_local_static_key(self, initiator: bool) -> bool {
if initiator {
match self {
N | NN | NK | NX | NK1 | NX1 => false,
_ => true,
}
!matches!(self, N | NN | NK | NX | NK1 | NX1)
} else {
match self {
NN | XN | KN | IN | X1N | K1N | I1N => false,
_ => true,
}
!matches!(self, NN | XN | KN | IN | X1N | K1N | I1N)
}
}

/// Whether this pattern demands a remote public key pre-message.
pub fn need_known_remote_pubkey(self, initiator: bool) -> bool {
if initiator {
match self {
N | K | X | NK | XK | KK | IK | NK1 | X1K | XK1 | X1K1 | K1K | KK1 | K1K1 | I1K
| IK1 | I1K1 => true,
_ => false,
}
matches!(
self,
N | K | X | NK | XK | KK | IK | NK1 | X1K | XK1 | X1K1 | K1K | KK1 | K1K1 | I1K | IK1 | I1K1
)
} else {
match self {
K | KN | KK | KX | K1N | K1K | KK1 | K1K1 | K1X | KX1 | K1X1 => true,
_ => false,
}
matches!(
self,
K | KN | KK | KX | K1N | K1K | KK1 | K1K1 | K1X | KX1 | K1X1
)
}
}
}
Expand Down Expand Up @@ -195,8 +185,10 @@ impl FromStr for HandshakeModifier {
}
}

/// Handshake modifiers that will be used during key exchange handshake.
#[derive(Clone, PartialEq, Debug)]
pub struct HandshakeModifierList {
/// List of parsed modifiers.
pub list: Vec<HandshakeModifier>,
}

Expand Down
9 changes: 4 additions & 5 deletions src/resolvers/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -523,15 +523,14 @@ impl Kem for Kyber1024 {

#[cfg(test)]
mod tests {
use self::hex::FromHex;
use hex::FromHex;
use super::*;
use hex;

#[test]
fn test_sha256() {
let mut output = [0u8; 32];
let mut hasher: HashSHA256 = Default::default();
hasher.input("abc".as_bytes());
hasher.input(b"abc");
hasher.result(&mut output);
assert!(
hex::encode(output)
Expand Down Expand Up @@ -568,7 +567,7 @@ mod tests {
// BLAKE2b test - draft-saarinen-blake2-06
let mut output = [0u8; 64];
let mut hasher: HashBLAKE2b = Default::default();
hasher.input("abc".as_bytes());
hasher.input(b"abc");
hasher.result(&mut output);
assert!(
hex::encode(output.to_vec())
Expand All @@ -584,7 +583,7 @@ mod tests {
// BLAKE2s test - draft-saarinen-blake2-06
let mut output = [0u8; 32];
let mut hasher: HashBLAKE2s = Default::default();
hasher.input("abc".as_bytes());
hasher.input(b"abc");
hasher.result(&mut output);
assert!(
hex::encode(output)
Expand Down
14 changes: 2 additions & 12 deletions tests/general.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#![cfg(any(feature = "default-resolver", feature = "ring-accelerated"))]
#![allow(clippy::needless_range_loop)]
#![allow(non_snake_case)]
use hex;
use rand_core;
use snow;
use x25519_dalek;

use hex::FromHex;
use snow::{
Expand Down Expand Up @@ -34,7 +30,8 @@ impl RngCore for CountingRng {
}

fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand_core::Error> {
Ok(self.fill_bytes(dest))
self.fill_bytes(dest);
Ok(())
}
}

Expand Down Expand Up @@ -85,13 +82,6 @@ impl CryptoResolver for TestResolver {
}
}

pub fn copy_memory(data: &[u8], out: &mut [u8]) -> usize {
for count in 0..data.len() {
out[count] = data[count];
}
data.len()
}

#[test]
fn test_protocol_name() {
let protocol_spec: NoiseParams = "Noise_NK_25519_ChaChaPoly_BLAKE2s".parse().unwrap();
Expand Down

0 comments on commit 7ccfaf4

Please sign in to comment.