Index: base/nonce_token.h |
diff --git a/base/nonce_token.h b/base/nonce_token.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d40eda57ca1cb4cf5afd12f8dd61957632e005ff |
--- /dev/null |
+++ b/base/nonce_token.h |
@@ -0,0 +1,94 @@ |
+// 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_TOKEN_H_ |
+#define BASE_NONCE_TOKEN_H_ |
+ |
+#include <stdint.h> |
+#include <string.h> |
+#include <tuple> |
+ |
+#include "base/base_export.h" |
+#include "base/hash.h" |
+#include "base/logging.h" |
+ |
+namespace base { |
+ |
+struct NonceTokenHash; |
+ |
+// A NonceToken is an unguessable 128-bit token generated from a |
+// cryptographically strong random source. |
+// |
+// NonceToken should be used when a sensitive ID needs to be unguessable, and is |
+// shared across processes. It can be used as part of a larger aggregate type, |
+// or as an ID in and of itself. |
+// |
+// Use Create() for creating new unguessable nonce tokens. |
+// |
+// NOTE: A NonceToken is not semantically a "cryptographic nonce", since we |
+// consider a zeroed out NonceToken to be a legal value. It is however illegal |
+// to send empty NonceTokens across processes. |
+class BASE_EXPORT NonceToken { |
+ public: |
+ // Create an unguessable and unique NonceToken. |
+ // USE ONLY THIS FUNCTION TO GENERATE NEW NONCES. |
danakj
2016/09/15 23:35:16
I'd prefer less capslock in comments. The comment
tguilbert
2016/09/16 01:03:01
Done.
|
+ static NonceToken Create(); |
+ |
+ // Create a zeroed out NonceToken. |
+ // NOTE: The NonceToken still needs to be initialized via Create(). |
danakj
2016/09/15 23:35:16
How about // Creates an empty NonceToken. Assign t
tguilbert
2016/09/16 01:03:01
Nicely worded, thank you! Done.
|
+ NonceToken() = default; |
+ |
+ // Return |high_| and |low_| as out parameters. |
+ // NOTE: Serializing an empty NonceToken is an illegal operation. |
+ void Serialize(uint64_t* high_out, uint64_t* low_out) const; |
+ |
+ // Return a NonceToken built from the high/low bytes provided. |
+ // It should only be used in deserialization scenarios. |
+ // |
+ // NOTE: If |high| and |low| are both 0, the NonceToken we are trying to |
+ // deserialize was never initialized, which is a security issue. |
+ static NonceToken Deserialize(uint64_t high, uint64_t low); |
danakj
2016/09/15 23:35:16
It's easier to read if the static methods are grou
tguilbert
2016/09/16 01:03:01
Done.
|
+ |
+ // Helper function to check if the high/low bits passed are zeroed out. |
+ static inline bool IsZeroData(uint64_t high, uint64_t low) { |
danakj
2016/09/15 23:35:16
Can be private? Why isn't it used for is_empty() a
tguilbert
2016/09/16 01:03:01
I am using it in the IPC and Mojo deserialization
|
+ return !(high | low); |
+ } |
+ |
+ bool is_empty() const { return !(high_ | low_); } |
+ |
+ std::string ToString() const; |
+ |
+ explicit operator bool() const { return !is_empty(); } |
+ |
+ bool operator<(const NonceToken& other) const { |
+ return std::tie(high_, low_) < std::tie(other.high_, other.low_); |
+ } |
+ |
+ bool operator==(const NonceToken& other) const { |
+ return !((high_ ^ other.high_) | (low_ ^ other.low_)); |
danakj
2016/09/15 23:35:16
Either just do high_==other.high_ && low==other.lo
tguilbert
2016/09/16 01:03:01
I think SecureMemEqual's comments give good contex
|
+ } |
+ |
+ bool operator!=(const NonceToken& other) const { return !(*this == other); } |
+ |
+ private: |
+ friend struct NonceTokenHash; |
+ NonceToken(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_empty(). |
+ uint64_t high_ = 0; |
+ uint64_t low_ = 0; |
+}; |
+ |
+// For use in std::unordered_map. |
+struct NonceTokenHash { |
+ size_t operator()(const base::NonceToken& nonce) const { |
+ DCHECK(nonce); |
+ return base::HashInts64(nonce.high_, nonce.low_); |
+ } |
+}; |
+ |
+} // namespace base |
+ |
+#endif // BASE_NONCE_TOKEN_H_ |