Retro68/gcc/libgo/go/crypto/rand/rand_linux.go

40 lines
886 B
Go
Raw Normal View History

2015-08-28 15:33:40 +00:00
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package rand
import (
2017-04-10 11:32:00 +00:00
"internal/syscall/unix"
2015-08-28 15:33:40 +00:00
"sync"
)
func init() {
altGetRandom = getRandomLinux
}
var (
once sync.Once
useSyscall bool
)
func pickStrategy() {
// Test whether we should use the system call or /dev/urandom.
// We'll fall back to urandom if:
// - the kernel is too old (before 3.17)
// - the machine has no entropy available (early boot + no hardware
// entropy source?) and we want to avoid blocking later.
var buf [1]byte
2017-04-10 11:32:00 +00:00
n, err := unix.GetRandom(buf[:], unix.GRND_NONBLOCK)
2015-08-28 15:33:40 +00:00
useSyscall = n == 1 && err == nil
}
func getRandomLinux(p []byte) (ok bool) {
once.Do(pickStrategy)
if !useSyscall {
return false
}
2017-04-10 11:32:00 +00:00
n, err := unix.GetRandom(p, 0)
2015-08-28 15:33:40 +00:00
return n == len(p) && err == nil
}