MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / from_bytes

Method from_bytes

nodedb-wal/src/crypto.rs:113–128  ·  view source on GitHub ↗

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])

Source from the content-addressed store, hash-verified

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 ///

Callers

nothing calls this directly

Calls 2

mlock_key_bytesFunction · 0.85
as_mut_ptrMethod · 0.45

Tested by

no test coverage detected