#![warn(missing_docs)]
#![warn(clippy::all)]
#![allow(dead_code)]
use std::time::Duration;
use thiserror::Error;
pub mod core;
pub use core::{monitor::Monitor, monitor::MonitorBuilder};
mod platform;
#[cfg(feature = "remote-attestation")]
pub mod attestation;
pub mod utils;
pub use core::{
forensics::ForensicSnapshot,
hasher::{HashAlgorithm, MerkleTree},
mapper::{MemorySegment, SegmentPermissions},
monitor::MonitorHandle,
};
#[derive(Error, Debug)]
pub enum Error {
#[error("Monitor initialization failed: {0}")]
InitializationFailed(String),
#[error("Memory mapping failed: {0}")]
MappingFailed(String),
#[error("Hashing failed: {0}")]
HashingFailed(String),
#[error("Tampering detected in segment: {0}")]
TamperingDetected(String),
#[error("Platform error: {0}")]
PlatformError(String),
#[error("I/O error: {0}")]
IoError(#[from] std::io::Error),
#[error("Configuration error: {0}")]
ConfigurationError(String),
#[error("Monitor is already running")]
AlreadyRunning,
#[error("Monitor is not running")]
NotRunning,
}
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Clone)]
pub struct MonitorConfig {
pub interval: Duration,
pub use_merkle: bool,
pub hash_algorithm: HashAlgorithm,
pub adaptive: bool,
pub whitelist_policy: WhitelistPolicy,
pub enable_forensics: bool,
pub max_snapshot_size: usize,
pub response: TamperResponse,
}
impl Default for MonitorConfig {
fn default() -> Self {
Self {
interval: Duration::from_secs(3),
use_merkle: true,
hash_algorithm: HashAlgorithm::Blake3,
adaptive: false,
whitelist_policy: WhitelistPolicy::None,
enable_forensics: false,
max_snapshot_size: 10 * 1024 * 1024, response: TamperResponse::Alert,
}
}
}
#[derive(Debug, Clone)]
pub enum WhitelistPolicy {
None,
ByPattern(Vec<String>),
ByAddressRange(Vec<(usize, usize)>),
Custom,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TamperResponse {
Log,
Alert,
Restore,
Terminate,
}
#[derive(Debug, Clone)]
pub struct TamperInfo {
pub segment: MemorySegment,
pub differences: Vec<u8>,
pub original_hash: Vec<u8>,
pub current_hash: Vec<u8>,
pub timestamp: std::time::SystemTime,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let config = MonitorConfig::default();
assert_eq!(config.interval, Duration::from_secs(3));
assert!(config.use_merkle);
assert_eq!(config.response, TamperResponse::Alert);
}
#[test]
fn test_tamper_response_variants() {
assert_eq!(TamperResponse::Log, TamperResponse::Log);
assert_ne!(TamperResponse::Log, TamperResponse::Alert);
}
}