32 bytes of evidence-quality randomness for the session MAC key. Prefers OS randomness; falls back to a process/time mix so evidence capture still works in sandboxes without `/dev/urandom`. This is not a secret: the key is stored beside the evidence it protects.
()
| 712 | /// capture still works in sandboxes without `/dev/urandom`. This is not a |
| 713 | /// secret: the key is stored beside the evidence it protects. |
| 714 | fn random_mac_key() -> String { |
| 715 | let mut bytes = [0u8; 32]; |
| 716 | #[cfg(unix)] |
| 717 | { |
| 718 | use std::io::Read; |
| 719 | if let Ok(mut file) = std::fs::File::open("/dev/urandom") { |
| 720 | if file.read_exact(&mut bytes).is_ok() { |
| 721 | return hex_encode(&bytes); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | // Deterministic fallback: unique per session/process/creation instant. |
| 726 | let nanos = std::time::SystemTime::now() |
| 727 | .duration_since(std::time::UNIX_EPOCH) |
| 728 | .map(|d| d.as_nanos()) |
| 729 | .unwrap_or(0); |
| 730 | let mix = format!( |
| 731 | "{}:{}:{}", |
| 732 | nanos, |
| 733 | std::process::id(), |
| 734 | "atomic-agent-mac-key" |
| 735 | ); |
| 736 | bytes.copy_from_slice(&blake3::derive_key( |
| 737 | "atomic-agent session mac key", |
| 738 | mix.as_bytes(), |
| 739 | )); |
| 740 | hex_encode(&bytes) |
| 741 | } |
| 742 | |
| 743 | /// Lowercase hex encoding for MAC keys. |
| 744 | fn hex_encode(bytes: &[u8]) -> String { |
no test coverage detected