Commit 79789af2 authored by gluk256's avatar gluk256 Committed by Felix Lange

whisper: project restructured, version 5 introduced (#3022)

whisper: project restructured, version 5 introduced

This commits adds a draft version of the new shh v5 protocol.
The new version is not on by default, --shh still selects version 2.
parent 00665a0b
......@@ -31,7 +31,7 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/tests"
"github.com/ethereum/go-ethereum/whisper"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
)
const defaultTestKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
......
......@@ -48,7 +48,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/whisper"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv2"
"gopkg.in/urfave/cli.v1"
)
......
This diff is collapsed.
// 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/>.
package shhapi
import (
"testing"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/whisper/whisperv5"
)
func TestBasic(x *testing.T) {
var id string = "test"
api := NewPublicWhisperAPI()
if api == nil {
x.Errorf("failed to create API.")
return
}
ver, err := api.Version()
if err != nil {
x.Errorf("failed generateFilter: %s.", err)
return
}
if ver.Uint64() != whisperv5.ProtocolVersion {
x.Errorf("wrong version: %d.", ver.Uint64())
return
}
var hexnum rpc.HexNumber
mail := api.GetFilterChanges(hexnum)
if len(mail) != 0 {
x.Errorf("failed GetFilterChanges")
return
}
exist, err := api.HasIdentity(id)
if err != nil {
x.Errorf("failed 1 HasIdentity: %s.", err)
return
}
if exist {
x.Errorf("failed 2 HasIdentity: false positive.")
return
}
err = api.DeleteIdentity(id)
if err != nil {
x.Errorf("failed 3 DeleteIdentity: %s.", err)
return
}
pub, err := api.NewIdentity()
if err != nil {
x.Errorf("failed 4 NewIdentity: %s.", err)
return
}
if len(pub) == 0 {
x.Errorf("NewIdentity 5: empty")
return
}
exist, err = api.HasIdentity(pub)
if err != nil {
x.Errorf("failed 6 HasIdentity: %s.", err)
return
}
if !exist {
x.Errorf("failed 7 HasIdentity: false negative.")
return
}
err = api.DeleteIdentity(pub)
if err != nil {
x.Errorf("failed 8 DeleteIdentity: %s.", err)
return
}
exist, err = api.HasIdentity(pub)
if err != nil {
x.Errorf("failed 9 HasIdentity: %s.", err)
return
}
if exist {
x.Errorf("failed 10 HasIdentity: false positive.")
return
}
id = "arbitrary text"
id2 := "another arbitrary string"
exist, err = api.HasSymKey(id)
if err != nil {
x.Errorf("failed 11 HasSymKey: %s.", err)
return
}
if exist {
x.Errorf("failed 12 HasSymKey: false positive.")
return
}
err = api.GenerateSymKey(id)
if err != nil {
x.Errorf("failed 13 GenerateSymKey: %s.", err)
return
}
exist, err = api.HasSymKey(id)
if err != nil {
x.Errorf("failed 14 HasSymKey: %s.", err)
return
}
if !exist {
x.Errorf("failed 15 HasSymKey: false negative.")
return
}
err = api.AddSymKey(id, []byte("some stuff here"))
if err == nil {
x.Errorf("failed 16 AddSymKey: %s.", err)
return
}
err = api.AddSymKey(id2, []byte("some stuff here"))
if err != nil {
x.Errorf("failed 17 AddSymKey: %s.", err)
return
}
exist, err = api.HasSymKey(id2)
if err != nil {
x.Errorf("failed 18 HasSymKey: %s.", err)
return
}
if !exist {
x.Errorf("failed 19 HasSymKey: false negative.")
return
}
err = api.DeleteSymKey(id)
if err != nil {
x.Errorf("failed 20 DeleteSymKey: %s.", err)
return
}
exist, err = api.HasSymKey(id)
if err != nil {
x.Errorf("failed 21 HasSymKey: %s.", err)
return
}
if exist {
x.Errorf("failed 22 HasSymKey: false positive.")
return
}
}
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"encoding/json"
......
......@@ -29,4 +29,4 @@ Whisper is a pure identity-based messaging system. Whisper provides a low-level
or prejudiced by the low-level hardware attributes and characteristics,
particularly the notion of singular endpoints.
*/
package whisper
package whisperv2
......@@ -17,7 +17,7 @@
// Contains the Whisper protocol Envelope element. For formal details please see
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#envelopes.
package whisper
package whisperv2
import (
"crypto/ecdsa"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"bytes"
......
......@@ -16,7 +16,7 @@
// Contains the message filter for fine grained subscriptions.
package whisper
package whisperv2
import (
"crypto/ecdsa"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"bytes"
......
......@@ -17,7 +17,7 @@
// Contains the Whisper protocol Message element. For formal details please see
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#messages.
package whisper
package whisperv2
import (
"crypto/ecdsa"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"bytes"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"fmt"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"testing"
......
......@@ -17,7 +17,7 @@
// Contains the Whisper protocol Topic element. For formal details please see
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics.
package whisper
package whisperv2
import "github.com/ethereum/go-ethereum/crypto"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"bytes"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"crypto/ecdsa"
......
......@@ -14,7 +14,7 @@
// 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/>.
package whisper
package whisperv2
import (
"testing"
......
// 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/>.
package whisperv5
import (
"testing"
"github.com/ethereum/go-ethereum/crypto"
)
func BenchmarkDeriveKeyMaterial(b *testing.B) {
for i := 0; i < b.N; i++ {
deriveKeyMaterial([]byte("test"), 0)
}
}
func BenchmarkDeriveOneTimeKey(b *testing.B) {
for i := 0; i < b.N; i++ {
DeriveOneTimeKey([]byte("test value 1"), []byte("test value 2"), 0)
}
}
//func TestEncryptionSym(b *testing.T) {
func BenchmarkEncryptionSym(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
for i := 0; i < b.N; i++ {
msg := NewSentMessage(params)
_, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
b.Errorf("i = %d, len(msg.Raw) = %d, params.Payload = %d.", i, len(msg.Raw), len(params.Payload))
return
}
}
}
func BenchmarkEncryptionAsym(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
key, err := crypto.GenerateKey()
if err != nil {
b.Errorf("failed GenerateKey with seed %d: %s.", seed, err)
return
}
params.KeySym = nil
params.Dst = &key.PublicKey
for i := 0; i < b.N; i++ {
msg := NewSentMessage(params)
_, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
}
}
func BenchmarkDecryptionSymValid(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
f := Filter{KeySym: params.KeySym}
for i := 0; i < b.N; i++ {
msg := env.Open(&f)
if msg == nil {
b.Errorf("failed to open with seed %d.", seed)
return
}
}
}
func BenchmarkDecryptionSymInvalid(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
f := Filter{KeySym: []byte("arbitrary stuff here")}
for i := 0; i < b.N; i++ {
msg := env.Open(&f)
if msg != nil {
b.Errorf("opened envelope with invalid key, seed: %d.", seed)
return
}
}
}
func BenchmarkDecryptionAsymValid(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
key, err := crypto.GenerateKey()
if err != nil {
b.Errorf("failed GenerateKey with seed %d: %s.", seed, err)
return
}
f := Filter{KeyAsym: key}
params.KeySym = nil
params.Dst = &key.PublicKey
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
for i := 0; i < b.N; i++ {
msg := env.Open(&f)
if msg == nil {
b.Errorf("fail to open, seed: %d.", seed)
return
}
}
}
func BenchmarkDecryptionAsymInvalid(b *testing.B) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
b.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
key, err := crypto.GenerateKey()
if err != nil {
b.Errorf("failed GenerateKey with seed %d: %s.", seed, err)
return
}
params.KeySym = nil
params.Dst = &key.PublicKey
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
b.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
key, err = crypto.GenerateKey()
if err != nil {
b.Errorf("failed GenerateKey with seed %d: %s.", seed, err)
return
}
f := Filter{KeyAsym: key}
for i := 0; i < b.N; i++ {
msg := env.Open(&f)
if msg != nil {
b.Errorf("opened envelope with invalid key, seed: %d.", seed)
return
}
}
}
// 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/>.
/*
Package whisper implements the Whisper PoC-1.
(https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec)
Whisper combines aspects of both DHTs and datagram messaging systems (e.g. UDP).
As such it may be likened and compared to both, not dissimilar to the
matter/energy duality (apologies to physicists for the blatant abuse of a
fundamental and beautiful natural principle).
Whisper is a pure identity-based messaging system. Whisper provides a low-level
(non-application-specific) but easily-accessible API without being based upon
or prejudiced by the low-level hardware attributes and characteristics,
particularly the notion of singular endpoints.
*/
package whisperv5
import (
"fmt"
"time"
)
const (
EnvelopeVersion = uint64(0)
ProtocolVersion = uint64(5)
ProtocolVersionStr = "5.0"
ProtocolName = "shh"
statusCode = 0
messagesCode = 1
p2pCode = 2
mailRequestCode = 3
NumberOfMessageCodes = 4
paddingMask = byte(3)
signatureFlag = byte(4)
TopicLength = 4
signatureLength = 65
aesKeyLength = 32
saltLength = 12
MaxMessageLength = 0xFFFF // todo: remove this restriction after testing in morden and analizing stats. this should be regulated by MinimumPoW.
MinimumPoW = 10.0 // todo: review
padSizeLimitLower = 128 // it can not be less - we don't want to reveal the absence of signature
padSizeLimitUpper = 256 // just an arbitrary number, could be changed without losing compatibility
expirationCycle = time.Second
transmissionCycle = 300 * time.Millisecond
DefaultTTL = 50 // seconds
SynchAllowance = 10 // seconds
)
type unknownVersionError uint64
func (e unknownVersionError) Error() string {
return fmt.Sprintf("invalid envelope version %d", uint64(e))
}
// MailServer represents a mail server, capable of
// archiving the old messages for subsequent delivery
// to the peers. Any implementation must ensure that both
// functions are thread-safe. Also, they must return ASAP.
// DeliverMail should use directMessagesCode for delivery,
// in order to bypass the expiry checks.
type MailServer interface {
Archive(env *Envelope)
DeliverMail(whisperPeer *Peer, data []byte)
}
// 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 Envelope element. For formal details please see
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#envelopes.
package whisperv5
import (
"crypto/ecdsa"
"encoding/binary"
"fmt"
"math"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/rlp"
)
// Envelope represents a clear-text data packet to transmit through the Whisper
// network. Its contents may or may not be encrypted and signed.
type Envelope struct {
Version []byte
Expiry uint32
TTL uint32
Topic TopicType
Salt []byte
AESNonce []byte
Data []byte
EnvNonce uint64
pow float64 // Message-specific PoW as described in the Whisper specification.
hash common.Hash // Cached hash of the envelope to avoid rehashing every time.
// Don't access hash directly, use Hash() function instead.
}
// NewEnvelope wraps a Whisper message with expiration and destination data
// included into an envelope for network forwarding.
func NewEnvelope(ttl uint32, topic TopicType, salt []byte, aesNonce []byte, msg *SentMessage) *Envelope {
env := Envelope{
Version: make([]byte, 1),
Expiry: uint32(time.Now().Add(time.Second * time.Duration(ttl)).Unix()),
TTL: ttl,
Topic: topic,
Salt: salt,
AESNonce: aesNonce,
Data: msg.Raw,
EnvNonce: 0,
}
if EnvelopeVersion < 256 {
env.Version[0] = byte(EnvelopeVersion)
} else {
panic("please increase the size of Envelope.Version before releasing this version")
}
return &env
}
func (e *Envelope) IsSymmetric() bool {
return e.AESNonce != nil
}
func (e *Envelope) isAsymmetric() bool {
return !e.IsSymmetric()
}
func (e *Envelope) Ver() uint64 {
return bytesToIntLittleEndian(e.Version)
}
// Seal closes the envelope by spending the requested amount of time as a proof
// of work on hashing the data.
func (e *Envelope) Seal(options *MessageParams) {
var target int
if options.PoW == 0 {
// adjust for the duration of Seal() execution only if execution time is predefined unconditionally
e.Expiry += options.WorkTime
} else {
target = e.powToFirstBit(options.PoW)
}
buf := make([]byte, 64)
h := crypto.Keccak256(e.rlpWithoutNonce())
copy(buf[:32], h)
finish, bestBit := time.Now().Add(time.Duration(options.WorkTime)*time.Second).UnixNano(), 0
for nonce := uint64(0); time.Now().UnixNano() < finish; {
for i := 0; i < 1024; i++ {
binary.BigEndian.PutUint64(buf[56:], nonce)
h = crypto.Keccak256(buf)
firstBit := common.FirstBitSet(common.BigD(h))
if firstBit > bestBit {
e.EnvNonce, bestBit = nonce, firstBit
if target > 0 && bestBit >= target {
return
}
}
nonce++
}
}
}
func (e *Envelope) PoW() float64 {
if e.pow == 0 {
e.calculatePoW(0)
}
return e.pow
}
func (e *Envelope) calculatePoW(diff uint32) {
buf := make([]byte, 64)
h := crypto.Keccak256(e.rlpWithoutNonce())
copy(buf[:32], h)
binary.BigEndian.PutUint64(buf[56:], e.EnvNonce)
h = crypto.Keccak256(buf)
firstBit := common.FirstBitSet(common.BigD(h))
x := math.Pow(2, float64(firstBit))
x /= float64(len(e.Data))
x /= float64(e.TTL + diff)
e.pow = x
}
func (e *Envelope) powToFirstBit(pow float64) int {
x := pow
x *= float64(len(e.Data))
x *= float64(e.TTL)
bits := math.Log2(x)
bits = math.Ceil(bits)
return int(bits)
}
// rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce.
func (e *Envelope) rlpWithoutNonce() []byte {
res, _ := rlp.EncodeToBytes([]interface{}{e.Expiry, e.TTL, e.Topic, e.Salt, e.AESNonce, e.Data})
return res
}
// Hash returns the SHA3 hash of the envelope, calculating it if not yet done.
func (e *Envelope) Hash() common.Hash {
if (e.hash == common.Hash{}) {
encoded, _ := rlp.EncodeToBytes(e)
e.hash = crypto.Keccak256Hash(encoded)
}
return e.hash
}
// DecodeRLP decodes an Envelope from an RLP data stream.
func (e *Envelope) DecodeRLP(s *rlp.Stream) error {
raw, err := s.Raw()
if err != nil {
return err
}
// The decoding of Envelope uses the struct fields but also needs
// to compute the hash of the whole RLP-encoded envelope. This
// type has the same structure as Envelope but is not an
// rlp.Decoder (does not implement DecodeRLP function).
// Only public members will be encoded.
type rlpenv Envelope
if err := rlp.DecodeBytes(raw, (*rlpenv)(e)); err != nil {
return err
}
e.hash = crypto.Keccak256Hash(raw)
return nil
}
// OpenAsymmetric tries to decrypt an envelope, potentially encrypted with a particular key.
func (e *Envelope) OpenAsymmetric(key *ecdsa.PrivateKey) (*ReceivedMessage, error) {
message := &ReceivedMessage{Raw: e.Data}
err := message.decryptAsymmetric(key)
switch err {
case nil:
return message, nil
case ecies.ErrInvalidPublicKey: // addressed to somebody else
return nil, err
default:
return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err)
}
}
// OpenSymmetric tries to decrypt an envelope, potentially encrypted with a particular key.
func (e *Envelope) OpenSymmetric(key []byte) (msg *ReceivedMessage, err error) {
msg = &ReceivedMessage{Raw: e.Data}
err = msg.decryptSymmetric(key, e.Salt, e.AESNonce)
if err != nil {
msg = nil
}
return msg, err
}
// Open tries to decrypt an envelope, and populates the message fields in case of success.
func (e *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) {
if e.isAsymmetric() {
msg, _ = e.OpenAsymmetric(watcher.KeyAsym)
if msg != nil {
msg.Dst = &watcher.KeyAsym.PublicKey
}
} else if e.IsSymmetric() {
msg, _ = e.OpenSymmetric(watcher.KeySym)
if msg != nil {
msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
}
}
if msg != nil {
ok := msg.Validate()
if !ok {
return nil
}
msg.Topic = e.Topic
msg.PoW = e.PoW()
msg.TTL = e.TTL
msg.Sent = e.Expiry - e.TTL
msg.EnvelopeHash = e.Hash()
msg.EnvelopeVersion = e.Ver()
}
return msg
}
// 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/>.
package whisperv5
import (
"crypto/ecdsa"
"sync"
"github.com/ethereum/go-ethereum/common"
)
type Filter struct {
Src *ecdsa.PublicKey // Sender of the message
KeyAsym *ecdsa.PrivateKey // Private Key of recipient
KeySym []byte // Key associated with the Topic
Topics []TopicType // Topics to filter messages with
PoW float64 // Proof of work as described in the Whisper spec
AcceptP2P bool // Indicates whether this filter is interested in direct peer-to-peer messages
SymKeyHash common.Hash // The Keccak256Hash of the symmetric key, needed for optimization
Messages map[common.Hash]*ReceivedMessage
mutex sync.RWMutex
}
type Filters struct {
id int
watchers map[int]*Filter
whisper *Whisper
mutex sync.RWMutex
}
func NewFilters(w *Whisper) *Filters {
return &Filters{
watchers: make(map[int]*Filter),
whisper: w,
}
}
func (fs *Filters) Install(watcher *Filter) int {
if watcher.Messages == nil {
watcher.Messages = make(map[common.Hash]*ReceivedMessage)
}
fs.mutex.Lock()
defer fs.mutex.Unlock()
fs.watchers[fs.id] = watcher
ret := fs.id
fs.id++
return ret
}
func (fs *Filters) Uninstall(id int) {
fs.mutex.Lock()
defer fs.mutex.Unlock()
delete(fs.watchers, id)
}
func (fs *Filters) Get(i int) *Filter {
fs.mutex.RLock()
defer fs.mutex.RUnlock()
return fs.watchers[i]
}
func (fs *Filters) NotifyWatchers(env *Envelope, messageCode uint64) {
fs.mutex.RLock()
var msg *ReceivedMessage
for _, watcher := range fs.watchers {
if messageCode == p2pCode && !watcher.AcceptP2P {
continue
}
match := false
if msg != nil {
match = watcher.MatchMessage(msg)
} else {
match = watcher.MatchEnvelope(env)
if match {
msg = env.Open(watcher)
}
}
if match && msg != nil {
watcher.Trigger(msg)
}
}
fs.mutex.RUnlock() // we need to unlock before calling addDecryptedMessage
if msg != nil {
fs.whisper.addDecryptedMessage(msg)
}
}
func (f *Filter) expectsAsymmetricEncryption() bool {
return f.KeyAsym != nil
}
func (f *Filter) expectsSymmetricEncryption() bool {
return f.KeySym != nil
}
func (f *Filter) Trigger(msg *ReceivedMessage) {
f.mutex.Lock()
defer f.mutex.Unlock()
if _, exist := f.Messages[msg.EnvelopeHash]; !exist {
f.Messages[msg.EnvelopeHash] = msg
}
}
func (f *Filter) Retrieve() (all []*ReceivedMessage) {
f.mutex.Lock()
defer f.mutex.Unlock()
all = make([]*ReceivedMessage, 0, len(f.Messages))
for _, msg := range f.Messages {
all = append(all, msg)
}
f.Messages = make(map[common.Hash]*ReceivedMessage) // delete old messages
return all
}
func (f *Filter) MatchMessage(msg *ReceivedMessage) bool {
if f.PoW > 0 && msg.PoW < f.PoW {
return false
}
if f.Src != nil && !isPubKeyEqual(msg.Src, f.Src) {
return false
}
if f.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
// if Dst match, ignore the topic
return isPubKeyEqual(&f.KeyAsym.PublicKey, msg.Dst)
} else if f.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
// check if that both the key and the topic match
if f.SymKeyHash == msg.SymKeyHash {
for _, t := range f.Topics {
if t == msg.Topic {
return true
}
}
return false
}
}
return false
}
func (f *Filter) MatchEnvelope(envelope *Envelope) bool {
if f.PoW > 0 && envelope.pow < f.PoW {
return false
}
encryptionMethodMatch := false
if f.expectsAsymmetricEncryption() && envelope.isAsymmetric() {
encryptionMethodMatch = true
if f.Topics == nil {
// wildcard
return true
}
} else if f.expectsSymmetricEncryption() && envelope.IsSymmetric() {
encryptionMethodMatch = true
}
if encryptionMethodMatch {
for _, t := range f.Topics {
if t == envelope.Topic {
return true
}
}
}
return false
}
func isPubKeyEqual(a, b *ecdsa.PublicKey) bool {
if !ValidatePublicKey(a) {
return false
} else if !ValidatePublicKey(b) {
return false
}
// the Curve is always the same, just compare the points
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
}
This diff is collapsed.
This diff is collapsed.
// 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/>.
package whisperv5
import (
"bytes"
"math/rand"
"testing"
"github.com/ethereum/go-ethereum/crypto"
)
func copyFromBuf(dst []byte, src []byte, beg int) int {
copy(dst, src[beg:])
return beg + len(dst)
}
func generateMessageParams() (*MessageParams, error) {
buf := make([]byte, 1024)
randomize(buf)
sz := rand.Intn(400)
var p MessageParams
p.TTL = uint32(rand.Intn(1024))
p.Payload = make([]byte, sz)
p.Padding = make([]byte, padSizeLimitUpper)
p.KeySym = make([]byte, aesKeyLength)
var b int
b = copyFromBuf(p.Payload, buf, b)
b = copyFromBuf(p.Padding, buf, b)
b = copyFromBuf(p.KeySym, buf, b)
p.Topic = BytesToTopic(buf[b:])
var err error
p.Src, err = crypto.GenerateKey()
if err != nil {
return nil, err
}
// p.Dst, p.PoW, p.WorkTime are not set
p.PoW = 0.01
return &p, nil
}
func singleMessageTest(x *testing.T, symmetric bool) {
params, err := generateMessageParams()
if err != nil {
x.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
key, err := crypto.GenerateKey()
if err != nil {
x.Errorf("failed GenerateKey with seed %d: %s.", seed, err)
return
}
if !symmetric {
params.KeySym = nil
params.Dst = &key.PublicKey
}
text := make([]byte, 0, 512)
steg := make([]byte, 0, 512)
raw := make([]byte, 0, 1024)
text = append(text, params.Payload...)
steg = append(steg, params.Padding...)
raw = append(raw, params.Padding...)
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
x.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
var decrypted *ReceivedMessage
if symmetric {
decrypted, err = env.OpenSymmetric(params.KeySym)
} else {
decrypted, err = env.OpenAsymmetric(key)
}
if err != nil {
x.Errorf("failed to encrypt with seed %d: %s.", seed, err)
return
}
if !decrypted.Validate() {
x.Errorf("failed to validate with seed %d.", seed)
return
}
padsz := len(decrypted.Padding)
if bytes.Compare(steg[:padsz], decrypted.Padding) != 0 {
x.Errorf("failed with seed %d: compare padding.", seed)
return
}
if bytes.Compare(text, decrypted.Payload) != 0 {
x.Errorf("failed with seed %d: compare payload.", seed)
return
}
if !isMessageSigned(decrypted.Raw[0]) {
x.Errorf("failed with seed %d: unsigned.", seed)
return
}
if len(decrypted.Signature) != signatureLength {
x.Errorf("failed with seed %d: signature len %d.", seed, len(decrypted.Signature))
return
}
if !isPubKeyEqual(decrypted.Src, &params.Src.PublicKey) {
x.Errorf("failed with seed %d: signature mismatch.", seed)
return
}
}
func TestMessageEncryption(x *testing.T) {
InitSingleTest()
var symmetric bool
for i := 0; i < 256; i++ {
singleMessageTest(x, symmetric)
symmetric = !symmetric
}
}
func TestMessageWrap(x *testing.T) {
seed = int64(1777444222)
rand.Seed(seed)
target := 128.0
params, err := generateMessageParams()
if err != nil {
x.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
msg := NewSentMessage(params)
params.TTL = 1
params.WorkTime = 12
params.PoW = target
env, err := msg.Wrap(params)
if err != nil {
x.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
pow := env.PoW()
if pow < target {
x.Errorf("failed Wrap with seed %d: pow < target (%f vs. %f).", seed, pow, target)
return
}
}
func TestMessageSeal(x *testing.T) {
// this test depends on deterministic choice of seed (1976726903)
seed = int64(1976726903)
rand.Seed(seed)
params, err := generateMessageParams()
if err != nil {
x.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
msg := NewSentMessage(params)
params.TTL = 1
aesnonce := make([]byte, 12)
salt := make([]byte, 12)
randomize(aesnonce)
randomize(salt)
env := NewEnvelope(params.TTL, params.Topic, salt, aesnonce, msg)
if err != nil {
x.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
env.Expiry = uint32(seed) // make it deterministic
target := 32.0
params.WorkTime = 4
params.PoW = target
env.Seal(params)
env.calculatePoW(0)
pow := env.PoW()
if pow < target {
x.Errorf("failed Wrap with seed %d: pow < target (%f vs. %f).", seed, pow, target)
return
}
params.WorkTime = 1
params.PoW = 1000000000.0
env.Seal(params)
env.calculatePoW(0)
pow = env.PoW()
if pow < 2*target {
x.Errorf("failed Wrap with seed %d: pow too small %f.", seed, pow)
return
}
}
func TestEnvelopeOpen(x *testing.T) {
InitSingleTest()
var symmetric bool
for i := 0; i < 256; i++ {
singleEnvelopeOpenTest(x, symmetric)
symmetric = !symmetric
}
}
func singleEnvelopeOpenTest(x *testing.T, symmetric bool) {
params, err := generateMessageParams()
if err != nil {
x.Errorf("failed generateMessageParams with seed %d: %s.", seed, err)
return
}
key, err := crypto.GenerateKey()
if err != nil {
x.Errorf("failed GenerateKey with seed %d: %s.", seed, err)
return
}
if !symmetric {
params.KeySym = nil
params.Dst = &key.PublicKey
}
text := make([]byte, 0, 512)
steg := make([]byte, 0, 512)
raw := make([]byte, 0, 1024)
text = append(text, params.Payload...)
steg = append(steg, params.Padding...)
raw = append(raw, params.Padding...)
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
x.Errorf("failed Wrap with seed %d: %s.", seed, err)
return
}
f := Filter{KeyAsym: key, KeySym: params.KeySym}
decrypted := env.Open(&f)
if decrypted == nil {
x.Errorf("failed to open with seed %d.", seed)
return
}
padsz := len(decrypted.Padding)
if bytes.Compare(steg[:padsz], decrypted.Padding) != 0 {
x.Errorf("failed with seed %d: compare padding.", seed)
return
}
if bytes.Compare(text, decrypted.Payload) != 0 {
x.Errorf("failed with seed %d: compare payload.", seed)
return
}
if !isMessageSigned(decrypted.Raw[0]) {
x.Errorf("failed with seed %d: unsigned.", seed)
return
}
if len(decrypted.Signature) != signatureLength {
x.Errorf("failed with seed %d: signature len %d.", seed, len(decrypted.Signature))
return
}
if !isPubKeyEqual(decrypted.Src, &params.Src.PublicKey) {
x.Errorf("failed with seed %d: signature mismatch.", seed)
return
}
if decrypted.isAsymmetricEncryption() == symmetric {
x.Errorf("failed with seed %d: asymmetric %v vs. %v.", seed, decrypted.isAsymmetricEncryption(), symmetric)
return
}
if decrypted.isSymmetricEncryption() != symmetric {
x.Errorf("failed with seed %d: symmetric %v vs. %v.", seed, decrypted.isSymmetricEncryption(), symmetric)
return
}
if !symmetric {
if decrypted.Dst == nil {
x.Errorf("failed with seed %d: dst is nil.", seed)
return
}
if !isPubKeyEqual(decrypted.Dst, &key.PublicKey) {
x.Errorf("failed with seed %d: Dst.", seed)
return
}
}
}
// 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/>.
package whisperv5
import (
"fmt"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/logger/glog"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
set "gopkg.in/fatih/set.v0"
)
// peer represents a whisper protocol peer connection.
type Peer struct {
host *Whisper
peer *p2p.Peer
ws p2p.MsgReadWriter
trusted bool
known *set.Set // Messages already known by the peer to avoid wasting bandwidth
quit chan struct{}
}
// newPeer creates a new whisper peer object, but does not run the handshake itself.
func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *Peer {
return &Peer{
host: host,
peer: remote,
ws: rw,
trusted: false,
known: set.New(),
quit: make(chan struct{}),
}
}
// start initiates the peer updater, periodically broadcasting the whisper packets
// into the network.
func (p *Peer) start() {
go p.update()
glog.V(logger.Debug).Infof("%v: whisper started", p.peer)
}
// stop terminates the peer updater, stopping message forwarding to it.
func (p *Peer) stop() {
close(p.quit)
glog.V(logger.Debug).Infof("%v: whisper stopped", p.peer)
}
// handshake sends the protocol initiation status message to the remote peer and
// verifies the remote status too.
func (p *Peer) handshake() error {
// Send the handshake status message asynchronously
errc := make(chan error, 1)
go func() {
errc <- p2p.Send(p.ws, statusCode, ProtocolVersion)
}()
// Fetch the remote status packet and verify protocol match
packet, err := p.ws.ReadMsg()
if err != nil {
return err
}
if packet.Code != statusCode {
return fmt.Errorf("peer sent %x before status packet", packet.Code)
}
s := rlp.NewStream(packet.Payload, uint64(packet.Size))
peerVersion, err := s.Uint()
if err != nil {
return fmt.Errorf("bad status message: %v", err)
}
if peerVersion != ProtocolVersion {
return fmt.Errorf("protocol version mismatch %d != %d", peerVersion, ProtocolVersion)
}
// Wait until out own status is consumed too
if err := <-errc; err != nil {
return fmt.Errorf("failed to send status packet: %v", err)
}
return nil
}
// update executes periodic operations on the peer, including message transmission
// and expiration.
func (p *Peer) update() {
// Start the tickers for the updates
expire := time.NewTicker(expirationCycle)
transmit := time.NewTicker(transmissionCycle)
// Loop and transmit until termination is requested
for {
select {
case <-expire.C:
p.expire()
case <-transmit.C:
if err := p.broadcast(); err != nil {
glog.V(logger.Info).Infof("%v: broadcast failed: %v", p.peer, err)
return
}
case <-p.quit:
return
}
}
}
// mark marks an envelope known to the peer so that it won't be sent back.
func (peer *Peer) mark(envelope *Envelope) {
peer.known.Add(envelope.Hash())
}
// marked checks if an envelope is already known to the remote peer.
func (peer *Peer) marked(envelope *Envelope) bool {
return peer.known.Has(envelope.Hash())
}
// expire iterates over all the known envelopes in the host and removes all
// expired (unknown) ones from the known list.
func (peer *Peer) expire() {
// Assemble the list of available envelopes
available := set.NewNonTS()
for _, envelope := range peer.host.Envelopes() {
available.Add(envelope.Hash())
}
// Cross reference availability with known status
unmark := make(map[common.Hash]struct{})
peer.known.Each(func(v interface{}) bool {
if !available.Has(v.(common.Hash)) {
unmark[v.(common.Hash)] = struct{}{}
}
return true
})
// Dump all known but unavailable
for hash, _ := range unmark {
peer.known.Remove(hash)
}
}
// broadcast iterates over the collection of envelopes and transmits yet unknown
// ones over the network.
func (p *Peer) broadcast() error {
// Fetch the envelopes and collect the unknown ones
envelopes := p.host.Envelopes()
transmit := make([]*Envelope, 0, len(envelopes))
for _, envelope := range envelopes {
if !p.marked(envelope) {
transmit = append(transmit, envelope)
p.mark(envelope)
}
}
// Transmit the unknown batch (potentially empty)
if err := p2p.Send(p.ws, messagesCode, transmit); err != nil {
return err
}
glog.V(logger.Detail).Infoln(p.peer, "broadcasted", len(transmit), "message(s)")
return nil
}
// 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/>.
package whisperv5
import (
"bytes"
"crypto/ecdsa"
"fmt"
"net"
"sync"
"testing"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover"
"github.com/ethereum/go-ethereum/p2p/nat"
)
var keys []string = []string{
"d49dcf37238dc8a7aac57dc61b9fee68f0a97f062968978b9fafa7d1033d03a9",
"73fd6143c48e80ed3c56ea159fe7494a0b6b393a392227b422f4c3e8f1b54f98",
"119dd32adb1daa7a4c7bf77f847fb28730785aa92947edf42fdd997b54de40dc",
"deeda8709dea935bb772248a3144dea449ffcc13e8e5a1fd4ef20ce4e9c87837",
"5bd208a079633befa349441bdfdc4d85ba9bd56081525008380a63ac38a407cf",
"1d27fb4912002d58a2a42a50c97edb05c1b3dffc665dbaa42df1fe8d3d95c9b5",
"15def52800c9d6b8ca6f3066b7767a76afc7b611786c1276165fbc61636afb68",
"51be6ab4b2dc89f251ff2ace10f3c1cc65d6855f3e083f91f6ff8efdfd28b48c",
"ef1ef7441bf3c6419b162f05da6037474664f198b58db7315a6f4de52414b4a0",
"09bdf6985aabc696dc1fbeb5381aebd7a6421727343872eb2fadfc6d82486fd9",
"15d811bf2e01f99a224cdc91d0cf76cea08e8c67905c16fee9725c9be71185c4",
"2f83e45cf1baaea779789f755b7da72d8857aeebff19362dd9af31d3c9d14620",
"73f04e34ac6532b19c2aae8f8e52f38df1ac8f5cd10369f92325b9b0494b0590",
"1e2e07b69e5025537fb73770f483dc8d64f84ae3403775ef61cd36e3faf162c1",
"8963d9bbb3911aac6d30388c786756b1c423c4fbbc95d1f96ddbddf39809e43a",
"0422da85abc48249270b45d8de38a4cc3c02032ede1fcf0864a51092d58a2f1f",
"8ae5c15b0e8c7cade201fdc149831aa9b11ff626a7ffd27188886cc108ad0fa8",
"acd8f5a71d4aecfcb9ad00d32aa4bcf2a602939b6a9dd071bab443154184f805",
"a285a922125a7481600782ad69debfbcdb0316c1e97c267aff29ef50001ec045",
"28fd4eee78c6cd4bf78f39f8ab30c32c67c24a6223baa40e6f9c9a0e1de7cef5",
"c5cca0c9e6f043b288c6f1aef448ab59132dab3e453671af5d0752961f013fc7",
"46df99b051838cb6f8d1b73f232af516886bd8c4d0ee07af9a0a033c391380fd",
"c6a06a53cbaadbb432884f36155c8f3244e244881b5ee3e92e974cfa166d793f",
"783b90c75c63dc72e2f8d11b6f1b4de54d63825330ec76ee8db34f06b38ea211",
"9450038f10ca2c097a8013e5121b36b422b95b04892232f930a29292d9935611",
"e215e6246ed1cfdcf7310d4d8cdbe370f0d6a8371e4eb1089e2ae05c0e1bc10f",
"487110939ed9d64ebbc1f300adeab358bc58875faf4ca64990fbd7fe03b78f2b",
"824a70ea76ac81366da1d4f4ac39de851c8ac49dca456bb3f0a186ceefa269a5",
"ba8f34fa40945560d1006a328fe70c42e35cc3d1017e72d26864cd0d1b150f15",
"30a5dfcfd144997f428901ea88a43c8d176b19c79dde54cc58eea001aa3d246c",
"de59f7183aca39aa245ce66a05245fecfc7e2c75884184b52b27734a4a58efa2",
"92629e2ff5f0cb4f5f08fffe0f64492024d36f045b901efb271674b801095c5a",
"7184c1701569e3a4c4d2ddce691edd983b81e42e09196d332e1ae2f1e062cff4",
}
const NumNodes = 16 // must not exceed the number of keys (32)
type TestData struct {
counter [NumNodes]int
mutex sync.RWMutex
}
type TestNode struct {
shh *Whisper
id *ecdsa.PrivateKey
server *p2p.Server
filerId int
}
var result TestData
var nodes [NumNodes]*TestNode
var sharedKey []byte = []byte("some arbitrary data here")
var sharedTopic TopicType = TopicType{0xF, 0x1, 0x2, 0}
var expectedMessage []byte = []byte("per rectum ad astra")
// This test does the following:
// 1. creates a chain of whisper nodes,
// 2. installs the filters with shared (predefined) parameters,
// 3. each node sends a number of random (undecryptable) messages,
// 4. first node sends one expected (decryptable) message,
// 5. checks if each node have received and decrypted exactly one message.
func TestSimulation(x *testing.T) {
initialize(x)
for i := 0; i < NumNodes; i++ {
sendMsg(x, false, i)
}
sendMsg(x, true, 0)
checkPropagation(x)
stopServers()
}
func initialize(x *testing.T) {
//glog.SetV(6)
//glog.SetToStderr(true)
var err error
ip := net.IPv4(127, 0, 0, 1)
port0 := 30303
for i := 0; i < NumNodes; i++ {
var node TestNode
node.shh = NewWhisper(nil)
node.shh.test = true
tt := make([]TopicType, 0)
tt = append(tt, sharedTopic)
f := Filter{KeySym: sharedKey, Topics: tt}
node.filerId = node.shh.Watch(&f)
node.id, err = crypto.HexToECDSA(keys[i])
if err != nil {
x.Errorf("failed convert the key: %s.", keys[i])
return
}
port := port0 + i
addr := fmt.Sprintf(":%d", port) // e.g. ":30303"
name := common.MakeName("whisper-go", "2.0")
var peers []*discover.Node
if i > 0 {
peerNodeId := nodes[i-1].id
peerPort := uint16(port - 1)
peerNode := discover.PubkeyID(&peerNodeId.PublicKey)
peer := discover.NewNode(peerNode, ip, peerPort, peerPort)
peers = append(peers, peer)
}
node.server = &p2p.Server{
Config: p2p.Config{
PrivateKey: node.id,
MaxPeers: NumNodes/2 + 1,
Name: name,
Protocols: node.shh.Protocols(),
ListenAddr: addr,
NAT: nat.Any(),
BootstrapNodes: peers,
StaticNodes: peers,
TrustedNodes: peers,
},
}
err = node.server.Start()
if err != nil {
x.Errorf("failed to start server %d.", i)
return
}
nodes[i] = &node
}
}
func stopServers() {
for i := 0; i < NumNodes; i++ {
n := nodes[i]
if n != nil {
n.shh.Unwatch(n.filerId)
n.server.Stop()
}
}
}
func checkPropagation(x *testing.T) {
if x.Failed() {
return
}
const cycle = 100
const iterations = 100
for j := 0; j < iterations; j++ {
time.Sleep(cycle * time.Millisecond)
for i := 0; i < NumNodes; i++ {
f := nodes[i].shh.GetFilter(nodes[i].filerId)
if f == nil {
x.Errorf("failed to get filterId %d from node %d.", nodes[i].filerId, i)
return
}
mail := f.Retrieve()
if !validateMail(x, i, mail) {
return
}
if isTestComplete() {
return
}
}
}
x.Errorf("Test was not complete: timeout %d seconds.", iterations*cycle/1000)
}
func validateMail(x *testing.T, index int, mail []*ReceivedMessage) bool {
var cnt int
for _, m := range mail {
if bytes.Compare(m.Payload, expectedMessage) == 0 {
cnt++
}
}
if cnt == 0 {
// no messages received yet: nothing is wrong
return true
}
if cnt > 1 {
x.Errorf("node %d received %d.", index, cnt)
return false
}
if cnt > 0 {
result.mutex.Lock()
defer result.mutex.Unlock()
result.counter[index] += cnt
if result.counter[index] > 1 {
x.Errorf("node %d accumulated %d.", index, result.counter[index])
return false
}
}
return true
}
func isTestComplete() bool {
result.mutex.RLock()
defer result.mutex.RUnlock()
for i := 0; i < NumNodes; i++ {
if result.counter[i] < 1 {
return false
}
}
for i := 0; i < NumNodes; i++ {
envelopes := nodes[i].shh.Envelopes()
if len(envelopes) < 2 {
return false
}
}
return true
}
func sendMsg(x *testing.T, expected bool, id int) {
if x.Failed() {
return
}
opt := MessageParams{KeySym: sharedKey, Topic: sharedTopic, Payload: expectedMessage, PoW: 0.00000001}
if !expected {
opt.KeySym[0]++
opt.Topic[0]++
opt.Payload = opt.Payload[1:]
}
msg := NewSentMessage(&opt)
envelope, err := msg.Wrap(&opt)
if err != nil {
x.Errorf("failed to seal message.")
return
}
err = nodes[id].shh.Send(envelope)
if err != nil {
x.Errorf("failed to send message.")
return
}
}
func TestPeerBasic(x *testing.T) {
InitSingleTest()
params, err := generateMessageParams()
if err != nil {
x.Errorf("failed 1 with seed %d.", seed)
return
}
params.PoW = 0.001
msg := NewSentMessage(params)
env, err := msg.Wrap(params)
if err != nil {
x.Errorf("failed 2 with seed %d.", seed)
return
}
p := newPeer(nil, nil, nil)
p.mark(env)
if !p.marked(env) {
x.Errorf("failed 3 with seed %d.", seed)
return
}
}
// 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 Topic element. For formal details please see
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics.
package whisperv5
import (
"fmt"
"strings"
"github.com/ethereum/go-ethereum/common"
)
// Topic represents a cryptographically secure, probabilistic partial
// classifications of a message, determined as the first (left) 4 bytes of the
// SHA3 hash of some arbitrary data given by the original author of the message.
type TopicType [TopicLength]byte
func BytesToTopic(b []byte) (t TopicType) {
sz := TopicLength
if x := len(b); x < TopicLength {
sz = x
}
for i := 0; i < sz; i++ {
t[i] = b[i]
}
return t
}
// String converts a topic byte array to a string representation.
func (topic *TopicType) String() string {
return string(common.ToHex(topic[:]))
}
// UnmarshalJSON parses a hex representation to a topic.
func (t *TopicType) UnmarshalJSON(input []byte) error {
length := len(input)
if length >= 2 && input[0] == '"' && input[length-1] == '"' {
input = input[1 : length-1]
}
// strip "0x" for length check
if len(input) > 1 && strings.ToLower(string(input[:2])) == "0x" {
input = input[2:]
}
// validate the length of the input
if len(input) != TopicLength*2 {
return fmt.Errorf("unmarshalJSON failed: topic must be exactly %d bytes", TopicLength)
}
b := common.FromHex(string(input))
if b == nil {
return fmt.Errorf("unmarshalJSON failed: wrong topic format")
}
*t = BytesToTopic(b)
return nil
}
// 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/>.
package whisperv5
import "testing"
var topicStringTests = []struct {
topic TopicType
str string
}{
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, str: "0x00000000"},
{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, str: "0x007f80ff"},
{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, str: "0xff807f00"},
{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, str: "0xf26e7779"},
}
func TestTopicString(x *testing.T) {
for i, tst := range topicStringTests {
s := tst.topic.String()
if s != tst.str {
x.Errorf("failed test %d: have %s, want %s.", i, s, tst.str)
}
}
}
var bytesToTopicTests = []struct {
data []byte
topic TopicType
}{
{topic: TopicType{0x8f, 0x9a, 0x2b, 0x7d}, data: []byte{0x8f, 0x9a, 0x2b, 0x7d}},
{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte{0x00, 0x7f, 0x80, 0xff}},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00, 0x00}},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{0x00, 0x00, 0x00}},
{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte{0x01}},
{topic: TopicType{0x00, 0xfe, 0x00, 0x00}, data: []byte{0x00, 0xfe}},
{topic: TopicType{0xea, 0x1d, 0x43, 0x00}, data: []byte{0xea, 0x1d, 0x43}},
{topic: TopicType{0x6f, 0x3c, 0xb0, 0xdd}, data: []byte{0x6f, 0x3c, 0xb0, 0xdd, 0x0f, 0x00, 0x90}},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte{}},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: nil},
}
func TestBytesToTopic(x *testing.T) {
for i, tst := range bytesToTopicTests {
t := BytesToTopic(tst.data)
if t != tst.topic {
x.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
}
}
}
var unmarshalTestsGood = []struct {
topic TopicType
data []byte
}{
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x00000000")},
{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte("0x007f80ff")},
{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte("0xff807f00")},
{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte("0xf26e7779")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("00000000")},
{topic: TopicType{0x00, 0x80, 0x01, 0x00}, data: []byte("00800100")},
{topic: TopicType{0x00, 0x7f, 0x80, 0xff}, data: []byte("007f80ff")},
{topic: TopicType{0xff, 0x80, 0x7f, 0x00}, data: []byte("ff807f00")},
{topic: TopicType{0xf2, 0x6e, 0x77, 0x79}, data: []byte("f26e7779")},
}
var unmarshalTestsBad = []struct {
topic TopicType
data []byte
}{
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x0000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x000000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0x0000000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("000000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("0000000000")},
{topic: TopicType{0x00, 0x00, 0x00, 0x00}, data: []byte("abcdefg0")},
}
var unmarshalTestsUgly = []struct {
topic TopicType
data []byte
}{
{topic: TopicType{0x01, 0x00, 0x00, 0x00}, data: []byte("00000001")},
}
func TestUnmarshalTestsGood(x *testing.T) {
for i, tst := range unmarshalTestsGood {
var t TopicType
err := t.UnmarshalJSON(tst.data)
if err != nil {
x.Errorf("failed test %d. input: %v.", i, tst.data)
} else if t != tst.topic {
x.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
}
}
}
func TestUnmarshalTestsBad(x *testing.T) {
// in this test UnmarshalJSON() is supposed to fail
for i, tst := range unmarshalTestsBad {
var t TopicType
err := t.UnmarshalJSON(tst.data)
if err == nil {
x.Errorf("failed test %d. input: %v.", i, tst.data)
}
}
}
func TestUnmarshalTestsUgly(x *testing.T) {
// in this test UnmarshalJSON() is NOT supposed to fail, but result should be wrong
for i, tst := range unmarshalTestsUgly {
var t TopicType
err := t.UnmarshalJSON(tst.data)
if err != nil {
x.Errorf("failed test %d. input: %v.", i, tst.data)
} else if t == tst.topic {
x.Errorf("failed test %d: have %v, want %v.", i, t, tst.topic)
}
}
}
This diff is collapsed.
// 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/>.
package whisperv5
import (
"bytes"
"testing"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func TestWhisperBasic(x *testing.T) {
w := NewWhisper(nil)
p := w.Protocols()
shh := p[0]
if shh.Name != ProtocolName {
x.Errorf("failed Protocol Name: %v.", shh.Name)
return
}
if uint64(shh.Version) != ProtocolVersion {
x.Errorf("failed Protocol Version: %v.", shh.Version)
return
}
if shh.Length != NumberOfMessageCodes {
x.Errorf("failed Protocol Length: %v.", shh.Length)
return
}
if shh.Run == nil {
x.Errorf("failed shh.Run.")
return
}
if uint64(w.Version()) != ProtocolVersion {
x.Errorf("failed whisper Version: %v.", shh.Version)
return
}
if w.GetFilter(0) != nil {
x.Errorf("failed GetFilter.")
return
}
peerID := make([]byte, 64)
randomize(peerID)
peer, err := w.getPeer(peerID)
if peer != nil {
x.Errorf("failed GetPeer.")
return
}
err = w.MarkPeerTrusted(peerID)
if err == nil {
x.Errorf("failed MarkPeerTrusted.")
return
}
err = w.RequestHistoricMessages(peerID, peerID)
if err == nil {
x.Errorf("failed RequestHistoricMessages.")
return
}
err = w.SendP2PMessage(peerID, nil)
if err == nil {
x.Errorf("failed SendP2PMessage.")
return
}
exist := w.HasSymKey("non-existing")
if exist {
x.Errorf("failed HasSymKey.")
return
}
key := w.GetSymKey("non-existing")
if key != nil {
x.Errorf("failed GetSymKey.")
return
}
mail := w.Envelopes()
if len(mail) != 0 {
x.Errorf("failed w.Envelopes().")
return
}
m := w.Messages(0)
if len(m) != 0 {
x.Errorf("failed w.Messages.")
return
}
var derived []byte
ver := uint64(0xDEADBEEF)
derived, err = deriveKeyMaterial(peerID, ver)
if err != unknownVersionError(ver) {
x.Errorf("failed deriveKeyMaterial 1 with param = %v: %s.", peerID, err)
return
}
derived, err = deriveKeyMaterial(peerID, 0)
if err != nil {
x.Errorf("failed deriveKeyMaterial 2 with param = %v: %s.", peerID, err)
return
}
if !validateSymmetricKey(derived) {
x.Errorf("failed validateSymmetricKey with param = %v.", derived)
return
}
if containsOnlyZeros(derived) {
x.Errorf("failed containsOnlyZeros with param = %v.", derived)
return
}
buf := []byte{0xFF, 0xE5, 0x80, 0x2, 0}
le := bytesToIntLittleEndian(buf)
be := BytesToIntBigEndian(buf)
if le != uint64(0x280e5ff) {
x.Errorf("failed bytesToIntLittleEndian: %d.", le)
return
}
if be != uint64(0xffe5800200) {
x.Errorf("failed BytesToIntBigEndian: %d.", be)
return
}
pk := w.NewIdentity()
if !validatePrivateKey(pk) {
x.Errorf("failed validatePrivateKey: %v.", pk)
return
}
if !ValidatePublicKey(&pk.PublicKey) {
x.Errorf("failed ValidatePublicKey: %v.", pk)
return
}
}
func TestWhisperIdentityManagement(x *testing.T) {
w := NewWhisper(nil)
id1 := w.NewIdentity()
id2 := w.NewIdentity()
pub1 := common.ToHex(crypto.FromECDSAPub(&id1.PublicKey))
pub2 := common.ToHex(crypto.FromECDSAPub(&id2.PublicKey))
pk1 := w.GetIdentity(pub1)
pk2 := w.GetIdentity(pub2)
if !w.HasIdentity(pub1) {
x.Errorf("failed HasIdentity 1.")
return
}
if !w.HasIdentity(pub2) {
x.Errorf("failed HasIdentity 2.")
return
}
if pk1 != id1 {
x.Errorf("failed GetIdentity 3.")
return
}
if pk2 != id2 {
x.Errorf("failed GetIdentity 4.")
return
}
// Delete one identity
w.DeleteIdentity(pub1)
pk1 = w.GetIdentity(pub1)
pk2 = w.GetIdentity(pub2)
if w.HasIdentity(pub1) {
x.Errorf("failed HasIdentity 11.")
return
}
if !w.HasIdentity(pub2) {
x.Errorf("failed HasIdentity 12.")
return
}
if pk1 != nil {
x.Errorf("failed GetIdentity 13.")
return
}
if pk2 != id2 {
x.Errorf("failed GetIdentity 14.")
return
}
// Delete again non-existing identity
w.DeleteIdentity(pub1)
pk1 = w.GetIdentity(pub1)
pk2 = w.GetIdentity(pub2)
if w.HasIdentity(pub1) {
x.Errorf("failed HasIdentity 21.")
return
}
if !w.HasIdentity(pub2) {
x.Errorf("failed HasIdentity 22.")
return
}
if pk1 != nil {
x.Errorf("failed GetIdentity 23.")
return
}
if pk2 != id2 {
x.Errorf("failed GetIdentity 24.")
return
}
// Delete second identity
w.DeleteIdentity(pub2)
pk1 = w.GetIdentity(pub1)
pk2 = w.GetIdentity(pub2)
if w.HasIdentity(pub1) {
x.Errorf("failed HasIdentity 31.")
return
}
if w.HasIdentity(pub2) {
x.Errorf("failed HasIdentity 32.")
return
}
if pk1 != nil {
x.Errorf("failed GetIdentity 33.")
return
}
if pk2 != nil {
x.Errorf("failed GetIdentity 34.")
return
}
}
func TestWhisperSymKeyManagement(x *testing.T) {
InitSingleTest()
var k1, k2 []byte
w := NewWhisper(nil)
id1 := string("arbitrary-string-1")
id2 := string("arbitrary-string-2")
err := w.GenerateSymKey(id1)
if err != nil {
x.Errorf("failed test case 1 with seed %d: %s.", seed, err)
return
}
k1 = w.GetSymKey(id1)
k2 = w.GetSymKey(id2)
if !w.HasSymKey(id1) {
x.Errorf("failed HasIdentity 2.")
return
}
if w.HasSymKey(id2) {
x.Errorf("failed HasIdentity 3.")
return
}
if k1 == nil {
x.Errorf("failed GetIdentity 4.")
return
}
if k2 != nil {
x.Errorf("failed GetIdentity 5.")
return
}
// add existing id, nothing should change
randomKey := make([]byte, 16)
randomize(randomKey)
err = w.AddSymKey(id1, randomKey)
if err == nil {
x.Errorf("failed test case 10 with seed %d.", seed)
return
}
k1 = w.GetSymKey(id1)
k2 = w.GetSymKey(id2)
if !w.HasSymKey(id1) {
x.Errorf("failed HasIdentity 12.")
return
}
if w.HasSymKey(id2) {
x.Errorf("failed HasIdentity 13.")
return
}
if k1 == nil {
x.Errorf("failed GetIdentity 14.")
return
}
if bytes.Compare(k1, randomKey) == 0 {
x.Errorf("failed GetIdentity 15: k1 == randomKey.")
return
}
if k2 != nil {
x.Errorf("failed GetIdentity 16.")
return
}
err = w.AddSymKey(id2, randomKey) // add non-existing (yet)
if err != nil {
x.Errorf("failed test case 21 with seed %d: %s.", seed, err)
return
}
k1 = w.GetSymKey(id1)
k2 = w.GetSymKey(id2)
if !w.HasSymKey(id1) {
x.Errorf("failed HasIdentity 22.")
return
}
if !w.HasSymKey(id2) {
x.Errorf("failed HasIdentity 23.")
return
}
if k1 == nil {
x.Errorf("failed GetIdentity 24.")
return
}
if k2 == nil {
x.Errorf("failed GetIdentity 25.")
return
}
if bytes.Compare(k1, k2) == 0 {
x.Errorf("failed GetIdentity 26.")
return
}
if bytes.Compare(k1, randomKey) == 0 {
x.Errorf("failed GetIdentity 27.")
return
}
if len(k1) != aesKeyLength {
x.Errorf("failed GetIdentity 28.")
return
}
if len(k2) != aesKeyLength {
x.Errorf("failed GetIdentity 29.")
return
}
w.DeleteSymKey(id1)
k1 = w.GetSymKey(id1)
k2 = w.GetSymKey(id2)
if w.HasSymKey(id1) {
x.Errorf("failed HasIdentity 31.")
return
}
if !w.HasSymKey(id2) {
x.Errorf("failed HasIdentity 32.")
return
}
if k1 != nil {
x.Errorf("failed GetIdentity 33.")
return
}
if k2 == nil {
x.Errorf("failed GetIdentity 34.")
return
}
w.DeleteSymKey(id1)
w.DeleteSymKey(id2)
k1 = w.GetSymKey(id1)
k2 = w.GetSymKey(id2)
if w.HasSymKey(id1) {
x.Errorf("failed HasIdentity 41.")
return
}
if w.HasSymKey(id2) {
x.Errorf("failed HasIdentity 42.")
return
}
if k1 != nil {
x.Errorf("failed GetIdentity 43.")
return
}
if k2 != nil {
x.Errorf("failed GetIdentity 44.")
return
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment