Skip to content

Commit 77397ff

Browse files
rolandshoemakergopherbot
authored andcommitted
[release-branch.go1.20] crypto/rand,runtime: revert "switch RtlGenRandom for ProcessPrng"
This reverts CL 545356. Reason for revert: 1.20 still supports Windows versions before ProcessPrng was introduced. Change-Id: I224b8c4e7d0ca9ad5e733819b24dd92d14e61ab8 Reviewed-on: https://go-review.googlesource.com/c/go/+/545995 Reviewed-by: Dmitri Shuralyov <dmitshur@google.com> TryBot-Bypass: Dmitri Shuralyov <dmitshur@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org> Reviewed-by: Dmitri Shuralyov <dmitshur@golang.org> Auto-Submit: Dmitri Shuralyov <dmitshur@google.com>
1 parent d77307f commit 77397ff

File tree

5 files changed

+32
-23
lines changed

5 files changed

+32
-23
lines changed

src/crypto/rand/rand.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import "io"
1515
// available, /dev/urandom otherwise.
1616
// On OpenBSD and macOS, Reader uses getentropy(2).
1717
// On other Unix-like systems, Reader reads from /dev/urandom.
18-
// On Windows systems, Reader uses the ProcessPrng API.
18+
// On Windows systems, Reader uses the RtlGenRandom API.
1919
// On Wasm, Reader uses the Web Crypto API.
2020
var Reader io.Reader
2121

src/crypto/rand/rand_windows.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@ func init() { Reader = &rngReader{} }
1515

1616
type rngReader struct{}
1717

18-
func (r *rngReader) Read(b []byte) (int, error) {
19-
if err := windows.ProcessPrng(b); err != nil {
18+
func (r *rngReader) Read(b []byte) (n int, err error) {
19+
// RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
20+
// most 1<<31-1 bytes at a time so that this works the same on 32-bit
21+
// and 64-bit systems.
22+
if err := batched(windows.RtlGenRandom, 1<<31-1)(b); err != nil {
2023
return 0, err
2124
}
2225
return len(b), nil

src/internal/syscall/windows/syscall_windows.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,4 +366,4 @@ func LoadGetFinalPathNameByHandle() error {
366366
//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
367367
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
368368

369-
//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
369+
//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036

src/internal/syscall/windows/zsyscall_windows.go

Lines changed: 10 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/os_windows.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,15 @@ var (
122122
_LoadLibraryExW,
123123
_ stdFunction
124124

125-
// Use ProcessPrng to generate cryptographically random data.
126-
_ProcessPrng stdFunction
125+
// Use RtlGenRandom to generate cryptographically random data.
126+
// This approach has been recommended by Microsoft (see issue
127+
// 15589 for details).
128+
// The RtlGenRandom is not listed in advapi32.dll, instead
129+
// RtlGenRandom function can be found by searching for SystemFunction036.
130+
// Also some versions of Mingw cannot link to SystemFunction036
131+
// when building executable as Cgo. So load SystemFunction036
132+
// manually during runtime startup.
133+
_RtlGenRandom stdFunction
127134

128135
// Load ntdll.dll manually during startup, otherwise Mingw
129136
// links wrong printf function to cgo executable (see issue
@@ -249,12 +256,12 @@ func loadOptionalSyscalls() {
249256
_LoadLibraryExW = windowsFindfunc(k32, []byte("LoadLibraryExW\000"))
250257
useLoadLibraryEx = (_LoadLibraryExW != nil && _LoadLibraryExA != nil && _AddDllDirectory != nil)
251258

252-
var bcryptprimitivesdll = []byte("bcryptprimitives.dll\000")
253-
bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll)
254-
if bcryptPrimitives == 0 {
255-
throw("bcryptprimitives.dll not found")
259+
var advapi32dll = []byte("advapi32.dll\000")
260+
a32 := windowsLoadSystemLib(advapi32dll)
261+
if a32 == 0 {
262+
throw("advapi32.dll not found")
256263
}
257-
_ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
264+
_RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
258265

259266
var ntdll = []byte("ntdll.dll\000")
260267
n32 := windowsLoadSystemLib(ntdll)
@@ -637,7 +644,7 @@ func initWine(k32 uintptr) {
637644
//go:nosplit
638645
func getRandomData(r []byte) {
639646
n := 0
640-
if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
647+
if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
641648
n = len(r)
642649
}
643650
extendRandom(r, n)

0 commit comments

Comments
 (0)