Serpent Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
- Source
- Serpent.cs
Serves as the abstract base class for the non-standard wide-block tweakable Serpent variants ( Serpent256, Serpent512, and Serpent1024).
public abstract class Serpent : TweakableSymmetricAlgorithm, IDisposable
- Inheritance
-
Serpent
- Implements
- Derived
- Inherited Members
- Extension Methods
Examples
// Use a concrete wide-block variant - Serpent-256 over a CTR mode.
using TweakableSymmetricAlgorithm alg = new Serpent256();
alg.GenerateKey();
alg.GenerateIV();
alg.GenerateTweak();
alg.BlockMode = CipherModeKind.CTR;
using ICryptoTransform encryptor = alg.CreateEncryptor(alg.Key, alg.IV, alg.Tweak);
using var cipherText = new MemoryStream();
using (var cs = new CryptoStream(cipherText, encryptor, CryptoStreamMode.Write))
cs.Write(plaintext, 0, plaintext.Length);
// For standard, externally vetted Serpent use Serpent128 instead - the wide-block variants
// are experimental and not interoperable with reference Serpent implementations.
Remarks
Each variant accepts a key whose size in bits matches its block size (256, 512, or 1024 bits) together with a 128-bit tweak. Derived classes must implement CreateCipher(byte[], byte[]) to instantiate the appropriate concrete engine.
The BlockMode property replaces the standard Mode property, enabling the use of additional or non-standard block cipher modes such as CTR and OFB.
important
The wide-block Serpent family is a **non-standard, experimental construction** and is not interoperable with any reference Serpent implementation. For standard, externally vetted Serpent, use Serpent128.
The underlying block-cipher implementation computes each S-box as a Boolean circuit (Osvik's) and the linear transform with rotations, shifts and XOR, so it reads no tables and takes no branches that depend on the key or the data: its running time does not depend on either.
Constructors
Serpent(int, int)
Initializes a new instance of the Serpent class with the specified block and tweak sizes.
protected Serpent(int blockSizeBits, int tweakSizeBits)
Parameters
Fields
BlockSizeBytes
The block size in bytes.
protected readonly int BlockSizeBytes
Field Value
KeySizeBytes
The key size in bytes.
protected readonly int KeySizeBytes
Field Value
Properties
BlockMode
Gets or sets the block cipher mode of operation used when creating encryptors and decryptors.
public CipherModeKind BlockMode { get; set; }
Property Value
- CipherModeKind
One of the CipherModeKind values. The default is CBC.
Remarks
This property replaces the inherited Mode property when used with BlockCipherModeFactory and the extended set of modes it supports, including CTR and OFB.
Methods
CreateCipher(byte[], byte[])
Instantiates the concrete Serpent block cipher with the specified key and tweak.
protected abstract IBlockCipher CreateCipher(byte[] key, byte[] tweak)
Parameters
Returns
- IBlockCipher
A configured IBlockCipher instance for encryption or decryption.
Exceptions
- ArgumentNullException
Thrown when
keyis null.- CryptographicException
Thrown when the underlying cryptographic algorithm fails.
CreateDecryptor(byte[], byte[]?, byte[])
Creates a symmetric decryptor using the specified key, initialization vector (IV), and tweak value.
public override ICryptoTransform CreateDecryptor(byte[] rgbKey, byte[]? rgbIV, byte[] tweak)
Parameters
rgbKeybyte[]The secret key to use for decryption.
rgbIVbyte[]The initialization vector to use for the decryption operation.
tweakbyte[]The tweak value that modifies the decryption process.
Returns
- ICryptoTransform
An ICryptoTransform instance that can be used to perform the decryption.
Remarks
This method must be implemented by derived types to support decryption with a tweak, as required by tweakable block ciphers such as Threefish.
Exceptions
- ArgumentNullException
Thrown if
rgbKey,rgbIV, ortweakis null.- CryptographicException
Thrown if any input does not conform to the expected size, format, or algorithm-specific constraints.
CreateEncryptor(byte[], byte[]?, byte[])
Creates a symmetric encryptor using the specified key, initialization vector (IV), and tweak value.
public override ICryptoTransform CreateEncryptor(byte[] rgbKey, byte[]? rgbIV, byte[] tweak)
Parameters
rgbKeybyte[]The secret key to use for encryption.
rgbIVbyte[]The initialization vector to use for the encryption operation.
tweakbyte[]The tweak value that modifies the encryption process.
Returns
- ICryptoTransform
An ICryptoTransform instance that can be used to perform the encryption.
Remarks
This method must be implemented by derived types to support encryption with a tweak, as required by tweakable block ciphers such as Threefish.
Exceptions
- ArgumentNullException
Thrown if
rgbKey,rgbIV, ortweakis null.- CryptographicException
Thrown if any input does not conform to the expected size, format, or algorithm-specific constraints.
Dispose(bool)
Releases the unmanaged resources used by the SymmetricAlgorithm and optionally releases the managed resources.
protected override void Dispose(bool disposing)
Parameters
disposingbooltrue to release both managed and unmanaged resources; false to release only unmanaged resources.
Remarks
Marks the instance as disposed, zeroes any retained key and IV buffers, and delegates the tweak buffer cleanup to the base implementation.
GenerateIV()
When overridden in a derived class, generates a random initialization vector (IV) to use for the algorithm.
public override void GenerateIV()
GenerateKey()
When overridden in a derived class, generates a random key (Key) to use for the algorithm.
public override void GenerateKey()
GenerateTweak()
Generates a new tweak value for the algorithm based on the current TweakSize.
public override void GenerateTweak()
Remarks
This method initializes the tweak with random or algorithm-specific data. The generated size will match the current TweakSize. If no size has been set, an exception will be thrown.
Exceptions
- CryptographicException
Thrown if TweakSize is not configured to a valid size.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |