Commit ddfe7b3d authored by Michael McConville's avatar Michael McConville Committed by Brad Fitzpatrick

crypto/rand: use the getentropy syscall on OpenBSD

Go already supports Linux's getrandom, which is a slightly modified
version of getentropy.

getentropy was added in OpenBSD 5.6. All supported versions of OpenBSD
include it so, unlike with Linux and getrandom, we don't need to test
for its presence.

Fixes #13785.

Change-Id: Ib536b96675f257cd8c5de1e3a36165e15c9abac9
Reviewed-on: https://go-review.googlesource.com/18219
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: 's avatarBrad Fitzpatrick <bradfitz@golang.org>
parent ee177279
......@@ -11,8 +11,9 @@ import "io"
// Reader is a global, shared instance of a cryptographically
// strong pseudo-random generator.
//
// On Unix-like systems, Reader reads from /dev/urandom.
// On Linux, Reader uses getrandom(2) if available, /dev/urandom otherwise.
// On OpenBSD, Reader uses getentropy(2).
// On other Unix-like systems, Reader reads from /dev/urandom.
// On Windows systems, Reader uses the CryptGenRandom API.
var Reader io.Reader
......
// Copyright 2016 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 (
"internal/syscall/unix"
)
func init() {
altGetRandom = getRandomOpenBSD
}
func getRandomOpenBSD(p []byte) (ok bool) {
// getentropy(2) returns a maximum of 256 bytes per call
for i := 0; i < len(p); i += 256 {
end := i + 256
if len(p) < end {
end = len(p)
}
err := unix.GetEntropy(p[i:end])
if err != nil {
return false
}
}
return true
}
// Copyright 2016 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 unix
import (
"syscall"
"unsafe"
)
// getentropy(2)'s syscall number, from /usr/src/sys/kern/syscalls.master
const entropyTrap uintptr = 7
// GetEntropy calls the OpenBSD getentropy system call.
func GetEntropy(p []byte) error {
_, _, errno := syscall.Syscall(entropyTrap,
uintptr(unsafe.Pointer(&p[0])),
uintptr(len(p)),
0)
if errno != 0 {
return errno
}
return nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment