Skip to content

Commit f6b9d56

Browse files
FiloSottilegopherbot
authored andcommitted
crypto/internal/fips140/entropy: fix benign race
Fixes #75690 Fixes #75842 Change-Id: I6a6a696420f51f28f48535c34cf347e2cbd4add5 Reviewed-on: https://go-review.googlesource.com/c/go/+/710058 Auto-Submit: Filippo Valsorda <filippo@golang.org> Reviewed-by: David Chase <drchase@google.com> Reviewed-by: Roland Shoemaker <roland@golang.org> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 60f6d2f commit f6b9d56

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/crypto/internal/fips140/entropy/entropy.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,9 @@ func (s *source) Sample() uint8 {
123123
// Perform a few memory accesses in an unpredictable pattern to expose the
124124
// next measurement to as much system noise as possible.
125125
memory, lcgState := s.memory, s.lcgState
126-
_ = memory[0] // hoist the nil check out of touchMemory
126+
if memory == nil { // remove the nil check from the inlined touchMemory calls
127+
panic("entropy: nil memory buffer")
128+
}
127129
for range 64 {
128130
lcgState = 1664525*lcgState + 1013904223
129131
// Discard the lower bits, which tend to fall into short cycles.

src/crypto/internal/fips140test/entropy_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,20 +241,20 @@ func TestEntropyUnchanged(t *testing.T) {
241241
// entropy source through the Entropy Source Validation program,
242242
// independently of the FIPS 140-3 module. It must not change even across
243243
// FIPS 140-3 module versions, in order to reuse the ESV certificate.
244-
exp := "1b68d4c091ef66c6006602e4ed3ac10f8a82ad193708ec99d63b145e3baa3e6c"
244+
exp := "2541273241ae8aafe55026328354ed3799df1e2fb308b2097833203a42911b53"
245245
if got := hex.EncodeToString(h.Sum(nil)); got != exp {
246246
t.Errorf("hash of crypto/internal/fips140/entropy = %s, want %s", got, exp)
247247
}
248248
}
249249

250250
func TestEntropyRace(t *testing.T) {
251251
// Check that concurrent calls to Seed don't trigger the race detector.
252-
for range 2 {
252+
for range 16 {
253253
go func() {
254254
_, _ = entropy.Seed(&memory)
255255
}()
256256
}
257-
// Same, with the higher-level DRBG. More concurrent calls to hit the Pool.
257+
// Same, with the higher-level DRBG.
258258
for range 16 {
259259
go func() {
260260
var b [64]byte

0 commit comments

Comments
 (0)