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],
)
| 393 | /// supplies the 4-byte `magic` that identifies its envelope variant |
| 394 | /// (`SEGA`, `SEGC`, `SEGT`, `SEGV`, …). |
| 395 | pub 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 | /// |
no test coverage detected