Skip to content

Commit

Permalink
[release-branch.go1.20] crypto/rand,runtime: switch RtlGenRandom for …
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
rolandshoemaker authored and gopherbot committed Nov 28, 2023
1 parent 1b59b01 commit 1bd7657
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/crypto/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import "io"
// available, /dev/urandom otherwise.
// On OpenBSD and macOS, Reader uses getentropy(2).
// On other Unix-like systems, Reader reads from /dev/urandom.
// On Windows systems, Reader uses the RtlGenRandom API.
// On Windows systems, Reader uses the ProcessPrng API.
// On Wasm, Reader uses the Web Crypto API.
var Reader io.Reader

Expand Down
7 changes: 2 additions & 5 deletions src/crypto/rand/rand_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ func init() { Reader = &rngReader{} }

type rngReader struct{}

func (r *rngReader) Read(b []byte) (n int, err error) {
// RtlGenRandom only returns 1<<32-1 bytes at a time. We only read at
// most 1<<31-1 bytes at a time so that this works the same on 32-bit
// and 64-bit systems.
if err := batched(windows.RtlGenRandom, 1<<31-1)(b); err != nil {
func (r *rngReader) Read(b []byte) (int, error) {
if err := windows.ProcessPrng(b); err != nil {
return 0, err
}
return len(b), nil
Expand Down
2 changes: 1 addition & 1 deletion src/internal/syscall/windows/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -366,4 +366,4 @@ func LoadGetFinalPathNameByHandle() error {
//sys CreateEnvironmentBlock(block **uint16, token syscall.Token, inheritExisting bool) (err error) = userenv.CreateEnvironmentBlock
//sys DestroyEnvironmentBlock(block *uint16) (err error) = userenv.DestroyEnvironmentBlock

//sys RtlGenRandom(buf []byte) (err error) = advapi32.SystemFunction036
//sys ProcessPrng(buf []byte) (err error) = bcryptprimitives.ProcessPrng
21 changes: 11 additions & 10 deletions src/internal/syscall/windows/zsyscall_windows.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 8 additions & 15 deletions src/runtime/os_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,8 @@ var (
_LoadLibraryExW,
_ stdFunction

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

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

var advapi32dll = []byte("advapi32.dll\000")
a32 := windowsLoadSystemLib(advapi32dll)
if a32 == 0 {
throw("advapi32.dll not found")
var bcryptprimitivesdll = []byte("bcryptprimitives.dll\000")
bcryptPrimitives := windowsLoadSystemLib(bcryptprimitivesdll)
if bcryptPrimitives == 0 {
throw("bcryptprimitives.dll not found")
}
_RtlGenRandom = windowsFindfunc(a32, []byte("SystemFunction036\000"))
_ProcessPrng = windowsFindfunc(bcryptPrimitives, []byte("ProcessPrng\000"))

var ntdll = []byte("ntdll.dll\000")
n32 := windowsLoadSystemLib(ntdll)
Expand Down Expand Up @@ -644,7 +637,7 @@ func initWine(k32 uintptr) {
//go:nosplit
func getRandomData(r []byte) {
n := 0
if stdcall2(_RtlGenRandom, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
if stdcall2(_ProcessPrng, uintptr(unsafe.Pointer(&r[0])), uintptr(len(r)))&0xff != 0 {
n = len(r)
}
extendRandom(r, n)
Expand Down

0 comments on commit 1bd7657

Please sign in to comment.