mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
add hash protection on the metadata header
stage is set for hash protection on the PDDB data itself. that comes next.
This commit is contained in:
parent
c1eed3e7ad
commit
a416069002
6 changed files with 168 additions and 16 deletions
|
|
@ -256,7 +256,7 @@ impl Default for BackupHeaderIpc {
|
|||
}
|
||||
}
|
||||
|
||||
pub const BACKUP_VERSION: u32 = 0x00_01_00_00;
|
||||
pub const BACKUP_VERSION: u32 = 0x00_01_00_01;
|
||||
|
||||
#[repr(u32)]
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
||||
|
|
@ -359,6 +359,21 @@ impl From::<[u8; 4]> for BackupKeyboardLayout {
|
|||
}
|
||||
}
|
||||
|
||||
pub const HEADER_TOTAL_SIZE: u32 = 4096;
|
||||
/// Size of a checksummed block in pages. 0x100 = 256 pages,
|
||||
/// or 1 MiB for a checksummed block. This is specified in 4kiB pages
|
||||
/// because it really doesn't make sense to checksum anything smaller
|
||||
/// than that, and it allows us to grow the size of a single checksummed
|
||||
/// block to well over 4GiB.
|
||||
pub const CHECKSUM_BLOCKLEN_PAGE: u32 = 0x100;
|
||||
/// The total number of checksums required to cover the length of the PDDB
|
||||
/// divided by the length of a checksummed page. This number does not count
|
||||
/// the single additional checksum that is included to check the integrity of the
|
||||
/// plaintext + ciphertext header itself.
|
||||
///
|
||||
/// Total number of checksums has to divide evenly into the size of the PDDB region
|
||||
pub const TOTAL_CHECKSUMS: u32 = xous::PDDB_LEN / CHECKSUM_BLOCKLEN_PAGE;
|
||||
|
||||
#[repr(C, align(8))]
|
||||
#[derive(Copy, Clone, Debug)]
|
||||
pub struct BackupHeader {
|
||||
|
|
@ -372,11 +387,26 @@ pub struct BackupHeader {
|
|||
pub language: [u8; 4],
|
||||
pub kbd_layout: [u8; 4],
|
||||
pub dna: [u8; 8],
|
||||
pub _reserved: [u8; 48],
|
||||
/// Length of a checksummed region, in 4kiB pages
|
||||
pub checksum_len_page: [u8; 4],
|
||||
/// Number of checksummed pages
|
||||
///
|
||||
/// The location of the first header is computed as follows:
|
||||
/// `header_start_address + header_total_size - (total_checksums) * 16`
|
||||
/// The backup data starts at `header_start_address + header_total_size`
|
||||
///
|
||||
/// In other words, checksums are aligned to the highest address in the header
|
||||
/// while the header plaintext+ciphertext data is aligned to the lowest address
|
||||
/// in the header region.
|
||||
pub total_checksums: [u8; 4],
|
||||
/// Total size of the header in bytes, including unused space
|
||||
pub header_total_size: [u8; 4],
|
||||
pub _reserved: [u8; 36],
|
||||
pub op: BackupOp,
|
||||
}
|
||||
impl Default for BackupHeader {
|
||||
fn default() -> Self {
|
||||
assert!(xous::PDDB_LEN & ((CHECKSUM_BLOCKLEN_PAGE * 0x1000) - 1) == 0, "PDDB_LEN is not an integer multiple of CHECKSUM_LEN_PAGE");
|
||||
BackupHeader {
|
||||
version: BACKUP_VERSION,
|
||||
xous_ver: [0u8; 16],
|
||||
|
|
@ -387,7 +417,10 @@ impl Default for BackupHeader {
|
|||
language: BackupLanguage::default().into(), // this is "correct by default"
|
||||
kbd_layout: BackupKeyboardLayout::default().into(), // this has to be adjusted
|
||||
dna: [0u8; 8],
|
||||
_reserved: [0u8; 48],
|
||||
checksum_len_page: CHECKSUM_BLOCKLEN_PAGE.to_le_bytes(),
|
||||
total_checksums: TOTAL_CHECKSUMS.to_le_bytes(),
|
||||
header_total_size: HEADER_TOTAL_SIZE.to_le_bytes(),
|
||||
_reserved: [0u8; 36],
|
||||
op: BackupOp::Archive,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@ impl Default for KeyRomExport {
|
|||
}
|
||||
}
|
||||
|
||||
/// This is the plaintext portion of the backup header
|
||||
#[derive(Zeroize)]
|
||||
#[zeroize(drop)]
|
||||
#[repr(C, align(8))]
|
||||
|
|
@ -62,6 +63,9 @@ impl DerefMut for BackupDataPt {
|
|||
}
|
||||
}
|
||||
|
||||
/// This is the ciphertext portion of the backup header. Note that there is an additional
|
||||
/// 32-byte section that is a SHA512/256 hash of the pt+ct region, appended directly to
|
||||
/// the end of the ct region.
|
||||
#[repr(C, align(8))]
|
||||
pub(crate) struct BackupDataCt {
|
||||
pub nonce: [u8; 12],
|
||||
|
|
@ -97,6 +101,52 @@ impl DerefMut for BackupDataCt {
|
|||
}
|
||||
}
|
||||
|
||||
/// The Checksums structure is an array of 16-byte (128-bit) checksums that
|
||||
/// are applied to backed up data. There is one checksum per block region
|
||||
/// (currently set to 1MiB).
|
||||
///
|
||||
/// The actual checksum is a SHA512 of the region, but with only the first
|
||||
/// 128 bits stored. The goal of the checksum isn't a cryptographic tamper
|
||||
/// proofing -- this is already handled by the underlying AEAD's that are
|
||||
/// applied to the PDDB. The utility of the checksum is to detect media
|
||||
/// or transmission errors in storage, without having to fully decrypt
|
||||
/// the entire PDDB to look for corruption. We use SHA512 and truncate it to
|
||||
/// 128 bits not because it's the optimal hash, but because it's what we
|
||||
/// have a hardware accelerator for. We don't truncate to 256 bits because
|
||||
/// we're trying to pack as many checksums into a 4k header, and going to
|
||||
/// 128 bits is still "strong" for a checksum and allows us to get the region
|
||||
/// size down to 1MiB, which is a reasonably sized region for a retry-download
|
||||
/// in case a checksum error is found.
|
||||
#[repr(C, align(8))]
|
||||
pub(crate) struct Checksums {
|
||||
pub checksums: [[u8; 16]; crate::api::TOTAL_CHECKSUMS as usize],
|
||||
}
|
||||
impl Default for Checksums {
|
||||
fn default() -> Self {
|
||||
Checksums {
|
||||
checksums: [[0u8; 16]; crate::api::TOTAL_CHECKSUMS as usize],
|
||||
}
|
||||
}
|
||||
}
|
||||
impl Deref for Checksums {
|
||||
type Target = [u8];
|
||||
fn deref(&self) -> &[u8] {
|
||||
unsafe {
|
||||
core::slice::from_raw_parts(self as *const Checksums as *const u8, size_of::<Checksums>())
|
||||
as &[u8]
|
||||
}
|
||||
}
|
||||
}
|
||||
impl DerefMut for Checksums {
|
||||
fn deref_mut(&mut self) -> &mut [u8] {
|
||||
unsafe {
|
||||
core::slice::from_raw_parts_mut(self as *mut Checksums as *mut u8, size_of::<Checksums>())
|
||||
as &mut [u8]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/// 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.
|
||||
|
|
|
|||
|
|
@ -2729,9 +2729,19 @@ impl<'a> RootKeys {
|
|||
header.op = BackupOp::Backup; // set the "we're backing up" flag
|
||||
|
||||
// condense the data into a single block, to reduce read/write cycles on the block
|
||||
let mut block = [0u8; size_of::<BackupHeader>() + size_of::<backups::BackupDataCt>()];
|
||||
const HASH_LEN: usize = 32;
|
||||
let mut block = [0u8; size_of::<BackupHeader>() + size_of::<backups::BackupDataCt>() + HASH_LEN];
|
||||
block[..size_of::<BackupHeader>()].copy_from_slice(header.as_ref());
|
||||
block[size_of::<BackupHeader>()..].copy_from_slice(backup_ct.as_ref());
|
||||
block[size_of::<BackupHeader>()..size_of::<BackupHeader>() + size_of::<backups::BackupDataCt>()].copy_from_slice(backup_ct.as_ref());
|
||||
|
||||
// compute a hash of the data, for fast verification of the backup header integrity to detect media errors, etc.
|
||||
let mut hasher = Sha512Trunc256::new_with_strategy(FallbackStrategy::HardwareThenSoftware);
|
||||
hasher.update(&block[..size_of::<BackupHeader>() + size_of::<backups::BackupDataCt>()]);
|
||||
let digest = hasher.finalize();
|
||||
assert!(digest.len() == HASH_LEN, "Wrong length hash selected! Check your code.");
|
||||
block[size_of::<BackupHeader>() + size_of::<backups::BackupDataCt>()..]
|
||||
.copy_from_slice(digest.as_slice());
|
||||
|
||||
self.spinor.patch(
|
||||
self.kernel(),
|
||||
self.kernel_base(),
|
||||
|
|
|
|||
|
|
@ -1061,6 +1061,13 @@ fn wrapped_main() -> ! {
|
|||
// sync the PDDB to disk prior to making backups
|
||||
let pddb = pddb::Pddb::new();
|
||||
pddb.sync().expect("couldn't synchronize PDDB to disk");
|
||||
|
||||
// TODO: add PDDB hash map computations here + UX flow to indicate pause
|
||||
|
||||
// TODO: add a call to PDDB that blocks the PDDB forever. The system is rebooted
|
||||
// after the backup is done. However, this call ensures that the PDDB does not
|
||||
// get updated mid-way through a backup
|
||||
|
||||
let mut metadata = root_keys::api::BackupHeader::default();
|
||||
// note: default() should set the language correctly by default since it's a systemwide constant
|
||||
metadata.timestamp = localtime.get_local_time_ms().unwrap_or(0);
|
||||
|
|
|
|||
|
|
@ -112,6 +112,7 @@ def main():
|
|||
TAG_LEN = 16
|
||||
COMMIT_NONCE_LEN = 32
|
||||
COMMIT_LEN = 32
|
||||
CHECKSUM_LEN = 32
|
||||
offset = PT_HEADER_LEN
|
||||
nonce = backup[offset:offset+NONCE_LEN]
|
||||
offset += NONCE_LEN
|
||||
|
|
@ -122,6 +123,9 @@ def main():
|
|||
commit_nonce = backup[offset:offset+COMMIT_NONCE_LEN]
|
||||
offset += COMMIT_NONCE_LEN
|
||||
commit = backup[offset:offset+COMMIT_LEN]
|
||||
offset += COMMIT_LEN
|
||||
check_region = backup[:offset]
|
||||
checksum = backup[offset:offset+CHECKSUM_LEN]
|
||||
|
||||
h_enc = SHA512.new(truncate="256")
|
||||
h_enc.update(key)
|
||||
|
|
@ -149,7 +153,8 @@ def main():
|
|||
exit(1)
|
||||
|
||||
i = 0
|
||||
logging.info("Backup version: 0x{:08x}".format(int.from_bytes(pt_data[i:i+4], 'little')))
|
||||
backup_version = int.from_bytes(pt_data[i:i+4], 'little')
|
||||
logging.info("Backup version: 0x{:08x}".format(backup_version))
|
||||
i += 4
|
||||
logging.info("Xous version: {}".format(bytes_to_semverstr(pt_data[i:i+16])))
|
||||
i += 16
|
||||
|
|
@ -171,11 +176,29 @@ def main():
|
|||
dna = pt_data[i:i+8]
|
||||
dna_int = int.from_bytes(dna, 'little')
|
||||
i += 8
|
||||
i += 48 # reserved
|
||||
checksum_region_len = int.from_bytes(pt_data[i:i+4], 'little') * 4096
|
||||
logging.info("Checksum region length: 0x{:x}".format(checksum_region_len))
|
||||
i += 4
|
||||
total_checksums = int.from_bytes(pt_data[i:i+4], 'little')
|
||||
logging.info("Number of checksums, including the header checksum itself: {}".format(total_checksums))
|
||||
i += 4
|
||||
header_total_size = int.from_bytes(pt_data[i:i+4], 'little')
|
||||
logging.info("Header total length in bytes: {}".format(header_total_size))
|
||||
i += 36 # reserved
|
||||
op = int.from_bytes(pt_data[i:i+4], 'little')
|
||||
logging.info("Stored Backup Opcode: {}".format(op))
|
||||
i += 8 # padding because align=8
|
||||
|
||||
if backup_version == 0x10001:
|
||||
logging.info("Doing hash verification of pt+ct metadata")
|
||||
hasher = SHA512.new(truncate="256")
|
||||
hasher.update(check_region)
|
||||
computed_checksum = hasher.digest()
|
||||
if computed_checksum != checksum:
|
||||
print("Header failed hash integrity check!")
|
||||
|
||||
# TODO: sector checks
|
||||
|
||||
keyrom = pt_data[i:i+1024]
|
||||
user_key_enc = get_key(40, keyrom, 32)
|
||||
pepper = get_key(248, keyrom, 16)
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ import csv
|
|||
import urllib.request
|
||||
from datetime import datetime
|
||||
from datetime import date
|
||||
from Crypto.Hash import SHA512
|
||||
|
||||
from progressbar.bar import ProgressBar
|
||||
|
||||
|
|
@ -457,8 +458,9 @@ class SemVer:
|
|||
|
||||
def check_header(backup):
|
||||
i = 0
|
||||
print("Backup protocol version: 0x{:08x}".format(int.from_bytes(backup[i:i+4], 'little')))
|
||||
if int.from_bytes(backup[i:i+4], 'little') != 0x00010000:
|
||||
backup_version = int.from_bytes(backup[i:i+4], 'little')
|
||||
print("Backup protocol version: 0x{:08x}".format(backup_version))
|
||||
if backup_version != 0x00010000 and backup_version != 0x00010001:
|
||||
print("Backup protocol version is not correct.")
|
||||
return False
|
||||
i += 4
|
||||
|
|
@ -475,12 +477,11 @@ def check_header(backup):
|
|||
ts = int.from_bytes(backup[i:i+8], 'little') / 1000
|
||||
print("Timestamp: {} / {}".format(ts, datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')))
|
||||
if (datetime.utcfromtimestamp(ts) - datetime.now()).total_seconds() > 600:
|
||||
print("Backup timestamp is in the future. Is the RTC and timezone set correctly on the device?")
|
||||
print("Note: UTC time of the host must not be more than 10 minutes ahead of the device")
|
||||
return False
|
||||
print("WARNING: Backup timestamp is in the future. Is the RTC and timezone set correctly on the device?")
|
||||
# return False # non-fatal, because we have hash checks now
|
||||
if datetime.utcfromtimestamp(ts).year < 2021:
|
||||
print("Backup timestamp is from before 2021. Is the RTC and timezone set correctly on the device?")
|
||||
return False
|
||||
print("WARNING: Backup timestamp is from before 2021. Is the RTC and timezone set correctly on the device?")
|
||||
# return False
|
||||
i += 8
|
||||
lcode = int.from_bytes(backup[i:i+4], 'little')
|
||||
print("Language code: {}".format(lcode))
|
||||
|
|
@ -494,8 +495,15 @@ def check_header(backup):
|
|||
i += 4
|
||||
print("DNA: 0x{:x}".format(int.from_bytes(backup[i:i+8], 'little')))
|
||||
i += 8
|
||||
i += 48 # reserved
|
||||
|
||||
checksum_region_len = int.from_bytes(backup[i:i+4], 'little') * 4096
|
||||
print("Checksum region length: 0x{:x}".format(checksum_region_len))
|
||||
i += 4
|
||||
total_checksums = int.from_bytes(backup[i:i+4], 'little') + 1
|
||||
print("Number of checksums, including the header checksum itself: {}".format(total_checksums))
|
||||
i += 4
|
||||
header_total_size = int.from_bytes(backup[i:i+4], 'little')
|
||||
print("Header total length in bytes: {}".format(header_total_size))
|
||||
i += 36 # reserved
|
||||
backup[i:i+4] = (2).to_bytes(4, 'little')
|
||||
op = int.from_bytes(backup[i:i+4], 'little')
|
||||
print("Opcode (should be 2, to trigger the next phase of restore): {}".format(op))
|
||||
|
|
@ -503,6 +511,27 @@ def check_header(backup):
|
|||
print("Opcode is incorrect.")
|
||||
return False
|
||||
|
||||
if backup_version == 0x10001:
|
||||
print("Checksums found in backup image, checking...")
|
||||
PT_HEADER_LEN = 4 + 16 * 4 + 4 + 8 + 64 + 4 + 4
|
||||
NONCE_LEN = 12
|
||||
CT_LEN = PT_HEADER_LEN + 1024 + 64
|
||||
TAG_LEN = 16
|
||||
COMMIT_NONCE_LEN = 32
|
||||
COMMIT_LEN = 32
|
||||
CHECKSUM_LEN = 32
|
||||
checksum_loc = PT_HEADER_LEN + NONCE_LEN + CT_LEN + TAG_LEN + COMMIT_NONCE_LEN + COMMIT_LEN
|
||||
check_region = backup[:checksum_loc]
|
||||
checksum = backup[checksum_loc:checksum_loc + CHECKSUM_LEN]
|
||||
hasher = SHA512.new(truncate="256")
|
||||
hasher.update(check_region)
|
||||
computed_checksum = hasher.digest()
|
||||
if computed_checksum != checksum:
|
||||
print("Header failed hash integrity check!")
|
||||
return False
|
||||
|
||||
# TODO: checksums on data region
|
||||
|
||||
print("Backup header passes sanity check!")
|
||||
return True
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue