aboutsummaryrefslogtreecommitdiff
path: root/src/internal/syscall
diff options
context:
space:
mode:
authorDavid Carlier <devnexen@gmail.com>2018-08-21 07:54:04 +0000
committerBrad Fitzpatrick <bradfitz@golang.org>2018-08-22 01:49:02 +0000
commit64f3d75bc288679e9e18cb9513897e7139f3fc3b (patch)
treed67f3064d7c3a7fcfa3df9fa5ec66f112d8b5786 /src/internal/syscall
parent45c7d80832dcb93239a1bd48ad7c8328ac6f0532 (diff)
downloadgo-64f3d75bc288679e9e18cb9513897e7139f3fc3b.tar.xz
crypto/rand: use the new getrandom syscall on FreeBSD
Since the 12.x branch, the getrandom syscall had been introduced with similar interface as Linux's and consistent syscall id across architectures. Change-Id: I63d6b45dbe9e29f07f1b5b6c2ec8be4fa624b9ee GitHub-Last-Rev: 6fb76e6522ef5ccb96d02445ffa39796dae89016 GitHub-Pull-Request: golang/go#25976 Reviewed-on: https://go-review.googlesource.com/120055 Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/internal/syscall')
-rw-r--r--src/internal/syscall/unix/getrandom_freebsd.go51
-rw-r--r--src/internal/syscall/unix/getrandom_linux_generic.go2
2 files changed, 52 insertions, 1 deletions
diff --git a/src/internal/syscall/unix/getrandom_freebsd.go b/src/internal/syscall/unix/getrandom_freebsd.go
new file mode 100644
index 0000000000..fc241f2345
--- /dev/null
+++ b/src/internal/syscall/unix/getrandom_freebsd.go
@@ -0,0 +1,51 @@
+// Copyright 2018 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 (
+ "sync/atomic"
+ "syscall"
+ "unsafe"
+)
+
+var randomUnsupported int32 // atomic
+
+// FreeBSD getrandom system call number.
+const randomTrap uintptr = 563
+
+// GetRandomFlag is a flag supported by the getrandom system call.
+type GetRandomFlag uintptr
+
+const (
+ // GRND_NONBLOCK means return EAGAIN rather than blocking.
+ GRND_NONBLOCK GetRandomFlag = 0x0001
+
+ // GRND_RANDOM is only set for portability purpose, no-op on FreeBSD.
+ GRND_RANDOM GetRandomFlag = 0x0002
+)
+
+// GetRandom calls the FreeBSD getrandom system call.
+func GetRandom(p []byte, flags GetRandomFlag) (n int, err error) {
+ if randomTrap == 0 {
+ return 0, syscall.ENOSYS
+ }
+ if len(p) == 0 {
+ return 0, nil
+ }
+ if atomic.LoadInt32(&randomUnsupported) != 0 {
+ return 0, syscall.ENOSYS
+ }
+ r1, _, errno := syscall.Syscall(randomTrap,
+ uintptr(unsafe.Pointer(&p[0])),
+ uintptr(len(p)),
+ uintptr(flags))
+ if errno != 0 {
+ if errno == syscall.ENOSYS {
+ atomic.StoreInt32(&randomUnsupported, 1)
+ }
+ return 0, errno
+ }
+ return int(r1), nil
+}
diff --git a/src/internal/syscall/unix/getrandom_linux_generic.go b/src/internal/syscall/unix/getrandom_linux_generic.go
index 8425800b6d..f8490ce978 100644
--- a/src/internal/syscall/unix/getrandom_linux_generic.go
+++ b/src/internal/syscall/unix/getrandom_linux_generic.go
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
-// +build arm64
+// +build linux,arm64
package unix