Index: base/nonce_token.cc |
diff --git a/base/nonce_token.cc b/base/nonce_token.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..dc6395dd3f116af965b9c464293822e980783c63 |
--- /dev/null |
+++ b/base/nonce_token.cc |
@@ -0,0 +1,48 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/nonce_token.h" |
+ |
+#include "base/format_macros.h" |
+#include "base/rand_util.h" |
+#include "base/strings/stringprintf.h" |
+ |
+namespace base { |
+ |
+// If base::NonceToken is no longer 128 bits, the IPC serialization logic and |
+// Mojo StructTraits should be updated to match the size of the struct. |
+static_assert(sizeof(NonceToken) == 2 * sizeof(uint64_t), |
danakj
2016/09/15 23:35:15
Can you put this assert at the site(s) of the code
tguilbert
2016/09/16 01:03:01
Done.
|
+ "base::NonceToken should be of size 2 * sizeof(uint64_t)."); |
+ |
+NonceToken::NonceToken(uint64_t high, uint64_t low) : high_(high), low_(low) {} |
+ |
+std::string NonceToken::ToString() const { |
+ return base::StringPrintf("(%" PRIu64 ":%" PRIu64 ")", high_, low_); |
+} |
+ |
+// static |
+NonceToken NonceToken::Create() { |
+ NonceToken token; |
+ // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the |
+ // base version directly, and to prevent the dependency from base/ to crypto/. |
+ base::RandBytes(&token, sizeof(token)); |
+ return token; |
+} |
+ |
+void NonceToken::Serialize(uint64_t* high_out, uint64_t* low_out) const { |
+ // Serializing an uninitialized NonceToken is a security issue. |
+ CHECK(!is_empty()); |
danakj
2016/09/15 23:35:16
Why not DCHECK?
tguilbert
2016/09/16 01:03:01
Empty NonceTokens can be copied and assigned norma
danakj
2016/09/16 01:21:55
You're right it's a security issue but you can't t
tguilbert
2016/09/16 22:16:40
Ah, I understand the point you are making.
My con
|
+ *high_out = high_; |
+ *low_out = low_; |
+} |
+ |
+// static |
+NonceToken NonceToken::Deserialize(uint64_t high, uint64_t low) { |
+ // Sending a zeroed out NonceToken across processes means that it was never |
+ // initialized via NonceToken::Create(), which is a security issue. |
+ CHECK(!NonceToken::IsZeroData(high, low)); |
danakj
2016/09/15 23:35:16
I don't think CHECK is appropriate here, it means
tguilbert
2016/09/16 01:03:01
The IPC and the Mojo deserialization code use Nonc
danakj
2016/09/16 01:21:55
That sounds good, but then I don't think you need
|
+ return NonceToken(high, low); |
+} |
+ |
+} // namespace base |