[Cryptographic Algorithm Seven] Analysis of GCM

  In my other blog [Cryptographic Algorithm 3] Block Cipher Working Mode (ECB \ CBC \ CFB \ OFB \ CTR \ XTS) I have introduced the symmetric algorithm (also known as "block cipher algorithm") in detail. various working modes. Among these working modes, CBC, CFB, and OFB can solve the defect that the same plaintext generates the same ciphertext in ECB mode, and CTR can provide multi-block parallel encryption on this basis, but none of them can provide ciphertext message integrity. sex check function, all have GCM mode.

1 Overview

  The full name of GCM is Galois/Counter Mode, where G refers to GMAC, and C refers to CTR mode. GCM can be considered as a kind of authentication mode, providing two functions of authentication and encryption. GCM模式的分组大小为128bit.
  Before analyzing the principle of GCM, you should first understand the GHASH function and GCTR function .

1.1 GHASH

  The GHASH function uses the "Galois Field Algorithm" to calculate the HASH value. The Galois field algorithm will not be described in detail here (those who are interested can learn by themselves).

  The execution steps of the GHASH function are as follows :
  (1) Divide the string x into 128bitblocks X 1 , X 2 , …, X m ;
  (2) Set Y 0 to 0 128 (that is, Y 0 is 128bit 0), since Y XOR of 0 and X 1 does not change the value of X 1 , so the following figure does not reflect the existence of Y 0
;   (3) Y i = (Y i-1 ⊕ X i ) • H (H is the sub-key of hash), The last Y m is the hash value of this calculation.
insert image description here

1.3 GCTR

  The execution steps of the GCTR function are as follows :
  (1) If X is an empty string (empty string), return an empty string Y;
  (2) Calculate the value of n, n = (len(X) + 127) / 128;
  (3) X = X 1 || X 2 || … || X n-1 || X n * , where X 1 , X 2 , …, X n-1 are all complete blocks, and X n * may be incomplete block;
  (4) CB 1 = ICB;
  (5) For i = 2 to n, calculate CB i (the calculation formula is: CB i = inc32(CB i-1 ), that is, calculate the value of CB i one by one ;
  (6 ) For let i = 1 to n-1, Y i = X i ⊕ CIPHK(CB i ) , first of all for CBi is encrypted, and then XORed with X i to get Y i ;
  (7) Y n * = X n * ⊕ MSB len(X n ) (CIPHK(CB i )); when the result is calculated by CIPHK(CB i ) After that, only the high-order (len(X n )) data is taken, and then XOR operation with X n *.
  (8) Y = Y 1 || Y 2 || ... || Y n-1 || Y n *;
insert image description here

2. GCM encryption

insert image description here

Note: When the GCTR operation is performed on the data P, the counter value starts from J1 (J0 is used in the GCTR operation of the last HASH value, as shown in the figure below)

insert image description here

3. GCM decryption

insert image description here

insert image description here

4. Summary

  • The packet length of the symmetric encryption algorithm required for GCM operation must be 128bit(DES\3DES does not satisfy the addition, AES\SM4 meets the conditions);
  • When encrypting, the additional data (A) and ciphertext (C) must be block-aligned before calculating the MAC;
  • During GCM operation, the acquisition of J0 needs to be divided into different situations, one is len(IV)= 96bit, and the other is len(IV) ≠ 96bit

Guess you like

Origin blog.csdn.net/KXue0703/article/details/130121487