Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(603)

Side by Side Diff: base/nonce_token.h

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Adressed comments Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/BUILD.gn ('k') | base/nonce_token.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 static NonceToken Create();
36
37 // Return a NonceToken built from the high/low bytes provided.
38 // It should only be used in deserialization scenarios.
39 //
40 // NOTE: If |high| and |low| are both 0, the NonceToken we are trying to
41 // deserialize was never initialized, which is a security issue.
42 // If there is a way to securely and gracefully fail the deserialization,
43 // inputs should be checked using IsZeroData() before calling Deserialize().
44 // See ParamTraits<base::NonceToken>::Read(), or NonceToken's
45 // StructTraits::Read() for examples of securely failing a deserialization.
46 static NonceToken Deserialize(uint64_t high, uint64_t low);
47
48 // Helper function to check if the high/low bits passed are zeroed out.
49 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
50 return !(high | low);
51 }
52
53 // Creates an empty NonceToken. Assign to it with Create() before using it.
54 NonceToken() = default;
55
56 // Return |high_| and |low_| as out parameters.
57 // NOTE: Serializing an empty NonceToken is an illegal operation.
58 void Serialize(uint64_t* high_out, uint64_t* low_out) const;
59
60 bool is_empty() const { return IsZeroData(high_, low_); }
61
62 std::string ToString() const;
63
64 explicit operator bool() const { return !is_empty(); }
65
66 bool operator<(const NonceToken& other) const {
67 return std::tie(high_, low_) < std::tie(other.high_, other.low_);
68 }
69
70 bool operator==(const NonceToken& other) const {
71 // Based on crypto::SecureMemEqual().
72 // See crypto::SecureMemEqual()'s comments for more context.
danakj 2016/09/16 01:21:55 Thanks for this pointer.
73 return ((high_ ^ other.high_) | (low_ ^ other.low_)) == 0;
74 }
75
76 bool operator!=(const NonceToken& other) const { return !(*this == other); }
77
78 private:
79 friend struct NonceTokenHash;
80 NonceToken(uint64_t high, uint64_t low);
81
82 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a
83 // simpler ToString() and is_empty().
84 uint64_t high_ = 0;
85 uint64_t low_ = 0;
86 };
87
88 // For use in std::unordered_map.
89 struct NonceTokenHash {
90 size_t operator()(const base::NonceToken& nonce) const {
91 DCHECK(nonce);
92 return base::HashInts64(nonce.high_, nonce.low_);
93 }
94 };
95
96 } // namespace base
97
98 #endif // BASE_NONCE_TOKEN_H_
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/nonce_token.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698