Stream and file encryption

Purpose

Encrypting streams and files should be done in chunks. This keeps memory usage low and detects corrupted chunks early. However, this is more complicated than one would expect because you need to prevent chunks from being removed, truncated, duplicated, and reordered.

This high-level APIarrow-up-right uses XChaCha20-Poly1305arrow-up-right internally whilst handling these issues for you. Plaintext chunks can have different sizes, there is no practical stream length limit, associated data is supported, and rekeying is possible midway through encrypting the stream.

The optional associated data is useful for authenticating file headers, version numbers, timestamps, counters, and so on. It can be used to prevent confused deputy attacksarrow-up-right and replay attacksarrow-up-right. It is not encrypted nor part of the ciphertext. It must be reproduceable or stored somewhere for decryption to be possible.

circle-check

Usage

IncrementalXChaCha20Poly1305

Initializes stream encryption or decryption using a key and header. For encryption, the header is filled with a random nonce. It MUST be sent/stored before the sequence of ciphertext messages because it is required to decrypt the stream.

using var secretstream = new IncrementalXChaCha20Poly1305(bool decryption, Span<byte> header, ReadOnlySpan<byte> key)

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

header has a length not equal to HeaderSize.

ArgumentOutOfRangeExceptionarrow-up-right

key has a length not equal to KeySize.

CryptographicExceptionarrow-up-right

Error initializing.

ChunkFlag

Before encryption, a flag is attached to each chunk to provide information about that chunk:

  • ChunkFlag.Message: an ordinary plaintext chunk.

  • ChunkFlag.Boundary: the end of a set of messages but not the end of the stream.

  • ChunkFlag.Rekey: derive a new key after this chunk.

  • ChunkFlag.Final: the last plaintext chunk, marking the end of the stream.

For file encryption, ChunkFlag.Message and ChunkFlag.Final are the only flags required. For online communication, ChunkFlag.Boundary and ChunkFlag.Rekey may also be useful.

triangle-exclamation

Push

Fills a span with the ciphertext chunk computed from a plaintext chunk and optional associated data. ChunkFlag.Final MUST be specified for the last plaintext chunk.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

ciphertextChunk has a length not equal to plaintextChunk.Length + TagSize.

InvalidOperationExceptionarrow-up-right

Cannot push into a decryption stream or push after ChunkFlag.Final.

ObjectDisposedExceptionarrow-up-right

The object has been disposed.

CryptographicExceptionarrow-up-right

Encryption failed.

Pull

Verifies that the tag appended to the ciphertext chunk is correct for the given inputs. If verification fails, an exception is thrown. Otherwise, it fills a span with the decrypted ciphertext and returns the decrypted flag for that chunk.

To detect stream truncation, if you reach the end of the stream and ChunkFlag.Final was not returned, you MUST throw a CryptographicExceptionarrow-up-right.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

plaintextChunk has a length not equal to ciphertextChunk.Length - TagSize.

InvalidOperationExceptionarrow-up-right

Cannot pull from an encryption stream or pull after ChunkFlag.Final.

ObjectDisposedExceptionarrow-up-right

The object has been disposed.

CryptographicExceptionarrow-up-right

Invalid authentication tag for the given inputs.

Rekey

Explicitly rekeys without storing the ChunkFlag.Rekey flag. For decryption, this function MUST be called at the same stream location to avoid an exception when decrypting the following chunk.

Exceptions

InvalidOperationExceptionarrow-up-right

Cannot rekey after ChunkFlag.Final.

ObjectDisposedExceptionarrow-up-right

The object has been disposed.

Reinitialize

Reinitializes stream encryption or decryption using a key and header. This avoids creating another using statement. For encryption, the header is filled with a random nonce. It MUST be sent/stored before the sequence of ciphertext messages because it is required to decrypt the stream.

Exceptions

ArgumentOutOfRangeExceptionarrow-up-right

header has a length not equal to HeaderSize.

ArgumentOutOfRangeExceptionarrow-up-right

key has a length not equal to KeySize.

ObjectDisposedExceptionarrow-up-right

The object has been disposed.

CryptographicExceptionarrow-up-right

Error reinitializing.

Constants

These are used for validation and/or save you defining your own constants.

Notes

triangle-exclamation
triangle-exclamation
circle-exclamation

Last updated