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

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 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) {
17 DCHECK_EQ(lhs.size(), rhs->size());
18 for (size_t i = 0, len = lhs.size(); i < len; ++i) {
19 (*rhs)[i] = lhs[i] | (*rhs)[i];
20 }
21 return rhs;
22 }
23
24 ByteVector* ByteVectorMerge(const ByteVector& mask,
25 const ByteVector& lhs,
26 ByteVector* rhs) {
27 DCHECK_EQ(lhs.size(), rhs->size());
28 for (size_t i = 0, len = lhs.size(); i < len; ++i) {
29 (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]);
30 }
31 return rhs;
32 }
33
34 int CountBits(const ByteVector& vector) {
35 int bit_count = 0;
36 for(size_t i = 0; i < vector.size(); ++i) {
37 uint8_t byte = vector[i];
38 for (int j = 0; j < 8 ; ++j) {
39 if (byte & (1 << j))
40 bit_count++;
41 }
42 }
43 return bit_count;
44 }
45
46 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
47 : byte_count_(byte_count) {}
48
49 ByteVectorGenerator::~ByteVectorGenerator() {}
50
51 ByteVector ByteVectorGenerator::GetRandomByteVector() {
52 ByteVector bytes(byte_count_);
53 crypto::RandBytes(&bytes[0], bytes.size());
54 return bytes;
55 }
56
57 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(
58 Probability probability) {
59 ByteVector bytes = GetRandomByteVector();
60 switch (probability) {
61 case PROBABILITY_75:
62 return *ByteVectorOr(GetRandomByteVector(), &bytes);
63 case PROBABILITY_50:
64 return bytes;
65 }
66 NOTREACHED();
67 return bytes;
68 }
69
70 HmacByteVectorGenerator::HmacByteVectorGenerator(
71 size_t byte_count,
72 const std::string& entropy_input,
73 const std::string& personalization_string)
74 : ByteVectorGenerator(byte_count),
75 hmac_(crypto::HMAC::SHA256),
76 value_(hmac_.DigestLength(), 0x01),
77 requested_bytes_(0) {
78 // HMAC_DRBG Instantiate Process
79 // 1. seed_material = entropy_input + nonce + personalization_string
80 // Note: We are using the 8.6.7 interpretation, where the entropy_input and
81 // nonce are acquired at the same time from the same source.
82 std::string seed_material(entropy_input + personalization_string);
edknapp 2014/02/12 00:27:17 DCHECK(entropy_input.length() == kEntropyInputSize
Steven Holte 2014/02/12 04:02:55 Done.
83 // 2. Key = 0x00 00...00
84 ByteVector key(hmac_.DigestLength(), 0x00);
85 // 3. V = 0x01 01...01
86 // (value_ in initializer list)
87
88 // 4. (Key, V) = HMAC_DRBG_Update(seed_material, Key, V)
89 Update(seed_material, key);
90 }
91
92 HmacByteVectorGenerator::~HmacByteVectorGenerator() {}
93
94 // static
95 std::string HmacByteVectorGenerator::GenerateEntropyInput() {
96 return base::RandBytesAsString(kEntropyInputSize);
97 }
98
99 void HmacByteVectorGenerator::Update(const std::string& provided_data,
100 const ByteVector& key) {
101 std::string initial_value(value_.begin(), value_.end());
102 // HMAC_DRBG Update Process
103 // 1. K = HMAC(K, V || 0x00 || provided_data)
104 ByteVector generated_key(hmac_.DigestLength());
105 crypto::HMAC keygen_hmac(crypto::HMAC::SHA256);
106 if (!keygen_hmac.Init(std::string(key.begin(), key.end())))
107 NOTREACHED();
108 if (!keygen_hmac.Sign(initial_value + char(0x00) + provided_data,
109 &generated_key[0], generated_key.size())) {
110 NOTREACHED();
111 }
112 // 2. V = HMAC(K, V)
113 if (!hmac_.Init(std::string(generated_key.begin(), generated_key.end())))
114 NOTREACHED();
115 if (!hmac_.Sign(initial_value, &value_[0], value_.size()))
116 NOTREACHED();
117 }
edknapp 2014/02/12 00:27:17 Steps 3-6: if (provided_data.length() > 0) { K =
Steven Holte 2014/02/12 04:02:55 Done.
118
119 ByteVector HmacByteVectorGenerator::GetRandomByteVector() {
120 ByteVector bytes(byte_count_);
121 uint8_t* data = bytes.data();
122 size_t digest_length = hmac_.DigestLength();
123 size_t bytes_to_go = byte_count_;
124 while (bytes_to_go > 0) {
125 size_t requested_byte_in_digest = requested_bytes_ % digest_length;
126 if (requested_byte_in_digest == 0) {
127 // Do step 4.1 of the HMAC_DRBG Generate Process for more bits.
128 // V = HMAC(Key, V)
129 if (!hmac_.Sign(std::string(value_.begin(), value_.end()),
130 &value_[0], value_.size())) {
131 NOTREACHED();
132 }
133 }
134 size_t n = std::min(bytes_to_go,
135 digest_length - requested_byte_in_digest);
136 memcpy(data, &value_[requested_byte_in_digest], n);
137 data += n;
138 bytes_to_go -= n;
139 requested_bytes_ += n;
140 // Check max_number_of_bits_per_request from 10.1 Table 2
141 // max_number_of_bits_per_request == 2^19 bits == 2^16 bytes
142 DCHECK_LT(requested_bytes_, 1U << 16);
143 }
144 return bytes;
145 }
146
147 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698