AES-CCM
Header: #include <cryptopp/aes.h> and #include <cryptopp/ccm.h>
Namespace: CryptoPP Since: Crypto++ 5.6.0 [1]
AES-CCM (AES in Counter with CBC-MAC Mode) is an authenticated encryption algorithm that provides both confidentiality and authenticity [2]. NIST SP 800-38C specifies it and states that its CCM is intended to be compatible with the use of CCM in the IEEE 802.11 wireless LAN amendment [2]. TLS defines AES-CCM cipher suites for TLS 1.2 and TLS 1.3 [3][4].
Quick Example
#include <cryptopp/aes.h>
#include <cryptopp/ccm.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <iostream>
int main() {
using namespace CryptoPP;
// Generate random key and nonce
AutoSeededRandomPool rng;
SecByteBlock key(AES::DEFAULT_KEYLENGTH); // 16 bytes
byte nonce[12]; // CCM nonce: 7-13 bytes (12 common)
rng.GenerateBlock(key, key.size());
rng.GenerateBlock(nonce, sizeof(nonce));
std::string plaintext = "Hello, World!";
std::string ciphertext, decrypted;
const int TAG_SIZE = 16; // Authentication tag size
// Encrypt
CCM<AES, TAG_SIZE>::Encryption enc;
enc.SetKeyWithIV(key, key.size(), nonce, sizeof(nonce));
enc.SpecifyDataLengths(0, plaintext.size()); // CCM requires lengths upfront
StringSource(plaintext, true,
new AuthenticatedEncryptionFilter(enc,
new StringSink(ciphertext)
)
);
// Decrypt
CCM<AES, TAG_SIZE>::Decryption dec;
dec.SetKeyWithIV(key, key.size(), nonce, sizeof(nonce));
dec.SpecifyDataLengths(0, ciphertext.size() - TAG_SIZE);
StringSource(ciphertext, true,
new AuthenticatedDecryptionFilter(dec,
new StringSink(decrypted)
)
);
std::cout << "Decrypted: " << decrypted << std::endl;
return 0;
}Usage Guidelines
Do:
- Use CCM when protocol compatibility requires it
- Always specify data lengths before encryption/decryption [5]
- Use unique nonces for each encryption with the same key [2]
- Use 128-bit or 256-bit keys for new systems
Avoid:
- Using CCM for streaming data; it is not designed for partial or stream processing
- Reusing nonces with the same key [2]
- Ignoring authentication failures during decryption
Where you are free to choose, AES-GCM processes data on-line and in parallel [6], and EAX is on-line [7]; CCM needs all the data before it starts [2].
Key Difference: CCM Requires Pre-specified Lengths
Unlike GCM and EAX, CCM requires you to know the plaintext and AAD lengths before encryption begins [2][6][7]. In the library, NeedsPrespecifiedDataLengths() returns true for CCM [5].
// REQUIRED for CCM - must call before processing data
enc.SpecifyDataLengths(aadLength, plaintextLength);If the lengths you pass do not match the data you process, the library throws InvalidArgument [8].
If you don’t know the data length upfront, use AES-GCM or AES-EAX instead.
Class: CCM<AES, TAG_SIZE>
Template class for AES in CCM authenticated encryption mode.
Template Parameters
template <class T_BlockCipher, int T_DefaultDigestSize = 16>
struct CCM {
typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, true> Encryption;
typedef CCM_Final<T_BlockCipher, T_DefaultDigestSize, false> Decryption;
};T_BlockCipher- The underlying block cipher (must have 16-byte block size, e.g.,AES)T_DefaultDigestSize- Authentication tag size in bytes (4, 6, 8, 10, 12, 14, or 16) [8]
Key Sizes
| Key Size | Constant | Recommended |
|---|---|---|
| 128-bit | AES::DEFAULT_KEYLENGTH (16) | Acceptable |
| 192-bit | 24 bytes | Rare |
| 256-bit | AES::MAX_KEYLENGTH (32) | ✓ Recommended |
The recommendation column is our advice; see Security Levels for what the key sizes mean.
Constants
// AES constants
AES::MIN_KEYLENGTH // 16 bytes (128 bits)
AES::MAX_KEYLENGTH // 32 bytes (256 bits)
AES::DEFAULT_KEYLENGTH // 16 bytes (128 bits)
AES::BLOCKSIZE // 16 bytes, required by CCM
// CCM-specific
// Nonce (IV) size: 7-13 bytes
// Tag sizes: 4, 6, 8, 10, 12, 14, or 16 bytes
The AES values come from Rijndael_Info [9]; the nonce and tag ranges come from the CCM implementation [5][8].
Valid Tag Sizes
CCM only supports specific tag sizes [2][8]:
| Tag Size | Tag bits |
|---|---|
| 4 bytes | 32-bit |
| 6 bytes | 48-bit |
| 8 bytes | 64-bit |
| 10 bytes | 80-bit |
| 12 bytes | 96-bit |
| 14 bytes | 112-bit |
| 16 bytes | 128-bit (✓ Recommended) |
CCM<AES>::Encryption
Authenticated encryption class.
Methods
SetKeyWithIV()
void SetKeyWithIV(const byte* key, size_t keyLength,
const byte* iv, size_t ivLength);Set encryption key and nonce.
Parameters:
key- Encryption key (16, 24, or 32 bytes)keyLength- Length of key in bytesiv- Nonce (7-13 bytes)ivLength- Length of nonce in bytes
Nonce size constraints [2][5]:
- Minimum: 7 bytes
- Maximum: 13 bytes
- Common: 12 bytes (allows messages up to 2^24 - 1 bytes)
The nonce size n fixes the length field q = 15 - n, and the payload must be shorter than 2^(8q) bytes [2]:
| Nonce Size | Max Message Length |
|---|---|
| 7 bytes | 2^64 - 1 bytes |
| 8 bytes | 2^56 - 1 bytes |
| … | … |
| 12 bytes | 2^24 - 1 bytes (~16 MB) |
| 13 bytes | 2^16 - 1 bytes (~64 KB) |
SpecifyDataLengths() - REQUIRED
void SpecifyDataLengths(lword headerLength, lword messageLength,
lword footerLength = 0);Must be called before processing any data. Specifies the lengths of AAD and plaintext [5].
Parameters:
headerLength- Length of AAD in bytes (0 if none)messageLength- Length of plaintext in bytesfooterLength- Footer length (usually 0)
Example:
CCM<AES>::Encryption enc;
enc.SetKeyWithIV(key, sizeof(key), nonce, sizeof(nonce));
// REQUIRED: specify lengths before encryption
enc.SpecifyDataLengths(0, plaintext.size());
// Now proceed with encryption
IVSize() / MinIVLength() / MaxIVLength()
unsigned int IVSize() const; // Default: 8 bytes
unsigned int MinIVLength() const; // Returns 7
unsigned int MaxIVLength() const; // Returns 13
These values are defined in ccm.h [5].
DigestSize()
unsigned int DigestSize() const;Returns the authentication tag size (template parameter, default 16) [5][8].
CCM<AES>::Decryption
Authenticated decryption class.
Methods
SetKeyWithIV()
Same as encryption.
SpecifyDataLengths() - REQUIRED
void SpecifyDataLengths(lword headerLength, lword messageLength,
lword footerLength = 0);Must be called before processing any data. For decryption, messageLength is the plaintext length (ciphertext length minus tag size).
CCM<AES, 16>::Decryption dec;
dec.SetKeyWithIV(key, sizeof(key), nonce, sizeof(nonce));
// messageLength = ciphertext.size() - TAG_SIZE
dec.SpecifyDataLengths(aadLength, ciphertext.size() - 16);Complete Example: Packet Encryption
Each packet nonce is built from a packet number that must never repeat under the key. The example uses an 8-byte tag, the length used by the _CCM_8 TLS cipher suites [3][4].
#include <cryptopp/aes.h>
#include <cryptopp/ccm.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/hex.h>
#include <iostream>
#include <vector>
using namespace CryptoPP;
class PacketEncryptor {
public:
static const int TAG_SIZE = 8; // 8-byte tag
static const int NONCE_SIZE = 13;
PacketEncryptor(const SecByteBlock& key) : key_(key) {
if (key.size() != 16 && key.size() != 32) {
throw std::invalid_argument("Key must be 16 or 32 bytes");
}
}
// Encrypt a packet with header (AAD) and payload
std::vector<byte> encryptPacket(const std::vector<byte>& header,
const std::vector<byte>& payload,
uint64_t packetNumber) {
// Create nonce from packet number
byte nonce[NONCE_SIZE] = {0};
for (int i = 0; i < 8; i++) {
nonce[NONCE_SIZE - 1 - i] = (packetNumber >> (i * 8)) & 0xFF;
}
CCM<AES, TAG_SIZE>::Encryption enc;
enc.SetKeyWithIV(key_, key_.size(), nonce, sizeof(nonce));
enc.SpecifyDataLengths(header.size(), payload.size());
std::string ciphertext;
AuthenticatedEncryptionFilter aef(enc,
new StringSink(ciphertext)
);
// Process AAD (header)
if (!header.empty()) {
aef.ChannelPut(AAD_CHANNEL, header.data(), header.size());
aef.ChannelMessageEnd(AAD_CHANNEL);
}
// Process payload
aef.ChannelPut(DEFAULT_CHANNEL, payload.data(), payload.size());
aef.ChannelMessageEnd(DEFAULT_CHANNEL);
// Return: header + encrypted_payload + tag
std::vector<byte> result;
result.insert(result.end(), header.begin(), header.end());
result.insert(result.end(), ciphertext.begin(), ciphertext.end());
return result;
}
std::vector<byte> decryptPacket(const std::vector<byte>& packet,
size_t headerSize,
uint64_t packetNumber) {
if (packet.size() < headerSize + TAG_SIZE) {
throw std::runtime_error("Packet too short");
}
// Extract header and ciphertext
std::vector<byte> header(packet.begin(), packet.begin() + headerSize);
std::vector<byte> ciphertext(packet.begin() + headerSize, packet.end());
// Reconstruct nonce
byte nonce[NONCE_SIZE] = {0};
for (int i = 0; i < 8; i++) {
nonce[NONCE_SIZE - 1 - i] = (packetNumber >> (i * 8)) & 0xFF;
}
size_t payloadSize = ciphertext.size() - TAG_SIZE;
CCM<AES, TAG_SIZE>::Decryption dec;
dec.SetKeyWithIV(key_, key_.size(), nonce, sizeof(nonce));
dec.SpecifyDataLengths(header.size(), payloadSize);
std::string plaintext;
AuthenticatedDecryptionFilter adf(dec,
new StringSink(plaintext)
);
// Process AAD
if (!header.empty()) {
adf.ChannelPut(AAD_CHANNEL, header.data(), header.size());
adf.ChannelMessageEnd(AAD_CHANNEL);
}
// Process ciphertext
adf.ChannelPut(DEFAULT_CHANNEL, ciphertext.data(), ciphertext.size());
adf.ChannelMessageEnd(DEFAULT_CHANNEL);
return std::vector<byte>(plaintext.begin(), plaintext.end());
}
private:
SecByteBlock key_;
};
int main() {
try {
// Generate key
AutoSeededRandomPool rng;
SecByteBlock key(16); // AES-128
rng.GenerateBlock(key, key.size());
PacketEncryptor encryptor(key);
// Simulate packet
std::vector<byte> header = {0x08, 0x00, 0x00, 0x00}; // Packet header
std::string message = "Hello, wireless world!";
std::vector<byte> payload(message.begin(), message.end());
uint64_t packetNum = 12345;
// Encrypt
auto encrypted = encryptor.encryptPacket(header, payload, packetNum);
std::cout << "Encrypted packet: " << encrypted.size() << " bytes" << std::endl;
// Decrypt
auto decrypted = encryptor.decryptPacket(encrypted, header.size(), packetNum);
std::string result(decrypted.begin(), decrypted.end());
std::cout << "Decrypted: " << result << std::endl;
} catch (const HashVerificationFilter::HashVerificationFailed& e) {
std::cerr << "Authentication failed!" << std::endl;
return 1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}Additional Authenticated Data (AAD)
CCM supports authenticating header data without encrypting it [2]:
#include <cryptopp/aes.h>
#include <cryptopp/ccm.h>
#include <cryptopp/filters.h>
void encryptWithAAD() {
using namespace CryptoPP;
SecByteBlock key(32);
byte nonce[12];
// ... initialize ...
std::string header = "packet-header";
std::string plaintext = "secret-payload";
std::string ciphertext;
CCM<AES>::Encryption enc;
enc.SetKeyWithIV(key, key.size(), nonce, sizeof(nonce));
// CRITICAL: specify lengths BEFORE processing
enc.SpecifyDataLengths(header.size(), plaintext.size());
AuthenticatedEncryptionFilter aef(enc,
new StringSink(ciphertext)
);
// Process AAD first
aef.ChannelPut(AAD_CHANNEL, (const byte*)header.data(), header.size());
aef.ChannelMessageEnd(AAD_CHANNEL);
// Then plaintext
aef.ChannelPut(DEFAULT_CHANNEL, (const byte*)plaintext.data(), plaintext.size());
aef.ChannelMessageEnd(DEFAULT_CHANNEL);
}Performance
CCM makes two passes over the data, one CBC-MAC pass for authenticity and one counter-mode pass for confidentiality [2][7].
Security
Quick Summary
| Property | Value |
|---|---|
| Confidentiality | Counter mode with AES [2] |
| Authenticity | Depends on the tag length, up to 128 bits [2] |
| Nonce requirement | Must not repeat under a key; need not be random [2] |
| Nonce size | 7-13 bytes [2][5] |
| Standard | NIST SP 800-38C [2]; also described in RFC 3610 (Informational) [10] |
Security Bounds
SP 800-38C bounds the chance that a single forged ciphertext passes verification, for a given nonce and AAD, at no more than 1 in 2^Tlen, where Tlen is the tag length in bits. An attacker who submits many forgeries raises the overall chance accordingly [2].
| Tag Size | Single-forgery probability |
|---|---|
| 4 bytes | at most 2^-32 |
| 8 bytes | at most 2^-64 |
| 16 bytes | at most 2^-128 |
Source: [2].
Nonce Management
CCM uses the nonce to create a counter for CTR mode encryption [2]. The nonce must be:
- Unique for each message under the same key [2]
- 7-13 bytes in length [2][5]
- Not secret (can be transmitted with ciphertext)
// Counter-based nonce (recommended for protocols)
byte nonce[12];
uint64_t counter = getNextCounter();
memset(nonce, 0, sizeof(nonce));
memcpy(nonce + 4, &counter, 8); // 64-bit counter
SP 800-38C does not require the nonce to be random [2], and the EAX authors note that CCM’s restricted nonce length may not give adequate security when nonces are chosen randomly [7]. Use a counter.
Thread Safety
Not thread-safe. Create separate objects per thread.
Exceptions
InvalidKeyLength- Key size is not 16, 24, or 32 bytes [11]InvalidArgument- Nonce size not in 7-13 byte range [11]InvalidArgument- Tag size not 4, 6, 8, 10, 12, 14 or 16 bytes [8]InvalidArgument- AAD or message length does not match the values given toSpecifyDataLengths()[8]HashVerificationFilter::HashVerificationFailed- Authentication failed inAuthenticatedDecryptionFilter[12]
CCM vs Other Modes
| Feature | CCM | GCM | EAX |
|---|---|---|---|
| Online | No [2] | Yes [6] | Yes [7] |
| Nonce size | 7-13 bytes [2] | Any; 12 recommended [6] | Any [7] |
| Passes | Two [7] | Counter mode plus GHASH [6] | Two [7] |
Protocol Usage
| Protocol | Key Size | Nonce Size | Tag Size |
|---|---|---|---|
| TLS 1.2 AES-CCM suites | 128/256-bit | 12 bytes | 16 bytes, or 8 bytes for _CCM_8 suites [3] |
| TLS 1.3 | 128-bit | - | TLS_AES_128_CCM_SHA256 and TLS_AES_128_CCM_8_SHA256 [4] |
SP 800-38C was written to be compatible with CCM as used in the IEEE 802.11 amendment [2]; consult the protocol’s own specification for its exact nonce and tag parameters.
Algorithm Details
- Construction: CTR mode encryption + CBC-MAC authentication [2]
- Block cipher: Must have 128-bit block size (AES) [2][8]
- Key sizes: 128, 192, or 256 bits [9]
- Nonce size: 7-13 bytes (affects max message length) [2]
- Tag sizes: 4, 6, 8, 10, 12, 14, or 16 bytes [2][8]
- Standard: NIST SP 800-38C [2]; RFC 3610 (Informational) [10]
See Also
- AES-GCM - On-line, parallelizable AEAD
- AES-EAX - Alternative two-pass mode
- ChaCha20-Poly1305 - Non-AES alternative
- Symmetric Encryption Guide - Conceptual overview
References
- cryptopp-modern History.txt at 2026.11.0, release 5.6.0: “added CCM, GCM (with SSE2 assembly), EAX, CMAC”.
- NIST SP 800-38C: The CCM Mode for Authentication and Confidentiality, sections 3, 5.3 and 5.4, appendix A.1 and appendix B.1.
- RFC 6655: AES-CCM Cipher Suites for Transport Layer Security (TLS), sections 3 and 4.
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3, appendix B.4.
- ccm.h at 2026.11.0, IV sizes, digest size, maximum lengths and
NeedsPrespecifiedDataLengths(). - NIST SP 800-38D: Galois/Counter Mode (GCM) and GMAC, sections 3 and 5.2.1.1.
- Bellare, Rogaway and Wagner: The EAX Mode of Operation, abstract and section 1.
- ccm.cpp at 2026.11.0, block-size and tag-size checks, and lines 104-121, length mismatch checks.
- rijndael.h at 2026.11.0,
Rijndael_Info: 16-byte block, keys of 16 to 32 bytes in steps of 8. - RFC 3610: Counter with CBC-MAC (CCM), section 2.
- cryptlib.cpp at 2026.11.0, key and IV length checks.
- filters.cpp at 2026.11.0,
AuthenticatedDecryptionFilter.