-
Notifications
You must be signed in to change notification settings - Fork 3
/
fuzz.go
60 lines (49 loc) · 1.08 KB
/
fuzz.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package chacha20poly1305
import (
"bytes"
asm "github.com/mmcloughlin/cryptofuzz/target/chacha20poly1305/fork/asm"
noasm "github.com/mmcloughlin/cryptofuzz/target/chacha20poly1305/fork/noasm"
)
func Fuzz(data []byte) int {
// Read key.
if len(data) < asm.KeySize {
return -1
}
key := data[:asm.KeySize]
data = data[asm.KeySize:]
// Read nonce.
if len(data) < asm.NonceSize {
return -1
}
nonce := data[:asm.NonceSize]
data = data[asm.NonceSize:]
// Read plaintext.
if len(data) == 0 {
return -1
}
plaintext := data
// Construct implementations to be compared.
a, err := asm.New(key)
if err != nil {
panic(err.Error())
}
b, err := noasm.New(key)
if err != nil {
panic(err.Error())
}
// Seal.
aciphertext := a.Seal(nil, nonce, plaintext, nil)
bciphertext := b.Seal(nil, nonce, plaintext, nil)
if !bytes.Equal(aciphertext, bciphertext) {
panic("mismatch")
}
// Roundtrip.
roundtrip, err := a.Open(nil, nonce, aciphertext, nil)
if err != nil {
panic(err.Error())
}
if !bytes.Equal(roundtrip, plaintext) {
panic("roundtrip failed")
}
return 1
}