Table of Contents

IStreamCipher Interface

Definition

Namespace
Bodu.Security.Cryptography
Assembly
Bodu.Security.Cryptography.dll
Package
Bodu.Security.Cryptography 1.2.0
Source
IStreamCipher.cs

Defines a synchronous, additive stream cipher engine that produces a key- and nonce-dependent keystream in fixed-size blocks.

public interface IStreamCipher : IDisposable
Inherited Members
Extension Methods

Remarks

Implementations represent primitives such as ChaCha20, Salsa20, Rabbit, and HC-128. Unlike IBlockCipher, a stream cipher never observes plaintext: it emits a pseudo-random keystream that the caller combines with the message by XOR. Confidentiality therefore depends entirely on never reusing a (key, nonce) pair, because the XOR of two ciphertexts produced under the same keystream reveals the XOR of their plaintexts.

The key and nonce are bound when the implementation is constructed, mirroring the way an IBlockCipher binds its key. The engine owns its own keystream position: each call to NextKeystreamBlock(Span<byte>) emits the next block and advances the internal counter or state. This keeps the abstraction uniform across ciphers with a seekable block counter (ChaCha20, Salsa20) and those whose keystream is produced by an evolving internal state with no random access (Rabbit, HC-128). Partial-block buffering across calls is the responsibility of the higher-level Bodu.Security.Cryptography.StreamCipherTransform.

How this fits with the rest of the library. IStreamCipher is the stream-cipher counterpart to IBlockCipher. A Bodu.Security.Cryptography.StreamCipherTransform wraps it to satisfy the ICryptoTransform contract, and a SymmetricStreamAlgorithm composes the two so callers can use it through a CryptoStream like any other algorithm in this library.

Implementations must release all sensitive key material when Dispose() is called.

Properties

BlockSize

Gets the keystream block size, in bytes (for example, 64 bytes for ChaCha20).

int BlockSize { get; }

Property Value

int

The keystream block size, in bytes.

Remarks

The block size is expressed in bytes - not bits - because stream ciphers operate on byte-granular keystream segments rather than the bit-oriented block sizes reported by BlockSize.

Methods

NextKeystreamBlock(Span<byte>)

Emits the next keystream block and advances the engine's internal counter or state.

void NextKeystreamBlock(Span<byte> destination)

Parameters

destination Span<byte>

A writable span that receives the keystream. Its length must be at least BlockSize.

Remarks

The keystream depends only on the bound key and nonce and the number of blocks already emitted; it is independent of any message data. Because the engine advances its own position, successive calls yield successive, non-overlapping keystream blocks.

Exceptions

ArgumentException

Thrown if destination is shorter than BlockSize.

CryptographicException

Thrown if the keystream is exhausted - for example, a fixed-width block counter would wrap and reuse earlier keystream. Continuing past this point would compromise confidentiality.

Applies to

ProductVersions
.NET8, 10

See Also

IBlockCipher
StreamCipherTransform