From 9b327ea6ba2a311e54115edc48160b5e8889fe6f Mon Sep 17 00:00:00 2001 From: braginini Date: Thu, 3 Jun 2021 11:39:19 +0200 Subject: [PATCH] test: add signal encryption test --- .github/workflows/golang-test.yml | 17 +++++++++++++ signal/encryption.go | 10 ++++---- signal/encryption_test.go | 40 +++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 5 deletions(-) create mode 100644 .github/workflows/golang-test.yml create mode 100644 signal/encryption_test.go diff --git a/.github/workflows/golang-test.yml b/.github/workflows/golang-test.yml new file mode 100644 index 00000000000..cde77516bff --- /dev/null +++ b/.github/workflows/golang-test.yml @@ -0,0 +1,17 @@ +on: [push, 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 ./... \ No newline at end of file diff --git a/signal/encryption.go b/signal/encryption.go index 2f4f1bccb30..0018e04a20f 100644 --- a/signal/encryption.go +++ b/signal/encryption.go @@ -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 diff --git a/signal/encryption_test.go b/signal/encryption_test.go new file mode 100644 index 00000000000..8e617953e23 --- /dev/null +++ b/signal/encryption_test.go @@ -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() + } + +}