github.com/arjunbeliever/ignite@v0.0.0-20220406110515-46bbbbec2587/crypto/signify/signify_fuzz.go (about) 1 // Copyright 2020 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 // +build gofuzz 18 19 package signify 20 21 import ( 22 "bufio" 23 "fmt" 24 "io/ioutil" 25 "log" 26 "os" 27 "os/exec" 28 29 fuzz "github.com/google/gofuzz" 30 "github.com/jedisct1/go-minisign" 31 ) 32 33 func Fuzz(data []byte) int { 34 if len(data) < 32 { 35 return -1 36 } 37 tmpFile, err := ioutil.TempFile("", "") 38 if err != nil { 39 panic(err) 40 } 41 defer os.Remove(tmpFile.Name()) 42 defer tmpFile.Close() 43 44 testSecKey, testPubKey := createKeyPair() 45 // Create message 46 tmpFile.Write(data) 47 if err = tmpFile.Close(); err != nil { 48 panic(err) 49 } 50 // Fuzz comments 51 var untrustedComment string 52 var trustedComment string 53 f := fuzz.NewFromGoFuzz(data) 54 f.Fuzz(&untrustedComment) 55 f.Fuzz(&trustedComment) 56 fmt.Printf("untrusted: %v\n", untrustedComment) 57 fmt.Printf("trusted: %v\n", trustedComment) 58 59 err = SignifySignFile(tmpFile.Name(), tmpFile.Name()+".sig", testSecKey, untrustedComment, trustedComment) 60 if err != nil { 61 panic(err) 62 } 63 defer os.Remove(tmpFile.Name() + ".sig") 64 65 signify := "signify" 66 path := os.Getenv("SIGNIFY") 67 if path != "" { 68 signify = path 69 } 70 71 _, err := exec.LookPath(signify) 72 if err != nil { 73 panic(err) 74 } 75 76 // Write the public key into the file to pass it as 77 // an argument to signify-openbsd 78 pubKeyFile, err := ioutil.TempFile("", "") 79 if err != nil { 80 panic(err) 81 } 82 defer os.Remove(pubKeyFile.Name()) 83 defer pubKeyFile.Close() 84 pubKeyFile.WriteString("untrusted comment: signify public key\n") 85 pubKeyFile.WriteString(testPubKey) 86 pubKeyFile.WriteString("\n") 87 88 cmd := exec.Command(signify, "-V", "-p", pubKeyFile.Name(), "-x", tmpFile.Name()+".sig", "-m", tmpFile.Name()) 89 if output, err := cmd.CombinedOutput(); err != nil { 90 panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output)) 91 } 92 93 // Verify the signature using a golang library 94 sig, err := minisign.NewSignatureFromFile(tmpFile.Name() + ".sig") 95 if err != nil { 96 panic(err) 97 } 98 99 pKey, err := minisign.NewPublicKey(testPubKey) 100 if err != nil { 101 panic(err) 102 } 103 104 valid, err := pKey.VerifyFromFile(tmpFile.Name(), sig) 105 if err != nil { 106 panic(err) 107 } 108 if !valid { 109 panic("invalid signature") 110 } 111 return 1 112 } 113 114 func getKey(fileS string) (string, error) { 115 file, err := os.Open(fileS) 116 if err != nil { 117 log.Fatal(err) 118 } 119 defer file.Close() 120 121 scanner := bufio.NewScanner(file) 122 // Discard the first line 123 scanner.Scan() 124 scanner.Scan() 125 return scanner.Text(), scanner.Err() 126 } 127 128 func createKeyPair() (string, string) { 129 // Create key and put it in correct format 130 tmpKey, err := ioutil.TempFile("", "") 131 if err != nil { 132 panic(err) 133 } 134 defer os.Remove(tmpKey.Name()) 135 defer os.Remove(tmpKey.Name() + ".pub") 136 defer os.Remove(tmpKey.Name() + ".sec") 137 cmd := exec.Command("signify", "-G", "-n", "-p", tmpKey.Name()+".pub", "-s", tmpKey.Name()+".sec") 138 if output, err := cmd.CombinedOutput(); err != nil { 139 panic(fmt.Sprintf("could not verify the file: %v, output: \n%s", err, output)) 140 } 141 secKey, err := getKey(tmpKey.Name() + ".sec") 142 if err != nil { 143 panic(err) 144 } 145 pubKey, err := getKey(tmpKey.Name() + ".pub") 146 if err != nil { 147 panic(err) 148 } 149 return secKey, pubKey 150 }