sm4: make sure test all asm codes

This commit is contained in:
Sun Yimin 2024-09-12 11:20:50 +08:00 committed by GitHub
parent 91af41952b
commit 38282cd292
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 83 additions and 20 deletions

View File

@ -42,26 +42,6 @@ jobs:
GOARCH: ${{ matrix.arch }}
GOPPC64: ${{ matrix.ppc64 }}
- name: Test Cipher
run: go test -v -short ./cipher/...
env:
GOARCH: ${{ matrix.arch }}
GOPPC64: ${{ matrix.ppc64 }}
- name: Test Cipher Force SM4 Single Block with AES-NI
run: go test -v -short ./cipher/...
env:
GOARCH: ${{ matrix.arch }}
GOPPC64: ${{ matrix.ppc64 }}
FORCE_SM4BLOCK_AESNI: 1
- name: Test Force SM4 Single Block with AES-NI
run: go test -v -short ./sm4/...
env:
GOARCH: ${{ matrix.arch }}
GOPPC64: ${{ matrix.ppc64 }}
FORCE_SM4BLOCK_AESNI: 1
- name: Test SM4
run: go test -v -short ./sm4/...
env:

View File

@ -47,3 +47,86 @@ func TestWithoutGFMUL(t *testing.T) {
t.Errorf("bad encryption")
}
}
func TestEncryptBlockAsm(t *testing.T) {
src := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
expected := []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46}
encRes2 := make([]uint32, 32)
decRes2 := make([]uint32, 32)
expandKeyAsm(&src[0], &ck[0], &encRes2[0], &decRes2[0], 0)
dst := make([]byte, 16)
encryptBlockAsm(&encRes2[0], &dst[0], &src[0], 0)
if !bytes.Equal(dst, expected) {
t.Errorf("expected=%x, result=%x\n", expected, dst)
}
encryptBlockAsm(&decRes2[0], &dst[0], &expected[0], 0)
if !bytes.Equal(dst, src) {
t.Errorf("expected=%x, result=%x\n", src, dst)
}
}
func TestEncryptBlocksWithAESNI(t *testing.T) {
if !supportsAES {
t.Skip("AES-NI not available")
}
blocks := 4
if useAVX2 {
blocks = 8
}
src := make([]byte, 16*blocks)
expected := make([]byte, 16*blocks)
key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
for i := 0; i < blocks; i++ {
copy(src[i*16:], key)
copy(expected[i*16:], []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46})
}
c := &sm4CipherAsm{sm4Cipher{}, blocks, blocks * BlockSize}
expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0], INST_AES)
dst := make([]byte, 16*blocks)
c.EncryptBlocks(dst, src)
if !bytes.Equal(dst, expected) {
t.Errorf("expected=%x, result=%x\n", expected, dst)
}
c.DecryptBlocks(dst, expected)
if !bytes.Equal(dst, src) {
t.Errorf("expected=%x, result=%x\n", src, dst)
}
}
func TestEncryptBlocksDoubleWithAESNI(t *testing.T) {
if !supportsAES {
t.Skip("AES-NI not available")
}
blocks := 4
if useAVX2 {
blocks = 8
}
src := make([]byte, 2*16*blocks)
expected := make([]byte, 2*16*blocks)
key := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10}
for i := 0; i < 2*blocks; i++ {
copy(src[i*16:], key)
copy(expected[i*16:], []byte{0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46})
}
c := &sm4CipherAsm{sm4Cipher{}, blocks, blocks * BlockSize}
expandKeyAsm(&key[0], &ck[0], &c.enc[0], &c.dec[0], INST_AES)
dst := make([]byte, 2*16*blocks)
c.EncryptBlocks(dst, src)
if !bytes.Equal(dst, expected) {
t.Errorf("expected=%x, result=%x\n", expected, dst)
}
c.DecryptBlocks(dst, expected)
if !bytes.Equal(dst, src) {
t.Errorf("expected=%x, result=%x\n", src, dst)
}
}