forked from coyim/otr3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
key_management.go
315 lines (252 loc) · 7.58 KB
/
key_management.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
package otr3
import (
"crypto/aes"
"crypto/sha1"
"crypto/sha256"
"encoding/binary"
"hash"
"io"
"math/big"
)
type dhKeyPair struct {
pub *big.Int
priv *big.Int
}
type akeKeys struct {
c [aes.BlockSize]byte
m1, m2 [sha256.Size]byte
}
type macKey [sha1.Size]byte
type sessionKeys struct {
sendingAESKey, receivingAESKey [aes.BlockSize]byte
sendingMACKey, receivingMACKey macKey
extraKey [sha256.Size]byte
}
type macKeyUsage struct {
ourKeyID, theirKeyID uint32
receivingKey macKey
}
type macKeyHistory struct {
items []macKeyUsage
}
func (h *macKeyHistory) deleteKeysAt(del ...int) {
for j := len(del) - 1; j >= 0; j-- {
l := len(h.items)
h.items[del[j]], h.items = h.items[l-1], h.items[:l-1]
}
}
func (h *macKeyHistory) addKeys(ourKeyID uint32, theirKeyID uint32, receivingMACKey macKey) {
macKeys := macKeyUsage{
ourKeyID: ourKeyID,
theirKeyID: theirKeyID,
receivingKey: receivingMACKey,
}
h.items = append(h.items, macKeys)
}
func (h *macKeyHistory) forgetMACKeysForOurKey(ourKeyID uint32) []macKey {
var ret []macKey
var del []int
for i, k := range h.items {
if k.ourKeyID == ourKeyID {
ret = append(ret, k.receivingKey)
del = append(del, i)
}
}
h.deleteKeysAt(del...)
return ret
}
func (h *macKeyHistory) forgetMACKeysForTheirKey(theirKeyID uint32) []macKey {
var ret []macKey
var del []int
for i, k := range h.items {
if k.theirKeyID == theirKeyID {
ret = append(ret, k.receivingKey)
del = append(del, i)
}
}
h.deleteKeysAt(del...)
return ret
}
type keyPairCounter struct {
ourKeyID, theirKeyID uint32
ourCounter, theirCounter uint64
}
type counterHistory struct {
counters []*keyPairCounter
}
func (h *counterHistory) findCounterFor(ourKeyID, theirKeyID uint32) *keyPairCounter {
for _, c := range h.counters {
if c.ourKeyID == ourKeyID && c.theirKeyID == theirKeyID {
return c
}
}
c := &keyPairCounter{
ourKeyID: ourKeyID,
theirKeyID: theirKeyID,
}
h.counters = append(h.counters, c)
return c
}
type keyManagementContext struct {
ourKeyID, theirKeyID uint32
ourCurrentDHKeys, ourPreviousDHKeys dhKeyPair
theirCurrentDHPubKey, theirPreviousDHPubKey *big.Int
counterHistory counterHistory
macKeyHistory macKeyHistory
oldMACKeys []macKey
}
func (k *keyManagementContext) incrementOurCounter(ourKeyID, theirKeyID uint32) {
counter := k.counterHistory.findCounterFor(ourKeyID, theirKeyID)
counter.ourCounter++
}
func (k *keyManagementContext) setTheirCurrentDHPubKey(key *big.Int) {
k.theirCurrentDHPubKey = setBigInt(k.theirCurrentDHPubKey, key)
}
func (k *keyManagementContext) setOurCurrentDHKeys(priv *big.Int, pub *big.Int) {
k.ourCurrentDHKeys.priv = setBigInt(k.ourCurrentDHKeys.priv, priv)
k.ourCurrentDHKeys.pub = setBigInt(k.ourCurrentDHKeys.pub, pub)
}
func (k *keyManagementContext) checkMessageCounter(message dataMsg) error {
counter := k.counterHistory.findCounterFor(message.recipientKeyID, message.senderKeyID)
theirNextCounter := binary.BigEndian.Uint64(message.topHalfCtr[:])
if theirNextCounter <= counter.theirCounter {
return newOtrConflictError("counter regressed")
}
counter.theirCounter = theirNextCounter
return nil
}
func (k *keyManagementContext) revealMACKeys() []macKey {
ret := k.oldMACKeys
k.oldMACKeys = []macKey{}
return ret
}
func (k *keyManagementContext) generateNewDHKeyPair(randomness io.Reader) error {
newPrivKey, err := randSizedMPI(randomness, 40)
if err != nil {
return err
}
k.ourPreviousDHKeys.wipe()
k.ourPreviousDHKeys = k.ourCurrentDHKeys
k.ourCurrentDHKeys = dhKeyPair{
priv: newPrivKey,
pub: modExp(g1, newPrivKey),
}
k.ourKeyID++
return nil
}
func (k *keyManagementContext) revealMACKeysForOurPreviousKeyID() {
keys := k.macKeyHistory.forgetMACKeysForOurKey(k.ourKeyID - 1)
k.oldMACKeys = append(k.oldMACKeys, keys...)
}
func (c *Conversation) rotateKeys(dataMessage dataMsg) error {
if err := c.keys.rotateOurKeys(dataMessage.recipientKeyID, c.rand()); err != nil {
return err
}
c.keys.rotateTheirKey(dataMessage.senderKeyID, dataMessage.y)
return nil
}
func (k *keyManagementContext) rotateOurKeys(recipientKeyID uint32, randomness io.Reader) error {
if recipientKeyID == k.ourKeyID {
k.revealMACKeysForOurPreviousKeyID()
return k.generateNewDHKeyPair(randomness)
}
return nil
}
func (k *keyManagementContext) revealMACKeysForTheirPreviousKeyID() {
keys := k.macKeyHistory.forgetMACKeysForTheirKey(k.theirKeyID - 1)
k.oldMACKeys = append(k.oldMACKeys, keys...)
}
func (k *keyManagementContext) rotateTheirKey(senderKeyID uint32, pubDHKey *big.Int) {
if senderKeyID == k.theirKeyID {
k.revealMACKeysForTheirPreviousKeyID()
k.theirPreviousDHPubKey = k.theirCurrentDHPubKey
k.theirCurrentDHPubKey = pubDHKey
k.theirKeyID++
}
}
func (k *keyManagementContext) calculateDHSessionKeys(ourKeyID, theirKeyID uint32) (sessionKeys, error) {
var ret sessionKeys
ourPrivKey, ourPubKey, err := k.pickOurKeys(ourKeyID)
if err != nil {
return ret, err
}
theirPubKey, err := k.pickTheirKey(theirKeyID)
if err != nil {
return ret, err
}
ret = calculateDHSessionKeys(ourPrivKey, ourPubKey, theirPubKey)
k.macKeyHistory.addKeys(ourKeyID, theirKeyID, ret.receivingMACKey)
return ret, nil
}
func calculateDHSessionKeys(ourPrivKey, ourPubKey, theirPubKey *big.Int) sessionKeys {
var ret sessionKeys
var sendbyte, recvbyte byte
if gt(ourPubKey, theirPubKey) {
//we are high end
sendbyte, recvbyte = 0x01, 0x02
} else {
//we are low end
sendbyte, recvbyte = 0x02, 0x01
}
s := new(big.Int).Exp(theirPubKey, ourPrivKey, p)
secbytes := appendMPI(nil, s)
sha := sha1.New()
copy(ret.sendingAESKey[:], h(sendbyte, secbytes, sha))
copy(ret.receivingAESKey[:], h(recvbyte, secbytes, sha))
ret.sendingMACKey = sha1.Sum(ret.sendingAESKey[:])
ret.receivingMACKey = sha1.Sum(ret.receivingAESKey[:])
copy(ret.extraKey[:], h(0xFF, secbytes, sha256.New()))
return ret
}
func (k *keyManagementContext) pickOurKeys(ourKeyID uint32) (privKey, pubKey *big.Int, err error) {
if ourKeyID == 0 || k.ourKeyID == 0 {
return nil, nil, newOtrConflictError("invalid key id for local peer")
}
switch ourKeyID {
case k.ourKeyID:
privKey, pubKey = k.ourCurrentDHKeys.priv, k.ourCurrentDHKeys.pub
case k.ourKeyID - 1:
privKey, pubKey = k.ourPreviousDHKeys.priv, k.ourPreviousDHKeys.pub
default:
err = newOtrConflictError("mismatched key id for local peer")
}
return privKey, pubKey, err
}
func (k *keyManagementContext) pickTheirKey(theirKeyID uint32) (pubKey *big.Int, err error) {
if theirKeyID == 0 || k.theirKeyID == 0 {
return nil, newOtrConflictError("invalid key id for remote peer")
}
switch theirKeyID {
case k.theirKeyID:
pubKey = k.theirCurrentDHPubKey
case k.theirKeyID - 1:
if k.theirPreviousDHPubKey == nil {
err = newOtrConflictError("no previous key for remote peer found")
} else {
pubKey = k.theirPreviousDHPubKey
}
default:
err = newOtrConflictError("mismatched key id for remote peer")
}
return pubKey, err
}
func calculateAKEKeys(s *big.Int) (ssid [8]byte, revealSigKeys, signatureKeys akeKeys) {
secbytes := appendMPI(nil, s)
sha := sha256.New()
keys := h(0x01, secbytes, sha)
copy(ssid[:], h(0x00, secbytes, sha)[:8])
copy(revealSigKeys.c[:], keys[:16])
copy(signatureKeys.c[:], keys[16:])
copy(revealSigKeys.m1[:], h(0x02, secbytes, sha))
copy(revealSigKeys.m2[:], h(0x03, secbytes, sha))
copy(signatureKeys.m1[:], h(0x04, secbytes, sha))
copy(signatureKeys.m2[:], h(0x05, secbytes, sha))
return
}
// h1() and h2() are the same
func h(b byte, secbytes []byte, h hash.Hash) []byte {
h.Reset()
h.Write([]byte{b})
h.Write(secbytes[:])
return h.Sum(nil)
}