Skip to content

Commit

Permalink
clean up clippy warnings
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Whitehead <cywolf@gmail.com>
  • Loading branch information
andrewwhitehead committed Mar 6, 2023
1 parent bda9cca commit dfaa389
Show file tree
Hide file tree
Showing 47 changed files with 446 additions and 490 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ jobs:
- name: Checkout
uses: actions/checkout@v3

- name: Install Rust toolchain
- if: "!matrix.use_cross"
name: Install Rust toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.toolchain || env.RUST_VERSION }}
Expand Down
38 changes: 19 additions & 19 deletions askar-crypto/benches/enc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,96 +21,96 @@ fn criterion_benchmark(c: &mut Criterion) {

let mut message = vec![0u8; MSG_SIZE];
fill_random(&mut message[..]);
let message = &message[..];
let message = message.as_slice();

c.bench_function(&format!("random nonce"), move |b| {
b.iter(|| AesKey::<A128Gcm>::random_nonce())
c.bench_function("random nonce", move |b| {
b.iter(AesKey::<A128Gcm>::random_nonce)
});
c.bench_function(&format!("aes128gcm encrypt"), move |b| {
c.bench_function("aes128gcm encrypt", move |b| {
let key = AesKey::<A128Gcm>::random().unwrap();
let nonce = AesKey::<A128Gcm>::random_nonce();
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});
c.bench_function(&format!("aes256gcm encrypt"), move |b| {
c.bench_function("aes256gcm encrypt", move |b| {
let key = AesKey::<A256Gcm>::random().unwrap();
let nonce = AesKey::<A256Gcm>::random_nonce();
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});

c.bench_function(&format!("aes128cbc-hs256 encrypt"), move |b| {
c.bench_function("aes128cbc-hs256 encrypt", move |b| {
let key = AesKey::<A128CbcHs256>::random().unwrap();
let nonce = AesKey::<A128CbcHs256>::random_nonce();
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});
c.bench_function(&format!("aes256cbc-hs512 encrypt"), move |b| {
c.bench_function("aes256cbc-hs512 encrypt", move |b| {
let key = AesKey::<A256CbcHs512>::random().unwrap();
let nonce = AesKey::<A256CbcHs512>::random_nonce();
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});

c.bench_function(&format!("chacha20-poly1305 encrypt"), move |b| {
c.bench_function("chacha20-poly1305 encrypt", move |b| {
let key = Chacha20Key::<C20P>::random().unwrap();
let nonce = Chacha20Key::<C20P>::random_nonce();
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});
c.bench_function(&format!("xchacha20-poly1305 encrypt"), move |b| {
c.bench_function("xchacha20-poly1305 encrypt", move |b| {
let key = Chacha20Key::<XC20P>::random().unwrap();
let nonce = Chacha20Key::<XC20P>::random_nonce();
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});

// test overhead of SecretBytes
c.bench_function(&format!("chacha20-poly1305 encrypt alloc"), move |b| {
c.bench_function("chacha20-poly1305 encrypt alloc", move |b| {
let key = Chacha20Key::<C20P>::random().unwrap();
let nonce = Chacha20Key::<C20P>::random_nonce();
let mut buffer = SecretBytes::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.buffer_write(black_box(&message[..])).unwrap();
buffer.buffer_write(black_box(message)).unwrap();
key.encrypt_in_place(&mut buffer, &nonce, &[]).unwrap();
})
});

// test overhead of AnyKey
c.bench_function(&format!("chacha20-poly1305 encrypt as any"), move |b| {
c.bench_function("chacha20-poly1305 encrypt as any", move |b| {
let key = Box::<AnyKey>::random(KeyAlg::Chacha20(Chacha20Types::C20P)).unwrap();
let mut nonce = [0u8; 255];
let nonce_len = key.aead_params().nonce_length;
fill_random(&mut nonce[..nonce_len]);
let mut buffer = Vec::with_capacity(ALLOC_SIZE);
b.iter(|| {
buffer.clear();
buffer.extend_from_slice(black_box(&message[..]));
buffer.extend_from_slice(black_box(message));
key.encrypt_in_place(&mut buffer, &nonce[..nonce_len], &[])
.unwrap();
})
Expand Down
2 changes: 1 addition & 1 deletion askar-crypto/benches/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn criterion_benchmark(c: &mut Criterion) {
prv_info: &[],
};

c.bench_function(&format!("concat kdf sha256"), move |b| {
c.bench_function("concat kdf sha256", move |b| {
b.iter(|| {
let mut output = [0u8; 32];
ConcatKDF::<Sha256>::derive_key(black_box(message), black_box(params), &mut output)
Expand Down
8 changes: 4 additions & 4 deletions askar-crypto/src/alg/aes/key_wrap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ where
nonce: &[u8],
aad: &[u8],
) -> Result<usize, Error> {
if nonce.len() != 0 {
if !nonce.is_empty() {
return Err(err_msg!(Unsupported, "Custom nonce not supported"));
}
if aad.len() != 0 {
if !aad.is_empty() {
return Err(err_msg!(Unsupported, "AAD not supported"));
}
let mut buf_len = buffer.as_ref().len();
Expand Down Expand Up @@ -108,10 +108,10 @@ where
nonce: &[u8],
aad: &[u8],
) -> Result<(), Error> {
if nonce.len() != 0 {
if !nonce.is_empty() {
return Err(err_msg!(Unsupported, "Custom nonce not supported"));
}
if aad.len() != 0 {
if !aad.is_empty() {
return Err(err_msg!(Unsupported, "AAD not supported"));
}
if buffer.as_ref().len() % 8 != 0 {
Expand Down
2 changes: 1 addition & 1 deletion askar-crypto/src/alg/aes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ mod key_wrap;
pub use key_wrap::{A128Kw, A256Kw};

/// The 'kty' value of a symmetric key JWK
pub static JWK_KEY_TYPE: &'static str = "oct";
pub static JWK_KEY_TYPE: &str = "oct";

/// Trait implemented by supported AES authenticated encryption algorithms
pub trait AesType: 'static {
Expand Down
62 changes: 25 additions & 37 deletions askar-crypto/src/alg/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,10 @@ fn generate_any<R: AllocKey>(alg: KeyAlg, rng: impl KeyMaterial) -> Result<R, Er
#[cfg(feature = "p256")]
KeyAlg::EcCurve(EcCurves::Secp256r1) => P256KeyPair::generate(rng).map(R::alloc_key),
#[allow(unreachable_patterns)]
_ => {
return Err(err_msg!(
Unsupported,
"Unsupported algorithm for key generation"
))
}
_ => Err(err_msg!(
Unsupported,
"Unsupported algorithm for key generation"
)),
}
}

Expand Down Expand Up @@ -265,12 +263,10 @@ fn from_public_bytes_any<R: AllocKey>(alg: KeyAlg, public: &[u8]) -> Result<R, E
P256KeyPair::from_public_bytes(public).map(R::alloc_key)
}
#[allow(unreachable_patterns)]
_ => {
return Err(err_msg!(
Unsupported,
"Unsupported algorithm for public key import"
))
}
_ => Err(err_msg!(
Unsupported,
"Unsupported algorithm for public key import"
)),
}
}

Expand Down Expand Up @@ -334,12 +330,10 @@ fn from_secret_bytes_any<R: AllocKey>(alg: KeyAlg, secret: &[u8]) -> Result<R, E
P256KeyPair::from_secret_bytes(secret).map(R::alloc_key)
}
#[allow(unreachable_patterns)]
_ => {
return Err(err_msg!(
Unsupported,
"Unsupported algorithm for secret key import"
))
}
_ => Err(err_msg!(
Unsupported,
"Unsupported algorithm for secret key import"
)),
}
}

Expand Down Expand Up @@ -385,12 +379,10 @@ where
Chacha20Key::<XC20P>::from_key_exchange(secret, public).map(R::alloc_key)
}
#[allow(unreachable_patterns)]
_ => {
return Err(err_msg!(
Unsupported,
"Unsupported algorithm for key exchange"
));
}
_ => Err(err_msg!(
Unsupported,
"Unsupported algorithm for key exchange"
)),
}
}

Expand Down Expand Up @@ -447,12 +439,10 @@ fn from_key_derivation_any<R: AllocKey>(
Chacha20Key::<XC20P>::from_key_derivation(derive).map(R::alloc_key)
}
#[allow(unreachable_patterns)]
_ => {
return Err(err_msg!(
Unsupported,
"Unsupported algorithm for key derivation"
));
}
_ => Err(err_msg!(
Unsupported,
"Unsupported algorithm for key derivation"
)),
}
}

Expand Down Expand Up @@ -484,12 +474,10 @@ fn convert_key_any<R: AllocKey>(key: &AnyKey, alg: KeyAlg) -> Result<R, Error> {
)
.map(R::alloc_key)?),
#[allow(unreachable_patterns)]
_ => {
return Err(err_msg!(
Unsupported,
"Unsupported key conversion operation"
))
}
_ => Err(err_msg!(
Unsupported,
"Unsupported key conversion operation"
)),
}
}

Expand Down Expand Up @@ -712,7 +700,7 @@ impl KeyExchange for AnyKey {
#[allow(unreachable_patterns)]
_ => {
let _ = out;
return Err(err_msg!(Unsupported, "Unsupported key exchange"));
Err(err_msg!(Unsupported, "Unsupported key exchange"))
}
}
}
Expand Down
39 changes: 18 additions & 21 deletions askar-crypto/src/alg/bls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::{
};

/// The 'kty' value of a BLS key JWK
pub const JWK_KEY_TYPE: &'static str = "OKP";
pub const JWK_KEY_TYPE: &str = "OKP";

/// A BLS12-381 key pair
#[derive(Clone, Zeroize)]
Expand Down Expand Up @@ -186,24 +186,21 @@ impl<Pk: BlsPublicKeyType> FromJwk for BlsKeyPair<Pk> {
ArrayKey::<Pk::BufferSize>::temp(|pk_arr| {
if jwk.x.decode_base64(pk_arr)? != pk_arr.len() {
Err(err_msg!(InvalidKeyData))
} else if jwk.d.is_some() {
ArrayKey::<U32>::temp(|sk_arr| {
if jwk.d.decode_base64(sk_arr)? != sk_arr.len() {
Err(err_msg!(InvalidKeyData))
} else {
let result = BlsKeyPair::from_secret_key(BlsSecretKey::from_bytes(sk_arr)?);
result.check_public_bytes(pk_arr)?;
Ok(result)
}
})
} else {
if jwk.d.is_some() {
ArrayKey::<U32>::temp(|sk_arr| {
if jwk.d.decode_base64(sk_arr)? != sk_arr.len() {
Err(err_msg!(InvalidKeyData))
} else {
let result =
BlsKeyPair::from_secret_key(BlsSecretKey::from_bytes(sk_arr)?);
result.check_public_bytes(pk_arr)?;
Ok(result)
}
})
} else {
Ok(Self {
secret: None,
public: Pk::from_public_bytes(pk_arr)?,
})
}
Ok(Self {
secret: None,
public: Pk::from_public_bytes(pk_arr)?,
})
}
})
}
Expand Down Expand Up @@ -429,7 +426,7 @@ impl From<&BlsKeyPair<G1G2>> for BlsKeyPair<G1> {
fn from(kp: &BlsKeyPair<G1G2>) -> Self {
BlsKeyPair {
secret: kp.secret.clone(),
public: kp.public.0.clone(),
public: kp.public.0,
}
}
}
Expand All @@ -438,7 +435,7 @@ impl From<&BlsKeyPair<G1G2>> for BlsKeyPair<G2> {
fn from(kp: &BlsKeyPair<G1G2>) -> Self {
BlsKeyPair {
secret: kp.secret.clone(),
public: kp.public.1.clone(),
public: kp.public.1,
}
}
}
Expand Down Expand Up @@ -530,7 +527,7 @@ mod tests {
let kp = BlsKeyPair::<G1>::from_secret_bytes(&test_pvt[..]).expect("Error creating key");

let jwk = kp.to_jwk_public(None).expect("Error converting key to JWK");
let jwk = JwkParts::from_str(&jwk).expect("Error parsing JWK");
let jwk = JwkParts::try_from_str(&jwk).expect("Error parsing JWK");
assert_eq!(jwk.kty, JWK_KEY_TYPE);
assert_eq!(jwk.crv, G1::JWK_CURVE);
assert_eq!(
Expand Down
2 changes: 1 addition & 1 deletion askar-crypto/src/alg/chacha20.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
};

/// The 'kty' value of a symmetric key JWK
pub static JWK_KEY_TYPE: &'static str = "oct";
pub static JWK_KEY_TYPE: &str = "oct";

/// Trait implemented by supported ChaCha20 algorithms
pub trait Chacha20Type: 'static {
Expand Down
Loading

0 comments on commit dfaa389

Please sign in to comment.