chore: Remove the use of curve25519 package

This commit is contained in:
H1JK 2023-12-09 15:43:40 +08:00
parent 0ab73a9beb
commit 78e5d3229e
2 changed files with 12 additions and 11 deletions

View File

@ -1,13 +1,13 @@
package outbound package outbound
import ( import (
"crypto/ecdh"
"encoding/base64" "encoding/base64"
"encoding/hex" "encoding/hex"
"errors" "errors"
"fmt"
tlsC "github.com/metacubex/mihomo/component/tls" tlsC "github.com/metacubex/mihomo/component/tls"
"golang.org/x/crypto/curve25519"
) )
type RealityOptions struct { type RealityOptions struct {
@ -19,10 +19,16 @@ func (o RealityOptions) Parse() (*tlsC.RealityConfig, error) {
if o.PublicKey != "" { if o.PublicKey != "" {
config := new(tlsC.RealityConfig) config := new(tlsC.RealityConfig)
n, err := base64.RawURLEncoding.Decode(config.PublicKey[:], []byte(o.PublicKey)) const x25519ScalarSize = 32
if err != nil || n != curve25519.ScalarSize { var publicKey [x25519ScalarSize]byte
n, err := base64.RawURLEncoding.Decode(publicKey[:], []byte(o.PublicKey))
if err != nil || n != x25519ScalarSize {
return nil, errors.New("invalid REALITY public key") return nil, errors.New("invalid REALITY public key")
} }
config.PublicKey, err = ecdh.X25519().NewPublicKey(publicKey[:])
if err != nil {
return nil, fmt.Errorf("fail to create REALITY public key: %w", err)
}
n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID)) n, err = hex.Decode(config.ShortID[:], []byte(o.ShortID))
if err != nil || n > tlsC.RealityMaxShortIDLen { if err != nil || n > tlsC.RealityMaxShortIDLen {

View File

@ -28,7 +28,6 @@ import (
utls "github.com/sagernet/utls" utls "github.com/sagernet/utls"
"github.com/zhangyunhao116/fastrand" "github.com/zhangyunhao116/fastrand"
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/curve25519"
"golang.org/x/crypto/hkdf" "golang.org/x/crypto/hkdf"
"golang.org/x/net/http2" "golang.org/x/net/http2"
) )
@ -36,7 +35,7 @@ import (
const RealityMaxShortIDLen = 8 const RealityMaxShortIDLen = 8
type RealityConfig struct { type RealityConfig struct {
PublicKey [curve25519.ScalarSize]byte PublicKey *ecdh.PublicKey
ShortID [RealityMaxShortIDLen]byte ShortID [RealityMaxShortIDLen]byte
} }
@ -82,10 +81,6 @@ func GetRealityConn(ctx context.Context, conn net.Conn, ClientFingerprint string
//log.Debugln("REALITY hello.sessionId[:16]: %v", hello.SessionId[:16]) //log.Debugln("REALITY hello.sessionId[:16]: %v", hello.SessionId[:16])
publicKey, err := ecdh.X25519().NewPublicKey(realityConfig.PublicKey[:])
if err != nil {
return nil, err
}
ecdheKey := uConn.HandshakeState.State13.EcdheKey ecdheKey := uConn.HandshakeState.State13.EcdheKey
if ecdheKey == nil { if ecdheKey == nil {
// WTF??? // WTF???
@ -94,7 +89,7 @@ func GetRealityConn(ctx context.Context, conn net.Conn, ClientFingerprint string
} }
continue // retry continue // retry
} }
authKey, err := ecdheKey.ECDH(publicKey) authKey, err := ecdheKey.ECDH(realityConfig.PublicKey)
if err != nil { if err != nil {
return nil, err return nil, err
} }