Authenticated Encryption: AES-GCM Primitives
Raw encryption without authentication invites bit-flipping and padding oracle attacks. Authenticated Encryption with Associated Data (AEAD), specifically AES-GCM (Galois/Counter Mode), encrypts plaintext while simultaneously generating an authentication tag to guarantee integrity 🔐🛡️.
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import secrets
# 256-bit symmetric encryption key:
key = AESGCM.generate_key(bit_length=256)
aesgcm = AESGCM(key)
# 96-bit (12-byte) unique nonce (NEVER REUSE A NONCE!):
nonce = secrets.token_bytes(12)
payload = b"DATABASE_PASSWORD=secret_sauce_99"
ciphertext = aesgcm.encrypt(nonce, payload, associated_data=None)
recovered = aesgcm.decrypt(nonce, ciphertext, associated_data=None)
print(f"Decrypted successfully: {recovered.decode()} 🚀")
# 66. Authenticated Encryption: AES-GCM Primitives Using unauthenticated encryption modes like AES-ECB or AES-CBC without HMAC is a classic rookie mistake. An attacker on the wire cannot read the plaintext, but they can flip individual bits in the ciphertext to alter data (e.g. changing `$00100` to `$99100`) without being detected 🛑. ```python # Unauthenticated Ciphers (Vulnerable to Bit-Flipping): # Ciphertext -> Attacker flips bit 42 -> Decrypts to modified payload! # Authenticated Encryption (AEAD: AES-GCM / ChaCha20-Poly1305): # Ciphertext + 16-byte GMAC Auth Tag # If even 1 bit is flipped -> Decryption fails instantly with InvalidTag! ``` ### The AES-GCM Invariants 1. **Confidentiality + Authenticity**: Combines AES Counter Mode (CTR) for stream-speed encryption with Galois Field Multiplication (GHASH) to generate a 128-bit authentication tag. 2. **Associated Data (AAD)**: Plaintext metadata (e.g. packet headers, tenant IDs) can be authenticated alongside the ciphertext without being encrypted. If an attacker modifies the header, decryption aborts. 3. **The Nonce-Reuse Disaster**: In Galois/Counter Mode, using the same 12-byte nonce twice under the same key completely destroys security, leaking the GHASH key and allowing message forgeries 🔐🛡️. --- ### Interactive Pop-Quiz 🧠 #### What catastrophic security failure happens if the same 96-bit nonce is reused with the same AES-GCM key across two different encryptions? - [ ] An attacker can mathematically recover the authentication subkey (GHASH), allowing them to forge arbitrary ciphertexts 🛑 - [ ] The CPU hardware flags an illegal instruction trap - [ ] The operating system drops the encrypted bytes from memory - [ ] The second ciphertext becomes an empty zero-byte array --- ### Practical Challenge 🎯 Using `AESGCM`, encrypt `b'HELLO_AEAD'` with a 256-bit key and a 12-byte nonce. Pass associated data `b'HEADER_V1'`. Decrypt the ciphertext with the same key, nonce, and associated data. Verify that attempting to decrypt with wrong associated data `b'HEADER_V2'` raises `cryptography.exceptions.InvalidTag`. Print `'AEAD_VERIFIED'`. <details> <summary>Solution</summary> ```python from cryptography.hazmat.primitives.ciphers.aead import AESGCM from cryptography.exceptions import InvalidTag import secrets key = AESGCM.generate_key(bit_length=256) aes = AESGCM(key) nonce = secrets.token_bytes(12) aad = b"HEADER_V1" plaintext = b"HELLO_AEAD" ct = aes.encrypt(nonce, plaintext, aad) dec = aes.decrypt(nonce, ct, aad) assert dec == plaintext tampered_caught = False try: aes.decrypt(nonce, ct, b"HEADER_V2") except InvalidTag: tampered_caught = True assert tampered_caught is True print("AEAD_VERIFIED") ``` </details>