Skip to content
This repository has been archived by the owner on Aug 2, 2021. It is now read-only.

pss: Modularize crypto and remove Whisper. Step 1 - isolate whisper code #1698

Merged
merged 8 commits into from
Sep 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions pss/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"fmt"

"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rpc"
"github.com/ethersphere/swarm/log"
Expand Down Expand Up @@ -118,13 +117,13 @@ func (pssapi *API) BaseAddr() (PssAddress, error) {
// Retrieves the node's public key in hex form
func (pssapi *API) GetPublicKey() (keybytes hexutil.Bytes) {
key := pssapi.Pss.PublicKey()
keybytes = crypto.FromECDSAPub(key)
keybytes = pssapi.Pss.Crypto.FromECDSAPub(key)
return keybytes
}

// Set Public key to associate with a particular Pss peer
func (pssapi *API) SetPeerPublicKey(pubkey hexutil.Bytes, topic Topic, addr PssAddress) error {
pk, err := crypto.UnmarshalPubkey(pubkey)
pk, err := pssapi.Pss.Crypto.UnmarshalPubkey(pubkey)
if err != nil {
return fmt.Errorf("Cannot unmarshal pubkey: %x", pubkey)
}
Expand Down
11 changes: 4 additions & 7 deletions pss/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import (
"github.com/ethereum/go-ethereum/p2p/simulations"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
"github.com/ethereum/go-ethereum/rpc"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"github.com/ethersphere/swarm/network"
"github.com/ethersphere/swarm/pss"
"github.com/ethersphere/swarm/state"
Expand All @@ -49,8 +48,7 @@ type protoCtrl struct {
var (
debugdebugflag = flag.Bool("vv", false, "veryverbose")
debugflag = flag.Bool("v", false, "verbose")
w *whisper.Whisper
wapi *whisper.PublicWhisperAPI
cryptoUtils pss.CryptoUtils
// custom logging
psslogmain log.Logger
pssprotocols map[string]*protoCtrl
Expand Down Expand Up @@ -78,8 +76,7 @@ func init() {
h := log.CallerFileHandler(hf)
log.Root().SetHandler(h)

w = whisper.New(&whisper.DefaultConfig)
wapi = whisper.NewPublicWhisperAPI(w)
cryptoUtils = pss.NewCryptoUtils()

pssprotocols = make(map[string]*protoCtrl)
}
Expand Down Expand Up @@ -250,11 +247,11 @@ func newServices() adapters.Services {
"pss": func(ctx *adapters.ServiceContext) (node.Service, error) {
ctxlocal, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
keys, err := wapi.NewKeyPair(ctxlocal)
keys, err := cryptoUtils.NewKeyPair(ctxlocal)
if err != nil {
return nil, err
}
privkey, err := w.GetPrivateKey(keys)
privkey, err := cryptoUtils.GetPrivateKey(keys)
if err != nil {
return nil, err
}
Expand Down
104 changes: 104 additions & 0 deletions pss/crypto.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright 2019 The Swarm Authors
// This file is part of the Swarm library.
//
// The Swarm 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 Swarm 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 Swarm library. If not, see <http://www.gnu.org/licenses/>.
package pss

import (
"context"
"crypto/ecdsa"

ethCrypto "github.com/ethereum/go-ethereum/crypto"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
)

var cryptoBackend defaultCryptoBackend
nolash marked this conversation as resolved.
Show resolved Hide resolved

type CryptoBackend interface {
GetSymKey(id string) ([]byte, error)
GenerateSymKey() (string, error)
AddSymKeyDirect(bytes []byte) (string, error)
FromECDSAPub(pub *ecdsa.PublicKey) []byte
UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error)
CompressPubkey(pubkey *ecdsa.PublicKey) []byte
}

//Used only in tests
type CryptoUtils interface {
GenerateKey() (*ecdsa.PrivateKey, error)
NewKeyPair(ctx context.Context) (string, error)
GetPrivateKey(id string) (*ecdsa.PrivateKey, error)
}

type defaultCryptoBackend struct {
whisper *whisper.Whisper
wapi *whisper.PublicWhisperAPI
}

func NewCryptoBackend() CryptoBackend {
w := whisper.New(&whisper.DefaultConfig)
cryptoBackend = defaultCryptoBackend{
whisper: w,
wapi: whisper.NewPublicWhisperAPI(w),
}
return &cryptoBackend
}

func NewCryptoUtils() CryptoUtils {
if cryptoBackend.whisper == nil {
NewCryptoBackend()
}
return &cryptoBackend
}

func (crypto *defaultCryptoBackend) GetSymKey(id string) ([]byte, error) {
return crypto.whisper.GetSymKey(id)
}

func (crypto *defaultCryptoBackend) GenerateSymKey() (string, error) {
return crypto.whisper.GenerateSymKey()
}

func (crypto *defaultCryptoBackend) AddSymKeyDirect(bytes []byte) (string, error) {
return crypto.whisper.AddSymKeyDirect(bytes)
}

// FromECDSA exports a public key into a binary dump.
func (crypto *defaultCryptoBackend) FromECDSAPub(pub *ecdsa.PublicKey) []byte {
return ethCrypto.FromECDSAPub(pub)
}

// CompressPubkey encodes a public key to the 33-byte compressed format.
func (crypto *defaultCryptoBackend) CompressPubkey(pubkey *ecdsa.PublicKey) []byte {
return ethCrypto.CompressPubkey(pubkey)
}

// UnmarshalPubkey converts bytes to a secp256k1 public key.
func (crypto *defaultCryptoBackend) UnmarshalPubkey(pub []byte) (*ecdsa.PublicKey, error) {
return ethCrypto.UnmarshalPubkey(pub)
}

// CryptoUtils

func (crypto *defaultCryptoBackend) GenerateKey() (*ecdsa.PrivateKey, error) {
return ethCrypto.GenerateKey()
}

func (crypto *defaultCryptoBackend) NewKeyPair(ctx context.Context) (string, error) {
return crypto.wapi.NewKeyPair(ctx)
}

func (crypto *defaultCryptoBackend) GetPrivateKey(id string) (*ecdsa.PrivateKey, error) {
return crypto.whisper.GetPrivateKey(id)
}
6 changes: 3 additions & 3 deletions pss/forwarding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ import (
"testing"
"time"

"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/enode"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"github.com/ethersphere/swarm/network"
"github.com/ethersphere/swarm/p2p/protocols"
"github.com/ethersphere/swarm/pot"
Expand All @@ -26,6 +24,8 @@ type testCase struct {
errors string
}

var crypto CryptoUtils = NewCryptoUtils()

// the purpose of this test is to see that pss.forward() function correctly
// selects the peers for message forwarding, depending on the message address
// and kademlia constellation.
Expand Down Expand Up @@ -351,7 +351,7 @@ func newTestMsg(addr []byte) *PssMsg {
msg := newPssMsg(&msgParams{})
msg.To = addr[:]
msg.Expire = uint32(time.Now().Add(time.Second * 60).Unix())
msg.Payload = &whisper.Envelope{
msg.Payload = &envelope{
Topic: [4]byte{},
Data: []byte("i have nothing to hide"),
}
Expand Down
3 changes: 1 addition & 2 deletions pss/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp"
"github.com/ethereum/go-ethereum/rpc"
Expand Down Expand Up @@ -325,7 +324,7 @@ func (ctl *HandshakeController) registerSymKeyUse(symkeyid string) error {
}
symKey.count++

receiver := common.ToHex(crypto.FromECDSAPub(ctl.pss.PublicKey()))
receiver := common.ToHex(ctl.pss.Crypto.FromECDSAPub(ctl.pss.PublicKey()))
log.Trace("increment symkey recv use", "symsymkeyid", symkeyid, "count", symKey.count, "limit", symKey.limit, "receiver", receiver)

return nil
Expand Down
Loading