sm4-gcm

Crates.iosm4-gcm
lib.rssm4-gcm
version0.1.2
sourcesrc
created_at2023-09-03 14:30:27.248177
updated_at2024-01-15 16:25:15.351975
descriptionSM4-GCM Library
homepage
repositoryhttps://git.hatter.ink/hatter/sm4-gcm
max_upload_size
id962273
size29,138
Hatter Jiang (jht5945)

documentation

README

sm4-gcm

Encrypt & Decrypt test code:

fn main() {
    let key = Sm4Key([0u8; 16]);
    let nonce = [0u8; 12];
    let plaintext = b"Hello World!";

    let ciphertext = sm4_gcm::sm4_gcm_encrypt(&key, &nonce, plaintext);
    println!("Encrypted: {}", hex::encode(&ciphertext));
    let decrypted = sm4_gcm::sm4_gcm_decrypt(&key, &nonce, &ciphertext).unwrap();
    println!("Decrypted: {}", String::from_utf8_lossy(&decrypted));
}

Generate test vector BC test code:

public static void encryptGcmNoPadding(String key, String data, String nonce, String associatedData) throws Exception {
    Cipher cipher = Cipher.getInstance("SM4/GCM/NoPadding", BouncyCastleProvider.PROVIDER_NAME);
    Key sm4Key = new SecretKeySpec(Bytes.fromHex(key).bytes(), "SM4");
    byte[] iv = Bytes.fromHex(nonce).bytes();
    GCMParameterSpec s = new GCMParameterSpec(128, iv);
    cipher.init(Cipher.ENCRYPT_MODE, sm4Key, s);
    if (associatedData != null && associatedData.length() > 0) {
        cipher.updateAAD(Bytes.fromHex(associatedData).bytes());
    }
    byte[] aa = cipher.doFinal(data.getBytes(StandardCharsets.UTF_8));
    System.out.println(Bytes.from(aa).asHex());
}

Benchmark @MacBook Pro (Retina, 15-inch, Late 2013/2 GHz Quad-Core Intel Core i7)

$ cargo run --release --example bench
SM4/GCM encrypt : 65.69 M/s
Commit count: 0

cargo fmt