-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproto.go
More file actions
257 lines (203 loc) · 5.21 KB
/
proto.go
File metadata and controls
257 lines (203 loc) · 5.21 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package treekeys
import (
"encoding/json"
"fmt"
)
type Endpoint struct {
identityKey PrivateKey
preKeys map[GroupElement]PrivateKey
// State for a group
roster []GroupElement
copath []GroupElement
λ PrivateKey
tk PrivateKey
}
func NewEndpoint() *Endpoint {
return &Endpoint{
identityKey: DHKeyGen(),
preKeys: map[GroupElement]PrivateKey{},
}
}
func (e *Endpoint) PreKey() GroupElement {
ek := DHKeyGen()
EK := PK(ek)
e.preKeys[EK] = ek
return EK
}
func (e Endpoint) Identity() GroupElement {
return PK(e.identityKey)
}
/////
type SetupMessage struct {
I int
ID []GroupElement
EK GroupElement
Ks GroupElement
P []GroupElement
}
type UpdateMessage struct {
J int
U []GroupElement
}
type MACMessage struct {
// XXX Should probably have an indicator of message type that is covered by the MAC
Message []byte
MAC []byte
}
func NewMACMessage(key []byte, msg interface{}) (*MACMessage, error) {
var err error
macmsg := &MACMessage{}
macmsg.Message, err = json.Marshal(msg)
if err != nil {
return nil, err
}
macmsg.MAC = MAC(key[:], macmsg.Message)
return macmsg, nil
}
func (macmsg MACMessage) Verify(key []byte) bool {
return VerifyMAC(key, macmsg.Message, macmsg.MAC)
}
func (macmsg MACMessage) ToSetupMessage() (*SetupMessage, error) {
var msg SetupMessage
err := json.Unmarshal(macmsg.Message, &msg)
return &msg, err
}
func (macmsg MACMessage) ToUpdateMessage() (*UpdateMessage, error) {
var msg UpdateMessage
err := json.Unmarshal(macmsg.Message, &msg)
return &msg, err
}
/////
type GroupState struct {
i int
ik PrivateKey
λ PrivateKey
ID []GroupElement
P []GroupElement
tk PrivateKey
sk PrivateKey
}
func (e *Endpoint) SetupGroup(peers []*Endpoint) (*GroupState, []*MACMessage) {
nPeers := len(peers)
π := &GroupState{}
π.ik = e.identityKey
π.i = 0
π.λ = DHKeyGen()
ks := KeyExchangeKeyGen()
IK := make([]GroupElement, nPeers)
EK := make([]GroupElement, nPeers)
λ := make([]PrivateKey, nPeers)
for i, peer := range peers {
IK[i] = peer.Identity()
EK[i] = peer.PreKey()
λ[i] = ι(PK(KeyExchange(true, π.ik, IK[i], ks, EK[i])))
}
T := CreateTree(append([]PrivateKey{π.λ}, λ...))
π.ID = append([]GroupElement{PK(π.ik)}, IK...)
m := make([]*MACMessage, nPeers)
for i := range peers {
sm := SetupMessage{
I: i + 1,
ID: π.ID,
EK: EK[i],
Ks: PK(ks),
P: Copath(T, i+1),
}
// XXX Ignoring error
m[i], _ = NewMACMessage(λ[i][:], sm)
}
π.tk = T.Value
π.P = Copath(T, 0)
// XXX How should π.sk be initialized on group creation? This just assumes
// it is set to the all-zero vector, and combined with the π.sk immediately.
π.DeriveStageKey()
return π, m
}
func (e *Endpoint) ProcessSetupMessage(macmsg *MACMessage) *GroupState {
π := &GroupState{}
π.ik = e.identityKey
// XXX Paper doesn't specify verifying MAC
// XXX Ignoring error
msg, _ := macmsg.ToSetupMessage()
ek, ok := e.preKeys[msg.EK]
if !ok {
// TODO: Handle this better
panic("Unknown PreKey")
}
π.λ = ι(PK(KeyExchange(false, π.ik, msg.ID[0], ek, msg.Ks)))
if !macmsg.Verify(π.λ[:]) {
panic("Bad MAC")
}
// XXX Paper doesn't specify doing anything with i
π.i = msg.I
π.ID = msg.ID
π.P = msg.P
// XXX PK conversion differs from paper
nks := PathNodeKeys(π.λ, π.P)
π.tk = nks[0]
// XXX How should π.sk be initialized on group creation? This just assumes
// it is set to the all-zero vector, and combined with the π.sk immediately.
π.DeriveStageKey()
return π
}
func (π *GroupState) UpdateKey() *MACMessage {
π.λ = DHKeyGen()
nks := PathNodeKeys(π.λ, π.P)
// XXX Not computing MAC because it will never be verified
// XXX π.sk is used as the MAC key but never computed
um := UpdateMessage{
J: π.i,
U: make([]GroupElement, len(π.P)),
}
for i, nk := range nks {
if i == 0 {
continue
}
um.U[i-1] = PK(nk)
}
// XXX Ignoring error
mm, _ := NewMACMessage(π.sk[:], um)
// XXX Assuming stage key update happens every time the tree key changes?
π.tk = nks[0]
π.DeriveStageKey()
return mm
}
// XXX This is completely upside-down compared to the paper. I think the paper
// is just wrong here; it acts as if the node-key and copath list were ordered
// starting from the leaves. They actually start from the root, so we need to
// count down from the root, instead of starting with the height and
// decrementing to the right place.
func IndexToUpdate(d, n, i, j int) int {
if i == j {
panic(fmt.Sprintf("Equal indices passed to IndexToUpdate [%d] [%d]", i, j))
}
m := pow2(n)
switch {
case i < m && j < m:
return IndexToUpdate(d+1, m, i, j)
case i >= m && j >= m:
return IndexToUpdate(d+1, n-m, i-m, j-m)
}
return d
}
func (π *GroupState) ProcessUpdateMessage(macmsg *MACMessage) {
if !macmsg.Verify(π.sk[:]) {
panic("Bad MAC")
}
// XXX Ignoring error
msg, _ := macmsg.ToUpdateMessage()
d := IndexToUpdate(0, len(π.ID), π.i, msg.J)
π.P[d] = msg.U[d]
nks := PathNodeKeys(π.λ, π.P)
π.tk = nks[0]
// XXX Assuming this happens every time the tree key changes?
π.DeriveStageKey()
return
}
func (π *GroupState) DeriveStageKey() {
idBytes := []byte{}
for _, id := range π.ID {
idBytes = append(idBytes, id[:]...)
}
π.sk = KDF(π.sk[:], π.tk[:], idBytes)
}