From 76635c4cf32e95a4682b37bbda2af4d1d4ba8327 Mon Sep 17 00:00:00 2001 From: Sun Yimin Date: Mon, 21 Aug 2023 11:07:42 +0800 Subject: [PATCH] cipher: xts asm, add random test --- .github/workflows/test_qemu.yml | 2 +- cipher/xts_asm_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test_qemu.yml b/.github/workflows/test_qemu.yml index 632aff1..3d2aadf 100644 --- a/.github/workflows/test_qemu.yml +++ b/.github/workflows/test_qemu.yml @@ -30,7 +30,7 @@ jobs: uses: actions/checkout@v3 - name: Test - run: go test -v -short ./cipher/... + run: go test -v -short ./... env: DISABLE_SM3NI: 1 DISABLE_SM4NI: 1 diff --git a/cipher/xts_asm_test.go b/cipher/xts_asm_test.go index 376bbdd..09e7368 100644 --- a/cipher/xts_asm_test.go +++ b/cipher/xts_asm_test.go @@ -5,7 +5,9 @@ package cipher import ( "bytes" + "crypto/rand" "encoding/hex" + "io" "testing" ) @@ -45,6 +47,31 @@ func TestDoubleTweakGB(t *testing.T) { testDoubleTweak(t, true) } +func testDoubleTweakRandomly(t *testing.T, isGB bool) { + var tweak, t1, t2 [16]byte + io.ReadFull(rand.Reader, tweak[:]) + copy(t1[:], tweak[:]) + copy(t2[:], tweak[:]) + mul2(&t1, isGB) + mul2Generic(&t2, isGB) + + if !bytes.Equal(t1[:], t2[:]) { + t.Errorf("tweak %x, expected %x, got %x", tweak[:], t2[:], t1[:]) + } +} + +func TestDoubleTweakRandomly(t *testing.T) { + for i := 0; i < 10; i++ { + testDoubleTweakRandomly(t, false) + } +} + +func TestDoubleTweakGBRandomly(t *testing.T) { + for i := 0; i < 10; i++ { + testDoubleTweakRandomly(t, true) + } +} + func testDoubleTweaks(t *testing.T, isGB bool) { for _, tk := range testTweakVector { tweak, _ := hex.DecodeString(tk)