add a check that the detached signature metadata matches signed data

the unsigned metadata in the signature header is used to compute
the signature, which contains a copy of the key metadata. We forgot
to make sure these match, which means the signed copy was worthless.

Until now.
This commit is contained in:
bunnie 2021-11-02 03:16:40 +08:00
parent edb43a17a3
commit 8d501cfcc6

View file

@ -339,13 +339,28 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
println!("Public key bytes: {:x?}", pubkey.as_bytes());
if sig.version != 1 {
println!("Warning: mismatch on signature record version numbering. Ignoring and moving on...")
}
let signed_len = sig.signed_len;
let image: &[u8] = unsafe{core::slice::from_raw_parts((xous_img_offset as usize + SIGBLOCK_SIZE) as *const u8, signed_len as usize)};
let ed25519_signature = ed25519_dalek::Signature::from(sig.signature);
let image: &[u8] = unsafe{core::slice::from_raw_parts(
(xous_img_offset as usize + SIGBLOCK_SIZE) as *const u8,
signed_len as usize)};
// extract the version and length from the signed region
use core::convert::TryInto;
let protected_version = u32::from_le_bytes(image[signed_len as usize - 8 .. signed_len as usize - 4].try_into().unwrap());
let protected_len = u32::from_le_bytes(image[signed_len as usize - 4 ..].try_into().unwrap());
// check that the signed versions match the version reported in the header
if sig.version != 1 || (sig.version != protected_version) {
gfx.msg("Check fail: mismatch on signature record version numbering.\n\r", &mut cursor);
println!("Check fail: mismatch on signature record version numbering.\n\r");
die();
}
if protected_len != signed_len - 4 {
gfx.msg("Check fail: mismatch on header length vs protected length.\n\r", &mut cursor);
println!("Check fail: mismatch on header length vs protected length.\n\r");
die();
}
let ed25519_signature = ed25519_dalek::Signature::from(sig.signature);
use ed25519_dalek::Verifier;
if pubkey.verify(image, &ed25519_signature).is_ok() {
gfx.msg("Signature check passed\n\r", &mut cursor);
@ -367,25 +382,7 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
// we're out of keys, display a message and try to power down
gfx.msg("Signature check failed, powering down\n\r", &mut cursor);
println!("Signature check failed");
let ticktimer = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
let mut power = CSR::new(utra::power::HW_POWER_BASE as *mut u32);
let mut com = CSR::new(utra::com::HW_COM_BASE as *mut u32);
let mut start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
loop {
// every 15 seconds, attempt to send a power down command
// any attempt to re-flash the system must halt the CPU before we time-out to this point!
if ticktimer.rf(utra::ticktimer::TIME0_TIME) - start > 15_000 {
println!("Powering down...");
power.rmwf(utra::power::POWER_STATE, 0);
power.rmwf(utra::power::POWER_SELF, 0);
// ship mode is the safest mode -- suitable for long-term storage (~years)
com.wfo(utra::com::TX_TX, com_rs::ComState::POWER_SHIPMODE.verb as u32);
while com.rf(utra::com::STATUS_TIP) == 1 {}
let _ = com.rf(utra::com::RX_RX); // discard the RX result
start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
}
}
die();
}
}
}
@ -416,3 +413,25 @@ pub fn validate_xous_img(xous_img_offset: *const u32) -> bool {
true
}
fn die() {
let ticktimer = CSR::new(utra::ticktimer::HW_TICKTIMER_BASE as *mut u32);
let mut power = CSR::new(utra::power::HW_POWER_BASE as *mut u32);
let mut com = CSR::new(utra::com::HW_COM_BASE as *mut u32);
let mut start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
loop {
// every 15 seconds, attempt to send a power down command
// any attempt to re-flash the system must halt the CPU before we time-out to this point!
if ticktimer.rf(utra::ticktimer::TIME0_TIME) - start > 15_000 {
println!("Powering down...");
power.rmwf(utra::power::POWER_STATE, 0);
power.rmwf(utra::power::POWER_SELF, 0);
// ship mode is the safest mode -- suitable for long-term storage (~years)
com.wfo(utra::com::TX_TX, com_rs::ComState::POWER_SHIPMODE.verb as u32);
while com.rf(utra::com::STATUS_TIP) == 1 {}
let _ = com.rf(utra::com::RX_RX); // discard the RX result
start = ticktimer.rf(utra::ticktimer::TIME0_TIME);
}
}
}