Skip to content

Commit 1bd7657

Browse files
rolandshoemakergopherbot
authored andcommitted
[release-branch.go1.20] crypto/rand,runtime: switch RtlGenRandom for ProcessPrng
RtlGenRandom is a semi-undocumented API, also known as SystemFunction036, which we use to generate random data on Windows. It's definition, in cryptbase.dll, is an opaque wrapper for the documented API ProcessPrng. Instead of using RtlGenRandom, switch to using ProcessPrng, since the former is simply a wrapper for the latter, there should be no practical change on the user side, other than a minor change in the DLLs we load. Updates #53192 Fixes #64412 Change-Id: Ie6891bf97b1d47f5368cccbe92f374dba2c2672a Reviewed-on: https://go-review.googlesource.com/c/go/+/536235 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Quim Muntal <[email protected]> Auto-Submit: Roland Shoemaker <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> (cherry picked from commit 693def1) Reviewed-on: https://go-review.googlesource.com/c/go/+/545356 Auto-Submit: Dmitri Shuralyov <[email protected]>
1 parent 1b59b01 commit 1bd7657

File tree

5 files changed

+23
-32
lines changed

5 files changed

+23
-32
lines changed

src/crypto/rand/rand.go

+1-1
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 RtlGenRandom API.
18+
// On Windows systems, Reader uses the ProcessPrng API.
1919
// On Wasm, Reader uses the Web Crypto API.
2020
var Reader io.Reader
2121

src/crypto/rand/rand_windows.go

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

1616
type rngReader struct{}
1717

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 {
18+
func (r *rngReader) Read(b []byte) (int, error) {
19+
if err := windows.ProcessPrng(b); err != nil {
2320
return 0, err
2421
}
2522
return len(b), nil

src/internal/syscall/windows/syscall_windows.go

+1-1
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 RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
369+
//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng

src/internal/syscall/windows/zsyscall_windows.go

+11-10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/runtime/os_windows.go

+8-15
Original file line numberDiff line numberDiff line change
@@ -122,15 +122,8 @@ var (
122122
_LoadLibraryExW,
123123
_ stdFunction
124124

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
125+
// Use ProcessPrng to generate cryptographically random data.
126+
_ProcessPrng stdFunction
134127

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

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

266259
var ntdll = []byte("ntdll.dll\000")
267260
n32 := windowsLoadSystemLib(ntdll)
@@ -644,7 +637,7 @@ func initWine(k32 uintptr) {
644637
//go:nosplit
645638
func getRandomData(r []byte) {
646639
n := 0
647-
if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
640+
if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
648641
n = len(r)
649642
}
650643
extendRandom(r, n)

0 commit comments

Comments
 (0)