Skip to content

Commit

Permalink
crypto: use stdlib crypto/rand. ref #2099 (#2669)
Browse files Browse the repository at this point in the history
* crypto: use stdlib crypto/rand. ref #2099

* comment
  • Loading branch information
ebuchman committed Oct 19, 2018
1 parent 30519e8 commit 9d62bd0
Showing 1 changed file with 31 additions and 11 deletions.
42 changes: 31 additions & 11 deletions crypto/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"sync"

"golang.org/x/crypto/chacha20poly1305"

. "github.com/tendermint/tendermint/libs/common"
)

// NOTE: This is ignored for now until we have time
// to properly review the MixEntropy function - https://github.com/tendermint/tendermint/issues/2099.
//
// The randomness here is derived from xoring a chacha20 keystream with
// output from crypto/rand's OS Entropy Reader. (Due to fears of the OS'
// entropy being backdoored)
Expand All @@ -23,9 +24,13 @@ var gRandInfo *randInfo

func init() {
gRandInfo = &randInfo{}
gRandInfo.MixEntropy(randBytes(32)) // Init

// TODO: uncomment after reviewing MixEntropy -
// https://github.com/tendermint/tendermint/issues/2099
// gRandInfo.MixEntropy(randBytes(32)) // Init
}

// WARNING: This function needs review - https://github.com/tendermint/tendermint/issues/2099.
// Mix additional bytes of randomness, e.g. from hardware, user-input, etc.
// It is OK to call it multiple times. It does not diminish security.
func MixEntropy(seedBytes []byte) {
Expand All @@ -37,20 +42,28 @@ func randBytes(numBytes int) []byte {
b := make([]byte, numBytes)
_, err := crand.Read(b)
if err != nil {
PanicCrisis(err)
panic(err)
}
return b
}

// This only uses the OS's randomness
func CRandBytes(numBytes int) []byte {
return randBytes(numBytes)
}

/* TODO: uncomment after reviewing MixEntropy - https://github.com/tendermint/tendermint/issues/2099
// This uses the OS and the Seed(s).
func CRandBytes(numBytes int) []byte {
b := make([]byte, numBytes)
_, err := gRandInfo.Read(b)
if err != nil {
PanicCrisis(err)
}
return b
return randBytes(numBytes)
b := make([]byte, numBytes)
_, err := gRandInfo.Read(b)
if err != nil {
panic(err)
}
return b
}
*/

// CRandHex returns a hex encoded string that's floor(numDigits/2) * 2 long.
//
Expand All @@ -60,10 +73,17 @@ func CRandHex(numDigits int) string {
return hex.EncodeToString(CRandBytes(numDigits / 2))
}

// Returns a crand.Reader.
func CReader() io.Reader {
return crand.Reader
}

/* TODO: uncomment after reviewing MixEntropy - https://github.com/tendermint/tendermint/issues/2099
// Returns a crand.Reader mixed with user-supplied entropy
func CReader() io.Reader {
return gRandInfo
}
*/

//--------------------------------------------------------------------------------

Expand All @@ -75,7 +95,7 @@ type randInfo struct {
}

// You can call this as many times as you'd like.
// XXX TODO review
// XXX/TODO: review - https://github.com/tendermint/tendermint/issues/2099
func (ri *randInfo) MixEntropy(seedBytes []byte) {
ri.mtx.Lock()
defer ri.mtx.Unlock()
Expand Down

0 comments on commit 9d62bd0

Please sign in to comment.