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

Side by Side Diff: base/nonce_token.cc

Issue 2333443002: Add base::UnguessableToken (Closed)
Patch Set: NonceTokenHash CHECK --> DCHECK 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
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 #include "base/nonce_token.h"
6
7 #include "base/format_macros.h"
8 #include "base/rand_util.h"
9 #include "base/strings/stringprintf.h"
10
11 namespace base {
12
13 // If base::NonceToken is no longer 128 bits, the IPC serialization logic and
14 // Mojo StructTraits should be updated to match the size of the struct.
15 static_assert(sizeof(NonceToken) == 2 * sizeof(uint64_t),
danakj 2016/09/15 23:35:15 Can you put this assert at the site(s) of the code
tguilbert 2016/09/16 01:03:01 Done.
16 "base::NonceToken should be of size 2 * sizeof(uint64_t).");
17
18 NonceToken::NonceToken(uint64_t high, uint64_t low) : high_(high), low_(low) {}
19
20 std::string NonceToken::ToString() const {
21 return base::StringPrintf("(%" PRIu64 ":%" PRIu64 ")", high_, low_);
22 }
23
24 // static
25 NonceToken NonceToken::Create() {
26 NonceToken token;
27 // Use base::RandBytes instead of crypto::RandBytes, because crypto calls the
28 // base version directly, and to prevent the dependency from base/ to crypto/.
29 base::RandBytes(&token, sizeof(token));
30 return token;
31 }
32
33 void NonceToken::Serialize(uint64_t* high_out, uint64_t* low_out) const {
34 // Serializing an uninitialized NonceToken is a security issue.
35 CHECK(!is_empty());
danakj 2016/09/15 23:35:16 Why not DCHECK?
tguilbert 2016/09/16 01:03:01 Empty NonceTokens can be copied and assigned norma
danakj 2016/09/16 01:21:55 You're right it's a security issue but you can't t
tguilbert 2016/09/16 22:16:40 Ah, I understand the point you are making. My con
36 *high_out = high_;
37 *low_out = low_;
38 }
39
40 // static
41 NonceToken NonceToken::Deserialize(uint64_t high, uint64_t low) {
42 // Sending a zeroed out NonceToken across processes means that it was never
43 // initialized via NonceToken::Create(), which is a security issue.
44 CHECK(!NonceToken::IsZeroData(high, low));
danakj 2016/09/15 23:35:16 I don't think CHECK is appropriate here, it means
tguilbert 2016/09/16 01:03:01 The IPC and the Mojo deserialization code use Nonc
danakj 2016/09/16 01:21:55 That sounds good, but then I don't think you need
45 return NonceToken(high, low);
46 }
47
48 } // namespace base
OLDNEW
« base/nonce_token.h ('K') | « base/nonce_token.h ('k') | base/nonce_token_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698