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_NONCE_TOKEN_H_ | |
6 #define BASE_NONCE_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 NonceTokenHash; | |
19 | |
20 // A NonceToken is an unguessable 128-bit token generated from a | |
21 // cryptographically strong random source. | |
22 // | |
23 // NonceToken should be used when a sensitive ID needs to be unguessable, and is | |
24 // shared across processes. It can be used as part of a larger aggregate type, | |
25 // or as an ID in and of itself. | |
26 // | |
27 // Use Create() for creating new unguessable nonce tokens. | |
28 // | |
29 // NOTE: A NonceToken is not semantically a "cryptographic nonce", since we | |
30 // consider a zeroed out NonceToken to be a legal value. It is however illegal | |
31 // to send empty NonceTokens across processes. | |
32 class BASE_EXPORT NonceToken { | |
33 public: | |
34 // Create an unguessable and unique NonceToken. | |
35 // 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.
| |
36 static NonceToken Create(); | |
37 | |
38 // Create a zeroed out NonceToken. | |
39 // 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.
| |
40 NonceToken() = default; | |
41 | |
42 // Return |high_| and |low_| as out parameters. | |
43 // NOTE: Serializing an empty NonceToken is an illegal operation. | |
44 void Serialize(uint64_t* high_out, uint64_t* low_out) const; | |
45 | |
46 // Return a NonceToken built from the high/low bytes provided. | |
47 // It should only be used in deserialization scenarios. | |
48 // | |
49 // NOTE: If |high| and |low| are both 0, the NonceToken we are trying to | |
50 // deserialize was never initialized, which is a security issue. | |
51 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.
| |
52 | |
53 // Helper function to check if the high/low bits passed are zeroed out. | |
54 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
| |
55 return !(high | low); | |
56 } | |
57 | |
58 bool is_empty() const { return !(high_ | low_); } | |
59 | |
60 std::string ToString() const; | |
61 | |
62 explicit operator bool() const { return !is_empty(); } | |
63 | |
64 bool operator<(const NonceToken& other) const { | |
65 return std::tie(high_, low_) < std::tie(other.high_, other.low_); | |
66 } | |
67 | |
68 bool operator==(const NonceToken& other) const { | |
69 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
| |
70 } | |
71 | |
72 bool operator!=(const NonceToken& other) const { return !(*this == other); } | |
73 | |
74 private: | |
75 friend struct NonceTokenHash; | |
76 NonceToken(uint64_t high, uint64_t low); | |
77 | |
78 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a | |
79 // simpler ToString() and is_empty(). | |
80 uint64_t high_ = 0; | |
81 uint64_t low_ = 0; | |
82 }; | |
83 | |
84 // For use in std::unordered_map. | |
85 struct NonceTokenHash { | |
86 size_t operator()(const base::NonceToken& nonce) const { | |
87 DCHECK(nonce); | |
88 return base::HashInts64(nonce.high_, nonce.low_); | |
89 } | |
90 }; | |
91 | |
92 } // namespace base | |
93 | |
94 #endif // BASE_NONCE_TOKEN_H_ | |
OLD | NEW |