-
Notifications
You must be signed in to change notification settings - Fork 5
/
crypt_codec_test.go
338 lines (278 loc) · 9.19 KB
/
crypt_codec_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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package gouxp
import (
"encoding/binary"
"testing"
"github.com/shaoyuan1943/gouxp/dh64"
)
var macLen = 16
func checkCrypto(t *testing.T, codec CryptCodec, data []byte) bool {
encryptoed, err := codec.Encrypt(data)
if err != nil {
t.Fatalf("codec.Encrypto err: %v\n", err)
return false
}
t.Logf("encryptoed: %v\n", string(encryptoed))
plaintext, err := codec.Decrypt(encryptoed)
if err != nil {
t.Fatalf("codec.Decrypto err: %v\n", err)
return false
}
t.Logf("%v\n", string(plaintext))
return true
}
func testCodec(t *testing.T, codec CryptCodec) {
for i := 0; i < 10; i++ {
data := []byte("afdoiru43098hfjasd'optieoprifsadfr3efvedvte")
testData := make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
data = []byte("5478432967489fnsadfjkasdkfwfdsfasdf234tr3453534534fde")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
data = []byte("wfmn;oi3kuhr48932yfhsadjhfvckjdsahfjdsf[3fasdfas'f;sad'f")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
data = []byte("f43n8ofy43p8fh3p8haisdhf80941ho;iasjhnifoahwe")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
data = []byte("fnpo34hp84h34f8hweafhcnaskljcfidsjdcf0j4-89jh4h3noihjkhj5ipo234u859-23y49p8r34")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
data = []byte("sdnajfhnoui324-hjfo[eqwihui324hf835498248fbjwadefbrwqekf-30ef=-[]f[]sdfsdfbu43i74fgs[w0re")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
data = []byte("fsd34809f43-2fhsdioafhio324h8r1h43fh4389hf9843hf8hsadiofhafh842391h348")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
if checkCrypto(t, codec, testData) {
t.Logf("%s\n", data)
}
}
}
func TestCryptoCodec(t *testing.T) {
codec := createCryptoCodec(UseSalsa20)
testCodec(t, codec)
}
func testErrorCrypto(t *testing.T, codec CryptCodec) {
data := []byte("f43n8ofy43p8fh3p8haisdhf80941ho;iasjhnifoahwe")
myData := make([]byte, len(data)+int(macLen))
copy(myData[macLen:], data)
encryptoed, err := codec.Encrypt(myData)
if err != nil {
t.Fatalf("salsa20Codec.Encrypto err: %v\n", err)
return
}
encryptoed[18] = 'Y'
plaintext, err := codec.Decrypt(encryptoed)
if err != nil {
t.Fatalf("salsa20Codec.Decrypto err: %v\n", err)
return
}
t.Logf("%v\n", string(plaintext))
}
func TestErrorCrypto(t *testing.T) {
//salsa20Codec := CreateCryptoCodec(UseSalsa20)
//testErrorCrypto(t, salsa20Codec)
chacha20Codec := createCryptoCodec(UseChacha20)
testErrorCrypto(t, chacha20Codec)
}
func TestTwoCodec(t *testing.T) {
codec1 := createCryptoCodec(UseSalsa20)
codec2 := createCryptoCodec(UseSalsa20)
data := []byte("sdnajfhnoui324-hjfo[eqwihui324hf835498248fbjwadefbrwqekf-30ef=-[]f[]sdfsdfbu43i74fgs[w0re")
testData := make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
t.Logf("testData: %v", testData)
cipherData, err := codec1.Encrypt(testData)
if err != nil {
t.Fatalf("encrypto err: %v", err)
return
}
t.Logf("cipherData: %v", cipherData)
plaintextData, err := codec2.Decrypt(cipherData)
if err != nil {
t.Fatalf("Decrypto err: %v", err)
return
}
t.Logf("plaintextData: %v", plaintextData)
}
func TestClientServerCodec(t *testing.T) {
clientCodec := createCryptoCodec(UseSalsa20)
privateKey, publicKey := dh64.KeyPair()
clientData := make([]byte, macLen+8)
binary.LittleEndian.PutUint64(clientData[macLen:], publicKey)
clientCipherData, err := clientCodec.Encrypt(clientData)
if err != nil {
t.Fatalf("clientCodec.Encrypto err: %v", err)
return
}
serverCodec := createCryptoCodec(UseSalsa20)
plaintextData, err := serverCodec.Decrypt(clientCipherData)
if err != nil {
t.Fatalf("serverCodec.Decrypto err: %v", err)
return
}
clientPublicKey := binary.LittleEndian.Uint64(plaintextData)
serverPrivateKey, serverPublicKey := dh64.KeyPair()
serverNum := dh64.Secret(serverPrivateKey, clientPublicKey)
var serverNonce [8]byte
binary.LittleEndian.PutUint64(serverNonce[:], serverNum)
serverData := make([]byte, macLen+16)
binary.LittleEndian.PutUint64(serverData[macLen:], serverPublicKey)
serverCipherData, err := serverCodec.Encrypt(serverData)
if err != nil {
t.Fatalf("serverCodec.Encrypto err: %v", err)
return
}
serverCodec.SetReadNonce(serverNonce[:])
serverCodec.SetWriteNonce(serverNonce[:])
serverPlaintextData, err := clientCodec.Decrypt(serverCipherData)
if err != nil {
t.Fatalf("clientCodec.Decrypto err: %v", err)
return
}
key := binary.LittleEndian.Uint64(serverPlaintextData)
clientNum := dh64.Secret(privateKey, key)
var clientNonce [8]byte
binary.LittleEndian.PutUint64(clientNonce[:], clientNum)
clientCodec.SetWriteNonce(clientNonce[:])
clientCodec.SetWriteNonce(clientNonce[:])
data := []byte("sdnajfhnoui324-hjfo[eqwihui324")
testData := make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
cipherData, err := clientCodec.Encrypt(testData)
if err != nil {
t.Fatalf("clientCodec.Encrypto err: %v", err)
return
}
orgData, err := serverCodec.Decrypt(cipherData)
if err != nil {
t.Fatalf("serverCodec.Decrypto err: %v", err)
return
}
t.Logf("plaintextData: %v", string(orgData))
data = []byte("34265734g0f0473281r5671r64392-hjfo[eqwihui324")
testData = make([]byte, len(data)+int(macLen))
copy(testData[macLen:], data)
cipherData, err = clientCodec.Encrypt(testData)
if err != nil {
t.Fatalf("clientCodec.Encrypto err: %v", err)
return
}
orgData, err = serverCodec.Decrypt(cipherData)
if err != nil {
t.Fatalf("serverCodec.Decrypto err: %v", err)
return
}
t.Logf("plaintextData: %v", string(orgData))
codecData(t, clientCodec, serverCodec)
}
func codecData(t *testing.T, encoder, decoder CryptCodec) {
data1 := []byte("342532452345dfsdvdfs-hjfo[eqwihui")
testData1 := make([]byte, len(data1)+int(macLen))
copy(testData1[macLen:], data1)
data2 := []byte("sdnajfhn[eqwihui90-97ghivuyt")
testData2 := make([]byte, len(data2)+int(macLen))
copy(testData2[macLen:], data2)
data3 := []byte("sd341348978 fcasdfhuashdfsdpfuh894390ui894")
testData3 := make([]byte, len(data3)+int(macLen))
copy(testData3[macLen:], data3)
cipherData2, err := encoder.Encrypt(testData2)
if err != nil {
t.Fatalf("encoder.Encrypt data2 err: %v", err)
return
}
cipherData1, err := encoder.Encrypt(testData1)
if err != nil {
t.Fatalf("encoder.Encrypt data1 err: %v", err)
return
}
cipherData3, err := encoder.Encrypt(testData3)
if err != nil {
t.Fatalf("encoder.Encrypt data3 err: %v", err)
return
}
plaintextData3, err := decoder.Decrypt(cipherData3)
if err != nil {
t.Fatalf("encoder.Decrypt cipherData3 err: %v", err)
return
}
plaintextData1, err := decoder.Decrypt(cipherData1)
if err != nil {
t.Fatalf("encoder.Decrypt cipherData1 err: %v", err)
return
}
plaintextData2, err := decoder.Decrypt(cipherData2)
if err != nil {
t.Fatalf("encoder.Decrypt cipherData2 err: %v", err)
return
}
t.Logf("plaintextData1: %v", string(plaintextData1))
t.Logf("plaintextData2: %v", string(plaintextData2))
t.Logf("plaintextData3: %v", string(plaintextData3))
}
func TestUnorderCodec(t *testing.T) {
encoder := createCryptoCodec(UseSalsa20)
decoder := createCryptoCodec(UseSalsa20)
codecData(t, encoder, decoder)
}
func exchangedCodec(t *testing.T) (CryptCodec, CryptCodec) {
clientCodec := createCryptoCodec(UseSalsa20)
privateKey, publicKey := dh64.KeyPair()
clientData := make([]byte, macLen+8)
binary.LittleEndian.PutUint64(clientData[macLen:], publicKey)
clientCipherData, err := clientCodec.Encrypt(clientData)
if err != nil {
t.Fatalf("clientCodec.Encrypto err: %v", err)
return nil, nil
}
serverCodec := createCryptoCodec(UseSalsa20)
plaintextData, err := serverCodec.Decrypt(clientCipherData)
if err != nil {
t.Fatalf("serverCodec.Decrypto err: %v", err)
return nil, nil
}
clientPublicKey := binary.LittleEndian.Uint64(plaintextData)
serverPrivateKey, serverPublicKey := dh64.KeyPair()
serverNum := dh64.Secret(serverPrivateKey, clientPublicKey)
var serverNonce [8]byte
binary.LittleEndian.PutUint64(serverNonce[:], serverNum)
serverData := make([]byte, macLen+16)
binary.LittleEndian.PutUint64(serverData[macLen:], serverPublicKey)
serverCipherData, err := serverCodec.Encrypt(serverData)
if err != nil {
t.Fatalf("serverCodec.Encrypto err: %v", err)
return nil, nil
}
serverCodec.SetReadNonce(serverNonce[:])
serverCodec.SetWriteNonce(serverNonce[:])
serverPlaintextData, err := clientCodec.Decrypt(serverCipherData)
if err != nil {
t.Fatalf("clientCodec.Decrypto err: %v", err)
return nil, nil
}
key := binary.LittleEndian.Uint64(serverPlaintextData)
clientNum := dh64.Secret(privateKey, key)
var clientNonce [8]byte
binary.LittleEndian.PutUint64(clientNonce[:], clientNum)
clientCodec.SetWriteNonce(clientNonce[:])
clientCodec.SetWriteNonce(clientNonce[:])
return clientCodec, serverCodec
}