diff options
author | Dmitri Shuralyov <dmitshur@google.com> | 2022-06-01 17:42:39 +0000 |
---|---|---|
committer | Dmitri Shuralyov <dmitshur@google.com> | 2022-06-01 17:42:39 +0000 |
commit | 4658e6e324a85b0076f66acdab77799ffa9ac7bb (patch) | |
tree | f3b4de12074d583057060fdf35d91b6591859644 /src/crypto/rand/rand_batched.go | |
parent | 6b07de790c08967d0dbe3a36ec86f3d13e1cfcbd (diff) | |
parent | 26cdea3acca29db94541236f0037a20aa22ce2d7 (diff) | |
download | go-4658e6e324a85b0076f66acdab77799ffa9ac7bb.tar.gz go-4658e6e324a85b0076f66acdab77799ffa9ac7bb.zip |
[dev.boringcrypto.go1.17] all: merge go1.17.11 into dev.boringcrypto.go1.17
Change-Id: I563433b2d02a5abea610a1561139d0980d5c2102
Diffstat (limited to 'src/crypto/rand/rand_batched.go')
-rw-r--r-- | src/crypto/rand/rand_batched.go | 22 |
1 files changed, 6 insertions, 16 deletions
diff --git a/src/crypto/rand/rand_batched.go b/src/crypto/rand/rand_batched.go index d7c5bf3562..8df715fdd1 100644 --- a/src/crypto/rand/rand_batched.go +++ b/src/crypto/rand/rand_batched.go @@ -8,6 +8,7 @@ package rand import ( + "errors" "internal/syscall/unix" ) @@ -16,20 +17,6 @@ func init() { altGetRandom = batched(getRandomBatch, maxGetRandomRead) } -// batched returns a function that calls f to populate a []byte by chunking it -// into subslices of, at most, readMax bytes. -func batched(f func([]byte) bool, readMax int) func([]byte) bool { - return func(buf []byte) bool { - for len(buf) > readMax { - if !f(buf[:readMax]) { - return false - } - buf = buf[readMax:] - } - return len(buf) == 0 || f(buf) - } -} - // If the kernel is too old to support the getrandom syscall(), // unix.GetRandom will immediately return ENOSYS and we will then fall back to // reading from /dev/urandom in rand_unix.go. unix.GetRandom caches the ENOSYS @@ -37,7 +24,10 @@ func batched(f func([]byte) bool, readMax int) func([]byte) bool { // If the kernel supports the getrandom() syscall, unix.GetRandom will block // until the kernel has sufficient randomness (as we don't use GRND_NONBLOCK). // In this case, unix.GetRandom will not return an error. -func getRandomBatch(p []byte) (ok bool) { +func getRandomBatch(p []byte) error { n, err := unix.GetRandom(p, 0) - return n == len(p) && err == nil + if n != len(p) { + return errors.New("short read") + } + return err } |