Skip to content

Commit 38d3c3c

Browse files
committed
TUN-7707: Use X25519Kyber768Draft00 curve when post-quantum feature is enabled
1 parent f2d7653 commit 38d3c3c

File tree

4 files changed

+39
-29
lines changed

4 files changed

+39
-29
lines changed

cmd/cloudflared/tunnel/configuration.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package tunnel
33
import (
44
"crypto/tls"
55
"fmt"
6-
mathRand "math/rand"
76
"net"
87
"net/netip"
98
"os"
@@ -203,12 +202,10 @@ func prepareTunnelConfig(
203202
log.Warn().Str("edgeIPVersion", edgeIPVersion.String()).Err(err).Msg("Overriding edge-ip-version")
204203
}
205204

206-
var pqKexIdx int
207205
if needPQ {
208-
pqKexIdx = mathRand.Intn(len(supervisor.PQKexes))
209206
log.Info().Msgf(
210-
"Using experimental hybrid post-quantum key agreement %s",
211-
supervisor.PQKexNames[supervisor.PQKexes[pqKexIdx]],
207+
"Using hybrid post-quantum key agreement %s",
208+
supervisor.PQKexName,
212209
)
213210
}
214211

@@ -237,7 +234,6 @@ func prepareTunnelConfig(
237234
ProtocolSelector: protocolSelector,
238235
EdgeTLSConfigs: edgeTLSConfigs,
239236
NeedPQ: needPQ,
240-
PQKexIdx: pqKexIdx,
241237
MaxEdgeAddrRetries: uint8(c.Int("max-edge-addr-retries")),
242238
UDPUnregisterSessionTimeout: c.Duration(udpUnregisterSessionTimeoutFlag),
243239
DisableQUICPathMTUDiscovery: c.Bool(quicDisablePathMTUDiscovery),

supervisor/pqtunnels.go

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,12 @@ import (
1212
// issue creating the tunnel, we'll report the first error
1313
// to https://pqtunnels.cloudflareresearch.com.
1414

15-
var (
16-
PQKexes = [...]tls.CurveID{
17-
tls.CurveID(0xfe30), // X25519Kyber512Draft00
18-
tls.CurveID(0xfe31), // X25519Kyber768Draft00
19-
}
20-
PQKexNames map[tls.CurveID]string = map[tls.CurveID]string{
21-
tls.CurveID(0xfe30): "X25519Kyber512Draft00",
22-
tls.CurveID(0xfe31): "X25519Kyber768Draft00",
23-
}
15+
const (
16+
PQKex = tls.CurveID(0xfe31) // X25519Kyber768Draft00
17+
PQKexName = "X25519Kyber768Draft00"
18+
)
2419

20+
var (
2521
pqtMux sync.Mutex // protects pqtSubmitted and pqtWaitForMessage
2622
pqtSubmitted bool // whether an error has already been submitted
2723

@@ -70,7 +66,7 @@ func submitPQTunnelError(rep error, config *TunnelConfig) {
7066
Message string `json:"m"`
7167
Version string `json:"v"`
7268
}{
73-
Group: int(PQKexes[config.PQKexIdx]),
69+
Group: int(PQKex),
7470
Message: rep.Error(),
7571
Version: config.ReportedVersion,
7672
})

supervisor/supervisor.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package supervisor
22

33
import (
44
"context"
5+
"crypto/tls"
56
"errors"
67
"net"
78
"strings"
@@ -10,6 +11,8 @@ import (
1011
"github.com/quic-go/quic-go"
1112
"github.com/rs/zerolog"
1213

14+
qtls120 "github.com/quic-go/qtls-go1-20"
15+
1316
"github.com/cloudflare/cloudflared/connection"
1417
"github.com/cloudflare/cloudflared/edgediscovery"
1518
"github.com/cloudflare/cloudflared/orchestration"
@@ -78,6 +81,8 @@ func NewSupervisor(config *TunnelConfig, orchestrator *orchestration.Orchestrato
7881

7982
reconnectCredentialManager := newReconnectCredentialManager(connection.MetricsNamespace, connection.TunnelSubsystem, config.HAConnections)
8083

84+
registerTLSEventLogger(config.Log)
85+
8186
tracker := tunnelstate.NewConnTracker(config.Log)
8287
log := NewConnAwareLogger(config.Log, tracker, config.Observer)
8388

@@ -336,3 +341,26 @@ func (s *Supervisor) waitForNextTunnel(index int) bool {
336341
func (s *Supervisor) unusedIPs() bool {
337342
return s.edgeIPs.AvailableAddrs() > s.config.HAConnections
338343
}
344+
345+
func registerTLSEventLogger(logger *zerolog.Logger) {
346+
qtls120.SetCFEventHandler(func(ev qtls120.CFEvent) {
347+
logger.Debug().Bool("handshake", ev.IsHandshake()).Str("handshake_duration", ev.Duration().String()).Str("curve", tlsCurveName(ev.KEX())).Msg("QUIC TLS event")
348+
})
349+
}
350+
351+
func tlsCurveName(curve tls.CurveID) string {
352+
switch curve {
353+
case tls.CurveP256:
354+
return "p256"
355+
case tls.CurveP384:
356+
return "p384"
357+
case tls.CurveP521:
358+
return "p521"
359+
case tls.X25519:
360+
return "X25519"
361+
case PQKex:
362+
return PQKexName
363+
default:
364+
return "unknown"
365+
}
366+
}

supervisor/tunnel.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,6 @@ type TunnelConfig struct {
6161

6262
NeedPQ bool
6363

64-
// Index into PQKexes of post-quantum kex to use if NeedPQ is set.
65-
PQKexIdx int
66-
6764
NamedTunnel *connection.NamedTunnelProperties
6865
ProtocolSelector connection.ProtocolSelector
6966
EdgeTLSConfigs map[connection.Protocol]*tls.Config
@@ -585,16 +582,9 @@ func (e *EdgeTunnelServer) serveQUIC(
585582
if e.config.NeedPQ {
586583
// If the user passes the -post-quantum flag, we override
587584
// CurvePreferences to only support hybrid post-quantum key agreements.
588-
cs := make([]tls.CurveID, len(PQKexes))
589-
copy(cs, PQKexes[:])
590-
591-
// It is unclear whether Kyber512 or Kyber768 will become the standard.
592-
// Kyber768 is a bit bigger (and doesn't fit in one initial
593-
// datagram anymore). We're enabling both, but pick randomly which
594-
// one to put first. (TLS will use the first one in the list
595-
// and allows a fallback to the second.)
596-
cs[0], cs[e.config.PQKexIdx] = cs[e.config.PQKexIdx], cs[0]
597-
tlsConfig.CurvePreferences = cs
585+
tlsConfig.CurvePreferences = []tls.CurveID{
586+
PQKex,
587+
}
598588
}
599589

600590
quicConfig := &quic.Config{

0 commit comments

Comments
 (0)