Skip to content

Commit 693def1

Browse files
rolandshoemakergopherbot
authored andcommitted
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. 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]>
1 parent 3de6033 commit 693def1

File tree

5 files changed

+27
-36
lines changed

5 files changed

+27
-36
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 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

Lines changed: 2 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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
type FILE_ID_BOTH_DIR_INFO struct {
379379
NextEntryOffset uint32

src/internal/syscall/windows/zsyscall_windows.go

Lines changed: 11 additions & 10 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: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,8 @@ var (
127127
_WriteFile,
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
@@ -151,11 +144,11 @@ var (
151144
)
152145

153146
var (
154-
advapi32dll = [...]uint16{'a', 'd', 'v', 'a', 'p', 'i', '3', '2', '.', 'd', 'l', 'l', 0}
155-
ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
156-
powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
157-
winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
158-
ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
147+
bcryptprimitivesdll = [...]uint16{'b', 'c', 'r', 'y', 'p', 't', 'p', 'r', 'i', 'm', 'i', 't', 'i', 'v', 'e', 's', '.', 'd', 'l', 'l', 0}
148+
ntdlldll = [...]uint16{'n', 't', 'd', 'l', 'l', '.', 'd', 'l', 'l', 0}
149+
powrprofdll = [...]uint16{'p', 'o', 'w', 'r', 'p', 'r', 'o', 'f', '.', 'd', 'l', 'l', 0}
150+
winmmdll = [...]uint16{'w', 'i', 'n', 'm', 'm', '.', 'd', 'l', 'l', 0}
151+
ws2_32dll = [...]uint16{'w', 's', '2', '_', '3', '2', '.', 'd', 'l', 'l', 0}
159152
)
160153

161154
// Function to be called by windows CreateThread
@@ -251,11 +244,11 @@ func windowsLoadSystemLib(name []uint16) uintptr {
251244
}
252245

253246
func loadOptionalSyscalls() {
254-
a32 := windowsLoadSystemLib(advapi32dll[:])
255-
if a32 == 0 {
256-
throw("advapi32.dll not found")
247+
bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll[:])
248+
if bcryptPrimitives == 0 {
249+
throw("bcryptprimitives.dll not found")
257250
}
258-
_RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
251+
_ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))
259252

260253
n32 := windowsLoadSystemLib(ntdlldll[:])
261254
if n32 == 0 {
@@ -528,7 +521,7 @@ func osinit() {
528521
//go:nosplit
529522
func getRandomData(r []byte) {
530523
n := 0
531-
if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
524+
if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
532525
n = len(r)
533526
}
534527
extendRandom(r, n)

0 commit comments

Comments
 (0)