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

Function decrypt_segment_bytes

nodedb/src/storage/segment.rs:221–264  ·  view source on GitHub ↗

Decrypt a segment byte envelope (no file I/O). Equivalent to `read_encrypted_segment` but operates on an in-memory byte slice. Used by object-store download paths.

(
    raw: &[u8],
    key: Option<&nodedb_wal::crypto::WalEncryptionKey>,
)

Source from the content-addressed store, hash-verified

219/// Equivalent to `read_encrypted_segment` but operates on an in-memory byte
220/// slice. Used by object-store download paths.
221pub fn decrypt_segment_bytes(
222 raw: &[u8],
223 key: Option<&nodedb_wal::crypto::WalEncryptionKey>,
224) -> crate::Result<Vec<u8>> {
225 if let Some(key) = key {
226 let min_len = PREAMBLE_SIZE + nodedb_wal::crypto::AUTH_TAG_SIZE + FOOTER_SIZE;
227 if raw.len() < min_len {
228 return Err(crate::Error::SegmentCorrupted {
229 detail: "encrypted segment envelope too small".into(),
230 });
231 }
232 let preamble_bytes: [u8; PREAMBLE_SIZE] = raw[..PREAMBLE_SIZE]
233 .try_into()
234 .expect("slice is PREAMBLE_SIZE bytes");
235 let preamble =
236 SegmentPreamble::from_bytes(&preamble_bytes, &SEG_PREAMBLE_MAGIC).map_err(|e| {
237 crate::Error::SegmentCorrupted {
238 detail: format!("invalid segment preamble: {e}"),
239 }
240 })?;
241 let footer_bytes: [u8; FOOTER_SIZE] = raw[raw.len() - FOOTER_SIZE..]
242 .try_into()
243 .expect("slice is FOOTER_SIZE bytes");
244 let footer = SegmentFooter::from_bytes(&footer_bytes)?;
245 let ciphertext = &raw[PREAMBLE_SIZE..raw.len() - FOOTER_SIZE];
246 key.decrypt_aad(
247 preamble.epoch(),
248 footer.min_lsn.as_u64(),
249 &preamble_bytes,
250 ciphertext,
251 )
252 .map_err(|e| crate::Error::Storage {
253 engine: "segment".into(),
254 detail: format!("segment decryption failed: {e}"),
255 })
256 } else {
257 if raw.len() < FOOTER_SIZE {
258 return Err(crate::Error::SegmentCorrupted {
259 detail: "envelope too small".into(),
260 });
261 }
262 Ok(raw[..raw.len() - FOOTER_SIZE].to_vec())
263 }
264}
265
266/// Read and decrypt a segment file's data portion.
267///

Callers 1

load_core_snapshotFunction · 0.85

Calls 6

decrypt_aadMethod · 0.80
lenMethod · 0.45
expectMethod · 0.45
epochMethod · 0.45
as_u64Method · 0.45
to_vecMethod · 0.45

Tested by

no test coverage detected