Skip to content

Commit

Permalink
clippy fixes
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Huseby <dwh@linuxprogrammer.org>
  • Loading branch information
dhuseby committed Aug 28, 2024
1 parent a513612 commit ea25369
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 173 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "multikey"
version = "1.0.6"
version = "1.0.7"
edition = "2021"
authors = ["Dave Grantham <dwg@linuxprogrammer.org>"]
description = "Multikey self-describing cryptographic key data"
Expand Down
14 changes: 7 additions & 7 deletions src/attrid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub enum AttrId {
impl AttrId {
/// Get the code for the attribute id
pub fn code(&self) -> u8 {
self.clone().into()
(*self).into()
}

/// Convert the attribute id to &str
Expand All @@ -60,9 +60,9 @@ impl AttrId {
}
}

impl Into<u8> for AttrId {
fn into(self) -> u8 {
self as u8
impl From<AttrId> for u8 {
fn from(val: AttrId) -> Self {
val as u8
}
}

Expand All @@ -88,9 +88,9 @@ impl TryFrom<u8> for AttrId {
}
}

impl Into<Vec<u8>> for AttrId {
fn into(self) -> Vec<u8> {
let v: u8 = self.into();
impl From<AttrId> for Vec<u8> {
fn from(val: AttrId) -> Self {
let v: u8 = val.into();
v.encode_into()
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Builder {
/// initialize from a multikey with kdf attributes in it
pub fn try_from_multikey(mut self, mk: &Multikey) -> Result<Self, Error> {
// try to look up the kdf codec in the multikey attributes
if let Some(v) = mk.attributes.get(&(AttrId::KdfCodec.into())) {
if let Some(v) = mk.attributes.get(&AttrId::KdfCodec) {
if let Ok(codec) = Codec::try_from(v.as_slice()) {
self.codec = codec;
}
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Idnetifier: Apache-2.0
//!
//! multikey
#![warn(missing_docs)]
#![deny(
trivial_casts,
Expand Down
17 changes: 9 additions & 8 deletions src/mk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ impl Builder {
let key_bytes = match codec {
Codec::Ed25519Priv => Ed25519Keypair::random(rng).private.to_bytes().to_vec(),
Codec::P256Priv => EcdsaKeypair::random(rng, EcdsaCurve::NistP256)
.map_err(|e| ConversionsError::SshKey(e))?
.map_err(ConversionsError::SshKey)?
.private_key_bytes()
.to_vec(),
Codec::P384Priv => EcdsaKeypair::random(rng, EcdsaCurve::NistP384)
.map_err(|e| ConversionsError::SshKey(e))?
.map_err(ConversionsError::SshKey)?
.private_key_bytes()
.to_vec(),
Codec::P521Priv => EcdsaKeypair::random(rng, EcdsaCurve::NistP521)
.map_err(|e| ConversionsError::SshKey(e))?
.map_err(ConversionsError::SshKey)?
.private_key_bytes()
.to_vec(),
Codec::Secp256K1Priv => k256::SecretKey::random(rng).to_bytes().to_vec(),
Expand Down Expand Up @@ -592,7 +592,7 @@ impl Builder {
..Default::default()
})
}
s => return Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
s => Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
},
Ed25519 => {
let key_bytes = match sshkey.key_data() {
Expand Down Expand Up @@ -780,7 +780,7 @@ impl Builder {
..Default::default()
})
}
s => return Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
s => Err(ConversionsError::UnsupportedAlgorithm(s.to_string()).into()),
},
Ed25519 => {
let key_bytes = match sshkey.key_data() {
Expand Down Expand Up @@ -819,7 +819,7 @@ impl Builder {

fn with_attribute(mut self, attr: AttrId, data: &Vec<u8>) -> Self {
let mut attributes = self.attributes.unwrap_or_default();
attributes.insert(attr, data.clone().into());
attributes.insert(attr, data.to_owned().into());
self.attributes = Some(attributes);
self
}
Expand All @@ -831,7 +831,8 @@ impl Builder {

/// add in the threshold value
pub fn with_threshold(self, threshold: usize) -> Self {
self.with_attribute(AttrId::Threshold, &Varuint(threshold).into())
let v: Vec<u8> = Varuint(threshold).into();
self.with_attribute(AttrId::Threshold, &v)
}

/// add in the limit value
Expand Down Expand Up @@ -861,7 +862,7 @@ impl Builder {
pub fn try_build_encoded(self) -> Result<EncodedMultikey, Error> {
Ok(BaseEncoded::new(
self.base_encoding
.unwrap_or_else(|| Multikey::preferred_encoding()),
.unwrap_or_else(Multikey::preferred_encoding),
self.try_build()?,
))
}
Expand Down
10 changes: 7 additions & 3 deletions src/nonce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ impl Nonce {
pub fn len(&self) -> usize {
self.nonce.len()
}

/// return if the nonce is empty
pub fn is_empty(&self) -> bool {
self.nonce.is_empty()
}
}

impl CodecInfo for Nonce {
Expand Down Expand Up @@ -121,8 +126,7 @@ pub struct Builder {
impl Builder {
/// build from random source
pub fn new_from_random_bytes(size: usize, rng: &mut (impl RngCore + CryptoRng)) -> Self {
let mut bytes = Vec::with_capacity(size);
bytes.resize(size, 0u8);
let mut bytes = vec![0; size];
rng.fill_bytes(bytes.as_mut());
Self {
bytes,
Expand All @@ -148,7 +152,7 @@ impl Builder {
pub fn try_build_encoded(&self) -> Result<EncodedNonce, Error> {
Ok(EncodedNonce::new(
self.base_encoding
.unwrap_or_else(|| Nonce::preferred_encoding()),
.unwrap_or_else(Nonce::preferred_encoding),
self.try_build()?,
))
}
Expand Down
12 changes: 6 additions & 6 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ impl<'de> Deserialize<'de> for Nonce {
where
D: Deserializer<'de>,
{
const FIELDS: &'static [&'static str] = &["nonce"];
const FIELDS: &[&str] = &["nonce"];

#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
Expand Down Expand Up @@ -84,28 +84,28 @@ impl<'de> Deserialize<'de> for AttrId {
where
E: Error,
{
Ok(AttrId::try_from(c).map_err(|e| Error::custom(e.to_string()))?)
AttrId::try_from(c).map_err(|e| Error::custom(e.to_string()))
}

fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))?)
AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))
}

fn visit_borrowed_str<E>(self, s: &'de str) -> Result<Self::Value, E>
where
E: Error,
{
Ok(AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))?)
AttrId::try_from(s).map_err(|e| Error::custom(e.to_string()))
}

fn visit_string<E>(self, s: String) -> Result<Self::Value, E>
where
E: Error,
{
Ok(AttrId::try_from(s.as_str()).map_err(|e| Error::custom(e.to_string()))?)
AttrId::try_from(s.as_str()).map_err(|e| Error::custom(e.to_string()))
}
}

Expand All @@ -119,7 +119,7 @@ impl<'de> Deserialize<'de> for Multikey {
where
D: Deserializer<'de>,
{
const FIELDS: &'static [&'static str] = &["codec", "comment", "attributes"];
const FIELDS: &[&str] = &["codec", "comment", "attributes"];

#[derive(Deserialize)]
#[serde(field_identifier, rename_all = "lowercase")]
Expand Down
12 changes: 6 additions & 6 deletions src/views/bcrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'a> KdfView for View<'a> {
/// generates a new Multikey by copying the viewed Multikey (self) and
/// storing the derived key and attributes in the new Multikey
fn derive_key(&self, passphrase: &[u8]) -> Result<Multikey, Error> {
let kdf = self.kdf.ok_or_else(|| KdfError::MissingCodec)?;
let kdf = self.kdf.ok_or(KdfError::MissingCodec)?;

// get the salt data and rounds attribute
let (salt, rounds) = {
Expand All @@ -42,21 +42,21 @@ impl<'a> KdfView for View<'a> {
// get the key length from the viewed Multikey
let key_length = {
let cattr = self.mk.cipher_attr_view()?;
let key_length = cattr.key_length()?;
key_length

cattr.key_length()?
};

// heap allocate a buffer to receive the derived key
let mut key: Zeroizing<Vec<u8>> = vec![0; key_length].into();

// derive the key
bcrypt_pbkdf::bcrypt_pbkdf(
passphrase.as_ref(),
passphrase,
&salt,
rounds as u32,
key.as_mut_slice(),
)
.map_err(|e| KdfError::Bcrypt(e))?;
.map_err(KdfError::Bcrypt)?;

// prepare the attributes
let kdf_codec: Vec<u8> = kdf.codec.into();
Expand Down Expand Up @@ -85,7 +85,7 @@ impl<'a> KdfAttrView for View<'a> {
.mk
.attributes
.get(&AttrId::KdfSalt)
.ok_or_else(|| KdfError::MissingSalt)?;
.ok_or(KdfError::MissingSalt)?;
if salt.len() != SALT_LENGTH {
Err(KdfError::InvalidSaltLen.into())
} else {
Expand Down
Loading

0 comments on commit ea25369

Please sign in to comment.