all: Use crypt/rand through its buffered version, but not in benchmarks (#7420)

This commit is contained in:
greatroar 2021-03-02 19:17:20 +01:00 committed by GitHub
parent 55d5e03639
commit 56b5352f64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 10 deletions

View File

@ -8,7 +8,6 @@ package protocol
import ( import (
"context" "context"
"crypto/rand"
"crypto/sha256" "crypto/sha256"
"encoding/base32" "encoding/base32"
"encoding/binary" "encoding/binary"
@ -20,6 +19,7 @@ import (
"github.com/gogo/protobuf/proto" "github.com/gogo/protobuf/proto"
"github.com/miscreant/miscreant.go" "github.com/miscreant/miscreant.go"
"github.com/syncthing/syncthing/lib/rand"
"golang.org/x/crypto/chacha20poly1305" "golang.org/x/crypto/chacha20poly1305"
"golang.org/x/crypto/hkdf" "golang.org/x/crypto/hkdf"
"golang.org/x/crypto/scrypt" "golang.org/x/crypto/scrypt"

View File

@ -9,13 +9,17 @@
package rand package rand
import ( import (
cryptoRand "crypto/rand" "io"
mathRand "math/rand" mathRand "math/rand"
"reflect" "reflect"
) )
// Reader is the standard crypto/rand.Reader, re-exported for convenience // Reader is the standard crypto/rand.Reader with added buffering.
var Reader = cryptoRand.Reader var Reader = defaultSecureSource
func Read(p []byte) (int, error) {
return io.ReadFull(defaultSecureSource, p)
}
// randomCharset contains the characters that can make up a rand.String(). // randomCharset contains the characters that can make up a rand.String().
const randomCharset = "2345679abcdefghijkmnopqrstuvwxyzACDEFGHJKLMNPQRSTUVWXYZ" const randomCharset = "2345679abcdefghijkmnopqrstuvwxyzACDEFGHJKLMNPQRSTUVWXYZ"

View File

@ -14,7 +14,7 @@ import (
"sync" "sync"
) )
// The secureSource is a math/rand.Source that reads bytes from // The secureSource is a math/rand.Source + io.Reader that reads bytes from
// crypto/rand.Reader. It means we can use the convenience functions // crypto/rand.Reader. It means we can use the convenience functions
// provided by math/rand.Rand on top of a secure source of numbers. It is // provided by math/rand.Rand on top of a secure source of numbers. It is
// concurrency safe for ease of use. // concurrency safe for ease of use.
@ -40,6 +40,12 @@ func (s *secureSource) Int63() int64 {
return int64(s.Uint64() & (1<<63 - 1)) return int64(s.Uint64() & (1<<63 - 1))
} }
func (s *secureSource) Read(p []byte) (int, error) {
s.mut.Lock()
defer s.mut.Unlock()
return s.rd.Read(p)
}
func (s *secureSource) Uint64() uint64 { func (s *secureSource) Uint64() uint64 {
var buf [8]byte var buf [8]byte

View File

@ -7,11 +7,11 @@
package sha256 package sha256
import ( import (
"crypto/rand"
cryptoSha256 "crypto/sha256" cryptoSha256 "crypto/sha256"
"encoding/hex" "encoding/hex"
"fmt" "fmt"
"hash" "hash"
"math/rand"
"os" "os"
"time" "time"
@ -111,7 +111,8 @@ func cpuBenchOnce(duration time.Duration, newFn func() hash.Hash) float64 {
chunkSize := 100 * 1 << 10 chunkSize := 100 * 1 << 10
h := newFn() h := newFn()
bs := make([]byte, chunkSize) bs := make([]byte, chunkSize)
rand.Reader.Read(bs) r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Read(bs)
t0 := time.Now() t0 := time.Now()
b := 0 b := 0

View File

@ -11,7 +11,6 @@ package signature
import ( import (
"crypto/ecdsa" "crypto/ecdsa"
"crypto/elliptic" "crypto/elliptic"
"crypto/rand"
"crypto/x509" "crypto/x509"
"encoding/asn1" "encoding/asn1"
"encoding/pem" "encoding/pem"
@ -20,6 +19,7 @@ import (
"io" "io"
"math/big" "math/big"
"github.com/syncthing/syncthing/lib/rand"
"github.com/syncthing/syncthing/lib/sha256" "github.com/syncthing/syncthing/lib/sha256"
) )

View File

@ -9,9 +9,9 @@ package ur
import ( import (
"bytes" "bytes"
"context" "context"
"crypto/rand"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"math/rand"
"net" "net"
"net/http" "net/http"
"runtime" "runtime"
@ -414,7 +414,8 @@ func CpuBench(ctx context.Context, iterations int, duration time.Duration, useWe
dataSize := 16 * protocol.MinBlockSize dataSize := 16 * protocol.MinBlockSize
bs := make([]byte, dataSize) bs := make([]byte, dataSize)
rand.Reader.Read(bs) r := rand.New(rand.NewSource(time.Now().UnixNano()))
r.Read(bs)
var perf float64 var perf float64
for i := 0; i < iterations; i++ { for i := 0; i < iterations; i++ {