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

Side by Side Diff: components/rappor/byte_vector_utils.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 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 2014 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 "components/rappor/byte_vector_utils.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/rand_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "crypto/random.h"
13
14 namespace rappor {
15
16 namespace {
17
18 base::StringPiece ByteVectorToStringPiece(const ByteVector& lhs) {
Alexei Svitkine (slow) 2014/02/12 22:42:36 Nit: I'd name this ByteVectorAsStringPiece(), sinc
Ilya Sherman 2014/02/13 01:39:03 nit: Docs.
Steven Holte 2014/02/13 05:11:12 Done.
Steven Holte 2014/02/13 05:11:12 Done.
19 return base::StringPiece(reinterpret_cast<const char *>(&lhs[0]), lhs.size());
Ilya Sherman 2014/02/13 01:39:03 IMPORTANT: I don't think this is safe, as the byte
Steven Holte 2014/02/13 05:11:12 It should be perfectly valid for the StringPiece t
Ilya Sherman 2014/02/13 23:23:08 Yes, looks like you're right -- I didn't realize t
20 }
21
22 std::string Concat(const ByteVector& value, char c, const std::string& data) {
Ilya Sherman 2014/02/13 01:39:03 nit: Docs.
23 return std::string(value.begin(), value.end()) + c + data;
Ilya Sherman 2014/02/13 01:39:03 IMPORTANT: One of the chars you pass to Concat is
Alexei Svitkine (slow) 2014/02/13 04:22:33 This CL is using the std::string type (and StringP
Steven Holte 2014/02/13 05:11:12 Yes, it does. std::string may contain null charac
Ilya Sherman 2014/02/13 23:23:08 Yeah, you guys are right. My bad.
24 }
25
26 // K = HMAC(K, data)
Ilya Sherman 2014/02/13 01:39:03 nit: Please expand the documentation to be written
Steven Holte 2014/02/13 05:11:12 Done.
27 bool HMAC_Rotate(const crypto::HMAC& hmac,
28 const std::string& data,
29 crypto::HMAC* out_hmac) {
Ilya Sherman 2014/02/13 01:39:03 Optional nit: "out_hmac" -> "result"
Steven Holte 2014/02/13 05:11:12 Done.
30 ByteVector key(hmac.DigestLength());
31 if (!hmac.Sign(data, &key[0], key.size()))
32 return false;
33 return out_hmac->Init(ByteVectorToStringPiece(key));
34 }
35
36 } // namespace
37
38 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) {
39 DCHECK_EQ(lhs.size(), rhs->size());
40 for (size_t i = 0, len = lhs.size(); i < len; ++i) {
41 (*rhs)[i] = lhs[i] | (*rhs)[i];
42 }
43 return rhs;
44 }
45
46 ByteVector* ByteVectorMerge(const ByteVector& mask,
47 const ByteVector& lhs,
48 ByteVector* rhs) {
49 DCHECK_EQ(lhs.size(), rhs->size());
50 for (size_t i = 0, len = lhs.size(); i < len; ++i) {
51 (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]);
52 }
53 return rhs;
54 }
55
56 int CountBits(const ByteVector& vector) {
57 int bit_count = 0;
58 for (size_t i = 0; i < vector.size(); ++i) {
59 uint8_t byte = vector[i];
60 for (int j = 0; j < 8 ; ++j) {
61 if (byte & (1 << j))
62 bit_count++;
63 }
64 }
65 return bit_count;
66 }
67
68 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
69 : byte_count_(byte_count) {}
70
71 ByteVectorGenerator::~ByteVectorGenerator() {}
72
73 ByteVector ByteVectorGenerator::GetRandomByteVector() {
74 ByteVector bytes(byte_count_);
75 crypto::RandBytes(&bytes[0], bytes.size());
76 return bytes;
77 }
78
79 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(
80 Probability probability) {
81 ByteVector bytes = GetRandomByteVector();
82 switch (probability) {
83 case PROBABILITY_75:
84 return *ByteVectorOr(GetRandomByteVector(), &bytes);
85 case PROBABILITY_50:
86 return bytes;
87 }
88 NOTREACHED();
89 return bytes;
90 }
91
92 HmacByteVectorGenerator::HmacByteVectorGenerator(
93 size_t byte_count,
94 const std::string& entropy_input,
95 const std::string& personalization_string)
96 : ByteVectorGenerator(byte_count),
97 hmac_(crypto::HMAC::SHA256),
98 value_(hmac_.DigestLength(), 0x01),
99 generated_bytes_(0) {
100 // HMAC_DRBG Instantiate Process
101 // 1. seed_material = entropy_input + nonce + personalization_string
102 // Note: We are using the 8.6.7 interpretation, where the entropy_input and
103 // nonce are acquired at the same time from the same source.
104 DCHECK_EQ(kEntropyInputSize, entropy_input.size());
105 std::string seed_material(entropy_input + personalization_string);
106 // 2. Key = 0x00 00...00
107 ByteVector key(hmac_.DigestLength(), 0x00);
108 // 3. V = 0x01 01...01
109 // (value_ in initializer list)
110
111 // 4. (Key, V) = HMAC_DRBG_Update(seed_material, Key, V)
112 Update(seed_material, key);
113 }
114
115 HmacByteVectorGenerator::~HmacByteVectorGenerator() {}
116
117 const size_t HmacByteVectorGenerator::kEntropyInputSize = (128 / 8) * 3 / 2;
Ilya Sherman 2014/02/13 01:39:03 Please document what's happening in this computati
Steven Holte 2014/02/13 05:11:12 Expanded, and also updated security_strength to ma
118
119 // static
120 std::string HmacByteVectorGenerator::GenerateEntropyInput() {
121 return base::RandBytesAsString(kEntropyInputSize);
122 }
123
124 void HmacByteVectorGenerator::Update(const std::string& provided_data,
125 const ByteVector& key1) {
126 // HMAC_DRBG Update Process
Ilya Sherman 2014/02/13 01:39:03 It seems wrong for you to need to implement this w
Ilya Sherman 2014/02/13 01:39:03 The logic in this method is not obviously correct
Steven Holte 2014/02/13 05:11:12 This was discussed a bit earlier. This isn't a ge
Steven Holte 2014/02/13 05:11:12 There is a link in the header file, along with som
Ilya Sherman 2014/02/13 23:23:08 Please re-link to it in the implementation file, a
Steven Holte 2014/02/14 02:53:28 Added in several comments.
127 crypto::HMAC hmac1(crypto::HMAC::SHA256);
128 crypto::HMAC hmac2(crypto::HMAC::SHA256);
129 if (!hmac1.Init(ByteVectorToStringPiece(key1)))
130 NOTREACHED();
131 // 1. K = HMAC(K, V || 0x00 || provided_data)
132 if (!HMAC_Rotate(hmac1, Concat(value_, 0x00, provided_data), &hmac2))
133 NOTREACHED();
134 // 2. V = HMAC(K, V)
135 if (!hmac2.Sign(ByteVectorToStringPiece(value_), &value_[0], value_.size()))
136 NOTREACHED();
137 // 3. If (provided_data = Null), then return K and V.
Ilya Sherman 2014/02/13 01:39:03 This comment doesn't seem to match the code.
Steven Holte 2014/02/13 05:11:12 Added a comment to the fact that we are ignoring t
138 DCHECK_GE(provided_data.size(), kEntropyInputSize);
139 // 4. K = HMAC(K, V || 0x01 || provided_data)
140 if (!HMAC_Rotate(hmac2, Concat(value_, 0x01, provided_data), &hmac_))
141 NOTREACHED();
142 // 5. V = HMAC(K, V)
143 if (!hmac_.Sign(ByteVectorToStringPiece(value_), &value_[0], value_.size()))
144 NOTREACHED();
145 }
146
147 ByteVector HmacByteVectorGenerator::GetRandomByteVector() {
148 const size_t digest_length = hmac_.DigestLength();
Ilya Sherman 2014/02/13 01:39:03 Is this expected to be equal to the size of |value
Steven Holte 2014/02/13 05:11:12 Added DCHECK
149 ByteVector bytes(byte_count_);
150 uint8_t* data = &bytes[0];
151 size_t bytes_to_go = byte_count_;
152 while (bytes_to_go > 0) {
153 size_t requested_byte_in_digest = generated_bytes_ % digest_length;
154 if (requested_byte_in_digest == 0) {
155 // Do step 4.1 of the HMAC_DRBG Generate Process for more bits.
156 // V = HMAC(Key, V)
157 if (!hmac_.Sign(ByteVectorToStringPiece(value_),
158 &value_[0], value_.size())) {
159 NOTREACHED();
160 }
161 }
162 size_t n = std::min(bytes_to_go,
163 digest_length - requested_byte_in_digest);
164 memcpy(data, &value_[requested_byte_in_digest], n);
165 data += n;
166 bytes_to_go -= n;
167 generated_bytes_ += n;
168 // Check max_number_of_bits_per_request from 10.1 Table 2
Ilya Sherman 2014/02/13 01:39:03 nit: Please provide a link to the reference that c
Steven Holte 2014/02/13 05:11:12 See class declaration in header file.
Ilya Sherman 2014/02/13 23:23:08 Please include the link at each relevant location.
Steven Holte 2014/02/14 02:53:28 Done.
169 // max_number_of_bits_per_request == 2^19 bits == 2^16 bytes
170 DCHECK_LT(generated_bytes_, 1U << 16);
171 }
172 return bytes;
173 }
174
175 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698