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.
5 #ifndef BASE_UNGUESSABLE_TOKEN_H_
6 #define BASE_UNGUESSABLE_TOKEN_H_
13 #include "base/base_export.h"
14 #include "base/hash.h"
15 #include "base/logging.h"
19 struct UnguessableTokenHash;
21 // A UnguessableToken is an 128-bit token generated from a cryptographically
22 // strong random source. It can be used as part of a larger aggregate type,
23 // or as an ID in and of itself.
25 // UnguessableToken can be used to implement "Capability-Based Security".
26 // In other words, UnguessableToken can be used when the resource associated
27 // with the ID needs to be protected against manipulation by other untrusted
28 // agents in the system, and there is no other convenient way to verify the
29 // authority of the agent to do so (because the resource is part of a table
30 // shared across processes, for instance). In such a scheme, knowledge of the
31 // token value in and of itself is sufficient proof of authority to carry out
32 // an operation against the associated resource.
34 // Use Create() for creating new UnguessableTokens.
36 // NOTE: It is illegal to send empty UnguessableTokens across processes, and
37 // sending/receiving empty tokens should be treated as a security issue.
38 // If there is a valid scenario for sending "no token" across processes,
39 // base::Optional should be used instead of an empty token.
40 class BASE_EXPORT UnguessableToken {
42 // Create a unique UnguessableToken.
43 static UnguessableToken Create();
45 // Return a UnguessableToken built from the high/low bytes provided.
46 // It should only be used in deserialization scenarios.
48 // NOTE: If the deserialized token is empty, it means that it was never
49 // initialized via Create(). This is a security issue, and should be handled.
50 static UnguessableToken Deserialize(uint64_t high, uint64_t low);
52 // Creates an empty UnguessableToken.
53 // Assign to it with Create() before using it.
54 constexpr UnguessableToken() = default;
56 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
57 uint64_t GetHighForSerialization() const {
62 // NOTE: Serializing an empty UnguessableToken is an illegal operation.
63 uint64_t GetLowForSerialization() const {
68 bool is_empty() const { return high_ == 0 && low_ == 0; }
70 // Hex representation of the unguessable token.
71 std::string ToString() const;
73 explicit operator bool() const { return !is_empty(); }
75 bool operator<(const UnguessableToken& other) const {
76 return std::tie(high_, low_) < std::tie(other.high_, other.low_);
79 bool operator==(const UnguessableToken& other) const {
80 return high_ == other.high_ && low_ == other.low_;
83 bool operator!=(const UnguessableToken& other) const {
84 return !(*this == other);
88 friend struct UnguessableTokenHash;
89 UnguessableToken(uint64_t high, uint64_t low);
91 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a
92 // simpler ToString() and is_empty().
97 BASE_EXPORT std::ostream& operator<<(std::ostream& out,
98 const UnguessableToken& token);
100 // For use in std::unordered_map.
101 struct UnguessableTokenHash {
102 size_t operator()(const base::UnguessableToken& token) const {
104 return base::HashInts64(token.high_, token.low_);
110 #endif // BASE_UNGUESSABLE_TOKEN_H_