Table of Contents

Salsa20 Class

Definition

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

Provides a managed implementation of the Salsa20 stream cipher specified by Daniel J. Bernstein. This class cannot be inherited.

public sealed class Salsa20 : SymmetricStreamAlgorithm, IDisposable
Inheritance
Salsa20
Implements
Inherited Members
Extension Methods

Examples

using Bodu.Security.Cryptography;
using Bodu.Security.Cryptography.Extensions;

using var salsa = new Salsa20();
salsa.GenerateKey(); // 256-bit
salsa.GenerateNonce(); // 64-bit nonce
byte[] ciphertext = salsa.Encrypt(plaintext);
byte[] roundTrip  = salsa.Decrypt(ciphertext);

Remarks

Salsa20 is an additive stream cipher and an eSTREAM software-portfolio finalist. It uses a 128- or 256-bit key, a 64-bit nonce, and a 64-bit block counter to generate a keystream that is XORed with the plaintext. This class exposes the raw, confidentiality-only cipher with no authentication; for new designs prefer an AEAD construction, or the closely related ChaCha20 which has largely superseded Salsa20.

Like the other stream ciphers in this library it is self-inverse: CreateEncryptor() and CreateDecryptor() are interchangeable. The nonce is supplied through the Nonce property.

Parameters at a glance.

  • Key size: 128 or 256 bits (16 or 32 bytes); 256-bit is the default.
  • Nonce (IV) size: 64 bits (8 bytes).
  • Block counter: 64-bit, starting at InitialCounter (default 0).

Nonce reuse is catastrophic. A given (key, nonce) pair must encrypt at most one message. The 64-bit nonce is too short to generate randomly without collision risk; for random nonces prefer the extended-nonce XSalsa20 variant.

Constructors

Salsa20()

Initializes a new instance of the Salsa20 class with default parameters.

public Salsa20()

Remarks

The default configuration uses a 256-bit key and a 64-bit nonce, with the block counter starting at 0. Assign KeySize to 128 before generating or supplying a key to use a 128-bit key.

Properties

InitialCounter

Gets or sets the initial 64-bit block counter used when generating the keystream.

public ulong InitialCounter { get; set; }

Property Value

ulong

The starting block-counter value. The default is 0.

Remarks

eSTREAM test vectors start the counter at 0. Set this before creating an encryptor or decryptor when matching an external counter convention.

Methods

Create()

Creates a new Salsa20 instance with default parameters.

public static Salsa20 Create()

Returns

Salsa20

A new Salsa20 instance.

CreateStreamCipher(byte[], byte[])

Builds a configured IStreamCipher engine from the validated key and nonce.

protected override IStreamCipher CreateStreamCipher(byte[] key, byte[] nonce)

Parameters

key byte[]

The key, already validated to the algorithm's key size.

nonce byte[]

The nonce, already validated to the algorithm's nonce size.

Returns

IStreamCipher

A new IStreamCipher engine positioned at the start of its keystream.

Remarks

Implementations receive a key and nonce whose lengths have already been checked by the base class, so they need only construct their engine. Ownership of the returned engine transfers to the caller.

Applies to

ProductVersions
.NET8, 10

See Also