Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
jack-signal committed Jul 30, 2020
1 parent 1756893 commit 2ef8c08
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 50 deletions.
1 change: 1 addition & 0 deletions .clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
too-many-arguments-threshold = 8
11 changes: 4 additions & 7 deletions src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ use sha2::Sha256;
pub fn aes_256_cbc_encrypt(ptext: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>> {
match Cbc::<Aes256, Pkcs7>::new_var(key, iv) {
Ok(mode) => Ok(mode.encrypt_vec(&ptext)),
Err(block_modes::InvalidKeyIvLength) => {
return Err(SignalProtocolError::InvalidCipherCryptographicParameters(
key.len(),
iv.len(),
))
}
Err(block_modes::InvalidKeyIvLength) => Err(
SignalProtocolError::InvalidCipherCryptographicParameters(key.len(), iv.len()),
),
}
}

pub fn aes_256_cbc_decrypt(ctext: &[u8], key: &[u8], iv: &[u8]) -> Result<Vec<u8>> {
if ctext.len() == 0 || ctext.len() % 16 != 0 {
if ctext.is_empty() || ctext.len() % 16 != 0 {
return Err(SignalProtocolError::InvalidCiphertext);
}

Expand Down
2 changes: 1 addition & 1 deletion src/fingerprint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub struct ScannableFingerprint {
impl ScannableFingerprint {
fn new(version: u32, local_fprint: &[u8], remote_fprint: &[u8]) -> Self {
Self {
version: version,
version,
local_fingerprint: local_fprint[..32].to_vec(),
remote_fingerprint: remote_fprint[..32].to_vec(),
}
Expand Down
4 changes: 2 additions & 2 deletions src/group_cipher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ fn get_sender_key(state: &mut SenderKeyState, iteration: u32) -> Result<SenderMe
));
}

let mut sender_chain_key = sender_chain_key.clone();
let mut sender_chain_key = sender_chain_key;

while sender_chain_key.iteration()? < iteration {
state.add_sender_message_key(&sender_chain_key.sender_message_key()?)?;
Expand Down Expand Up @@ -115,7 +115,7 @@ pub fn process_sender_key_distribution_message(
) -> Result<()> {
let mut sender_key_record = sender_key_store
.load_sender_key(sender_key_name)?
.unwrap_or(SenderKeyRecord::new_empty());
.unwrap_or_else(SenderKeyRecord::new_empty);

sender_key_record.add_sender_key_state(
skdm.id()?,
Expand Down
2 changes: 1 addition & 1 deletion src/identity_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl TryFrom<&[u8]> for IdentityKey {
type Error = SignalProtocolError;

fn try_from(value: &[u8]) -> Result<Self> {
IdentityKey::decode(value).into()
IdentityKey::decode(value)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ impl TryFrom<&[u8]> for SenderKeyDistributionMessage {
.ok_or(SignalProtocolError::InvalidProtobufEncoding)?;

if chain_key.len() != 32 || signing_key.len() != 33 {
return Err(SignalProtocolError::InvalidProtobufEncoding)?;
return Err(SignalProtocolError::InvalidProtobufEncoding);
}

let signing_key = curve::PublicKey::deserialize(&signing_key)?;
Expand Down
2 changes: 1 addition & 1 deletion src/ratchet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use rand::{CryptoRng, Rng};
fn derive_keys(secret_input: &[u8]) -> Result<(RootKey, ChainKey)> {
let kdf = crate::kdf::HKDF::new(3)?;

let secrets = kdf.derive_secrets(secret_input, "WhisperText".as_bytes(), 64)?;
let secrets = kdf.derive_secrets(secret_input, b"WhisperText", 64)?;

let root_key = RootKey::new(kdf, &secrets[0..32])?;
let chain_key = ChainKey::new(kdf, &secrets[32..64], 0)?;
Expand Down
8 changes: 4 additions & 4 deletions src/sender_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct SenderMessageKey {
impl SenderMessageKey {
pub fn new(iteration: u32, seed: &[u8]) -> Result<Self> {
let hkdf = HKDF::new(3)?;
let derived = hkdf.derive_secrets(seed, "WhisperGroup".as_bytes(), 48)?;
let derived = hkdf.derive_secrets(seed, b"WhisperGroup", 48)?;
Ok(Self {
iteration,
seed: seed.to_vec(),
Expand Down Expand Up @@ -96,7 +96,7 @@ impl SenderChainKey {
pub fn new(iteration: u32, chain_key: Vec<u8>) -> Result<Self> {
Ok(Self {
iteration,
chain_key: chain_key,
chain_key,
})
}

Expand Down Expand Up @@ -270,11 +270,11 @@ impl SenderKeyRecord {
}

pub fn empty(&self) -> Result<bool> {
Ok(self.states.len() == 0)
Ok(self.states.is_empty())
}

pub fn sender_key_state(&mut self) -> Result<&mut SenderKeyState> {
if self.states.len() > 0 {
if !self.states.is_empty() {
return Ok(&mut self.states[0]);
}
Err(SignalProtocolError::NoSenderKeyState)
Expand Down
4 changes: 2 additions & 2 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn process_prekey_v3(
our_signed_pre_key_pair, // signed pre key
our_one_time_pre_key_pair,
our_signed_pre_key_pair, // ratchet key
message.identity_key().clone(),
*message.identity_key(),
*message.base_key(),
);

Expand Down Expand Up @@ -130,7 +130,7 @@ pub fn process_prekey_bundle<R: Rng + CryptoRng>(

let mut session_record = session_store
.load_session(&remote_address)?
.unwrap_or(SessionRecord::new_fresh());
.unwrap_or_else(SessionRecord::new_fresh);

let our_base_key_pair = curve::KeyPair::new(&mut csprng);
let their_signed_prekey = bundle.signed_pre_key_public()?;
Expand Down
41 changes: 14 additions & 27 deletions src/state/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,23 +218,13 @@ impl SessionState {
}

pub fn get_sender_chain_key(&self) -> Result<ChainKey> {
let sender_chain =
self.session
.sender_chain
.as_ref()
.ok_or(SignalProtocolError::InvalidState(
"get_sender_chain_key",
"No chain".to_owned(),
))?;

let chain_key =
sender_chain
.chain_key
.as_ref()
.ok_or(SignalProtocolError::InvalidState(
"get_sender_chain_key",
"No chain key".to_owned(),
))?;
let sender_chain = self.session.sender_chain.as_ref().ok_or_else(|| {
SignalProtocolError::InvalidState("get_sender_chain_key", "No chain".to_owned())
})?;

let chain_key = sender_chain.chain_key.as_ref().ok_or_else(|| {
SignalProtocolError::InvalidState("get_sender_chain_key", "No chain key".to_owned())
})?;

let hkdf = kdf::HKDF::new(self.session_version()?)?;
ChainKey::new(hkdf, &chain_key.key, chain_key.index)
Expand Down Expand Up @@ -329,12 +319,12 @@ impl SessionState {
}

self.session.receiver_chains[chain_and_index.1] = updated_chain;
return Ok(());
Ok(())
} else {
return Err(SignalProtocolError::InvalidState(
Err(SignalProtocolError::InvalidState(
"set_message_keys",
"No receiver".to_string(),
));
))
}
}

Expand Down Expand Up @@ -611,17 +601,14 @@ impl SessionRecord {
old_session: usize,
updated_session: SessionState,
) -> Result<()> {
self.previous_sessions
.remove(old_session)
.ok_or(SignalProtocolError::InvalidState(
"promote_old_session",
"out of range".into(),
))?;
self.previous_sessions.remove(old_session).ok_or_else(|| {
SignalProtocolError::InvalidState("promote_old_session", "out of range".into())
})?;
self.promote_state(updated_session)
}

pub fn is_fresh(&self) -> Result<bool> {
Ok(self.current_session.is_none() && self.previous_sessions.len() == 0)
Ok(self.current_session.is_none() && self.previous_sessions.is_empty())
}

pub fn promote_state(&mut self, new_state: SessionState) -> Result<()> {
Expand Down
32 changes: 28 additions & 4 deletions src/storage/inmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl InMemIdentityKeyStore {

impl traits::IdentityKeyStore for InMemIdentityKeyStore {
fn get_identity_key_pair(&self) -> Result<IdentityKeyPair> {
Ok(self.key_pair.clone())
Ok(self.key_pair)
}

fn get_local_registration_id(&self) -> Result<u32> {
Expand All @@ -33,14 +33,14 @@ impl traits::IdentityKeyStore for InMemIdentityKeyStore {
fn save_identity(&mut self, address: &ProtocolAddress, identity: &IdentityKey) -> Result<bool> {
match self.known_keys.get(address) {
None => {
self.known_keys.insert(address.clone(), identity.clone());
self.known_keys.insert(address.clone(), *identity);
Ok(false) // new key
}
Some(k) if k == identity => {
Ok(false) // same key
}
Some(_k) => {
self.known_keys.insert(address.clone(), identity.clone());
self.known_keys.insert(address.clone(), *identity);
Ok(true) // overwrite
}
}
Expand Down Expand Up @@ -80,6 +80,12 @@ impl InMemPreKeyStore {
}
}

impl Default for InMemPreKeyStore {
fn default() -> Self {
Self::new()
}
}

impl traits::PreKeyStore for InMemPreKeyStore {
fn get_pre_key(&self, id: PreKeyId) -> Result<PreKeyRecord> {
Ok(self
Expand Down Expand Up @@ -118,6 +124,12 @@ impl InMemSignedPreKeyStore {
}
}

impl Default for InMemSignedPreKeyStore {
fn default() -> Self {
Self::new()
}
}

impl traits::SignedPreKeyStore for InMemSignedPreKeyStore {
fn get_signed_pre_key(&self, id: SignedPreKeyId) -> Result<SignedPreKeyRecord> {
Ok(self
Expand Down Expand Up @@ -168,6 +180,12 @@ impl InMemSessionStore {
}
}

impl Default for InMemSessionStore {
fn default() -> Self {
Self::new()
}
}

impl traits::SessionStore for InMemSessionStore {
fn load_session(&self, address: &ProtocolAddress) -> Result<Option<SessionRecord>> {
match self.sessions.get(address) {
Expand Down Expand Up @@ -220,6 +238,12 @@ impl InMemSenderKeyStore {
}
}

impl Default for InMemSenderKeyStore {
fn default() -> Self {
Self::new()
}
}

impl traits::SenderKeyStore for InMemSenderKeyStore {
fn store_sender_key(
&mut self,
Expand All @@ -234,7 +258,7 @@ impl traits::SenderKeyStore for InMemSenderKeyStore {
&mut self,
sender_key_name: &SenderKeyName,
) -> Result<Option<SenderKeyRecord>> {
Ok(self.keys.get(&sender_key_name).map(|x| x.clone()))
Ok(self.keys.get(&sender_key_name).cloned())
}
}

Expand Down

0 comments on commit 2ef8c08

Please sign in to comment.