wire: switch to crypto/rand for greased transport parameter generation (#3904)

This commit is contained in:
Marten Seemann 2023-06-20 18:31:12 +02:00 committed by GitHub
parent f57f876446
commit 28d5106a1c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,14 +2,13 @@ package wire
import ( import (
"bytes" "bytes"
"crypto/rand"
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"io" "io"
"math/rand"
"net" "net"
"sort" "sort"
"sync"
"time" "time"
"github.com/quic-go/quic-go/internal/protocol" "github.com/quic-go/quic-go/internal/protocol"
@ -26,15 +25,6 @@ var AdditionalTransportParametersClient map[uint64][]byte
const transportParameterMarshalingVersion = 1 const transportParameterMarshalingVersion = 1
var (
randomMutex sync.Mutex
random rand.Rand
)
func init() {
random = *rand.New(rand.NewSource(time.Now().UnixNano()))
}
type transportParameterID uint64 type transportParameterID uint64
const ( const (
@ -341,13 +331,12 @@ func (p *TransportParameters) Marshal(pers protocol.Perspective) []byte {
b := make([]byte, 0, 256) b := make([]byte, 0, 256)
// add a greased value // add a greased value
b = quicvarint.Append(b, uint64(27+31*rand.Intn(100))) random := make([]byte, 18)
randomMutex.Lock() rand.Read(random)
length := random.Intn(16) b = quicvarint.Append(b, 27+31*uint64(random[0]))
length := random[1] % 16
b = quicvarint.Append(b, uint64(length)) b = quicvarint.Append(b, uint64(length))
b = b[:len(b)+length] b = append(b, random[2:2+length]...)
random.Read(b[len(b)-length:])
randomMutex.Unlock()
// initial_max_stream_data_bidi_local // initial_max_stream_data_bidi_local
b = p.marshalVarintParam(b, initialMaxStreamDataBidiLocalParameterID, uint64(p.InitialMaxStreamDataBidiLocal)) b = p.marshalVarintParam(b, initialMaxStreamDataBidiLocalParameterID, uint64(p.InitialMaxStreamDataBidiLocal))