aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/randentropy/rand_entropy.go
diff options
context:
space:
mode:
authorGustav Simonsson <gustav.simonsson@gmail.com>2015-04-02 23:02:56 +0800
committerGustav Simonsson <gustav.simonsson@gmail.com>2015-05-12 23:18:30 +0800
commit9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98 (patch)
tree0da6e8cf6523c1d8667f0a68be648b875039806a /crypto/randentropy/rand_entropy.go
parent58d6ec689ff44232cd5d6a7cbbaad2d7a2cb44bd (diff)
downloaddexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.tar
dexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.tar.gz
dexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.tar.bz2
dexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.tar.lz
dexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.tar.xz
dexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.tar.zst
dexon-9918b6c84e2547f3d24a6cfeb97cfcbd6cb4dc98.zip
Remove the awesome, ever misunderstood entropy mixing
Diffstat (limited to 'crypto/randentropy/rand_entropy.go')
-rw-r--r--crypto/randentropy/rand_entropy.go51
1 files changed, 1 insertions, 50 deletions
diff --git a/crypto/randentropy/rand_entropy.go b/crypto/randentropy/rand_entropy.go
index b87fa564e..68bb8808b 100644
--- a/crypto/randentropy/rand_entropy.go
+++ b/crypto/randentropy/rand_entropy.go
@@ -2,12 +2,8 @@ package randentropy
import (
crand "crypto/rand"
- "encoding/binary"
"github.com/ethereum/go-ethereum/crypto/sha3"
"io"
- "os"
- "strings"
- "time"
)
var Reader io.Reader = &randEntropy{}
@@ -16,7 +12,7 @@ type randEntropy struct {
}
func (*randEntropy) Read(bytes []byte) (n int, err error) {
- readBytes := GetEntropyMixed(len(bytes))
+ readBytes := GetEntropyCSPRNG(len(bytes))
copy(bytes, readBytes)
return len(bytes), nil
}
@@ -29,40 +25,6 @@ func Sha3(data []byte) []byte {
return d.Sum(nil)
}
-// TODO: verify. this needs to be audited
-// we start with crypt/rand, then XOR in additional entropy from OS
-func GetEntropyMixed(n int) []byte {
- startTime := time.Now().UnixNano()
- // for each source, we take SHA3 of the source and use it as seed to math/rand
- // then read bytes from it and XOR them onto the bytes read from crypto/rand
- mainBuff := GetEntropyCSPRNG(n)
- // 1. OS entropy sources
- startTimeBytes := make([]byte, 32)
- binary.PutVarint(startTimeBytes, startTime)
- startTimeHash := Sha3(startTimeBytes)
- mixBytes(mainBuff, startTimeHash)
-
- pid := os.Getpid()
- pidBytes := make([]byte, 32)
- binary.PutUvarint(pidBytes, uint64(pid))
- pidHash := Sha3(pidBytes)
- mixBytes(mainBuff, pidHash)
-
- osEnv := os.Environ()
- osEnvBytes := []byte(strings.Join(osEnv, ""))
- osEnvHash := Sha3(osEnvBytes)
- mixBytes(mainBuff, osEnvHash)
-
- // not all OS have hostname in env variables
- osHostName, err := os.Hostname()
- if err != nil {
- osHostNameBytes := []byte(osHostName)
- osHostNameHash := Sha3(osHostNameBytes)
- mixBytes(mainBuff, osHostNameHash)
- }
- return mainBuff
-}
-
func GetEntropyCSPRNG(n int) []byte {
mainBuff := make([]byte, n)
_, err := io.ReadFull(crand.Reader, mainBuff)
@@ -71,14 +33,3 @@ func GetEntropyCSPRNG(n int) []byte {
}
return mainBuff
}
-
-func mixBytes(buff []byte, mixBuff []byte) []byte {
- bytesToMix := len(buff)
- if bytesToMix > 32 {
- bytesToMix = 32
- }
- for i := 0; i < bytesToMix; i++ {
- buff[i] ^= mixBuff[i]
- }
- return buff
-}