diff options
| author | Filippo Valsorda <filippo@golang.org> | 2024-08-25 13:55:59 +0200 |
|---|---|---|
| committer | Filippo Valsorda <filippo@golang.org> | 2024-10-07 15:34:19 +0000 |
| commit | dd6b3821ca4ffec1ae074e60f5a990835b976662 (patch) | |
| tree | 7686ae6ee3e7a28e2fab0e6b202008381eaca3be /src/internal/syscall | |
| parent | 65679cfeb4b2fa0f24ac4ed8757b8a83ab0d5690 (diff) | |
| download | go-dd6b3821ca4ffec1ae074e60f5a990835b976662.tar.xz | |
crypto/rand: use arc4random_buf() on OpenBSD
OpenBSD system calls are mediated by libc anyway, and arc4random_buf()
is the preferred mechanism to obtain random bytes.
Also, rename NetBSD's function to reflect it's not actually calling
getentropy(3).
Cq-Include-Trybots: luci.golang.try:gotip-openbsd-amd64
Change-Id: Id1f3f7af16750537e2420bcf44b086de5854198c
Reviewed-on: https://go-review.googlesource.com/c/go/+/608395
Reviewed-by: Daniel McCarney <daniel@binaryparadox.net>
Reviewed-by: Michael Knyszek <mknyszek@google.com>
TryBot-Bypass: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Diffstat (limited to 'src/internal/syscall')
| -rw-r--r-- | src/internal/syscall/unix/arandom_netbsd.go (renamed from src/internal/syscall/unix/getentropy_netbsd.go) | 4 | ||||
| -rw-r--r-- | src/internal/syscall/unix/arc4random_openbsd.go | 23 | ||||
| -rw-r--r-- | src/internal/syscall/unix/asm_openbsd.s | 2 | ||||
| -rw-r--r-- | src/internal/syscall/unix/getentropy_openbsd.go | 18 | ||||
| -rw-r--r-- | src/internal/syscall/unix/getentropy_openbsd_mips64.go | 25 |
5 files changed, 26 insertions, 46 deletions
diff --git a/src/internal/syscall/unix/getentropy_netbsd.go b/src/internal/syscall/unix/arandom_netbsd.go index 02bac1be00..23ca8739e8 100644 --- a/src/internal/syscall/unix/getentropy_netbsd.go +++ b/src/internal/syscall/unix/arandom_netbsd.go @@ -2,8 +2,6 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build netbsd - package unix import ( @@ -17,7 +15,7 @@ const ( _KERN_ARND = 81 ) -func GetEntropy(p []byte) error { +func Arandom(p []byte) error { mib := [2]uint32{_CTL_KERN, _KERN_ARND} n := uintptr(len(p)) _, _, errno := syscall.Syscall6( diff --git a/src/internal/syscall/unix/arc4random_openbsd.go b/src/internal/syscall/unix/arc4random_openbsd.go new file mode 100644 index 0000000000..652e0cb19d --- /dev/null +++ b/src/internal/syscall/unix/arc4random_openbsd.go @@ -0,0 +1,23 @@ +// Copyright 2024 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 ( + "internal/abi" + "syscall" + "unsafe" +) + +//go:linkname syscall_syscall syscall.syscall +func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno) + +//go:cgo_import_dynamic libc_arc4random_buf arc4random_buf "libc.so" + +func libc_arc4random_buf_trampoline() + +func ARC4Random(p []byte) { + syscall_syscall(abi.FuncPCABI0(libc_arc4random_buf_trampoline), + uintptr(unsafe.Pointer(unsafe.SliceData(p))), uintptr(len(p)), 0) +} diff --git a/src/internal/syscall/unix/asm_openbsd.s b/src/internal/syscall/unix/asm_openbsd.s index cc54a14ca5..d6c4320539 100644 --- a/src/internal/syscall/unix/asm_openbsd.s +++ b/src/internal/syscall/unix/asm_openbsd.s @@ -8,3 +8,5 @@ TEXT ·libc_faccessat_trampoline(SB),NOSPLIT,$0-0 JMP libc_faccessat(SB) +TEXT ·libc_arc4random_buf_trampoline(SB),NOSPLIT,$0-0 + JMP libc_arc4random_buf(SB) diff --git a/src/internal/syscall/unix/getentropy_openbsd.go b/src/internal/syscall/unix/getentropy_openbsd.go deleted file mode 100644 index 7516ac7ce7..0000000000 --- a/src/internal/syscall/unix/getentropy_openbsd.go +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2022 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. - -//go:build openbsd && !mips64 - -package unix - -import _ "unsafe" // for linkname - -// GetEntropy calls the OpenBSD getentropy system call. -func GetEntropy(p []byte) error { - return getentropy(p) -} - -//go:linkname getentropy syscall.getentropy -//go:noescape -func getentropy(p []byte) error diff --git a/src/internal/syscall/unix/getentropy_openbsd_mips64.go b/src/internal/syscall/unix/getentropy_openbsd_mips64.go deleted file mode 100644 index d5caa8095a..0000000000 --- a/src/internal/syscall/unix/getentropy_openbsd_mips64.go +++ /dev/null @@ -1,25 +0,0 @@ -// 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 -} |
