Create from a 32-byte key with a fresh random epoch. Returns an error if the OS RNG (`getrandom`) is unavailable. Without a fresh epoch we cannot guarantee nonce uniqueness across WAL lifetimes, so panicking would silently risk nonce reuse on RNG failure — better to surface it.
(key: &[u8; 32])
| 111 | /// lifetimes, so panicking would silently risk nonce reuse on RNG |
| 112 | /// failure — better to surface it. |
| 113 | pub fn from_bytes(key: &[u8; 32]) -> Result<Self> { |
| 114 | let mut epoch = [0u8; 4]; |
| 115 | getrandom::fill(&mut epoch).map_err(|e| WalError::EncryptionError { |
| 116 | detail: format!("getrandom failed while generating epoch: {e}"), |
| 117 | })?; |
| 118 | // mlock key_bytes so they are not swapped to disk. Best-effort: if the |
| 119 | // OS refuses (e.g. RLIMIT_MEMLOCK exceeded in a container) we log and |
| 120 | // continue rather than aborting startup. |
| 121 | let mut key_bytes = *key; |
| 122 | secure_mem::mlock_key_bytes(key_bytes.as_mut_ptr(), 32); |
| 123 | Ok(Self { |
| 124 | cipher: Aes256Gcm::new(key.into()), |
| 125 | key_bytes, |
| 126 | epoch, |
| 127 | }) |
| 128 | } |
| 129 | |
| 130 | /// Create from a 32-byte key with a **caller-supplied epoch**. |
| 131 | /// |
nothing calls this directly
no test coverage detected