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 #ifndef BASE_UNGUESSABLE_TOKEN_H_ | |
6 #define BASE_UNGUESSABLE_TOKEN_H_ | |
7 | |
8 #include <stdint.h> | |
9 #include <string.h> | |
10 #include <tuple> | |
11 | |
12 #include "base/base_export.h" | |
13 #include "base/hash.h" | |
14 #include "base/logging.h" | |
15 | |
16 namespace base { | |
17 | |
18 struct UnguessableTokenHash; | |
19 | |
20 // A UnguessableToken is an 128-bit token generated from a cryptographically | |
21 // strong random source. | |
22 // | |
23 // UnguessableToken should be used when a sensitive ID needs to be unguessable, | |
24 // and is shared across processes. It can be used as part of a larger aggregate | |
25 // type, or as an ID in and of itself. | |
26 // | |
27 // Use Create() for creating new UnguessableTokens. | |
28 // | |
29 // NOTE: It is illegal to send empty UnguessableTokens across processes, and | |
30 // sending/receiving empty tokens should be treated as a security issue. | |
31 // If there is a valid scenario for sending "no token" across processes, | |
32 // base::Optional should be used instead of an empty token. | |
33 class BASE_EXPORT UnguessableToken { | |
34 public: | |
35 // Create a unique UnguessableToken. | |
36 static UnguessableToken Create(); | |
37 | |
38 // Return a UnguessableToken built from the high/low bytes provided. | |
39 // It should only be used in deserialization scenarios. | |
40 // | |
41 // NOTE: If the deserialized token is empty, it means that it was never | |
42 // initialized via Create(). This is a security issue, and should be handled. | |
43 static UnguessableToken Deserialize(uint64_t high, uint64_t low); | |
44 | |
45 // Creates an empty UnguessableToken. | |
46 // Assign to it with Create() before using it. | |
47 UnguessableToken() = default; | |
48 | |
49 // Return |high_| and |low_| as out parameters. | |
50 // NOTE: Serializing an empty UnguessableToken is an illegal operation. | |
51 void Serialize(uint64_t* high_out, uint64_t* low_out) const; | |
52 | |
53 bool is_empty() const { return high_ == 0 && low_ == 0; } | |
54 | |
55 std::string ToString() const; | |
56 | |
57 explicit operator bool() const { return !is_empty(); } | |
58 | |
59 bool operator<(const UnguessableToken& other) const { | |
60 return std::tie(high_, low_) < std::tie(other.high_, other.low_); | |
61 } | |
62 | |
63 bool operator==(const UnguessableToken& other) const { | |
64 // Based on crypto::SecureMemEqual(). | |
sandersd (OOO until July 31)
2016/09/16 22:13:55
I am still opposed to suggesting that the implemen
danakj
2016/09/16 22:21:35
I'm super fine with just doing high==other.high&&l
tguilbert
2016/09/16 22:48:14
Done.
| |
65 // See crypto::SecureMemEqual()'s comments for more context. | |
66 return ((high_ ^ other.high_) | (low_ ^ other.low_)) == 0; | |
67 } | |
68 | |
69 bool operator!=(const UnguessableToken& other) const { | |
70 return !(*this == other); | |
71 } | |
72 | |
73 private: | |
74 friend struct UnguessableTokenHash; | |
75 UnguessableToken(uint64_t high, uint64_t low); | |
76 | |
77 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a | |
78 // simpler ToString() and is_empty(). | |
79 uint64_t high_ = 0; | |
80 uint64_t low_ = 0; | |
81 }; | |
82 | |
83 // For use in std::unordered_map. | |
84 struct UnguessableTokenHash { | |
85 size_t operator()(const base::UnguessableToken& token) const { | |
86 DCHECK(token); | |
87 return base::HashInts64(token.high_, token.low_); | |
88 } | |
89 }; | |
90 | |
91 } // namespace base | |
92 | |
93 #endif // BASE_UNGUESSABLE_TOKEN_H_ | |
OLD | NEW |