Expand description
ยงAES-256-CBC-HMAC-Legacy-Stream
Aes256CBC-HMAC-Legacy-Stream is a format for streaming encryption of attachments. It consists of a header, an AES-CBC stream, and a hmac over IV + CBC ciphertext. Because there is just one HMAC over the entire stream, it is not permissible to decrypt a partial portion of this stream, as the integrity of that portion cannot be guaranteed.
ยงFormat
The stream looks as follows:
(KEY_E, KEY_A) = KEY
IV | HMAC[KEY_A] (over IV + ciphertext) | AES-CBC[KEY_E]() ciphertextยงLimitations
Because the HMAC is written to the start, encryption must be buffered in memory fully before emitting out as an encryption stream, since the HMAC can only be calculated after all ciphertext is produced. This is a limitation to the format.
Further, the HMAC covers the entire stream, not chunks of it, so the entire stream must be fully read before the output is used. IMPORTANT: YOU MUST READ THE ENTIRE STREAM BEFORE USING THE DECRYPTED OUTPUT. This contract cannot be enforced by the interface and requires the correct usage of the caller.
NOTE: The attachments, stored on the server contain a header idicating which format they are encrypted with. This header is NOT included in the stream processed by this file, and is expected to be stripped by the caller before passing the ciphertext to this code.
Random access is not possible with this format, both because of the use of CBC chaining, and because of the single HMAC over the entire cipher stream.
Structsยง
- CbcDecryptor ๐
- CbcEncryptor ๐
- CbcPlaintext
Block ๐ - Ciphertext
Buffer ๐ - Hmac
Stream ๐Validator - A higher level interface over the HMAC validation of the attachment ciphertext
- Stream
Header ๐ - Streaming
Aes256 CbcHmac Decryptor - Streaming AES-256-CBC + HMAC-SHA256 decryptor. The HMAC is verified only when
StreamingDecryptor::updateis called withlast_block = true; bytes returned from earlierupdatecalls asChunkDecryptionResult::DecryptedChunkare decrypted but not yet authenticated and must be treated as untrusted until the terminalChunkDecryptionResult::FinalDecryptedChunkis observed. - Streaming
Aes256 CbcHmac Encryptor - Streaming AES-256-CBC + HMAC-SHA256 encryptor. The IV is generated at construction time
and the HMAC is computed over IV || ciphertext, matching the wire format consumed by
StreamingAes256CbcHmacDecryptor. Because the MAC depends on the entire ciphertext, the complete wire stream (IV, ciphertext) is only emitted onceupdateis called withlast_block = true, as a singleChunkEncryptionResult::FinalEncryptedChunk.
Enumsยง
- Decryptor
Initialize ๐With Header Error - Decryptor
State ๐ - Encryptor
State ๐ - Pkcs7
Validation ๐Result
Constantsยง
- AES256_
CBC_ ๐BLOCK_ SIZE - AES256_
CBC_ ๐IV_ SIZE - EMISSION_
CHUNK_ ๐SIZE - HEADER_
LENGTH ๐ - HMAC_
SIZE ๐
Functionsยง
- read_
block_ ๐ciphertext - Reads and removes the first block from the buffer. The size must be checked by the caller before calling this function, and it may panic otherwise.
- read_
header ๐ - read_
plaintext_ ๐block - Reads and removes the first block from the buffer. The size must be checked by the caller before calling this function, and it may panic otherwise.
Type Aliasesยง
- CbcCiphertext
Block ๐ - Iv ๐
- CBC IV
- Mac ๐
- HMAC over IV + Ciphertext
- Stream
Header ๐Bytes - Header is IV || HMAC