Skip to content

x.crypto.sm4 #

Description

crypto.sm4 is a module that assists in the encryption and decryption of binary data using the SM4 block cipher.

SM4 is the block cipher algorithm in China's Standards of Encryption Algorithms.

fn new_cipher #

fn new_cipher(mode Mode, key []u8) !&SM4Cipher

new_cipher creates and returns a new SM4Cipher. The mode should be .sm4_encrypt or .sm4_decrypt. The key argument must be the 16 bytes SM4 key.

enum Mode #

enum Mode {
	sm4_encrypt = 0
	sm4_decrypt = 1
}

struct SM4Cipher #

struct SM4Cipher {
mut:
	mode Mode
	rk   [32]u32 // SM4 round keys
}

fn (SM4Cipher) crypt_ecb #

fn (c &SM4Cipher) crypt_ecb(input []u8, mut output []u8) !

crypt_ecb SM4-ECB block encryption/decryption input must be padded to mutiple of 16 bytes. output must be exactly the same length as input.

fn (SM4Cipher) crypt_cbc #

fn (c &SM4Cipher) crypt_cbc(mut iv []u8, input []u8, mut output []u8) !

crypt_cbc SM4-CBC buffer encryption/decryption iv is a 16 bytes Initialization Vector. input must be padded to mutiple of 16 bytes. output must be exactly the same length as input.