diff options
author | mixa108 <mixa108@yandex-team.com> | 2023-11-16 19:41:57 +0300 |
---|---|---|
committer | mixa108 <mixa108@yandex-team.com> | 2023-11-16 20:39:42 +0300 |
commit | 177b2aacb85a58acd19ca2e22d79c6030cd4c68b (patch) | |
tree | 948ffeb9b1aa2a741b4a89fbecdffee6460f1031 /contrib/go/_std_1.21/src/crypto/rand | |
parent | b098c151d43da3ece1440d49d69bf76061429aeb (diff) | |
download | ydb-177b2aacb85a58acd19ca2e22d79c6030cd4c68b.tar.gz |
bump go ver 1.21.3
bump go to 1.21.3
build go.conf
Golang 1.20.6 -> 1.21.3 init
Golang 1.20.6 -> 1.21.3: copy blame
Diffstat (limited to 'contrib/go/_std_1.21/src/crypto/rand')
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/rand.go | 45 | ||||
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/rand_getentropy.go | 14 | ||||
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/rand_getrandom.go | 48 | ||||
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/rand_unix.go | 87 | ||||
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/rand_windows.go | 26 | ||||
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/util.go | 99 | ||||
-rw-r--r-- | contrib/go/_std_1.21/src/crypto/rand/ya.make | 42 |
7 files changed, 361 insertions, 0 deletions
diff --git a/contrib/go/_std_1.21/src/crypto/rand/rand.go b/contrib/go/_std_1.21/src/crypto/rand/rand.go new file mode 100644 index 0000000000..62738e2cb1 --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/rand.go @@ -0,0 +1,45 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Package rand implements a cryptographically secure +// random number generator. +package rand + +import "io" + +// Reader is a global, shared instance of a cryptographically +// secure random number generator. +// +// On Linux, FreeBSD, Dragonfly, NetBSD and Solaris, Reader uses getrandom(2) if +// 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 JS/Wasm, Reader uses the Web Crypto API. +// On WASIP1/Wasm, Reader uses random_get from wasi_snapshot_preview1. +var Reader io.Reader + +// Read is a helper function that calls Reader.Read using io.ReadFull. +// On return, n == len(b) if and only if err == nil. +func Read(b []byte) (n int, err error) { + return io.ReadFull(Reader, b) +} + +// 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) error, readMax int) func([]byte) error { + return func(out []byte) error { + for len(out) > 0 { + read := len(out) + if read > readMax { + read = readMax + } + if err := f(out[:read]); err != nil { + return err + } + out = out[read:] + } + return nil + } +} diff --git a/contrib/go/_std_1.21/src/crypto/rand/rand_getentropy.go b/contrib/go/_std_1.21/src/crypto/rand/rand_getentropy.go new file mode 100644 index 0000000000..68f921b0fc --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/rand_getentropy.go @@ -0,0 +1,14 @@ +// Copyright 2016 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build (darwin && !ios) || openbsd + +package rand + +import "internal/syscall/unix" + +func init() { + // getentropy(2) returns a maximum of 256 bytes per call + altGetRandom = batched(unix.GetEntropy, 256) +} diff --git a/contrib/go/_std_1.21/src/crypto/rand/rand_getrandom.go b/contrib/go/_std_1.21/src/crypto/rand/rand_getrandom.go new file mode 100644 index 0000000000..46c4133a73 --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/rand_getrandom.go @@ -0,0 +1,48 @@ +// Copyright 2014 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build dragonfly || freebsd || linux || netbsd || solaris + +package rand + +import ( + "internal/syscall/unix" + "runtime" + "syscall" +) + +func init() { + var maxGetRandomRead int + switch runtime.GOOS { + case "linux", "android": + // Per the manpage: + // When reading from the urandom source, a maximum of 33554431 bytes + // is returned by a single call to getrandom() on systems where int + // has a size of 32 bits. + maxGetRandomRead = (1 << 25) - 1 + case "dragonfly", "freebsd", "illumos", "netbsd", "solaris": + maxGetRandomRead = 1 << 8 + default: + panic("no maximum specified for GetRandom") + } + altGetRandom = batched(getRandom, maxGetRandomRead) +} + +// 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 +// result so we only suffer the syscall overhead once in this case. +// 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 getRandom(p []byte) error { + n, err := unix.GetRandom(p, 0) + if err != nil { + return err + } + if n != len(p) { + return syscall.EIO + } + return nil +} diff --git a/contrib/go/_std_1.21/src/crypto/rand/rand_unix.go b/contrib/go/_std_1.21/src/crypto/rand/rand_unix.go new file mode 100644 index 0000000000..40fce36314 --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/rand_unix.go @@ -0,0 +1,87 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build unix + +// Unix cryptographically secure pseudorandom number +// generator. + +package rand + +import ( + "crypto/internal/boring" + "errors" + "io" + "os" + "sync" + "sync/atomic" + "syscall" + "time" +) + +const urandomDevice = "/dev/urandom" + +func init() { + if boring.Enabled { + Reader = boring.RandReader + return + } + Reader = &reader{} +} + +// A reader satisfies reads by reading from urandomDevice +type reader struct { + f io.Reader + mu sync.Mutex + used atomic.Uint32 // Atomic: 0 - never used, 1 - used, but f == nil, 2 - used, and f != nil +} + +// altGetRandom if non-nil specifies an OS-specific function to get +// urandom-style randomness. +var altGetRandom func([]byte) (err error) + +func warnBlocked() { + println("crypto/rand: blocked for 60 seconds waiting to read random data from the kernel") +} + +func (r *reader) Read(b []byte) (n int, err error) { + boring.Unreachable() + if r.used.CompareAndSwap(0, 1) { + // First use of randomness. Start timer to warn about + // being blocked on entropy not being available. + t := time.AfterFunc(time.Minute, warnBlocked) + defer t.Stop() + } + if altGetRandom != nil && altGetRandom(b) == nil { + return len(b), nil + } + if r.used.Load() != 2 { + r.mu.Lock() + if r.used.Load() != 2 { + f, err := os.Open(urandomDevice) + if err != nil { + r.mu.Unlock() + return 0, err + } + r.f = hideAgainReader{f} + r.used.Store(2) + } + r.mu.Unlock() + } + return io.ReadFull(r.f, b) +} + +// hideAgainReader masks EAGAIN reads from /dev/urandom. +// See golang.org/issue/9205 +type hideAgainReader struct { + r io.Reader +} + +func (hr hideAgainReader) Read(p []byte) (n int, err error) { + n, err = hr.r.Read(p) + if errors.Is(err, syscall.EAGAIN) { + err = nil + } + return +} diff --git a/contrib/go/_std_1.21/src/crypto/rand/rand_windows.go b/contrib/go/_std_1.21/src/crypto/rand/rand_windows.go new file mode 100644 index 0000000000..6c0655c72b --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/rand_windows.go @@ -0,0 +1,26 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// Windows cryptographically secure pseudorandom number +// generator. + +package rand + +import ( + "internal/syscall/windows" +) + +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 { + return 0, err + } + return len(b), nil +} diff --git a/contrib/go/_std_1.21/src/crypto/rand/util.go b/contrib/go/_std_1.21/src/crypto/rand/util.go new file mode 100644 index 0000000000..11b1a28ec5 --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/util.go @@ -0,0 +1,99 @@ +// Copyright 2011 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package rand + +import ( + "crypto/internal/randutil" + "errors" + "io" + "math/big" +) + +// Prime returns a number of the given bit length that is prime with high probability. +// Prime will return error for any error returned by rand.Read or if bits < 2. +func Prime(rand io.Reader, bits int) (*big.Int, error) { + if bits < 2 { + return nil, errors.New("crypto/rand: prime size must be at least 2-bit") + } + + randutil.MaybeReadByte(rand) + + b := uint(bits % 8) + if b == 0 { + b = 8 + } + + bytes := make([]byte, (bits+7)/8) + p := new(big.Int) + + for { + if _, err := io.ReadFull(rand, bytes); err != nil { + return nil, err + } + + // Clear bits in the first byte to make sure the candidate has a size <= bits. + bytes[0] &= uint8(int(1<<b) - 1) + // Don't let the value be too small, i.e, set the most significant two bits. + // Setting the top two bits, rather than just the top bit, + // means that when two of these values are multiplied together, + // the result isn't ever one bit short. + if b >= 2 { + bytes[0] |= 3 << (b - 2) + } else { + // Here b==1, because b cannot be zero. + bytes[0] |= 1 + if len(bytes) > 1 { + bytes[1] |= 0x80 + } + } + // Make the value odd since an even number this large certainly isn't prime. + bytes[len(bytes)-1] |= 1 + + p.SetBytes(bytes) + if p.ProbablyPrime(20) { + return p, nil + } + } +} + +// Int returns a uniform random value in [0, max). It panics if max <= 0. +func Int(rand io.Reader, max *big.Int) (n *big.Int, err error) { + if max.Sign() <= 0 { + panic("crypto/rand: argument to Int is <= 0") + } + n = new(big.Int) + n.Sub(max, n.SetUint64(1)) + // bitLen is the maximum bit length needed to encode a value < max. + bitLen := n.BitLen() + if bitLen == 0 { + // the only valid result is 0 + return + } + // k is the maximum byte length needed to encode a value < max. + k := (bitLen + 7) / 8 + // b is the number of bits in the most significant byte of max-1. + b := uint(bitLen % 8) + if b == 0 { + b = 8 + } + + bytes := make([]byte, k) + + for { + _, err = io.ReadFull(rand, bytes) + if err != nil { + return nil, err + } + + // Clear bits in the first byte to increase the probability + // that the candidate is < max. + bytes[0] &= uint8(int(1<<b) - 1) + + n.SetBytes(bytes) + if n.Cmp(max) < 0 { + return + } + } +} diff --git a/contrib/go/_std_1.21/src/crypto/rand/ya.make b/contrib/go/_std_1.21/src/crypto/rand/ya.make new file mode 100644 index 0000000000..d3eb5da2ba --- /dev/null +++ b/contrib/go/_std_1.21/src/crypto/rand/ya.make @@ -0,0 +1,42 @@ +GO_LIBRARY() + +SRCS( + rand.go + util.go +) + +GO_TEST_SRCS(rand_test.go) + +GO_XTEST_SRCS( + example_test.go + util_test.go +) + +IF (OS_LINUX) + SRCS( + rand_getrandom.go + rand_unix.go + ) + + GO_TEST_SRCS(rand_batched_test.go) +ENDIF() + +IF (OS_DARWIN) + SRCS( + rand_getentropy.go + rand_unix.go + ) + + GO_TEST_SRCS(rand_batched_test.go) +ENDIF() + +IF (OS_WINDOWS) + SRCS( + rand_windows.go + ) +ENDIF() + +END() + +RECURSE( +) |