-
Notifications
You must be signed in to change notification settings - Fork 1
/
skipped_messages.go
57 lines (40 loc) · 1.1 KB
/
skipped_messages.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
package doubleratchet
import "errors"
var errPersistorFull = errors.New("Persistor has reached its max size")
const maxSkippedKeys int = 10
// A skippedStore allows the double ratchet to maintain a set of message
// keys that have not been encountered.
type skipPersistor interface {
Get(DiffieHellmanPublic, int) []byte
Put(DiffieHellmanPublic, int, []byte) error
}
type mappedSkipPersistor map[DiffieHellmanPublic]map[int][]byte
func (m mappedSkipPersistor) Get(dh DiffieHellmanPublic, n int) []byte {
dhMap := m[dh]
if dhMap == nil {
return nil
}
mk := dhMap[n]
delete(m[dh], n)
if len(m[dh]) == 0 {
delete(m, dh)
}
return mk
}
func (m mappedSkipPersistor) Put(dh DiffieHellmanPublic, n int, mk []byte) error {
// Prevent a baddie from flooding the missed key store.
//
// TODO: It may be better to make a circular buffer so the store isn't
// plugged up forever with baddie turds.
if len(m) == maxSkippedKeys {
return errPersistorFull
}
if m[dh] == nil {
m[dh] = make(map[int][]byte, 0)
}
if len(m[dh]) == maxSkippedKeys {
return errPersistorFull
}
m[dh][n] = mk
return nil
}