go/doc/next/6-stdlib/99-minor/crypto
Filippo Valsorda 2b8dbb35b0 crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom
First, we centralize all random bytes generation through drbg.Read. The
rest of the FIPS 140-3 module can't use external functions anyway, so
drbg.Read needs to have all the logic.

Then, make sure that the crypto/... tree uses drbg.Read (or the new
crypto/internal/rand.Reader wrapper) instead of crypto/rand, so it is
unaffected by applications setting crypto/rand.Reader.

Next, pass all unspecified random io.Reader parameters through the new
crypto/internal/rand.CustomReader, which just redirects to drbg.Read
unless GODEBUG=cryptocustomrand=1 is set. Move all the calls to
MaybeReadByte there, since it's only needed for these custom Readers.

Finally, add testing/cryptotest.SetGlobalRandom which sets
crypto/rand.Reader to a locked deterministic source and overrides
drbg.Read. This way SetGlobalRandom should affect all cryptographic
randomness in the standard library.

Fixes #70942

Co-authored-by: qiulaidongfeng <2645477756@qq.com>
Change-Id: I6a6a69641311d9fac318abcc6d79677f0e406100
Reviewed-on: https://go-review.googlesource.com/c/go/+/724480
Reviewed-by: Nicholas Husin <nsh@golang.org>
Auto-Submit: Filippo Valsorda <filippo@golang.org>
Reviewed-by: Nicholas Husin <husin@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
2025-11-26 16:01:17 -08:00
..
dsa crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom 2025-11-26 16:01:17 -08:00
ecdh crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom 2025-11-26 16:01:17 -08:00
ecdsa crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom 2025-11-26 16:01:17 -08:00
ed25519 crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom 2025-11-26 16:01:17 -08:00
fips140 crypto/fips140: add WithoutEnforcement 2025-11-26 14:26:06 -08:00
hpke crypto/hpke: expose crypto/internal/hpke 2025-11-25 10:35:07 -08:00
mlkem crypto: add Encapsulator and Decapsulator interfaces 2025-11-19 14:14:13 -08:00
rand crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom 2025-11-26 16:01:17 -08:00
rsa crypto,testing/cryptotest: ignore random io.Reader params, add SetGlobalRandom 2025-11-26 16:01:17 -08:00
tls crypto/tls: support crypto.MessageSigner private keys 2025-11-26 15:17:42 -08:00
x509 crypto/x509: add ExtKeyUsage.OID method 2025-11-26 14:21:13 -08:00
75300.md crypto: add Encapsulator and Decapsulator interfaces 2025-11-19 14:14:13 -08:00