File tree 2 files changed +38
-10
lines changed
src/crypto/internal/fips140/drbg 2 files changed +38
-10
lines changed Original file line number Diff line number Diff line change @@ -13,8 +13,15 @@ import (
13
13
"sync"
14
14
)
15
15
16
- var mu sync.Mutex
17
- var drbg * Counter
16
+ var drbgs = sync.Pool {
17
+ New : func () any {
18
+ var c * Counter
19
+ entropy .Depleted (func (seed * [48 ]byte ) {
20
+ c = NewCounter (seed )
21
+ })
22
+ return c
23
+ },
24
+ }
18
25
19
26
// Read fills b with cryptographically secure random bytes. In FIPS mode, it
20
27
// uses an SP 800-90A Rev. 1 Deterministic Random Bit Generator (DRBG).
@@ -33,14 +40,8 @@ func Read(b []byte) {
33
40
additionalInput := new ([SeedSize ]byte )
34
41
sysrand .Read (additionalInput [:16 ])
35
42
36
- mu .Lock ()
37
- defer mu .Unlock ()
38
-
39
- if drbg == nil {
40
- entropy .Depleted (func (seed * [48 ]byte ) {
41
- drbg = NewCounter (seed )
42
- })
43
- }
43
+ drbg := drbgs .Get ().(* Counter )
44
+ defer drbgs .Put (drbg )
44
45
45
46
for len (b ) > 0 {
46
47
size := min (len (b ), maxRequestSize )
Original file line number Diff line number Diff line change
1
+ // Copyright 2025 The Go Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package drbg
6
+
7
+ import (
8
+ "crypto/internal/fips140"
9
+ "testing"
10
+ )
11
+
12
+ func BenchmarkDBRG (b * testing.B ) {
13
+ old := fips140 .Enabled
14
+ defer func () {
15
+ fips140 .Enabled = old
16
+ }()
17
+ fips140 .Enabled = true
18
+
19
+ const N = 64
20
+ b .SetBytes (N )
21
+ b .RunParallel (func (pb * testing.PB ) {
22
+ buf := make ([]byte , N )
23
+ for pb .Next () {
24
+ Read (buf )
25
+ }
26
+ })
27
+ }
You can’t perform that action at this time.
0 commit comments