mirror of
https://github.com/XTLS/REALITY.git
synced 2025-08-22 14:38:35 +00:00
REALITY practice: Support X25519MLKEM768 for TLS' communication
Thank https://github.com/XTLS/REALITY/pull/14 @yuhan6665
This commit is contained in:
parent
ce2747b9b0
commit
f07c896f71
@ -25,7 +25,7 @@ import (
|
|||||||
"slices"
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/xtls/reality/fips140tls"
|
"github.com/xtls/reality/fips140tls"
|
||||||
"github.com/xtls/reality/hpke"
|
"github.com/xtls/reality/hpke"
|
||||||
"github.com/xtls/reality/tls13"
|
"github.com/xtls/reality/tls13"
|
||||||
@ -93,12 +93,32 @@ func (hs *serverHandshakeStateTLS13) handshake() error {
|
|||||||
hs.suite = cipherSuiteTLS13ByID(hs.hello.cipherSuite)
|
hs.suite = cipherSuiteTLS13ByID(hs.hello.cipherSuite)
|
||||||
c.cipherSuite = hs.suite.id
|
c.cipherSuite = hs.suite.id
|
||||||
hs.transcript = hs.suite.hash.New()
|
hs.transcript = hs.suite.hash.New()
|
||||||
|
|
||||||
|
var peerData []byte
|
||||||
|
for _, keyShare := range hs.clientHello.keyShares {
|
||||||
|
if keyShare.group == hs.hello.serverShare.group {
|
||||||
|
peerData = keyShare.data
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var peerPub = peerData
|
||||||
|
if hs.hello.serverShare.group == X25519MLKEM768 {
|
||||||
|
peerPub = peerData[mlkem.EncapsulationKeySize768:]
|
||||||
|
}
|
||||||
|
|
||||||
key, _ := generateECDHEKey(c.config.rand(), X25519)
|
key, _ := generateECDHEKey(c.config.rand(), X25519)
|
||||||
copy(hs.hello.serverShare.data, key.PublicKey().Bytes())
|
copy(hs.hello.serverShare.data, key.PublicKey().Bytes())
|
||||||
peerKey, _ := key.Curve().NewPublicKey(hs.clientHello.keyShares[hs.clientHello.keyShares[0].group].data)
|
peerKey, _ := key.Curve().NewPublicKey(peerPub)
|
||||||
hs.sharedKey, _ = key.ECDH(peerKey)
|
hs.sharedKey, _ = key.ECDH(peerKey)
|
||||||
|
|
||||||
|
if hs.hello.serverShare.group == X25519MLKEM768 {
|
||||||
|
k, _ := mlkem.NewEncapsulationKey768(peerData[:mlkem.EncapsulationKeySize768])
|
||||||
|
mlkemSharedSecret, ciphertext := k.Encapsulate()
|
||||||
|
hs.sharedKey = append(mlkemSharedSecret, hs.sharedKey...)
|
||||||
|
copy(hs.hello.serverShare.data, append(ciphertext, hs.hello.serverShare.data[:32]...))
|
||||||
|
}
|
||||||
|
|
||||||
c.serverName = hs.clientHello.serverName
|
c.serverName = hs.clientHello.serverName
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
|
9
tls.go
9
tls.go
@ -34,6 +34,7 @@ import (
|
|||||||
"crypto/cipher"
|
"crypto/cipher"
|
||||||
"crypto/ecdsa"
|
"crypto/ecdsa"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
|
"crypto/mlkem"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
"crypto/x509"
|
"crypto/x509"
|
||||||
@ -54,8 +55,8 @@ import (
|
|||||||
"golang.org/x/crypto/curve25519"
|
"golang.org/x/crypto/curve25519"
|
||||||
"golang.org/x/crypto/hkdf"
|
"golang.org/x/crypto/hkdf"
|
||||||
|
|
||||||
"github.com/xtls/reality/gcm"
|
|
||||||
fipsaes "github.com/xtls/reality/aes"
|
fipsaes "github.com/xtls/reality/aes"
|
||||||
|
"github.com/xtls/reality/gcm"
|
||||||
)
|
)
|
||||||
|
|
||||||
type CloseWriteConn interface {
|
type CloseWriteConn interface {
|
||||||
@ -180,7 +181,7 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
|
|||||||
if copying || err != nil || hs.c.vers != VersionTLS13 || !config.ServerNames[hs.clientHello.serverName] {
|
if copying || err != nil || hs.c.vers != VersionTLS13 || !config.ServerNames[hs.clientHello.serverName] {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
for i, keyShare := range hs.clientHello.keyShares {
|
for _, keyShare := range hs.clientHello.keyShares {
|
||||||
if keyShare.group != X25519 || len(keyShare.data) != 32 {
|
if keyShare.group != X25519 || len(keyShare.data) != 32 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@ -222,7 +223,6 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
|
|||||||
(config.ShortIds[hs.c.ClientShortId]) {
|
(config.ShortIds[hs.c.ClientShortId]) {
|
||||||
hs.c.conn = conn
|
hs.c.conn = conn
|
||||||
}
|
}
|
||||||
hs.clientHello.keyShares[0].group = CurveID(i)
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if config.Show {
|
if config.Show {
|
||||||
@ -308,7 +308,8 @@ func Server(ctx context.Context, conn net.Conn, config *Config) (*Conn, error) {
|
|||||||
if !hs.hello.unmarshal(s2cSaved[recordHeaderLen:handshakeLen]) ||
|
if !hs.hello.unmarshal(s2cSaved[recordHeaderLen:handshakeLen]) ||
|
||||||
hs.hello.vers != VersionTLS12 || hs.hello.supportedVersion != VersionTLS13 ||
|
hs.hello.vers != VersionTLS12 || hs.hello.supportedVersion != VersionTLS13 ||
|
||||||
cipherSuiteTLS13ByID(hs.hello.cipherSuite) == nil ||
|
cipherSuiteTLS13ByID(hs.hello.cipherSuite) == nil ||
|
||||||
hs.hello.serverShare.group != X25519 || len(hs.hello.serverShare.data) != 32 {
|
(!(hs.hello.serverShare.group == X25519 && len(hs.hello.serverShare.data) == 32) &&
|
||||||
|
!(hs.hello.serverShare.group == X25519MLKEM768 && len(hs.hello.serverShare.data) == mlkem.CiphertextSize768+32)) {
|
||||||
break f
|
break f
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user