convert salt to use an HMAC instead of SHA to harden against length extension attacks

(as reccommended by reviewer)
This commit is contained in:
bunnie 2022-01-02 19:37:30 +08:00
parent 9910547e21
commit 6d655c8085
4 changed files with 45 additions and 35 deletions

21
Cargo.lock generated
View file

@ -521,6 +521,16 @@ dependencies = [
"cfg-if 1.0.0", "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]] [[package]]
name = "csv" name = "csv"
version = "1.1.5" version = "1.1.5"
@ -957,6 +967,16 @@ dependencies = [
"proc-macro-hack", "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]] [[package]]
name = "humantime" name = "humantime"
version = "1.3.0" version = "1.3.0"
@ -1452,6 +1472,7 @@ dependencies = [
"cipher", "cipher",
"digest", "digest",
"gam", "gam",
"hmac",
"llio", "llio",
"locales", "locales",
"log", "log",

View file

@ -29,6 +29,7 @@ llio = {path="../llio"}
# passwords # passwords
sha2 = {path = "../engine-sha512"} sha2 = {path = "../engine-sha512"}
hmac = "0.9.0"
digest = "0.9" digest = "0.9"
# bcrypt # bcrypt
@ -59,6 +60,4 @@ mbbb = []
deterministic = [] deterministic = []
# this flag adds CI tests to the build # this flag adds CI tests to the build
ci = [] ci = []
# selects the salt strategy; defaults to salt_v2 if salt_v1 is not present
salt_v1 = []
default = ["mbbb"] default = ["mbbb"]

View file

@ -15,6 +15,19 @@ pub(crate) const PDDB_VERSION: u32 = 0x00_00_00_01;
#[allow(dead_code)] #[allow(dead_code)]
pub(crate) const PDDB_A_LEN: usize = 4 * 1024 * 1024; 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 /// 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. /// 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 /// A value of 1.0 would allow an attacker to deduce the real size of data because all

View file

@ -5,6 +5,7 @@ use aes_gcm_siv::{Aes256GcmSiv, Nonce, Key, Tag};
use aes_gcm_siv::aead::{Aead, NewAead, Payload}; use aes_gcm_siv::aead::{Aead, NewAead, Payload};
use aes::{Aes256, Block}; use aes::{Aes256, Block};
use aes::cipher::{BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray}; use aes::cipher::{BlockDecrypt, BlockEncrypt, NewBlockCipher, generic_array::GenericArray};
use hmac::NewMac;
use root_keys::api::AesRootkeyType; use root_keys::api::AesRootkeyType;
use core::ops::{Deref, DerefMut}; use core::ops::{Deref, DerefMut};
use core::mem::size_of; 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. /// 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. /// 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] { 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 digest::Digest;
use backend::bcrypt::*; use backend::bcrypt::*;
@ -1358,41 +1360,16 @@ impl PddbOs {
} }
plaintext_pw[72] = 0; // always null terminate 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"); 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 scd = self.static_crypto_data_get();
let mut salt = [0u8; 16]; let mut salt = [0u8; 16];
log::info!("got scd"); let mut mac = Hmac::<Sha256>::new_varkey(&scd.salt_base).expect("Couldn't build HMAC");
#[cfg(feature="salt_v1")] mac.update(&bname_copy);
for (index_bytes, dst) in digest.chunks(2).into_iter().zip(salt.iter_mut()) { mac.update(&plaintext_pw);
let index = u16::from_le_bytes(index_bytes.try_into().unwrap()) as usize % scd.salt_base.len(); let result = mac.finalize().into_bytes();
*dst = scd.salt_base[index]; for (&src, dst) in result.iter().zip(salt.iter_mut()) {
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; *dst = src;
} }
log::info!("derived salt: {:x?}", salt); log::info!("derived salt: {:x?}", salt);