speck-core 0.2.0

Secure runtime package manager for MMU-less microcontrollers
Documentation
//! Module header definitions

/// Fixed-size module header (48 bytes before cryptographic fields)
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ModuleHeader {
    /// Format version (monotonically increasing)
    pub version: u16,
    
    /// Offset to entry point from start of code
    pub entry_offset: u32,
    
    /// Feature flags
    pub flags: u32,
    
    /// Anti-rollback version counter
    pub monotonic_version: u64,
    
    /// Required hardware revision (0 = any)
    pub hw_revision: u32,
    
    /// CRC32 checksum of code section
    pub code_crc: u32,
}

impl Default for ModuleHeader {
    fn default() -> Self {
        Self {
            version: crate::CURRENT_MODULE_VERSION,
            entry_offset: 0,
            flags: 0,
            monotonic_version: 0,
            hw_revision: 0,
            code_crc: 0,
        }
    }
}

/// Human-readable flag descriptions
#[derive(Debug, Clone, Copy)]
pub enum HeaderFlags {
    /// Module has valid signature
    Signed,
    /// Code section is compressed
    Compressed,
    /// Requires specific hardware
    HardwareLocked,
    /// Critical security update
    Critical,
}

impl HeaderFlags {
    /// Get the bit flag value
    pub fn bit(self) -> u32 {
        match self {
            HeaderFlags::Signed => super::FLAG_SIGNED,
            HeaderFlags::Compressed => super::FLAG_COMPRESSED,
            HeaderFlags::HardwareLocked => super::FLAG_HW_LOCKED,
            HeaderFlags::Critical => super::FLAG_CRITICAL,
        }
    }
    
    /// Get human-readable name
    pub fn name(self) -> &'static str {
        match self {
            HeaderFlags::Signed => "signed",
            HeaderFlags::Compressed => "compressed",
            HeaderFlags::HardwareLocked => "hw-locked",
            HeaderFlags::Critical => "critical",
        }
    }
}