OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/rappor/byte_vector_utils.h" | 5 #include "components/rappor/byte_vector_utils.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/rand_util.h" | 10 #include "base/rand_util.h" |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 return true; | 71 return true; |
72 // 4. K = HMAC(K, V || 0x01 || provided_data) | 72 // 4. K = HMAC(K, V || 0x01 || provided_data) |
73 if (!HMAC_Rotate(*hmac2, Concat(*value, 0x01, provided_data), out_hmac)) | 73 if (!HMAC_Rotate(*hmac2, Concat(*value, 0x01, provided_data), out_hmac)) |
74 return false; | 74 return false; |
75 // 5. V = HMAC(K, V) | 75 // 5. V = HMAC(K, V) |
76 return HMAC_Rehash(*out_hmac, value); | 76 return HMAC_Rehash(*out_hmac, value); |
77 } | 77 } |
78 | 78 |
79 } // namespace | 79 } // namespace |
80 | 80 |
| 81 ByteVector* ByteVectorAnd(const ByteVector& lhs, ByteVector* rhs) { |
| 82 DCHECK_EQ(lhs.size(), rhs->size()); |
| 83 for (size_t i = 0; i < lhs.size(); ++i) { |
| 84 (*rhs)[i] = lhs[i] & (*rhs)[i]; |
| 85 } |
| 86 return rhs; |
| 87 } |
| 88 |
81 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) { | 89 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) { |
82 DCHECK_EQ(lhs.size(), rhs->size()); | 90 DCHECK_EQ(lhs.size(), rhs->size()); |
83 for (size_t i = 0, len = lhs.size(); i < len; ++i) { | 91 for (size_t i = 0; i < lhs.size(); ++i) { |
84 (*rhs)[i] = lhs[i] | (*rhs)[i]; | 92 (*rhs)[i] = lhs[i] | (*rhs)[i]; |
85 } | 93 } |
86 return rhs; | 94 return rhs; |
87 } | 95 } |
88 | 96 |
89 ByteVector* ByteVectorMerge(const ByteVector& mask, | 97 ByteVector* ByteVectorMerge(const ByteVector& mask, |
90 const ByteVector& lhs, | 98 const ByteVector& lhs, |
91 ByteVector* rhs) { | 99 ByteVector* rhs) { |
92 DCHECK_EQ(lhs.size(), rhs->size()); | 100 DCHECK_EQ(lhs.size(), rhs->size()); |
93 for (size_t i = 0, len = lhs.size(); i < len; ++i) { | 101 for (size_t i = 0; i < lhs.size(); ++i) { |
94 (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]); | 102 (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]); |
95 } | 103 } |
96 return rhs; | 104 return rhs; |
97 } | 105 } |
98 | 106 |
99 int CountBits(const ByteVector& vector) { | 107 int CountBits(const ByteVector& vector) { |
100 int bit_count = 0; | 108 int bit_count = 0; |
101 for (size_t i = 0; i < vector.size(); ++i) { | 109 for (size_t i = 0; i < vector.size(); ++i) { |
102 uint8_t byte = vector[i]; | 110 uint8_t byte = vector[i]; |
103 for (int j = 0; j < 8 ; ++j) { | 111 for (int j = 0; j < 8 ; ++j) { |
(...skipping 16 matching lines...) Expand all Loading... |
120 } | 128 } |
121 | 129 |
122 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( | 130 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( |
123 Probability probability) { | 131 Probability probability) { |
124 ByteVector bytes = GetRandomByteVector(); | 132 ByteVector bytes = GetRandomByteVector(); |
125 switch (probability) { | 133 switch (probability) { |
126 case PROBABILITY_75: | 134 case PROBABILITY_75: |
127 return *ByteVectorOr(GetRandomByteVector(), &bytes); | 135 return *ByteVectorOr(GetRandomByteVector(), &bytes); |
128 case PROBABILITY_50: | 136 case PROBABILITY_50: |
129 return bytes; | 137 return bytes; |
| 138 case PROBABILITY_25: |
| 139 return *ByteVectorAnd(GetRandomByteVector(), &bytes); |
130 } | 140 } |
131 NOTREACHED(); | 141 NOTREACHED(); |
132 return bytes; | 142 return bytes; |
133 } | 143 } |
134 | 144 |
135 HmacByteVectorGenerator::HmacByteVectorGenerator( | 145 HmacByteVectorGenerator::HmacByteVectorGenerator( |
136 size_t byte_count, | 146 size_t byte_count, |
137 const std::string& entropy_input, | 147 const std::string& entropy_input, |
138 const std::string& personalization_string) | 148 const std::string& personalization_string) |
139 : ByteVectorGenerator(byte_count), | 149 : ByteVectorGenerator(byte_count), |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 bytes_to_go -= n; | 216 bytes_to_go -= n; |
207 generated_bytes_ += n; | 217 generated_bytes_ += n; |
208 // Check max_number_of_bits_per_request from 10.1 Table 2 | 218 // Check max_number_of_bits_per_request from 10.1 Table 2 |
209 // max_number_of_bits_per_request == 2^19 bits == 2^16 bytes | 219 // max_number_of_bits_per_request == 2^19 bits == 2^16 bytes |
210 DCHECK_LT(generated_bytes_, 1U << 16); | 220 DCHECK_LT(generated_bytes_, 1U << 16); |
211 } | 221 } |
212 return bytes; | 222 return bytes; |
213 } | 223 } |
214 | 224 |
215 } // namespace rappor | 225 } // namespace rappor |
OLD | NEW |