mirror of https://github.com/ethereum/go-ethereum
commit
49a739c8d6
@ -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 |
||||
} |
@ -1,97 +0,0 @@ |
||||
package secp256k1 |
||||
|
||||
import ( |
||||
crand "crypto/rand" |
||||
"io" |
||||
mrand "math/rand" |
||||
"os" |
||||
"strings" |
||||
"time" |
||||
) |
||||
|
||||
/* |
||||
Note: |
||||
|
||||
- On windows cryto/rand uses CrytoGenRandom which uses RC4 which is insecure |
||||
- Android random number generator is known to be insecure. |
||||
- Linux uses /dev/urandom , which is thought to be secure and uses entropy pool |
||||
|
||||
Therefore the output is salted. |
||||
*/ |
||||
|
||||
//finalizer from MurmerHash3
|
||||
func mmh3f(key uint64) uint64 { |
||||
key ^= key >> 33 |
||||
key *= 0xff51afd7ed558ccd |
||||
key ^= key >> 33 |
||||
key *= 0xc4ceb9fe1a85ec53 |
||||
key ^= key >> 33 |
||||
return key |
||||
} |
||||
|
||||
//knuth hash
|
||||
func knuth_hash(in []byte) uint64 { |
||||
var acc uint64 = 3074457345618258791 |
||||
for i := 0; i < len(in); i++ { |
||||
acc += uint64(in[i]) |
||||
acc *= 3074457345618258799 |
||||
} |
||||
return acc |
||||
} |
||||
|
||||
var _rand *mrand.Rand |
||||
|
||||
func init() { |
||||
var seed1 uint64 = mmh3f(uint64(time.Now().UnixNano())) |
||||
var seed2 uint64 = knuth_hash([]byte(strings.Join(os.Environ(), ""))) |
||||
var seed3 uint64 = mmh3f(uint64(os.Getpid())) |
||||
|
||||
_rand = mrand.New(mrand.NewSource(int64(seed1 ^ seed2 ^ seed3))) |
||||
} |
||||
|
||||
func saltByte(n int) []byte { |
||||
buff := make([]byte, n) |
||||
for i := 0; i < len(buff); i++ { |
||||
var v uint64 = uint64(_rand.Int63()) |
||||
var b byte |
||||
for j := 0; j < 8; j++ { |
||||
b ^= byte(v & 0xff) |
||||
v = v >> 8 |
||||
} |
||||
buff[i] = b |
||||
} |
||||
return buff |
||||
} |
||||
|
||||
//On Unix-like systems, Reader reads from /dev/urandom.
|
||||
//On Windows systems, Reader uses the CryptGenRandom API.
|
||||
|
||||
//use entropy pool etc and cryptographic random number generator
|
||||
//mix in time
|
||||
//mix in mix in cpu cycle count
|
||||
func RandByte(n int) []byte { |
||||
buff := make([]byte, n) |
||||
ret, err := io.ReadFull(crand.Reader, buff) |
||||
if len(buff) != ret || err != nil { |
||||
return nil |
||||
} |
||||
|
||||
buff2 := saltByte(n) |
||||
for i := 0; i < n; i++ { |
||||
buff[i] ^= buff2[2] |
||||
} |
||||
return buff |
||||
} |
||||
|
||||
/* |
||||
On Unix-like systems, Reader reads from /dev/urandom. |
||||
On Windows systems, Reader uses the CryptGenRandom API. |
||||
*/ |
||||
func RandByteWeakCrypto(n int) []byte { |
||||
buff := make([]byte, n) |
||||
ret, err := io.ReadFull(crand.Reader, buff) |
||||
if len(buff) != ret || err != nil { |
||||
return nil |
||||
} |
||||
return buff |
||||
} |
Loading…
Reference in new issue