2021-04-28 20:10:44 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/cruxic/go-hmac-drbg/hmacdrbg"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-01-18 18:17:06 +00:00
|
|
|
"go.uber.org/zap"
|
2021-04-28 20:10:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var brHmacDrbgPool = sync.Pool{New: func() interface{} {
|
|
|
|
seed := make([]byte, 48)
|
|
|
|
_, err := rand.Read(seed)
|
|
|
|
if err != nil {
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Fatal("rand.Read err", zap.Error(err))
|
2021-04-28 20:10:44 +00:00
|
|
|
}
|
|
|
|
return hmacdrbg.NewHmacDrbg(256, seed, nil)
|
|
|
|
}}
|
|
|
|
|
2023-09-11 14:24:05 +00:00
|
|
|
// GenerateRequestID generates a random 32 byte slice that can be used for
|
2021-10-09 18:18:53 +00:00
|
|
|
// creating requests inf the filter, store and lightpush protocols
|
2023-09-11 14:24:05 +00:00
|
|
|
func GenerateRequestID() []byte {
|
2021-04-28 20:10:44 +00:00
|
|
|
rng := brHmacDrbgPool.Get().(*hmacdrbg.HmacDrbg)
|
|
|
|
defer brHmacDrbgPool.Put(rng)
|
|
|
|
|
|
|
|
randData := make([]byte, 32)
|
|
|
|
if !rng.Generate(randData) {
|
|
|
|
//Reseed is required every 10,000 calls
|
|
|
|
seed := make([]byte, 48)
|
|
|
|
_, err := rand.Read(seed)
|
|
|
|
if err != nil {
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Fatal("rand.Read err", zap.Error(err))
|
2021-04-28 20:10:44 +00:00
|
|
|
}
|
|
|
|
err = rng.Reseed(seed)
|
|
|
|
if err != nil {
|
|
|
|
//only happens if seed < security-level
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Fatal("rng.Reseed err", zap.Error(err))
|
2021-04-28 20:10:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if !rng.Generate(randData) {
|
2022-01-18 18:17:06 +00:00
|
|
|
utils.Logger().Error("could not generate random request id")
|
2021-04-28 20:10:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return randData
|
|
|
|
}
|