blob: 6c0655c72b692ae444a85025ecc3d27d65107404 [file] [log] [blame]
Brad Fitzpatrick51947442016-03-01 22:57:46 +00001// Copyright 2010 The Go Authors. All rights reserved.
Peter Mundyccd28e82010-07-12 16:37:53 -07002// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5// Windows cryptographically secure pseudorandom number
6// generator.
7
8package rand
9
10import (
Jason A. Donenfeldbb5075a2021-01-15 00:04:10 +010011 "internal/syscall/windows"
Peter Mundyccd28e82010-07-12 16:37:53 -070012)
13
Peter Mundyccd28e82010-07-12 16:37:53 -070014func init() { Reader = &rngReader{} }
15
Jason A. Donenfeld333e9042019-12-05 18:48:21 +010016type rngReader struct{}
Peter Mundyccd28e82010-07-12 16:37:53 -070017
Russ Coxc2049d22011-11-01 22:04:37 -040018func (r *rngReader) Read(b []byte) (n int, err error) {
Roland Shoemakerbb1f4412022-04-25 19:02:35 -070019 // 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 {
23 return 0, err
Adam Langleyb752ab22012-09-09 20:07:09 -040024 }
Roland Shoemakerbb1f4412022-04-25 19:02:35 -070025 return len(b), nil
Peter Mundyccd28e82010-07-12 16:37:53 -070026}