Table of Contents

Class ChaCha20

Namespace
CryptoHives.Foundation.Security.Cryptography.Cipher
Assembly
CryptoHives.Foundation.Security.Cryptography.dll

ChaCha20 stream cipher implementation as specified in RFC 8439.

public sealed class ChaCha20 : SymmetricCipher, IDisposable
Inheritance
ChaCha20
Implements
Inherited Members

Remarks

ChaCha20 is a high-speed stream cipher designed by Daniel J. Bernstein. It uses a 256-bit key and a 96-bit nonce (IETF variant).

Security properties:

  • 256-bit security level
  • No weak keys
  • Resistant to timing attacks (no table lookups)
  • Efficient in software on all platforms

Important: Never reuse a (key, nonce) pair. Each encryption must use a unique nonce with the same key.

Example usage:

using var chacha = ChaCha20.Create();
chacha.GenerateKey();
chacha.GenerateIV(); // Generates 12-byte nonce

byte[] ciphertext = chacha.Encrypt(plaintext); byte[] decrypted = chacha.Decrypt(ciphertext);

Constructors

ChaCha20()

Initializes a new instance of the ChaCha20 class.

public ChaCha20()

Fields

KeySizeBits

Key size in bits for ChaCha20 (256 bits).

public const int KeySizeBits = 256

Field Value

int

KeySizeConst

Key size in bytes for ChaCha20 (32 bytes).

public const int KeySizeConst = 32

Field Value

int

NonceSizeConst

Nonce size in bytes for ChaCha20 IETF variant (12 bytes).

public const int NonceSizeConst = 12

Field Value

int

Properties

AlgorithmName

Gets the name of the cipher algorithm.

public override string AlgorithmName { get; }

Property Value

string

Examples

"AES-256", "ChaCha20", "AES-256-GCM"

IVSize

Gets the required IV/nonce size in bytes for the current mode.

public override int IVSize { get; }

Property Value

int

InitialCounter

Gets or sets the initial block counter value for stream ciphers.

public override uint InitialCounter { get; set; }

Property Value

uint

Remarks

The default value is 0. Some protocols (like TLS) use 1 as the initial counter. Block ciphers ignore this property.

Methods

CalculateOutputSize(int, bool)

Calculates the output buffer size required for the given input size.

protected override int CalculateOutputSize(int inputLength, bool encrypting)

Parameters

inputLength int

The input data length in bytes.

encrypting bool

True if encrypting, false if decrypting.

Returns

int

The required output buffer size in bytes.

Create()

Creates a new instance of the ChaCha20 cipher.

public static ChaCha20 Create()

Returns

ChaCha20

A new ChaCha20 cipher instance.

CreateCipherDecryptor(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Creates a decryptor transform using the specified key and IV.

protected override ICipherTransform CreateCipherDecryptor(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)

Parameters

key ReadOnlySpan<byte>

The secret key.

iv ReadOnlySpan<byte>

The initialization vector or nonce.

Returns

ICipherTransform

A new cipher decryptor transform.

CreateCipherEncryptor(ReadOnlySpan<byte>, ReadOnlySpan<byte>)

Creates an encryptor transform using the specified key and IV.

protected override ICipherTransform CreateCipherEncryptor(ReadOnlySpan<byte> key, ReadOnlySpan<byte> iv)

Parameters

key ReadOnlySpan<byte>

The secret key.

iv ReadOnlySpan<byte>

The initialization vector or nonce.

Returns

ICipherTransform

A new cipher encryptor transform.

ValidateIVSize(int)

Validates that the specified IV size is valid for this algorithm and mode.

protected override void ValidateIVSize(int byteLength)

Parameters

byteLength int

The IV size in bytes.

Exceptions

CryptographicException

The IV size is invalid.