github.com/emmansun/gmsm@v0.29.1/cipher/xts_asm_test.go (about) 1 //go:build (amd64 || arm64 || s390x || ppc64 || ppc64le) && !purego 2 3 package cipher 4 5 import ( 6 "bytes" 7 "crypto/rand" 8 "encoding/hex" 9 "io" 10 "testing" 11 ) 12 13 var testTweakVector = []string{ 14 "F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF", 15 "66e94bd4ef8a2c3b884cfa59ca342b2e", 16 "3f803bcd0d7fd2b37558419f59d5cda6", 17 "6dcfba212f5d82bf525ee9793cfa505a", 18 "c172964cd58be2b8d8e09d9c5e9cfe36", 19 "1a267577a90caad6ae988e22714a2b8b", 20 "33fab707493702e77ff8d66ba9e6c6fe", 21 "23fb188b0f87f6ee2ec0803a99771341", 22 "e8de0a4188b7efbc1ac3979eb906cf36", 23 } 24 25 func testDoubleTweak(t *testing.T, isGB bool) { 26 for _, tk := range testTweakVector { 27 tweak, _ := hex.DecodeString(tk) 28 29 var t1, t2 [16]byte 30 copy(t1[:], tweak) 31 copy(t2[:], tweak) 32 mul2(&t1, isGB) 33 mul2Generic(&t2, isGB) 34 35 if !bytes.Equal(t1[:], t2[:]) { 36 t.Errorf("tweak %v, expected %x, got %x", tk, t2[:], t1[:]) 37 } 38 } 39 } 40 41 func TestDoubleTweak(t *testing.T) { 42 testDoubleTweak(t, false) 43 } 44 45 func TestDoubleTweakGB(t *testing.T) { 46 testDoubleTweak(t, true) 47 } 48 49 func testDoubleTweakRandomly(t *testing.T, isGB bool) { 50 var tweak, t1, t2 [16]byte 51 io.ReadFull(rand.Reader, tweak[:]) 52 copy(t1[:], tweak[:]) 53 copy(t2[:], tweak[:]) 54 mul2(&t1, isGB) 55 mul2Generic(&t2, isGB) 56 57 if !bytes.Equal(t1[:], t2[:]) { 58 t.Errorf("tweak %x, expected %x, got %x", tweak[:], t2[:], t1[:]) 59 } 60 } 61 62 func TestDoubleTweakRandomly(t *testing.T) { 63 for i := 0; i < 10; i++ { 64 testDoubleTweakRandomly(t, false) 65 } 66 } 67 68 func TestDoubleTweakGBRandomly(t *testing.T) { 69 for i := 0; i < 10; i++ { 70 testDoubleTweakRandomly(t, true) 71 } 72 } 73 74 func testDoubleTweaks(t *testing.T, isGB bool) { 75 for _, tk := range testTweakVector { 76 tweak, _ := hex.DecodeString(tk) 77 78 var t1, t2 [16]byte 79 var t11, t12 [128]byte 80 copy(t1[:], tweak) 81 copy(t2[:], tweak) 82 83 for i := 0; i < 8; i++ { 84 copy(t12[16*i:], t2[:]) 85 mul2Generic(&t2, isGB) 86 } 87 88 doubleTweaks(&t1, t11[:], isGB) 89 90 if !bytes.Equal(t1[:], t2[:]) { 91 t.Errorf("1 tweak %v, expected %x, got %x", tk, t2[:], t1[:]) 92 } 93 if !bytes.Equal(t11[:], t12[:]) { 94 t.Errorf("2 tweak %v, expected %x, got %x", tk, t12[:], t11[:]) 95 } 96 } 97 } 98 99 func TestDoubleTweaks(t *testing.T) { 100 testDoubleTweaks(t, false) 101 } 102 103 func TestDoubleTweaksGB(t *testing.T) { 104 testDoubleTweaks(t, true) 105 }