Path: blob/master/Authenticated-Encryption/AES-GCM/README.md
1402 views
AES-GCM
Prerequisites:
AES-GCM is an example of Authenticated Encryption with Associated Data (AEAD) based system. As the name suggests, the algorithm used for encryption is AES along with Galois Counter Mode (GCM) for authentication. GCM is similar to CTR mode of block cipher mode of encryption- GCM has an added component for computing the authentication tag.
In this section, we will discuss the following internals of AES-GCM:
How encryption takes place
Authentication internals in AES-GCM- Wegman Carter MAC
AES-GCM also has the concept of adding nonces during encryption; nonces are basically strings that are used only once during encryption. This is to ensure that the cipher is in no way predictable. In the next section, we will see in detail, how nonces are included during encryption.
We will also see how security of AES-GCM gets affected when nonces are repeated (Forbidden Attack)
This is how AES-GCM looks like:
There are some notations in the picture plus we will also be using some notations frequently in this article, so let us define them now for our convenience:
Ji:
i
-th block constructed from concatenation of a 96 bit nonce and a 32 bit counter. This block is then encrypted using AES.Pi:
i
-th block of plaintextCi:
i
-th block of ciphertextEk(): Encryption function with key
k
Dk(): Decryption function with key
k
T: Authentication Tag
GmulH(X): Modular Multiplication H.X in Galois Field GF(2**128), with irreducible polynomial f = 1 + x + x2 + x7 + x128
len(X): 64 bit representation of length of string X
A: Associated Data that is to be authenticated but not encrypted
Encryption in AES-GCM
Encryption in AES-GCM is almost the same as AES-CTR mode of encryption. Let us look at how plaintext is encrypted using AES-CTR:
Let us see how encryption in AES-GCM mode takes place:
A random nonce/IV of size 96 bits (or 12 bytes) is generated using a secure pseudo random number generator. IV must be known only to the sender and the receiver.
IV is then concatenated with a 32 bit counter
ctr
, starting from 1 (The counter starts from 0 in the case of CTR mode for encrypting the plaintexxt). The concatenated string block is of size 128 bits and can now be encrypted.The block is encrypted with AES using key. This results into a string
s
of size 16 bytes or 128 bits.The resultant string
s
is XORed with the first block of plaintext to get the first block of ciphertext. Value of counter is then increased by 1.Steps 1 to 5 are repeated for each value of counter and different plaintext blocks.
Note that for the last block of plaintext, if the plaintext block is not a multiple of 16 (ie. the blocksize),
s
will be sliced to a length equal to the length of last block of plaintext and then XORed with the last block of plaintext.
Note that there is more to AES-GCM than just encryption and that is the process of generating Authentication Tag, which we will see in the next section.
The code below is a basic implementation of encryption part in AES-GCM:
Authentication Tag generation in AES-GCM
The process of generating Authentication Tag in AES-GCM is an Encrypt-then-MAC construction, and Wegman-Carter MAC with a few tweaks is used for generating Authentication Tag.
The function used for generating Authentication Tag is also known as GHASH()
Authentication Tag in AES-GCM is generated using the following steps:
The secret string
H
is generated as H = Ek("\x00"*16) ("\x00"*16 is a null string of size 16 bytes)Authentication Tag is generated in a series of steps, block wise. Hence, for each block of associated data Ai, a string Xi is generated using the following computation, and this contributes to the final authentication tag:
Xi = GmulH(Xi-1 xor Ai), for i = 1,..., m, X0 = 0. Here
m
is the number of blocks to be authenticated only (Associated Data blocks).
Now that associated data blocks Ai have been included in the authentication tag, the current authentication tag will be processed and
n
ciphertext blocks will also be included in the authentication tag:Xi+m = GmulH(Xi+m-1 xor Ci), for i = 1,..., n. Here
n
is the number of blocks of ciphertext.
After the above step, 64 bit representation of length of Associated Data
A
is concatenated with 64 bit representation of length of ciphertextC
. The resultant string is supplied as a parameter to GmulH() as follows:S = GmulH = (Xm+n xor (len(A) || len(C)))
Authentication Tag
T
is generated as:T = S xor Ek(J0)
Note: Authentication tag in AES-GCM can be generated parallely after generation of each block of ciphertext from the encryption function.
The service returns concatenation of ciphertext C
and corresponding authentication tag T
as the final output!
Here is a trivial implementation of Authentication part of AES-GCM:
You can check out the entire implementation of AES-GCM here.