Files
ssw-mvp/scard/code/common/crypto/cipher_suite_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

353 lines
8.8 KiB
Go

// CipherSuite tests
package crypto
import (
"bytes"
"testing"
)
func TestCipherSuiteType(t *testing.T) {
gm := GetCipherSuiteByType(CipherSuiteGM)
if gm.GetType() != CipherSuiteGM {
t.Errorf("GM cipher suite should have type GM")
}
intl := GetCipherSuiteByType(CipherSuiteInternational)
if intl.GetType() != CipherSuiteInternational {
t.Errorf("International cipher suite should have type International")
}
}
func TestGMCipherSuiteKeyGeneration(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteGM)
priv, pub, err := cs.GenerateKeyPair()
if err != nil {
t.Fatalf("GM: failed to generate key pair: %v", err)
}
if priv == nil || pub == nil {
t.Error("GM: key pair should not be nil")
}
// Verify key types
_, ok := priv.(*SM2PrivateKey)
if !ok {
t.Error("GM: private key should be SM2PrivateKey")
}
_, ok = pub.(*SM2PublicKey)
if !ok {
t.Error("GM: public key should be SM2PublicKey")
}
}
func TestInternationalCipherSuiteKeyGeneration(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteInternational)
priv, pub, err := cs.GenerateKeyPair()
if err != nil {
t.Fatalf("International: failed to generate key pair: %v", err)
}
if priv == nil || pub == nil {
t.Error("International: key pair should not be nil")
}
}
func TestGMCipherSuiteECDH(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteGM)
// Generate two key pairs
privA, pubA, _ := cs.GenerateKeyPair()
privB, pubB, _ := cs.GenerateKeyPair()
// Derive shared secrets
secretA, err := cs.DeriveSharedSecret(privA, pubB)
if err != nil {
t.Fatalf("GM ECDH: failed to derive shared secret A: %v", err)
}
secretB, err := cs.DeriveSharedSecret(privB, pubA)
if err != nil {
t.Fatalf("GM ECDH: failed to derive shared secret B: %v", err)
}
if !bytes.Equal(secretA, secretB) {
t.Error("GM ECDH: shared secrets should match")
}
}
func TestInternationalCipherSuiteECDH(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteInternational)
// Generate two key pairs
privA, pubA, _ := cs.GenerateKeyPair()
privB, pubB, _ := cs.GenerateKeyPair()
// Derive shared secrets
secretA, err := cs.DeriveSharedSecret(privA, pubB)
if err != nil {
t.Fatalf("International ECDH: failed to derive shared secret A: %v", err)
}
secretB, err := cs.DeriveSharedSecret(privB, pubA)
if err != nil {
t.Fatalf("International ECDH: failed to derive shared secret B: %v", err)
}
if !bytes.Equal(secretA, secretB) {
t.Error("International ECDH: shared secrets should match")
}
}
func TestGMCipherSuiteSignAndVerify(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteGM)
priv, pub, err := cs.GenerateKeyPair()
if err != nil {
t.Fatalf("GM: failed to generate key pair: %v", err)
}
message := []byte("Test message for GM signing")
sig, err := cs.Sign(priv, message)
if err != nil {
t.Fatalf("GM: failed to sign: %v", err)
}
if !cs.Verify(pub, message, sig) {
t.Error("GM: signature verification failed")
}
}
func TestInternationalCipherSuiteSignAndVerify(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteInternational)
priv, pub, err := cs.GenerateKeyPair()
if err != nil {
t.Fatalf("International: failed to generate key pair: %v", err)
}
message := []byte("Test message for signing")
sig, err := cs.Sign(priv, message)
if err != nil {
t.Fatalf("International: failed to sign: %v", err)
}
if !cs.Verify(pub, message, sig) {
t.Error("International: signature verification failed")
}
}
func TestGMCipherSuiteEncryptDecrypt(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteGM)
key := make([]byte, 16)
for i := range key {
key[i] = byte(i)
}
plaintext := []byte("Hello, GM cipher!")
aad := []byte("additional authenticated data")
ciphertext, err := cs.Encrypt(key, plaintext, aad)
if err != nil {
t.Fatalf("GM: encryption failed: %v", err)
}
decrypted, err := cs.Decrypt(key, ciphertext, aad)
if err != nil {
t.Fatalf("GM: decryption failed: %v", err)
}
if !bytes.Equal(plaintext, decrypted) {
t.Error("GM: decrypted text does not match original")
}
}
func TestInternationalCipherSuiteEncryptDecrypt(t *testing.T) {
cs := GetCipherSuiteByType(CipherSuiteInternational)
key := make([]byte, 32) // AES-256 key
for i := range key {
key[i] = byte(i)
}
plaintext := []byte("Hello, International cipher!")
aad := []byte("additional authenticated data")
ciphertext, err := cs.Encrypt(key, plaintext, aad)
if err != nil {
t.Fatalf("International: encryption failed: %v", err)
}
decrypted, err := cs.Decrypt(key, ciphertext, aad)
if err != nil {
t.Fatalf("International: decryption failed: %v", err)
}
if !bytes.Equal(plaintext, decrypted) {
t.Error("International: decrypted text does not match original")
}
}
func TestCipherSuiteHash(t *testing.T) {
// Test GM hash
gmCS := GetCipherSuiteByType(CipherSuiteGM)
data := []byte("test data")
hash := gmCS.Hash(data)
if len(hash) != 32 {
t.Errorf("GM: hash should be 32 bytes, got %d", len(hash))
}
// Test International hash
intlCS := GetCipherSuiteByType(CipherSuiteInternational)
hash = intlCS.Hash(data)
if len(hash) != 32 {
t.Errorf("International: hash should be 32 bytes, got %d", len(hash))
}
}
func TestCipherSuiteNewHash(t *testing.T) {
// Test GM hash
gmCS := GetCipherSuiteByType(CipherSuiteGM)
h := gmCS.NewHash()
if h == nil {
t.Error("GM: NewHash() should not return nil")
}
// Test International hash
intlCS := GetCipherSuiteByType(CipherSuiteInternational)
h = intlCS.NewHash()
if h == nil {
t.Error("International: NewHash() should not return nil")
}
}
func TestCipherSuiteDeriveKey(t *testing.T) {
// Test GM key derivation
gmCS := GetCipherSuiteByType(CipherSuiteGM)
key, err := gmCS.DeriveKey(nil, []byte("test ikm"), "test info", 32)
if err != nil {
t.Fatalf("GM: key derivation failed: %v", err)
}
if len(key) != 32 {
t.Errorf("GM: derived key should be 32 bytes, got %d", len(key))
}
// Test International key derivation
intlCS := GetCipherSuiteByType(CipherSuiteInternational)
key, err = intlCS.DeriveKey(nil, []byte("test ikm"), "test info", 32)
if err != nil {
t.Fatalf("International: key derivation failed: %v", err)
}
if len(key) != 32 {
t.Errorf("International: derived key should be 32 bytes, got %d", len(key))
}
}
func TestSetCipherSuite(t *testing.T) {
// Test setting to GM
SetCipherSuite(CipherSuiteGM)
cs := GetCipherSuite()
if cs.GetType() != CipherSuiteGM {
t.Errorf("SetCipherSuite(GM) should set GM cipher suite")
}
// Test setting to International
SetCipherSuite(CipherSuiteInternational)
cs = GetCipherSuite()
if cs.GetType() != CipherSuiteInternational {
t.Errorf("SetCipherSuite(International) should set International cipher suite")
}
}
func TestCipherSuiteString(t *testing.T) {
gmCS := GetCipherSuiteByType(CipherSuiteGM)
if gmCS.String() != "GM (SM2/SM3/SM4)" {
t.Errorf("GM cipher suite should have string 'GM (SM2/SM3/SM4)', got '%s'", gmCS.String())
}
intlCS := GetCipherSuiteByType(CipherSuiteInternational)
if intlCS.String() != "International (P-256/ECDSA/SHA-256)" {
t.Errorf("International cipher suite should have string 'International (P-256/ECDSA/SHA-256)', got '%s'", intlCS.String())
}
}
func TestCipherSuiteEncodeParsePublicKey(t *testing.T) {
// Test GM public key encoding/parsing
gmCS := GetCipherSuiteByType(CipherSuiteGM)
_, pub, _ := gmCS.GenerateKeyPair()
encoded, err := gmCS.EncodePublicKey(pub)
if err != nil {
t.Fatalf("GM: failed to encode public key: %v", err)
}
parsed, err := gmCS.ParsePublicKey(encoded)
if err != nil {
t.Fatalf("GM: failed to parse public key: %v", err)
}
if parsed == nil {
t.Error("GM: parsed public key should not be nil")
}
// Test International public key encoding/parsing
intlCS := GetCipherSuiteByType(CipherSuiteInternational)
_, pubIntl, _ := intlCS.GenerateKeyPair()
encoded, err = intlCS.EncodePublicKey(pubIntl)
if err != nil {
t.Fatalf("International: failed to encode public key: %v", err)
}
parsed, err = intlCS.ParsePublicKey(encoded)
if err != nil {
t.Fatalf("International: failed to parse public key: %v", err)
}
if parsed == nil {
t.Error("International: parsed public key should not be nil")
}
}
func TestCipherSuiteGetCurve(t *testing.T) {
// Test GM curve
gmCS := &gmCipherSuite{}
curve := gmCS.GetCurve()
if curve != SM2P256V1 {
t.Error("GM cipher suite should return SM2P256V1 curve")
}
// Test International curve
intlCS := &internationalCipherSuite{}
curve = intlCS.GetCurve()
if curve != CurveP256 {
t.Error("International cipher suite should return P-256 curve")
}
}
func BenchmarkCipherSuiteGMEncrypt(b *testing.B) {
cs := GetCipherSuiteByType(CipherSuiteGM)
key := make([]byte, 16)
data := make([]byte, 1024)
for i := 0; i < b.N; i++ {
cs.Encrypt(key, data, nil)
}
}
func BenchmarkCipherSuiteInternationalEncrypt(b *testing.B) {
cs := GetCipherSuiteByType(CipherSuiteInternational)
key := make([]byte, 32)
data := make([]byte, 1024)
for i := 0; i < b.N; i++ {
cs.Encrypt(key, data, nil)
}
}