From c276f504b57b4073cb3de7e5ad0113b4def5f418 Mon Sep 17 00:00:00 2001 From: Andy Wang Date: Fri, 16 Aug 2019 23:44:40 +0100 Subject: [PATCH] Use ENUM constants for encryption methods --- internal/multiplex/obfs.go | 12 +++++++++--- internal/multiplex/obfs_test.go | 6 +++--- internal/multiplex/session_test.go | 14 +++++++------- 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/internal/multiplex/obfs.go b/internal/multiplex/obfs.go index 733bb97..b71e8a0 100644 --- a/internal/multiplex/obfs.go +++ b/internal/multiplex/obfs.go @@ -20,6 +20,12 @@ var putU32 = binary.BigEndian.PutUint32 const HEADER_LEN = 12 +const ( + E_METHOD_PLAIN = iota + E_METHOD_AES_GCM + E_METHOD_CHACHA20_POLY1305 +) + func MakeObfs(salsaKey [32]byte, payloadCipher cipher.AEAD) Obfser { obfs := func(f *Frame, buf []byte) (int, error) { var extraLen uint8 @@ -133,9 +139,9 @@ func GenerateObfs(encryptionMethod byte, sessionKey []byte) (obfuscator *Obfusca var payloadCipher cipher.AEAD switch encryptionMethod { - case 0x00: + case E_METHOD_PLAIN: payloadCipher = nil - case 0x01: + case E_METHOD_AES_GCM: var c cipher.Block c, err = aes.NewCipher(sessionKey) if err != nil { @@ -145,7 +151,7 @@ func GenerateObfs(encryptionMethod byte, sessionKey []byte) (obfuscator *Obfusca if err != nil { return } - case 0x02: + case E_METHOD_CHACHA20_POLY1305: payloadCipher, err = chacha20poly1305.New(sessionKey) if err != nil { return diff --git a/internal/multiplex/obfs_test.go b/internal/multiplex/obfs_test.go index c9833ae..1dddd4d 100644 --- a/internal/multiplex/obfs_test.go +++ b/internal/multiplex/obfs_test.go @@ -39,7 +39,7 @@ func TestGenerateObfs(t *testing.T) { } t.Run("plain", func(t *testing.T) { - obfuscator, err := GenerateObfs(0x00, sessionKey) + obfuscator, err := GenerateObfs(E_METHOD_PLAIN, sessionKey) if err != nil { t.Errorf("failed to generate obfuscator %v", err) } else { @@ -47,7 +47,7 @@ func TestGenerateObfs(t *testing.T) { } }) t.Run("aes-gcm", func(t *testing.T) { - obfuscator, err := GenerateObfs(0x01, sessionKey) + obfuscator, err := GenerateObfs(E_METHOD_AES_GCM, sessionKey) if err != nil { t.Errorf("failed to generate obfuscator %v", err) } else { @@ -55,7 +55,7 @@ func TestGenerateObfs(t *testing.T) { } }) t.Run("chacha20-poly1305", func(t *testing.T) { - obfuscator, err := GenerateObfs(0x02, sessionKey) + obfuscator, err := GenerateObfs(E_METHOD_CHACHA20_POLY1305, sessionKey) if err != nil { t.Errorf("failed to generate obfuscator %v", err) } else { diff --git a/internal/multiplex/session_test.go b/internal/multiplex/session_test.go index a2f6d64..d52837d 100644 --- a/internal/multiplex/session_test.go +++ b/internal/multiplex/session_test.go @@ -35,7 +35,7 @@ func TestRecvDataFromRemote(t *testing.T) { sessionKey := make([]byte, 32) rand.Read(sessionKey) t.Run("plain ordered", func(t *testing.T) { - obfuscator, _ := GenerateObfs(0x00, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_PLAIN, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf) @@ -58,7 +58,7 @@ func TestRecvDataFromRemote(t *testing.T) { } }) t.Run("aes-gcm ordered", func(t *testing.T) { - obfuscator, _ := GenerateObfs(0x01, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_AES_GCM, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf) @@ -81,7 +81,7 @@ func TestRecvDataFromRemote(t *testing.T) { } }) t.Run("chacha20-poly1305 ordered", func(t *testing.T) { - obfuscator, _ := GenerateObfs(0x02, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_CHACHA20_POLY1305, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf) @@ -105,7 +105,7 @@ func TestRecvDataFromRemote(t *testing.T) { }) t.Run("plain unordered", func(t *testing.T) { - obfuscator, _ := GenerateObfs(0x00, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_PLAIN, sessionKey) seshConfigUnordered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf) @@ -146,7 +146,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { rand.Read(sessionKey) b.Run("plain", func(b *testing.B) { - obfuscator, _ := GenerateObfs(0x00, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_PLAIN, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf) @@ -159,7 +159,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { }) b.Run("aes-gcm", func(b *testing.B) { - obfuscator, _ := GenerateObfs(0x01, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_AES_GCM, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf) @@ -172,7 +172,7 @@ func BenchmarkRecvDataFromRemote_Ordered(b *testing.B) { }) b.Run("chacha20-poly1305", func(b *testing.B) { - obfuscator, _ := GenerateObfs(0x02, sessionKey) + obfuscator, _ := GenerateObfs(E_METHOD_CHACHA20_POLY1305, sessionKey) seshConfigOrdered.Obfuscator = obfuscator sesh := MakeSession(0, seshConfigOrdered) n, _ := sesh.Obfs(f, obfsBuf)