From a8b0991b52b4ea68519a096feb255b5a562152b4 Mon Sep 17 00:00:00 2001 From: Hellojack <106379370+H1JK@users.noreply.github.com> Date: Sat, 20 Aug 2022 03:56:32 +0800 Subject: [PATCH] Feat: refine the logic of security type AUTO (#1913) * Feat: refine the logic of security type AUTO https://cs.opensource.google/go/go/+/master:src/crypto/tls/cipher_suites.go;drc=9e6cd3985dbcdcfe0ed2075be6dbe8c5d6de59cb;l=364 * fix fmt --- common/protocol/headers.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/common/protocol/headers.go b/common/protocol/headers.go index ee2622e62..6b65fd529 100644 --- a/common/protocol/headers.go +++ b/common/protocol/headers.go @@ -3,6 +3,8 @@ package protocol import ( "runtime" + "golang.org/x/sys/cpu" + "github.com/v2fly/v2ray-core/v5/common/bitmask" "github.com/v2fly/v2ray-core/v5/common/net" "github.com/v2fly/v2ray-core/v5/common/uuid" @@ -79,9 +81,21 @@ type CommandSwitchAccount struct { ValidMin byte } +var ( + hasGCMAsmAMD64 = cpu.X86.HasAES && cpu.X86.HasPCLMULQDQ + hasGCMAsmARM64 = cpu.ARM64.HasAES && cpu.ARM64.HasPMULL + // Keep in sync with crypto/aes/cipher_s390x.go. + hasGCMAsmS390X = cpu.S390X.HasAES && cpu.S390X.HasAESCBC && cpu.S390X.HasAESCTR && + (cpu.S390X.HasGHASH || cpu.S390X.HasAESGCM) + + hasAESGCMHardwareSupport = runtime.GOARCH == "amd64" && hasGCMAsmAMD64 || + runtime.GOARCH == "arm64" && hasGCMAsmARM64 || + runtime.GOARCH == "s390x" && hasGCMAsmS390X +) + func (sc *SecurityConfig) GetSecurityType() SecurityType { if sc == nil || sc.Type == SecurityType_AUTO { - if runtime.GOARCH == "amd64" || runtime.GOARCH == "s390x" || runtime.GOARCH == "arm64" { + if hasAESGCMHardwareSupport { return SecurityType_AES128_GCM } return SecurityType_CHACHA20_POLY1305