Table of Contents

Class AesCcm

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

AES-CCM (Counter with CBC-MAC) authenticated encryption implementation.

public abstract class AesCcm : IAeadCipher, IDisposable
Inheritance
AesCcm
Implements
Derived
Inherited Members

Remarks

AES-CCM provides authenticated encryption with associated data (AEAD) by combining CBC-MAC for authentication with CTR mode for encryption. It is widely used in wireless protocols (802.11i, Bluetooth LE), IoT, and constrained environments.

Specification: RFC 3610, NIST SP 800-38C

Security properties:

  • Authenticated encryption with associated data (AEAD)
  • Authenticate-then-encrypt construction
  • Variable tag length (4-16 bytes, even values)
  • Variable nonce length (7-13 bytes)

Important: Never reuse a (key, nonce) pair. Each encryption must use a unique nonce. For typical usage, 12-byte nonces are recommended.

Example usage:

using var ccm = AesCcm128.Create(key);

byte[] nonce = new byte[12]; RandomNumberGenerator.Fill(nonce);

// Encrypt with 16-byte tag byte[] ciphertext = new byte[plaintext.Length]; byte[] tag = new byte[16]; ccm.Encrypt(nonce, plaintext, ciphertext, tag, associatedData);

// Decrypt and verify if (ccm.Decrypt(nonce, ciphertext, tag, plaintext, associatedData)) { // Authentication successful, plaintext is valid }

Constructors

AesCcm(ReadOnlySpan<byte>)

Initializes a new instance of the AesCcm class.

protected AesCcm(ReadOnlySpan<byte> key)

Parameters

key ReadOnlySpan<byte>

The AES key (16, 24, or 32 bytes).

Properties

AlgorithmName

Gets the algorithm name.

public abstract string AlgorithmName { get; }

Property Value

string

KeySizeBytes

Gets the key size in bytes.

public abstract int KeySizeBytes { get; }

Property Value

int

NonceSizeBytes

Gets the nonce size in bytes.

public int NonceSizeBytes { get; }

Property Value

int

TagSizeBytes

Gets the authentication tag size in bytes.

public int TagSizeBytes { get; }

Property Value

int

Methods

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

nonce ReadOnlySpan<byte>

The nonce used during encryption.

ciphertextWithTag ReadOnlySpan<byte>

The ciphertext with appended authentication tag.

associatedData ReadOnlySpan<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 ciphertext and verifies authentication tag.

public bool Decrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> ciphertext, ReadOnlySpan<byte> tag, Span<byte> plaintext, ReadOnlySpan<byte> associatedData = default)

Parameters

nonce ReadOnlySpan<byte>

The nonce (7-13 bytes, typically 12).

ciphertext ReadOnlySpan<byte>

The ciphertext to decrypt.

tag ReadOnlySpan<byte>

The authentication tag to verify (4-16 bytes, even).

plaintext Span<byte>

Output buffer for plaintext (must be same size as ciphertext).

associatedData ReadOnlySpan<byte>

Additional authenticated data (optional).

Returns

bool

True if authentication succeeded; otherwise false.

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Dispose(bool)

Releases resources used by this instance.

protected virtual void Dispose(bool disposing)

Parameters

disposing bool

True if called from Dispose(), false if from finalizer.

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

nonce ReadOnlySpan<byte>

The unique nonce for this encryption.

plaintext ReadOnlySpan<byte>

The data to encrypt.

associatedData ReadOnlySpan<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 plaintext and computes authentication tag.

public void Encrypt(ReadOnlySpan<byte> nonce, ReadOnlySpan<byte> plaintext, Span<byte> ciphertext, Span<byte> tag, ReadOnlySpan<byte> associatedData = default)

Parameters

nonce ReadOnlySpan<byte>

The nonce (7-13 bytes, typically 12).

plaintext ReadOnlySpan<byte>

The plaintext to encrypt.

ciphertext Span<byte>

Output buffer for ciphertext (must be same size as plaintext).

tag Span<byte>

Output buffer for authentication tag (4-16 bytes, even).

associatedData ReadOnlySpan<byte>

Additional authenticated data (optional).

Exceptions

ObjectDisposedException

Thrown when the instance has been disposed.