Skip to content

Commit

Permalink
feature(wire/net/simple & backend/sim/wire): change wire implementati…
Browse files Browse the repository at this point in the history
…on to fit new interface + add tests for authentification

Signed-off-by: Minh Huy Tran <[email protected]>
  • Loading branch information
NhoxxKienn committed Feb 16, 2024
1 parent ee69526 commit 284cebd
Show file tree
Hide file tree
Showing 9 changed files with 400 additions and 64 deletions.
5 changes: 5 additions & 0 deletions backend/sim/wire/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ func (acc *Account) Address() wire.Address {
return acc.addr
}

// Sign signs the given message with the account's private key.
func (acc *Account) Sign(msg []byte) ([]byte, error) {
return []byte("Authenticate"), nil
}

// NewRandomAccount generates a new random account.
func NewRandomAccount(rng *rand.Rand) *Account {
return &Account{
Expand Down
9 changes: 9 additions & 0 deletions backend/sim/wire/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package wire

import (
"bytes"
"errors"
"math/rand"

"perun.network/go-perun/wire"
Expand Down Expand Up @@ -62,6 +63,14 @@ func (a Address) Cmp(b wire.Address) int {
return bytes.Compare(a[:], bTyped[:])
}

// Verify verifies a signature.
func (a Address) Verify(msg, sig []byte) error {
if !bytes.Equal(sig, []byte("Authenticate")) {
return errors.New("invalid signature")
}
return nil
}

// NewRandomAddress returns a new random peer address.
func NewRandomAddress(rng *rand.Rand) *Address {
addr := Address{}
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,13 @@ require (
go.uber.org/goleak v1.1.11
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c
google.golang.org/protobuf v1.23.0
google.golang.org/protobuf v1.32.0
polycry.pt/poly-go v0.0.0-20220222131629-aa4bdbaab60b
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/go-cmp v0.5.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.1.1 // indirect
Expand Down
9 changes: 6 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ github.com/golang/protobuf v1.4.0-rc.2/go.mod h1:LlEzMj4AhA7rCAGe4KMBDvJI+AwstrU
github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:WU3c8KckQ9AFe+yFwt9sWVRKCVIyN9cPHBJSNnbL67w=
github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0=
github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU=
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.4 h1:L8R9j+yAqZuZjsqh/z+F1NCffTKKLShY6zXTItVIZ8M=
github.com/google/go-cmp v0.5.4/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
Expand Down Expand Up @@ -114,8 +115,10 @@ google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
Expand Down
39 changes: 30 additions & 9 deletions wire/net/simple/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,52 @@
package simple

import (
"crypto"
crypto_rand "crypto/rand"
"crypto/rsa"
"crypto/sha256"
"math/rand"

"github.com/pkg/errors"
"perun.network/go-perun/wire"
)

// Account is a wire account.
type Account struct {
addr wire.Address
}

// NewAccount creates a new account.
func NewAccount(addr *Address) *Account {
return &Account{
addr: addr,
}
addr wire.Address
privateKey *rsa.PrivateKey
}

// Address returns the account's address.
func (acc *Account) Address() wire.Address {
return acc.addr
}

// Sign signs the given message with the account's private key.
func (acc *Account) Sign(msg []byte) ([]byte, error) {
if acc.privateKey == nil {
return nil, errors.New("private key is nil")
}
hashed := sha256.Sum256([]byte(msg))

Check failure on line 44 in wire/net/simple/account.go

View workflow job for this annotation

GitHub Actions / Vetting

unnecessary conversion (unconvert)
signature, err := rsa.SignPKCS1v15(crypto_rand.Reader, acc.privateKey, crypto.SHA256, hashed[:])
if err != nil {
return nil, err
}
return signature, nil
}

// NewRandomAccount generates a new random account.
func NewRandomAccount(rng *rand.Rand) *Account {
privateKey, err := rsa.GenerateKey(rng, 2048)

Check failure on line 54 in wire/net/simple/account.go

View workflow job for this annotation

GitHub Actions / Vetting

mnd: Magic number: 2048, in <argument> detected (gomnd)
if err != nil {
panic(err)
}

address := NewRandomAddress(rng)
address.PublicKey = &privateKey.PublicKey

return &Account{
addr: NewRandomAddress(rng),
addr: address,
privateKey: privateKey,
}
}
116 changes: 97 additions & 19 deletions wire/net/simple/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,54 +16,120 @@ package simple

import (
"bytes"
"crypto"
"crypto/rsa"
"crypto/sha256"
"encoding/binary"
"encoding/gob"
"math/rand"

"perun.network/go-perun/wire"
)

// Address is a wire address.
type Address string
type Address struct {
Name string
PublicKey *rsa.PublicKey // Public key for verifying signatures

var _ wire.Address = NewAddress("")
}

var _ wire.Address = (*Address)(nil)

// NewAddress returns a new address.
func NewAddress(host string) *Address {
a := Address(host)
return &a
return &Address{
Name: host,
PublicKey: nil,
}
}

// MarshalBinary marshals the address to binary.
func (a Address) MarshalBinary() ([]byte, error) {
buf := make([]byte, len(a))
copy(buf, []byte(a))
return buf, nil
func (a *Address) MarshalBinary() ([]byte, error) {
// Initialize a buffer to hold the binary data
var buf bytes.Buffer

// Encode the length of the name string and the name itself
nameLen := uint16(len(a.Name))
if err := binary.Write(&buf, binary.BigEndian, nameLen); err != nil {
return nil, err
}
if _, err := buf.WriteString(a.Name); err != nil {
return nil, err
}

// If the public key is not nil, encode it using gob
if a.PublicKey != nil {
enc := gob.NewEncoder(&buf)
if err := enc.Encode(a.PublicKey); err != nil {
return nil, err
}
}

// Return the binary representation
return buf.Bytes(), nil
}

// UnmarshalBinary unmarshals an address from binary.
func (a *Address) UnmarshalBinary(data []byte) error {
buf := make([]byte, len(data))
copy(buf, data)
*a = Address(buf)
// Initialize a buffer with the binary data
buf := bytes.NewReader(data)

// Decode the length of the name string
var nameLen uint16
if err := binary.Read(buf, binary.BigEndian, &nameLen); err != nil {
return err
}

// Read the name string from the buffer
nameBytes := make([]byte, nameLen)
if _, err := buf.Read(nameBytes); err != nil {
return err
}
a.Name = string(nameBytes)

// If there's remaining data, decode the public key using gob
if buf.Len() > 0 {
dec := gob.NewDecoder(buf)
if err := dec.Decode(&a.PublicKey); err != nil {
return err
}
}

return nil
}

// Equal returns whether the two addresses are equal.
func (a Address) Equal(b wire.Address) bool {
func (a *Address) Equal(b wire.Address) bool {
bTyped, ok := b.(*Address)
if !ok {
return false
}
return a == *bTyped
if a.PublicKey == nil {
return a.Name == bTyped.Name && bTyped.PublicKey == nil
}

return a.Name == bTyped.Name && a.PublicKey.Equal(bTyped.PublicKey)
}

// Cmp compares the byte representation of two addresses. For `a.Cmp(b)`
// returns -1 if a < b, 0 if a == b, 1 if a > b.
func (a Address) Cmp(b wire.Address) int {
// Cmp compares the byte representation of two addresses.
func (a *Address) Cmp(b wire.Address) int {
bTyped, ok := b.(*Address)
if !ok {
panic("wrong type")
}
return bytes.Compare([]byte(a), []byte(*bTyped))
if cmp := bytes.Compare([]byte(a.Name), []byte(bTyped.Name)); cmp != 0 {
return cmp
}

bytesA, err := a.MarshalBinary()
if err != nil {
panic(err)
}
bytesB, err := bTyped.MarshalBinary()
if err != nil {
panic(err)
}
return bytes.Compare(bytesA, bytesB)
}

// NewRandomAddress returns a new random peer address.
Expand All @@ -75,6 +141,18 @@ func NewRandomAddress(rng *rand.Rand) *Address {
panic(err)
}

a := Address(d)
return &a
a := &Address{
Name: string(d),
}
return a
}

// Verify verifies a message signature.
func (a *Address) Verify(msg []byte, sig []byte) error {
hashed := sha256.Sum256(msg)
err := rsa.VerifyPKCS1v15(a.PublicKey, crypto.SHA256, hashed[:], sig)
if err != nil {
return err
}
return nil
}
Loading

0 comments on commit 284cebd

Please sign in to comment.