-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdouble_ratchet_test.go
149 lines (107 loc) · 4.99 KB
/
double_ratchet_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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package e2ee
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestDoubleRatchet(t *testing.T) {
alice, err := generateIdentityKeyPair()
assert.Nil(t, err)
aliceX25519EphemeralKeyPair, err := generateEphemeralKeyPair()
assert.Nil(t, err)
bob, err := generateIdentityKeyPair()
assert.Nil(t, err)
bobPreKeyPair, err := generatePreKeyPair()
assert.Nil(t, err)
bobPreKeyBundle := generatePreKeyBundle(*bob, *bobPreKeyPair)
aliceX25519IdentityPrivateKey := alice.privateEd25519KeyToCurve25519()
aliceX25519IdentityPublicKey, err := alice.publicEd25519KeyToCurve25519()
assert.Nil(t, err)
bobX25519IdentityPrivateKey := bob.privateEd25519KeyToCurve25519()
bobX25519IdentityPublicKey, err := bob.publicEd25519KeyToCurve25519()
assert.Nil(t, err)
aliceRootKey, err := senderRootKey(aliceX25519IdentityPrivateKey, aliceX25519EphemeralKeyPair.privateKey, bobX25519IdentityPublicKey, bobPreKeyPair.publicKey)
assert.Nil(t, err)
bobRootKey, err := receiverRootKey(bobX25519IdentityPrivateKey, bobPreKeyPair.privateKey, aliceX25519IdentityPublicKey, aliceX25519EphemeralKeyPair.publicKey)
assert.Nil(t, err)
assert.Equal(t, aliceRootKey, bobRootKey)
plaintext := []byte("hello world")
// 関数化
var ad []byte = append(alice.publicKey[:], bob.publicKey[:]...)
aliceRatchetState, err := senderRatchetInit(aliceRootKey, *bobPreKeyBundle)
bobRatchetState := receiverRatchetInit(bobRootKey, bobPreKeyBundle.signedPreKey, bobPreKeyPair.privateKey)
// Alice 1 回目のメッセージ
header, ciphertext, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext1, err := bobRatchetState.ratchetDecrypt(header, ciphertext, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext1)
// Alice 2 回目のメッセージ
header2, ciphertext2, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext2, _ := bobRatchetState.ratchetDecrypt(header2, ciphertext2, ad)
assert.Equal(t, plaintext, plaintext2)
// Alice 3 回目のメッセージ
header3, ciphertext3, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext3, err := bobRatchetState.ratchetDecrypt(header3, ciphertext3, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext3)
// Bob 1 回目のメッセージ
header4, ciphertext4, err := bobRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext4, err := aliceRatchetState.ratchetDecrypt(header4, ciphertext4, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext4)
// Alice 4 回目のメッセージ
header5, ciphertext5, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext5, err := bobRatchetState.ratchetDecrypt(header5, ciphertext5, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext5)
}
func TestSkipMessageKey(t *testing.T) {
alice, err := generateIdentityKeyPair()
assert.Nil(t, err)
aliceX25519EphemeralKeyPair, err := generateEphemeralKeyPair()
assert.Nil(t, err)
bob, err := generateIdentityKeyPair()
assert.Nil(t, err)
bobPreKeyPair, err := generatePreKeyPair()
assert.Nil(t, err)
bobPreKeyBundle := generatePreKeyBundle(*bob, *bobPreKeyPair)
aliceX25519IdentityPrivateKey := alice.privateEd25519KeyToCurve25519()
aliceX25519IdentityPublicKey, err := alice.publicEd25519KeyToCurve25519()
assert.Nil(t, err)
bobX25519IdentityPrivateKey := bob.privateEd25519KeyToCurve25519()
bobX25519IdentityPublicKey, err := bob.publicEd25519KeyToCurve25519()
assert.Nil(t, err)
aliceRootKey, err := senderRootKey(aliceX25519IdentityPrivateKey, aliceX25519EphemeralKeyPair.privateKey, bobX25519IdentityPublicKey, bobPreKeyPair.publicKey)
assert.Nil(t, err)
bobRootKey, err := receiverRootKey(bobX25519IdentityPrivateKey, bobPreKeyPair.privateKey, aliceX25519IdentityPublicKey, aliceX25519EphemeralKeyPair.publicKey)
assert.Nil(t, err)
assert.Equal(t, aliceRootKey, bobRootKey)
plaintext := []byte("hello world")
// 関数化
var ad []byte = append(alice.publicKey[:], bobPreKeyBundle.identityKey[:]...)
aliceRatchetState, err := senderRatchetInit(aliceRootKey, *bobPreKeyBundle)
assert.Nil(t, err)
bobRatchetState := receiverRatchetInit(bobRootKey, bobPreKeyBundle.signedPreKey, bobPreKeyPair.privateKey)
// Alice 1 回目のメッセージ
header, ciphertext, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext1, err := bobRatchetState.ratchetDecrypt(header, ciphertext, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext1)
// Alice 2 回目のメッセージ
header2, ciphertext2, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
// Alice 3 回目のメッセージ
header3, ciphertext3, err := aliceRatchetState.ratchetEncrypt(plaintext, ad)
assert.Nil(t, err)
plaintext3, err := bobRatchetState.ratchetDecrypt(header3, ciphertext3, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext3)
// メッセージが送れてきた
plaintext2, err := bobRatchetState.ratchetDecrypt(header2, ciphertext2, ad)
assert.Nil(t, err)
assert.Equal(t, plaintext, plaintext2)
}