AES-EAX
Header: #include <cryptopp/aes.h> and #include <cryptopp/eax.h>
Namespace: CryptoPP Since: Crypto++ 5.6.0 [1]
AES-EAX (AES in EAX Mode) is an authenticated encryption algorithm that provides both confidentiality and authenticity. EAX was designed by Bellare, Rogaway and Wagner as a two-pass scheme built from CTR mode and OMAC [2]. The library computes the OMAC step with its CMAC class [3].
Quick Example
#include <cryptopp/aes.h>
#include <cryptopp/eax.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
SecByteBlock nonce(16); // EAX supports variable nonce length
rng.GenerateBlock(key, key.size());
rng.GenerateBlock(nonce, nonce.size());
std::string plaintext = "Hello, World!";
std::string ciphertext, decrypted;
// Encrypt
EAX<AES>::Encryption enc;
enc.SetKeyWithIV(key, key.size(), nonce, nonce.size());
StringSource(plaintext, true,
new AuthenticatedEncryptionFilter(enc,
new StringSink(ciphertext)
)
);
// Decrypt
EAX<AES>::Decryption dec;
dec.SetKeyWithIV(key, key.size(), nonce, nonce.size());
StringSource(ciphertext, true,
new AuthenticatedDecryptionFilter(dec,
new StringSink(decrypted)
)
);
std::cout << "Decrypted: " << decrypted << std::endl;
return 0;
}Usage Guidelines
Do:
- Use EAX when you need an on-line two-pass AEAD that its authors describe as free and unencumbered for all uses
- Use unique nonces for each encryption with the same key [2]
- Use 128-bit or 256-bit keys for new systems
- Verify authentication before using decrypted data
Avoid:
- Reusing nonces with the same key
- Ignoring authentication failures during decryption
- Expecting parallel processing; parallelism was not a design goal of EAX [2]
Why Choose EAX?
| Feature | EAX | GCM | CCM |
|---|---|---|---|
| Nonce flexibility | Any length [2] | 12 bytes recommended [4] | 7-13 bytes [5] |
| Online | Yes [2] | Yes [4] | No (needs lengths upfront) [5] |
| Two-pass | Yes [2] | Yes [2] | Yes [2] |
| Parallelizable | Not a design goal [2] | Yes [4] | - |
Choose EAX when:
- You want a scheme that the authors designed for simplicity and that has a security proof
- Variable-length nonces are useful
- You need a mode whose authors state they know of no patents relevant to it [2]
Class: EAX<AES>
Template class for AES in EAX authenticated encryption mode.
Template Parameters
template <class T_BlockCipher>
struct EAX {
typedef EAX_Final<T_BlockCipher, true> Encryption;
typedef EAX_Final<T_BlockCipher, false> Decryption;
};T_BlockCipher- The underlying block cipher (e.g.,AES)
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
// Inherited from AES
static const int MIN_KEYLENGTH = 16; // 128 bits
static const int MAX_KEYLENGTH = 32; // 256 bits
static const int DEFAULT_KEYLENGTH = 16; // 128 bits
static const int BLOCKSIZE = 16; // 128 bits
These are the Rijndael_Info values [6].
Tag size: Up to the block size (16 bytes for AES). The full tag is 16 bytes [3]; you can truncate via the truncatedDigestSize argument to AuthenticatedEncryptionFilter / AuthenticatedDecryptionFilter [7]. The EAX specification allows any tag length from 0 to the block length [2].
Nonce size: Any length from 0 to UINT_MAX [3]. We recommend 12 to 16 bytes.
EAX<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 (IV).
Parameters:
key- Encryption key (16, 24, or 32 bytes)keyLength- Length of key in bytesiv- Nonce (any length, block size recommended)ivLength- Length of nonce in bytes
Thread Safety: Not thread-safe. Create separate objects per thread.
Example:
EAX<AES>::Encryption enc;
SecByteBlock key(32); // 256-bit key
SecByteBlock nonce(16); // 128-bit nonce
AutoSeededRandomPool rng;
rng.GenerateBlock(key, key.size());
rng.GenerateBlock(nonce, nonce.size());
enc.SetKeyWithIV(key, key.size(), nonce, nonce.size());AlgorithmName()
std::string AlgorithmName() const;Returns the algorithm name (e.g., “AES/EAX”) [3].
DigestSize()
unsigned int DigestSize() const;Returns the authentication tag size (16 bytes for AES) [3].
IVSize()
unsigned int IVSize() const;Returns the default nonce size (block size, 16 bytes for AES) [3].
MinIVLength() / MaxIVLength()
unsigned int MinIVLength() const; // Returns 0
unsigned int MaxIVLength() const; // Returns UINT_MAX
EAX accepts nonces of any length from 0 bytes up to UINT_MAX [3]. In practice, use at least 12-16 bytes.
EAX<AES>::Decryption
Authenticated decryption class.
Methods
SetKeyWithIV()
void SetKeyWithIV(const byte* key, size_t keyLength,
const byte* iv, size_t ivLength);Set decryption key and nonce (same as encryption).
Complete Example: Message Encryption
#include <cryptopp/aes.h>
#include <cryptopp/eax.h>
#include <cryptopp/filters.h>
#include <cryptopp/osrng.h>
#include <cryptopp/hex.h>
#include <iostream>
using namespace CryptoPP;
class SecureMessenger {
public:
SecureMessenger() {
// Generate random 256-bit key
AutoSeededRandomPool rng;
key_.resize(AES::MAX_KEYLENGTH);
rng.GenerateBlock(key_, key_.size());
}
std::string encrypt(const std::string& plaintext) {
AutoSeededRandomPool rng;
// Generate random nonce for each message
SecByteBlock nonce(AES::BLOCKSIZE);
rng.GenerateBlock(nonce, nonce.size());
// Encrypt
std::string ciphertext;
EAX<AES>::Encryption enc;
enc.SetKeyWithIV(key_, key_.size(), nonce, nonce.size());
StringSource(plaintext, true,
new AuthenticatedEncryptionFilter(enc,
new StringSink(ciphertext)
)
);
// Prepend nonce to ciphertext
std::string result;
result.append((const char*)nonce.data(), nonce.size());
result.append(ciphertext);
return result;
}
std::string decrypt(const std::string& encrypted) {
if (encrypted.size() < AES::BLOCKSIZE + 16) {
throw std::runtime_error("Ciphertext too short");
}
// Extract nonce (first 16 bytes)
SecByteBlock nonce((const byte*)encrypted.data(), AES::BLOCKSIZE);
// Extract ciphertext (remainder)
std::string ciphertext = encrypted.substr(AES::BLOCKSIZE);
// Decrypt
std::string plaintext;
EAX<AES>::Decryption dec;
dec.SetKeyWithIV(key_, key_.size(), nonce, nonce.size());
StringSource(ciphertext, true,
new AuthenticatedDecryptionFilter(dec,
new StringSink(plaintext)
)
);
return plaintext;
}
private:
SecByteBlock key_;
};
int main() {
try {
SecureMessenger messenger;
std::string original = "This is a secret message!";
std::cout << "Original: " << original << std::endl;
std::string encrypted = messenger.encrypt(original);
std::cout << "Encrypted length: " << encrypted.size() << " bytes" << std::endl;
std::string decrypted = messenger.decrypt(encrypted);
std::cout << "Decrypted: " << decrypted << std::endl;
// Verify round-trip
if (original == decrypted) {
std::cout << "Success: Messages match!" << std::endl;
}
} catch (const HashVerificationFilter::HashVerificationFailed& e) {
std::cerr << "Authentication failed: message tampered!" << std::endl;
return 1;
} catch (const std::exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}Additional Authenticated Data (AAD)
EAX supports authenticated but unencrypted header data [2]:
#include <cryptopp/aes.h>
#include <cryptopp/eax.h>
#include <cryptopp/filters.h>
void encryptWithAAD() {
using namespace CryptoPP;
SecByteBlock key(32), nonce(16);
// ... initialize key and nonce ...
std::string header = "message-type:secure"; // AAD (authenticated, not encrypted)
std::string plaintext = "Secret payload";
std::string ciphertext;
EAX<AES>::Encryption enc;
enc.SetKeyWithIV(key, key.size(), nonce, nonce.size());
AuthenticatedEncryptionFilter aef(enc,
new StringSink(ciphertext)
);
// Add AAD first
aef.ChannelPut(AAD_CHANNEL, (const byte*)header.data(), header.size());
aef.ChannelMessageEnd(AAD_CHANNEL);
// Then add plaintext
aef.ChannelPut(DEFAULT_CHANNEL, (const byte*)plaintext.data(), plaintext.size());
aef.ChannelMessageEnd(DEFAULT_CHANNEL);
// ciphertext now contains: encrypted payload + authentication tag
// The header is authenticated but transmitted separately
}
void decryptWithAAD(const std::string& header,
const std::string& ciphertext,
const SecByteBlock& key,
const SecByteBlock& nonce) {
using namespace CryptoPP;
std::string plaintext;
EAX<AES>::Decryption dec;
dec.SetKeyWithIV(key, key.size(), nonce, nonce.size());
AuthenticatedDecryptionFilter adf(dec,
new StringSink(plaintext)
);
// Must provide same AAD during decryption
adf.ChannelPut(AAD_CHANNEL, (const byte*)header.data(), header.size());
adf.ChannelMessageEnd(AAD_CHANNEL);
adf.ChannelPut(DEFAULT_CHANNEL, (const byte*)ciphertext.data(), ciphertext.size());
adf.ChannelMessageEnd(DEFAULT_CHANNEL);
// If AAD doesn't match, authentication will fail
}Streaming Encryption
EAX is an “online” mode, meaning you can encrypt data without knowing the total length upfront [2]:
#include <cryptopp/aes.h>
#include <cryptopp/eax.h>
#include <cryptopp/files.h>
#include <cryptopp/osrng.h>
void encryptStream(std::istream& in, std::ostream& out,
const CryptoPP::SecByteBlock& key) {
using namespace CryptoPP;
AutoSeededRandomPool rng;
SecByteBlock nonce(16);
rng.GenerateBlock(nonce, nonce.size());
// Write nonce first
out.write((const char*)nonce.data(), nonce.size());
// Stream encryption
EAX<AES>::Encryption enc;
enc.SetKeyWithIV(key, key.size(), nonce, nonce.size());
FileSource(in, true,
new AuthenticatedEncryptionFilter(enc,
new FileSink(out)
)
);
}For decryption, remember that AuthenticatedDecryptionFilter passes decrypted data to its attachment before it checks the tag [8]; collect the output somewhere you can discard on failure.
Performance
EAX makes one pass for privacy and one for authenticity, using about 2|M|/n + |H|/n + |N|/n block-cipher calls for an n-bit block cipher. A fixed header can be preprocessed, and the authors list parallelism as a non-goal [2].
Security
Quick Summary
| Property | Value |
|---|---|
| Confidentiality | CTR mode with AES |
| Authenticity | Tag length chosen by the user, up to the block size |
| Nonce requirement | Must not repeat under a key |
| Nonce reuse impact | The same nonce gives the same CTR keystream, which reveals the XOR of the plaintexts |
| Specification | Bellare, Rogaway and Wagner, 2004 |
Source: [2].
Security Properties
- Provably secure: secure if the block cipher is a secure pseudorandom permutation [2]
- Privacy: indistinguishability from random bits [2]
- Authenticity: authenticity of ciphertexts [2]
- Combined: the authors note these imply non-malleability and indistinguishability under chosen-ciphertext attack [2]
- Two-pass: the tag is computed over the ciphertext [2][3]
Nonce Requirements
EAX assumes only that nonces do not repeat [2]. A repeated nonce under the same key reuses the CTR keystream, so it reveals the XOR of the plaintexts [2].
// Generate unique nonce for each message
AutoSeededRandomPool rng;
SecByteBlock nonce(16);
rng.GenerateBlock(nonce, nonce.size());Safe approaches:
- Random nonce: 128-bit random value per message
- Counter nonce: Incrementing counter (requires state management)
Thread Safety
Not thread-safe. Create separate EAX<AES>::Encryption and EAX<AES>::Decryption objects for each thread.
// WRONG - sharing between threads
EAX<AES>::Encryption shared_enc;
// CORRECT - one per thread
void threadFunc() {
EAX<AES>::Encryption enc; // Thread-local
// ... use enc ...
}Exceptions
InvalidKeyLength- Key size is not 16, 24, or 32 bytes [9]HashVerificationFilter::HashVerificationFailed- Authentication failed inAuthenticatedDecryptionFilter(message tampered) [8]
EAX vs Other Modes
| Aspect | EAX | GCM | CCM |
|---|---|---|---|
| Nonce size | Any [2] | 12 bytes recommended [4] | 7-13 bytes [5] |
| Online | Yes [2] | Yes [4] | No [5] |
| Block cipher direction | Forward only [2] | Forward only [4] | Forward only [5] |
Algorithm Details
- Construction: CTR mode encryption + OMAC authentication [2]
- Authentication: OMAC, implemented with the library’s
CMACclass [2][3] - Key sizes: 128, 192, or 256 bits (AES) [6]
- Block size: 128 bits (AES) [6]
- Nonce size: Any length [2][3]
- Tag size: Up to the block size (128 bits for AES) [2][3]
- Specification: Bellare, Rogaway and Wagner, “The EAX Mode of Operation” (2004) [2]
See Also
- AES-GCM - Parallelizable authenticated encryption
- AES-CCM - 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”.
- Bellare, Rogaway and Wagner: The EAX Mode of Operation, abstract, sections 1 and 3, figure 2, and section 4 (intellectual property statement).
- eax.h at 2026.11.0, algorithm name, IV and tag sizes, authentication on ciphertext, and the
CMACmember. - NIST SP 800-38D: Galois/Counter Mode (GCM) and GMAC, sections 3 and 5.2.1.1.
- NIST SP 800-38C: The CCM Mode for Authentication and Confidentiality, section 3 and appendix A.1.
- rijndael.h at 2026.11.0,
Rijndael_Info: 16-byte block, keys of 16 to 32 bytes in steps of 8. - filters.h at 2026.11.0,
AuthenticatedEncryptionFiltertruncatedDigestSizeparameter. - filters.cpp at 2026.11.0,
AuthenticatedDecryptionFilterforwards decrypted data before the final tag check. - cryptlib.cpp at 2026.11.0, key length check.