From 6d655c80858773ce504f976e69f9851784c6ef95 Mon Sep 17 00:00:00 2001 From: bunnie Date: Sun, 2 Jan 2022 19:37:30 +0800 Subject: [PATCH] convert salt to use an HMAC instead of SHA to harden against length extension attacks (as reccommended by reviewer) --- Cargo.lock | 21 ++++++++++++++++ services/pddb/Cargo.toml | 3 +-- services/pddb/src/api.rs | 13 ++++++++++ services/pddb/src/backend/hw.rs | 43 ++++++++------------------------- 4 files changed, 45 insertions(+), 35 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e01ce8511..23511ec70 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -521,6 +521,16 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "crypto-mac" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58bcd97a54c7ca5ce2f6eb16f6bede5b0ab5f0055fedc17d2f0b4466e21671ca" +dependencies = [ + "generic-array", + "subtle", +] + [[package]] name = "csv" version = "1.1.5" @@ -957,6 +967,16 @@ dependencies = [ "proc-macro-hack", ] +[[package]] +name = "hmac" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "deae6d9dbb35ec2c502d62b8f7b1c000a0822c3b0794ba36b3149c0a1c840dff" +dependencies = [ + "crypto-mac", + "digest", +] + [[package]] name = "humantime" version = "1.3.0" @@ -1452,6 +1472,7 @@ dependencies = [ "cipher", "digest", "gam", + "hmac", "llio", "locales", "log", diff --git a/services/pddb/Cargo.toml b/services/pddb/Cargo.toml index b657acfbf..83ccbc5ab 100644 --- a/services/pddb/Cargo.toml +++ b/services/pddb/Cargo.toml @@ -29,6 +29,7 @@ llio = {path="../llio"} # passwords sha2 = {path = "../engine-sha512"} +hmac = "0.9.0" digest = "0.9" # bcrypt @@ -59,6 +60,4 @@ 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/api.rs b/services/pddb/src/api.rs index 013ea4b1d..1a3a1dfcc 100644 --- a/services/pddb/src/api.rs +++ b/services/pddb/src/api.rs @@ -15,6 +15,19 @@ pub(crate) const PDDB_VERSION: u32 = 0x00_00_00_01; #[allow(dead_code)] pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024; +/// range for the starting point of a journal number, picked from a random seed +/// the goal is to reduce info leakage about the age of structures relative to each other +/// in various basis in case of partial disclosure of passwords (especially the system password) +#[allow(dead_code)] +pub(crate) const JOURNAL_RAND_RANGE: u32 = 1_000_000; +/// fixed offset for system journal entries. If we get "unlucky" and a new system record has +/// a low journal number, and then a basis entry has a lower journal number, we could conclude +/// that this basis data is younger (because journal numbers start at a value and count up). +/// By introducing a fixed offset on the system basis records, a lower offset on a basis entry +/// is no longer such a surprising outcome (I hope...). +#[allow(dead_code)] +pub(crate) const JOURNAL_SYS_OFFSET: u32 = 100_000; + /// A number between (0, 1] that defines how many of the "truly free" pages we /// should put into the FSCB. A value of 0.0 is not allowed as that leaves no free pages. /// A value of 1.0 would allow an attacker to deduce the real size of data because all diff --git a/services/pddb/src/backend/hw.rs b/services/pddb/src/backend/hw.rs index 4f5fb9242..e285e0a8e 100644 --- a/services/pddb/src/backend/hw.rs +++ b/services/pddb/src/backend/hw.rs @@ -5,6 +5,7 @@ use aes_gcm_siv::{Aes256GcmSiv, Nonce, Key, Tag}; use aes_gcm_siv::aead::{Aead, NewAead, Payload}; use aes::{Aes256, Block}; use aes::cipher::{BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray}; +use hmac::NewMac; use root_keys::api::AesRootkeyType; use core::ops::{Deref, DerefMut}; use core::mem::size_of; @@ -1341,7 +1342,8 @@ impl PddbOs { /// Derives a 256-bit AES encryption key for a basis given a basis name and its password. /// You will also need to derive the AAD for the basis using the basis_name. pub(crate) fn basis_derive_key(&self, basis_name: &str, password: &str) -> [u8; AES_KEYSIZE] { - use sha2::{FallbackStrategy, Sha512, Sha512Trunc256}; + use sha2::{FallbackStrategy, Sha256, Sha512Trunc256}; + use hmac::{Hmac, Mac}; use digest::Digest; use backend::bcrypt::*; @@ -1358,41 +1360,16 @@ impl PddbOs { } plaintext_pw[72] = 0; // always null terminate - log::info!("making hasher"); - 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. log::info!("creating salt"); + // uses an HMAC keyed with the salt array to generate a compressed version of + // the basis name and plaintext password, which forms the Salt that is fed into bcrypt 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()) { + let mut mac = Hmac::::new_varkey(&scd.salt_base).expect("Couldn't build HMAC"); + mac.update(&bname_copy); + mac.update(&plaintext_pw); + let result = mac.finalize().into_bytes(); + for (&src, dst) in result.iter().zip(salt.iter_mut()) { *dst = src; } log::info!("derived salt: {:x?}", salt);