2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
2015-07-22 19:48:40 +03:00
|
|
|
// This file is part of the go-ethereum library.
|
2015-07-07 03:54:22 +03:00
|
|
|
//
|
2015-07-23 19:35:11 +03:00
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
2015-07-07 03:54:22 +03:00
|
|
|
// 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.
|
|
|
|
//
|
2015-07-22 19:48:40 +03:00
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
2015-07-07 03:54:22 +03:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-07-22 19:48:40 +03:00
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2015-07-07 03:54:22 +03:00
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
2015-07-22 19:48:40 +03:00
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
2015-07-07 03:54:22 +03:00
|
|
|
|
2016-10-29 15:11:37 +03:00
|
|
|
package whisperv2
|
2014-12-10 15:17:32 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"testing"
|
2015-04-21 11:43:11 +03:00
|
|
|
"time"
|
2014-12-10 15:17:32 +02:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-09-29 20:37:44 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/secp256k1"
|
2014-12-10 15:17:32 +02:00
|
|
|
)
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
// Tests whether a message can be wrapped without any identity or encryption.
|
|
|
|
func TestMessageSimpleWrap(t *testing.T) {
|
|
|
|
payload := []byte("hello world")
|
|
|
|
|
|
|
|
msg := NewMessage(payload)
|
2015-04-14 15:16:02 +03:00
|
|
|
if _, err := msg.Wrap(DefaultPoW, Options{}); err != nil {
|
2015-04-10 15:53:21 +03:00
|
|
|
t.Fatalf("failed to wrap message: %v", err)
|
|
|
|
}
|
2015-04-13 11:31:51 +03:00
|
|
|
if msg.Flags&signatureFlag != 0 {
|
|
|
|
t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, 0)
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
if len(msg.Signature) != 0 {
|
|
|
|
t.Fatalf("signature found for simple wrapping: 0x%x", msg.Signature)
|
|
|
|
}
|
|
|
|
if bytes.Compare(msg.Payload, payload) != 0 {
|
|
|
|
t.Fatalf("payload mismatch after wrapping: have 0x%x, want 0x%x", msg.Payload, payload)
|
|
|
|
}
|
2015-04-21 11:43:11 +03:00
|
|
|
if msg.TTL/time.Second != DefaultTTL/time.Second {
|
|
|
|
t.Fatalf("message TTL mismatch: have %v, want %v", msg.TTL, DefaultTTL)
|
|
|
|
}
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tests whether a message can be signed, and wrapped in plain-text.
|
|
|
|
func TestMessageCleartextSignRecover(t *testing.T) {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create crypto key: %v", err)
|
|
|
|
}
|
|
|
|
payload := []byte("hello world")
|
|
|
|
|
|
|
|
msg := NewMessage(payload)
|
2015-04-14 15:16:02 +03:00
|
|
|
if _, err := msg.Wrap(DefaultPoW, Options{
|
2015-04-10 15:53:21 +03:00
|
|
|
From: key,
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failed to sign message: %v", err)
|
|
|
|
}
|
2015-04-13 11:31:51 +03:00
|
|
|
if msg.Flags&signatureFlag != signatureFlag {
|
|
|
|
t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, signatureFlag)
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
if bytes.Compare(msg.Payload, payload) != 0 {
|
|
|
|
t.Fatalf("payload mismatch after signing: have 0x%x, want 0x%x", msg.Payload, payload)
|
|
|
|
}
|
2014-12-10 15:17:32 +02:00
|
|
|
|
|
|
|
pubKey := msg.Recover()
|
2015-04-10 15:53:21 +03:00
|
|
|
if pubKey == nil {
|
|
|
|
t.Fatalf("failed to recover public key")
|
|
|
|
}
|
2015-09-29 20:37:44 +03:00
|
|
|
p1 := elliptic.Marshal(secp256k1.S256(), key.PublicKey.X, key.PublicKey.Y)
|
|
|
|
p2 := elliptic.Marshal(secp256k1.S256(), pubKey.X, pubKey.Y)
|
2014-12-10 15:17:32 +02:00
|
|
|
if !bytes.Equal(p1, p2) {
|
2015-04-10 15:53:21 +03:00
|
|
|
t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1)
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
// Tests whether a message can be encrypted and decrypted using an anonymous
|
|
|
|
// sender (i.e. no signature).
|
|
|
|
func TestMessageAnonymousEncryptDecrypt(t *testing.T) {
|
|
|
|
key, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create recipient crypto key: %v", err)
|
|
|
|
}
|
|
|
|
payload := []byte("hello world")
|
2014-12-10 15:17:32 +02:00
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
msg := NewMessage(payload)
|
2015-04-14 15:16:02 +03:00
|
|
|
envelope, err := msg.Wrap(DefaultPoW, Options{
|
2015-04-10 15:53:21 +03:00
|
|
|
To: &key.PublicKey,
|
2014-12-10 15:17:32 +02:00
|
|
|
})
|
|
|
|
if err != nil {
|
2015-04-10 15:53:21 +03:00
|
|
|
t.Fatalf("failed to encrypt message: %v", err)
|
|
|
|
}
|
2015-04-13 11:31:51 +03:00
|
|
|
if msg.Flags&signatureFlag != 0 {
|
|
|
|
t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, 0)
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
if len(msg.Signature) != 0 {
|
|
|
|
t.Fatalf("signature found for anonymous message: 0x%x", msg.Signature)
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
out, err := envelope.Open(key)
|
2014-12-10 15:17:32 +02:00
|
|
|
if err != nil {
|
2015-04-10 15:53:21 +03:00
|
|
|
t.Fatalf("failed to open encrypted message: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(out.Payload, payload) {
|
2016-04-15 12:06:57 +03:00
|
|
|
t.Errorf("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload)
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests whether a message can be properly signed and encrypted.
|
|
|
|
func TestMessageFullCrypto(t *testing.T) {
|
|
|
|
fromKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create sender crypto key: %v", err)
|
|
|
|
}
|
|
|
|
toKey, err := crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to create recipient crypto key: %v", err)
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
payload := []byte("hello world")
|
|
|
|
msg := NewMessage(payload)
|
2015-04-14 15:16:02 +03:00
|
|
|
envelope, err := msg.Wrap(DefaultPoW, Options{
|
2015-04-10 15:53:21 +03:00
|
|
|
From: fromKey,
|
|
|
|
To: &toKey.PublicKey,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to encrypt message: %v", err)
|
|
|
|
}
|
2015-04-13 11:31:51 +03:00
|
|
|
if msg.Flags&signatureFlag != signatureFlag {
|
|
|
|
t.Fatalf("signature flag mismatch: have %d, want %d", msg.Flags&signatureFlag, signatureFlag)
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
if len(msg.Signature) == 0 {
|
|
|
|
t.Fatalf("no signature found for signed message")
|
|
|
|
}
|
|
|
|
|
|
|
|
out, err := envelope.Open(toKey)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to open encrypted message: %v", err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(out.Payload, payload) {
|
2016-04-15 12:06:57 +03:00
|
|
|
t.Errorf("payload mismatch: have 0x%x, want 0x%x", out.Payload, payload)
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pubKey := out.Recover()
|
|
|
|
if pubKey == nil {
|
|
|
|
t.Fatalf("failed to recover public key")
|
|
|
|
}
|
2015-09-29 20:37:44 +03:00
|
|
|
p1 := elliptic.Marshal(secp256k1.S256(), fromKey.PublicKey.X, fromKey.PublicKey.Y)
|
|
|
|
p2 := elliptic.Marshal(secp256k1.S256(), pubKey.X, pubKey.Y)
|
2015-04-10 15:53:21 +03:00
|
|
|
if !bytes.Equal(p1, p2) {
|
|
|
|
t.Fatalf("public key mismatch: have 0x%x, want 0x%x", p2, p1)
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
|
|
|
}
|