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

Function encrypt_segment_envelope

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

Encrypt `plaintext` into a self-describing segment envelope. Returns `preamble || AES-256-GCM(plaintext) || auth_tag`. The caller supplies the 4-byte `magic` that identifies its envelope variant (`SEGA`, `SEGC`, `SEGT`, `SEGV`, …).

(
    key: &WalEncryptionKey,
    magic: &[u8; 4],
    plaintext: &[u8],
)

Source from the content-addressed store, hash-verified

393/// supplies the 4-byte `magic` that identifies its envelope variant
394/// (`SEGA`, `SEGC`, `SEGT`, `SEGV`, …).
395pub fn encrypt_segment_envelope(
396 key: &WalEncryptionKey,
397 magic: &[u8; 4],
398 plaintext: &[u8],
399) -> Result<Vec<u8>> {
400 let fresh_key = key.with_fresh_epoch()?;
401 let epoch = *fresh_key.epoch();
402 let preamble = encode_envelope_preamble(magic, &epoch);
403 let ciphertext = fresh_key.encrypt_aad(SEGMENT_ENVELOPE_NONCE_LSN, &preamble, plaintext)?;
404 let mut out = Vec::with_capacity(SEGMENT_ENVELOPE_PREAMBLE_SIZE + ciphertext.len());
405 out.extend_from_slice(&preamble);
406 out.extend_from_slice(&ciphertext);
407 Ok(out)
408}
409
410/// Decrypt a segment envelope produced by [`encrypt_segment_envelope`].
411///

Callers 5

encrypt_payloadFunction · 0.85
encrypt_checkpointFunction · 0.85
encrypt_segmentFunction · 0.85
encrypt_fileFunction · 0.85
encrypt_segmentFunction · 0.85

Calls 5

encode_envelope_preambleFunction · 0.85
with_fresh_epochMethod · 0.80
encrypt_aadMethod · 0.80
epochMethod · 0.45
lenMethod · 0.45

Tested by

no test coverage detected