diff --git a/common/crypto/auth.go b/common/crypto/auth.go index 355e5bc3..13866a4d 100644 --- a/common/crypto/auth.go +++ b/common/crypto/auth.go @@ -43,6 +43,14 @@ func GenerateInitialAEADNonce() BytesGenerator { return GenerateIncreasingNonce([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}) } +func GenerateAEADNonceWithSize(nonceSize int) BytesGenerator { + c := make([]byte, nonceSize) + for i := 0; i < nonceSize; i++ { + c[i] = 0xFF + } + return GenerateIncreasingNonce(c) +} + type Authenticator interface { NonceSize() int Overhead() int diff --git a/infra/conf/shadowsocks.go b/infra/conf/shadowsocks.go index 648a7b8c..f0e778d5 100644 --- a/infra/conf/shadowsocks.go +++ b/infra/conf/shadowsocks.go @@ -18,6 +18,8 @@ func cipherFromString(c string) shadowsocks.CipherType { return shadowsocks.CipherType_AES_256_GCM case "chacha20-poly1305", "aead_chacha20_poly1305", "chacha20-ietf-poly1305": return shadowsocks.CipherType_CHACHA20_POLY1305 + case "xchacha20-poly1305", "aead_xchacha20_poly1305", "xchacha20-ietf-poly1305": + return shadowsocks.CipherType_XCHACHA20_POLY1305 case "none", "plain": return shadowsocks.CipherType_NONE default: diff --git a/proxy/shadowsocks/config.go b/proxy/shadowsocks/config.go index 3fc8eb73..9bbc7736 100644 --- a/proxy/shadowsocks/config.go +++ b/proxy/shadowsocks/config.go @@ -77,6 +77,12 @@ func createChaCha20Poly1305(key []byte) cipher.AEAD { return ChaChaPoly1305 } +func createXChaCha20Poly1305(key []byte) cipher.AEAD { + XChaChaPoly1305, err := chacha20poly1305.NewX(key) + common.Must(err) + return XChaChaPoly1305 +} + func (a *Account) getCipher() (Cipher, error) { switch a.CipherType { case CipherType_AES_128_GCM: @@ -97,6 +103,12 @@ func (a *Account) getCipher() (Cipher, error) { IVBytes: 32, AEADAuthCreator: createChaCha20Poly1305, }, nil + case CipherType_XCHACHA20_POLY1305: + return &AEADCipher{ + KeyBytes: 32, + IVBytes: 32, + AEADAuthCreator: createXChaCha20Poly1305, + }, nil case CipherType_NONE: return NoneCipher{}, nil default: @@ -152,11 +164,12 @@ func (c *AEADCipher) IVSize() int32 { } func (c *AEADCipher) createAuthenticator(key []byte, iv []byte) *crypto.AEADAuthenticator { - nonce := crypto.GenerateInitialAEADNonce() subkey := make([]byte, c.KeyBytes) hkdfSHA1(key, iv, subkey) + aead := c.AEADAuthCreator(subkey) + nonce := crypto.GenerateAEADNonceWithSize(aead.NonceSize()) return &crypto.AEADAuthenticator{ - AEAD: c.AEADAuthCreator(subkey), + AEAD: aead, NonceGenerator: nonce, } } diff --git a/proxy/shadowsocks/config.pb.go b/proxy/shadowsocks/config.pb.go index 8d8d9987..a70ab8f7 100644 --- a/proxy/shadowsocks/config.pb.go +++ b/proxy/shadowsocks/config.pb.go @@ -25,11 +25,12 @@ const ( type CipherType int32 const ( - CipherType_UNKNOWN CipherType = 0 - CipherType_AES_128_GCM CipherType = 5 - CipherType_AES_256_GCM CipherType = 6 - CipherType_CHACHA20_POLY1305 CipherType = 7 - CipherType_NONE CipherType = 8 + CipherType_UNKNOWN CipherType = 0 + CipherType_AES_128_GCM CipherType = 5 + CipherType_AES_256_GCM CipherType = 6 + CipherType_CHACHA20_POLY1305 CipherType = 7 + CipherType_XCHACHA20_POLY1305 CipherType = 8 + CipherType_NONE CipherType = 9 ) // Enum value maps for CipherType. @@ -39,14 +40,16 @@ var ( 5: "AES_128_GCM", 6: "AES_256_GCM", 7: "CHACHA20_POLY1305", - 8: "NONE", + 8: "XCHACHA20_POLY1305", + 9: "NONE", } CipherType_value = map[string]int32{ - "UNKNOWN": 0, - "AES_128_GCM": 5, - "AES_256_GCM": 6, - "CHACHA20_POLY1305": 7, - "NONE": 8, + "UNKNOWN": 0, + "AES_128_GCM": 5, + "AES_256_GCM": 6, + "CHACHA20_POLY1305": 7, + "XCHACHA20_POLY1305": 8, + "NONE": 9, } ) @@ -274,20 +277,22 @@ var file_proxy_shadowsocks_config_proto_rawDesc = []byte{ 0x3c, 0x0a, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x18, 0x01, 0x20, 0x03, 0x28, 0x0b, 0x32, 0x24, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x63, 0x6f, 0x6d, 0x6d, 0x6f, 0x6e, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x63, 0x6f, 0x6c, 0x2e, 0x53, 0x65, 0x72, 0x76, 0x65, 0x72, 0x45, 0x6e, 0x64, - 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x52, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2a, 0x5c, 0x0a, + 0x70, 0x6f, 0x69, 0x6e, 0x74, 0x52, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65, 0x72, 0x2a, 0x74, 0x0a, 0x0a, 0x43, 0x69, 0x70, 0x68, 0x65, 0x72, 0x54, 0x79, 0x70, 0x65, 0x12, 0x0b, 0x0a, 0x07, 0x55, 0x4e, 0x4b, 0x4e, 0x4f, 0x57, 0x4e, 0x10, 0x00, 0x12, 0x0f, 0x0a, 0x0b, 0x41, 0x45, 0x53, 0x5f, 0x31, 0x32, 0x38, 0x5f, 0x47, 0x43, 0x4d, 0x10, 0x05, 0x12, 0x0f, 0x0a, 0x0b, 0x41, 0x45, 0x53, 0x5f, 0x32, 0x35, 0x36, 0x5f, 0x47, 0x43, 0x4d, 0x10, 0x06, 0x12, 0x15, 0x0a, 0x11, 0x43, 0x48, 0x41, 0x43, 0x48, 0x41, 0x32, 0x30, 0x5f, 0x50, 0x4f, 0x4c, 0x59, 0x31, 0x33, 0x30, 0x35, 0x10, - 0x07, 0x12, 0x08, 0x0a, 0x04, 0x4e, 0x4f, 0x4e, 0x45, 0x10, 0x08, 0x42, 0x64, 0x0a, 0x1a, 0x63, - 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x73, 0x68, - 0x61, 0x64, 0x6f, 0x77, 0x73, 0x6f, 0x63, 0x6b, 0x73, 0x50, 0x01, 0x5a, 0x2b, 0x67, 0x69, 0x74, - 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, - 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x73, 0x68, 0x61, - 0x64, 0x6f, 0x77, 0x73, 0x6f, 0x63, 0x6b, 0x73, 0xaa, 0x02, 0x16, 0x58, 0x72, 0x61, 0x79, 0x2e, - 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x53, 0x68, 0x61, 0x64, 0x6f, 0x77, 0x73, 0x6f, 0x63, 0x6b, - 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, + 0x07, 0x12, 0x16, 0x0a, 0x12, 0x58, 0x43, 0x48, 0x41, 0x43, 0x48, 0x41, 0x32, 0x30, 0x5f, 0x50, + 0x4f, 0x4c, 0x59, 0x31, 0x33, 0x30, 0x35, 0x10, 0x08, 0x12, 0x08, 0x0a, 0x04, 0x4e, 0x4f, 0x4e, + 0x45, 0x10, 0x09, 0x42, 0x64, 0x0a, 0x1a, 0x63, 0x6f, 0x6d, 0x2e, 0x78, 0x72, 0x61, 0x79, 0x2e, + 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x73, 0x68, 0x61, 0x64, 0x6f, 0x77, 0x73, 0x6f, 0x63, 0x6b, + 0x73, 0x50, 0x01, 0x5a, 0x2b, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, + 0x78, 0x74, 0x6c, 0x73, 0x2f, 0x78, 0x72, 0x61, 0x79, 0x2d, 0x63, 0x6f, 0x72, 0x65, 0x2f, 0x70, + 0x72, 0x6f, 0x78, 0x79, 0x2f, 0x73, 0x68, 0x61, 0x64, 0x6f, 0x77, 0x73, 0x6f, 0x63, 0x6b, 0x73, + 0xaa, 0x02, 0x16, 0x58, 0x72, 0x61, 0x79, 0x2e, 0x50, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x53, 0x68, + 0x61, 0x64, 0x6f, 0x77, 0x73, 0x6f, 0x63, 0x6b, 0x73, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, + 0x33, } var ( diff --git a/proxy/shadowsocks/config.proto b/proxy/shadowsocks/config.proto index f9db66b2..8f9fb891 100644 --- a/proxy/shadowsocks/config.proto +++ b/proxy/shadowsocks/config.proto @@ -22,7 +22,8 @@ enum CipherType { AES_128_GCM = 5; AES_256_GCM = 6; CHACHA20_POLY1305 = 7; - NONE = 8; + XCHACHA20_POLY1305 = 8; + NONE = 9; } message ServerConfig {