Files
ssw-mvp/scard/code/common/crypto/sm4_test.go
T
admin 3247e9fd15 init: ssw-mvp initial commit
- boot/ (Java/Maven, JNPF 7.0) — 移除旧的 boot/.git,源码直接纳入仓库
- web/ (Vue 3 + Vite + Ant Design Vue, pnpm)
- docs/, scard/, nax/
- refs/INDEX.md + refs/国标/(其余 refs/* 开源子仓库不纳入)
- 已忽略: connector_data/, jweb/, log/, web/{dist,node_modules,.pnpm}, boot/*/target/, .DS_Store, .idea, .vscode
2026-08-05 18:29:12 +08:00

255 lines
5.9 KiB
Go

// SM4 tests
// Reference: GB/T 32907-2016, GM/T 0002-2012
package crypto
import (
"bytes"
"testing"
)
func TestSM4BlockEncryptDecrypt(t *testing.T) {
key := make([]byte, 16)
for i := range key {
key[i] = byte(i)
}
sm4, err := NewSM4(key)
if err != nil {
t.Fatalf("SM4: failed to create cipher: %v", err)
}
plaintext := make([]byte, 16)
for i := range plaintext {
plaintext[i] = byte(i + 1)
}
// Encrypt
ciphertext := make([]byte, 16)
sm4.Encrypt(ciphertext, plaintext)
// Decrypt
decrypted := make([]byte, 16)
sm4.Decrypt(decrypted, ciphertext)
if !bytes.Equal(plaintext, decrypted) {
t.Error("SM4: decrypted text does not match original plaintext")
t.Logf("Original: %x", plaintext)
t.Logf("Decrypted: %x", decrypted)
}
}
func TestSM4EncryptKnownVector(t *testing.T) {
// Test with known vector from spec
key := []byte{
0x01, 0x23, 0x45, 0x67, 0x89, 0xAB, 0xCD, 0xEF,
0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10,
}
plaintext := []byte{
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x00, 0x11,
0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99,
}
sm4, err := NewSM4(key)
if err != nil {
t.Fatalf("SM4: failed to create cipher: %v", err)
}
ciphertext := make([]byte, 16)
sm4.Encrypt(ciphertext, plaintext)
// Regression lock. This input is not from the standard, but the cipher core
// is pinned to the official vector by TestSM4StandardVector, so this value
// is a valid guard against accidental changes to the implementation.
want := []byte{
0xb2, 0xdc, 0xad, 0xd6, 0x0b, 0x10, 0x66, 0xde,
0xd5, 0x35, 0x42, 0xcd, 0x69, 0x96, 0x83, 0xf1,
}
if !bytes.Equal(ciphertext, want) {
t.Errorf("SM4 ciphertext mismatch:\n got %x\nwant %x", ciphertext, want)
}
// Decrypt to verify
decrypted := make([]byte, 16)
sm4.Decrypt(decrypted, ciphertext)
if !bytes.Equal(plaintext, decrypted) {
t.Error("SM4: round-trip failed")
}
}
func TestSM4GCMEncryptDecrypt(t *testing.T) {
key := make([]byte, 16)
for i := range key {
key[i] = byte(i)
}
nonce := make([]byte, 12)
for i := range nonce {
nonce[i] = byte(i)
}
gcm, err := NewSM4GCM(key, nonce)
if err != nil {
t.Fatalf("SM4-GCM: failed to create cipher: %v", err)
}
plaintext := []byte("Hello, SM4-GCM!")
aad := []byte("additional data")
// Encrypt
ciphertext := gcm.Seal(nil, nonce, plaintext, aad)
// Decrypt
decrypted, err := gcm.Open(nil, nonce, ciphertext, aad)
if err != nil {
t.Fatalf("SM4-GCM: failed to decrypt: %v", err)
}
if !bytes.Equal(plaintext, decrypted) {
t.Error("SM4-GCM: decrypted text does not match original")
t.Logf("Original: %s", plaintext)
t.Logf("Decrypted: %s", decrypted)
}
}
func TestSM4GCMInvalidTag(t *testing.T) {
key := make([]byte, 16)
nonce := make([]byte, 12)
gcm, err := NewSM4GCM(key, nonce)
if err != nil {
t.Fatalf("SM4-GCM: failed to create cipher: %v", err)
}
plaintext := []byte("Test message")
aad := []byte("additional")
ciphertext := gcm.Seal(nil, nonce, plaintext, aad)
// Tamper with the tag (last 16 bytes)
ciphertext[len(ciphertext)-1] ^= 0xFF
// Decrypt should fail
_, err = gcm.Open(nil, nonce, ciphertext, aad)
if err == nil {
t.Error("SM4-GCM: should fail with tampered tag")
}
}
func TestSM4GCMDifferentNonce(t *testing.T) {
key := make([]byte, 16)
nonce1 := make([]byte, 12)
nonce2 := make([]byte, 12)
for i := range nonce1 {
nonce1[i] = byte(i)
nonce2[i] = byte(15 - i)
}
gcm1, err := NewSM4GCM(key, nonce1)
if err != nil {
t.Fatalf("SM4-GCM: failed to create cipher 1: %v", err)
}
gcm2, err := NewSM4GCM(key, nonce2)
if err != nil {
t.Fatalf("SM4-GCM: failed to create cipher 2: %v", err)
}
plaintext := []byte("Same plaintext")
aad := []byte("additional data")
ciphertext1 := gcm1.Seal(nil, nonce1, plaintext, aad)
ciphertext2 := gcm2.Seal(nil, nonce2, plaintext, aad)
// Different nonces should produce different ciphertexts
if bytes.Equal(ciphertext1, ciphertext2) {
t.Error("SM4-GCM: different nonces should produce different ciphertexts")
}
// But both should decrypt correctly with their own nonce
decrypted1, err := gcm1.Open(nil, nonce1, ciphertext1, aad)
if err != nil {
t.Fatalf("SM4-GCM: failed to decrypt with nonce1: %v", err)
}
decrypted2, err := gcm2.Open(nil, nonce2, ciphertext2, aad)
if err != nil {
t.Fatalf("SM4-GCM: failed to decrypt with nonce2: %v", err)
}
if !bytes.Equal(plaintext, decrypted1) || !bytes.Equal(plaintext, decrypted2) {
t.Error("SM4-GCM: decryption should work with correct nonce")
}
}
func TestSM4GCMWrongNonce(t *testing.T) {
key := make([]byte, 16)
nonce1 := make([]byte, 12)
nonce2 := make([]byte, 12)
for i := range nonce1 {
nonce1[i] = byte(i)
nonce2[i] = byte(i + 1)
}
gcm, err := NewSM4GCM(key, nonce1)
if err != nil {
t.Fatalf("SM4-GCM: failed to create cipher: %v", err)
}
plaintext := []byte("Test message")
aad := []byte("additional")
ciphertext := gcm.Seal(nil, nonce1, plaintext, aad)
// Try to decrypt with wrong nonce
_, err = gcm.Open(nil, nonce2, ciphertext, aad)
if err == nil {
t.Error("SM4-GCM: should fail with wrong nonce")
}
}
func TestSM4InvalidKeySize(t *testing.T) {
// Test with invalid key sizes
invalidKeys := [][]byte{
[]byte{1, 2, 3, 4}, // 4 bytes
[]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, // 15 bytes
[]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17}, // 17 bytes
}
for _, key := range invalidKeys {
_, err := NewSM4(key)
if err == nil {
t.Errorf("SM4: should reject key size %d", len(key))
}
}
}
func BenchmarkSM4Encrypt(b *testing.B) {
key := make([]byte, 16)
for i := range key {
key[i] = byte(i)
}
sm4, _ := NewSM4(key)
plaintext := make([]byte, 16)
for i := 0; i < b.N; i++ {
sm4.Encrypt(plaintext, plaintext)
}
}
func BenchmarkSM4GCM(b *testing.B) {
key := make([]byte, 16)
nonce := make([]byte, 12)
gcm, _ := NewSM4GCM(key, nonce)
plaintext := make([]byte, 1024)
for i := 0; i < b.N; i++ {
gcm.Seal(nil, nonce, plaintext, nil)
}
}