@@ -26,6 +26,7 @@ const (
2626 PQKdfID = 0x01
2727 PQAeadID = 0x01
2828 PQResumedPaddingFloor = 256
29+ PQQueryOverhead = PQClientMagicLen + PQXWingCiphertextSize + HalfNonceSize + TagSize
2930)
3031
3132var (
@@ -111,12 +112,11 @@ func pqResumedSharedKey(resumeSecret [32]byte, clientMagic [8]byte, clientNonce,
111112 return key
112113}
113114
114- // pqPad applies ISO/IEC 7816-4 padding to the next multiple of 64, with a
115- // minimum floor (itself a multiple of 64).
115+ // pqPad applies ISO/IEC 7816-4 padding up to floor bytes, or to the next multiple of 64 if the packet does not fit.
116116func pqPad (packet []byte , floor int ) []byte {
117- target := ( len ( packet ) + 1 + 63 ) &^ 63
118- if target < floor {
119- target = floor
117+ target := floor
118+ if len ( packet ) >= floor {
119+ target = ( len ( packet ) + 1 + 63 ) &^ 63
120120 }
121121 padded := make ([]byte , target )
122122 copy (padded , packet )
@@ -221,19 +221,6 @@ func pqUDPPacketLimit(serverInfo *ServerInfo) int {
221221 return MaxDNSUDPPacketSize
222222}
223223
224- // pqResumedPaddingFloor returns the padding floor for a resumed query. Over
225- // UDP, the encrypted response can never be larger than the query, so the
226- // query is grown to the regular UDP question size target, leaving the
227- // resolver the same response budget as a classic query.
228- func (proxy * Proxy ) pqResumedPaddingFloor (serverInfo * ServerInfo , ticketLen int , proto string ) int {
229- if proto != "udp" {
230- return PQResumedPaddingFloor
231- }
232- overhead := pqResumedOverhead (ticketLen )
233- want := proxy .questionSizeEstimator .MinQuestionSize () - overhead
234- return Max (PQResumedPaddingFloor , Min ((want + 63 )&^63 , (pqUDPPacketLimit (serverInfo )- overhead )&^63 ))
235- }
236-
237224// encryptPQ builds a PQ query: a resumed query when a valid ticket is held,
238225// otherwise a query that carries an X-Wing ciphertext, reusing the cached
239226// encapsulation when one is available for the current network epoch.
@@ -252,16 +239,22 @@ func (proxy *Proxy) encryptPQ(
252239
253240 if ticket , resumeSecret , ok := serverInfo .pqSession .get (queryEpoch ); ok {
254241 key := pqResumedSharedKey (resumeSecret , serverInfo .MagicQuery , clientNonce , ticket )
255- padded := pqPad (packet , proxy .pqResumedPaddingFloor (serverInfo , len (ticket ), proto ))
256- ct := xsecretbox .Seal (nil , nonce , padded , key [:])
257- out := make ([]byte , 0 , len (PQResumeMagic )+ 2 + len (ticket )+ HalfNonceSize + len (ct ))
242+ overhead := pqResumedOverhead (len (ticket ))
243+ floor := Max (PQResumedPaddingFloor , minRelayedQuerySize (serverInfo , proto )- overhead )
244+ if proto == "udp" {
245+ // A UDP response can't be larger than its query, so this one gets the same size target as a classic query.
246+ want := proxy .questionSizeEstimator .MinQuestionSize () - overhead
247+ floor = Max (PQResumedPaddingFloor , Min ((want + 63 )&^63 , (pqUDPPacketLimit (serverInfo )- overhead )&^63 ))
248+ }
249+ padded := pqPad (packet , floor )
250+ out := make ([]byte , 0 , overhead + len (padded ))
258251 out = append (out , PQResumeMagic [:]... )
259252 var tl [2 ]byte
260253 binary .BigEndian .PutUint16 (tl [:], uint16 (len (ticket )))
261254 out = append (out , tl [:]... )
262255 out = append (out , ticket ... )
263256 out = append (out , clientNonce ... )
264- out = append (out , ct ... )
257+ out = xsecretbox . Seal (out , nonce , padded , key [:] )
265258 return & key , out , clientNonce , queryEpoch , nil
266259 }
267260
@@ -274,13 +267,12 @@ func (proxy *Proxy) encryptPQ(
274267 key = pqDeriveSharedKey (kemSS , serverInfo .MagicQuery , serverInfo .PqCertContext , ctKem )
275268 serverInfo .pqSession .storeEncapsulation (ctKem , key , queryEpoch )
276269 }
277- padded := pqPad (packet , 64 )
278- ct := xsecretbox .Seal (nil , nonce , padded , key [:])
279- out := make ([]byte , 0 , PQClientMagicLen + len (ctKem )+ HalfNonceSize + len (ct ))
270+ padded := pqPad (packet , Max (64 , minRelayedQuerySize (serverInfo , proto )- PQQueryOverhead ))
271+ out := make ([]byte , 0 , PQQueryOverhead + len (padded ))
280272 out = append (out , serverInfo .MagicQuery [:]... )
281273 out = append (out , ctKem ... )
282274 out = append (out , clientNonce ... )
283- out = append (out , ct ... )
275+ out = xsecretbox . Seal (out , nonce , padded , key [:] )
284276 return & key , out , clientNonce , queryEpoch , nil
285277}
286278
0 commit comments