Index: base/nonce_token.h |
diff --git a/base/nonce_token.h b/base/nonce_token.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..47fbf6adbf75714d7efdb18834a35958e38e615d |
--- /dev/null |
+++ b/base/nonce_token.h |
@@ -0,0 +1,98 @@ |
+// 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. |
+ static NonceToken Create(); |
+ |
+ // 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. |
+ // If there is a way to securely and gracefully fail the deserialization, |
+ // inputs should be checked using IsZeroData() before calling Deserialize(). |
+ // See ParamTraits<base::NonceToken>::Read(), or NonceToken's |
+ // StructTraits::Read() for examples of securely failing a deserialization. |
+ static NonceToken Deserialize(uint64_t high, uint64_t low); |
+ |
+ // 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/16 01:21:55
I think this should not be part of this api, here'
tguilbert
2016/09/16 22:16:40
I was not concerned about timing attacks with this
|
+ return !(high | low); |
+ } |
+ |
+ // Creates an empty NonceToken. Assign to it with Create() before using it. |
+ 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; |
+ |
+ bool is_empty() const { return IsZeroData(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 { |
+ // Based on crypto::SecureMemEqual(). |
+ // See crypto::SecureMemEqual()'s comments for more context. |
danakj
2016/09/16 01:21:55
Thanks for this pointer.
|
+ return ((high_ ^ other.high_) | (low_ ^ other.low_)) == 0; |
+ } |
+ |
+ 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_ |