-
Notifications
You must be signed in to change notification settings - Fork 376
/
cipher_test.go
198 lines (179 loc) · 3.61 KB
/
cipher_test.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
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
package ws
import (
"fmt"
"math/rand"
"reflect"
"testing"
)
func TestCipher(t *testing.T) {
type test struct {
name string
in []byte
mask [4]byte
offset int
}
cases := []test{
{
name: "simple",
in: []byte("Hello, XOR!"),
mask: [4]byte{1, 2, 3, 4},
},
{
name: "simple",
in: []byte("Hello, XOR!"),
mask: [4]byte{255, 255, 255, 255},
},
}
for offset := 0; offset < 4; offset++ {
for tail := 0; tail < 8; tail++ {
for b64 := 0; b64 < 3; b64++ {
var (
ln = remain[offset]
rn = tail
n = b64*8 + ln + rn
)
p := make([]byte, n)
rand.Read(p)
var m [4]byte
rand.Read(m[:])
cases = append(cases, test{
in: p,
mask: m,
offset: offset,
})
}
}
}
for _, test := range cases {
t.Run(test.name, func(t *testing.T) {
// naive implementation of xor-cipher
exp := cipherNaive(test.in, test.mask, test.offset)
res := make([]byte, len(test.in))
copy(res, test.in)
Cipher(res, test.mask, test.offset)
if !reflect.DeepEqual(res, exp) {
t.Errorf("Cipher(%v, %v):\nact:\t%v\nexp:\t%v\n", test.in, test.mask, res, exp)
}
})
}
}
func TestCipherChops(t *testing.T) {
for n := 2; n <= 1024; n <<= 1 {
t.Run(fmt.Sprintf("%d", n), func(t *testing.T) {
p := make([]byte, n)
b := make([]byte, n)
var m [4]byte
_, err := rand.Read(p)
if err != nil {
t.Fatal(err)
}
_, err = rand.Read(m[:])
if err != nil {
t.Fatal(err)
}
exp := cipherNaive(p, m, 0)
for i := 1; i <= n; i <<= 1 {
copy(b, p)
s := n / i
for j := s; j <= n; j += s {
l, r := j-s, j
Cipher(b[l:r], m, l)
if !reflect.DeepEqual(b[l:r], exp[l:r]) {
t.Fatalf("unexpected Cipher([%d:%d]) = %x; want %x", l, r, b[l:r], exp[l:r])
}
}
}
l := 0
copy(b, p)
for l < n {
r := rand.Intn(n-l) + l + 1
Cipher(b[l:r], m, l)
if !reflect.DeepEqual(b[l:r], exp[l:r]) {
t.Fatalf("unexpected Cipher([%d:%d]):\nact:\t%v\nexp:\t%v\nact:\t%#x\nexp:\t%#x\n\n", l, r, b[l:r], exp[l:r], b[l:r], exp[l:r])
}
l = r
}
})
}
}
func cipherNaive(p []byte, m [4]byte, pos int) []byte {
r := make([]byte, len(p))
copy(r, p)
cipherNaiveNoCp(r, m, pos)
return r
}
func cipherNaiveNoCp(p []byte, m [4]byte, pos int) []byte {
for i := 0; i < len(p); i++ {
p[i] ^= m[(pos+i)%4]
}
return p
}
func BenchmarkCipher(b *testing.B) {
for _, bench := range []struct {
size int
offset int
}{
{
size: 7,
offset: 1,
},
{
size: 125,
},
{
size: 1024,
},
{
size: 4096,
},
{
size: 4100,
offset: 4,
},
{
size: 4099,
offset: 3,
},
{
size: (1 << 15) + 7,
offset: 49,
},
} {
bts := make([]byte, bench.size)
_, err := rand.Read(bts)
if err != nil {
b.Fatal(err)
}
var mask [4]byte
_, err = rand.Read(mask[:])
if err != nil {
b.Fatal(err)
}
b.Run(fmt.Sprintf("naive_bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
var sink int64
b.SetBytes(int64(bench.size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
r := cipherNaiveNoCp(bts, mask, bench.offset)
sink += int64(len(r))
}
sinkValue(sink)
})
b.Run(fmt.Sprintf("bytes=%d;offset=%d", bench.size, bench.offset), func(b *testing.B) {
var sink int64
b.SetBytes(int64(bench.size))
b.ResetTimer()
for i := 0; i < b.N; i++ {
Cipher(bts, mask, bench.offset)
sink += int64(len(bts))
}
sinkValue(sink)
})
}
}
// sinkValue makes variable used and prevents dead code elimination.
func sinkValue(v int64) {
if r := rand.Float32(); r > 2 {
panic(fmt.Sprintf("impossible %g: %v", r, v))
}
}