OLD | NEW |
(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/strings/string_number_conversions.h" |
| 11 #include "crypto/random.h" |
| 12 |
| 13 namespace rappor { |
| 14 |
| 15 ByteVector* ByteVectorOr(const ByteVector& lhs, ByteVector* rhs) { |
| 16 DCHECK_EQ(lhs.size(), rhs->size()); |
| 17 for (size_t i = 0, len = lhs.size(); i < len; ++i) { |
| 18 (*rhs)[i] = lhs[i] | (*rhs)[i]; |
| 19 } |
| 20 return rhs; |
| 21 } |
| 22 |
| 23 ByteVector* ByteVectorMerge(const ByteVector& mask, |
| 24 const ByteVector& lhs, |
| 25 ByteVector* rhs) { |
| 26 DCHECK_EQ(lhs.size(), rhs->size()); |
| 27 for (size_t i = 0, len = lhs.size(); i < len; ++i) { |
| 28 (*rhs)[i] = (lhs[i] & ~mask[i]) | ((*rhs)[i] & mask[i]); |
| 29 } |
| 30 return rhs; |
| 31 } |
| 32 |
| 33 int CountBits(const ByteVector& v) { |
| 34 int bit_count = 0; |
| 35 for(size_t i = 0; i < v.size(); ++i) { |
| 36 uint8_t byte = v[i]; |
| 37 for (int j = 0; j < 8 ; ++j) { |
| 38 if (byte & (1 << j)) |
| 39 bit_count++; |
| 40 } |
| 41 } |
| 42 return bit_count; |
| 43 } |
| 44 |
| 45 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count) |
| 46 : byte_count_(byte_count) {} |
| 47 |
| 48 ByteVectorGenerator::~ByteVectorGenerator() {} |
| 49 |
| 50 uint8_t ByteVectorGenerator::RandByte() { |
| 51 uint8_t random_bits; |
| 52 crypto::RandBytes(&random_bits, sizeof(uint8_t)); |
| 53 return random_bits; |
| 54 } |
| 55 |
| 56 ByteVector ByteVectorGenerator::GetRandomByteVector() { |
| 57 ByteVector bytes(byte_count_); |
| 58 for (size_t i = 0; i < byte_count_; ++i) { |
| 59 bytes[i] = RandByte(); |
| 60 } |
| 61 return bytes; |
| 62 } |
| 63 |
| 64 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( |
| 65 Probability probability) { |
| 66 ByteVector bytes = GetRandomByteVector(); |
| 67 switch (probability) { |
| 68 case PROBABILITY_75: |
| 69 return *ByteVectorOr(GetRandomByteVector(), &bytes); |
| 70 case PROBABILITY_50: |
| 71 return bytes; |
| 72 } |
| 73 NOTREACHED(); |
| 74 return bytes; |
| 75 } |
| 76 |
| 77 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, |
| 78 const std::string& secret) |
| 79 : ByteVectorGenerator(byte_count), |
| 80 hmac_(crypto::HMAC::SHA256) { |
| 81 // "Nothing up my sleeve" number for the initial value of an |
| 82 // HmacByteVectorGenerator's state. The initial digits of pi. |
| 83 const uint64_t kHmacInitialState = 3141592653589793; |
| 84 |
| 85 if (!hmac_.Init(secret)) |
| 86 NOTREACHED(); |
| 87 |
| 88 DCHECK_GT(hmac_.DigestLength(), sizeof(uint8_t)); |
| 89 hmac_state_ = kHmacInitialState; |
| 90 } |
| 91 |
| 92 HmacByteVectorGenerator::~HmacByteVectorGenerator() {} |
| 93 |
| 94 uint8_t HmacByteVectorGenerator::RandByte() { |
| 95 uint8_t random_bits; |
| 96 std::string state = base::Uint64ToString(hmac_state_); |
| 97 if (!hmac_.Sign(state, &random_bits, sizeof(uint8_t))) |
| 98 NOTREACHED(); |
| 99 |
| 100 ++hmac_state_; |
| 101 return random_bits; |
| 102 } |
| 103 |
| 104 } // namespace rappor |
OLD | NEW |