Pad PQ queries sent over TCP

Fixes #3376
This commit is contained in:
Frank Denis 2026-10-22 17:17:07 +02:00
parent 71c139ad0e
commit f8e8e1e49d
2 changed files with 42 additions and 26 deletions

View file

@ -26,6 +26,7 @@
PQKdfID = 0x01
PQAeadID = 0x01
PQResumedPaddingFloor = 256
PQQueryOverhead = PQClientMagicLen + PQXWingCiphertextSize + HalfNonceSize + TagSize
)
var (
@ -111,12 +112,11 @@ func pqResumedSharedKey(resumeSecret [32]byte, clientMagic [8]byte, clientNonce,
return key
}
// pqPad applies ISO/IEC 7816-4 padding to the next multiple of 64, with a
// minimum floor (itself a multiple of 64).
// pqPad applies ISO/IEC 7816-4 padding up to floor bytes, or to the next multiple of 64 if the packet does not fit.
func pqPad(packet []byte, floor int) []byte {
target := (len(packet) + 1 + 63) &^ 63
if target < floor {
target = floor
target := floor
if len(packet) >= floor {
target = (len(packet) + 1 + 63) &^ 63
}
padded := make([]byte, target)
copy(padded, packet)
@ -221,19 +221,6 @@ func pqUDPPacketLimit(serverInfo *ServerInfo) int {
return MaxDNSUDPPacketSize
}
// pqResumedPaddingFloor returns the padding floor for a resumed query. Over
// UDP, the encrypted response can never be larger than the query, so the
// query is grown to the regular UDP question size target, leaving the
// resolver the same response budget as a classic query.
func (proxy *Proxy) pqResumedPaddingFloor(serverInfo *ServerInfo, ticketLen int, proto string) int {
if proto != "udp" {
return PQResumedPaddingFloor
}
overhead := pqResumedOverhead(ticketLen)
want := proxy.questionSizeEstimator.MinQuestionSize() - overhead
return Max(PQResumedPaddingFloor, Min((want+63)&^63, (pqUDPPacketLimit(serverInfo)-overhead)&^63))
}
// encryptPQ builds a PQ query: a resumed query when a valid ticket is held,
// otherwise a query that carries an X-Wing ciphertext, reusing the cached
// encapsulation when one is available for the current network epoch.
@ -252,16 +239,22 @@ func (proxy *Proxy) encryptPQ(
if ticket, resumeSecret, ok := serverInfo.pqSession.get(queryEpoch); ok {
key := pqResumedSharedKey(resumeSecret, serverInfo.MagicQuery, clientNonce, ticket)
padded := pqPad(packet, proxy.pqResumedPaddingFloor(serverInfo, len(ticket), proto))
ct := xsecretbox.Seal(nil, nonce, padded, key[:])
out := make([]byte, 0, len(PQResumeMagic)+2+len(ticket)+HalfNonceSize+len(ct))
overhead := pqResumedOverhead(len(ticket))
floor := Max(PQResumedPaddingFloor, minRelayedQuerySize(serverInfo, proto)-overhead)
if proto == "udp" {
// A UDP response can't be larger than its query, so this one gets the same size target as a classic query.
want := proxy.questionSizeEstimator.MinQuestionSize() - overhead
floor = Max(PQResumedPaddingFloor, Min((want+63)&^63, (pqUDPPacketLimit(serverInfo)-overhead)&^63))
}
padded := pqPad(packet, floor)
out := make([]byte, 0, overhead+len(padded))
out = append(out, PQResumeMagic[:]...)
var tl [2]byte
binary.BigEndian.PutUint16(tl[:], uint16(len(ticket)))
out = append(out, tl[:]...)
out = append(out, ticket...)
out = append(out, clientNonce...)
out = append(out, ct...)
out = xsecretbox.Seal(out, nonce, padded, key[:])
return &key, out, clientNonce, queryEpoch, nil
}
@ -274,13 +267,12 @@ func (proxy *Proxy) encryptPQ(
key = pqDeriveSharedKey(kemSS, serverInfo.MagicQuery, serverInfo.PqCertContext, ctKem)
serverInfo.pqSession.storeEncapsulation(ctKem, key, queryEpoch)
}
padded := pqPad(packet, 64)
ct := xsecretbox.Seal(nil, nonce, padded, key[:])
out := make([]byte, 0, PQClientMagicLen+len(ctKem)+HalfNonceSize+len(ct))
padded := pqPad(packet, Max(64, minRelayedQuerySize(serverInfo, proto)-PQQueryOverhead))
out := make([]byte, 0, PQQueryOverhead+len(padded))
out = append(out, serverInfo.MagicQuery[:]...)
out = append(out, ctKem...)
out = append(out, clientNonce...)
out = append(out, ct...)
out = xsecretbox.Seal(out, nonce, padded, key[:])
return &key, out, clientNonce, queryEpoch, nil
}

View file

@ -252,6 +252,30 @@ func TestPQResumedQueryMeetsUDPSizeTarget(t *testing.T) {
}
}
// A relay drops responses larger than the query, so PQ queries sent to it over TCP must use the maximum size.
func TestPQRelayedTCPQueryLeavesRoomForResponse(t *testing.T) {
proxy := newTestPQProxy(1)
serverInfo := newTestPQServerInfo()
serverInfo.Relay = &Relay{Dnscrypt: &DNSCryptRelay{}}
query := mustHex("12340100000100000000000003777777076578616d706c6503636f6d0000010001")
// Over UDP, a truncated answer triggers a TCP retry, so the query keeps its regular size.
if _, out, _, _, err := proxy.Encrypt(serverInfo, query, "udp"); err != nil || len(out) != PQQueryOverhead+64 {
t.Fatalf("relayed UDP query is %d bytes (%v)", len(out), err)
}
_, out, _, _, err := proxy.Encrypt(serverInfo, query, "tcp")
if err != nil || len(out) != MaxDNSPacketSize {
t.Fatalf("relayed TCP query is %d bytes (%v)", len(out), err)
}
serverInfo.pqSession.store(iotaBytes(130, 0x24), [32]byte{42}, time.Now().Add(time.Minute), 1)
_, out, _, _, err = proxy.Encrypt(serverInfo, query, "tcp")
if err != nil || !bytes.HasPrefix(out, PQResumeMagic[:]) || len(out) != MaxDNSPacketSize {
t.Fatalf("relayed resumed TCP query is %d bytes (%v)", len(out), err)
}
}
func TestPQProcessControlDiscardsTicketAfterNetworkChange(t *testing.T) {
proxy := newTestPQProxy(2)
serverInfo := newTestPQServerInfo()