git.sr.ht/~pingoo/stdx@v0.0.0-20240218134121-094174641f6e/zign/verify_test.go (about) 1 package zign 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "git.sr.ht/~pingoo/stdx/crypto" 9 ) 10 11 func TestSignAndVerifyInternal(t *testing.T) { 12 randomData10M, err := crypto.RandBytes(10_000_00) 13 if err != nil { 14 t.Error(err) 15 } 16 testsVector := [][]byte{ 17 []byte("Hello World"), 18 {}, 19 randomData10M, 20 } 21 22 for _, data := range testsVector { 23 dataReader := bytes.NewReader(data) 24 25 publicKey, privateKey, err := crypto.GenerateEd25519KeyPair() 26 if err != nil { 27 t.Error(err) 28 } 29 30 hash, signature, err := hashAndSignFile(privateKey, dataReader) 31 if err != nil { 32 t.Error(err) 33 } 34 35 dataReader.Seek(0, io.SeekStart) 36 37 verifyInput := VerifyInput{ 38 Reader: dataReader, 39 HashBlake3: hash, 40 Signature: signature, 41 } 42 err = hashDataAndVerifySignature(publicKey, verifyInput) 43 if err != nil { 44 t.Error(err) 45 } 46 } 47 } 48 49 func TestSignAndVerifyIvalidHashInternal(t *testing.T) { 50 randomData10M, err := crypto.RandBytes(10_000_00) 51 if err != nil { 52 t.Error(err) 53 } 54 testsVector := [][]byte{ 55 []byte("Hello World"), 56 {}, 57 randomData10M, 58 } 59 60 for index, data := range testsVector { 61 dataReader := bytes.NewReader(data) 62 63 publicKey, privateKey, err := crypto.GenerateEd25519KeyPair() 64 if err != nil { 65 t.Error(err) 66 } 67 68 hash, signature, err := hashAndSignFile(privateKey, dataReader) 69 if err != nil { 70 t.Error(err) 71 } 72 73 hash[1] += 1 74 75 dataReader.Seek(0, io.SeekStart) 76 77 verifyInput := VerifyInput{ 78 Reader: dataReader, 79 HashBlake3: hash, 80 Signature: signature, 81 } 82 err = hashDataAndVerifySignature(publicKey, verifyInput) 83 if err == nil { 84 t.Errorf("verify accepting an invalid hash for test vector at index: %d", index) 85 } 86 } 87 } 88 89 func TestSignAndVerifyIvalidSignatureInternal(t *testing.T) { 90 randomData10M, err := crypto.RandBytes(10_000_00) 91 if err != nil { 92 t.Error(err) 93 } 94 testsVector := [][]byte{ 95 []byte("Hello World"), 96 {}, 97 randomData10M, 98 } 99 100 for index, data := range testsVector { 101 dataReader := bytes.NewReader(data) 102 103 publicKey, privateKey, err := crypto.GenerateEd25519KeyPair() 104 if err != nil { 105 t.Error(err) 106 } 107 108 hash, signature, err := hashAndSignFile(privateKey, dataReader) 109 if err != nil { 110 t.Error(err) 111 } 112 113 signature[1] += 1 114 115 dataReader.Seek(0, io.SeekStart) 116 117 verifyInput := VerifyInput{ 118 Reader: dataReader, 119 HashBlake3: hash, 120 Signature: signature, 121 } 122 err = hashDataAndVerifySignature(publicKey, verifyInput) 123 if err == nil { 124 t.Errorf("verify accepting an invalid signature for test vector at index: %d", index) 125 } 126 } 127 }