github.com/safing/portbase@v0.19.5/rng/osfeeder.go (about) 1 package rng 2 3 import ( 4 "context" 5 "crypto/rand" 6 "fmt" 7 ) 8 9 func osFeeder(ctx context.Context) error { 10 entropyBytes := minFeedEntropy / 8 11 feeder := NewFeeder() 12 defer feeder.CloseFeeder() 13 14 for { 15 // gather 16 osEntropy := make([]byte, entropyBytes) 17 n, err := rand.Read(osEntropy) 18 if err != nil { 19 return fmt.Errorf("could not read entropy from os: %w", err) 20 } 21 if n != entropyBytes { 22 return fmt.Errorf("could not read enough entropy from os: got only %d bytes instead of %d", n, entropyBytes) 23 } 24 25 // feed 26 select { 27 case feeder.input <- &entropyData{ 28 data: osEntropy, 29 entropy: entropyBytes * 8, 30 }: 31 case <-ctx.Done(): 32 return nil 33 } 34 } 35 }