]> Cypherpunks repositories - gostls13.git/commitdiff
crypto/rand, internal/syscall/unix: add support for getentropy syscall on darwin
authorTobias Klauser <tklauser@distanz.ch>
Wed, 17 Mar 2021 10:17:02 +0000 (11:17 +0100)
committerTobias Klauser <tobias.klauser@gmail.com>
Wed, 17 Mar 2021 22:14:28 +0000 (22:14 +0000)
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>

src/crypto/rand/rand_getentropy.go [moved from src/crypto/rand/rand_openbsd.go with 79% similarity]
src/internal/syscall/unix/asm_darwin.s [new file with mode: 0644]
src/internal/syscall/unix/getentropy_darwin.go [new file with mode: 0644]

similarity index 79%
rename from src/crypto/rand/rand_openbsd.go
rename to src/crypto/rand/rand_getentropy.go
index 9cc39f72d1d9b221563ab37717dd1931daab0f28..f82018a4951c743c82f15c28b82262b601112f27 100644 (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
diff --git a/src/internal/syscall/unix/asm_darwin.s b/src/internal/syscall/unix/asm_darwin.s
new file mode 100644 (file)
index 0000000..8fbdc1d
--- /dev/null
@@ -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)
diff --git a/src/internal/syscall/unix/getentropy_darwin.go b/src/internal/syscall/unix/getentropy_darwin.go
new file mode 100644 (file)
index 0000000..6da6f84
--- /dev/null
@@ -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