wireguard-go/noise_test.go

142 lines
2.6 KiB
Go
Raw Normal View History

/* SPDX-License-Identifier: GPL-2.0
*
* Copyright (C) 2017-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
2017-06-23 11:41:59 +00:00
package main
import (
2017-06-24 13:34:17 +00:00
"bytes"
"encoding/binary"
2017-06-23 11:41:59 +00:00
"testing"
)
2017-06-24 13:34:17 +00:00
func TestCurveWrappers(t *testing.T) {
sk1, err := newPrivateKey()
assertNil(t, err)
sk2, err := newPrivateKey()
assertNil(t, err)
pk1 := sk1.publicKey()
pk2 := sk2.publicKey()
ss1 := sk1.sharedSecret(pk2)
ss2 := sk2.sharedSecret(pk1)
if ss1 != ss2 {
t.Fatal("Failed to compute shared secet")
}
}
func TestNoiseHandshake(t *testing.T) {
dev1 := randDevice(t)
dev2 := randDevice(t)
defer dev1.Close()
defer dev2.Close()
2017-06-23 11:41:59 +00:00
2018-02-02 19:45:25 +00:00
peer1, _ := dev2.NewPeer(dev1.noise.privateKey.publicKey())
peer2, _ := dev1.NewPeer(dev2.noise.privateKey.publicKey())
2017-06-23 11:41:59 +00:00
2017-06-24 13:34:17 +00:00
assertEqual(
t,
peer1.handshake.precomputedStaticStatic[:],
peer2.handshake.precomputedStaticStatic[:],
)
/* simulate handshake */
2017-06-24 20:03:52 +00:00
// initiation message
t.Log("exchange initiation message")
2017-06-24 13:34:17 +00:00
2017-06-26 11:14:02 +00:00
msg1, err := dev1.CreateMessageInitiation(peer2)
2017-06-24 13:34:17 +00:00
assertNil(t, err)
packet := make([]byte, 0, 256)
writer := bytes.NewBuffer(packet)
err = binary.Write(writer, binary.LittleEndian, msg1)
2017-06-26 11:14:02 +00:00
peer := dev2.ConsumeMessageInitiation(msg1)
2017-06-24 13:34:17 +00:00
if peer == nil {
t.Fatal("handshake failed at initiation message")
}
2017-06-23 11:41:59 +00:00
2017-06-24 13:34:17 +00:00
assertEqual(
t,
peer1.handshake.chainKey[:],
peer2.handshake.chainKey[:],
)
2017-06-23 11:41:59 +00:00
2017-06-24 13:34:17 +00:00
assertEqual(
t,
peer1.handshake.hash[:],
peer2.handshake.hash[:],
)
2017-06-23 11:41:59 +00:00
2017-06-24 20:03:52 +00:00
// response message
t.Log("exchange response message")
msg2, err := dev2.CreateMessageResponse(peer1)
assertNil(t, err)
peer = dev1.ConsumeMessageResponse(msg2)
if peer == nil {
t.Fatal("handshake failed at response message")
}
assertEqual(
t,
peer1.handshake.chainKey[:],
peer2.handshake.chainKey[:],
)
assertEqual(
t,
peer1.handshake.hash[:],
peer2.handshake.hash[:],
)
// key pairs
t.Log("deriving keys")
key1 := peer1.NewKeypair()
key2 := peer2.NewKeypair()
2017-06-24 20:03:52 +00:00
if key1 == nil {
t.Fatal("failed to dervice key-pair for peer 1")
}
if key2 == nil {
t.Fatal("failed to dervice key-pair for peer 2")
}
2017-06-23 11:41:59 +00:00
2017-06-24 20:03:52 +00:00
// encrypting / decryption test
t.Log("test key pairs")
func() {
testMsg := []byte("wireguard test message 1")
var err error
var out []byte
var nonce [12]byte
out = key1.send.Seal(out, nonce[:], testMsg, nil)
out, err = key2.receive.Open(out[:0], nonce[:], out, nil)
2017-06-24 20:03:52 +00:00
assertNil(t, err)
assertEqual(t, out, testMsg)
}()
func() {
testMsg := []byte("wireguard test message 2")
var err error
var out []byte
var nonce [12]byte
out = key2.send.Seal(out, nonce[:], testMsg, nil)
out, err = key1.receive.Open(out[:0], nonce[:], out, nil)
2017-06-24 20:03:52 +00:00
assertNil(t, err)
assertEqual(t, out, testMsg)
}()
2017-06-23 11:41:59 +00:00
}