recheck connection ID for 3 times for some implementations with broken HW PRNG

This commit is contained in:
Lawrence, Rendall
2024-10-01 16:37:03 +03:00
parent 9fc94617ce
commit 49fdd7d908
2 changed files with 9 additions and 4 deletions

View File

@@ -121,7 +121,7 @@ func (g *ConnectionIDGenerator) Generate(ip netip.Addr, now time.Time) (out []by
g.connID[0], g.connID[1], g.connID[2] = g.buff[0], g.buff[7], g.buff[8] g.connID[0], g.connID[1], g.connID[2] = g.buff[0], g.buff[7], g.buff[8]
copy(g.connID[connIDLen-hmacLen:], g.scratch[:hmacLen]) copy(g.connID[connIDLen-hmacLen:], g.scratch[:hmacLen])
log.Debug(). log.Trace().
Stringer("ip", ip). Stringer("ip", ip).
Hex("connID", g.connID). Hex("connID", g.connID).
Msg("generated connection ID") Msg("generated connection ID")
@@ -145,7 +145,7 @@ func (g *ConnectionIDGenerator) Validate(connectionID []byte, ip netip.Addr, now
// ts-skew < now < ts+ttl+skew // ts-skew < now < ts+ttl+skew
res = ts-g.maxClockSkew < nowTS && res res = ts-g.maxClockSkew < nowTS && res
res = nowTS < ts+ttl+g.maxClockSkew && res res = nowTS < ts+ttl+g.maxClockSkew && res
log.Debug(). log.Trace().
Stringer("ip", ip). Stringer("ip", ip).
Hex("connID", connectionID). Hex("connID", connectionID).
Bool("result", res). Bool("result", res).

View File

@@ -1,6 +1,7 @@
package udp package udp
import ( import (
"bytes"
"crypto/hmac" "crypto/hmac"
"encoding/binary" "encoding/binary"
"fmt" "fmt"
@@ -12,8 +13,9 @@ import (
"time" "time"
"github.com/cespare/xxhash/v2" "github.com/cespare/xxhash/v2"
"github.com/sot-tech/mochi/pkg/log"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/sot-tech/mochi/pkg/log"
) )
var golden = []struct { var golden = []struct {
@@ -99,10 +101,13 @@ func TestReuseGeneratorGenerate(t *testing.T) {
gen := NewConnectionIDGenerator(tt.key, 0) gen := NewConnectionIDGenerator(tt.key, 0)
eq := true
for i := 0; i < 3; i++ { for i := 0; i < 3; i++ {
connID := gen.Generate(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0)) connID := gen.Generate(netip.MustParseAddr(tt.ip), time.Unix(tt.createdAt, 0))
require.NotEqual(t, cid, connID) // IDs should NOT be equal because of salt eq = eq && bytes.Equal(cid, connID)
} }
// at least one of generated IDs should NOT be equal because of salt. 3 attempts to check collisions
require.Equal(t, eq, false)
}) })
} }
} }