OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "net/quic/crypto/strike_register.h" | |
6 | |
7 #include "base/logging.h" | |
8 | |
9 using std::pair; | |
10 using std::set; | |
11 using std::vector; | |
12 | |
13 namespace net { | |
14 | |
15 // InternalNode represents a non-leaf node in the critbit tree. See the comment | |
16 // in the .h file for details. | |
17 class StrikeRegister::InternalNode { | |
18 public: | |
19 void SetChild(unsigned direction, uint32 child) { | |
20 data_[direction] = (data_[direction] & 0xff) | (child << 8); | |
21 } | |
22 | |
23 void SetCritByte(uint8 critbyte) { | |
24 data_[0] &= 0xffffff00; | |
25 data_[0] |= critbyte; | |
26 } | |
27 | |
28 void SetOtherBits(uint8 otherbits) { | |
29 data_[1] &= 0xffffff00; | |
30 data_[1] |= otherbits; | |
31 } | |
32 | |
33 void SetNextPtr(uint32 next) { | |
34 data_[0] = next; | |
35 } | |
36 | |
37 uint32 next() const { | |
38 return data_[0]; | |
39 } | |
40 | |
41 uint32 child(unsigned n) const { | |
42 return data_[n] >> 8; | |
43 } | |
44 | |
45 uint8 critbyte() const { | |
46 return data_[0]; | |
47 } | |
48 | |
49 uint8 otherbits() const { | |
50 return data_[1]; | |
51 } | |
52 | |
53 // These bytes are organised thus: | |
54 // <24 bits> left child | |
55 // <8 bits> crit-byte | |
56 // <24 bits> right child | |
57 // <8 bits> other-bits | |
58 uint32 data_[2]; | |
59 }; | |
60 | |
61 StrikeRegister::StrikeRegister(unsigned max_entries, | |
62 uint32 current_time, | |
63 uint32 window_secs, | |
64 const uint8 orbit[8]) | |
65 : max_entries_(max_entries), | |
66 window_secs_(window_secs), | |
67 // The horizon is initially set |window_secs| into the future because, if | |
68 // we just crashed, then we may have accepted nonces in the span | |
69 // [current_time...current_time+window_secs) and so we conservatively | |
70 // reject the whole timespan. | |
71 horizon_(current_time + window_secs) { | |
72 memcpy(orbit_, orbit, sizeof(orbit_)); | |
73 | |
74 // We only have 23 bits of index available. | |
75 CHECK_LT(max_entries, static_cast<unsigned>(1 << 23)); | |
jar (doing other things)
2013/04/06 02:26:37
nit: 1u << 23 might work
ramant (doing other things)
2013/04/06 04:20:10
Done.
| |
76 CHECK_GT(max_entries, 1u); // There must be at least two entries. | |
77 CHECK_EQ(sizeof(InternalNode), 8u); // in case of compiler changes. | |
78 internal_nodes_ = new InternalNode[max_entries]; | |
79 external_nodes_.reset(new uint8[kExternalNodeSize * max_entries]); | |
80 | |
81 Reset(); | |
82 } | |
83 | |
84 StrikeRegister::~StrikeRegister() { | |
85 delete[] internal_nodes_; | |
86 } | |
87 | |
88 void StrikeRegister::Reset() { | |
89 // Thread a free list through all of the internal nodes. | |
90 internal_node_free_head_ = 0; | |
91 for (unsigned i = 0; i < max_entries_ - 1; i++) | |
92 internal_nodes_[i].SetNextPtr(i + 1); | |
93 internal_nodes_[max_entries_ - 1].SetNextPtr(static_cast<uint32>(kNil)); | |
94 | |
95 // Also thread a free list through the external nodes. | |
96 external_node_free_head_ = 0; | |
97 for (unsigned i = 0; i < max_entries_ - 1; i++) | |
98 external_node_next_ptr(i) = i + 1; | |
99 external_node_next_ptr(max_entries_ - 1) = static_cast<uint32>(kNil); | |
100 | |
101 // This is the root of the tree. | |
102 internal_node_head_ = static_cast<uint32>(kNil); | |
103 } | |
104 | |
105 bool StrikeRegister::Insert(const uint8 nonce[32], | |
106 const uint32 current_time) { | |
107 // If current_time is very small or very large then we assume that we have | |
108 // just rolled over / are about to roll over and it's 2038 or 2106. Since | |
109 // we don't deal with this situation we flush everything and start over. | |
110 // This means that we reject everything for 2 * |window_secs_| every 68 | |
111 // years. | |
112 if (current_time < window_secs_ || | |
113 current_time + window_secs_ < current_time) { | |
114 if (internal_node_head_ != static_cast<uint32>(kNil)) { | |
115 Reset(); | |
116 } | |
117 horizon_ = current_time; | |
118 return false; | |
119 } | |
120 | |
121 // Check to see if the orbit is correct. | |
122 if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) { | |
123 return false; | |
124 } | |
125 const uint32 nonce_time = TimeFromBytes(nonce); | |
126 // We have dropped one or more nonces with a time value of |horizon_|, so | |
127 // we have to reject anything with a timestamp less than or equal to that. | |
128 if (nonce_time <= horizon_) { | |
129 return false; | |
130 } | |
131 | |
132 // Check that the timestamp is in the current window. | |
133 if (nonce_time < (current_time - window_secs_) || | |
134 nonce_time > (current_time + window_secs_)) { | |
135 return false; | |
136 } | |
137 | |
138 // We strip the orbit out of the nonce. | |
139 uint8 value[24]; | |
140 memcpy(value, nonce, sizeof(current_time)); | |
141 memcpy(value + sizeof(current_time), | |
142 nonce + sizeof(current_time) + sizeof(orbit_), | |
143 sizeof(value) - sizeof(current_time)); | |
144 | |
145 // Find the best match to |value| in the crit-bit tree. The best match is | |
146 // simply the value which /could/ match |value|, if any does, so we still | |
147 // need a memcmp to check. | |
148 uint32 best_match_index = BestMatch(value); | |
149 if (best_match_index == static_cast<uint32>(kNil)) { | |
150 // Empty tree. Just insert the new value at the root. | |
151 uint32 index = GetFreeExternalNode(); | |
152 memcpy(external_node(index), value, sizeof(value)); | |
153 internal_node_head_ = (index | static_cast<uint32>(kExternalFlag)) << 8; | |
154 return true; | |
155 } | |
156 | |
157 const uint8* best_match = external_node(best_match_index); | |
158 if (memcmp(best_match, value, sizeof(value)) == 0) { | |
159 // We found the value in the tree. | |
160 return false; | |
161 } | |
162 | |
163 // We are going to insert a new entry into the tree, so get the nodes now. | |
164 uint32 internal_node_index = GetFreeInternalNode(); | |
165 uint32 external_node_index = GetFreeExternalNode(); | |
166 | |
167 // If we just evicted the best match, then we have to try and match again. | |
168 // We know that we didn't just empty the tree because we require that | |
169 // max_entries_ >= 2. Also, we know that it doesn't match because, if it | |
170 // did, it would have been returned previously. | |
171 if (external_node_index == best_match_index) { | |
172 best_match_index = BestMatch(value); | |
173 best_match = external_node(best_match_index); | |
174 } | |
175 | |
176 // Now we need to find the first bit where we differ from |best_match|. | |
177 unsigned differing_byte; | |
178 uint8 new_other_bits; | |
179 for (differing_byte = 0; differing_byte < sizeof(value); differing_byte++) { | |
180 new_other_bits = value[differing_byte] ^ best_match[differing_byte]; | |
181 if (new_other_bits) { | |
182 break; | |
183 } | |
184 } | |
185 | |
186 // Once we have the XOR the of first differing byte in new_other_bits we need | |
187 // to find the most significant differing bit. We could do this with a simple | |
188 // for loop, testing bits 7..0. Instead we fold the bits so that we end up | |
189 // with a byte where all the bits below the most significant one, are set. | |
190 new_other_bits |= new_other_bits >> 1; | |
191 new_other_bits |= new_other_bits >> 2; | |
192 new_other_bits |= new_other_bits >> 4; | |
193 // Now this bit trick results in all the bits set, except the original | |
194 // most-significant one. | |
195 new_other_bits = (new_other_bits & ~(new_other_bits >> 1)) ^ 255; | |
196 | |
197 // Consider the effect of ORing against |new_other_bits|. If |value| did not | |
198 // have the critical bit set, the result is the same as |new_other_bits|. If | |
199 // it did, the result is all ones. | |
200 | |
201 unsigned newdirection; | |
202 if ((new_other_bits | value[differing_byte]) == 0xff) { | |
203 newdirection = 1; | |
204 } else { | |
205 newdirection = 0; | |
206 } | |
207 | |
208 memcpy(external_node(external_node_index), value, sizeof(value)); | |
209 InternalNode* inode = &internal_nodes_[internal_node_index]; | |
210 | |
211 inode->SetChild(newdirection, | |
212 external_node_index | static_cast<uint32>(kExternalFlag)); | |
213 inode->SetCritByte(differing_byte); | |
214 inode->SetOtherBits(new_other_bits); | |
215 | |
216 // |where_index| is a pointer to the uint32 which needs to be updated in | |
217 // order to insert the new internal node into the tree. The internal nodes | |
218 // store the child indexes in the top 24-bits of a 32-bit word and, to keep | |
219 // the code simple, we define that |internal_node_head_| is organised the | |
220 // same way. | |
221 DCHECK_EQ(internal_node_head_ & 0xff, 0u); | |
222 uint32* where_index = &internal_node_head_; | |
223 while (((*where_index >> 8) & static_cast<uint32>(kExternalFlag)) == 0) { | |
224 InternalNode* node = &internal_nodes_[*where_index >> 8]; | |
225 if (node->critbyte() > differing_byte) { | |
226 break; | |
227 } | |
228 if (node->critbyte() == differing_byte && | |
229 node->otherbits() > new_other_bits) { | |
230 break; | |
231 } | |
232 if (node->critbyte() == differing_byte && | |
233 node->otherbits() == new_other_bits) { | |
234 CHECK(false); | |
235 } | |
236 | |
237 uint8 c = value[node->critbyte()]; | |
238 const int direction = | |
239 (1 + static_cast<unsigned>(node->otherbits() | c)) >> 8; | |
240 where_index = &node->data_[direction]; | |
241 } | |
242 | |
243 inode->SetChild(newdirection ^ 1, *where_index >> 8); | |
244 *where_index = (*where_index & 0xff) | (internal_node_index << 8); | |
245 | |
246 return true; | |
247 } | |
248 | |
249 void StrikeRegister::Validate() { | |
250 set<uint32> free_internal_nodes; | |
251 for (uint32 i = internal_node_free_head_; i != static_cast<uint32>(kNil); | |
252 i = internal_nodes_[i].next()) { | |
253 CHECK_LT(i, static_cast<uint32>(max_entries_)); | |
254 CHECK_EQ(free_internal_nodes.count(i), (size_t)0); | |
255 free_internal_nodes.insert(i); | |
256 } | |
257 | |
258 set<uint32> free_external_nodes; | |
259 for (uint32 i = external_node_free_head_; i != static_cast<uint32>(kNil); | |
260 i = external_node_next_ptr(i)) { | |
261 CHECK_LT(i, max_entries_); | |
262 CHECK_EQ(free_external_nodes.count(i), (size_t)0); | |
jar (doing other things)
2013/04/06 02:26:37
nit 0u
ramant (doing other things)
2013/04/06 04:20:10
Done.
| |
263 free_external_nodes.insert(i); | |
264 } | |
265 | |
266 set<uint32> used_external_nodes; | |
267 set<uint32> used_internal_nodes; | |
268 | |
269 if (internal_node_head_ != static_cast<uint32>(kNil) && | |
270 ((internal_node_head_ >> 8) & static_cast<uint32>(kExternalFlag)) == 0) { | |
271 vector<pair<unsigned, bool> > bits; | |
272 ValidateTree(internal_node_head_ >> 8, -1, bits, free_internal_nodes, | |
273 free_external_nodes, &used_internal_nodes, | |
274 &used_external_nodes); | |
275 } | |
276 } | |
277 | |
278 // static | |
279 uint32 StrikeRegister::TimeFromBytes(const uint8 d[4]) { | |
280 return static_cast<uint32>(d[0]) << 24 | | |
281 static_cast<uint32>(d[1]) << 16 | | |
282 static_cast<uint32>(d[2]) << 8 | | |
283 static_cast<uint32>(d[3]); | |
284 } | |
285 | |
286 uint32 StrikeRegister::BestMatch(const uint8 v[24]) const { | |
287 if (internal_node_head_ == static_cast<uint32>(kNil)) { | |
288 return static_cast<uint32>(kNil); | |
289 } | |
290 | |
291 uint32 next = internal_node_head_ >> 8; | |
292 while ((next & static_cast<uint32>(kExternalFlag)) == 0) { | |
293 InternalNode* node = &internal_nodes_[next]; | |
294 uint8 b = v[node->critbyte()]; | |
295 unsigned direction = | |
296 (1 + static_cast<unsigned>(node->otherbits() | b)) >> 8; | |
297 next = node->child(direction); | |
298 } | |
299 | |
300 return next & ~static_cast<uint32>(kExternalFlag); | |
301 } | |
302 | |
303 uint32& StrikeRegister::external_node_next_ptr(unsigned i) { | |
304 return *reinterpret_cast<uint32*>(&external_nodes_[i * kExternalNodeSize]); | |
305 } | |
306 | |
307 uint8* StrikeRegister::external_node(unsigned i) { | |
308 return &external_nodes_[i * kExternalNodeSize]; | |
309 } | |
310 | |
311 uint32 StrikeRegister::GetFreeExternalNode() { | |
312 uint32 index = external_node_free_head_; | |
313 if (index == static_cast<uint32>(kNil)) { | |
314 DropNode(); | |
315 return GetFreeExternalNode(); | |
316 } | |
317 | |
318 external_node_free_head_ = external_node_next_ptr(index); | |
319 return index; | |
320 } | |
321 | |
322 uint32 StrikeRegister::GetFreeInternalNode() { | |
323 uint32 index = internal_node_free_head_; | |
324 if (index == static_cast<uint32>(kNil)) { | |
325 DropNode(); | |
326 return GetFreeInternalNode(); | |
327 } | |
328 | |
329 internal_node_free_head_ = internal_nodes_[index].next(); | |
330 return index; | |
331 } | |
332 | |
333 void StrikeRegister::DropNode() { | |
334 // DropNode should never be called on an empty tree. | |
335 DCHECK(internal_node_head_ != static_cast<uint32>(kNil)); | |
336 | |
337 // An internal node in a crit-bit tree always has exactly two children. | |
338 // This means that, if we are removing an external node (which is one of | |
339 // those children), then we also need to remove an internal node. In order | |
340 // to do that we keep pointers to the parent (wherep) and grandparent | |
341 // (whereq) when walking down the tree. | |
342 | |
343 uint32 p = internal_node_head_ >> 8, *wherep = &internal_node_head_, | |
344 *whereq = NULL; | |
345 while ((p & static_cast<uint32>(kExternalFlag)) == 0) { | |
346 whereq = wherep; | |
347 InternalNode* inode = &internal_nodes_[p]; | |
348 // We always go left, towards the smallest element, exploiting the fact | |
349 // that the timestamp is big-endian and at the start of the value. | |
350 wherep = &inode->data_[0]; | |
351 p = (*wherep) >> 8; | |
352 } | |
353 | |
354 const uint32 ext_index = p & ~static_cast<uint32>(kExternalFlag); | |
355 const uint8* ext_node = external_node(ext_index); | |
356 horizon_ = TimeFromBytes(ext_node); | |
357 | |
358 if (!whereq) { | |
359 // We are removing the last element in a tree. | |
360 internal_node_head_ = static_cast<uint32>(kNil); | |
361 FreeExternalNode(ext_index); | |
362 return; | |
363 } | |
364 | |
365 // |wherep| points to the left child pointer in the parent so we can add | |
366 // one and dereference to get the right child. | |
367 const uint32 other_child = wherep[1]; | |
368 FreeInternalNode((*whereq) >> 8); | |
369 *whereq = (*whereq & 0xff) | (other_child & 0xffffff00); | |
370 FreeExternalNode(ext_index); | |
371 } | |
372 | |
373 void StrikeRegister::FreeExternalNode(uint32 index) { | |
374 external_node_next_ptr(index) = external_node_free_head_; | |
375 external_node_free_head_ = index; | |
376 } | |
377 | |
378 void StrikeRegister::FreeInternalNode(uint32 index) { | |
379 internal_nodes_[index].SetNextPtr(internal_node_free_head_); | |
380 internal_node_free_head_ = index; | |
381 } | |
382 | |
383 void StrikeRegister::ValidateTree( | |
384 uint32 internal_node, | |
385 int last_bit, | |
386 const vector<pair<unsigned, bool> >& bits, | |
387 const set<uint32>& free_internal_nodes, | |
388 const set<uint32>& free_external_nodes, | |
389 set<uint32>* used_internal_nodes, | |
390 set<uint32>* used_external_nodes) { | |
391 CHECK_LT(internal_node, max_entries_); | |
392 const InternalNode* i = &internal_nodes_[internal_node]; | |
393 unsigned bit = 0; | |
394 switch (i->otherbits()) { | |
395 case 0x7f: | |
396 bit = 0; | |
397 break; | |
398 case 0xbf: | |
jar (doing other things)
2013/04/06 02:26:37
nit: much more readable might be:
switch (i->oth
ramant (doing other things)
2013/04/06 04:20:10
Done.
| |
399 bit = 1; | |
400 break; | |
401 case 0xdf: | |
402 bit = 2; | |
403 break; | |
404 case 0xef: | |
405 bit = 3; | |
406 break; | |
407 case 0xf7: | |
408 bit = 4; | |
409 break; | |
410 case 0xfb: | |
411 bit = 5; | |
412 break; | |
413 case 0xfd: | |
414 bit = 6; | |
415 break; | |
416 case 0xfe: | |
417 bit = 7; | |
418 break; | |
419 default: | |
420 CHECK(false); | |
421 } | |
422 | |
423 bit += 8 * i->critbyte(); | |
424 if (last_bit > -1) { | |
425 CHECK_GT(bit, static_cast<unsigned>(last_bit)); | |
426 } | |
427 | |
428 CHECK_EQ(free_internal_nodes.count(internal_node), (size_t)0); | |
429 | |
430 for (unsigned child = 0; child < 2; child++) { | |
431 if (i->child(child) & static_cast<uint32>(kExternalFlag)) { | |
432 uint32 ext = i->child(child) & ~static_cast<uint32>(kExternalFlag); | |
433 CHECK_EQ(free_external_nodes.count(ext), (size_t)0); | |
434 CHECK_EQ(used_external_nodes->count(ext), (size_t)0); | |
jar (doing other things)
2013/04/06 02:26:37
agl: Why are these CHECKs, and not DCHECKS?
ramant (doing other things)
2013/04/06 04:20:10
Done.
| |
435 used_external_nodes->insert(ext); | |
436 const uint8* bytes = external_node(ext); | |
437 for (vector<pair<unsigned, bool> >::const_iterator | |
438 i = bits.begin(); i != bits.end(); i++) { | |
439 unsigned byte = i->first / 8; | |
jar (doing other things)
2013/04/06 02:26:37
nit: Given the name, perhaps:
DCHECK_LE(byte, 0x
ramant (doing other things)
2013/04/06 04:20:10
Would like to leave this to agl to comment.
ramant (doing other things)
2013/04/07 05:03:32
Added DCHECK_LE(byte, 0xff); check
| |
440 unsigned bit = i->first % 8; | |
441 static const uint8 kMasks[8] = | |
442 {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01}; | |
jar (doing other things)
2013/04/06 02:26:37
nit: indent 2 more.
ramant (doing other things)
2013/04/06 04:20:10
Done.
| |
443 CHECK_EQ(bytes[byte] & kMasks[bit] ? true : false, i->second); | |
jar (doing other things)
2013/04/06 02:26:37
nit: rather than using ?: use != 0
ramant (doing other things)
2013/04/06 04:20:10
Done.
| |
444 } | |
445 } else { | |
446 uint32 inter = i->child(child); | |
447 vector<pair<unsigned, bool> > new_bits(bits); | |
448 new_bits.push_back(pair<unsigned, bool>(bit, child ? true : false)); | |
449 CHECK_EQ(free_internal_nodes.count(inter), (size_t)0); | |
450 CHECK_EQ(used_internal_nodes->count(inter), (size_t)0); | |
451 used_internal_nodes->insert(inter); | |
452 ValidateTree(inter, bit, bits, free_internal_nodes, free_external_nodes, | |
453 used_internal_nodes, used_external_nodes); | |
454 } | |
455 } | |
456 } | |
457 | |
458 } // namespace net | |
OLD | NEW |