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& vector) { |
| 34 int bit_count = 0; |
| 35 for(size_t i = 0; i < vector.size(); ++i) { |
| 36 uint8_t byte = vector[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 ByteVector ByteVectorGenerator::GetRandomByteVector() { |
| 51 ByteVector bytes(byte_count_); |
| 52 crypto::RandBytes(bytes.data(), bytes.size()); |
| 53 return bytes; |
| 54 } |
| 55 |
| 56 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( |
| 57 Probability probability) { |
| 58 ByteVector bytes = GetRandomByteVector(); |
| 59 switch (probability) { |
| 60 case PROBABILITY_75: |
| 61 return *ByteVectorOr(GetRandomByteVector(), &bytes); |
| 62 case PROBABILITY_50: |
| 63 return bytes; |
| 64 } |
| 65 NOTREACHED(); |
| 66 return bytes; |
| 67 } |
| 68 |
| 69 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, |
| 70 const std::string& secret) |
| 71 : ByteVectorGenerator(byte_count), |
| 72 hmac_(crypto::HMAC::SHA256), |
| 73 value_(hmac_.DigestLength(), 0x01), |
| 74 requested_bytes_(0) { |
| 75 // HMAC_DRBG Instantiate Process |
| 76 // 2. Key = 0x00 00...00 |
| 77 // 3. V = 0x01 01...01 |
| 78 ByteVector key(hmac_.DigestLength(), 0x00); |
| 79 std::string initial_value(value_.begin(), value_.end()); |
| 80 |
| 81 // 4. (Key, V) = HMAC_DRBG_Update(seed_material, Key, V) |
| 82 // HMAC_DRBG Update Process inlined |
| 83 // 1. K = HMAC(K, V || 0x00 || provided_data) |
| 84 crypto::HMAC keygen_hmac(crypto::HMAC::SHA256); |
| 85 if (!keygen_hmac.Init(std::string(key.begin(), key.end()))) |
| 86 NOTREACHED(); |
| 87 if (!keygen_hmac.Sign(initial_value + char(0x00) + secret, |
| 88 key.data(), key.size())) |
| 89 NOTREACHED(); |
| 90 // 2. V = HMAC(K, V) |
| 91 if (!hmac_.Init(std::string(key.begin(), key.end()))) |
| 92 NOTREACHED(); |
| 93 if (!hmac_.Sign(initial_value, value_.data(), value_.size())) |
| 94 NOTREACHED(); |
| 95 } |
| 96 |
| 97 HmacByteVectorGenerator::~HmacByteVectorGenerator() {} |
| 98 |
| 99 uint8_t HmacByteVectorGenerator::RandByte() { |
| 100 size_t requested_byte_in_digest = requested_bytes_ % hmac_.DigestLength(); |
| 101 if (requested_byte_in_digest == 0) { |
| 102 // Do step 4.1 of the HMAC_DRBG Generate Process for more bits. |
| 103 // V = HMAC(Key, V) |
| 104 if(!hmac_.Sign(std::string(value_.begin(), value_.end()), |
| 105 value_.data(), value_.size())) |
| 106 NOTREACHED(); |
| 107 } |
| 108 uint8_t result = value_[requested_byte_in_digest]; |
| 109 requested_bytes_++; |
| 110 // Check max_number_of_bits_per_request from 10.1 Table 2 |
| 111 // max_number_of_bits_per_request == 2^19 bits == 2^16 bytes |
| 112 DCHECK_LT(requested_bytes_, 1U << 16); |
| 113 return result; |
| 114 } |
| 115 |
| 116 ByteVector HmacByteVectorGenerator::GetRandomByteVector() { |
| 117 ByteVector bytes(byte_count_); |
| 118 for (size_t i = 0; i < byte_count_; ++i) { |
| 119 bytes[i] = RandByte(); |
| 120 } |
| 121 return bytes; |
| 122 } |
| 123 |
| 124 } // namespace rappor |
OLD | NEW |