SerpentBlockCipherBase Class
Definition
- Namespace
- Bodu.Security.Cryptography
- Assembly
- Bodu.Security.Cryptography.dll
- Package
- Bodu.Security.Cryptography 1.2.0
Serves as the abstract base class for managed Serpent block cipher engines, providing the shared S-boxes, bitsliced
linear transform, prekey recurrence, and resource-disposal plumbing used by the standard Serpent-128 variant
and the non-standard wide-block tweakable variants (Serpent-256, Serpent-512, Serpent-1024).
public abstract class SerpentBlockCipherBase : IBlockCipher, IDisposable
- Inheritance
-
SerpentBlockCipherBase
- Implements
- Derived
- Inherited Members
- Extension Methods
Remarks
Derived classes supply the state width (in 32-bit words), the round count, and their own Encrypt(ReadOnlySpan<byte>, Span<byte>) and
Decrypt(ReadOnlySpan<byte>, Span<byte>) implementations, whose rounds run in Bodu.Security.Cryptography.SerpentCore. The base class exposes the
Serpent S-boxes S0..S7 the key schedules apply (Osvik's Boolean circuits, from Bodu.Security.Cryptography.SerpentCore)
and the round-key expansion helper ExpandPrekeys(ReadOnlySpan<uint>, Span<uint>, int) driven by the golden-ratio constant
phi = 0x9E3779B9.
Serpent operates on four 32-bit words in bitsliced form. Each bit position across those four words represents one 4-bit S-box input. The helpers in this base class keep that representation explicit so the concrete ciphers can share the standard key-schedule operations.
External callers cannot derive new variants: the constructor and protected members are scoped
private protected. Use Serpent128Cipher or one of the wide-block
Serpent256Cipher / Serpent512Cipher / Serpent1024Cipher types directly,
or compose with IBlockCipherModeTransform via BlockCipherModeFactory.
Constructors
SerpentBlockCipherBase()
protected SerpentBlockCipherBase()
Properties
BlockSize
Gets the block size, in bits, of the cipher (for example, 128 bits / 16 bytes for AES).
public abstract int BlockSize { get; }
Property Value
- int
The block size, in bits.
Remarks
The block size is expressed in bits to align with the BCL convention used by
BlockSize. Byte-array operations (encrypt,
decrypt, slice) convert to bytes at the call site as BlockSize / 8.
Methods
Decrypt(ReadOnlySpan<byte>, Span<byte>)
Decrypts a single block of ciphertext into the specified output span.
public abstract void Decrypt(ReadOnlySpan<byte> input, Span<byte> output)
Parameters
inputReadOnlySpan<byte>A read-only span containing the ciphertext block. Its byte length must equal BlockSize / 8.
outputSpan<byte>A writable span that receives the plaintext block. Its byte length must equal BlockSize / 8.
Remarks
In-place decryption (passing the same buffer as both input and output)
is supported only when the implementation explicitly permits it; otherwise the spans must not overlap.
Exceptions
- ArgumentException
Thrown if the length of
inputoroutputdoes not match BlockSize.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Dispose(bool)
Releases all internal buffers and sensitive material.
protected virtual void Dispose(bool disposing)
Parameters
Remarks
The base implementation records the disposed state. Derived Serpent implementations should override this method
to clear expanded round keys, tweak material, and other sensitive buffers before calling
base.Dispose(disposing).
Encrypt(ReadOnlySpan<byte>, Span<byte>)
Encrypts a single block of plaintext into the specified output span.
public abstract void Encrypt(ReadOnlySpan<byte> input, Span<byte> output)
Parameters
inputReadOnlySpan<byte>A read-only span containing the plaintext block. Its byte length must equal BlockSize / 8.
outputSpan<byte>A writable span that receives the ciphertext block. Its byte length must equal BlockSize / 8.
Remarks
In-place encryption (passing the same buffer as both input and output)
is supported only when the implementation explicitly permits it; otherwise the spans must not overlap.
Exceptions
- ArgumentException
Thrown if the length of
inputoroutputdoes not match BlockSize.
~SerpentBlockCipherBase()
Finalizes an instance of the SerpentBlockCipherBase class.
protected ~SerpentBlockCipherBase()
Remarks
The finalizer delegates to the standard dispose pattern so derived cipher implementations can clear round-key material even if callers fail to dispose the instance explicitly.
ThrowIfDisposed()
Throws an ObjectDisposedException if the algorithm instance has been disposed.
protected void ThrowIfDisposed()
Exceptions
- ObjectDisposedException
Thrown when any public method or property is accessed after the instance has been disposed.
Applies to
| Product | Versions |
|---|---|
| .NET | 8, 10 |