OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/quic/core/crypto/strike_register.h" | 5 #include "net/quic/core/crypto/strike_register.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <limits> | 8 #include <limits> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 const uint32_t current_time = ExternalTimeToInternal(current_time_external); | 143 const uint32_t current_time = ExternalTimeToInternal(current_time_external); |
144 | 144 |
145 // Check to see if the orbit is correct. | 145 // Check to see if the orbit is correct. |
146 if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) { | 146 if (memcmp(nonce + sizeof(current_time), orbit_, sizeof(orbit_))) { |
147 return NONCE_INVALID_ORBIT_FAILURE; | 147 return NONCE_INVALID_ORBIT_FAILURE; |
148 } | 148 } |
149 | 149 |
150 const uint32_t nonce_time = ExternalTimeToInternal(TimeFromBytes(nonce)); | 150 const uint32_t nonce_time = ExternalTimeToInternal(TimeFromBytes(nonce)); |
151 | 151 |
152 // Check that the timestamp is in the valid range. | 152 // Check that the timestamp is in the valid range. |
153 pair<uint32_t, uint32_t> valid_range = | 153 std::pair<uint32_t, uint32_t> valid_range = |
154 StrikeRegister::GetValidRange(current_time); | 154 StrikeRegister::GetValidRange(current_time); |
155 if (nonce_time < valid_range.first || nonce_time > valid_range.second) { | 155 if (nonce_time < valid_range.first || nonce_time > valid_range.second) { |
156 return NONCE_INVALID_TIME_FAILURE; | 156 return NONCE_INVALID_TIME_FAILURE; |
157 } | 157 } |
158 | 158 |
159 // We strip the orbit out of the nonce. | 159 // We strip the orbit out of the nonce. |
160 uint8_t value[24]; | 160 uint8_t value[24]; |
161 memcpy(value, nonce, sizeof(nonce_time)); | 161 memcpy(value, nonce, sizeof(nonce_time)); |
162 memcpy(value + sizeof(nonce_time), | 162 memcpy(value + sizeof(nonce_time), |
163 nonce + sizeof(nonce_time) + sizeof(orbit_), | 163 nonce + sizeof(nonce_time) + sizeof(orbit_), |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 return NONCE_OK; | 269 return NONCE_OK; |
270 } | 270 } |
271 | 271 |
272 const uint8_t* StrikeRegister::orbit() const { | 272 const uint8_t* StrikeRegister::orbit() const { |
273 return orbit_; | 273 return orbit_; |
274 } | 274 } |
275 | 275 |
276 uint32_t StrikeRegister::GetCurrentValidWindowSecs( | 276 uint32_t StrikeRegister::GetCurrentValidWindowSecs( |
277 uint32_t current_time_external) const { | 277 uint32_t current_time_external) const { |
278 uint32_t current_time = ExternalTimeToInternal(current_time_external); | 278 uint32_t current_time = ExternalTimeToInternal(current_time_external); |
279 pair<uint32_t, uint32_t> valid_range = | 279 std::pair<uint32_t, uint32_t> valid_range = |
280 StrikeRegister::GetValidRange(current_time); | 280 StrikeRegister::GetValidRange(current_time); |
281 if (valid_range.second >= valid_range.first) { | 281 if (valid_range.second >= valid_range.first) { |
282 return valid_range.second - current_time + 1; | 282 return valid_range.second - current_time + 1; |
283 } else { | 283 } else { |
284 return 0; | 284 return 0; |
285 } | 285 } |
286 } | 286 } |
287 | 287 |
288 void StrikeRegister::Validate() { | 288 void StrikeRegister::Validate() { |
289 set<uint32_t> free_internal_nodes; | 289 std::set<uint32_t> free_internal_nodes; |
290 for (uint32_t i = internal_node_free_head_; i != kNil; | 290 for (uint32_t i = internal_node_free_head_; i != kNil; |
291 i = internal_nodes_[i].next()) { | 291 i = internal_nodes_[i].next()) { |
292 CHECK_LT(i, max_entries_); | 292 CHECK_LT(i, max_entries_); |
293 CHECK_EQ(free_internal_nodes.count(i), 0u); | 293 CHECK_EQ(free_internal_nodes.count(i), 0u); |
294 free_internal_nodes.insert(i); | 294 free_internal_nodes.insert(i); |
295 } | 295 } |
296 | 296 |
297 set<uint32_t> free_external_nodes; | 297 std::set<uint32_t> free_external_nodes; |
298 for (uint32_t i = external_node_free_head_; i != kNil; | 298 for (uint32_t i = external_node_free_head_; i != kNil; |
299 i = external_node_next_ptr(i)) { | 299 i = external_node_next_ptr(i)) { |
300 CHECK_LT(i, max_entries_); | 300 CHECK_LT(i, max_entries_); |
301 CHECK_EQ(free_external_nodes.count(i), 0u); | 301 CHECK_EQ(free_external_nodes.count(i), 0u); |
302 free_external_nodes.insert(i); | 302 free_external_nodes.insert(i); |
303 } | 303 } |
304 | 304 |
305 set<uint32_t> used_external_nodes; | 305 std::set<uint32_t> used_external_nodes; |
306 set<uint32_t> used_internal_nodes; | 306 std::set<uint32_t> used_internal_nodes; |
307 | 307 |
308 if (internal_node_head_ != kNil && | 308 if (internal_node_head_ != kNil && |
309 ((internal_node_head_ >> 8) & kExternalFlag) == 0) { | 309 ((internal_node_head_ >> 8) & kExternalFlag) == 0) { |
310 vector<pair<unsigned, bool>> bits; | 310 std::vector<std::pair<unsigned, bool>> bits; |
311 ValidateTree(internal_node_head_ >> 8, -1, bits, free_internal_nodes, | 311 ValidateTree(internal_node_head_ >> 8, -1, bits, free_internal_nodes, |
312 free_external_nodes, &used_internal_nodes, | 312 free_external_nodes, &used_internal_nodes, |
313 &used_external_nodes); | 313 &used_external_nodes); |
314 } | 314 } |
315 } | 315 } |
316 | 316 |
317 // static | 317 // static |
318 uint32_t StrikeRegister::TimeFromBytes(const uint8_t d[4]) { | 318 uint32_t StrikeRegister::TimeFromBytes(const uint8_t d[4]) { |
319 return static_cast<uint32_t>(d[0]) << 24 | static_cast<uint32_t>(d[1]) << 16 | | 319 return static_cast<uint32_t>(d[0]) << 24 | static_cast<uint32_t>(d[1]) << 16 | |
320 static_cast<uint32_t>(d[2]) << 8 | static_cast<uint32_t>(d[3]); | 320 static_cast<uint32_t>(d[2]) << 8 | static_cast<uint32_t>(d[3]); |
321 } | 321 } |
322 | 322 |
323 pair<uint32_t, uint32_t> StrikeRegister::GetValidRange( | 323 std::pair<uint32_t, uint32_t> StrikeRegister::GetValidRange( |
324 uint32_t current_time_internal) const { | 324 uint32_t current_time_internal) const { |
325 if (current_time_internal < horizon_) { | 325 if (current_time_internal < horizon_) { |
326 // Empty valid range. | 326 // Empty valid range. |
327 return std::make_pair(std::numeric_limits<uint32_t>::max(), 0); | 327 return std::make_pair(std::numeric_limits<uint32_t>::max(), 0); |
328 } | 328 } |
329 | 329 |
330 uint32_t lower_bound; | 330 uint32_t lower_bound; |
331 if (current_time_internal >= window_secs_) { | 331 if (current_time_internal >= window_secs_) { |
332 lower_bound = std::max(horizon_, current_time_internal - window_secs_); | 332 lower_bound = std::max(horizon_, current_time_internal - window_secs_); |
333 } else { | 333 } else { |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 void StrikeRegister::FreeExternalNode(uint32_t index) { | 435 void StrikeRegister::FreeExternalNode(uint32_t index) { |
436 external_node_next_ptr(index) = external_node_free_head_; | 436 external_node_next_ptr(index) = external_node_free_head_; |
437 external_node_free_head_ = index; | 437 external_node_free_head_ = index; |
438 } | 438 } |
439 | 439 |
440 void StrikeRegister::FreeInternalNode(uint32_t index) { | 440 void StrikeRegister::FreeInternalNode(uint32_t index) { |
441 internal_nodes_[index].SetNextPtr(internal_node_free_head_); | 441 internal_nodes_[index].SetNextPtr(internal_node_free_head_); |
442 internal_node_free_head_ = index; | 442 internal_node_free_head_ = index; |
443 } | 443 } |
444 | 444 |
445 void StrikeRegister::ValidateTree(uint32_t internal_node, | 445 void StrikeRegister::ValidateTree( |
446 int last_bit, | 446 uint32_t internal_node, |
447 const vector<pair<unsigned, bool>>& bits, | 447 int last_bit, |
448 const set<uint32_t>& free_internal_nodes, | 448 const std::vector<std::pair<unsigned, bool>>& bits, |
449 const set<uint32_t>& free_external_nodes, | 449 const std::set<uint32_t>& free_internal_nodes, |
450 set<uint32_t>* used_internal_nodes, | 450 const std::set<uint32_t>& free_external_nodes, |
451 set<uint32_t>* used_external_nodes) { | 451 std::set<uint32_t>* used_internal_nodes, |
| 452 std::set<uint32_t>* used_external_nodes) { |
452 CHECK_LT(internal_node, max_entries_); | 453 CHECK_LT(internal_node, max_entries_); |
453 const InternalNode* i = &internal_nodes_[internal_node]; | 454 const InternalNode* i = &internal_nodes_[internal_node]; |
454 unsigned bit = 0; | 455 unsigned bit = 0; |
455 switch (i->otherbits()) { | 456 switch (i->otherbits()) { |
456 case 0xff & ~(1 << 7): | 457 case 0xff & ~(1 << 7): |
457 bit = 0; | 458 bit = 0; |
458 break; | 459 break; |
459 case 0xff & ~(1 << 6): | 460 case 0xff & ~(1 << 6): |
460 bit = 1; | 461 bit = 1; |
461 break; | 462 break; |
(...skipping 26 matching lines...) Expand all Loading... |
488 | 489 |
489 CHECK_EQ(free_internal_nodes.count(internal_node), 0u); | 490 CHECK_EQ(free_internal_nodes.count(internal_node), 0u); |
490 | 491 |
491 for (unsigned child = 0; child < 2; child++) { | 492 for (unsigned child = 0; child < 2; child++) { |
492 if (i->child(child) & kExternalFlag) { | 493 if (i->child(child) & kExternalFlag) { |
493 uint32_t ext = i->child(child) & ~kExternalFlag; | 494 uint32_t ext = i->child(child) & ~kExternalFlag; |
494 CHECK_EQ(free_external_nodes.count(ext), 0u); | 495 CHECK_EQ(free_external_nodes.count(ext), 0u); |
495 CHECK_EQ(used_external_nodes->count(ext), 0u); | 496 CHECK_EQ(used_external_nodes->count(ext), 0u); |
496 used_external_nodes->insert(ext); | 497 used_external_nodes->insert(ext); |
497 const uint8_t* bytes = external_node(ext); | 498 const uint8_t* bytes = external_node(ext); |
498 for (const pair<unsigned, bool>& pair : bits) { | 499 for (const std::pair<unsigned, bool>& pair : bits) { |
499 unsigned byte = pair.first / 8; | 500 unsigned byte = pair.first / 8; |
500 DCHECK_LE(byte, 0xffu); | 501 DCHECK_LE(byte, 0xffu); |
501 unsigned bit_new = pair.first % 8; | 502 unsigned bit_new = pair.first % 8; |
502 static const uint8_t kMasks[8] = {0x80, 0x40, 0x20, 0x10, | 503 static const uint8_t kMasks[8] = {0x80, 0x40, 0x20, 0x10, |
503 0x08, 0x04, 0x02, 0x01}; | 504 0x08, 0x04, 0x02, 0x01}; |
504 CHECK_EQ((bytes[byte] & kMasks[bit_new]) != 0, pair.second); | 505 CHECK_EQ((bytes[byte] & kMasks[bit_new]) != 0, pair.second); |
505 } | 506 } |
506 } else { | 507 } else { |
507 uint32_t inter = i->child(child); | 508 uint32_t inter = i->child(child); |
508 vector<pair<unsigned, bool>> new_bits(bits); | 509 std::vector<std::pair<unsigned, bool>> new_bits(bits); |
509 new_bits.push_back(pair<unsigned, bool>(bit, child != 0)); | 510 new_bits.push_back(std::pair<unsigned, bool>(bit, child != 0)); |
510 CHECK_EQ(free_internal_nodes.count(inter), 0u); | 511 CHECK_EQ(free_internal_nodes.count(inter), 0u); |
511 CHECK_EQ(used_internal_nodes->count(inter), 0u); | 512 CHECK_EQ(used_internal_nodes->count(inter), 0u); |
512 used_internal_nodes->insert(inter); | 513 used_internal_nodes->insert(inter); |
513 ValidateTree(inter, bit, bits, free_internal_nodes, free_external_nodes, | 514 ValidateTree(inter, bit, bits, free_internal_nodes, free_external_nodes, |
514 used_internal_nodes, used_external_nodes); | 515 used_internal_nodes, used_external_nodes); |
515 } | 516 } |
516 } | 517 } |
517 } | 518 } |
518 | 519 |
519 } // namespace net | 520 } // namespace net |
OLD | NEW |