Skip to content

Commit 9e43850

Browse files
rolandshoemakergopherbot
authored andcommitted
[release-branch.go1.21] 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 #64413 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/+/545355 Auto-Submit: Dmitri Shuralyov <[email protected]>
1 parent 8caf4bb commit 9e43850

File tree

5 files changed

+28
-37
lines changed

5 files changed

+28
-37
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 JS/Wasm, Reader uses the Web Crypto API.
2020
// On WASIP1/Wasm, Reader uses random_get from wasi_snapshot_preview1.
2121
var Reader io.Reader

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
@@ -373,7 +373,7 @@ func ErrorLoadingGetTempPath2() error {
373373
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock
374374
//sys CreateEvent(eventAttrs *SecurityAttributes, manualReset uint32, initialState uint32, name *uint16) (handle syscall.Handle, err error) = kernel32.CreateEventW
375375

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

378378
//sys RtlLookupFunctionEntry(pc uintptr, baseAddress *uintptr, table *byte) (ret uintptr) = kernel32.RtlLookupFunctionEntry
379379
//sys RtlVirtualUnwind(handlerType uint32, baseAddress uintptr, pc uintptr, entry uintptr, ctxt uintptr, data *uintptr, frame *uintptr, ctxptrs *byte) (ret uintptr) = kernel32.RtlVirtualUnwind

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

+13-20
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,8 @@ var (
127127
_AddVectoredContinueHandler,
128128
_ stdFunction
129129

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

140133
// Load ntdll.dll manually during startup, otherwise Mingw
141134
// links wrong printf function to cgo executable (see issue
@@ -152,12 +145,12 @@ var (
152145
)
153146

154147
var (
155-
advapi32dll = [...]uint16{'a', 'd', 'v', 'a', 'p', 'i', '3', '2', '.', 'd', 'l', 'l', 0}
156-
kernel32dll = [...]uint16{'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0}
157-
ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
158-
powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
159-
winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
160-
ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
148+
bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
149+
kernel32dll = [...]uint16{'k', 'e', 'r', 'n', 'e', 'l', '3', '2', '.', 'd', 'l', 'l', 0}
150+
ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
151+
powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
152+
winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
153+
ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
161154
)
162155

163156
// Function to be called by windows CreateThread
@@ -256,11 +249,11 @@ func loadOptionalSyscalls() {
256249
}
257250
_AddVectoredContinueHandler = windowsFindfunc(k32, []byte("AddVectoredContinueHandler\000"))
258251

259-
a32 := windowsLoadSystemLib(advapi32dll[:])
260-
if a32 == 0 {
261-
throw("advapi32.dll not found")
252+
bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
253+
if bcryptPrimitives == 0 {
254+
throw("bcryptprimitives.dll not found")
262255
}
263-
_RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
256+
_ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
264257

265258
n32 := windowsLoadSystemLib(ntdlldll[:])
266259
if n32 == 0 {
@@ -617,7 +610,7 @@ func initWine(k32 uintptr) {
617610
//go:nosplit
618611
func getRandomData(r []byte) {
619612
n := 0
620-
if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
613+
if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
621614
n = len(r)
622615
}
623616
extendRandom(r, n)

0 commit comments

Comments
 (0)