Skip to content

Commit

Permalink
Feat: refine the logic of security type AUTO (v2fly#1913)
Browse files Browse the repository at this point in the history
  • Loading branch information
H1JK authored Aug 19, 2022
1 parent 0a0fecb commit a8b0991
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion common/protocol/headers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a8b0991

Please sign in to comment.