Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop needless bool in constant-time comparison/verification functions #102

Merged
merged 4 commits into from
Oct 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
//! let msg = "Some message.".as_bytes();
//!
//! let expected_tag = auth::authenticate(&key, msg)?;
//! assert!(auth::authenticate_verify(&expected_tag, &key, &msg)?);
//! assert!(auth::authenticate_verify(&expected_tag, &key, &msg).is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`SecretKey`]: struct.SecretKey.html
Expand All @@ -82,7 +82,7 @@ pub fn authenticate_verify(
expected: &Tag,
secret_key: &SecretKey,
data: &[u8],
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
let key = hmac::SecretKey::from_slice(secret_key.unprotected_as_bytes())?;
hmac::verify(expected, &key, data)
}
Expand All @@ -102,10 +102,7 @@ mod public {

let hmac_bob = authenticate(&sec_key_correct, &msg).unwrap();

assert_eq!(
authenticate_verify(&hmac_bob, &sec_key_correct, &msg).unwrap(),
true
);
assert!(authenticate_verify(&hmac_bob, &sec_key_correct, &msg).is_ok());
assert!(authenticate_verify(&hmac_bob, &sec_key_false, &msg).is_err());
}

Expand All @@ -116,10 +113,7 @@ mod public {

let hmac_bob = authenticate(&sec_key, &msg).unwrap();

assert_eq!(
authenticate_verify(&hmac_bob, &sec_key, &msg).unwrap(),
true
);
assert!(authenticate_verify(&hmac_bob, &sec_key, &msg).is_ok());
assert!(authenticate_verify(&hmac_bob, &sec_key, b"bad msg").is_err());
}
}
Expand All @@ -136,7 +130,7 @@ mod public {
let sk = SecretKey::default();

let tag = authenticate(&sk, &input[..]).unwrap();
authenticate_verify(&tag, &sk, &input[..]).unwrap()
authenticate_verify(&tag, &sk, &input[..]).is_ok()
}
}

Expand Down
23 changes: 10 additions & 13 deletions src/hazardous/hash/blake2b.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
//! let mut state_keyed = blake2b::Blake2b::new(Some(&secret_key), 64)?;
//! state_keyed.update(b"Some data")?;
//! let mac = state_keyed.finalize()?;
//! assert!(blake2b::verify(&mac, &secret_key, 64, b"Some data")?);
//! assert!(blake2b::verify(&mac, &secret_key, 64, b"Some data").is_ok());
//!
//! // Using the `Hasher` for convenience functions.
//! let digest = blake2b::Hasher::Blake2b512.digest(b"Some data")?;
Expand Down Expand Up @@ -461,12 +461,12 @@ pub fn verify(
secret_key: &SecretKey,
size: usize,
data: &[u8],
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
let mut state = Blake2b::new(Some(secret_key), size)?;
state.update(data)?;

if expected == &state.finalize()? {
Ok(true)
Ok(())
} else {
Err(UnknownCryptoError)
}
Expand Down Expand Up @@ -568,16 +568,13 @@ mod public {
let mut tag = Blake2b::new(Some(&secret_key), 64).unwrap();
tag.update(data).unwrap();

assert_eq!(
verify(
&tag.finalize().unwrap(),
&SecretKey::from_slice("Jefe".as_bytes()).unwrap(),
64,
data
)
.unwrap(),
true
);
assert!(verify(
&tag.finalize().unwrap(),
&SecretKey::from_slice("Jefe".as_bytes()).unwrap(),
64,
data
)
.is_ok());
}

// Proptests. Only exectued when NOT testing no_std.
Expand Down
4 changes: 2 additions & 2 deletions src/hazardous/kdf/hkdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
//! "IKM".as_bytes(),
//! None,
//! &mut okm_out
//! )?);
//! ).is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`util::secure_rand_bytes()`]: ../../../util/fn.secure_rand_bytes.html
Expand Down Expand Up @@ -143,7 +143,7 @@ pub fn verify(
ikm: &[u8],
info: Option<&[u8]>,
dst_out: &mut [u8],
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
expand(&extract(salt, ikm)?, info, dst_out)?;
util::secure_cmp(&dst_out, expected)
}
Expand Down
4 changes: 2 additions & 2 deletions src/hazardous/kdf/pbkdf2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
//! &salt,
//! 10000,
//! &mut dk_out
//! )?);
//! ).is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`Password::generate()`]: struct.Password.html#method.generate
Expand Down Expand Up @@ -175,7 +175,7 @@ pub fn verify(
salt: &[u8],
iterations: usize,
dst_out: &mut [u8],
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
derive_key(password, salt, iterations, dst_out)?;
util::secure_cmp(&dst_out, expected)
}
Expand Down
21 changes: 9 additions & 12 deletions src/hazardous/mac/hmac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
//! state.update(b"Some message.")?;
//! let tag = state.finalize()?;
//!
//! assert!(hmac::verify(&tag, &key, b"Some message.")?);
//! assert!(hmac::verify(&tag, &key, b"Some message.").is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`update()`]: struct.Hmac.html
Expand Down Expand Up @@ -196,12 +196,12 @@ pub fn verify(
expected: &Tag,
secret_key: &SecretKey,
data: &[u8],
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
let mut hmac_state = Hmac::new(secret_key);
hmac_state.update(data)?;

if expected == &hmac_state.finalize()? {
Ok(true)
Ok(())
} else {
Err(UnknownCryptoError)
}
Expand Down Expand Up @@ -237,15 +237,12 @@ mod public {
let mut tag = Hmac::new(&secret_key);
tag.update(data).unwrap();

assert_eq!(
verify(
&tag.finalize().unwrap(),
&SecretKey::from_slice("Jefe".as_bytes()).unwrap(),
data
)
.unwrap(),
true
);
assert!(verify(
&tag.finalize().unwrap(),
&SecretKey::from_slice("Jefe".as_bytes()).unwrap(),
data
)
.is_ok());
}

// Proptests. Only exectued when NOT testing no_std.
Expand Down
21 changes: 9 additions & 12 deletions src/hazardous/mac/poly1305.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
//! poly1305_state.update(msg.as_bytes())?;
//! let tag = poly1305_state.finalize()?;
//!
//! assert!(poly1305::verify(&tag, &one_time_key, msg.as_bytes())?);
//! assert!(poly1305::verify(&tag, &one_time_key, msg.as_bytes()).is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`update()`]: struct.Poly1305.html
Expand Down Expand Up @@ -418,9 +418,9 @@ pub fn verify(
expected: &Tag,
one_time_key: &OneTimeKey,
data: &[u8],
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
if &poly1305(one_time_key, data)? == expected {
Ok(true)
Ok(())
} else {
Err(UnknownCryptoError)
}
Expand Down Expand Up @@ -479,15 +479,12 @@ mod public {
let mut tag = Poly1305::new(&secret_key);
tag.update(data).unwrap();

assert_eq!(
verify(
&tag.finalize().unwrap(),
&OneTimeKey::from_slice(&[0u8; 32]).unwrap(),
data
)
.unwrap(),
true
);
assert!(verify(
&tag.finalize().unwrap(),
&OneTimeKey::from_slice(&[0u8; 32]).unwrap(),
data
)
.is_ok());
}

// Proptests. Only exectued when NOT testing no_std.
Expand Down
10 changes: 5 additions & 5 deletions src/kdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
//! &user_password,
//! &salt,
//! 100000
//! )?);
//! ).is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`Salt`]: struct.Salt.html
Expand Down Expand Up @@ -117,10 +117,10 @@ pub fn derive_key_verify(
password: &Password,
salt: &Salt,
iterations: usize,
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
let mut buffer = vec![0u8; expected.get_length()];

let is_good = pbkdf2::verify(
pbkdf2::verify(
expected.unprotected_as_bytes(),
&pbkdf2::Password::from_slice(password.unprotected_as_bytes())?,
salt.as_ref(),
Expand All @@ -130,7 +130,7 @@ pub fn derive_key_verify(

buffer.zeroize();

Ok(is_good)
Ok(())
}

// Testing public functions in the module.
Expand All @@ -147,7 +147,7 @@ mod public {

let dk = derive_key(&password, &salt, 100, 64).unwrap();

assert!(derive_key_verify(&dk, &password, &salt, 100).unwrap());
assert!(derive_key_verify(&dk, &password, &salt, 100).is_ok());
}

#[test]
Expand Down
13 changes: 5 additions & 8 deletions src/pwhash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
//! let password = pwhash::Password::from_slice(b"Secret password")?;
//!
//! let hash = pwhash::hash_password(&password, 100000)?;
//! assert!(pwhash::hash_password_verify(&hash, &password, 100000)?);
//! assert!(pwhash::hash_password_verify(&hash, &password, 100000).is_ok());
//! # Ok::<(), orion::errors::UnknownCryptoError>(())
//! ```
//! [`PasswordHash`]: struct.PasswordHash.html
Expand Down Expand Up @@ -111,10 +111,10 @@ pub fn hash_password_verify(
expected_with_salt: &PasswordHash,
password: &Password,
iterations: usize,
) -> Result<bool, UnknownCryptoError> {
) -> Result<(), UnknownCryptoError> {
let mut dk = [0u8; 64];

let is_good = pbkdf2::verify(
pbkdf2::verify(
&expected_with_salt.unprotected_as_bytes()[64..],
&pbkdf2::Password::from_slice(password.unprotected_as_bytes())?,
&expected_with_salt.unprotected_as_bytes()[..64],
Expand All @@ -124,7 +124,7 @@ pub fn hash_password_verify(

dk.zeroize();

Ok(is_good)
Ok(())
}

// Testing public functions in the module.
Expand All @@ -141,10 +141,7 @@ mod public {

let pbkdf2_dk = hash_password(&password, 100).unwrap();

assert_eq!(
hash_password_verify(&pbkdf2_dk, &password, 100).unwrap(),
true
);
assert!(hash_password_verify(&pbkdf2_dk, &password, 100).is_ok());
}

#[test]
Expand Down
10 changes: 5 additions & 5 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,15 +89,15 @@ pub fn secure_rand_bytes(dst: &mut [u8]) -> Result<(), errors::UnknownCryptoErro
/// use orion::util;
///
/// let mut rnd_bytes = [0u8; 64];
/// assert!(util::secure_cmp(&rnd_bytes, &[0u8; 64])?);
/// assert!(util::secure_cmp(&rnd_bytes, &[0u8; 64]).is_ok());
///
/// util::secure_rand_bytes(&mut rnd_bytes)?;
/// assert!(util::secure_cmp(&rnd_bytes, &[0u8; 64]).is_err());
/// # Ok::<(), orion::errors::UnknownCryptoError>(())
/// ```
pub fn secure_cmp(a: &[u8], b: &[u8]) -> Result<bool, errors::UnknownCryptoError> {
pub fn secure_cmp(a: &[u8], b: &[u8]) -> Result<(), errors::UnknownCryptoError> {
if a.ct_eq(b).into() {
Ok(true)
Ok(())
} else {
Err(errors::UnknownCryptoError)
}
Expand Down Expand Up @@ -127,8 +127,8 @@ fn test_ct_eq_ok() {
let buf_1 = [0x06; 10];
let buf_2 = [0x06; 10];

assert_eq!(secure_cmp(&buf_1, &buf_2).unwrap(), true);
assert_eq!(secure_cmp(&buf_2, &buf_1).unwrap(), true);
assert!(secure_cmp(&buf_1, &buf_2).is_ok());
assert!(secure_cmp(&buf_2, &buf_1).is_ok());
}

#[test]
Expand Down
Loading