fix bug with concurrent math/rand.Rand.Read

firstly by using crypto/rand in those cases. and secondly by putting a lock
around the Read (though it isn't used at the moment).

found while working while implementing sending tls reports.
This commit is contained in:
Mechiel Lukkien
2023-11-09 17:15:46 +01:00
parent d02ac0cb86
commit 2535f351ed
7 changed files with 38 additions and 15 deletions

View File

@ -5,11 +5,24 @@ import (
"encoding/binary"
"fmt"
mathrand "math/rand"
"sync"
)
// NewRand returns a new PRNG seeded with random bytes from crypto/rand.
func NewRand() *mathrand.Rand {
return mathrand.New(mathrand.NewSource(CryptoRandInt()))
type rand struct {
*mathrand.Rand
sync.Mutex
}
// NewPseudoRand returns a new PRNG seeded with random bytes from crypto/rand.
func NewPseudoRand() *rand {
return &rand{Rand: mathrand.New(mathrand.NewSource(CryptoRandInt()))}
}
// Read can be called concurrently.
func (r *rand) Read(buf []byte) (int, error) {
r.Lock()
defer r.Unlock()
return r.Rand.Read(buf)
}
// CryptoRandInt returns a cryptographically random number.