-
Notifications
You must be signed in to change notification settings - Fork 3
/
x3dh_test.go
71 lines (52 loc) · 1.74 KB
/
x3dh_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package x3dh
import (
"crypto/rand"
"crypto/sha256"
"testing"
require "github.com/stretchr/testify/require"
)
const testProtocolName = "testing"
func TestX3dh_CalculateSecretNoOneTimePreKey(t *testing.T) {
c := NewCurve25519(rand.Reader)
// alice id key pair
aliceIdKeyPair, err := c.GenerateKeyPair()
require.Nil(t, err)
// bob id key pair
bobIdKeyPair, err := c.GenerateKeyPair()
require.Nil(t, err)
// bob signed pre key pair
bobSignedPreKey, err := c.GenerateKeyPair()
aliceX := New(&c, sha256.New(), testProtocolName, aliceIdKeyPair)
// calculate secret with bob's pre key bundle
initializedProtocol, err := aliceX.CalculateSecret(TestPreKeyBundle{
validSignature: func() (bool, error) {
return true, nil
},
identityKey: bobIdKeyPair.PublicKey,
signedPreKey: bobSignedPreKey.PublicKey,
preKeySignature: []byte(""),
})
require.Nil(t, err)
bobX := New(&c, sha256.New(), testProtocolName, bobIdKeyPair)
secretBob, err := bobX.SecretFromRemote(ProtocolInitialisation{
RemoteIdKey: aliceIdKeyPair.PublicKey,
RemoteEphemeralKey: initializedProtocol.EphemeralKey,
MySignedPreKey: bobSignedPreKey.PrivateKey,
})
require.Nil(t, err)
require.Equal(t, initializedProtocol.SharedSecret, secretBob)
}
func TestX3dh_CalculateSecretInvalidSignature(t *testing.T) {
c := NewCurve25519(rand.Reader)
// alice id key pair
aliceIdKeyPair, err := c.GenerateKeyPair()
require.Nil(t, err)
aliceX := New(&c, sha256.New(), testProtocolName, aliceIdKeyPair)
// since we set the signature to invalid an error should be returned
_, err = aliceX.CalculateSecret(TestPreKeyBundle{
validSignature: func() (bool, error) {
return false, nil
},
})
require.EqualError(t, err, PreKeyBundleInvalidSignature.Error())
}