From 9d62bd0ad3bf691002a815339b4dd675441bce93 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Fri, 19 Oct 2018 14:29:45 -0400 Subject: [PATCH] crypto: use stdlib crypto/rand. ref #2099 (#2669) * crypto: use stdlib crypto/rand. ref #2099 * comment --- crypto/random.go | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/crypto/random.go b/crypto/random.go index af328642..914c321b 100644 --- a/crypto/random.go +++ b/crypto/random.go @@ -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) @@ -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) { @@ -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. // @@ -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 } +*/ //-------------------------------------------------------------------------------- @@ -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()