Skip to content

Commit

Permalink
crypto/rand: use BCryptGenRandom instead of CryptGenRandom on Windows
Browse files Browse the repository at this point in the history
The existing function that is used is CryptGenRandom. This function
and the whole underling API is deprecated.

Use the function BCryptGenRandom from the new recommended
API called "Cryptography API: Next Generation (CNG)".

Fixes golang#33542
  • Loading branch information
neolit123 committed May 8, 2020
1 parent 4fda7dc commit 88f6563
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 46 deletions.
2 changes: 1 addition & 1 deletion src/crypto/rand/rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import "io"
// On Linux and FreeBSD, Reader uses getrandom(2) if available, /dev/urandom otherwise.
// On OpenBSD, Reader uses getentropy(2).
// On other Unix-like systems, Reader reads from /dev/urandom.
// On Windows systems, Reader uses the CryptGenRandom API.
// On Windows systems, Reader uses the BCryptGenRandom API.
// On Wasm, Reader uses the Web Crypto API.
var Reader io.Reader

Expand Down
39 changes: 5 additions & 34 deletions src/crypto/rand/rand_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,48 +9,19 @@ package rand

import (
"os"
"sync"
"sync/atomic"
"syscall"
"time"
)

// Implemented by using Windows CryptoAPI 2.0.
// Implemented by using the Cryptography Next Generation (CNG) API.

func init() { Reader = &rngReader{} }

// A rngReader satisfies reads by reading from the Windows CryptGenRandom API.
type rngReader struct {
used int32 // atomic; whether this rngReader has been used
prov syscall.Handle
mu sync.Mutex
}
type rngReader struct{}

func (r *rngReader) Read(b []byte) (n int, err error) {
if atomic.CompareAndSwapInt32(&r.used, 0, 1) {
// First use of randomness. Start timer to warn about
// being blocked on entropy not being available.
t := time.AfterFunc(60*time.Second, warnBlocked)
defer t.Stop()
}
r.mu.Lock()
if r.prov == 0 {
const provType = syscall.PROV_RSA_FULL
const flags = syscall.CRYPT_VERIFYCONTEXT | syscall.CRYPT_SILENT
err := syscall.CryptAcquireContext(&r.prov, nil, nil, provType, flags)
if err != nil {
r.mu.Unlock()
return 0, os.NewSyscallError("CryptAcquireContext", err)
}
}
r.mu.Unlock()

if len(b) == 0 {
return 0, nil
}
err = syscall.CryptGenRandom(r.prov, uint32(len(b)), &b[0])
err = syscall.BCryptGenRandom(nil, &b[0], uint32(len(b)), syscall.BCRYPT_USE_SYSTEM_PREFERRED_RNG)
if err != nil {
return 0, os.NewSyscallError("CryptGenRandom", err)
return 0, os.NewSyscallError("BCryptGenRandom", err)
}
return len(b), nil
return int(uint32(len(b))), nil
}
1 change: 1 addition & 0 deletions src/syscall/syscall_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ func NewCallbackCDecl(fn interface{}) uintptr {
//sys CryptAcquireContext(provhandle *Handle, container *uint16, provider *uint16, provtype uint32, flags uint32) (err error) = advapi32.CryptAcquireContextW
//sys CryptReleaseContext(provhandle Handle, flags uint32) (err error) = advapi32.CryptReleaseContext
//sys CryptGenRandom(provhandle Handle, buflen uint32, buf *byte) (err error) = advapi32.CryptGenRandom
//sys BCryptGenRandom(algorithmhandle Handle, buf *uint8, buflen uint32, flags uint32) (err error) = bcrypt.BCryptGenRandom
//sys GetEnvironmentStrings() (envs *uint16, err error) [failretval==nil] = kernel32.GetEnvironmentStringsW
//sys FreeEnvironmentStrings(envs *uint16) (err error) = kernel32.FreeEnvironmentStringsW
//sys GetEnvironmentVariable(name *uint16, buffer *uint16, size uint32) (n uint32, err error) = kernel32.GetEnvironmentVariableW
Expand Down
3 changes: 3 additions & 0 deletions src/syscall/types_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ const (
CRYPT_SILENT = 0x00000040
CRYPT_DEFAULT_CONTAINER_OPTIONAL = 0x00000080

// bcrypt.h
BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002

USAGE_MATCH_TYPE_AND = 0
USAGE_MATCH_TYPE_OR = 1

Expand Down
36 changes: 25 additions & 11 deletions src/syscall/zsyscall_windows.go

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

0 comments on commit 88f6563

Please sign in to comment.