2019-08-02 19:50:08 +00:00
|
|
|
package multiplex
|
|
|
|
|
|
|
|
import (
|
2019-08-03 21:05:06 +00:00
|
|
|
"bytes"
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"golang.org/x/crypto/chacha20poly1305"
|
2019-08-02 19:50:08 +00:00
|
|
|
"math/rand"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"testing/quick"
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:15:12 +00:00
|
|
|
func TestGenerateObfs(t *testing.T) {
|
2020-04-07 20:19:40 +00:00
|
|
|
var sessionKey [32]byte
|
|
|
|
rand.Read(sessionKey[:])
|
2019-08-02 19:50:08 +00:00
|
|
|
|
2020-04-10 15:14:32 +00:00
|
|
|
run := func(obfuscator Obfuscator, ct *testing.T) {
|
2019-08-04 09:38:49 +00:00
|
|
|
obfsBuf := make([]byte, 512)
|
2020-12-22 14:45:29 +00:00
|
|
|
_testFrame, _ := quick.Value(reflect.TypeOf(&Frame{}), rand.New(rand.NewSource(42)))
|
2019-08-03 21:05:06 +00:00
|
|
|
testFrame := _testFrame.Interface().(*Frame)
|
2020-04-13 21:48:28 +00:00
|
|
|
i, err := obfuscator.Obfs(testFrame, obfsBuf, 0)
|
2019-08-03 21:05:06 +00:00
|
|
|
if err != nil {
|
2019-08-04 09:38:49 +00:00
|
|
|
ct.Error("failed to obfs ", err)
|
2019-08-08 14:08:16 +00:00
|
|
|
return
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
2019-08-02 19:50:08 +00:00
|
|
|
|
2020-12-22 14:45:29 +00:00
|
|
|
var resultFrame Frame
|
|
|
|
err = obfuscator.Deobfs(&resultFrame, obfsBuf[:i])
|
2019-08-03 21:05:06 +00:00
|
|
|
if err != nil {
|
2019-08-04 09:38:49 +00:00
|
|
|
ct.Error("failed to deobfs ", err)
|
2019-08-08 14:08:16 +00:00
|
|
|
return
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
if !bytes.Equal(testFrame.Payload, resultFrame.Payload) || testFrame.StreamID != resultFrame.StreamID {
|
2019-08-04 09:38:49 +00:00
|
|
|
ct.Error("expecting", testFrame,
|
2019-08-03 21:05:06 +00:00
|
|
|
"got", resultFrame)
|
2019-08-08 14:08:16 +00:00
|
|
|
return
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
2019-08-02 19:50:08 +00:00
|
|
|
}
|
2019-08-03 21:05:06 +00:00
|
|
|
|
|
|
|
t.Run("plain", func(t *testing.T) {
|
2020-10-21 15:42:24 +00:00
|
|
|
obfuscator, err := MakeObfuscator(EncryptionMethodPlain, sessionKey)
|
2019-09-01 19:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to generate obfuscator %v", err)
|
|
|
|
} else {
|
|
|
|
run(obfuscator, t)
|
|
|
|
}
|
|
|
|
})
|
2019-08-03 21:05:06 +00:00
|
|
|
t.Run("aes-gcm", func(t *testing.T) {
|
2020-10-21 15:42:24 +00:00
|
|
|
obfuscator, err := MakeObfuscator(EncryptionMethodAESGCM, sessionKey)
|
2019-09-01 19:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to generate obfuscator %v", err)
|
|
|
|
} else {
|
|
|
|
run(obfuscator, t)
|
|
|
|
}
|
|
|
|
})
|
2019-08-03 21:05:06 +00:00
|
|
|
t.Run("chacha20-poly1305", func(t *testing.T) {
|
2020-10-21 15:42:24 +00:00
|
|
|
obfuscator, err := MakeObfuscator(EncryptionMethodChaha20Poly1305, sessionKey)
|
2019-08-03 21:05:06 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("failed to generate obfuscator %v", err)
|
2019-08-06 23:19:30 +00:00
|
|
|
} else {
|
|
|
|
run(obfuscator, t)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
t.Run("unknown encryption method", func(t *testing.T) {
|
2020-04-08 20:47:31 +00:00
|
|
|
_, err := MakeObfuscator(0xff, sessionKey)
|
2019-08-06 23:19:30 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("unknown encryption mehtod error expected")
|
|
|
|
}
|
|
|
|
})
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkObfs(b *testing.B) {
|
|
|
|
testPayload := make([]byte, 1024)
|
|
|
|
rand.Read(testPayload)
|
|
|
|
testFrame := &Frame{
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
testPayload,
|
2019-08-02 19:50:08 +00:00
|
|
|
}
|
|
|
|
|
2020-04-13 18:40:23 +00:00
|
|
|
obfsBuf := make([]byte, defaultSendRecvBufSize)
|
2019-08-04 09:38:49 +00:00
|
|
|
|
2019-08-03 21:05:06 +00:00
|
|
|
var key [32]byte
|
|
|
|
rand.Read(key[:])
|
|
|
|
b.Run("AES256GCM", func(b *testing.B) {
|
|
|
|
c, _ := aes.NewCipher(key[:])
|
|
|
|
payloadCipher, _ := cipher.NewGCM(c)
|
|
|
|
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, payloadCipher)
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(len(testFrame.Payload)))
|
2019-08-03 21:05:06 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-13 21:48:28 +00:00
|
|
|
obfs(testFrame, obfsBuf, 0)
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("AES128GCM", func(b *testing.B) {
|
|
|
|
c, _ := aes.NewCipher(key[:16])
|
|
|
|
payloadCipher, _ := cipher.NewGCM(c)
|
|
|
|
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, payloadCipher)
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(len(testFrame.Payload)))
|
2019-08-03 21:05:06 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-13 21:48:28 +00:00
|
|
|
obfs(testFrame, obfsBuf, 0)
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("plain", func(b *testing.B) {
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, nil)
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(len(testFrame.Payload)))
|
2019-08-03 21:05:06 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-13 21:48:28 +00:00
|
|
|
obfs(testFrame, obfsBuf, 0)
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("chacha20Poly1305", func(b *testing.B) {
|
|
|
|
payloadCipher, _ := chacha20poly1305.New(key[:16])
|
|
|
|
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, payloadCipher)
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(len(testFrame.Payload)))
|
2019-08-03 21:05:06 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-04-13 21:48:28 +00:00
|
|
|
obfs(testFrame, obfsBuf, 0)
|
2019-08-07 16:06:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkDeobfs(b *testing.B) {
|
|
|
|
testPayload := make([]byte, 1024)
|
|
|
|
rand.Read(testPayload)
|
|
|
|
testFrame := &Frame{
|
|
|
|
1,
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
testPayload,
|
|
|
|
}
|
|
|
|
|
2020-04-13 18:40:23 +00:00
|
|
|
obfsBuf := make([]byte, defaultSendRecvBufSize)
|
2019-08-07 16:06:31 +00:00
|
|
|
|
|
|
|
var key [32]byte
|
|
|
|
rand.Read(key[:])
|
|
|
|
b.Run("AES256GCM", func(b *testing.B) {
|
|
|
|
c, _ := aes.NewCipher(key[:])
|
|
|
|
payloadCipher, _ := cipher.NewGCM(c)
|
|
|
|
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, payloadCipher)
|
2020-04-13 21:48:28 +00:00
|
|
|
n, _ := obfs(testFrame, obfsBuf, 0)
|
2020-04-08 20:47:31 +00:00
|
|
|
deobfs := MakeDeobfs(key, payloadCipher)
|
2019-08-07 16:06:31 +00:00
|
|
|
|
2020-12-22 14:45:29 +00:00
|
|
|
frame := new(Frame)
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(n))
|
2019-08-07 16:06:31 +00:00
|
|
|
b.ResetTimer()
|
|
|
|
for i := 0; i < b.N; i++ {
|
2020-12-22 14:45:29 +00:00
|
|
|
deobfs(frame, obfsBuf[:n])
|
2019-08-07 16:06:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("AES128GCM", func(b *testing.B) {
|
|
|
|
c, _ := aes.NewCipher(key[:16])
|
|
|
|
payloadCipher, _ := cipher.NewGCM(c)
|
|
|
|
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, payloadCipher)
|
2020-04-13 21:48:28 +00:00
|
|
|
n, _ := obfs(testFrame, obfsBuf, 0)
|
2020-04-08 20:47:31 +00:00
|
|
|
deobfs := MakeDeobfs(key, payloadCipher)
|
2019-08-07 16:06:31 +00:00
|
|
|
|
2020-12-22 14:45:29 +00:00
|
|
|
frame := new(Frame)
|
2019-08-07 16:06:31 +00:00
|
|
|
b.ResetTimer()
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(n))
|
2019-08-07 16:06:31 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-12-22 14:45:29 +00:00
|
|
|
deobfs(frame, obfsBuf[:n])
|
2019-08-07 16:06:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("plain", func(b *testing.B) {
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, nil)
|
2020-04-13 21:48:28 +00:00
|
|
|
n, _ := obfs(testFrame, obfsBuf, 0)
|
2020-04-08 20:47:31 +00:00
|
|
|
deobfs := MakeDeobfs(key, nil)
|
2019-08-07 16:06:31 +00:00
|
|
|
|
2020-12-22 14:45:29 +00:00
|
|
|
frame := new(Frame)
|
2019-08-07 16:06:31 +00:00
|
|
|
b.ResetTimer()
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(n))
|
2019-08-07 16:06:31 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-12-22 14:45:29 +00:00
|
|
|
deobfs(frame, obfsBuf[:n])
|
2019-08-07 16:06:31 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
b.Run("chacha20Poly1305", func(b *testing.B) {
|
|
|
|
payloadCipher, _ := chacha20poly1305.New(key[:16])
|
|
|
|
|
2020-04-08 20:47:31 +00:00
|
|
|
obfs := MakeObfs(key, payloadCipher)
|
2020-04-13 21:48:28 +00:00
|
|
|
n, _ := obfs(testFrame, obfsBuf, 0)
|
2020-04-08 20:47:31 +00:00
|
|
|
deobfs := MakeDeobfs(key, payloadCipher)
|
2019-08-07 16:06:31 +00:00
|
|
|
|
2020-12-22 14:45:29 +00:00
|
|
|
frame := new(Frame)
|
2019-08-07 16:06:31 +00:00
|
|
|
b.ResetTimer()
|
2020-04-13 18:40:23 +00:00
|
|
|
b.SetBytes(int64(n))
|
2019-08-07 16:06:31 +00:00
|
|
|
for i := 0; i < b.N; i++ {
|
2020-12-22 14:45:29 +00:00
|
|
|
deobfs(frame, obfsBuf[:n])
|
2019-08-03 21:05:06 +00:00
|
|
|
}
|
|
|
|
})
|
2019-08-02 19:50:08 +00:00
|
|
|
}
|