Merge pull request #3046 from lucas-clemente/avoid-rand-source

avoid using rand.Source
This commit is contained in:
Marten Seemann
2021-02-18 11:13:47 +08:00
committed by GitHub
4 changed files with 66 additions and 37 deletions

View File

@@ -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"
@@ -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)
@@ -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)),
}
}
@@ -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)
}

View File

@@ -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"
)
@@ -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.
@@ -65,7 +40,7 @@ type skippingPacketNumberGenerator struct {
next protocol.PacketNumber
nextToSkip protocol.PacketNumber
rng rng
rng utils.Rand
}
var _ packetNumberGenerator = &skippingPacketNumberGenerator{}

29
internal/utils/rand.go Normal file
View File

@@ -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
}

View File

@@ -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))
})
})