Index: base/nonce.h |
diff --git a/base/nonce.h b/base/nonce.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..49e30cdd58edbbbc8eb1f892f13200179a97a2bd |
--- /dev/null |
+++ b/base/nonce.h |
@@ -0,0 +1,84 @@ |
+// 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. |
+ |
+#ifndef BASE_NONCE_H_ |
+#define BASE_NONCE_H_ |
+ |
+#include <stdint.h> |
+#include <string.h> |
+#include <tuple> |
+ |
+#include "base/base_export.h" |
+#include "base/hash.h" |
+ |
+namespace base { |
+ |
+// A Nonce is an unguessable 128-bit token generated from a cryptographically |
+// strong random source. Nonce should be used when a sensitive ID needs to be |
+// unguessable. |
+// |
+// Use Generate() for creating new unguessable nonces. |
+// Deserialize() is a helper intended only for implementing IPC deserialization. |
+class BASE_EXPORT Nonce { |
+ public: |
+ // Create an unguessable and unique nonce. |
+ // USE ONLY THIS FUNCTION TO GENERATE NEW NONCES. |
+ static Nonce Generate(); |
+ |
+ // Returns a nonce built from the high/low bytes provided. |
+ // It should only be used in deserialization scenarios. |
+ // |
+ // NOTE: Trying to deserialize an empty Nonce is not a valid scenario, and is |
+ // likely the result of having forgotten to Generate() the Nonce. |
+ static Nonce Deserialize(uint64_t high, uint64_t low); |
+ |
+ // Create a zero-initialized empty Nonce. |
+ // NOTE: Sending an empty nonce across processes is an illegal operation. |
+ Nonce(); |
+ |
+ bool is_empty() const { return !(high_ | low_); } |
+ |
+ uint64_t high() const { |
+ DCHECK(!is_empty()); |
danakj
2016/09/15 18:06:37
I don't think you need to DCHECK these.
tguilbert
2016/09/15 22:57:40
Removed in newest iteration.
|
+ return high_; |
+ } |
+ |
+ uint64_t low() const { |
+ DCHECK(!is_empty()); |
+ return low_; |
+ } |
+ |
+ std::string ToString() const; |
+ |
+ bool operator<(const Nonce& other) const { |
+ return std::tie(high_, low_) < std::tie(other.high_, other.low_); |
+ } |
+ |
+ bool operator==(const Nonce& other) const { |
+ // Constant time comparison check. |
danakj
2016/09/15 18:06:37
I don't know what you mean by constant time. Compa
sandersd (OOO until July 31)
2016/09/15 18:52:26
I just want to call out here that constant-time do
Tom Sepez
2016/09/15 19:43:36
https://en.wikipedia.org/wiki/Timing_attack. Extr
tguilbert
2016/09/15 22:57:40
Removed the comment. Leaving the equality check as
|
+ return !((high_ ^ other.high_) | (low_ ^ other.low_)); |
+ } |
+ |
+ bool operator!=(const Nonce& other) const { return !operator==(other); } |
danakj
2016/09/15 18:06:37
nit: normally written !(*this == other)
tguilbert
2016/09/15 22:57:40
Done.
|
+ |
+ private: |
+ Nonce(uint64_t high, uint64_t low); |
+ |
+ // Note: Two uint64_t are used instead of uint8_t[16], in order to have a |
+ // simpler ToString() and is_null(). |
watk
2016/09/15 18:46:10
drive by: s/is_null/is_empty
tguilbert
2016/09/15 22:57:40
Done.
|
+ uint64_t high_; |
danakj
2016/09/15 18:06:37
can you "= 0" here and just "=default" the constr
tguilbert
2016/09/15 22:57:39
Done.
|
+ uint64_t low_; |
+}; |
+ |
+// For use in std::unordered_map. |
+struct NonceHash { |
+ size_t operator()(const base::Nonce& nonce) const { |
+ CHECK(!nonce.is_empty()); |
danakj
2016/09/15 18:06:36
DCHECK?
Also, you need to include base/logging.h
tguilbert
2016/09/15 22:57:40
Done.
|
+ return base::HashInts64(nonce.high(), nonce.low()); |
+ } |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_NONCE_H_ |