Skip to content

This Repository is a collection of different ethical hacking tools and malware's for penetration testing and research purpose written in python, ruby, rust, c++, go and c.

8.3 KB
Raw Blame

Cryptography - SM4 Encryption Algorithm

SM4 is a symmetric key block cipher that was developed by Zhongguo (China) as part of its national encryption standard. It is part of the Chinese Commercial Cryptography Algorithm Suite, commonly referred to as the SM Series. SM4 was specifically designed for China's domestic use and is mainly used in applications such as secure communications, data encryption, and financial systems.

SM4 is also known as the "SM4 block cipher" or "SMS4", and it is widely used in China's banking, government, and telecommunications sectors. The algorithm is specifically designed to meet the cryptographic requirements for data confidentiality, data integrity, and secure key exchange in Chinese standards.


Key Features of SM4 Algorithm

  1. Block Size: SM4 is a block cipher that operates on 128-bit blocks of data, meaning that each block of plaintext is processed in chunks of 128 bits.

  2. Key Size: SM4 uses a 128-bit key for encryption and decryption. This key size is considered to provide a good level of security and is similar to other modern block ciphers such as AES.

  3. Rounds: The algorithm uses 32 rounds of encryption, which is higher than the number of rounds in other ciphers like AES-128 (which has 10 rounds). The increased number of rounds in SM4 contributes to a higher level of security.

  4. Subkey Generation: SM4 utilizes a key expansion mechanism where the 128-bit key is expanded into 32 subkeys, one for each of the 32 rounds. This helps to provide more complexity in the encryption process.

  5. Feistel Structure: Similar to many well-known block ciphers like DES, Blowfish, and CAST5, SM4 is based on a Feistel network. This structure divides the data block into two halves, applies a round function, and then combines the results at each round.

  6. S-Boxes: SM4 uses non-linear S-boxes to provide confusion in the encryption process, making it more resistant to cryptanalysis attacks. The S-boxes are used to substitute portions of the input data during the round function.


SM4 Algorithm Structure

SM4 is designed around a Feistel network and operates with the following steps:

  1. Key Expansion: The 128-bit key is expanded into 32 subkeys, each of 32 bits, which will be used in the 32 rounds of encryption.

  2. Rounds: The encryption process in SM4 involves 32 rounds. Each round consists of the following operations:

    • The 128-bit block of plaintext is divided into two 64-bit halves.
    • In each round, the right half of the data block is processed using the round function with the current subkey, and then the result is XORed with the left half.
    • The halves are swapped after each round.

    After 32 rounds, the final output is the ciphertext.

  3. Round Function: The round function in SM4 involves a series of transformations:

    • Substitution: The input data is substituted using a set of S-boxes to introduce non-linearity (confusion).
    • Permutation: A series of bitwise operations like XOR and modular addition are applied to mix the bits and increase diffusion.
    • XOR with Subkey: Each round uses a unique 32-bit subkey generated during the key expansion step.
  4. Final Output: After all rounds are completed, the left and right halves are recombined to produce the 128-bit ciphertext, which is the final encrypted output.


SM4 Decryption Process

Since SM4 is a symmetric key cipher, the decryption process is essentially the reverse of the encryption process. The same key is used for both encryption and decryption. In decryption:

  • The ciphertext is processed using the same 128-bit key.
  • The same round function is applied, but the subkeys are used in reverse order.
  • After the 32 rounds of decryption, the original plaintext is recovered.

Security of SM4

SM4 has been designed to meet Chinese national standards and is generally considered to be secure. However, as with any cryptographic algorithm, its security depends on factors such as key management, implementation, and the protection of the encryption key. Here are some key aspects of SM4's security:

  1. Resistance to Cryptanalysis: SM4 has undergone extensive cryptanalysis, and as of now, no effective attacks against the algorithm have been found. It is considered secure against both differential and linear cryptanalysis.

  2. Key Size: The use of a 128-bit key offers a good level of security, making brute-force attacks infeasible. However, given that the key size is fixed, SM4's security is somewhat limited compared to algorithms with larger key sizes, such as AES-256.

  3. Round Structure: With 32 rounds, SM4 is designed to be resistant to attacks that could break weaker ciphers with fewer rounds. The increased number of rounds helps ensure that each bit of the plaintext is sufficiently mixed and diffused before it is encrypted.

  4. S-boxes: The use of S-boxes adds non-linearity to the encryption process, which is critical for resisting attacks like differential cryptanalysis and linear cryptanalysis.

  5. Standardization: SM4 is a standardized encryption algorithm in China and is used in various national cryptographic applications, which adds confidence in its security. However, due to geopolitical factors, its use is largely limited to China and countries that follow Chinese cryptographic standards.


Applications of SM4

SM4 is used in various cryptographic applications, especially within China, where it is the government-approved encryption algorithm. Some common applications include:

  1. Chinese National Standards: SM4 is part of the Chinese Commercial Cryptography Algorithm Suite, which defines cryptographic algorithms for use in governmental and commercial sectors.

  2. Financial Systems: SM4 is used in banking and financial systems to encrypt sensitive financial data and transactions, ensuring data privacy and integrity.

  3. Secure Communications: SM4 is employed in secure communication systems for encryption and decryption of transmitted data, such as in VPNs (Virtual Private Networks) and government communications.

  4. Government and Military: The algorithm is used for securing government documents, military communications, and other classified information within China.

  5. Smart Cards and ID Cards: SM4 is used in smart cards and ID cards in China to protect personal and biometric data.

  6. Network Security: SM4 is also used in network security protocols in China, including securing internet communications and wireless networks.


Comparison with AES

When comparing SM4 with AES (Advanced Encryption Standard), there are several similarities and differences:

  • Block Size: Both SM4 and AES use a 128-bit block size, making them comparable in terms of how they process data blocks.
  • Key Size: SM4 uses a 128-bit key, while AES can support key sizes of 128 bits, 192 bits, and 256 bits, with larger keys providing more security against brute-force attacks.
  • Rounds: SM4 uses 32 rounds, while AES uses 10 rounds for 128-bit keys, 12 rounds for 192-bit keys, and 14 rounds for 256-bit keys. The higher round count in SM4 can provide stronger security, but it also comes with a potential performance cost.
  • Adoption: AES is widely adopted globally, while SM4 is primarily used in China. SM4 has not seen as broad international adoption as AES, partly due to geopolitical reasons.

Conclusion

The SM4 encryption algorithm is a secure and efficient symmetric key block cipher designed for Chinese commercial and government use. It is widely used within China for securing data in applications such as financial systems, secure communications, and government documents. SM4 has undergone extensive cryptanalysis and is considered secure against known attacks. While it uses a 128-bit key and operates with 32 rounds, it remains less widely adopted outside of China compared to global standards like AES. However, SM4 is an important encryption standard in China, and its use is likely to grow as part of China's national cryptographic infrastructure.