From 9910547e21f73c61f5921cca641633779acff781 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jan 2022 17:53:00 +0800 Subject: [PATCH] modify salt generation scheme based on feedback that the current mechanism is overly complicated and doesn't add any real security, and it's better to stick with something simple e.g. just a tried-and-true hash of the basics --- buildpush.ps1 | 2 +- services/pddb/Cargo.toml | 2 ++ services/pddb/src/backend/hw.rs | 21 +++++++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) diff --git a/buildpush.ps1 b/buildpush.ps1 index eca0a0dc4..f25b2faba 100644 --- a/buildpush.ps1 +++ b/buildpush.ps1 @@ -11,7 +11,7 @@ CertUtil -hashfile precursors/soc_csr.bin MD5 CertUtil -hashfile target/riscv32imac-unknown-xous-elf/release/loader.bin MD5 CertUtil -hashfile target/riscv32imac-unknown-xous-elf/release/xous.img MD5 -scp -i c:/users/bunnie/.ssh/id_pi target/riscv32imac-unknown-xous-elf/release/xous.img target/riscv32imac-unknown-xous-elf/release/loader.bin precursors/soc_csr.bin pi@10.0.245.90:code/precursors/ +scp -i c:/users/bunnie/.ssh/id_pi target/riscv32imac-unknown-xous-elf/release/xous.img target/riscv32imac-unknown-xous-elf/release/loader.bin precursors/soc_csr.bin pi@10.0.245.181:code/precursors/ # CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/loader.bin MD5 # CertUtil -hashfile target/riscv32imac-unknown-none-elf/release/xous.img MD5 diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index 88230d755..b657acfbf 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -59,4 +59,6 @@ mbbb = [] deterministic = [] # this flag adds CI tests to the build ci = [] +# selects the salt strategy; defaults to salt_v2 if salt_v1 is not present +salt_v1 = [] default = ["mbbb"] diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index fdde320ce..4f5fb9242 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -1362,6 +1362,18 @@ impl PddbOs { let mut hasher = Sha512::new_with_strategy(FallbackStrategy::SoftwareOnly); hasher.update(&bname_copy); hasher.update(&plaintext_pw); + + /* + v1 salt: takes Sha512 hash of {basis name, plaintext password}, and uses the bits of the hash + to sequence through a lookup table of salt values. It was noted that this process actually could + reduce entropy, and it's complicated and hard to analyze without necessarily adding any security. + + v2 salt: takes Sha512 hash of {basis name, plaintext password, salt region}, and uses the first + 16 bytes as the salt to the bcrypt algorithm. This is much "simpler" and hopefully easier to analyze + for problems. We use the full salt region only because we already have it, we could, of course, + go to a shorter string if for some reason that was a better idea. + */ + #[cfg(feature="salt_v1")] let digest = hasher.finalize(); // 2. now derive the actual salt by using indices from the digest to progressively // index into the salt table. @@ -1369,11 +1381,20 @@ impl PddbOs { let scd = self.static_crypto_data_get(); let mut salt = [0u8; 16]; log::info!("got scd"); + #[cfg(feature="salt_v1")] for (index_bytes, dst) in digest.chunks(2).into_iter().zip(salt.iter_mut()) { let index = u16::from_le_bytes(index_bytes.try_into().unwrap()) as usize % scd.salt_base.len(); *dst = scd.salt_base[index]; log::info!("index: {}, dst: {}", index as usize % scd.salt_base.len(), *dst); } + #[cfg(not(feature="salt_v1"))] + hasher.update(&scd.salt_base); + #[cfg(not(feature="salt_v1"))] + let digest = hasher.finalize(); + #[cfg(not(feature="salt_v1"))] + for (&src, dst) in digest.iter().zip(salt.iter_mut()) { + *dst = src; + } log::info!("derived salt: {:x?}", salt); // 3. use the salt + password and run bcrypt on it to derive a key.