To generate a 12-byte (96-bit) authentication tag using AES-GCM, you need to configure the GCM mode to output a tag of the desired length. Here’s how to achieve it:
Steps to Get a 12-Byte Authentication Tag
-
Use a Library Supporting GCM
Ensure the cryptographic library you use supports specifying the tag length. Examples include OpenSSL, PyCryptodome (Python), and JCE (Java).
-
Specify the Tag Length
When initializing the AES-GCM operation, set the tag length parameter to 12 bytes (96 bits). Here's how it works in common tools:
- OpenSSL: Use the EVP_CIPHER_CTX_ctrl function to set the tag length.
- PyCryptodome (Python): AES-GCM uses a default 16-byte tag but allows specifying a shorter tag when finalizing encryption.
- JCE (Java): Use the GCMParameterSpec class and set the tLen parameter to 96 (bits).
-
Encrypt Data
Perform the encryption with AES-GCM, and it will produce a 12-byte tag alongside the ciphertext.
-
Store or Transmit the Tag
Save the tag securely along with the ciphertext, as it’s required for decryption and verification.
Example (Using PyCryptodome in Python)
from Cryptodome.Cipher import AES
from Cryptodome.Random import get_random_bytes
# Generate a key and data
key = get_random_bytes(16)
data = b"Secret message"
nonce = get_random_bytes(12)
# Encrypt with AES-GCM
cipher = AES.new(key, AES.MODE_GCM, nonce=nonce, mac_len=12) # mac_len specifies tag length
ciphertext, tag = cipher.encrypt_and_digest(data)
print(f"Ciphertext: {ciphertext}")
print(f"Authentication Tag (12 bytes): {tag}")