2017-11-03 15:29:49 -05:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
// Contains the Whisper protocol Message element.
|
|
|
|
|
|
|
|
package whisperv6
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/aes"
|
|
|
|
"crypto/cipher"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
crand "crypto/rand"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2018-01-30 02:55:08 -06:00
|
|
|
mrand "math/rand"
|
2017-11-03 15:29:49 -05:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2018-01-26 05:45:10 -06:00
|
|
|
// MessageParams specifies the exact way a message should be wrapped
|
|
|
|
// into an Envelope.
|
2017-11-03 15:29:49 -05:00
|
|
|
type MessageParams struct {
|
|
|
|
TTL uint32
|
|
|
|
Src *ecdsa.PrivateKey
|
|
|
|
Dst *ecdsa.PublicKey
|
|
|
|
KeySym []byte
|
|
|
|
Topic TopicType
|
|
|
|
WorkTime uint32
|
|
|
|
PoW float64
|
|
|
|
Payload []byte
|
|
|
|
Padding []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// SentMessage represents an end-user data packet to transmit through the
|
|
|
|
// Whisper protocol. These are wrapped into Envelopes that need not be
|
|
|
|
// understood by intermediate nodes, just forwarded.
|
|
|
|
type sentMessage struct {
|
|
|
|
Raw []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceivedMessage represents a data packet to be received through the
|
2018-01-30 02:55:08 -06:00
|
|
|
// Whisper protocol and successfully decrypted.
|
2017-11-03 15:29:49 -05:00
|
|
|
type ReceivedMessage struct {
|
|
|
|
Raw []byte
|
|
|
|
|
|
|
|
Payload []byte
|
|
|
|
Padding []byte
|
|
|
|
Signature []byte
|
2017-12-08 04:40:59 -06:00
|
|
|
Salt []byte
|
2017-11-03 15:29:49 -05:00
|
|
|
|
|
|
|
PoW float64 // Proof of work as described in the Whisper spec
|
|
|
|
Sent uint32 // Time when the message was posted into the network
|
|
|
|
TTL uint32 // Maximum time to live allowed for the message
|
|
|
|
Src *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
|
|
|
Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
|
|
|
Topic TopicType
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
SymKeyHash common.Hash // The Keccak256Hash of the key
|
2017-12-08 09:08:56 -06:00
|
|
|
EnvelopeHash common.Hash // Message envelope hash to act as a unique id
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func isMessageSigned(flags byte) bool {
|
|
|
|
return (flags & signatureFlag) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *ReceivedMessage) isSymmetricEncryption() bool {
|
|
|
|
return msg.SymKeyHash != common.Hash{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
|
|
|
return msg.Dst != nil
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:45:10 -06:00
|
|
|
// NewSentMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
2018-02-10 07:35:32 -06:00
|
|
|
func NewSentMessage(params *MessageParams) (*sentMessage, error) {
|
2018-01-30 02:55:08 -06:00
|
|
|
const payloadSizeFieldMaxSize = 4
|
2017-11-03 15:29:49 -05:00
|
|
|
msg := sentMessage{}
|
2018-01-30 02:55:08 -06:00
|
|
|
msg.Raw = make([]byte, 1,
|
|
|
|
flagsLength+payloadSizeFieldMaxSize+len(params.Payload)+len(params.Padding)+signatureLength+padSizeLimit)
|
2017-11-03 15:29:49 -05:00
|
|
|
msg.Raw[0] = 0 // set all the flags to zero
|
2018-01-30 02:55:08 -06:00
|
|
|
msg.addPayloadSizeField(params.Payload)
|
2017-11-03 15:29:49 -05:00
|
|
|
msg.Raw = append(msg.Raw, params.Payload...)
|
2018-01-30 02:55:08 -06:00
|
|
|
err := msg.appendPadding(params)
|
|
|
|
return &msg, err
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
// addPayloadSizeField appends the auxiliary field containing the size of payload
|
|
|
|
func (msg *sentMessage) addPayloadSizeField(payload []byte) {
|
|
|
|
fieldSize := getSizeOfPayloadSizeField(payload)
|
|
|
|
field := make([]byte, 4)
|
|
|
|
binary.LittleEndian.PutUint32(field, uint32(len(payload)))
|
|
|
|
field = field[:fieldSize]
|
|
|
|
msg.Raw = append(msg.Raw, field...)
|
|
|
|
msg.Raw[0] |= byte(fieldSize)
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
// getSizeOfPayloadSizeField returns the number of bytes necessary to encode the size of payload
|
|
|
|
func getSizeOfPayloadSizeField(payload []byte) int {
|
|
|
|
s := 1
|
|
|
|
for i := len(payload); i >= 256; i /= 256 {
|
|
|
|
s++
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
// appendPadding appends the padding specified in params.
|
|
|
|
// If no padding is provided in params, then random padding is generated.
|
2017-11-03 15:29:49 -05:00
|
|
|
func (msg *sentMessage) appendPadding(params *MessageParams) error {
|
2018-01-30 02:55:08 -06:00
|
|
|
if len(params.Padding) != 0 {
|
|
|
|
// padding data was provided by the Dapp, just use it as is
|
|
|
|
msg.Raw = append(msg.Raw, params.Padding...)
|
|
|
|
return nil
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
2017-12-11 05:32:58 -06:00
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
rawSize := flagsLength + getSizeOfPayloadSizeField(params.Payload) + len(params.Payload)
|
|
|
|
if params.Src != nil {
|
|
|
|
rawSize += signatureLength
|
2017-12-11 05:32:58 -06:00
|
|
|
}
|
2017-11-03 15:29:49 -05:00
|
|
|
odd := rawSize % padSizeLimit
|
2018-01-30 02:55:08 -06:00
|
|
|
paddingSize := padSizeLimit - odd
|
|
|
|
pad := make([]byte, paddingSize)
|
|
|
|
_, err := crand.Read(pad)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
2018-01-30 02:55:08 -06:00
|
|
|
if !validateDataIntegrity(pad, paddingSize) {
|
|
|
|
return errors.New("failed to generate random padding of size " + strconv.Itoa(paddingSize))
|
|
|
|
}
|
|
|
|
msg.Raw = append(msg.Raw, pad...)
|
2017-11-03 15:29:49 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sign calculates and sets the cryptographic signature for the message,
|
|
|
|
// also setting the sign flag.
|
|
|
|
func (msg *sentMessage) sign(key *ecdsa.PrivateKey) error {
|
|
|
|
if isMessageSigned(msg.Raw[0]) {
|
|
|
|
// this should not happen, but no reason to panic
|
|
|
|
log.Error("failed to sign the message: already signed")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
msg.Raw[0] |= signatureFlag // it is important to set this flag before signing
|
2017-11-03 15:29:49 -05:00
|
|
|
hash := crypto.Keccak256(msg.Raw)
|
|
|
|
signature, err := crypto.Sign(hash, key)
|
|
|
|
if err != nil {
|
2018-01-30 02:55:08 -06:00
|
|
|
msg.Raw[0] &= (0xFF ^ signatureFlag) // clear the flag
|
2017-11-03 15:29:49 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.Raw = append(msg.Raw, signature...)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptAsymmetric encrypts a message with a public key.
|
|
|
|
func (msg *sentMessage) encryptAsymmetric(key *ecdsa.PublicKey) error {
|
|
|
|
if !ValidatePublicKey(key) {
|
|
|
|
return errors.New("invalid public key provided for asymmetric encryption")
|
|
|
|
}
|
|
|
|
encrypted, err := ecies.Encrypt(crand.Reader, ecies.ImportECDSAPublic(key), msg.Raw, nil, nil)
|
|
|
|
if err == nil {
|
|
|
|
msg.Raw = encrypted
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// encryptSymmetric encrypts a message with a topic key, using AES-GCM-256.
|
|
|
|
// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
|
2017-12-08 04:40:59 -06:00
|
|
|
func (msg *sentMessage) encryptSymmetric(key []byte) (err error) {
|
2018-01-30 02:55:08 -06:00
|
|
|
if !validateDataIntegrity(key, aesKeyLength) {
|
|
|
|
return errors.New("invalid key provided for symmetric encryption, size: " + strconv.Itoa(len(key)))
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
2017-12-08 04:40:59 -06:00
|
|
|
return err
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
aesgcm, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
2017-12-08 04:40:59 -06:00
|
|
|
return err
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
2018-01-30 02:55:08 -06:00
|
|
|
salt, err := generateSecureRandomData(aesNonceLength) // never use more than 2^32 random nonces with a given key
|
2017-11-03 15:29:49 -05:00
|
|
|
if err != nil {
|
2017-12-08 04:40:59 -06:00
|
|
|
return err
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
2018-01-30 02:55:08 -06:00
|
|
|
encrypted := aesgcm.Seal(nil, salt, msg.Raw, nil)
|
|
|
|
msg.Raw = append(encrypted, salt...)
|
2017-12-08 04:40:59 -06:00
|
|
|
return nil
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
// generateSecureRandomData generates random data where extra security is required.
|
|
|
|
// The purpose of this function is to prevent some bugs in software or in hardware
|
|
|
|
// from delivering not-very-random data. This is especially useful for AES nonce,
|
|
|
|
// where true randomness does not really matter, but it is very important to have
|
|
|
|
// a unique nonce for every message.
|
|
|
|
func generateSecureRandomData(length int) ([]byte, error) {
|
|
|
|
x := make([]byte, length)
|
|
|
|
y := make([]byte, length)
|
|
|
|
res := make([]byte, length)
|
|
|
|
|
|
|
|
_, err := crand.Read(x)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !validateDataIntegrity(x, length) {
|
|
|
|
return nil, errors.New("crypto/rand failed to generate secure random data")
|
|
|
|
}
|
|
|
|
_, err = mrand.Read(y)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
} else if !validateDataIntegrity(y, length) {
|
|
|
|
return nil, errors.New("math/rand failed to generate secure random data")
|
|
|
|
}
|
|
|
|
for i := 0; i < length; i++ {
|
|
|
|
res[i] = x[i] ^ y[i]
|
|
|
|
}
|
|
|
|
if !validateDataIntegrity(res, length) {
|
|
|
|
return nil, errors.New("failed to generate secure random data")
|
|
|
|
}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2017-11-03 15:29:49 -05:00
|
|
|
// Wrap bundles the message into an Envelope to transmit over the network.
|
|
|
|
func (msg *sentMessage) Wrap(options *MessageParams) (envelope *Envelope, err error) {
|
|
|
|
if options.TTL == 0 {
|
|
|
|
options.TTL = DefaultTTL
|
|
|
|
}
|
|
|
|
if options.Src != nil {
|
|
|
|
if err = msg.sign(options.Src); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.Dst != nil {
|
|
|
|
err = msg.encryptAsymmetric(options.Dst)
|
|
|
|
} else if options.KeySym != nil {
|
2017-12-08 04:40:59 -06:00
|
|
|
err = msg.encryptSymmetric(options.KeySym)
|
2017-11-03 15:29:49 -05:00
|
|
|
} else {
|
|
|
|
err = errors.New("unable to encrypt the message: neither symmetric nor assymmetric key provided")
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-08 04:40:59 -06:00
|
|
|
envelope = NewEnvelope(options.TTL, options.Topic, msg)
|
2017-11-03 15:29:49 -05:00
|
|
|
if err = envelope.Seal(options); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return envelope, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptSymmetric decrypts a message with a topic key, using AES-GCM-256.
|
|
|
|
// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
|
2017-12-08 04:40:59 -06:00
|
|
|
func (msg *ReceivedMessage) decryptSymmetric(key []byte) error {
|
2018-01-30 02:55:08 -06:00
|
|
|
// symmetric messages are expected to contain the 12-byte nonce at the end of the payload
|
|
|
|
if len(msg.Raw) < aesNonceLength {
|
2017-12-08 04:40:59 -06:00
|
|
|
return errors.New("missing salt or invalid payload in symmetric message")
|
|
|
|
}
|
2018-01-30 02:55:08 -06:00
|
|
|
salt := msg.Raw[len(msg.Raw)-aesNonceLength:]
|
2017-12-08 04:40:59 -06:00
|
|
|
|
2017-11-03 15:29:49 -05:00
|
|
|
block, err := aes.NewCipher(key)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
aesgcm, err := cipher.NewGCM(block)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-01-30 02:55:08 -06:00
|
|
|
decrypted, err := aesgcm.Open(nil, salt, msg.Raw[:len(msg.Raw)-aesNonceLength], nil)
|
2017-11-03 15:29:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.Raw = decrypted
|
2017-12-08 04:40:59 -06:00
|
|
|
msg.Salt = salt
|
2017-11-03 15:29:49 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// decryptAsymmetric decrypts an encrypted payload with a private key.
|
|
|
|
func (msg *ReceivedMessage) decryptAsymmetric(key *ecdsa.PrivateKey) error {
|
2018-03-26 05:46:18 -05:00
|
|
|
decrypted, err := ecies.ImportECDSA(key).Decrypt(msg.Raw, nil, nil)
|
2017-11-03 15:29:49 -05:00
|
|
|
if err == nil {
|
|
|
|
msg.Raw = decrypted
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
// ValidateAndParse checks the message validity and extracts the fields in case of success.
|
|
|
|
func (msg *ReceivedMessage) ValidateAndParse() bool {
|
2017-11-03 15:29:49 -05:00
|
|
|
end := len(msg.Raw)
|
|
|
|
if end < 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if isMessageSigned(msg.Raw[0]) {
|
|
|
|
end -= signatureLength
|
|
|
|
if end <= 1 {
|
|
|
|
return false
|
|
|
|
}
|
2018-01-30 02:55:08 -06:00
|
|
|
msg.Signature = msg.Raw[end : end+signatureLength]
|
2017-11-03 15:29:49 -05:00
|
|
|
msg.Src = msg.SigToPubKey()
|
|
|
|
if msg.Src == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
beg := 1
|
|
|
|
payloadSize := 0
|
|
|
|
sizeOfPayloadSizeField := int(msg.Raw[0] & SizeMask) // number of bytes indicating the size of payload
|
|
|
|
if sizeOfPayloadSizeField != 0 {
|
|
|
|
payloadSize = int(bytesToUintLittleEndian(msg.Raw[beg : beg+sizeOfPayloadSizeField]))
|
|
|
|
if payloadSize+1 > end {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
beg += sizeOfPayloadSizeField
|
|
|
|
msg.Payload = msg.Raw[beg : beg+payloadSize]
|
2017-11-03 15:29:49 -05:00
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
beg += payloadSize
|
|
|
|
msg.Padding = msg.Raw[beg:end]
|
2017-11-03 15:29:49 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-01-26 05:45:10 -06:00
|
|
|
// SigToPubKey returns the public key associated to the message's
|
|
|
|
// signature.
|
2017-11-03 15:29:49 -05:00
|
|
|
func (msg *ReceivedMessage) SigToPubKey() *ecdsa.PublicKey {
|
|
|
|
defer func() { recover() }() // in case of invalid signature
|
|
|
|
|
|
|
|
pub, err := crypto.SigToPub(msg.hash(), msg.Signature)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to recover public key from signature", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pub
|
|
|
|
}
|
|
|
|
|
2018-01-30 02:55:08 -06:00
|
|
|
// hash calculates the SHA3 checksum of the message flags, payload size field, payload and padding.
|
2017-11-03 15:29:49 -05:00
|
|
|
func (msg *ReceivedMessage) hash() []byte {
|
|
|
|
if isMessageSigned(msg.Raw[0]) {
|
|
|
|
sz := len(msg.Raw) - signatureLength
|
|
|
|
return crypto.Keccak256(msg.Raw[:sz])
|
|
|
|
}
|
|
|
|
return crypto.Keccak256(msg.Raw)
|
|
|
|
}
|