mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
revise auth_data handling for key header block
auth_data actually changes with each signing operation (ostensibly to prevent replay of signatures coming from tokens) so it has to be presented as data parallel to the signature, instead of as a static filed baked into the key header. This commit revises the header record to reflect this organizatio of data and also revises the signature check to be in-line with the protocol.
This commit is contained in:
parent
7d6630b7ec
commit
3b4e035a4f
8 changed files with 87 additions and 99 deletions
|
|
@ -96,3 +96,5 @@ pub const BOOT1_REVOCATION_OFFSET: usize = LOADER_REVOCATION_OFFSET + PUBKEY_SLO
|
|||
/// Offset in the one-way counter array for boot0 key revocations. Provisions for up to four
|
||||
/// key slots, from [124..=127].
|
||||
pub const BOOT0_REVOCATION_OFFSET: usize = BOOT1_REVOCATION_OFFSET + PUBKEY_SLOTS;
|
||||
|
||||
// slots from 128..=255 are totally unused by the boot logic
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
/// ===== PLACEHOLDER - bao2 signing HSM ceremony pending ======
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::signatures::Pubkey;
|
||||
|
||||
/// Bao1 public key. Will be extracted from signing/bao1_id_ed25519_sk using extract_sk_credential.py
|
||||
pub const ID_ED25519_SK_PUB: [u8; 32] = [0u8; 32];
|
||||
|
||||
/// Bao1 public key auth_data
|
||||
pub const ID_ED25519_SK_AUTH_DATA: [u8; 37] = [0u8; 37];
|
||||
/// Bao1 public key "key handle"
|
||||
pub const ID_ED25519_SK_CRED_ID: [u8; 241] = [0u8; 241];
|
||||
/// Bao1 public key "relying party"
|
||||
|
|
@ -15,7 +14,5 @@ pub const ID_ED25519_SK_RP: &'static str = "ssh:";
|
|||
pub const PUBKEY: Pubkey = Pubkey {
|
||||
pk: ID_ED25519_SK_PUB,
|
||||
// Placeholder: tag is 0.
|
||||
tag: [0u8; 4], // *KEYSLOT_INITIAL_TAGS[super::BAO1_KEY_SLOT],
|
||||
aad_len: ID_ED25519_SK_AUTH_DATA.len() as u8,
|
||||
aad: pad_array(&ID_ED25519_SK_AUTH_DATA),
|
||||
tag: [0u8; 4], // *KEYSLOT_INITIAL_TAGS[BAO1_KEY_SLOT],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
/// ===== PLACEHOLDER - bao2 signing HSM ceremony pending ======
|
||||
#[allow(unused_imports)]
|
||||
use super::*;
|
||||
use crate::signatures::Pubkey;
|
||||
|
||||
/// Bao1 public key. Will be extracted from signing/bao1_id_ed25519_sk using extract_sk_credential.py
|
||||
pub const ID_ED25519_SK_PUB: [u8; 32] = [0u8; 32];
|
||||
|
||||
/// Bao1 public key auth_data
|
||||
pub const ID_ED25519_SK_AUTH_DATA: [u8; 37] = [0u8; 37];
|
||||
/// Bao1 public key "key handle"
|
||||
pub const ID_ED25519_SK_CRED_ID: [u8; 241] = [0u8; 241];
|
||||
/// Bao1 public key "relying party"
|
||||
|
|
@ -15,7 +14,5 @@ pub const ID_ED25519_SK_RP: &'static str = "ssh:";
|
|||
pub const PUBKEY: Pubkey = Pubkey {
|
||||
pk: ID_ED25519_SK_PUB,
|
||||
// Placeholder: tag is 0.
|
||||
tag: [0u8; 4], // *KEYSLOT_INITIAL_TAGS[super::BAO2_KEY_SLOT],
|
||||
aad_len: ID_ED25519_SK_AUTH_DATA.len() as u8,
|
||||
aad: pad_array(&ID_ED25519_SK_AUTH_DATA),
|
||||
tag: [0u8; 4], // *KEYSLOT_INITIAL_TAGS[BAO2_KEY_SLOT],
|
||||
};
|
||||
|
|
|
|||
|
|
@ -6,14 +6,7 @@ pub const ID_ED25519_SK_PUB: [u8; 32] = [
|
|||
0x80, 0x97, 0x99, 0x29, 0xed, 0xd0, 0x4e, 0x40, 0x12, 0x4b, 0x52, 0xca, 0xe9, 0xae, 0x54, 0xb2, 0x4b,
|
||||
0xdf, 0xf7, 0x2a, 0x7b, 0x8a, 0x00, 0x4c, 0x41, 0x06, 0x5b, 0xd1, 0x40, 0x20, 0x78, 0xa7,
|
||||
];
|
||||
/// Beta public key auth_data - needed to validate an image. Extracted by a trial run of
|
||||
/// signing/fido_signer with `cargo run -- --credential-file ..\credentials\beta.json --file .\test.bin`
|
||||
/// inside the fido_signer directory. Needs to have the token & key connected to generate this.
|
||||
pub const ID_ED25519_SK_AUTH_DATA: [u8; 37] = [
|
||||
0xe3, 0x06, 0x10, 0xe8, 0xa1, 0x62, 0x11, 0x59, 0x60, 0xfe, 0x1e, 0xc2, 0x23, 0xe6, 0x52, 0x9c, 0x9f,
|
||||
0x4b, 0x6e, 0x80, 0x20, 0x0d, 0xcb, 0x5e, 0x5c, 0x32, 0x1c, 0x8a, 0xf1, 0xe2, 0xb1, 0xbf, 0x05, 0x00,
|
||||
0x00, 0x02, 0x0f,
|
||||
];
|
||||
|
||||
/// Beta public key "key handle" - used to retrieve the key for signing from the token.
|
||||
/// Extracted from signing/beta_id_ed25519_sk using extract_sk_credential.py
|
||||
pub const ID_ED25519_SK_CRED_ID: [u8; 241] = [
|
||||
|
|
@ -37,9 +30,4 @@ pub const ID_ED25519_SK_CRED_ID: [u8; 241] = [
|
|||
/// Extracted from signing/beta_id_ed25519_sk using extract_sk_credential.py
|
||||
pub const ID_ED25519_SK_RP: &'static str = "ssh:";
|
||||
|
||||
pub const PUBKEY: Pubkey = Pubkey {
|
||||
pk: ID_ED25519_SK_PUB,
|
||||
tag: *KEYSLOT_INITIAL_TAGS[BETA_KEY_SLOT],
|
||||
aad_len: ID_ED25519_SK_AUTH_DATA.len() as u8,
|
||||
aad: pad_array(&ID_ED25519_SK_AUTH_DATA),
|
||||
};
|
||||
pub const PUBKEY: Pubkey = Pubkey { pk: ID_ED25519_SK_PUB, tag: *KEYSLOT_INITIAL_TAGS[BETA_KEY_SLOT] };
|
||||
|
|
|
|||
|
|
@ -7,12 +7,5 @@ pub const PUB: [u8; 32] = [
|
|||
0x1c, 0x9b, 0xea, 0xe3, 0x2a, 0xea, 0xc8, 0x75, 0x07, 0xc1, 0x80, 0x94, 0x38, 0x7e, 0xff, 0x1c, 0x74,
|
||||
0x61, 0x42, 0x82, 0xaf, 0xfd, 0x81, 0x52, 0xd8, 0x71, 0x35, 0x2e, 0xdf, 0x3f, 0x58, 0xbb,
|
||||
];
|
||||
/// Developer key has no auth data. It is a classic ed25519ph signature.
|
||||
pub const AUTH_DATA: [u8; 0] = [];
|
||||
|
||||
pub const PUBKEY: Pubkey = Pubkey {
|
||||
pk: PUB,
|
||||
tag: *KEYSLOT_INITIAL_TAGS[DEVELOPER_KEY_SLOT],
|
||||
aad_len: AUTH_DATA.len() as u8,
|
||||
aad: pad_array(&AUTH_DATA),
|
||||
};
|
||||
pub const PUBKEY: Pubkey = Pubkey { pk: PUB, tag: *KEYSLOT_INITIAL_TAGS[DEVELOPER_KEY_SLOT] };
|
||||
|
|
|
|||
|
|
@ -23,7 +23,9 @@ pub const DEVELOPER_KEY_SLOT: usize = 3;
|
|||
/// needs to be replaced, upgraded, or cycled out for any reason.
|
||||
pub const KEYSLOT_INITIAL_TAGS: [&'static [u8; 4]; 4] = [b"bao1", b"bao2", b"beta", b"dev "];
|
||||
|
||||
// helper function to pad arrays out, useful for the auth_data
|
||||
// helper function to pad arrays out, if needed. Previously used for auth_data until
|
||||
// I realized that auth_data changes with every signing.
|
||||
#[allow(dead_code)]
|
||||
const fn pad_array<const N: usize, const M: usize>(input: &[u8; N]) -> [u8; M] {
|
||||
let mut result = [0u8; M];
|
||||
let mut i = 0;
|
||||
|
|
|
|||
|
|
@ -3,11 +3,12 @@ use bytemuck::{Pod, Zeroable};
|
|||
/// Total reserved space for the signature block
|
||||
pub const SIGBLOCK_LEN: usize = 768;
|
||||
/// The jump instruction and the signature itself are not protected
|
||||
pub const UNSIGNED_LEN: usize = size_of::<u32>() + SIGNATURE_LENGTH;
|
||||
pub const UNSIGNED_LEN: usize = SignatureInFlash::sealed_data_offset();
|
||||
|
||||
// These are vendored in so we don't have a circular dependency on ed25519 crate
|
||||
pub const SIGNATURE_LENGTH: usize = 64; // length of an ed25519 signature.
|
||||
pub const PUBLIC_KEY_LENGTH: usize = 32; // length of an ed25519 public key.
|
||||
pub const AAD_LENGTH: usize = 60; // at least 37 bytes, rounded up to the nearest 32-byte boundary
|
||||
|
||||
/// These are notional and subject to change
|
||||
#[repr(u32)]
|
||||
|
|
@ -43,7 +44,8 @@ pub enum FunctionCode {
|
|||
}
|
||||
|
||||
pub const BAOCHIP_SIG_VERSION: u32 = 0x1_00;
|
||||
pub const PADDING_LEN: usize = SIGBLOCK_LEN - size_of::<SealedFields>() - SIGNATURE_LENGTH - size_of::<u32>();
|
||||
pub const PADDING_LEN: usize = SIGBLOCK_LEN - size_of::<SignatureInFlash>();
|
||||
pub const MAGIC_NUMBER: [u32; 2] = [u32::from_be_bytes(*b"yumy"), u32::from_be_bytes(*b"Bao3")];
|
||||
/// Representation of the signature block in memory.
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
|
|
@ -53,10 +55,17 @@ pub struct SignatureInFlash {
|
|||
pub _jal_instruction: u32,
|
||||
/// The actual signature.
|
||||
pub signature: [u8; SIGNATURE_LENGTH],
|
||||
/// `aad_len` also specifies the signing protocol. If it is `0`, then a pure `ed25519ph`
|
||||
/// signature is assumed. If it is greater than `0`, then it's assumed to be a
|
||||
/// FIDO2/WebAuthn signature format using ed25519, where the signature is computed as:
|
||||
/// `signature = Ed25519.sign(authenticatorData || SHA-256(clientData))`
|
||||
/// `authenticatorData` is a field that is at least 37 bytes in size. The size is influenced
|
||||
/// by the type of signing and operation, but for our implementation I think it stays
|
||||
/// fixed at 37 bytes.
|
||||
pub aad_len: u32,
|
||||
pub aad: [u8; AAD_LENGTH],
|
||||
/// All data from this point onward are included in the signature computation.
|
||||
pub sealed_data: SealedFields,
|
||||
/// Padding to target length
|
||||
pub padding: [u8; PADDING_LEN],
|
||||
}
|
||||
unsafe impl Zeroable for SignatureInFlash {}
|
||||
unsafe impl Pod for SignatureInFlash {}
|
||||
|
|
@ -71,38 +80,34 @@ impl Default for SignatureInFlash {
|
|||
Self {
|
||||
_jal_instruction: 0,
|
||||
signature: [0u8; SIGNATURE_LENGTH],
|
||||
aad_len: 0,
|
||||
aad: [0u8; AAD_LENGTH],
|
||||
sealed_data: SealedFields::default(),
|
||||
padding: [0u8; PADDING_LEN],
|
||||
}
|
||||
}
|
||||
}
|
||||
impl SignatureInFlash {
|
||||
// The offset is after the jal instruction + signature.
|
||||
pub const fn sealed_data_offset() -> usize {
|
||||
size_of::<u32>() + SIGNATURE_LENGTH + size_of::<u32>() + AAD_LENGTH
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct Pubkey {
|
||||
pub pk: [u8; PUBLIC_KEY_LENGTH],
|
||||
pub tag: [u8; 4],
|
||||
/// `aad_len` also specifies the signing protocol. If it is `0`, then a pure `ed25519ph`
|
||||
/// signature is assumed. If it is greater than `0`, then it's assumed to be a
|
||||
/// FIDO2/WebAuthn signature format using ed25519, where the signature is computed as:
|
||||
/// `signature = Ed25519.sign(authenticatorData || SHA-256(clientData))`
|
||||
/// `authenticatorData` is a field that is at least 37 bytes in size, and can be much
|
||||
/// larger if optional extentions are enabled. 59 bytes are made available since this
|
||||
/// pads nicely into the record format, and gives us some wiggle room for compatibility
|
||||
pub aad_len: u8,
|
||||
pub aad: [u8; 59],
|
||||
}
|
||||
unsafe impl Zeroable for Pubkey {}
|
||||
unsafe impl Pod for Pubkey {}
|
||||
impl Default for Pubkey {
|
||||
fn default() -> Self { Self { pk: [0u8; PUBLIC_KEY_LENGTH], tag: [0u8; 4], aad_len: 0, aad: [0u8; 59] } }
|
||||
fn default() -> Self { Self { pk: [0u8; PUBLIC_KEY_LENGTH], tag: [0u8; 4] } }
|
||||
}
|
||||
impl Pubkey {
|
||||
pub fn populate_from(&mut self, record: &Pubkey) {
|
||||
self.pk.copy_from_slice(&record.pk);
|
||||
self.tag.copy_from_slice(&record.tag);
|
||||
self.aad_len = record.aad_len;
|
||||
self.aad.copy_from_slice(&record.aad);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -111,6 +116,8 @@ impl Pubkey {
|
|||
pub struct SealedFields {
|
||||
/// Version number of this signature record
|
||||
pub version: u32,
|
||||
/// Magic number, to quickly differentiate uninitialized memory from a valid signature record
|
||||
pub magic: [u32; 2],
|
||||
/// Length of the signed code.
|
||||
pub signed_len: u32,
|
||||
/// Function code of the signed block. Used to provide metadata about what is inside the signed
|
||||
|
|
@ -147,7 +154,8 @@ impl AsRef<[u8]> for SealedFields {
|
|||
impl Default for SealedFields {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
version: 0,
|
||||
version: BAOCHIP_SIG_VERSION,
|
||||
magic: MAGIC_NUMBER,
|
||||
signed_len: 0,
|
||||
function_code: 0,
|
||||
reserved: 0,
|
||||
|
|
|
|||
|
|
@ -57,9 +57,15 @@ pub fn validate_image(
|
|||
|
||||
let pubkey_ptr = pubkeys_offset as *const SignatureInFlash;
|
||||
let pk_src: &SignatureInFlash = unsafe { pubkey_ptr.as_ref().unwrap() };
|
||||
if pk_src.sealed_data.magic != MAGIC_NUMBER {
|
||||
return Err(String::from("Invalid magic number in verifying key record"));
|
||||
}
|
||||
|
||||
let signed_len = sig.sealed_data.signed_len;
|
||||
|
||||
if sig.sealed_data.magic != MAGIC_NUMBER {
|
||||
return Err(String::from("Invalid magic number on incoming record to be verified"));
|
||||
}
|
||||
if sig.sealed_data.version != BAOCHIP_SIG_VERSION {
|
||||
crate::println!(
|
||||
"Version {:x} sig found, should be {:x}",
|
||||
|
|
@ -78,6 +84,7 @@ pub fn validate_image(
|
|||
|
||||
let developer = sig.sealed_data.function_code == FunctionCode::Developer as u32;
|
||||
|
||||
// crate::println!("Signature: {:x?}", sig.signature);
|
||||
let one_way_counters = OneWayCounter::new();
|
||||
let mut secure = false;
|
||||
let mut passing_key: Option<usize> = None;
|
||||
|
|
@ -95,36 +102,35 @@ pub fn validate_image(
|
|||
|
||||
let ed25519_signature = ed25519_dalek::Signature::from(sig.signature);
|
||||
|
||||
if key.aad_len == 0 {
|
||||
let mut h: Sha512 = Sha512::new();
|
||||
// this code has to be implemented *twice* because we can't make `h` a generic trait due
|
||||
// to the type bounds reasonably imposed by `verify_prehashed`.
|
||||
if let Some(ref mut spim) = spim {
|
||||
// need to read the data out page by page and hash it.
|
||||
// ASSUME: the SPIM driver has allocated a read buffer that is actually PAGE_SIZE. If the SPIM
|
||||
// driver has a smaller buffer, reads get less efficient.
|
||||
let end = img_offset as usize + UNSIGNED_LEN + signed_len as usize;
|
||||
for offset in ((img_offset as usize + UNSIGNED_LEN)..end).step_by(PAGE_SIZE) {
|
||||
let mut buf = [0u8; PAGE_SIZE];
|
||||
spim.mem_read(offset as u32, &mut buf, false);
|
||||
let valid_length = if offset + PAGE_SIZE < end { PAGE_SIZE } else { end - offset };
|
||||
h.update(&buf[..valid_length]);
|
||||
}
|
||||
} else {
|
||||
// easy peasy
|
||||
let image: &[u8] = unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
(img_offset as usize + UNSIGNED_LEN) as *const u8,
|
||||
signed_len as usize,
|
||||
)
|
||||
};
|
||||
// crate::println!("Verifying image {:x?} with {:x?}", &image[..16], &key);
|
||||
h.update(&image);
|
||||
let mut h: Sha512 = Sha512::new();
|
||||
if let Some(ref mut spim) = spim {
|
||||
// need to read the data out page by page and hash it.
|
||||
// ASSUME: the SPIM driver has allocated a read buffer that is actually PAGE_SIZE. If the SPIM
|
||||
// driver has a smaller buffer, reads get less efficient.
|
||||
let end = img_offset as usize + UNSIGNED_LEN + signed_len as usize;
|
||||
for offset in ((img_offset as usize + UNSIGNED_LEN)..end).step_by(PAGE_SIZE) {
|
||||
let mut buf = [0u8; PAGE_SIZE];
|
||||
spim.mem_read(offset as u32, &mut buf, false);
|
||||
let valid_length = if offset + PAGE_SIZE < end { PAGE_SIZE } else { end - offset };
|
||||
h.update(&buf[..valid_length]);
|
||||
}
|
||||
} else {
|
||||
// easy peasy
|
||||
let image: &[u8] = unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
(img_offset as usize + UNSIGNED_LEN) as *const u8,
|
||||
signed_len as usize,
|
||||
)
|
||||
};
|
||||
h.update(&image);
|
||||
}
|
||||
if sig.aad_len == 0 {
|
||||
// crate::println!("ed25519ph verifying with {:x?}", &key.pk);
|
||||
// debugging note: h.clone() does *not* work. You have to print the hash by modifying
|
||||
// the function inside the ed25519 crate.
|
||||
match verifying_key.verify_prehashed(h, None, &ed25519_signature) {
|
||||
Ok(_) => {
|
||||
crate::println!("ed25519ph verification passed");
|
||||
passing_key = Some(i);
|
||||
if i != sig.sealed_data.pubkeys.len() - 1 {
|
||||
secure = true;
|
||||
|
|
@ -135,35 +141,28 @@ pub fn validate_image(
|
|||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
_ => {
|
||||
crate::println!("ed25519ph verification failed");
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let sha512_hashed_image = h.finalize();
|
||||
// create a *new* hasher because a token can only sign a hash, not the full image.
|
||||
let mut h: Sha256 = Sha256::new();
|
||||
// this code has to be implemented *twice* because we can't make `h` a generic trait due
|
||||
// to the type bounds reasonably imposed by `verify_prehashed`.
|
||||
if let Some(ref mut spim) = spim {
|
||||
let end = img_offset as usize + UNSIGNED_LEN + signed_len as usize;
|
||||
for offset in ((img_offset as usize + UNSIGNED_LEN)..end).step_by(PAGE_SIZE) {
|
||||
let mut buf = [0u8; PAGE_SIZE];
|
||||
spim.mem_read(offset as u32, &mut buf, false);
|
||||
let valid_length = if offset + PAGE_SIZE < end { PAGE_SIZE } else { end - offset };
|
||||
h.update(&buf[..valid_length]);
|
||||
}
|
||||
} else {
|
||||
let image: &[u8] = unsafe {
|
||||
core::slice::from_raw_parts(
|
||||
(img_offset as usize + UNSIGNED_LEN) as *const u8,
|
||||
signed_len as usize,
|
||||
)
|
||||
};
|
||||
h.update(&image);
|
||||
}
|
||||
// hash dat hash!
|
||||
// crate::println!("verifying base hash {:x?}", &sha512_hashed_image.as_slice());
|
||||
h.update(&sha512_hashed_image.as_slice());
|
||||
let hashed_hash = h.finalize();
|
||||
// crate::println!("hashed hash: {:x?}", hashed_hash.as_slice());
|
||||
|
||||
let mut msg: Vec<u8> = Vec::new();
|
||||
msg.extend_from_slice(&key.aad[..key.aad_len as usize]);
|
||||
msg.extend_from_slice(h.finalize().as_slice());
|
||||
msg.extend_from_slice(&sig.aad[..sig.aad_len as usize]);
|
||||
msg.extend_from_slice(hashed_hash.as_slice());
|
||||
// crate::println!("assembled msg({}): {:x?}", msg.len(), msg);
|
||||
|
||||
match verifying_key.verify_strict(&msg, &ed25519_signature) {
|
||||
Ok(_) => {
|
||||
crate::println!("FIDO2 ed25519 verification passed");
|
||||
passing_key = Some(i);
|
||||
if i != sig.sealed_data.pubkeys.len() - 1 {
|
||||
secure = true;
|
||||
|
|
@ -174,7 +173,9 @@ pub fn validate_image(
|
|||
break;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
_ => {
|
||||
crate::println!("FIDO2 verification failed");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue