Skip to content

Commit

Permalink
Update key manager hasher labels
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronFeickert committed May 7, 2024
1 parent 4033f07 commit 7ed5d94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions base_layer/key_manager/src/cipher_seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,18 @@ use crate::{
CipherSeedEncryptionKey,
CipherSeedMacKey,
SeedWords,
LABEL_ARGON_ENCODING,
LABEL_CHACHA20_ENCODING,
LABEL_MAC_GENERATION,
HASHER_LABEL_CIPHER_SEED_ENCRYPTION_NONCE,
HASHER_LABEL_CIPHER_SEED_MAC,
HASHER_LABEL_CIPHER_SEED_PBKDF_SALT,
};

// The version should be incremented for any breaking change to the format
// NOTE: Only the most recent version is supported!
// History:
// 0: initial version
// 1: fixed incorrect key derivation and birthday genesis
const CIPHER_SEED_VERSION: u8 = 1u8;
// 2: updated hasher domain labels and MAC input ordering
const CIPHER_SEED_VERSION: u8 = 2u8;

pub const BIRTHDAY_GENESIS_FROM_UNIX_EPOCH: u64 = 1640995200; // seconds to 2022-01-01 00:00:00 UTC
pub const DEFAULT_CIPHER_SEED_PASSPHRASE: &str = "TARI_CIPHER_SEED"; // the default passphrase if none is supplied
Expand Down Expand Up @@ -180,9 +181,9 @@ impl CipherSeed {

// Generate the MAC
let mac = Self::generate_mac(
CIPHER_SEED_VERSION,
&self.birthday.to_le_bytes(),
self.entropy.as_ref(),
CIPHER_SEED_VERSION,
self.salt.as_ref(),
&mac_key,
)?;
Expand Down Expand Up @@ -279,7 +280,7 @@ impl CipherSeed {
let birthday = u16::from_le_bytes(birthday_bytes);

// Generate the MAC
let expected_mac = Self::generate_mac(&birthday_bytes, entropy.reveal(), version, salt.as_ref(), &mac_key)?;
let expected_mac = Self::generate_mac(version, &birthday_bytes, entropy.reveal(), salt.as_ref(), &mac_key)?;

// Verify the MAC in constant time to avoid leaking data
if mac.ct_eq(&expected_mac).into() {
Expand All @@ -301,7 +302,7 @@ impl CipherSeed {
salt: &[u8],
) -> Result<(), KeyManagerError> {
// The ChaCha20 nonce is derived from the main salt
let encryption_nonce = mac_domain_hasher::<Blake2b<U32>>(LABEL_CHACHA20_ENCODING)
let encryption_nonce = mac_domain_hasher::<Blake2b<U32>>(HASHER_LABEL_CIPHER_SEED_ENCRYPTION_NONCE)
.chain(salt)
.finalize();
let encryption_nonce = &encryption_nonce.as_ref()[..size_of::<Nonce>()];
Expand Down Expand Up @@ -330,9 +331,9 @@ impl CipherSeed {

/// Generate a MAC using Blake2b
fn generate_mac(
version: u8,
birthday: &[u8],
entropy: &[u8],
cipher_seed_version: u8,
salt: &[u8],
mac_key: &CipherSeedMacKey,
) -> Result<Vec<u8>, KeyManagerError> {
Expand All @@ -347,10 +348,10 @@ impl CipherSeed {
return Err(KeyManagerError::InvalidData);
}

Ok(mac_domain_hasher::<Blake2b<U32>>(LABEL_MAC_GENERATION)
Ok(mac_domain_hasher::<Blake2b<U32>>(HASHER_LABEL_CIPHER_SEED_MAC)
.chain([version])
.chain(birthday)
.chain(entropy)
.chain([cipher_seed_version])
.chain(salt)
.chain(mac_key.reveal())
.finalize()
Expand All @@ -361,7 +362,7 @@ impl CipherSeed {
/// Use Argon2 to derive encryption and MAC keys from a passphrase and main salt
fn derive_keys(passphrase: &SafePassword, salt: &[u8]) -> DerivedCipherSeedKeys {
// The Argon2 salt is derived from the main salt
let argon2_salt = mac_domain_hasher::<Blake2b<U32>>(LABEL_ARGON_ENCODING)
let argon2_salt = mac_domain_hasher::<Blake2b<U32>>(HASHER_LABEL_CIPHER_SEED_PBKDF_SALT)
.chain(salt)
.finalize();
let argon2_salt = &argon2_salt.as_ref()[..ARGON2_SALT_BYTES];
Expand Down
6 changes: 3 additions & 3 deletions base_layer/key_manager/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ pub mod schema;

hash_domain!(KeyManagerDomain, "com.tari.base_layer.key_manager", 1);

const LABEL_ARGON_ENCODING: &str = "argon2_encoding";
const LABEL_CHACHA20_ENCODING: &str = "chacha20_encoding";
const LABEL_MAC_GENERATION: &str = "mac_generation";
const HASHER_LABEL_CIPHER_SEED_PBKDF_SALT: &str = "cipher_seed_pbkdf_salt";
const HASHER_LABEL_CIPHER_SEED_ENCRYPTION_NONCE: &str = "cipher_seed_encryption_nonce";
const HASHER_LABEL_CIPHER_SEED_MAC: &str = "cipher_seed_mac";
const LABEL_DERIVE_KEY: &str = "derive_key";

pub(crate) fn mac_domain_hasher<D: Digest + LengthExtensionAttackResistant>(
Expand Down

0 comments on commit 7ed5d94

Please sign in to comment.