aboutsummaryrefslogtreecommitdiffstats
path: root/crypto/randentropy/rand_entropy.go
diff options
context:
space:
mode:
authorEthan Buchman <ethan@coinculture.info>2015-02-18 08:25:18 +0800
committerEthan Buchman <ethan@coinculture.info>2015-02-18 08:25:18 +0800
commit2ba65f4fbaea49c1e0d99959b0331e09b153f931 (patch)
treeadd8cabb05cd7fbf0ba4b4bbaf9460dacfc2082d /crypto/randentropy/rand_entropy.go
parent2da367a2bee84d74d1bb0ea1b42d4c22fae486dd (diff)
parent60318c96d03bcaaf731802b1080a3d87cb482124 (diff)
downloadgo-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar
go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.gz
go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.bz2
go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.lz
go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.xz
go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.tar.zst
go-tangerine-2ba65f4fbaea49c1e0d99959b0331e09b153f931.zip
Merge branch 'develop' of https://github.com/ethereum/go-ethereum into develop
Diffstat (limited to 'crypto/randentropy/rand_entropy.go')
-rw-r--r--crypto/randentropy/rand_entropy.go84
1 files changed, 84 insertions, 0 deletions
diff --git a/crypto/randentropy/rand_entropy.go b/crypto/randentropy/rand_entropy.go
new file mode 100644
index 000000000..b87fa564e
--- /dev/null
+++ b/crypto/randentropy/rand_entropy.go
@@ -0,0 +1,84 @@
+package randentropy
+
+import (
+ crand "crypto/rand"
+ "encoding/binary"
+ "github.com/ethereum/go-ethereum/crypto/sha3"
+ "io"
+ "os"
+ "strings"
+ "time"
+)
+
+var Reader io.Reader = &randEntropy{}
+
+type randEntropy struct {
+}
+
+func (*randEntropy) Read(bytes []byte) (n int, err error) {
+ readBytes := GetEntropyMixed(len(bytes))
+ copy(bytes, readBytes)
+ return len(bytes), nil
+}
+
+// TODO: copied from crypto.go , move to sha3 package?
+func Sha3(data []byte) []byte {
+ d := sha3.NewKeccak256()
+ d.Write(data)
+
+ 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)
+ if err != nil {
+ panic("reading from crypto/rand failed: " + err.Error())
+ }
+ 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
+}