mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-17 01:13:45 +00:00
whisper: style fixes
This commit is contained in:
parent
f1b5e24264
commit
80b347d0bc
6 changed files with 35 additions and 31 deletions
|
|
@ -277,7 +277,7 @@ func (api *PublicWhisperAPI) Post(ctx context.Context, req NewMessage) (bool, er
|
||||||
if params.KeySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
|
if params.KeySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
if !validateRandomData(params.KeySym, aesKeyLength) {
|
if !validateDataIntegrity(params.KeySym, aesKeyLength) {
|
||||||
return false, ErrInvalidSymmetricKey
|
return false, ErrInvalidSymmetricKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -383,7 +383,7 @@ func (api *PublicWhisperAPI) Messages(ctx context.Context, crit Criteria) (*rpc.
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if !validateRandomData(key, aesKeyLength) {
|
if !validateDataIntegrity(key, aesKeyLength) {
|
||||||
return nil, ErrInvalidSymmetricKey
|
return nil, ErrInvalidSymmetricKey
|
||||||
}
|
}
|
||||||
filter.KeySym = key
|
filter.KeySym = key
|
||||||
|
|
@ -555,7 +555,7 @@ func (api *PublicWhisperAPI) NewMessageFilter(req Criteria) (string, error) {
|
||||||
if keySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
|
if keySym, err = api.w.GetSymKey(req.SymKeyID); err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if !validateRandomData(keySym, aesKeyLength) {
|
if !validateDataIntegrity(keySym, aesKeyLength) {
|
||||||
return "", ErrInvalidSymmetricKey
|
return "", ErrInvalidSymmetricKey
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,12 +51,14 @@ const (
|
||||||
auxFieldSizeMask = byte(3) // mask used to extract the size of auxiliary field from the flags
|
auxFieldSizeMask = byte(3) // mask used to extract the size of auxiliary field from the flags
|
||||||
signatureFlag = byte(4)
|
signatureFlag = byte(4)
|
||||||
|
|
||||||
TopicLength = 4 // in bytes
|
TopicLength = 4 // in bytes
|
||||||
signatureLength = 65 // in bytes
|
signatureLength = 65 // in bytes
|
||||||
aesKeyLength = 32 // in bytes
|
aesKeyLength = 32 // in bytes
|
||||||
aesNonceLength = 12 // in bytes; for more info please see cipher.gcmStandardNonceSize & aesgcm.NonceSize()
|
aesNonceLength = 12 // in bytes; for more info please see cipher.gcmStandardNonceSize & aesgcm.NonceSize()
|
||||||
keyIdSize = 32 // in bytes
|
keyIdSize = 32 // in bytes
|
||||||
bloomFilterSize = 64 // in bytes
|
bloomFilterSize = 64 // in bytes
|
||||||
|
flagsLength = 1
|
||||||
|
auxiliaryFieldMaxSize = 4
|
||||||
|
|
||||||
EnvelopeHeaderLength = 20
|
EnvelopeHeaderLength = 20
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -90,17 +90,18 @@ func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
||||||
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
||||||
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
msg.Raw = make([]byte, 1, 5+len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
msg.Raw = make([]byte, 1,
|
||||||
|
flagsLength+auxiliaryFieldMaxSize+len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
||||||
msg.Raw[0] = 0 // set all the flags to zero
|
msg.Raw[0] = 0 // set all the flags to zero
|
||||||
msg.addPayloadSizeField(params.Payload)
|
msg.addAuxiliaryField(params.Payload)
|
||||||
msg.Raw = append(msg.Raw, params.Payload...)
|
msg.Raw = append(msg.Raw, params.Payload...)
|
||||||
err := msg.appendPadding(params)
|
err := msg.appendPadding(params)
|
||||||
return &msg, err
|
return &msg, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// appendPayloadSizeField appends the auxiliary field containing the size of payload
|
// appendPayloadSizeField appends the auxiliary field containing the size of payload
|
||||||
func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
func (msg *sentMessage) addAuxiliaryField(payload []byte) {
|
||||||
fieldSize := getAuxFieldSize(payload)
|
fieldSize := getAuxiliaryFieldSize(payload)
|
||||||
field := make([]byte, 4)
|
field := make([]byte, 4)
|
||||||
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
||||||
field = field[:fieldSize]
|
field = field[:fieldSize]
|
||||||
|
|
@ -109,7 +110,7 @@ func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAuxFieldSize returns the number of bytes necessary to encode the size of payload
|
// getAuxFieldSize returns the number of bytes necessary to encode the size of payload
|
||||||
func getAuxFieldSize(payload []byte) int {
|
func getAuxiliaryFieldSize(payload []byte) int {
|
||||||
s := 1
|
s := 1
|
||||||
for i := len(payload); i >= 256; i /= 256 {
|
for i := len(payload); i >= 256; i /= 256 {
|
||||||
s++
|
s++
|
||||||
|
|
@ -126,8 +127,8 @@ func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
auxFieldSize := getAuxFieldSize(params.Payload)
|
auxFieldSize := getAuxiliaryFieldSize(params.Payload)
|
||||||
rawSize := 1 + auxFieldSize + len(params.Payload)
|
rawSize := flagsLength + auxFieldSize + len(params.Payload)
|
||||||
if params.Src != nil {
|
if params.Src != nil {
|
||||||
rawSize += signatureLength
|
rawSize += signatureLength
|
||||||
}
|
}
|
||||||
|
|
@ -138,7 +139,7 @@ func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !validateRandomData(pad, paddingSize) {
|
if !validateDataIntegrity(pad, paddingSize) {
|
||||||
return errors.New("failed to generate random padding of size " + strconv.Itoa(paddingSize))
|
return errors.New("failed to generate random padding of size " + strconv.Itoa(paddingSize))
|
||||||
}
|
}
|
||||||
msg.Raw = append(msg.Raw, pad...)
|
msg.Raw = append(msg.Raw, pad...)
|
||||||
|
|
@ -180,7 +181,7 @@ func (msg *sentMessage) encryptAsymmetric(key *ecdsa.PublicKey) error {
|
||||||
// encryptSymmetric encrypts a message with a topic key, using AES-GCM-256.
|
// encryptSymmetric encrypts a message with a topic key, using AES-GCM-256.
|
||||||
// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
|
// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
|
||||||
func (msg *sentMessage) encryptSymmetric(key []byte) (err error) {
|
func (msg *sentMessage) encryptSymmetric(key []byte) (err error) {
|
||||||
if !validateRandomData(key, aesKeyLength) {
|
if !validateDataIntegrity(key, aesKeyLength) {
|
||||||
return errors.New("invalid key provided for symmetric encryption, size: " + strconv.Itoa(len(key)))
|
return errors.New("invalid key provided for symmetric encryption, size: " + strconv.Itoa(len(key)))
|
||||||
}
|
}
|
||||||
block, err := aes.NewCipher(key)
|
block, err := aes.NewCipher(key)
|
||||||
|
|
@ -213,19 +214,19 @@ func generateSecureRandomData(length int) ([]byte, error) {
|
||||||
_, err := crand.Read(x)
|
_, err := crand.Read(x)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !validateRandomData(x, length) {
|
} else if !validateDataIntegrity(x, length) {
|
||||||
return nil, errors.New("crypto/rand failed to generate secure random data")
|
return nil, errors.New("crypto/rand failed to generate secure random data")
|
||||||
}
|
}
|
||||||
_, err = mrand.Read(y)
|
_, err = mrand.Read(y)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !validateRandomData(y, length) {
|
} else if !validateDataIntegrity(y, length) {
|
||||||
return nil, errors.New("math/rand failed to generate secure random data")
|
return nil, errors.New("math/rand failed to generate secure random data")
|
||||||
}
|
}
|
||||||
for i := 0; i < length; i++ {
|
for i := 0; i < length; i++ {
|
||||||
res[i] = x[i] ^ y[i]
|
res[i] = x[i] ^ y[i]
|
||||||
}
|
}
|
||||||
if !validateRandomData(res, length) {
|
if !validateDataIntegrity(res, length) {
|
||||||
return nil, errors.New("failed to generate secure random data")
|
return nil, errors.New("failed to generate secure random data")
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
|
|
@ -294,7 +295,7 @@ func (msg *ReceivedMessage) decryptAsymmetric(key *ecdsa.PrivateKey) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate checks the message validity and extracts the fields in case of success.
|
// ValidateAndParse checks the message validity and extracts the fields in case of success.
|
||||||
func (msg *ReceivedMessage) ValidateAndParse() bool {
|
func (msg *ReceivedMessage) ValidateAndParse() bool {
|
||||||
end := len(msg.Raw)
|
end := len(msg.Raw)
|
||||||
if end < 1 {
|
if end < 1 {
|
||||||
|
|
|
||||||
|
|
@ -438,7 +438,8 @@ func TestPaddingAppendedToSymMessagesWithSignature(t *testing.T) {
|
||||||
// payload + flag + signature > 256. Check that the result
|
// payload + flag + signature > 256. Check that the result
|
||||||
// is padded on the next 256 boundary.
|
// is padded on the next 256 boundary.
|
||||||
msg := sentMessage{}
|
msg := sentMessage{}
|
||||||
msg.Raw = make([]byte, 1+1+len(params.Payload))
|
const auxiliaryFieldMinSize = 1
|
||||||
|
msg.Raw = make([]byte, flagsLength+auxiliaryFieldMinSize+len(params.Payload))
|
||||||
|
|
||||||
err = msg.appendPadding(params)
|
err = msg.appendPadding(params)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -444,7 +444,7 @@ func (w *Whisper) GenerateSymKey() (string, error) {
|
||||||
key, err := generateSecureRandomData(aesKeyLength)
|
key, err := generateSecureRandomData(aesKeyLength)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
} else if !validateRandomData(key, aesKeyLength) {
|
} else if !validateDataIntegrity(key, aesKeyLength) {
|
||||||
return "", fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data")
|
return "", fmt.Errorf("error in GenerateSymKey: crypto/rand failed to generate random data")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -981,13 +981,13 @@ func validatePrivateKey(k *ecdsa.PrivateKey) bool {
|
||||||
return ValidatePublicKey(&k.PublicKey)
|
return ValidatePublicKey(&k.PublicKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
// validateSymmetricKey returns false if the key contains all zeros,
|
// validateDataIntegrity returns false if the data have the wrong or contains all zeros,
|
||||||
// which is a simplest and the most common bug.
|
// which is the simplest and the most common bug.
|
||||||
func validateRandomData(k []byte, expectedSize int) bool {
|
func validateDataIntegrity(k []byte, expectedSize int) bool {
|
||||||
if len(k) != expectedSize {
|
if len(k) != expectedSize {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if expectedSize > 4 && containsOnlyZeros(k) {
|
if expectedSize > 3 && containsOnlyZeros(k) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
@ -1028,7 +1028,7 @@ func GenerateRandomID() (id string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
if !validateRandomData(buf, keyIdSize) {
|
if !validateDataIntegrity(buf, keyIdSize) {
|
||||||
return "", fmt.Errorf("error in generateRandomID: crypto/rand failed to generate random data")
|
return "", fmt.Errorf("error in generateRandomID: crypto/rand failed to generate random data")
|
||||||
}
|
}
|
||||||
id = common.Bytes2Hex(buf)
|
id = common.Bytes2Hex(buf)
|
||||||
|
|
|
||||||
|
|
@ -81,7 +81,7 @@ func TestWhisperBasic(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
derived := pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New)
|
derived := pbkdf2.Key([]byte(peerID), nil, 65356, aesKeyLength, sha256.New)
|
||||||
if !validateRandomData(derived, aesKeyLength) {
|
if !validateDataIntegrity(derived, aesKeyLength) {
|
||||||
t.Fatalf("failed validateSymmetricKey with param = %v.", derived)
|
t.Fatalf("failed validateSymmetricKey with param = %v.", derived)
|
||||||
}
|
}
|
||||||
if containsOnlyZeros(derived) {
|
if containsOnlyZeros(derived) {
|
||||||
|
|
@ -448,7 +448,7 @@ func TestWhisperSymKeyManagement(t *testing.T) {
|
||||||
if !w.HasSymKey(id2) {
|
if !w.HasSymKey(id2) {
|
||||||
t.Fatalf("HasSymKey(id2) failed.")
|
t.Fatalf("HasSymKey(id2) failed.")
|
||||||
}
|
}
|
||||||
if !validateRandomData(k2, aesKeyLength) {
|
if !validateDataIntegrity(k2, aesKeyLength) {
|
||||||
t.Fatalf("key validation failed.")
|
t.Fatalf("key validation failed.")
|
||||||
}
|
}
|
||||||
if !bytes.Equal(k1, k2) {
|
if !bytes.Equal(k1, k2) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue