evict salamanders from AES-GCM-SIV in the PDDB

So you'd like to think that an authenticating cipher would like,
authenticate stuff on decryption. And AES-GCM-SIV was billed as sort
of AES with the pointy edges taken off.

Well, there's always one more bug, actually this time a salamander:
https://eprint.iacr.org/2020/1456.pdf Apparently one can forge
authentication if the attacker can control keys and ciphertext. In the
case of the PDDB I think the attack is largely theoretical, because
you'd have to forge through both the AES-GCM-SIV and the rather
insufficient nonce/murmur hash check on the PTE that locates the page
to be decrypted.

But it's never good to have cryptographic properties depend upon
something that's known to be insufficient, so let's assume the attacker
can simply forge keys and stick some data on the device.

This wouldn't be a problem for confidentiality but it could lead to
the ability to plant evidence, but more importantly could lead to code
paths that were thought to be protected against fuzzing being fuzzed
as you could conceivably create a Basis root record that "checked out"
and then the larger body of the PDDB code would try to chomp on other
pages that are forged, leading to a bigger attack surface. Plus who
knows what else might exist beyond salamanders...

This patch does a "key commitment" on the Basis root page, so that it
is now cryptographically hard to create a salamander.  Basically
instead of using the system root key directly in AES-GCM-SIV, the key
goes through a KDF that expands it with yet another nonce plus a bit
of header data. The KDF is SHA512/256 and it expands it twice,
changing one bit in the header data (as prescribed by the paper cited
above), to create a version of the key that is written to disk (the
commitment), and a version of the key that is used to
decrypt/authenticate the AES-GCM-SIV data. Only if both the commitment
and the authentication check out, do we accept a Basis root page as
valid.

I had some concerns about the slightly different structure of the
Basis Root page being detectable, but I have been assured that the key
commitment form and the MAC in AES-GCM-SIV satisfy a property known as
IND$-CPA ("indistinguishable from uniform randomness by a
chosen-plaintext attacker") by Rogaway[1], and, more strongly, "Secure
nonce misuse-resistant authenticated encryption (mrAE)" by Gueron and
Lindell (see appendix for def)[2]. Still, just out of total paranoia
and nothing else, I organize the basis root page such that the MAC is
in the identical location as the other non-committed pages...just in
case there is some mathematical property of the MAC that could be
leveraged to leak info about the existence of the basis root record.
The commitment nonce and sha512/256 result I understand better and
feel pretty comfortable that they are going to be strongly random and
indistinguishable from AES ciphertext.

Thanks to Michael Rosenberg for taking the time to explain all this to
me and to guide me through the process of making the fixes. It is a
major PDDB API-breaking change and I'm glad it's able to be rolled out
before most users have received a device.

[1] https://web.cs.ucdavis.edu/~rogaway/papers/ad.pdf
[2] https://eprint.iacr.org/2015/102
This commit is contained in:
bunnie 2022-02-28 03:13:26 +08:00
parent 2c668fde3d
commit 0b454cb7a8
5 changed files with 219 additions and 23 deletions

View file

@ -659,8 +659,12 @@ impl BasisCache {
}
// refetch the basis here to avoid the re-borrow problem, now that all the potential dict cache mutations are done
let basis = &mut self.cache[basis_index];
basis.age = basis.age.saturating_add(1);
basis.clean = false;
// bumping this every key update affects performance *a lot* -- don't think this is worth it.
// the bases should only "age" when dicts or keys are modified, not when any data in it is updated for any reason.
// basis.age = basis.age.saturating_add(1);
// basis.clean = false;
// now do the sync
if let Some(dict_entry) = basis.dicts.get_mut(dict) {
let updated_ptr = dict_entry.key_update(hw, &mut basis.v2p_map,
@ -785,10 +789,9 @@ impl BasisCache {
policy: BasisRetentionPolicy) -> Option<BasisCacheEntry> {
let basis_key = hw.basis_derive_key(name, password);
if let Some(basis_map) = hw.pt_scan_key(&basis_key, name) {
let cipher = Aes256GcmSiv::new(Key::from_slice(&basis_key));
let aad = hw.data_aad(name);
if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) {
let vpage = match hw.data_decrypt_page(&cipher, &aad, root_page) {
let vpage = match hw.data_decrypt_page_with_commit(&basis_key, &aad, root_page) {
Some(data) => data,
None => {log::error!("Could not find basis {} root", name); return None;},
};
@ -856,12 +859,11 @@ impl BasisCache {
let slice_iter =
journal_bytes.iter() // journal rev
.chain(basis_root.as_ref().iter());
let mut block = [0 as u8; VPAGE_SIZE + size_of::<JournalType>()];
let mut block = [0 as u8; KCOM_CT_LEN];
for (&src, dst) in slice_iter.zip(block.iter_mut()) {
*dst = src;
}
let key = Key::clone_from_slice(&basis_key);
hw.data_encrypt_and_patch_page(&Aes256GcmSiv::new(&key), &aad, &mut block, &pp);
hw.data_encrypt_and_patch_page_with_commit(&basis_key, &aad, &mut block, &pp);
let cipher = Aes256::new(GenericArray::from_slice(&basis_key));
for (&virt, phys) in basis_v2p_map.iter_mut() {
@ -983,6 +985,8 @@ pub(crate) struct BasisCacheEntry {
pub cipher: Aes256GcmSiv,
/// derived cipher for encrypting PTEs -- cache it, so we can save the time cost of constructing the cipher key schedule
pub cipher_ecb: Aes256,
/// raw AES key -- needed because we have to use this to derive commitment keys for the basis root record, to work around AES-GCM-SIV salamanders
pub key: GenericArray<u8, cipher::consts::U32>,
/// the AAD associated with this Basis
pub aad: Vec::<u8>,
/// modification count
@ -1009,7 +1013,7 @@ impl BasisCacheEntry {
let aad = hw.data_aad(name);
// get the first page, where the basis root is guaranteed to be
if let Some(root_page) = basis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) {
let vpage = match hw.data_decrypt_page(&cipher, &aad, root_page) {
let vpage = match hw.data_decrypt_page_with_commit(key, &aad, root_page) {
Some(data) => data,
None => {log::error!("System basis decryption did not authenticate. Unrecoverable error."); return None;},
};
@ -1040,6 +1044,7 @@ impl BasisCacheEntry {
dicts: HashMap::<String, DictCacheEntry>::new(),
cipher,
cipher_ecb: Aes256::new(GenericArray::from_slice(key)),
key: GenericArray::clone_from_slice(key),
aad,
age: basis_root.age,
free_dict_offset: None,
@ -1492,14 +1497,14 @@ impl BasisCacheEntry {
let slice_iter =
journal_bytes.iter() // journal rev
.chain(basis_root.as_ref().iter());
let mut block = [0 as u8; VPAGE_SIZE + size_of::<JournalType>()];
let mut block = [0 as u8; KCOM_CT_LEN];
for (&src, dst) in (hw.trng_u32() % JOURNAL_RAND_RANGE).to_le_bytes().iter().zip(block[..size_of::<JournalType>()].iter_mut()) {
*dst = src;
}
for (&src, dst) in slice_iter.zip(block.iter_mut()) {
*dst = src;
}
hw.data_encrypt_and_patch_page(&self.cipher, &self.aad, &mut block, &pp);
hw.data_encrypt_and_patch_page_with_commit(self.key.as_slice(), &self.aad, &mut block, &pp);
self.clean = true;
}
}

View file

@ -94,6 +94,7 @@ impl HostedSpinor {
}
}
pub fn patch(&self, _region: &[u8], _region_base: u32, data: &[u8], offset: u32) -> Result<(), xous::Error> {
// println!("patch at {:x}+{}", offset, data.len());
for (&src, dst) in data.iter().zip(
flashmem().memory.as_mut_slice()[offset as usize..offset as usize + data.len()].iter_mut()
) {

View file

@ -33,6 +33,11 @@ pub const PAGE_SIZE: usize = spinor::SPINOR_ERASE_SIZE as usize;
/// size of a virtual page -- after the AES encryption and journaling overhead is subtracted
pub const VPAGE_SIZE: usize = PAGE_SIZE - size_of::<Nonce>() - size_of::<Tag>() - size_of::<JournalType>();
/// length of the ciphertext in an AES-GCM-SIV page with key commitments
/// equal to the total plaintext to be encrypted, including the journal number
/// does not include the MAC overhead
pub const KCOM_CT_LEN: usize = 4004;
const WRAPPED_AES_KEYSIZE: usize = AES_KEYSIZE + 8;
const SCD_VERSION: u32 = 1;
#[repr(C)] // this can map directly into Flash
@ -1113,6 +1118,89 @@ impl PddbOs {
}
}
}
/// returns a decrypted page that also encodes a key commitments. In this case, a raw key is passed,
/// instead of the generic AES-GCM-SIV cipher, because we need to derive the key commitment.
/// Key commitments are a patch to work-around the salamander problem in AES-GCM-SIV see https://eprint.iacr.org/2020/1456.pdf
///
/// The structure of a page with commit key storage is as follows:
/// - Nonce - 12 bytes
/// - ciphertext - 4004 bytes (includes the journal number)
/// - kcomm_nonce - 32 bytes
/// - kcomm - 32 bytes
/// - MAC - 16 bytes
/// We stripe the MAC at the end just in case the MAC has some arithmetic property that can betray the existence
/// of a basis root record with key commitment. The committed key and the nonce both should be indistinguishable
/// from ciphertext.
pub(crate) fn data_decrypt_page_with_commit(&mut self, key: &[u8], aad: &[u8], page: &PhysPage) -> Option<Vec::<u8>> {
const KCOM_NONCE_LEN: usize = 32;
const KCOM_LEN: usize = 32;
const MAC_LEN: usize = 16;
let ct_slice = &self.pddb_mr.as_slice()[
self.data_phys_base.as_usize() + page.page_number() as usize * PAGE_SIZE ..
self.data_phys_base.as_usize() + (page.page_number() as usize + 1) * PAGE_SIZE];
log::debug!("commit data at 0x{:x}", self.data_phys_base.as_usize() + page.page_number() as usize * PAGE_SIZE);
let nonce = &ct_slice[..size_of::<Nonce>()];
let ct_total = &ct_slice[size_of::<Nonce>()..];
// extract the regions of the stored data and place them into their respective buffers
let mut ct_plus_mac = [0u8; KCOM_CT_LEN + MAC_LEN];
let mut nonce_comm = [0u8; KCOM_NONCE_LEN];
let mut key_comm_stored = [0u8; KCOM_LEN];
let mut ct_pos = 0;
for (&src, dst) in ct_total[ct_pos..].iter().zip(ct_plus_mac[..KCOM_CT_LEN].iter_mut()) {
*dst = src;
ct_pos += 1;
}
for (&src, dst) in ct_total[ct_pos..].iter().zip(nonce_comm.iter_mut()) {
*dst = src;
ct_pos += 1;
}
for (&src, dst) in ct_total[ct_pos..].iter().zip(key_comm_stored.iter_mut()) {
*dst = src;
ct_pos += 1;
}
for (&src, dst) in ct_total[ct_pos..].iter().zip(ct_plus_mac[KCOM_CT_LEN..].iter_mut()) {
*dst = src;
ct_pos += 1;
}
assert!(ct_pos == PAGE_SIZE - size_of::<Nonce>(), "struct sizing error in unpacking page with key commit");
log::debug!("found nonce of {:x?}", nonce);
log::debug!("found kcom_nonce of {:x?}", nonce_comm);
let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &nonce_comm);
let cipher = Aes256GcmSiv::new(Key::from_slice(&kenc));
match cipher.decrypt(
Nonce::from_slice(nonce),
Payload {
aad,
msg: &ct_plus_mac,
}
) {
Ok(data) => {
assert!(data.len() == KCOM_CT_LEN, "authentication successful, but wrong amount of data was recovered");
if kcom == key_comm_stored {
Some(data)
} else {
log::debug!("Key commitment failed to match on a reputed basis root block");
None
}
},
Err(e) => {
// do the commit check again at the same point, even if there is an error, for some semblance of constant time
// (of course the error messages leak information, if they are forced on)
if kcom == key_comm_stored {
log::trace!("Error decrypting page: {:?}", e); // sometimes this is totally "normal", like when we're testing for valid data.
None
} else {
log::trace!("Error decrypting page + commitment error: {:?}", e);
None
}
}
}
}
/// `data` includes the journal entry on top. The data passed in must be exactly one vpage plus the journal entry
pub(crate) fn data_encrypt_and_patch_page(&mut self, cipher: &Aes256GcmSiv, aad: &[u8], data: &mut [u8], pp: &PhysPage) {
assert!(data.len() == VPAGE_SIZE + size_of::<JournalType>(), "did not get a page-sized region to patch");
@ -1129,6 +1217,74 @@ impl PddbOs {
self.patch_data(&[nonce.as_slice(), &ciphertext].concat(), pp.page_number() * PAGE_SIZE as u32);
}
/// `data` includes the journal entry on top.
/// The data passed in must be exactly one vpage plus the journal entry minus the length of the commit structure (64 bytes),
/// which is 4004 bytes total
pub(crate) fn data_encrypt_and_patch_page_with_commit(&mut self, key: &[u8], aad: &[u8], data: &mut [u8], pp: &PhysPage) {
assert!(data.len() == KCOM_CT_LEN, "did not get a key-commit sized region to patch");
// updates the journal type
let j = JournalType::from_le_bytes(data[..size_of::<JournalType>()].try_into().unwrap()).saturating_add(1);
for (&src, dst) in j.to_le_bytes().iter().zip(data[..size_of::<JournalType>()].iter_mut()) { *dst = src; }
// gets the AES-GCM-SIV nonce
let nonce = self.nonce_gen();
// makes a nonce for the key commit
let mut kcom_nonce = [0u8; 32];
self.trng_slice(&mut kcom_nonce);
// generates the encryption and commit keys
let (kenc, kcom) = self.kcom_func(key.try_into().unwrap(), &kcom_nonce);
let cipher = Aes256GcmSiv::new(Key::from_slice(&kenc));
let ciphertext = cipher.encrypt(
&nonce,
Payload {
aad,
msg: &data,
}
).expect("couldn't encrypt data");
let mut dest_page = [0u8; PAGE_SIZE];
let mut written = 0; // used as a sanity check on the insane iterator chain constructed below
for (&src, dst) in
nonce.as_slice().iter()
.chain(ciphertext[..KCOM_CT_LEN].iter())
.chain(kcom_nonce.iter())
.chain(kcom.iter())
.chain(ciphertext[KCOM_CT_LEN..].iter())
.zip(dest_page.iter_mut()) {
*dst = src;
written += 1;
}
assert!(written == 4096, "data sizing error in encryption with key commit");
log::trace!("nonce: {:x?}", &nonce);
log::info!("dest_page[kcom_nonce]: {:x?}", &dest_page[12+4004..12+4004+32]);
self.patch_data(&dest_page, pp.page_number() * PAGE_SIZE as u32);
}
/// Derive a key commitment. This takes in a base `key`, which is 256 bits;
/// a `nonce` which is the 96-bit nonce used in the AES-GCM-SIV for a given block;
/// and `nonce_com` which is the commitment nonce, set at 256 bits.
/// The result is two tuples, (kenc, kcom).
fn kcom_func(&self,
key: &[u8; 32],
nonce_com: &[u8; 32]) -> ([u8; 32], [u8; 32]) {
use sha2::{FallbackStrategy, Sha512Trunc256};
use digest::Digest;
let mut h_enc = Sha512Trunc256::new_with_strategy(FallbackStrategy::SoftwareOnly);
h_enc.update(key);
// per https://eprint.iacr.org/2020/1456.pdf Table 4 on page 13 Type I Lenc
h_enc.update([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x01]);
h_enc.update(nonce_com);
let k_enc = h_enc.finalize();
let mut h_com = Sha512Trunc256::new_with_strategy(FallbackStrategy::SoftwareOnly);
h_com.update(key);
// per https://eprint.iacr.org/2020/1456.pdf Table 4 on page 13 Type I Lcom. Note one-bit difference in last byte.
h_com.update([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x02]);
h_com.update(nonce_com);
let k_com = h_com.finalize();
(k_enc.into(), k_com.into())
}
/// Meant to be called on boot. This will read the FastSpace record, and then attempt to load
/// in the system basis.
pub(crate) fn pddb_mount(&mut self) -> Option<BasisCacheEntry> {
@ -1136,11 +1292,10 @@ impl PddbOs {
self.syskey_ensure();
if let Some(syskey) = self.system_basis_key {
if let Some(sysbasis_map) = self.pt_scan_key(&syskey, PDDB_DEFAULT_SYSTEM_BASIS) {
let cipher = Aes256GcmSiv::new(Key::from_slice(&syskey));
let aad = self.data_aad(PDDB_DEFAULT_SYSTEM_BASIS);
// get the first page, where the basis root is guaranteed to be
if let Some(root_page) = sysbasis_map.get(&VirtAddr::new(VPAGE_SIZE as u64).unwrap()) {
let vpage = match self.data_decrypt_page(&cipher, &aad, root_page) {
let vpage = match self.data_decrypt_page_with_commit(&syskey, &aad, root_page) {
Some(data) => data,
None => {log::error!("System basis decryption did not authenticate. Unrecoverable error."); return None;},
};
@ -1448,12 +1603,13 @@ impl PddbOs {
let slice_iter =
journal_bytes.iter() // journal rev
.chain(basis_root.as_ref().iter());
let mut block = [0 as u8; VPAGE_SIZE + size_of::<JournalType>()];
let mut block = [0 as u8; KCOM_CT_LEN];
for (&src, dst) in slice_iter.zip(block.iter_mut()) {
*dst = src;
}
let key = Key::clone_from_slice(self.system_basis_key.as_ref().unwrap());
self.data_encrypt_and_patch_page(&Aes256GcmSiv::new(&key), &aad, &mut block, &pp);
let syskey = self.system_basis_key.unwrap(); // take the key out
self.data_encrypt_and_patch_page_with_commit(&syskey, &aad, &mut block, &pp);
self.system_basis_key = Some(syskey); // put the key back
if let Some(modals) = progress {
modals.update_progress(66).expect("couldn't update progress bar");
self.tt.sleep_ms(100).unwrap();

View file

@ -111,6 +111,7 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas
let dict_start_index = dict_list.len();
for (evicted, evict_dict) in dict_list.iter().enumerate() {
if evicted < evict_count {
log::debug!("evicting dict {}", evict_dict);
match basis_cache.dict_remove(hw, evict_dict, basis_name, false) {
Ok(_) => {},
Err(e) => log::error!("Error evicting dictionary {}: {:?}", evict_dict, e),
@ -124,6 +125,7 @@ pub(crate) fn delete_add_dict_consistency(hw: &mut PddbOs, basis_cache: &mut Bas
for dictnum in 1..=evict_count {
let dictname = format!("dict{}", dictnum + dict_start_index);
let (keyname, keydata) = gen_key(&dictname, keynum, key_lower_bound, key_upper_bound);
log::debug!("adding {}:{}", dictname, keyname);
// now we're ready to write it out
if maybe_extra_reserved.is_none() {
// we do this slightly funky way instead of just passing Some(0) because we want to test the "None" path explicitly
@ -169,6 +171,7 @@ pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache,
#[cfg(not(feature = "deterministic"))]
let key_list = key_list_unord;
for key in key_list.iter() {
log::debug!("updating {}:{}", dict, key);
// this actually does something a bit more complicated on a multi-basis system than you'd think:
// it will get the union of all key names, and then patch the *latest open basis* only with new data.
// note: if the key didn't already exist in the latest open basis, it's added, with just that patch data in it.
@ -182,14 +185,11 @@ pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache,
}
}
log::debug!("bef readback");
// now fix the CI checksum. structured as two separate patches. not because it's efficient,
// but because it exercises the code harder.
let mut patchbuf = [0u8; UPPER_BOUND];
let readlen = basis_cache.key_read(hw, dict, key, &mut patchbuf, Some(0), None).unwrap();
log::debug!("bef no extend");
if !extend {
log::debug!("bef no extend");
// now re-compute the checksum
let mut checkdata = Vec::<u8>::new();
for &b in &patchbuf[..readlen-4] {
@ -202,7 +202,6 @@ pub(crate) fn patch_test(hw: &mut PddbOs, basis_cache: &mut BasisCache,
let checksum = murmur3_32(&checkdata, 0);
basis_cache.key_update(hw, dict, key, &checksum.to_le_bytes(), Some(readlen-4), None, None, false)?;
} else {
log::debug!("extend");
// this gloms a new checksum onto the existing record, adding 4 new bytes.
let mut checkdata = Vec::<u8>::new();
for &b in &patchbuf[..readlen] {
@ -408,7 +407,6 @@ pub(crate) fn ci_tests(pddb_os: &mut PddbOs) -> Result<()> {
None, None, Some(32))?;
log::info!("Saving `basecase1e` to local host");
pddb_os.dbg_dump(Some("basecase1e".to_string()), None);
let extra_basis_key = pddb_os.basis_derive_key(EXTRA_BASIS, EXTRA_BASIS_PW);
let mut name = [0 as u8; 64];
for (&src, dst) in EXTRA_BASIS.as_bytes().iter().zip(name.iter_mut()) {

View file

@ -4,6 +4,8 @@ import sys
from Crypto.Cipher import AES
from rfc8452 import AES_GCM_SIV
from Crypto.Hash import SHA512
import binascii
import logging
@ -99,6 +101,41 @@ def mm3_hash( key, seed = 0x0 ):
else:
return -( (unsigned_val ^ 0xFFFFFFFF) + 1 )
def keycommit_decrypt(key, aad, pp_data):
# print([hex(d) for d in pp_data[:32]])
nonce = pp_data[:12]
ct = pp_data[12:12+4004]
kcom_nonce = pp_data[12+4004:12+4004+32]
kcom = pp_data[12+4004+32:12+4004+32+32]
mac = pp_data[-16:]
#print([hex(d) for d in kcom_nonce])
# print([hex(d) for d in kcom])
#h_test = SHA512.new(truncate="256") # just something to make sure our hash functions are sane
#h_test.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x01]))
#print(h_test.hexdigest())
h_enc = SHA512.new(truncate="256")
h_enc.update(key)
h_enc.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x01]))
h_enc.update(kcom_nonce)
k_enc = h_enc.digest()
h_com = SHA512.new(truncate="256")
h_com.update(key)
h_com.update(bytes([0x43, 0x6f, 0x6, 0xd6, 0xd, 0x69, 0x74, 0x01, 0x02]))
h_com.update(kcom_nonce)
k_com_derived = h_com.digest()
print('kcom_stored: ' + binascii.hexlify(kcom).decode('utf-8'))
print('kcom_derived: ' + binascii.hexlify(k_com_derived).decode('utf-8'))
cipher = AES_GCM_SIV(k_enc, nonce)
pt_data = cipher.decrypt(ct + mac, aad)
if k_com_derived != kcom:
print("basis failed key commit test")
raise Exception(ValueError)
return pt_data
def main():
parser = argparse.ArgumentParser(description="Debug PDDB Images")
@ -191,11 +228,10 @@ def main():
basis_data = bytearray()
pp_start = v2p_table[VPAGE_SIZE]
# print("pp_start: {}, vp: {}".format(pp_start, vp))
# print("pp_start: {:x}".format(pp_start))
pp_data = data[pp_start:pp_start + PAGE_SIZE]
try:
cipher = AES_GCM_SIV(key, pp_data[:12])
pt_data = cipher.decrypt(pp_data[12:], basis_aad(name))
pt_data = keycommit_decrypt(key, basis_aad(name), pp_data)
basis_data.extend(bytearray(pt_data))
logging.debug("decrypted vpage @ {:x} ppage @ {:x}".format(VPAGE_SIZE, v2p_table[VPAGE_SIZE]))
# print([hex(x) for x in basis_data[:256]])