diff --git a/sm4/cipher_asm_test.go b/sm4/cipher_asm_test.go new file mode 100644 index 0000000..33ad3fc --- /dev/null +++ b/sm4/cipher_asm_test.go @@ -0,0 +1,50 @@ +//go:build (amd64 && !generic) || (arm64 && !generic) +// +build amd64,!generic arm64,!generic + +package sm4 + +import ( + "bytes" + "crypto/cipher" + "testing" +) + +func TestWithoutGFMUL(t *testing.T) { + key := make([]byte, 16) + src := make([]byte, 16) + var dst []byte + var nonce [12]byte + var c cipher.Block + var err error + + if supportSM4 { + c, err = newCipherNI(key) + } else if !supportsAES { + c, err = newCipherGeneric(key) + } else { + blocks := 4 + if useAVX2 { + blocks = 8 + } + c1 := &sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}, blocks, blocks * BlockSize} + expandKeyAsm(&key[0], &ck[0], &c1.enc[0], &c1.dec[0], INST_AES) + c = c1 + } + if err != nil { + t.Fatal(err) + } + + var sm4gcm cipher.AEAD + sm4gcm, err = cipher.NewGCM(c) + if err != nil { + t.Fatal(err) + } + dst = sm4gcm.Seal(nil, nonce[:], src, nil) + src, err = sm4gcm.Open(nil, nonce[:], dst, nil) + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(key, src) { + t.Errorf("bad encryption") + } +} diff --git a/sm4/cipher_test.go b/sm4/cipher_test.go index 437fbf4..2a3f4d0 100644 --- a/sm4/cipher_test.go +++ b/sm4/cipher_test.go @@ -1,8 +1,6 @@ package sm4 import ( - "bytes" - "crypto/cipher" "reflect" "testing" ) @@ -82,46 +80,6 @@ func TestEncryptDecryptPanic(t *testing.T) { shouldPanic(t, func() { c.Decrypt(src[1:], src) }) } -func TestWithoutGFMUL(t *testing.T) { - key := make([]byte, 16) - src := make([]byte, 16) - var dst []byte - var nonce [12]byte - var c cipher.Block - var err error - - if supportSM4 { - c, err = newCipherNI(key) - } else if !supportsAES { - c, err = newCipherGeneric(key) - } else { - blocks := 4 - if useAVX2 { - blocks = 8 - } - c1 := &sm4CipherAsm{sm4Cipher{make([]uint32, rounds), make([]uint32, rounds)}, blocks, blocks * BlockSize} - expandKeyAsm(&key[0], &ck[0], &c1.enc[0], &c1.dec[0], INST_AES) - c = c1 - } - if err != nil { - t.Fatal(err) - } - - var sm4gcm cipher.AEAD - sm4gcm, err = cipher.NewGCM(c) - if err != nil { - t.Fatal(err) - } - dst = sm4gcm.Seal(nil, nonce[:], src, nil) - src, err = sm4gcm.Open(nil, nonce[:], dst, nil) - if err != nil { - t.Fatal(err) - } - if !bytes.Equal(key, src) { - t.Errorf("bad encryption") - } -} - func shouldPanic(t *testing.T, f func()) { t.Helper() defer func() { _ = recover() }()