Skip to content

Commit

Permalink
Merge pull request netbirdio#23 from wiretrustee/test-signal-encryption
Browse files Browse the repository at this point in the history
test: add signal encryption test
  • Loading branch information
braginini authored Jun 4, 2021
2 parents 5927ca2 + ae4cb51 commit 248b786
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 7 deletions.
21 changes: 21 additions & 0 deletions .github/workflows/golang-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
on:
push:
branches:
- main
pull_request:
name: Test
jobs:
test:
strategy:
matrix:
go-version: [1.16.x]
runs-on: ubuntu-latest
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Test
run: go test ./...
10 changes: 5 additions & 5 deletions signal/encryption.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,24 @@ import (
// Wireguard keys are used for encryption

// Encrypt encrypts a message using local Wireguard private key and remote peer's public key.
func Encrypt(msg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) {
func Encrypt(msg []byte, peerPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) {
nonce, err := genNonce()
if err != nil {
return nil, err
}
return box.Seal(nonce[:], msg, nonce, toByte32(peersPublicKey), toByte32(privateKey)), nil
return box.Seal(nonce[:], msg, nonce, toByte32(peerPublicKey), toByte32(privateKey)), nil
}

// Decrypt decrypts a message that has been encrypted by the remote peer using Wireguard private key and remote peer's public key.
func Decrypt(encryptedMsg []byte, peersPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) {
func Decrypt(encryptedMsg []byte, peerPublicKey wgtypes.Key, privateKey wgtypes.Key) ([]byte, error) {
nonce, err := genNonce()
if err != nil {
return nil, err
}
copy(nonce[:], encryptedMsg[:24])
opened, ok := box.Open(nil, encryptedMsg[24:], nonce, toByte32(peersPublicKey), toByte32(privateKey))
opened, ok := box.Open(nil, encryptedMsg[24:], nonce, toByte32(peerPublicKey), toByte32(privateKey))
if !ok {
return nil, fmt.Errorf("failed to decrypt message from peer %s", peersPublicKey.String())
return nil, fmt.Errorf("failed to decrypt message from peer %s", peerPublicKey.String())
}

return opened, nil
Expand Down
40 changes: 40 additions & 0 deletions signal/encryption_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package signal

import (
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
"testing"
)

func TestEncryptDecrypt(t *testing.T) {
strMsg := "message to encrypt"
bytesMsg := []byte(strMsg)

peerAKey, err := wgtypes.GenerateKey()
if err != nil {
t.Error()
return
}

peerBKey, err := wgtypes.GenerateKey()
if err != nil {
t.Error()
return
}

encryptedMessage, err := Encrypt(bytesMsg, peerBKey.PublicKey(), peerAKey)
if err != nil {
t.Error(err)
return
}

decryptedMessage, err := Decrypt(encryptedMessage, peerAKey.PublicKey(), peerBKey)
if err != nil {
t.Error(err)
return
}

if string(decryptedMessage) != strMsg {
t.Error()
}

}
4 changes: 2 additions & 2 deletions signal/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (reg *Registry) Register(peer *Peer) {
reg.Peers[peer.Id] = peer
}

// DeregisterHub deregister Peer from the Registry (usually once it disconnects)
func (reg *Registry) DeregisterHub(peer *Peer) {
// Deregister deregister Peer from the Registry (usually once it disconnects)
func (reg *Registry) Deregister(peer *Peer) {
if _, ok := reg.Peers[peer.Id]; ok {
delete(reg.Peers, peer.Id)
log.Printf("deregistered peer [%s]", peer.Id)
Expand Down
48 changes: 48 additions & 0 deletions signal/peer/peer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package peer

import (
"testing"
)

func TestRegistry_Register(t *testing.T) {
r := NewRegistry()
peer1 := NewPeer("test_peer_1", nil)
peer2 := NewPeer("test_peer_2", nil)
r.Register(peer1)
r.Register(peer2)

if len(r.Peers) != 2 {
t.Errorf("expected 2 registered peers")
}

if _, ok := r.Peers["test_peer_1"]; !ok {
t.Errorf("expected test_peer_1 not found in the registry")
}

if _, ok := r.Peers["test_peer_2"]; !ok {
t.Errorf("expected test_peer_2 not found in the registry")
}
}

func TestRegistry_Deregister(t *testing.T) {
r := NewRegistry()
peer1 := NewPeer("test_peer_1", nil)
peer2 := NewPeer("test_peer_2", nil)
r.Register(peer1)
r.Register(peer2)

r.Deregister(peer1)

if len(r.Peers) != 1 {
t.Errorf("expected 1 registered peers after deregistring")
}

if _, ok := r.Peers["test_peer_1"]; ok {
t.Errorf("expected test_peer_1 to absent in the registry after deregistering")
}

if _, ok := r.Peers["test_peer_2"]; !ok {
t.Errorf("expected test_peer_2 not found in the registry")
}

}

0 comments on commit 248b786

Please sign in to comment.