From 38282cd2929bbc12a2c4406e1b72d51dc3134301 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Thu, 12 Sep 2024 11:20:50 +0800 Subject: [PATCH] sm4: make sure test all asm codes --- .github/workflows/test_ppc64.yaml | 20 -------- sm4/cipher_asm_test.go | 83 +++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 20 deletions(-) diff --git a/.github/workflows/test_ppc64.yaml b/.github/workflows/test_ppc64.yaml index 8e0a82b..40e3b80 100644 --- a/.github/workflows/test_ppc64.yaml +++ b/.github/workflows/test_ppc64.yaml @@ -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: diff --git a/sm4/cipher_asm_test.go b/sm4/cipher_asm_test.go index f015939..9bf2851 100644 --- a/sm4/cipher_asm_test.go +++ b/sm4/cipher_asm_test.go @@ -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) + } +}