Skip to content

Commit

Permalink
Added more tests to sharedkey feature
Browse files Browse the repository at this point in the history
  • Loading branch information
daeMOn63 committed Feb 3, 2020
1 parent 4d1b9ca commit 3cac3b1
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 3 deletions.
29 changes: 26 additions & 3 deletions client_test.go
Expand Up @@ -826,7 +826,7 @@ func TestCommandsSymClient(t *testing.T) {
t.Fatalf("Failed to protect command: %v", err)
}
if _, err := c.Unprotect(badProtectedSetPubKeyCmd, receivingTopic); err == nil {
t.Fatal("Expected an error with a bad setIDKey Command length")
t.Fatal("Expected an error with a bad setPubKey Command length")
}

_, err = c.Unprotect(protectedSetPubKeyCmd, receivingTopic)
Expand All @@ -848,7 +848,7 @@ func TestCommandsSymClient(t *testing.T) {
t.Fatalf("Failed to protect command: %v", err)
}
if _, err := c.Unprotect(badProtectedRemovePubKeyCmd, receivingTopic); err == nil {
t.Fatal("Expected an error with a bad setIDKey Command length")
t.Fatal("Expected an error with a bad removePubKey Command length")
}

_, err = c.Unprotect(protectedRemovePubKeyCmd, receivingTopic)
Expand All @@ -869,14 +869,37 @@ func TestCommandsSymClient(t *testing.T) {
t.Fatalf("Failed to protect command: %v", err)
}
if _, err := c.Unprotect(badProtectedResetPubKeyCmd, receivingTopic); err == nil {
t.Fatal("Expected an error with a bad setIDKey Command length")
t.Fatal("Expected an error with a bad resetPubKey Command length")
}

_, err = c.Unprotect(protectedResetPubKeyCmd, receivingTopic)
if err != ErrUnsupportedOperation {
t.Fatalf("Invalid error when unprotecting command: got %v, wanted %v", err, ErrUnsupportedOperation)
}

setC2KeyCmd := []byte{SetC2Key}
badProtectedSetC2KeyCmd, err := e4crypto.ProtectSymKey(append(setC2KeyCmd, 0x01), newClientKey)
if err != nil {
t.Fatalf("Failed to protect command: %v", err)
}
if _, err := c.Unprotect(badProtectedSetC2KeyCmd, receivingTopic); err == nil {
t.Fatal("Expected an error with a bad setC2Key Command length")
}

pubkey, err := curve25519.X25519(e4crypto.RandomKey(), curve25519.Basepoint)
if err != nil {
t.Fatalf("failed to generate pubkey: %v", err)
}
protectedSetC2KeyCmd, err := e4crypto.ProtectSymKey(append(setC2KeyCmd, pubkey...), newClientKey)
if err != nil {
t.Fatalf("Failed to protect command: %v", err)
}

_, err = c.Unprotect(protectedSetC2KeyCmd, receivingTopic)
if err != ErrUnsupportedOperation {
t.Fatalf("Invalid error when unprotecting command: got %v, wanted %v", err, ErrUnsupportedOperation)
}

// Unknown command
unknownCmd := []byte{0xFF}
protectedUnknownCmd, err := e4crypto.ProtectSymKey(unknownCmd, newClientKey)
Expand Down
62 changes: 62 additions & 0 deletions keys/publickey_test.go
Expand Up @@ -425,3 +425,65 @@ func TestPubKeyMaterialMarshalJSON(t *testing.T) {
t.Fatalf("Invalid unmarshalled key: got %v, wanted %v", unmarshalledKey, k)
}
}

func TestPrecomputeSharedKey(t *testing.T) {
_, privateKey, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}

clientID := e4crypto.HashIDAlias("test")
c2Pk := getTestC2PubKey(t)

k, err := NewPubKeyMaterial(clientID, privateKey, c2Pk)
if err != nil {
t.Fatalf("Failed to create pubKeyMaterial: %v", err)
}

typedKey, ok := k.(*pubKeyMaterial)
if !ok {
t.Fatalf("failed to cast key to pubKeyMaterial")
}

if len(typedKey.sharedKey) == 0 {
t.Fatalf("Expected sharedKey to have length > 0")
}

if !bytes.Equal(typedKey.C2PubKey, c2Pk) {
t.Fatalf("Expected C2 pubkey to be %v, got %v", c2Pk, typedKey.C2PubKey)
}

originalSharedKey := make([]byte, len(typedKey.sharedKey))
copy(originalSharedKey, typedKey.sharedKey)

newC2Pk := getTestC2PubKey(t)
if err := typedKey.SetC2PubKey(newC2Pk); err != nil {
t.Fatalf("failed to set key: %v", err)
}

if !bytes.Equal(typedKey.C2PubKey, newC2Pk) {
t.Fatalf("Expected new C2 pubkey to be %v, got %v", newC2Pk, typedKey.C2PubKey)
}

if bytes.Equal(typedKey.sharedKey, originalSharedKey) {
t.Fatal("Expected shared key to change when c2 key is changed")
}

copy(originalSharedKey, typedKey.sharedKey)

_, newPrivateKey, err := ed25519.GenerateKey(nil)
if err != nil {
t.Fatalf("Failed to generate key: %v", err)
}

if err := typedKey.SetKey(newPrivateKey); err != nil {
t.Fatalf("failed to set key: %v", err)
}

if !bytes.Equal(typedKey.PrivateKey, newPrivateKey) {
t.Fatalf("Expected new private key to be %v, got %v", newPrivateKey, typedKey.PrivateKey)
}
if bytes.Equal(typedKey.sharedKey, originalSharedKey) {
t.Fatal("Expected shared key to change when private key is changed")
}
}

0 comments on commit 3cac3b1

Please sign in to comment.