xous-core/services/pddb/src/backend/hosted.rs
bunnie 0b454cb7a8 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
2022-02-28 03:13:26 +08:00

111 lines
No EOL
3.5 KiB
Rust

#![allow(dead_code)]
use crate::api::*;
use std::sync::Once;
use std::mem::MaybeUninit;
use std::fs::File;
use std::io::prelude::*;
// This is considered bad practice for Rust to use a global singleton.
// However, this hack puts the burden of emulation on the emulator, while
// keeping the production code clean (otherwise we'd have large sections
// of production code with #cfg regions to deal with lifetime differences).
// Besides, in reality, FLASH memory is a static, globally mutable pool of data.
//
// Note that this is a concurrently accessed, unsafe, unchecked vector.
struct FlashSingleton {
memory: Vec::<u8>,
}
fn flashmem() -> &'static mut FlashSingleton {
static mut SINGLETON: MaybeUninit<FlashSingleton> = MaybeUninit::uninit();
static ONCE: Once = Once::new();
unsafe {
ONCE.call_once(|| {
let mut memory = Vec::<u8>::with_capacity(PDDB_A_LEN);
for _ in 0..PDDB_A_LEN {
memory.push(0xFF);
}
let flashmem = FlashSingleton {
memory,
};
SINGLETON.write(flashmem);
});
SINGLETON.assume_init_mut()
}
}
#[derive(Copy, Clone)]
pub struct KeyExport {
pub basis_name: [u8; 64],
pub key: [u8; 32],
}
pub struct EmuStorage {
}
impl EmuStorage {
pub fn new() -> Self {
EmuStorage {
}
}
pub fn as_slice<T>(&self) -> &[T] {
unsafe {
core::slice::from_raw_parts(
flashmem().memory.as_ptr() as *const T,
flashmem().memory.len() / core::mem::size_of::<T>(),
)
}
}
pub fn as_mut_slice(&mut self) -> &mut [u8] {
flashmem().memory.as_mut_slice()
}
/// used to reset the storage for repeated test case generation
pub fn reset(&mut self) {
for b in flashmem().memory.as_mut_slice() {
*b = 0xFF;
}
}
pub fn dump_fs(&self, name: &Option<String>) {
let defaultname = String::from("pddb");
let rootname = name.as_ref().unwrap_or(&defaultname);
let mut f = File::create(format!("../tools/pddb-images/{}.bin", rootname)).unwrap();
f.write_all(flashmem().memory.as_slice()).unwrap();
f.flush().unwrap();
}
pub fn dump_keys(&self, known_keys: &[KeyExport], name: &Option<String>) {
let defaultname = String::from("pddb");
let rootname = name.as_ref().unwrap_or(&defaultname);
let mut f = File::create(format!("../tools/pddb-images/{}.key", rootname)).unwrap();
f.write_all(&(known_keys.len() as u32).to_le_bytes()).unwrap();
for key in known_keys {
f.write_all(&key.basis_name).unwrap();
f.write_all(&key.key).unwrap();
}
f.flush().unwrap();
}
}
pub struct HostedSpinor {
}
impl HostedSpinor {
pub fn new() -> Self {
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()
) {
*dst = src;
}
Ok(())
}
pub fn bulk_erase(&self, start: u32, len: u32) -> Result<(), xous::Error> {
for b in flashmem().memory.as_mut_slice()[(start - xous::PDDB_LOC) as usize .. (start - xous::PDDB_LOC + len) as usize].iter_mut() {
*b = 0xFF;
}
Ok(())
}
}