-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuzz_transcripts_test.go
More file actions
244 lines (211 loc) · 5.49 KB
/
fuzz_transcripts_test.go
File metadata and controls
244 lines (211 loc) · 5.49 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package thyrse_test
import (
"bytes"
"fmt"
"testing"
"github.com/codahale/thyrse"
"github.com/codahale/thyrse/internal/testdata"
fuzz "github.com/trailofbits/go-fuzz-utils"
)
// FuzzProtocolDivergence generates a random transcript of operations and performs them in on two separate protocol
// objects in parallel, checking to see that all outputs are the same.
func FuzzProtocolDivergence(f *testing.F) {
drbg := testdata.New("thyrse divergence")
for range 10 {
f.Add(drbg.Data(1024))
}
f.Fuzz(func(t *testing.T, data []byte) {
tp, err := fuzz.NewTypeProvider(data)
if err != nil {
t.Skip(err)
}
opCount, err := tp.GetUint16()
if err != nil {
t.Skip(err)
}
p1 := thyrse.New("divergence")
p2 := thyrse.New("divergence")
for range opCount % 50 {
opTypeRaw, err := tp.GetByte()
if err != nil {
t.Skip(err)
}
label, err := tp.GetString()
if err != nil {
t.Skip(err)
}
const opTypeCount = 5 // Mix, Derive, Ratchet, Mask, Seal
switch opType := opTypeRaw % opTypeCount; opType {
case 0: // Mix
input, err := tp.GetBytes()
if err != nil {
t.Skip(err)
}
p1.Mix(label, input)
p2.Mix(label, input)
case 1: // Derive
n, err := tp.GetUint16()
if err != nil || n == 0 {
t.Skip(err)
}
res1, res2 := p1.Derive(label, nil, int(n)), p2.Derive(label, nil, int(n))
if !bytes.Equal(res1, res2) {
t.Fatalf("Divergent Derive outputs: %x != %x", res1, res2)
}
case 2: // Ratchet
p1.Ratchet(label)
p2.Ratchet(label)
case 3: // Mask
input, err := tp.GetBytes()
if err != nil {
t.Skip(err)
}
res1, res2 := p1.Mask(label, nil, input), p2.Mask(label, nil, input)
if !bytes.Equal(res1, res2) {
t.Fatalf("Divergent Mask outputs: %x != %x", res1, res2)
}
case 4: // Seal
input, err := tp.GetBytes()
if err != nil {
t.Skip(err)
}
res1, res2 := p1.Seal(label, nil, input), p2.Seal(label, nil, input)
if !bytes.Equal(res1, res2) {
t.Fatalf("Divergent Seal outputs: %x != %x", res1, res2)
}
default:
panic(fmt.Sprintf("unknown operation type: %v", opType))
}
}
if p1.Equal(p2) != 1 {
t.Fatal("divergent final states")
}
})
}
// FuzzProtocolReversibility generates a transcript of reversible operations (Mix, Derive, Mask, and Seal) and
// performs them on a protocol, recording the outputs. It then runs the transcript's duals (Mix, Derive, Unmask, and
// Open) on another protocol object, ensuring the outputs are the same as the inputs.
func FuzzProtocolReversibility(f *testing.F) {
drbg := testdata.New("thyrse reversibility")
for range 10 {
f.Add(drbg.Data(1024))
}
f.Fuzz(func(t *testing.T, data []byte) {
tp, err := fuzz.NewTypeProvider(data)
if err != nil {
t.Skip(err)
}
opCount, err := tp.GetUint16()
if err != nil {
t.Skip(err)
}
p1 := thyrse.New("reversibility")
var operations []operation
for range opCount % 50 {
opTypeRaw, err := tp.GetByte()
if err != nil {
t.Skip(err)
}
label, err := tp.GetString()
if err != nil {
t.Skip(err)
}
const opTypeCount = 5 // Mix, Derive, Ratchet, Mask, Seal
switch opType := opTypeRaw % opTypeCount; opType {
case 0: // Mix
input, err := tp.GetBytes()
if err != nil {
t.Skip(err)
}
p1.Mix(label, input)
operations = append(operations, operation{
opType: 0,
label: label,
input: input,
})
case 1: // Derive
n, err := tp.GetUint16()
if err != nil || n == 0 {
t.Skip(err)
}
output := p1.Derive(label, nil, max(int(n), 1))
operations = append(operations, operation{
opType: 1,
label: label,
n: max(int(n), 1),
output: output,
})
case 2: // Ratchet
p1.Ratchet(label)
operations = append(operations, operation{
opType: 2,
label: label,
})
case 3: // Mask
input, err := tp.GetBytes()
if err != nil {
t.Skip(err)
}
output := p1.Mask(label, nil, input)
operations = append(operations, operation{
opType: 3,
label: label,
input: input,
output: output,
})
case 4: // Seal
input, err := tp.GetBytes()
if err != nil {
t.Skip(err)
}
output := p1.Seal(label, nil, input)
operations = append(operations, operation{
opType: 4,
label: label,
input: input,
output: output,
})
default:
panic(fmt.Sprintf("unknown operation type: %v", opType))
}
}
p2 := thyrse.New("reversibility")
for _, op := range operations {
switch op.opType {
case 0: // Mix
p2.Mix(op.label, op.input)
case 1: // Derive
output := p2.Derive(op.label, nil, op.n)
if !bytes.Equal(output, op.output) {
t.Fatalf("Divergent Derive outputs: %x != %x", output, op.output)
}
case 2: // Ratchet
p2.Ratchet(op.label)
case 3: // Unmask
plaintext := p2.Unmask(op.label, nil, op.output)
if !bytes.Equal(plaintext, op.input) {
t.Fatalf("Invalid Unmask output: %x != %x", plaintext, op.input)
}
case 4: // Open
plaintext, err := p2.Open(op.label, nil, op.output)
if err != nil {
t.Fatalf("Invalid Open operation: %v", err)
}
if !bytes.Equal(plaintext, op.input) {
t.Fatalf("Invalid Open output: %x != %x", plaintext, op.input)
}
default:
panic(fmt.Sprintf("unknown operation type: %v", op.opType))
}
}
if p1.Equal(p2) != 1 {
t.Fatal("divergent final states")
}
})
}
type operation struct {
opType byte
label string
input, output []byte
n int
}