Skip to content

XAES-256-GCM

Header: #include <cryptopp/xaes_256_gcm.h> Namespace: CryptoPP Since: cryptopp-modern 2026.1.0 [1]

XAES-256-GCM is an extended-nonce variant of AES-256-GCM. Its specification says the nonce can be randomly generated for a virtually unlimited number of messages, giving 2^80 messages with a collision risk of 2^-32. It follows the C2SP XAES-256-GCM specification [2].

Quick Example

#include <cryptopp/xaes_256_gcm.h>
#include <cryptopp/osrng.h>
#include <iostream>

int main() {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;

    // Generate random key and nonce
    SecByteBlock key(32);
    prng.GenerateBlock(key, key.size());

    byte nonce[24];
    prng.GenerateBlock(nonce, sizeof(nonce));

    // Message to encrypt
    std::string plaintext = "Hello, XAES-256-GCM!";
    std::string ciphertext(plaintext.size(), '\0');
    byte tag[16];

    // Encrypt
    XAES_256_GCM::Encryption enc;
    enc.SetKey(key, key.size());
    enc.EncryptAndAuthenticate(
        reinterpret_cast<byte*>(&ciphertext[0]), tag, sizeof(tag),
        nonce, static_cast<int>(sizeof(nonce)),
        nullptr, 0,
        (const byte*)plaintext.data(), plaintext.size()
    );

    // Decrypt
    std::string recovered(ciphertext.size(), '\0');
    XAES_256_GCM::Decryption dec;
    dec.SetKey(key, key.size());

    bool valid = dec.DecryptAndVerify(
        reinterpret_cast<byte*>(&recovered[0]), tag, sizeof(tag),
        nonce, static_cast<int>(sizeof(nonce)),
        nullptr, 0,
        (const byte*)ciphertext.data(), ciphertext.size()
    );

    if (valid) {
        std::cout << "Decrypted: " << recovered << std::endl;
    }

    return 0;
}

Usage Guidelines

Do:

  • Use XAES-256-GCM when you need safe random nonce generation
  • Generate random 24-byte nonces using AutoSeededRandomPool
  • Always verify the return value of DecryptAndVerify()
  • Supply a fresh nonce for every message. The one-shot APIs use the nonce you pass in and accept a repeated one, so uniqueness is your responsibility; for streaming, call Resynchronize() with a new nonce before each message

Avoid:

  • Calling Restart() (throws BadState to prevent nonce reuse)
  • Using keys not exactly 32 bytes (throws InvalidKeyLength)
  • Using nonces not exactly 24 bytes (throws InvalidArgument) [3]
  • Ignoring authentication failures during decryption

Class: XAES_256_GCM

Extended-nonce AES-256-GCM authenticated encryption with safe random nonce generation.

XAES_256_GCM provides two nested types:

  • XAES_256_GCM::Encryption - for authenticated encryption
  • XAES_256_GCM::Decryption - for authenticated decryption

Constants

Available on XAES_256_GCM::Encryption and XAES_256_GCM::Decryption:

XAES_256_GCM::Encryption::KEY_SIZE  // 32 (256 bits, fixed)
XAES_256_GCM::Encryption::IV_SIZE   // 24 (192 bits, fixed)
XAES_256_GCM::Encryption::TAG_SIZE  // 16 (128 bits, fixed)

These constants are defined in the header [3].

Key Features

FeatureValueNotes
Key size32 bytes (256 bits)Fixed, no other sizes supported [2][3]
Nonce size24 bytes (192 bits)Fixed [2][3]
Tag size16 bytes (128 bits)Fixed in this library [3]
Random noncesYes2^80 messages at a 2^-32 collision risk [2]
Overhead3 extra AES-256 calls per messageOne of them can be computed once per key [2]

XAES_256_GCM::Encryption

Authenticated encryption class.

Methods

SetKey()

void SetKey(const byte* key, size_t keyLength,
            const NameValuePairs& params = g_nullNameValuePairs);

Set the encryption key.

Parameters:

  • key - Encryption key (must be exactly 32 bytes)
  • keyLength - Length of key in bytes (must be 32)
  • params - Optional algorithm parameters (typically unused)

Throws: InvalidKeyLength if key is not 32 bytes [3]

Example:

XAES_256_GCM::Encryption enc;
SecByteBlock key(32);

AutoSeededRandomPool prng;
prng.GenerateBlock(key, key.size());

enc.SetKey(key, key.size());

SetKeyWithIV()

void SetKeyWithIV(const byte* key, size_t keyLength,
                  const byte* iv, size_t ivLength = IV_SIZE);

Set key and nonce in a single call.

Parameters:

  • key - Encryption key (32 bytes)
  • keyLength - Length of key (must be 32)
  • iv - Nonce (24 bytes)
  • ivLength - Length of nonce (defaults to 24)

Example:

XAES_256_GCM::Encryption enc;
SecByteBlock key(32);
byte nonce[24];

AutoSeededRandomPool prng;
prng.GenerateBlock(key, key.size());
prng.GenerateBlock(nonce, sizeof(nonce));

enc.SetKeyWithIV(key, key.size(), nonce, sizeof(nonce));

Resynchronize()

void Resynchronize(const byte* iv, int ivLength = -1);

Set a new nonce for the next message. Use this between messages with the same key.

Parameters:

  • iv - New nonce (24 bytes)
  • ivLength - Length of nonce; -1 (default) uses IVSize() which is 24

Throws: InvalidArgument if nonce is not 24 bytes [3]

Example:

// Process multiple messages with same key
XAES_256_GCM::Encryption enc;
enc.SetKey(key, key.size());

// Message 1
byte nonce1[24];
prng.GenerateBlock(nonce1, sizeof(nonce1));
enc.Resynchronize(nonce1);
// ... encrypt message 1 ...

// Message 2 - MUST use fresh nonce
byte nonce2[24];
prng.GenerateBlock(nonce2, sizeof(nonce2));
enc.Resynchronize(nonce2);
// ... encrypt message 2 ...

EncryptAndAuthenticate()

void EncryptAndAuthenticate(byte* ciphertext, byte* mac, size_t macSize,
                            const byte* iv, int ivLength,
                            const byte* aad, size_t aadLength,
                            const byte* message, size_t messageLength);

One-shot encryption with authentication.

Parameters:

  • ciphertext - Output buffer (same size as message)
  • mac - Output authentication tag (16 bytes)
  • macSize - Size of MAC buffer (must be exactly 16)
  • iv - Nonce (24 bytes)
  • ivLength - Nonce length (24)
  • aad - Additional authenticated data (can be NULL if aadLength is 0)
  • aadLength - AAD length
  • message - Plaintext to encrypt (can be NULL if messageLength is 0)
  • messageLength - Plaintext length

Throws: InvalidArgument if macSize is not 16, or if buffers are null with non-zero lengths [3]

Example:

XAES_256_GCM::Encryption enc;
enc.SetKey(key, key.size());

std::string plaintext = "Secret message";
std::string ciphertext(plaintext.size(), '\0');
byte tag[16];

enc.EncryptAndAuthenticate(
    reinterpret_cast<byte*>(&ciphertext[0]), tag, sizeof(tag),
    nonce, static_cast<int>(sizeof(nonce)),
    nullptr, 0,  // No AAD
    (const byte*)plaintext.data(), plaintext.size()
);

GetNextIV()

void GetNextIV(RandomNumberGenerator& rng, byte* iv);

Generate a random nonce suitable for use with this cipher.

Parameters:

  • rng - Random number generator
  • iv - Output buffer for 24-byte nonce

Example:

AutoSeededRandomPool prng;
XAES_256_GCM::Encryption enc;
enc.SetKey(key, key.size());

byte nonce[24];
enc.GetNextIV(prng, nonce);
enc.Resynchronize(nonce);  // uses IVSize() internally

XAES_256_GCM::Decryption

Authenticated decryption class.

Methods

DecryptAndVerify()

bool DecryptAndVerify(byte* message,
                      const byte* mac, size_t macSize,
                      const byte* iv, int ivLength,
                      const byte* aad, size_t aadLength,
                      const byte* ciphertext, size_t ciphertextLength);

One-shot decryption with authentication verification.

Parameters:

  • message - Output buffer for plaintext
  • mac - Authentication tag to verify (16 bytes)
  • macSize - Size of MAC (must be exactly 16)
  • iv - Nonce (must match encryption)
  • ivLength - Nonce length (24)
  • aad - Additional authenticated data (must match encryption, can be NULL if aadLength is 0)
  • aadLength - AAD length
  • ciphertext - Encrypted data (can be NULL if ciphertextLength is 0)
  • ciphertextLength - Ciphertext length

Returns: true if authentication succeeded, false if verification failed

Throws: InvalidArgument if macSize is not 16, or if buffers are null with non-zero lengths [3]

Important: Always check the return value. If false, the message has been tampered with and you must discard the plaintext.

Example:

XAES_256_GCM::Decryption dec;
dec.SetKey(key, key.size());

std::string plaintext(ciphertext.size(), '\0');
bool valid = dec.DecryptAndVerify(
    reinterpret_cast<byte*>(&plaintext[0]),
    tag, sizeof(tag),
    nonce, static_cast<int>(sizeof(nonce)),
    nullptr, 0,  // No AAD
    (const byte*)ciphertext.data(), ciphertext.size()
);

if (!valid) {
    SecureWipeArray(reinterpret_cast<byte*>(&plaintext[0]), plaintext.size());
    throw std::runtime_error("Authentication failed!");
}

Streaming Interface

For large messages or incremental processing.

Streaming Misuse Protection: The streaming interface enforces that Resynchronize() must be called before each message. After TruncatedFinal() or TruncatedVerify() completes, attempting to call Update() or ProcessData() without first calling Resynchronize() will throw BadState. This prevents accidental nonce reuse [3].

Fixed Tag Size: TruncatedFinal() and TruncatedVerify() require exactly 16 bytes for the tag (throws InvalidArgument otherwise) [3].

Streaming Encryption

#include <cryptopp/xaes_256_gcm.h>
#include <cryptopp/osrng.h>

void streamingEncrypt() {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    SecByteBlock key(32);
    prng.GenerateBlock(key, key.size());

    byte nonce[24];
    prng.GenerateBlock(nonce, sizeof(nonce));

    XAES_256_GCM::Encryption enc;
    enc.SetKey(key, key.size());
    enc.Resynchronize(nonce);

    // Optional: Process AAD first
    std::string aad = "header-data";
    enc.Update((const byte*)aad.data(), aad.size());

    // Process plaintext in chunks
    std::string chunk1 = "First chunk. ";
    std::string chunk2 = "Second chunk.";

    std::string ct1(chunk1.size(), '\0');
    std::string ct2(chunk2.size(), '\0');

    enc.ProcessData(reinterpret_cast<byte*>(&ct1[0]),
                    (const byte*)chunk1.data(), chunk1.size());
    enc.ProcessData(reinterpret_cast<byte*>(&ct2[0]),
                    (const byte*)chunk2.data(), chunk2.size());

    // Finalize and get the tag
    byte tag[16];
    enc.TruncatedFinal(tag, sizeof(tag));
}

Streaming Decryption

void streamingDecrypt(const SecByteBlock& key, const byte* nonce,
                      const std::string& aad,
                      const std::string& ciphertext, const byte* tag) {
    using namespace CryptoPP;

    XAES_256_GCM::Decryption dec;
    dec.SetKey(key, key.size());
    dec.Resynchronize(nonce, 24);

    // Process AAD (must match encryption)
    dec.Update((const byte*)aad.data(), aad.size());

    // Decrypt data
    std::string plaintext(ciphertext.size(), '\0');
    dec.ProcessData(reinterpret_cast<byte*>(&plaintext[0]),
                    (const byte*)ciphertext.data(), ciphertext.size());

    // Verify the tag
    if (!dec.TruncatedVerify(tag, 16)) {
        SecureWipeArray(reinterpret_cast<byte*>(&plaintext[0]), plaintext.size());
        throw std::runtime_error("Authentication failed");
    }
}

Additional Authenticated Data (AAD)

AAD provides authenticated (but not encrypted) metadata bound to the ciphertext.

void encryptWithAAD() {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    SecByteBlock key(32);
    prng.GenerateBlock(key, key.size());

    byte nonce[24];
    prng.GenerateBlock(nonce, sizeof(nonce));

    // Message and AAD
    std::string plaintext = "Secret message";
    std::string aad = "message-id:12345;timestamp:2025-01-15";

    std::string ciphertext(plaintext.size(), '\0');
    byte tag[16];

    // Encrypt with AAD
    XAES_256_GCM::Encryption enc;
    enc.SetKey(key, key.size());
    enc.EncryptAndAuthenticate(
        reinterpret_cast<byte*>(&ciphertext[0]), tag, sizeof(tag),
        nonce, static_cast<int>(sizeof(nonce)),
        (const byte*)aad.data(), aad.size(),
        (const byte*)plaintext.data(), plaintext.size()
    );
}

void decryptWithAAD(const SecByteBlock& key, const byte* nonce,
                    const std::string& aad, const std::string& ciphertext,
                    const byte* tag) {
    using namespace CryptoPP;

    std::string recovered(ciphertext.size(), '\0');

    XAES_256_GCM::Decryption dec;
    dec.SetKey(key, key.size());

    // AAD must match exactly for verification to succeed
    bool valid = dec.DecryptAndVerify(
        reinterpret_cast<byte*>(&recovered[0]), tag, 16,
        nonce, 24,
        (const byte*)aad.data(), aad.size(),
        (const byte*)ciphertext.data(), ciphertext.size()
    );

    if (!valid) {
        SecureWipeArray(reinterpret_cast<byte*>(&recovered[0]), recovered.size());
        throw std::runtime_error("Authentication failed - AAD or ciphertext tampered");
    }
}

Complete Example

A full encryption/decryption round-trip with best practices.

#include <cryptopp/xaes_256_gcm.h>
#include <cryptopp/osrng.h>
#include <cryptopp/hex.h>
#include <iostream>
#include <stdexcept>

using namespace CryptoPP;

struct EncryptedMessage {
    SecByteBlock nonce;
    std::string ciphertext;
    SecByteBlock tag;

    EncryptedMessage() : nonce(24), tag(16) {}
};

EncryptedMessage Encrypt(const SecByteBlock& key,
                         const std::string& plaintext,
                         const std::string& aad = "") {
    AutoSeededRandomPool prng;
    EncryptedMessage msg;

    // Generate random nonce
    prng.GenerateBlock(msg.nonce, msg.nonce.size());

    // Prepare ciphertext buffer
    msg.ciphertext.resize(plaintext.size());

    // Encrypt
    XAES_256_GCM::Encryption enc;
    enc.SetKey(key, key.size());
    enc.EncryptAndAuthenticate(
        (byte*)msg.ciphertext.data(), msg.tag, msg.tag.size(),
        msg.nonce, (int)msg.nonce.size(),
        aad.empty() ? nullptr : (const byte*)aad.data(), aad.size(),
        (const byte*)plaintext.data(), plaintext.size()
    );

    return msg;
}

std::string Decrypt(const SecByteBlock& key,
                    const EncryptedMessage& msg,
                    const std::string& aad = "") {
    std::string plaintext(msg.ciphertext.size(), '\0');

    XAES_256_GCM::Decryption dec;
    dec.SetKey(key, key.size());

    bool valid = dec.DecryptAndVerify(
        reinterpret_cast<byte*>(&plaintext[0]), msg.tag, msg.tag.size(),
        msg.nonce, (int)msg.nonce.size(),
        aad.empty() ? nullptr : (const byte*)aad.data(), aad.size(),
        (const byte*)msg.ciphertext.data(), msg.ciphertext.size()
    );

    if (!valid) {
        SecureWipeArray(reinterpret_cast<byte*>(&plaintext[0]), plaintext.size());
        throw std::runtime_error("XAES-256-GCM: Authentication failed");
    }

    return plaintext;
}

int main() {
    try {
        AutoSeededRandomPool prng;

        // Generate a random key
        SecByteBlock key(32);
        prng.GenerateBlock(key, key.size());

        std::string original = "This is a secret message using XAES-256-GCM!";
        std::string aad = "user-id:alice;session:xyz123";

        std::cout << "Original:  " << original << std::endl;

        // Encrypt
        EncryptedMessage encrypted = Encrypt(key, original, aad);

        // Print hex values
        std::string nonceHex, ctHex, tagHex;
        StringSource(encrypted.nonce, encrypted.nonce.size(), true,
            new HexEncoder(new StringSink(nonceHex)));
        StringSource((const byte*)encrypted.ciphertext.data(),
            encrypted.ciphertext.size(), true,
            new HexEncoder(new StringSink(ctHex)));
        StringSource(encrypted.tag, encrypted.tag.size(), true,
            new HexEncoder(new StringSink(tagHex)));

        std::cout << "Nonce:     " << nonceHex << std::endl;
        std::cout << "Ciphertext:" << ctHex << std::endl;
        std::cout << "Tag:       " << tagHex << std::endl;

        // Decrypt
        std::string decrypted = Decrypt(key, encrypted, aad);
        std::cout << "Decrypted: " << decrypted << std::endl;

        if (original == decrypted)
            std::cout << "\nSuccess! Messages match." << std::endl;

        return 0;
    }
    catch (const Exception& e) {
        std::cerr << "Crypto++ error: " << e.what() << std::endl;
        return 1;
    }
}

Error Handling

Invalid Key Length

void handleInvalidKey() {
    using namespace CryptoPP;

    try {
        XAES_256_GCM::Encryption enc;

        // Wrong key size - must be exactly 32 bytes
        SecByteBlock shortKey(16);
        enc.SetKey(shortKey, shortKey.size());  // Throws!
    }
    catch (const InvalidKeyLength& e) {
        std::cerr << "Key error: " << e.what() << std::endl;
    }
}

Invalid Nonce Length

void handleInvalidNonce() {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    SecByteBlock key(32);
    prng.GenerateBlock(key, key.size());

    try {
        XAES_256_GCM::Encryption enc;
        enc.SetKey(key, key.size());

        // Wrong nonce size - must be exactly 24 bytes
        byte shortNonce[12];
        enc.Resynchronize(shortNonce, sizeof(shortNonce));  // Throws!
    }
    catch (const InvalidArgument& e) {
        std::cerr << "Nonce error: " << e.what() << std::endl;
    }
}

Authentication Failure

void handleAuthFailure() {
    using namespace CryptoPP;

    AutoSeededRandomPool prng;
    SecByteBlock key(32);
    prng.GenerateBlock(key, key.size());

    byte nonce[24];
    prng.GenerateBlock(nonce, sizeof(nonce));

    // Encrypt
    std::string plaintext = "Original message";
    std::string ciphertext(plaintext.size(), '\0');
    byte tag[16];

    XAES_256_GCM::Encryption enc;
    enc.SetKey(key, key.size());
    enc.EncryptAndAuthenticate(
        reinterpret_cast<byte*>(&ciphertext[0]), tag, sizeof(tag),
        nonce, static_cast<int>(sizeof(nonce)), nullptr, 0,
        (const byte*)plaintext.data(), plaintext.size()
    );

    // Tamper with ciphertext
    ciphertext[0] ^= 0x01;

    // Decrypt - will fail verification
    std::string recovered(ciphertext.size(), '\0');
    XAES_256_GCM::Decryption dec;
    dec.SetKey(key, key.size());

    bool valid = dec.DecryptAndVerify(
        reinterpret_cast<byte*>(&recovered[0]), tag, sizeof(tag),
        nonce, static_cast<int>(sizeof(nonce)), nullptr, 0,
        (const byte*)ciphertext.data(), ciphertext.size()
    );

    if (!valid) {
        // IMPORTANT: Do not use recovered data!
        SecureWipeArray(reinterpret_cast<byte*>(&recovered[0]), recovered.size());
        std::cerr << "Authentication failed - data was tampered!" << std::endl;
    }
}

Security

Quick Summary

AspectValueSource
Key size256 bits (fixed)C2SP [2], library [3]
Nonce size192 bits (fixed)C2SP [2], library [3]
Tag length128 bits (fixed in this library)Library [3]
Random nonces2^80 messages per key at a 2^-32 collision riskC2SP [2]
Key derivationNIST SP 800-108r1 KDF with CMAC-AES256C2SP [2]
Misuse resistanceNot nonce misuse-resistant, not key-committingC2SP [2]

Security best practices:

  • Generate a random 24-byte nonce for every encryption [2]
  • Always verify the return value of DecryptAndVerify() before using decrypted data
  • Use SecByteBlock for keys; its allocator wipes the memory before releasing it [4]
  • Restart() is intentionally disabled (throws BadState) to prevent nonce reuse [3]
Detailed Security Properties

Why XAES-256-GCM?

Standard AES-GCM uses a 12-byte (96-bit) IV. NIST SP 800-38D limits a key to 2^32 invocations when IVs are generated randomly [5].

XAES-256-GCM extends the nonce to 24 bytes (192 bits). The C2SP specification gives 2^80 messages with a collision risk of 2^-32 for random nonces [2].

Key Derivation

XAES-256-GCM derives a per-message key with a NIST SP 800-108r1 KDF instantiated with CMAC-AES256 [2]:

  1. Takes the 32-byte input key and the first 12 bytes of the 24-byte nonce
  2. Derives a 32-byte key with two AES-256 encryptions
  3. Uses the last 12 bytes of the nonce as the AES-256-GCM nonce
  4. The CMAC subkey computation (one AES-256 encryption) can be done once per input key; the library caches it [2][3]

Restart() Protection

Unlike standard GCM, calling Restart() on XAES-256-GCM throws a BadState exception. This prevents accidental nonce reuse. Always use Resynchronize() with a fresh nonce instead [3].

Comparison with Standard GCM

FeatureAES-256-GCMXAES-256-GCM
Key size32 bytes32 bytes [2]
Nonce size12 bytes recommended [5]24 bytes [2]
Tag size16 bytes16 bytes [3]
Random noncesAt most 2^32 messages per key [5]2^80 messages at 2^-32 risk [2]
Per-message overheadNone3 extra AES-256 calls, one amortizable per key [2]
SpecificationNIST SP 800-38D [5]C2SP XAES-256-GCM [2]

When to use XAES-256-GCM:

  • Random nonce generation is preferred
  • Managing unique nonces is impractical
  • High message volume per key
  • Distributed systems where nonce coordination is difficult

When to use standard AES-GCM:

  • Counter-based nonces are feasible
  • The extra AES calls per message matter
  • A NIST-specified mode or interoperability with existing AES-GCM peers is required [5]

Thread Safety

Not thread-safe. Create separate XAES_256_GCM::Encryption and XAES_256_GCM::Decryption objects for each thread.

// WRONG - sharing between threads
XAES_256_GCM::Encryption shared_enc;

// CORRECT - one per thread
void threadFunc() {
    XAES_256_GCM::Encryption enc;  // Thread-local
    // ... use enc ...
}

Exceptions

  • InvalidKeyLength - Key size is not exactly 32 bytes
  • InvalidArgument - Nonce size is not exactly 24 bytes, or tag size is not 16, or null buffer with non-zero length
  • BadState - Restart() was called, or streaming methods called without Resynchronize() [3]

Algorithm Details

  • Base algorithm: AES-256-GCM [2]
  • Key derivation: NIST SP 800-108r1 KDF with CMAC-AES256 [2]
  • Key size: 256 bits (fixed) [2]
  • Nonce size: 192 bits (24 bytes, fixed) [2]
  • Tag size: 128 bits (16 bytes, fixed in this library) [3]
  • Standard: C2SP XAES-256-GCM [2]

See Also

References

  1. cryptopp-modern History.txt at 2026.11.0, release 2026.1.0: “added XAES-256-GCM extended-nonce authenticated encryption”.
  2. C2SP: The XAES-256-GCM extended-nonce AEAD, introduction, overview and detailed key derivation algorithm.
  3. xaes_256_gcm.h at 2026.11.0, constants, tag-size checks and Restart(); lines 249-388 for the state checks, key setup, nonce length check, key derivation and GetNextIV().
  4. secblock.h at 2026.11.0, AllocatorWithCleanup::deallocate wipes before freeing.
  5. NIST SP 800-38D: Galois/Counter Mode (GCM) and GMAC, sections 5.2.1.1 and 8.3.