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

Side by Side Diff: base/nonce.h

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: Addressed comments. Added CHECKs, const time ==. 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.cc » ('j') | base/nonce.cc » ('J')
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_H_
6 #define BASE_NONCE_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
15 namespace base {
16
17 // A Nonce is an unguessable 128-bit token generated from a cryptographically
18 // strong random source. Nonce should be used when a sensitive ID needs to be
19 // unguessable.
20 //
21 // Use Generate() for creating new unguessable nonces.
22 // Deserialize() is a helper intended only for implementing IPC deserialization.
23 class BASE_EXPORT Nonce {
24 public:
25 // Create an unguessable and unique nonce.
26 // USE ONLY THIS FUNCTION TO GENERATE NEW NONCES.
27 static Nonce Generate();
28
29 // Returns a nonce built from the high/low bytes provided.
30 // It should only be used in deserialization scenarios.
31 //
32 // NOTE: Trying to deserialize an empty Nonce is not a valid scenario, and is
33 // likely the result of having forgotten to Generate() the Nonce.
34 static Nonce Deserialize(uint64_t high, uint64_t low);
35
36 // Create a zero-initialized empty Nonce.
37 // NOTE: Sending an empty nonce across processes is an illegal operation.
38 Nonce();
39
40 bool is_empty() const { return !(high_ | low_); }
41
42 uint64_t high() const {
43 DCHECK(!is_empty());
danakj 2016/09/15 18:06:37 I don't think you need to DCHECK these.
tguilbert 2016/09/15 22:57:40 Removed in newest iteration.
44 return high_;
45 }
46
47 uint64_t low() const {
48 DCHECK(!is_empty());
49 return low_;
50 }
51
52 std::string ToString() const;
53
54 bool operator<(const Nonce& other) const {
55 return std::tie(high_, low_) < std::tie(other.high_, other.low_);
56 }
57
58 bool operator==(const Nonce& other) const {
59 // Constant time comparison check.
danakj 2016/09/15 18:06:37 I don't know what you mean by constant time. Compa
sandersd (OOO until July 31) 2016/09/15 18:52:26 I just want to call out here that constant-time do
Tom Sepez 2016/09/15 19:43:36 https://en.wikipedia.org/wiki/Timing_attack. Extr
tguilbert 2016/09/15 22:57:40 Removed the comment. Leaving the equality check as
60 return !((high_ ^ other.high_) | (low_ ^ other.low_));
61 }
62
63 bool operator!=(const Nonce& other) const { return !operator==(other); }
danakj 2016/09/15 18:06:37 nit: normally written !(*this == other)
tguilbert 2016/09/15 22:57:40 Done.
64
65 private:
66 Nonce(uint64_t high, uint64_t low);
67
68 // Note: Two uint64_t are used instead of uint8_t[16], in order to have a
69 // simpler ToString() and is_null().
watk 2016/09/15 18:46:10 drive by: s/is_null/is_empty
tguilbert 2016/09/15 22:57:40 Done.
70 uint64_t high_;
danakj 2016/09/15 18:06:37 can you "= 0" here and just "=default" the constr
tguilbert 2016/09/15 22:57:39 Done.
71 uint64_t low_;
72 };
73
74 // For use in std::unordered_map.
75 struct NonceHash {
76 size_t operator()(const base::Nonce& nonce) const {
77 CHECK(!nonce.is_empty());
danakj 2016/09/15 18:06:36 DCHECK? Also, you need to include base/logging.h
tguilbert 2016/09/15 22:57:40 Done.
78 return base::HashInts64(nonce.high(), nonce.low());
79 }
80 };
81
82 } // namespace base
83
84 #endif // BASE_NONCE_H_
OLDNEW
« no previous file with comments | « base/BUILD.gn ('k') | base/nonce.cc » ('j') | base/nonce.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698