1
0
mirror of https://github.com/golang/go synced 2024-11-11 22:40:22 -07:00

crypto/rand, internal/syscall/unix: add support for getentropy syscall on darwin

The getentropy syscall is available on macOS since version 10.12, which
is the minimum required version since Go 1.15.

Change-Id: I294259af0b11df9669e4dc5fa891d2f2f039d91a
Reviewed-on: https://go-review.googlesource.com/c/go/+/302489
Trust: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
Tobias Klauser 2021-03-17 11:17:02 +01:00 committed by Tobias Klauser
parent f82ce7fb23
commit f38b6428a2
3 changed files with 45 additions and 2 deletions

View File

@ -2,6 +2,9 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//go:build darwin || openbsd
// +build darwin openbsd
package rand
import (
@ -9,10 +12,10 @@ import (
)
func init() {
altGetRandom = getRandomOpenBSD
altGetRandom = getEntropy
}
func getRandomOpenBSD(p []byte) (ok bool) {
func getEntropy(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

View File

@ -0,0 +1,8 @@
// Copyright 2021 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.
#include "textflag.h"
TEXT ·libc_getentropy_trampoline(SB),NOSPLIT,$0-0
JMP libc_getentropy(SB)

View File

@ -0,0 +1,32 @@
// Copyright 2021 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"
)
//go:cgo_import_dynamic libc_getentropy getentropy "/usr/lib/libSystem.B.dylib"
func libc_getentropy_trampoline()
// GetEntropy calls the macOS getentropy system call.
func GetEntropy(p []byte) error {
_, _, errno := syscall_syscall(funcPC(libc_getentropy_trampoline),
uintptr(unsafe.Pointer(&p[0])),
uintptr(len(p)),
0)
if errno != 0 {
return errno
}
return nil
}
//go:linkname syscall_syscall syscall.syscall
func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
//go:linkname funcPC runtime.funcPC
func funcPC(f interface{}) uintptr