-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtriplesec_test.go
225 lines (192 loc) · 6.23 KB
/
triplesec_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
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
// The design and name of TripleSec is (C) Keybase 2013
// This Go implementation is (C) Filippo Valsorda 2014
// Use of this source code is governed by the MIT License
package triplesec
import (
"bytes"
"encoding/hex"
"testing"
)
func functionThatPrintsUglyWarnings() {}
func isProduction() bool {
return false
}
func testCycle(t *testing.T, version Version) {
plaintext := []byte("1234567890-")
password := []byte("42")
c, err := NewCipher(password, nil, version, functionThatPrintsUglyWarnings, isProduction)
if err != nil {
t.Fatal(err)
}
origPlaintext := append([]byte{}, plaintext...)
ciphertext, err := c.Encrypt(plaintext)
if err != nil {
t.Fatal(err)
}
origCiphertext := append([]byte{}, ciphertext...)
newPlaintext, err := c.Decrypt(ciphertext)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(newPlaintext, plaintext) {
t.Error("newPlaintext != plaintext")
}
if !bytes.Equal(origPlaintext, plaintext) {
t.Error("origPlaintext != plaintext")
}
if !bytes.Equal(origCiphertext, ciphertext) {
t.Error("origCiphertext != ciphertext")
}
if !bytes.Equal(password, []byte("42")) {
t.Error("password changed")
}
}
func TestCycle(t *testing.T) {
for version := range versionParamsLookup {
testCycle(t, version)
}
}
func BenchmarkEncrypt(b *testing.B) {
plaintext := []byte("1234567890-")
password := []byte("42")
c, err := NewCipher(password, nil, Version(3), functionThatPrintsUglyWarnings, isProduction)
if err != nil {
b.Fatal(err)
}
for i := 0; i < b.N; i++ {
_, err := c.Encrypt(plaintext)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkDecrypt(b *testing.B) {
plaintext := []byte("1234567890-")
password := []byte("42")
c, err := NewCipher(password, nil, Version(3), functionThatPrintsUglyWarnings, isProduction)
if err != nil {
b.Fatal(err)
}
ciphertext, err := c.Encrypt(plaintext)
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err = c.Decrypt(ciphertext)
if err != nil {
b.Fatal(err)
}
}
}
func testBiggerBufSizes(t *testing.T, version Version) {
// TODO: should we resize the buffers when we are passed smaller ones?
plaintext := []byte("1234567890-")
password := []byte("42")
c, err := NewCipher(password, nil, version, functionThatPrintsUglyWarnings, isProduction)
if err != nil {
t.Fatal(err)
}
origPlaintext := append([]byte{}, plaintext...)
ciphertext, err := c.Encrypt(plaintext)
if err != nil {
t.Fatal(err)
}
origCiphertext := append([]byte{}, ciphertext...)
newPlaintext, err := c.Decrypt(ciphertext)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(newPlaintext[:len(plaintext)], plaintext) {
t.Error("newPlaintext != plaintext")
}
if !bytes.Equal(origPlaintext, plaintext) {
t.Error("origPlaintext != plaintext")
}
if !bytes.Equal(origCiphertext, ciphertext) {
t.Error("origCiphertext != ciphertext")
}
if !bytes.Equal(password, []byte("42")) {
t.Error("password changed")
}
}
func TestBiggerBufSizes(t *testing.T) {
for version := range versionParamsLookup {
testBiggerBufSizes(t, version)
}
}
func testSmallerBufSizes(t *testing.T, version Version) {
plaintext := []byte("1234567890-")
password := []byte("42")
c, err := NewCipher(password, nil, version, functionThatPrintsUglyWarnings, isProduction)
if err != nil {
t.Fatal(err)
}
origPlaintext := append([]byte{}, plaintext...)
ciphertext, err := c.Encrypt(plaintext)
if err != nil {
t.Fatal(err)
}
origCiphertext := append([]byte{}, ciphertext...)
newPlaintext, err := c.Decrypt(ciphertext)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(newPlaintext, plaintext) {
t.Error("newPlaintext != plaintext")
}
if !bytes.Equal(origPlaintext, plaintext) {
t.Error("origPlaintext != plaintext")
}
if !bytes.Equal(origCiphertext, ciphertext) {
t.Error("origCiphertext != ciphertext")
}
if !bytes.Equal(password, []byte("42")) {
t.Error("password changed")
}
}
func TestSmallerBufSizes(t *testing.T) {
for version := range versionParamsLookup {
testSmallerBufSizes(t, version)
}
}
func TestBadPwV3(t *testing.T) {
ciphertext, _ := hex.DecodeString("1c94d7de0000000359a5e5d60f09ebb6bc3fdab6642725e03bc3d51e167fa60327df567476d467f8b6ce65a909b4f582443f230ff10a36f60315ebce1cf1395d7b763c768764207f4f4cc5207a21272f3a5542f35db73c94fbc7bd551d4d6b0733e0b27fdf9606b8a26d45c4b79818791b6ae1ad34c23e58de482d454895618a1528ec722c5218650f8a2f55f63a6066ccf875f46c9b68ed31bc1ddce8881d704be597e1b5006d16ebe091a02e24d569f3d09b0578d12f955543e1a1f1dd75784b8b4cba7ca0bb7044389eb6354cea628a21538d")
c, _ := NewCipher([]byte("423"), nil, Version(3), functionThatPrintsUglyWarnings, isProduction)
_, err := c.Decrypt(ciphertext)
if err == nil {
t.Error("needed an error on bad PW")
} else if _, ok := err.(BadPassphraseError); !ok {
t.Error("got wrong type of error")
}
}
func TestBadPwV4(t *testing.T) {
ciphertext, _ := hex.DecodeString("1c94d7de00000004ab62712b6a43fba017a7a13333b59d3650365fcebded3bd64741a99b2070fa12e4145766afa1b2dbcaca4d2053963f441be82963046766f16a4f82186a9ba7a7cf04f19da9a695a4a7ae9f6036a5d3b456ad97d512af55e61245c9a096db8a4b73cc64491ec67e5381ec14f2ce6f3db922b5cec7cea86305681a0204d6fb9522e7ec8851f8d85c4e4319473c2899ece487324093f144d27ea13355fd9a03a0765afc5f5750152824c6632dfd50bd25ac340aaa6e6cd3664c21d4501ef8b3107c3fa62b9a97d79a9ccc64")
c, _ := NewCipher([]byte("wrong password"), nil, Version(4), functionThatPrintsUglyWarnings, isProduction)
_, err := c.Decrypt(ciphertext)
if err == nil {
t.Error("needed an error on bad PW")
} else if _, ok := err.(BadPassphraseError); !ok {
t.Error("got wrong type of error")
}
}
func TestRandomness(t *testing.T) {
for _, version := range []Version{3, 4} {
key := []byte("YELLOW_SUBMARINE")
cipher, _ := NewCipher(key, nil, version, functionThatPrintsUglyWarnings, isProduction)
pt := []byte("foobar")
once, _ := cipher.Encrypt(pt)
twice, _ := cipher.Encrypt(pt)
onceHex := hex.EncodeToString(once)
twiceHex := hex.EncodeToString(twice)
if onceHex == twiceHex {
t.Errorf("got same encryption twice in a row")
}
cipher, _ = NewCipher(key, nil, version, functionThatPrintsUglyWarnings, isProduction)
thrice, _ := cipher.Encrypt(pt)
thriceHex := hex.EncodeToString(thrice)
if onceHex == thriceHex || twiceHex == thriceHex {
t.Errorf("got same encryption twice after making cipher again")
}
}
}