diff options
author | gluk256 <gluk256@users.noreply.github.com> | 2018-01-30 16:55:08 +0800 |
---|---|---|
committer | Péter Szilágyi <peterke@gmail.com> | 2018-01-30 16:55:08 +0800 |
commit | a9e4a90d57d3c072fb727ee62b71002325e23934 (patch) | |
tree | 45b0cd4885ebfb562fe0dbeb8b8b9d6859875401 /whisper/whisperv6/whisper.go | |
parent | 59a852e4188e18e587b6d54f646a6780f4d1f12c (diff) | |
download | go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.tar go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.tar.gz go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.tar.bz2 go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.tar.lz go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.tar.xz go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.tar.zst go-tangerine-a9e4a90d57d3c072fb727ee62b71002325e23934.zip |
whisper: change the whisper message format so as to add the payload size (#15870)
* whisper: message format changed
* whisper: tests fixed
* whisper: style fixes
* whisper: fixed names, fixed failing tests
* whisper: fix merge issue in #15870
Occured while using the github online merge tool. Lesson learned.
* whisper: fix a gofmt error for #15870
Diffstat (limited to 'whisper/whisperv6/whisper.go')
-rw-r--r-- | whisper/whisperv6/whisper.go | 24 |
1 files changed, 14 insertions, 10 deletions
diff --git a/whisper/whisperv6/whisper.go b/whisper/whisperv6/whisper.go index 1b440a7f9..d75ad04ac 100644 --- a/whisper/whisperv6/whisper.go +++ b/whisper/whisperv6/whisper.go @@ -19,7 +19,6 @@ package whisperv6 import ( "bytes" "crypto/ecdsa" - crand "crypto/rand" "crypto/sha256" "fmt" "math" @@ -444,11 +443,10 @@ func (whisper *Whisper) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) { // GenerateSymKey generates a random symmetric key and stores it under id, // which is then returned. Will be used in the future for session key exchange. func (whisper *Whisper) GenerateSymKey() (string, error) { - key := make([]byte, aesKeyLength) - _, err := crand.Read(key) + key, err := generateSecureRandomData(aesKeyLength) if err != nil { return "", err - } else if !validateSymmetricKey(key) { + } else if !validateDataIntegrity(key, aesKeyLength) { return "", fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data") } @@ -983,9 +981,16 @@ func validatePrivateKey(k *ecdsa.PrivateKey) bool { return ValidatePublicKey(&k.PublicKey) } -// validateSymmetricKey returns false if the key contains all zeros -func validateSymmetricKey(k []byte) bool { - return len(k) > 0 && !containsOnlyZeros(k) +// validateDataIntegrity returns false if the data have the wrong or contains all zeros, +// which is the simplest and the most common bug. +func validateDataIntegrity(k []byte, expectedSize int) bool { + if len(k) != expectedSize { + return false + } + if expectedSize > 3 && containsOnlyZeros(k) { + return false + } + return true } // containsOnlyZeros checks if the data contain only zeros. @@ -1019,12 +1024,11 @@ func BytesToUintBigEndian(b []byte) (res uint64) { // GenerateRandomID generates a random string, which is then returned to be used as a key id func GenerateRandomID() (id string, err error) { - buf := make([]byte, keyIDSize) - _, err = crand.Read(buf) + buf, err := generateSecureRandomData(keyIDSize) if err != nil { return "", err } - if !validateSymmetricKey(buf) { + if !validateDataIntegrity(buf, keyIDSize) { return "", fmt.Errorf("error in generateRandomID: crypto/rand failed to generate random data") } id = common.Bytes2Hex(buf) |