Skip to main content

speck_core/format/
header.rs

1//! Module header definitions
2
3/// Fixed-size module header (48 bytes before cryptographic fields)
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub struct ModuleHeader {
6    /// Format version (monotonically increasing)
7    pub version: u16,
8    
9    /// Offset to entry point from start of code
10    pub entry_offset: u32,
11    
12    /// Feature flags
13    pub flags: u32,
14    
15    /// Anti-rollback version counter
16    pub monotonic_version: u64,
17    
18    /// Required hardware revision (0 = any)
19    pub hw_revision: u32,
20    
21    /// CRC32 checksum of code section
22    pub code_crc: u32,
23}
24
25impl Default for ModuleHeader {
26    fn default() -> Self {
27        Self {
28            version: crate::CURRENT_MODULE_VERSION,
29            entry_offset: 0,
30            flags: 0,
31            monotonic_version: 0,
32            hw_revision: 0,
33            code_crc: 0,
34        }
35    }
36}
37
38/// Human-readable flag descriptions
39#[derive(Debug, Clone, Copy)]
40pub enum HeaderFlags {
41    /// Module has valid signature
42    Signed,
43    /// Code section is compressed
44    Compressed,
45    /// Requires specific hardware
46    HardwareLocked,
47    /// Critical security update
48    Critical,
49}
50
51impl HeaderFlags {
52    /// Get the bit flag value
53    pub fn bit(self) -> u32 {
54        match self {
55            HeaderFlags::Signed => super::FLAG_SIGNED,
56            HeaderFlags::Compressed => super::FLAG_COMPRESSED,
57            HeaderFlags::HardwareLocked => super::FLAG_HW_LOCKED,
58            HeaderFlags::Critical => super::FLAG_CRITICAL,
59        }
60    }
61    
62    /// Get human-readable name
63    pub fn name(self) -> &'static str {
64        match self {
65            HeaderFlags::Signed => "signed",
66            HeaderFlags::Compressed => "compressed",
67            HeaderFlags::HardwareLocked => "hw-locked",
68            HeaderFlags::Critical => "critical",
69        }
70    }
71}