use core::fmt;
use crypt_io::hash::blake3;
use super::{Anchor, ct_eq};
use crate::{Error, Result};
pub const FINGERPRINT_LEN: usize = 112;
const MAGIC: &[u8; 4] = b"SLFP";
const VERSION: u8 = 1;
const ANCHORS: usize = Anchor::ALL.len();
const HASHES_AT: usize = 8;
const CHECKSUM_AT: usize = HASHES_AT + ANCHORS * 16;
const fn weight(anchor: Anchor) -> u32 {
match anchor {
Anchor::InstallId | Anchor::PlatformUuid => 3,
Anchor::BoardSerial | Anchor::ProductSerial => 2,
Anchor::CpuSignature | Anchor::PermanentMac => 1,
}
}
#[derive(Clone, PartialEq, Eq)]
pub struct Fingerprint {
present: u8,
hashes: [[u8; 16]; ANCHORS],
}
impl fmt::Debug for Fingerprint {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_list().entries(self.anchors()).finish()
}
}
impl Fingerprint {
pub(super) const fn empty() -> Self {
Self {
present: 0,
hashes: [[0; 16]; ANCHORS],
}
}
pub(super) fn set(&mut self, slot: usize, tag: &[u8; 32]) {
self.hashes[slot].copy_from_slice(&tag[..16]);
self.present |= 1 << slot;
}
fn has(&self, slot: usize) -> bool {
self.present & (1 << slot) != 0
}
pub fn anchors(&self) -> impl Iterator<Item = Anchor> + '_ {
Anchor::ALL
.into_iter()
.enumerate()
.filter(|&(slot, _)| self.has(slot))
.map(|(_, a)| a)
}
#[must_use]
pub fn compare(&self, stored: &Fingerprint) -> Comparison {
let (mut matched, mut changed, mut missing) = (Vec::new(), Vec::new(), Vec::new());
for (slot, anchor) in Anchor::ALL.into_iter().enumerate() {
match (self.has(slot), stored.has(slot)) {
(true, true) if ct_eq(&self.hashes[slot], &stored.hashes[slot]) => {
matched.push(anchor);
}
(true, true) => changed.push(anchor),
(false, false) => {}
_ => missing.push(anchor),
}
}
let weight_of = |list: &[Anchor]| list.iter().map(|&a| weight(a)).sum::<u32>();
let strong_changed = changed.iter().filter(|&&a| weight(a) >= 2).count();
let identifies = matched.iter().any(|&a| a != Anchor::CpuSignature);
let verdict = if identifies && changed.is_empty() {
Verdict::Same
} else if identifies && weight_of(&matched) >= 5 && strong_changed <= 1 {
Verdict::LikelySame
} else {
Verdict::Different
};
Comparison {
verdict,
matched,
changed,
missing,
}
}
#[must_use]
pub fn to_bytes(&self) -> [u8; FINGERPRINT_LEN] {
let mut out = [0u8; FINGERPRINT_LEN];
out[..4].copy_from_slice(MAGIC);
out[4] = VERSION;
out[5] = self.present;
for (slot, hash) in self.hashes.iter().enumerate() {
out[HASHES_AT + slot * 16..HASHES_AT + (slot + 1) * 16].copy_from_slice(hash);
}
let sum = blake3(&out[..CHECKSUM_AT]);
out[CHECKSUM_AT..].copy_from_slice(&sum[..8]);
out
}
pub fn from_bytes(bytes: &[u8]) -> Result<Fingerprint> {
const BAD: Error = Error::Malformed {
what: "device fingerprint",
source: "stored fingerprint",
line: None,
};
if bytes.len() != FINGERPRINT_LEN
|| bytes[..4] != *MAGIC
|| bytes[4] != VERSION
|| bytes[6..8] != [0, 0]
|| bytes[5] >> ANCHORS != 0
{
return Err(BAD);
}
let mut out = Self {
present: bytes[5],
..Self::empty()
};
for slot in 0..ANCHORS {
let at = HASHES_AT + slot * 16;
out.hashes[slot].copy_from_slice(&bytes[at..at + 16]);
if !out.has(slot) && out.hashes[slot] != [0; 16] {
return Err(BAD);
}
}
if blake3(&bytes[..CHECKSUM_AT])[..8] != bytes[CHECKSUM_AT..] {
return Err(BAD);
}
Ok(out)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct Comparison {
pub verdict: Verdict,
pub matched: Vec<Anchor>,
pub changed: Vec<Anchor>,
pub missing: Vec<Anchor>,
}
#[derive(Clone, Copy, PartialEq, Eq, Hash, Debug)]
#[non_exhaustive]
pub enum Verdict {
Same,
LikelySame,
Different,
}
#[cfg(test)]
mod tests {
use super::*;
fn with(entries: &[(Anchor, u8)]) -> Fingerprint {
let mut f = Fingerprint::empty();
for &(anchor, byte) in entries {
let slot = Anchor::ALL.iter().position(|&a| a == anchor).unwrap();
f.set(slot, &[byte; 32]);
}
f
}
const FULL: &[(Anchor, u8)] = &[
(Anchor::InstallId, 1),
(Anchor::PlatformUuid, 2),
(Anchor::BoardSerial, 3),
(Anchor::ProductSerial, 4),
(Anchor::CpuSignature, 5),
(Anchor::PermanentMac, 6),
];
#[test]
fn test_fingerprint_verdicts() {
let a = with(FULL);
assert_eq!(a.compare(&a).verdict, Verdict::Same);
let mut parts = FULL.to_vec();
parts[5].1 = 66;
let c = a.compare(&with(&parts));
assert_eq!(c.verdict, Verdict::LikelySame);
assert_eq!(c.changed, vec![Anchor::PermanentMac]);
let mut parts = FULL.to_vec();
parts[0].1 = 11;
assert_eq!(a.compare(&with(&parts)).verdict, Verdict::LikelySame);
parts[1].1 = 22;
assert_eq!(a.compare(&with(&parts)).verdict, Verdict::Different);
let other: Vec<_> = FULL.iter().map(|&(a, b)| (a, b + 100)).collect();
assert_eq!(a.compare(&with(&other)).verdict, Verdict::Different);
}
#[test]
fn test_fingerprint_cpu_alone_proves_nothing() {
let a = with(&[(Anchor::CpuSignature, 5)]);
assert_eq!(a.compare(&a).verdict, Verdict::Different);
assert_eq!(
Fingerprint::empty().compare(&Fingerprint::empty()).verdict,
Verdict::Different
);
}
#[test]
fn test_fingerprint_missing_anchors_do_not_count_against() {
let a = with(&FULL[..2]);
let b = with(FULL);
let c = a.compare(&b);
assert_eq!(c.verdict, Verdict::Same);
assert_eq!(c.missing.len(), 4);
}
#[test]
fn test_fingerprint_round_trip() {
let f = with(&FULL[1..4]);
let bytes = f.to_bytes();
assert_eq!(&bytes[..5], b"SLFP\x01");
assert_eq!(bytes[5], 0b1110);
let back = Fingerprint::from_bytes(&bytes).unwrap();
assert_eq!(back, f);
assert_eq!(
back.anchors().collect::<Vec<_>>(),
vec![
Anchor::PlatformUuid,
Anchor::BoardSerial,
Anchor::ProductSerial
]
);
assert_eq!(
format!("{f:?}"),
"[PlatformUuid, BoardSerial, ProductSerial]"
);
}
#[test]
fn test_fingerprint_e78_every_bad_form_is_malformed() {
let good = with(FULL).to_bytes();
let reject = |bytes: &[u8], why: &str| {
assert!(
matches!(Fingerprint::from_bytes(bytes), Err(Error::Malformed { .. })),
"{why}"
);
};
reject(&good[..FINGERPRINT_LEN - 1], "truncated");
reject(&[good.as_slice(), &[0]].concat(), "extended");
reject(&[], "empty");
for (at, why) in [
(0, "magic"),
(4, "version"),
(6, "reserved"),
(7, "reserved"),
(20, "hash without a checksum"),
(FINGERPRINT_LEN - 1, "checksum"),
] {
let mut bad = good;
bad[at] ^= 0xFF;
reject(&bad, why);
}
let mut bad = good;
bad[5] |= 0x80;
reject(&bad, "bitmap");
let mut f = with(&FULL[..1]).to_bytes();
f[HASHES_AT + 16] = 1;
let sum = blake3(&f[..CHECKSUM_AT]);
f[CHECKSUM_AT..].copy_from_slice(&sum[..8]);
reject(&f, "inconsistent bitmap");
}
}