Skip to content

Commit f38b642

Browse files
committed
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 <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Tobias Klauser <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent f82ce7f commit f38b642

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

src/crypto/rand/rand_openbsd.go renamed to src/crypto/rand/rand_getentropy.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5+
//go:build darwin || openbsd
6+
// +build darwin openbsd
7+
58
package rand
69

710
import (
811
"internal/syscall/unix"
912
)
1013

1114
func init() {
12-
altGetRandom = getRandomOpenBSD
15+
altGetRandom = getEntropy
1316
}
1417

15-
func getRandomOpenBSD(p []byte) (ok bool) {
18+
func getEntropy(p []byte) (ok bool) {
1619
// getentropy(2) returns a maximum of 256 bytes per call
1720
for i := 0; i < len(p); i += 256 {
1821
end := i + 256
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
#include "textflag.h"
6+
7+
TEXT ·libc_getentropy_trampoline(SB),NOSPLIT,$0-0
8+
JMP libc_getentropy(SB)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2021 The Go Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
5+
package unix
6+
7+
import (
8+
"syscall"
9+
"unsafe"
10+
)
11+
12+
//go:cgo_import_dynamic libc_getentropy getentropy "/usr/lib/libSystem.B.dylib"
13+
14+
func libc_getentropy_trampoline()
15+
16+
// GetEntropy calls the macOS getentropy system call.
17+
func GetEntropy(p []byte) error {
18+
_, _, errno := syscall_syscall(funcPC(libc_getentropy_trampoline),
19+
uintptr(unsafe.Pointer(&p[0])),
20+
uintptr(len(p)),
21+
0)
22+
if errno != 0 {
23+
return errno
24+
}
25+
return nil
26+
}
27+
28+
//go:linkname syscall_syscall syscall.syscall
29+
func syscall_syscall(fn, a1, a2, a3 uintptr) (r1, r2 uintptr, err syscall.Errno)
30+
31+
//go:linkname funcPC runtime.funcPC
32+
func funcPC(f interface{}) uintptr

0 commit comments

Comments
 (0)