Skip to content

Commit

Permalink
Merge branch 'master' into staging-client
Browse files Browse the repository at this point in the history
  • Loading branch information
rod-hynes committed Nov 13, 2018
2 parents 710dc9f + 0183b4a commit eed250f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
6 changes: 3 additions & 3 deletions psiphon/common/quic/obfuscator.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
MAX_OBFUSCATED_QUIC_IPV6_PACKET_SIZE = 1352
MAX_PADDING = 64
NONCE_SIZE = 12
RANDOM_STREAM_LIMIT = 1 << 38
RANDOM_STREAM_LIMIT = 1<<38 - 64
)

// ObfuscatedPacketConn wraps a QUIC net.PacketConn with an obfuscation layer
Expand Down Expand Up @@ -83,7 +83,7 @@ func (p *peerMode) isStale() bool {
return monotime.Since(p.lastPacketTime) >= SERVER_IDLE_TIMEOUT
}

// NewObfuscatedPacketConnPacketConn creates a new ObfuscatedPacketConn.
// NewObfuscatedPacketConn creates a new ObfuscatedPacketConn.
func NewObfuscatedPacketConn(
conn net.PacketConn,
isServer bool,
Expand Down Expand Up @@ -368,7 +368,7 @@ func (conn *ObfuscatedPacketConn) getRandomBytes(b []byte) error {

if conn.randomStreamCount+int64(len(b)) >= RANDOM_STREAM_LIMIT {

// Re-key before reaching the 2^38 chacha20 key stream limit.
// Re-key before reaching the 2^38-64 chacha20 key stream limit.

var randomStreamKey [32]byte
_, err := rand.Read(randomStreamKey[:])
Expand Down
25 changes: 21 additions & 4 deletions psiphon/common/quic/obfuscator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,36 @@ func Disabled_TestPaddingLenLimit(t *testing.T) {
t.Fatalf("NewObfuscatedPacketConn failed: %s", err)
}

// Use large blocks to get close to the key stream limit.

var b [2 * 1024 * 1024 * 1024]byte
n := int64(0)

for {
for i := 0; i < 127; i++ {
err := c.getRandomBytes(b[:])
if err != nil {
t.Fatalf("getRandomBytes failed: %s", err)
}
n += int64(len(b))
if n > (1<<38)+1 {
// We're past the chacha20 key stream limit.
break
}

// Stop using large blocks 64 bytes short of the limit, 2^38-64.

err = c.getRandomBytes(b[0 : len(b)-128])
if err != nil {
t.Fatalf("getRandomBytes failed: %s", err)
}
n += int64(len(b) - 128)

// Invoke byte at a time across the limit boundary to ensure we
// don't jump over the limit case.

for i := 0; i < 192; i++ {
err := c.getRandomBytes(b[0:1])
if err != nil {
t.Fatalf("getRandomBytes failed: %s", err)
}
n += int64(1)
}
}

Expand Down

0 comments on commit eed250f

Please sign in to comment.