Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 "base/nonce.h" | |
| 6 | |
| 7 #include "base/format_macros.h" | |
| 8 #include "base/rand_util.h" | |
| 9 #include "base/strings/stringprintf.h" | |
| 10 | |
| 11 namespace base { | |
| 12 | |
| 13 // If base::Nonce is no longer 128 bits, the IPC serialization logic and Mojo | |
| 14 // StructTraits should be updated to match the size of the struct. | |
| 15 static_assert(sizeof(Nonce) == 2 * sizeof(uint64_t), | |
| 16 "base::Nonce should be of size 2 * sizeof(uint64_t)."); | |
| 17 | |
| 18 Nonce::Nonce() : high_(0), low_(0) {} | |
| 19 | |
| 20 Nonce::Nonce(uint64_t high, uint64_t low) : high_(high), low_(low) {} | |
| 21 | |
| 22 std::string Nonce::ToString() const { | |
| 23 return base::StringPrintf("(%" PRIu64 ":%" PRIu64 ")", high_, low_); | |
| 24 } | |
| 25 | |
| 26 // static | |
| 27 Nonce Nonce::Generate() { | |
| 28 Nonce nonce; | |
| 29 // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the | |
| 30 // base version directly, and to prevent the dependency from base/ to crypto/. | |
| 31 base::RandBytes(&nonce, sizeof(nonce)); | |
| 32 return nonce; | |
| 33 } | |
| 34 | |
| 35 // static | |
| 36 Nonce Nonce::Deserialize(uint64_t high, uint64_t low) { | |
| 37 // Make sure we are not trying to deserialize an empty nonce. | |
| 38 // Sending an empty nonce accross processes likely means that | |
|
watk
2016/09/15 18:46:10
across
tguilbert
2016/09/15 22:57:39
Done.
| |
| 39 // Nonce::Generate() was never called, which points to a security hole. | |
| 40 CHECK((high | low)); | |
|
danakj
2016/09/15 18:06:36
DCHECK?
tguilbert
2016/09/15 22:57:39
I think this CHECK is important. There is no case
| |
| 41 return Nonce(high, low); | |
| 42 } | |
| 43 | |
| 44 } // namespace base | |
| OLD | NEW |