How does AES-256-GCM protect an invoice and its tracking number together?
You want to keep the amount on an invoice private. The shipping system needs to see the tracking number, and that number needs to stay associated with the right invoice. With GCM, you can put the amount in the message body and the tracking number in the additional authenticated data (AAD). Before accepting the decrypted amount, the recipient uses the key to verify a short value called an authentication tag that covers both the ciphertext and the AAD. GCM specification
- Shared inputs Both parties hold the secret key. The protocol provides both with the public 96-bit nonce (unique per key) and tracking number (AAD).
-
Sender encrypts the amount Form
J₀by appending 1 as 32 bits to the nonce. Frominc32(J₀)onward, encrypt counter blocks with AES and XOR the output with the amount to get ciphertextC.Sender prepares tag inputs Encrypt the zero block with AES to get the GHASH keyH. EncryptJ₀to get the tag mask. - Sender makes the tag Run GHASH with
Hover the formatted AAD andC. XOR the result with the tag mask. - To the recipient The sender sends
Cand the tag. - Recipient checks the tag Rebuild
J₀,H, and the mask. Recompute the tag from the AAD andCand compare it with the received tag in constant time. -
Tags match Reproduce the keystream from
inc32(J₀)onward. XOR it withCand return the amount.Tags differ Return an authentication error.
Using a block cipher for a long message
AES-256 is a block cipher that uses a 256-bit key to map a 16-byte block to another 16-byte block. It arranges the bytes in a four-by-four grid and XORs in the first round key. It then runs 14 rounds that combine byte substitution, row shifts, mixing within columns, and XOR with another round key. The final round skips column mixing. AES derives all the round keys from the original key. FIPS 197
GCM uses a per-packet value called a nonce and a counter that increments for each block. For a 96-bit nonce (IV), GCM appends the 32-bit value 1 to the IV to form the initial counter block J₀. It reserves J₀ for masking the tag and encrypts the first message block using inc32(J₀), which increments the low 32 bits of J₀ to 2. Encrypting the message counter blocks with AES produces a byte sequence that only a key holder can reproduce. XOR combines this sequence with the message body to produce ciphertext. XOR gives 0 when two bits match and 1 when they differ. With four bits, for example, 1010 XOR 0110 = 1100. The recipient XORs the result with 0110 again to recover 1010. Each block uses a different counter value, so the blocks can be processed in parallel.
What happens if one ciphertext bit changes?
With XOR alone, flipping one bit of ciphertext flips the same bit in the decrypted message. GCM therefore feeds the AAD and ciphertext into GHASH, padding each separately with zeros to a block boundary and appending both original bit lengths. It processes each 128-bit block X using Y ← (Y XOR X) · H. Y starts at zero, and H is the AES encryption of an all-zero block. The multiplication takes place in a finite field where the product also fits in 128 bits.
To produce the tag, GCM XORs the final Y with the AES output for J₀. If someone swaps in a tracking number from a different order, the recipient’s tag calculation uses different AAD. Reusing a nonce with the same key repeats both the encryption keystream and the tag mask, so GCM requires a unique nonce for each use of a given key. QuantAegis runs a fresh encapsulation on each call to Envelope.seal and derives the key and nonce from the shared secret and header. Using authenticated encryption · Envelope format