Skip to content

Commit

Permalink
Merge pull request quic-go#3046 from lucas-clemente/avoid-rand-source
Browse files Browse the repository at this point in the history
avoid using rand.Source
  • Loading branch information
marten-seemann authored Feb 18, 2021
2 parents c72f05a + 03fe636 commit bfad411
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 37 deletions.
15 changes: 4 additions & 11 deletions conn_id_manager.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package quic

import (
"crypto/rand"
"encoding/binary"
"fmt"
mrand "math/rand"

"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/qerr"
Expand All @@ -24,9 +21,9 @@ type connIDManager struct {
// We change the connection ID after sending on average
// protocol.PacketsPerConnectionID packets. The actual value is randomized
// hide the packet loss rate from on-path observers.
packetsSinceLastChange uint64
rand *mrand.Rand
packetsPerConnectionID uint64
rand utils.Rand
packetsSinceLastChange uint32
packetsPerConnectionID uint32

addStatelessResetToken func(protocol.StatelessResetToken)
removeStatelessResetToken func(protocol.StatelessResetToken)
Expand All @@ -39,15 +36,11 @@ func newConnIDManager(
removeStatelessResetToken func(protocol.StatelessResetToken),
queueControlFrame func(wire.Frame),
) *connIDManager {
b := make([]byte, 8)
_, _ = rand.Read(b) // ignore the error here. Nothing bad will happen if the seed is not perfectly random.
seed := int64(binary.BigEndian.Uint64(b))
return &connIDManager{
activeConnectionID: initialDestConnID,
addStatelessResetToken: addStatelessResetToken,
removeStatelessResetToken: removeStatelessResetToken,
queueControlFrame: queueControlFrame,
rand: mrand.New(mrand.NewSource(seed)),
}
}

Expand Down Expand Up @@ -155,7 +148,7 @@ func (h *connIDManager) updateConnectionID() {
h.activeConnectionID = front.ConnectionID
h.activeStatelessResetToken = &front.StatelessResetToken
h.packetsSinceLastChange = 0
h.packetsPerConnectionID = protocol.PacketsPerConnectionID/2 + uint64(h.rand.Int63n(protocol.PacketsPerConnectionID))
h.packetsPerConnectionID = protocol.PacketsPerConnectionID/2 + uint32(h.rand.Int31n(protocol.PacketsPerConnectionID))
h.addStatelessResetToken(*h.activeStatelessResetToken)
}

Expand Down
27 changes: 1 addition & 26 deletions internal/ackhandler/packet_number_generator.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package ackhandler

import (
"crypto/rand"
"encoding/binary"

"github.com/lucas-clemente/quic-go/internal/protocol"
"github.com/lucas-clemente/quic-go/internal/utils"
)
Expand Down Expand Up @@ -33,28 +30,6 @@ func (p *sequentialPacketNumberGenerator) Pop() protocol.PacketNumber {
return next
}

type rng struct {
buf [4]byte
}

func (r *rng) Int31() int32 {
rand.Read(r.buf[:])
return int32(binary.BigEndian.Uint32(r.buf[:]) & ^uint32(1<<31))
}

// copied from the standard library math/rand implementation of Int63n
func (r *rng) Int31n(n int32) int32 {
if n&(n-1) == 0 { // n is power of two, can mask
return r.Int31() & (n - 1)
}
max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
v := r.Int31()
for v > max {
v = r.Int31()
}
return v % n
}

// The skippingPacketNumberGenerator generates the packet number for the next packet
// it randomly skips a packet number every averagePeriod packets (on average).
// It is guaranteed to never skip two consecutive packet numbers.
Expand All @@ -65,7 +40,7 @@ type skippingPacketNumberGenerator struct {
next protocol.PacketNumber
nextToSkip protocol.PacketNumber

rng rng
rng utils.Rand
}

var _ packetNumberGenerator = &skippingPacketNumberGenerator{}
Expand Down
29 changes: 29 additions & 0 deletions internal/utils/rand.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package utils

import (
"crypto/rand"
"encoding/binary"
)

// Rand is a wrapper around crypto/rand that adds some convenience functions known from math/rand.
type Rand struct {
buf [4]byte
}

func (r *Rand) Int31() int32 {
rand.Read(r.buf[:])
return int32(binary.BigEndian.Uint32(r.buf[:]) & ^uint32(1<<31))
}

// copied from the standard library math/rand implementation of Int63n
func (r *Rand) Int31n(n int32) int32 {
if n&(n-1) == 0 { // n is power of two, can mask
return r.Int31() & (n - 1)
}
max := int32((1 << 31) - 1 - (1<<31)%uint32(n))
v := r.Int31()
for v > max {
v = r.Int31()
}
return v % n
}
32 changes: 32 additions & 0 deletions internal/utils/rand_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package utils

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("Rand", func() {
It("generates random numbers", func() {
const (
num = 1000
max = 123456
)

var values [num]int32
var r Rand
for i := 0; i < num; i++ {
v := r.Int31n(max)
Expect(v).To(And(
BeNumerically(">=", 0),
BeNumerically("<", max),
))
values[i] = v
}

var sum uint64
for _, n := range values {
sum += uint64(n)
}
Expect(float64(sum) / num).To(BeNumerically("~", max/2, max/25))
})
})

0 comments on commit bfad411

Please sign in to comment.