Files
ssw-mvp/scard/code/server/crypto/crypto_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

121 lines
3.1 KiB
Go

package crypto
import (
"crypto/rand"
"testing"
commonCrypto "security-connector/common/crypto"
)
func TestNewServerKeyManager(t *testing.T) {
km, err := NewServerKeyManager(commonCrypto.CipherSuiteInternational)
if err != nil {
t.Fatalf("NewServerKeyManager failed: %v", err)
}
if km == nil {
t.Fatal("KeyManager should not be nil")
}
if km.serverECDH == nil {
t.Error("Server ECDH key should be initialized")
}
}
func TestGetServerPublicKey(t *testing.T) {
km, _ := NewServerKeyManager(commonCrypto.CipherSuiteInternational)
pubKey, err := km.GetServerPublicKey()
if err != nil {
t.Fatalf("GetServerPublicKey failed: %v", err)
}
if len(pubKey) == 0 {
t.Error("Public key should not be empty")
}
}
func TestDeriveDeviceKEK(t *testing.T) {
km, _ := NewServerKeyManager(commonCrypto.CipherSuiteInternational)
// Generate a device public key
devicePrivKey, err := commonCrypto.GenerateECDSAKey()
if err != nil {
t.Fatalf("GenerateECDSAKey failed: %v", err)
}
devicePubKeyBytes, err := commonCrypto.EncodePublicKey(&devicePrivKey.PublicKey)
if err != nil {
t.Fatalf("EncodePublicKey failed: %v", err)
}
devicePubKey, err := commonCrypto.ParsePublicKey(devicePubKeyBytes)
if err != nil {
t.Fatalf("ParsePublicKey failed: %v", err)
}
// Derive KEK
kek, err := km.DeriveDeviceKEK("DEV_test_001", devicePubKey)
if err != nil {
t.Fatalf("DeriveDeviceKEK failed: %v", err)
}
if kek == nil {
t.Fatal("KEK should not be nil")
}
if kek.Version == 0 {
t.Error("KEK version should not be zero")
}
if len(kek.Key) == 0 {
t.Error("KEK key should not be empty")
}
}
func TestGetDeviceKEK(t *testing.T) {
km, _ := NewServerKeyManager(commonCrypto.CipherSuiteInternational)
// First derive a KEK
devicePrivKey, _ := commonCrypto.GenerateECDSAKey()
devicePubKeyBytes, _ := commonCrypto.EncodePublicKey(&devicePrivKey.PublicKey)
devicePubKey, _ := commonCrypto.ParsePublicKey(devicePubKeyBytes)
km.DeriveDeviceKEK("DEV_KEK_test", devicePubKey)
// Now retrieve it
kek, err := km.GetDeviceKEK("DEV_KEK_test")
if err != nil {
t.Fatalf("GetDeviceKEK failed: %v", err)
}
if kek.DeviceID != "DEV_KEK_test" {
t.Errorf("KEK DeviceID mismatch: got %s, want DEV_KEK_test", kek.DeviceID)
}
}
func TestGetDeviceKEKNotFound(t *testing.T) {
km, _ := NewServerKeyManager(commonCrypto.CipherSuiteInternational)
_, err := km.GetDeviceKEK("nonexistent")
if err == nil {
t.Error("GetDeviceKEK should fail for nonexistent device")
}
}
func TestEncryptKEKForDevice(t *testing.T) {
km, _ := NewServerKeyManager(commonCrypto.CipherSuiteInternational)
// Derive a KEK first
devicePrivKey, _ := commonCrypto.GenerateECDSAKey()
devicePubKeyBytes, _ := commonCrypto.EncodePublicKey(&devicePrivKey.PublicKey)
devicePubKey, _ := commonCrypto.ParsePublicKey(devicePubKeyBytes)
kek, _ := km.DeriveDeviceKEK("DEV_encrypt_test", devicePubKey)
// Create a session key
sessionKey := make([]byte, 32)
_, _ = rand.Read(sessionKey)
// Encrypt KEK
encrypted, err := km.EncryptKEKForDevice(kek, sessionKey)
if err != nil {
t.Fatalf("EncryptKEKForDevice failed: %v", err)
}
if len(encrypted) == 0 {
t.Error("Encrypted KEK should not be empty")
}
}