chore: sync vless encryption code

This commit is contained in:
wwqgtxx 2025-08-14 09:57:20 +08:00
parent a18e99f966
commit 41b321dfe1
3 changed files with 20 additions and 17 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -36,13 +37,13 @@ func init() {
type ClientInstance struct { type ClientInstance struct {
sync.RWMutex sync.RWMutex
nfsEKey *mlkem.EncapsulationKey768 nfsEKey *mlkem.EncapsulationKey768
nfsEKeyBytes []byte nfsEKeySha256 [32]byte
xor uint32 xor uint32
minutes time.Duration minutes time.Duration
expire time.Time expire time.Time
baseKey []byte baseKey []byte
ticket []byte ticket []byte
} }
type ClientConn struct { type ClientConn struct {
@ -61,7 +62,7 @@ type ClientConn struct {
func (i *ClientInstance) Init(nfsEKeyBytes []byte, xor uint32, minutes time.Duration) (err error) { func (i *ClientInstance) Init(nfsEKeyBytes []byte, xor uint32, minutes time.Duration) (err error) {
i.nfsEKey, err = mlkem.NewEncapsulationKey768(nfsEKeyBytes) i.nfsEKey, err = mlkem.NewEncapsulationKey768(nfsEKeyBytes)
if xor > 0 { if xor > 0 {
i.nfsEKeyBytes = nfsEKeyBytes i.nfsEKeySha256 = sha256.Sum256(nfsEKeyBytes)
i.xor = xor i.xor = xor
} }
i.minutes = minutes i.minutes = minutes
@ -73,7 +74,7 @@ func (i *ClientInstance) Handshake(conn net.Conn) (net.Conn, error) {
return nil, errors.New("uninitialized") return nil, errors.New("uninitialized")
} }
if i.xor > 0 { if i.xor > 0 {
conn = NewXorConn(conn, i.nfsEKeyBytes) conn = NewXorConn(conn, i.nfsEKeySha256[:])
} }
c := &ClientConn{Conn: conn} c := &ClientConn{Conn: conn}

View File

@ -9,4 +9,5 @@
// https://github.com/XTLS/Xray-core/commit/1720be168fa069332c418503d30341fc6e01df7f // https://github.com/XTLS/Xray-core/commit/1720be168fa069332c418503d30341fc6e01df7f
// https://github.com/XTLS/Xray-core/commit/0fd7691d6b28e05922d7a5a9313d97745a51ea63 // https://github.com/XTLS/Xray-core/commit/0fd7691d6b28e05922d7a5a9313d97745a51ea63
// https://github.com/XTLS/Xray-core/commit/09cc92c61d9067e0d65c1cae9124664ecfc78f43 // https://github.com/XTLS/Xray-core/commit/09cc92c61d9067e0d65c1cae9124664ecfc78f43
// https://github.com/XTLS/Xray-core/commit/7f778a4e2f123dc03fe57fbf24da59dcaf270f8a
package encryption package encryption

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"crypto/cipher" "crypto/cipher"
"crypto/rand" "crypto/rand"
"crypto/sha256"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -23,12 +24,12 @@ type ServerSession struct {
type ServerInstance struct { type ServerInstance struct {
sync.RWMutex sync.RWMutex
nfsDKey *mlkem.DecapsulationKey768 nfsDKey *mlkem.DecapsulationKey768
nfsEKeyBytes []byte nfsEKeySha256 [32]byte
xor uint32 xor uint32
minutes time.Duration minutes time.Duration
sessions map[[21]byte]*ServerSession sessions map[[21]byte]*ServerSession
closed bool closed bool
} }
type ServerConn struct { type ServerConn struct {
@ -47,7 +48,7 @@ type ServerConn struct {
func (i *ServerInstance) Init(nfsDKeySeed []byte, xor uint32, minutes time.Duration) (err error) { func (i *ServerInstance) Init(nfsDKeySeed []byte, xor uint32, minutes time.Duration) (err error) {
i.nfsDKey, err = mlkem.NewDecapsulationKey768(nfsDKeySeed) i.nfsDKey, err = mlkem.NewDecapsulationKey768(nfsDKeySeed)
if xor > 0 { if xor > 0 {
i.nfsEKeyBytes = i.nfsDKey.EncapsulationKey().Bytes() i.nfsEKeySha256 = sha256.Sum256(i.nfsDKey.EncapsulationKey().Bytes())
i.xor = xor i.xor = xor
} }
if minutes > 0 { if minutes > 0 {
@ -86,7 +87,7 @@ func (i *ServerInstance) Handshake(conn net.Conn) (net.Conn, error) {
return nil, errors.New("uninitialized") return nil, errors.New("uninitialized")
} }
if i.xor > 0 { if i.xor > 0 {
conn = NewXorConn(conn, i.nfsEKeyBytes) conn = NewXorConn(conn, i.nfsEKeySha256[:])
} }
c := &ServerConn{Conn: conn} c := &ServerConn{Conn: conn}