This repository has been archived by the owner on Apr 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
272 lines (225 loc) · 6.9 KB
/
map.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
// Copyright 2017 Tom Thorogood. All rights reserved.
// Use of this source code is governed by a
// Modified BSD License license that can be found in
// the LICENSE file.
// Package ctmap implements a constant-time key-value map.
package ctmap
import "crypto/subtle"
// Map is a constant-time key-value map.
type Map struct {
m [][]byte
keySize, valSize int
}
// New returns a new constant-time map with the
// given key and value sizes.
//
// Every key and value must be of equal size.
//
// For a constant-time equivalent of map[string]struct{},
// use 0 for valSize.
func New(keySize, valSize int) *Map {
return &Map{
keySize: keySize,
valSize: valSize,
}
}
// NewWithCapacity returns a new constant-time map with
// the given key and value sizes. It preallocates the
// map's backing storage, sized to fit capacity entries
// without reallocating.
//
// Every key and value must be of equal size.
//
// For a constant-time equivalent of map[string]struct{},
// use 0 for valSize.
func NewWithCapacity(keySize, valSize, capacity int) *Map {
return &Map{
m: make([][]byte, 0, capacity),
keySize: keySize,
valSize: valSize,
}
}
// Len returns the number of entries in the map. It does
// not account for duplicates.
func (m *Map) Len() int {
return len(m.m)
}
// Add appends a new entry to the map. It does not check
// for duplicates, nor does it handle them.
func (m *Map) Add(key, val []byte) {
if len(key) != m.keySize {
panic("key has invalid size")
}
if len(val) != m.valSize {
panic("val has invalid size")
}
entry := make([]byte, m.keySize+m.valSize)
copy(entry[:m.keySize], key)
copy(entry[m.keySize:], val)
m.m = append(m.m, entry)
}
// Set sets an existing map entry to val in constant-time.
// It returns 1 if the key was present and the val set, 0
// otherwise.
//
// If there are multiple entries with the same key, only
// the first entry will have it's value set to val.
func (m *Map) Set(key, val []byte) int {
if len(key) != m.keySize {
panic("key has invalid size")
}
if len(val) != m.valSize {
panic("val has invalid size")
}
var v int
for _, entry := range m.m {
vv := subtle.ConstantTimeCompare(entry[:m.keySize], key) &^ v
subtle.ConstantTimeCopy(vv, entry[m.keySize:], val)
v |= vv
}
return v
}
// Replace replaces an existing entry in the map with a new
// entry in constant-time. It returns 1 if oldKey was present
// in the map and the entry replaced, 0 otherwise.
//
// If there are duplicate entries matching oldKey, only the
// first entry will be replaced.
func (m *Map) Replace(oldKey, newKey, val []byte) int {
if len(oldKey) != m.keySize {
panic("oldKey has invalid size")
}
if len(newKey) != m.keySize {
panic("newKey has invalid size")
}
if len(val) != m.valSize {
panic("val has invalid size")
}
var v int
for _, entry := range m.m {
vv := subtle.ConstantTimeCompare(entry[:m.keySize], oldKey) &^ v
subtle.ConstantTimeCopy(vv, entry[:m.keySize], newKey)
subtle.ConstantTimeCopy(vv, entry[m.keySize:], val)
v |= vv
}
return v
}
// Rename is like Replace but it only changes they key and
// leaves the value untouched.
//
// If there are duplicate entries matching oldKey, only the
// first entry's key will be replaced.
func (m *Map) Rename(oldKey, newKey []byte) int {
if len(oldKey) != m.keySize {
panic("oldKey has invalid size")
}
if len(newKey) != m.keySize {
panic("newKey has invalid size")
}
var v int
for _, entry := range m.m {
vv := subtle.ConstantTimeCompare(entry[:m.keySize], oldKey) &^ v
subtle.ConstantTimeCopy(vv, entry[:m.keySize], newKey)
v |= vv
}
return v
}
// Contains determines if a key is present in the map in
// constant-time. It returns 1 if the key is present, 0
// otherwise.
func (m *Map) Contains(key []byte) int {
if len(key) != m.keySize {
panic("key has invalid size")
}
var v int
for _, entry := range m.m {
v |= subtle.ConstantTimeCompare(entry[:m.keySize], key)
}
return v
}
// Lookup finds the value associated with a key in
// constant-time. The value is copied, in constant-time,
// into val which must be the correct length. It returns
// 1 if the key was present, 0 otherwise.
//
// If there are multiple entries matching key, only the
// first will be returned.
func (m *Map) Lookup(key, val []byte) int {
if len(key) != m.keySize {
panic("key has invalid size")
}
if len(val) != m.valSize {
panic("val has invalid size")
}
var v int
for _, entry := range m.m {
vv := subtle.ConstantTimeCompare(entry[:m.keySize], key) &^ v
subtle.ConstantTimeCopy(vv, val, entry[m.keySize:])
v |= vv
}
return v
}
// Delete removes an entry with a given key from the map.
// It returns 1 if an entry was removed, 0 otherwise. The
// removed entry is zeroed.
//
// If the map contains multiple entries with the same key,
// only the first is removed.
//
// WARNING: The interaction between Delete and other methods
// leaks timing information. In particular, if an entry was
// removed the size of the map is reduced and the time taken
// by other methods in this package will be reduced. If an
// entry was not removed, then the time taken does not change.
func (m *Map) Delete(key []byte) int {
if len(key) != m.keySize {
panic("key has invalid size")
}
if len(m.m) == 0 {
return 0
}
var v int
for i, entry := range m.m[:len(m.m)-1] {
v |= subtle.ConstantTimeCompare(entry[:m.keySize], key)
subtle.ConstantTimeCopy(v, entry, m.m[i+1])
}
last := m.m[len(m.m)-1]
v |= subtle.ConstantTimeCompare(last[:m.keySize], key)
for i := range last {
last[i] &= byte(v - 1)
}
// The last entry in the list will not be garbage
// collected until the next call to Add, that will leak
// information about whether a key was removed or not.
// Allowing the entry to be garbage collected now, by
// setting the final entry to nil, would leak timing
// information immediately. It is also an operation that
// cannot be done in constant-time. Because of this, a
// memory leak is tolerated. Even though m.m is truncated
// bellow, it still contains a reference to the removed
// slice which prevents it from being garbage collected.
// When Add is next called, the append call will overwrite
// the reference and the slice will be garbage collected
// resulting in a possible timing leak.
//
// By also reducing the backing slice's capacity, future
// calls to Add take a near constant-time regardless of
// whether an entry was removed. The time taken by Add
// will then only depend on the length of the map.
// XXX: Hopefully this slice is constant-time.
m.m = m.m[:len(m.m)-v : cap(m.m)-v]
return v
}
// Range calls f for each entry in the map. f must be carefull
// not to leak any timing information about the contents of key.
//
// If the map contains duplicate keys, f will be called once for
// each key.
//
// The behaviour of the map is undefined if key or val are
// modified.
func (m *Map) Range(f func(key, val []byte)) {
for _, entry := range m.m {
f(entry[:m.keySize], entry[m.valSize:])
}
}