From 004d5260492e938bda8d00d0718798e5950ab6c4 Mon Sep 17 00:00:00 2001 From: Aaron Feickert <66188213+AaronFeickert@users.noreply.github.com> Date: Mon, 5 Feb 2024 12:42:30 -0600 Subject: [PATCH] Make MAC equality check more idiomatic --- base_layer/key_manager/src/cipher_seed.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/base_layer/key_manager/src/cipher_seed.rs b/base_layer/key_manager/src/cipher_seed.rs index 0b5f7a63fa..acae2f54c1 100644 --- a/base_layer/key_manager/src/cipher_seed.rs +++ b/base_layer/key_manager/src/cipher_seed.rs @@ -283,16 +283,16 @@ impl CipherSeed { let expected_mac = Self::generate_mac(&birthday_bytes, entropy.reveal(), version, salt.as_ref(), &mac_key)?; // Verify the MAC in constant time to avoid leaking data - if mac.ct_eq(&expected_mac).unwrap_u8() == 0 { - return Err(KeyManagerError::DecryptionFailed); + if mac.ct_eq(&expected_mac).into() { + Ok(Self { + version, + birthday, + entropy: Box::from(*entropy.reveal()), + salt, + }) + } else { + Err(KeyManagerError::DecryptionFailed) } - - Ok(Self { - version, - birthday, - entropy: Box::from(*entropy.reveal()), - salt, - }) } /// Encrypt or decrypt data using ChaCha20