52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
package pnyx
|
|
|
|
import (
|
|
"crypto/ed25519"
|
|
"crypto/rand"
|
|
"slices"
|
|
"testing"
|
|
)
|
|
|
|
func fatalErr(t *testing.T, err error) {
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestSessionOpen(t *testing.T) {
|
|
client_pubkey, client_key, err := ed25519.GenerateKey(rand.Reader)
|
|
fatalErr(t, err)
|
|
|
|
session_open, client_ecdh, err := NewSessionOpen(client_key)
|
|
fatalErr(t, err)
|
|
|
|
client_pubkey_parsed, client_ecdh_parsed, err := ParseSessionOpen(session_open)
|
|
fatalErr(t, err)
|
|
|
|
if slices.Compare(client_pubkey, client_pubkey_parsed) != 0 {
|
|
t.Fatalf("Client Pubkey %x does not match parsed %x", client_pubkey, client_pubkey_parsed)
|
|
}
|
|
|
|
if slices.Compare(client_ecdh.Public().(ed25519.PublicKey), client_ecdh_parsed) != 0 {
|
|
t.Fatalf("Client Pubkey %x does not match parsed %x", client_pubkey, client_pubkey_parsed)
|
|
}
|
|
}
|
|
|
|
func TestECDH(t *testing.T) {
|
|
client_public, client_private, err := ed25519.GenerateKey(rand.Reader)
|
|
fatalErr(t, err)
|
|
|
|
server_public, server_private, err := ed25519.GenerateKey(rand.Reader)
|
|
fatalErr(t, err)
|
|
|
|
server_secret, err := ECDH(client_public, server_private)
|
|
fatalErr(t, err)
|
|
|
|
client_secret, err := ECDH(server_public, client_private)
|
|
fatalErr(t, err)
|
|
|
|
if slices.Compare(server_secret, client_secret) != 0 {
|
|
t.Fatalf("Server and Client secrets do not match")
|
|
}
|
|
}
|