-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcrypt_test.go
193 lines (167 loc) · 5.67 KB
/
crypt_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
/*
Copyright (c) Facebook, Inc. and its affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
package tacquito
import (
"fmt"
"testing"
"github.com/davecgh/go-spew/spew"
"github.com/stretchr/testify/assert"
)
// returns an encrypted TACACs+ packet's byte values, contains the 12 byte header
// encrypted with secret []byte("fooman")
func getEncryptedBytes() []byte {
return []byte{0xc1, 0x01, 0x01, 0x00, 0x00, 0x00, 0x30, 0x39, 0x00, 0x00, 0x00, 0x2c, 0x9c, 0xed, 0x73,
0xaa, 0x3d, 0x6d, 0x2f, 0x1f, 0xef, 0x62, 0x98, 0x73, 0xf0, 0xac, 0x2f, 0x11, 0x8a, 0xe2, 0x89, 0x8a,
0xcb, 0x50, 0x72, 0xb2, 0x6d, 0xd2, 0xec, 0xab, 0xe1, 0x4e, 0x22, 0x64, 0x4c, 0x7c, 0xb2, 0xe, 0x43,
0xe, 0x33, 0x92, 0x85, 0x47, 0xca, 0xfc}
}
// returns a decrypted TACACs+ packet's byte values, does NOT contain the 12 byte header
func getDecryptedBytes() []byte {
return []byte{0x01, 0x01, 0x01, 0x01, 0x05, 0x0B, 0x14, 0x00, 0x61, 0x64, 0x6D, 0x69, 0x6E, 0x63, 0x6F,
0x6D, 0x6D, 0x61, 0x6E, 0x64, 0x2D, 0x61, 0x70, 0x69, 0x32, 0x30, 0x30, 0x31, 0x3A, 0x34, 0x38,
0x36, 0x30, 0x3A, 0x34, 0x38, 0x36, 0x30, 0x3A, 0x3A, 0x38, 0x38, 0x38, 0x38}
}
func TestDecrypt(t *testing.T) {
encrypted := getEncryptedBytes()
decrypted := getDecryptedBytes()
var header Header
err := Unmarshal(encrypted[:12], &header)
assert.NoError(t, err)
packet := &Packet{Header: &header, Body: encrypted[12:]}
err = crypt([]byte("fooman"), packet)
assert.NoError(t, err)
assert.Equal(t, decrypted, packet.Body)
var body AuthenStart
err = Unmarshal(packet.Body, &body)
assert.NoError(t, err)
t.Log(spew.Sdump(body))
}
func TestEncrypt(t *testing.T) {
encrypted := getEncryptedBytes()
decrypted := getDecryptedBytes()
body, _ := NewAuthenStart(
SetAuthenStartAction(AuthenActionLogin),
SetAuthenStartPrivLvl(PrivLvlUser),
SetAuthenStartType(AuthenTypeASCII),
SetAuthenStartService(AuthenServiceLogin),
SetAuthenStartUser("admin"),
SetAuthenStartPort("command-api"),
SetAuthenStartRemAddr("2001:4860:4860::8888"),
).MarshalBinary()
packet := NewPacket(
SetPacketHeader(
NewHeader(
SetHeaderVersion(Version{MajorVersion: MajorVersion, MinorVersion: MinorVersionOne}),
SetHeaderType(Authenticate),
SetHeaderSessionID(12345),
),
),
SetPacketBody(body),
)
t.Log(spew.Sdump(packet))
err := crypt([]byte("fooman"), packet)
assert.NoError(t, err)
t.Log(spew.Sdump(packet))
assert.Equal(t, encrypted[12:], packet.Body)
err = crypt([]byte("fooman"), packet)
assert.NoError(t, err)
t.Log(spew.Sdump(packet))
assert.Equal(t, decrypted, packet.Body)
}
func TestEncryptDecryptSecretMismatch(t *testing.T) {
body := NewAuthenReply(
SetAuthenReplyStatus(AuthenStatusGetUser),
SetAuthenReplyServerMsg("\nUser Access Verification\n\nUsername:"),
)
b, _ := body.MarshalBinary()
packet := NewPacket(
SetPacketHeader(
NewHeader(
SetHeaderVersion(Version{MajorVersion: MajorVersion, MinorVersion: MinorVersionOne}),
SetHeaderType(Authenticate),
SetHeaderSessionID(12345),
),
),
SetPacketBody(b),
)
secret := []byte("chilled cow")
err := crypt(secret, packet)
assert.NoError(t, err)
// We need to ensure Encrypt and Decrypt operations result in an error if a secret mismatches
// ensure secret mismatch causes an error
secret = []byte("imma bad secret")
err = crypt(secret, packet)
assert.NoError(t, err)
// Unmarshal decrypted bytes back into original packet body type
// this should cause a malformed packet error because of a secret mismatch when encrypt/decrypt
newAuthenReply := &AuthenReply{}
err = newAuthenReply.UnmarshalBinary(packet.Body)
assert.Error(t, err, "a bad secret change should have caused this packet to be malformed")
assert.NotEqual(t, *body, *newAuthenReply)
}
func TestPacketEncryptDecryptUnencryptFlagSet(t *testing.T) {
body := NewAuthenReply(
SetAuthenReplyStatus(AuthenStatusGetUser),
SetAuthenReplyServerMsg("\nUser Access Verification\n\nUsername:"),
)
b, _ := body.MarshalBinary()
packet := NewPacket(
SetPacketHeader(
NewHeader(
SetHeaderVersion(Version{MajorVersion: MajorVersion, MinorVersion: MinorVersionOne}),
SetHeaderType(Authenticate),
SetHeaderFlag(UnencryptedFlag),
SetHeaderSessionID(12345),
),
),
SetPacketBody(b),
)
secret := []byte("chilled cow")
err := crypt(secret, packet)
assert.NoError(t, err)
assert.Equal(t, b, packet.Body)
err = crypt(secret, packet)
assert.NoError(t, err)
assert.Equal(t, b, packet.Body)
}
// benchTest is used for allocation testing
type benchTest struct {
name string
fn func(b *testing.B)
expected func(name string, r testing.BenchmarkResult)
}
func TestCrypterAllocation(t *testing.T) {
tests := []benchTest{
{
name: "encrypt",
fn: BenchmarkCrypterAllocation,
expected: func(name string, r testing.BenchmarkResult) {
t.Log(spew.Sdump(r))
expectedAllocs := 4
actual := r.AllocsPerOp()
assert.EqualValues(t, expectedAllocs, actual, fmt.Sprintf("%s allocations were not nominal; wanted %v got %v", name, expectedAllocs, actual))
},
},
}
for _, test := range tests {
r := testing.Benchmark(test.fn)
test.expected(test.name, r)
}
}
// BenchmarkCrypterAllocation benchmarks the allocs/op crypter takes when called with crypted
// or decrypted bytes. Since the op is the same in both directions we only test one form of it
func BenchmarkCrypterAllocation(b *testing.B) {
encrypted := getEncryptedBytes()
var header Header
Unmarshal(encrypted[:12], &header)
packet := &Packet{Header: &header, Body: encrypted[12:]}
secret := []byte("fooman")
// record allocations regardless of go test -test.bench
b.ReportAllocs()
for i := 0; i < b.N; i++ {
crypt(secret, packet)
}
}