Class AsconAead128
- Namespace
- CryptoHives.Foundation.Security.Cryptography.Cipher
- Assembly
- CryptoHives.Foundation.Security.Cryptography.dll
Ascon-AEAD128 authenticated encryption as specified in NIST SP 800-232.
public sealed class AsconAead128 : IAeadCipher, IDisposable
- Inheritance
-
AsconAead128
- Implements
- Inherited Members
Remarks
Ascon-AEAD128 is the primary AEAD algorithm from the NIST Lightweight Cryptography standard (SP 800-232). It provides authenticated encryption with associated data using a 128-bit key, 128-bit nonce, and 128-bit authentication tag.
Ascon was the winner of the NIST Lightweight Cryptography competition (2023), designed for constrained environments while maintaining strong security guarantees.
Security properties:
- 128-bit security level
- 128-bit authentication tag
- Sponge-based construction with 320-bit state
- Resistant to timing attacks (no table lookups)
Important: Never reuse a (key, nonce) pair. Each encryption must use a unique 128-bit (16-byte) nonce.
Example usage:
using var aead = AsconAead128.Create(key);
// Encrypt with associated data
byte[] ciphertext = aead.Encrypt(nonce, plaintext, associatedData);
// Decrypt and verify
byte[] plaintext = aead.Decrypt(nonce, ciphertext, associatedData);
References:
Constructors
AsconAead128(byte[])
Initializes a new instance of the AsconAead128 class.
public AsconAead128(byte[] key)
Parameters
keybyte[]The 16-byte key.
Exceptions
- ArgumentNullException
keyis null.- ArgumentException
keyis not 16 bytes.
Fields
KeySizeBytesConst
Key size in bytes (128 bits).
public const int KeySizeBytesConst = 16
Field Value
NonceSizeBytesConst
Nonce size in bytes (128 bits).
public const int NonceSizeBytesConst = 16
Field Value
TagSizeBytesConst
Tag size in bytes (128 bits).
public const int TagSizeBytesConst = 16
Field Value
Properties
AlgorithmName
Gets the algorithm name.
public string AlgorithmName { get; }
Property Value
KeySizeBytes
Gets the key size in bytes.
public int KeySizeBytes { get; }
Property Value
NonceSizeBytes
Gets the nonce size in bytes.
public int NonceSizeBytes { get; }
Property Value
TagSizeBytes
Gets the authentication tag size in bytes.
public int TagSizeBytes { get; }
Property Value
Methods
Create(byte[])
Creates a new Ascon-AEAD128 instance.
public static AsconAead128 Create(byte[] key)
Parameters
keybyte[]The 16-byte key.
Returns
- AsconAead128
A new Ascon-AEAD128 instance.
Decrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Decrypts the ciphertext (with appended tag) and verifies authenticity.
public byte[] Decrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertextWithTag, ReadOnlySpan<byte> associatedData = default)
Parameters
nonceReadOnlySpan<byte>The nonce used during encryption.
ciphertextWithTagReadOnlySpan<byte>The ciphertext with appended authentication tag.
associatedDataReadOnlySpan<byte>Additional authenticated data (must match encryption).
Returns
- byte[]
The decrypted plaintext.
Exceptions
- CryptographicException
Authentication failed - the data has been tampered with or the wrong key/nonce was used.
Decrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, ReadOnlySpan<byte>)
Decrypts the ciphertext and verifies the authentication tag.
public bool Decrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, ReadOnlySpan<byte> associatedData = default)
Parameters
nonceReadOnlySpan<byte>The nonce used during encryption.
ciphertextReadOnlySpan<byte>The encrypted data.
tagReadOnlySpan<byte>The authentication tag to verify.
plaintextSpan<byte>The output buffer for decrypted data (same size as ciphertext).
associatedDataReadOnlySpan<byte>Additional authenticated data (must match encryption).
Returns
- bool
True if decryption and authentication succeeded; false if authentication failed.
Exceptions
- ArgumentException
nonceortagis not the correct size, orplaintextbuffer is too small.
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
Encrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, ReadOnlySpan<byte>)
Encrypts the plaintext and returns ciphertext with appended tag.
public byte[] Encrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, ReadOnlySpan<byte> associatedData = default)
Parameters
nonceReadOnlySpan<byte>The unique nonce for this encryption.
plaintextReadOnlySpan<byte>The data to encrypt.
associatedDataReadOnlySpan<byte>Additional data to authenticate (optional).
Returns
- byte[]
The ciphertext with authentication tag appended.
Encrypt(ReadOnlySpan<byte>, ReadOnlySpan<byte>, Span<byte>, Span<byte>, ReadOnlySpan<byte>)
Encrypts the plaintext and computes the authentication tag.
public void Encrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag, ReadOnlySpan<byte> associatedData = default)
Parameters
nonceReadOnlySpan<byte>The unique nonce for this encryption.
plaintextReadOnlySpan<byte>The data to encrypt.
ciphertextSpan<byte>The output buffer for ciphertext (same size as plaintext).
tagSpan<byte>The output buffer for the authentication tag.
associatedDataReadOnlySpan<byte>Additional data to authenticate (optional).
Exceptions
- ArgumentException
nonceis not the correct size, orciphertextortagbuffers are too small.