Chromium Code Reviews| 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 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 value_(hmac_.DigestLength(), 0x01), | |
| 82 requested_bytes_(0) { | |
| 83 // HMAC_DRBG Instantiate Process | |
| 84 // 2. Key = 0x00 00...00 | |
| 85 // 3. V = 0x01 01...01 | |
| 86 ByteVector key(hmac_.DigestLength(), 0x00); | |
| 87 std::string initial_value(value_.begin(), value_.end()); | |
| 88 | |
| 89 // 4. (Key, V) = HMAC_DRBG_Update(seed_material, Key, V) | |
| 90 // HMAC_DRBG Update Process inlined | |
| 91 // 1. K = HMAC(K, V || 0x00 || provided_data) | |
| 92 crypto::HMAC keygen_hmac(crypto::HMAC::SHA256); | |
| 93 if (!keygen_hmac.Init(std::string(key.begin(), key.end()))) | |
| 94 NOTREACHED(); | |
| 95 if (!keygen_hmac.Sign(initial_value + secret, key.data(), key.size())) | |
|
ulfar
2014/02/08 01:51:33
Isn't the zero byte from V || 0x00 || provided_dat
Steven Holte
2014/02/08 02:19:45
Done.
| |
| 96 NOTREACHED(); | |
| 97 // 2. V = HMAC(K, V) | |
| 98 if (!hmac_.Init(std::string(key.begin(), key.end()))) | |
| 99 NOTREACHED(); | |
| 100 if (!hmac_.Sign(initial_value, value_.data(), value_.size())) | |
| 101 NOTREACHED(); | |
| 102 } | |
| 103 | |
| 104 HmacByteVectorGenerator::~HmacByteVectorGenerator() {} | |
| 105 | |
| 106 uint8_t HmacByteVectorGenerator::RandByte() { | |
| 107 if (requested_bytes_ % hmac_.DigestLength()) { | |
|
ulfar
2014/02/08 01:51:33
make a variable for this. For example,
size_t req
Steven Holte
2014/02/08 02:19:45
Done.
| |
| 108 // Do step 4.1 of the HMAC_DRBG Generate Process for more bits. | |
| 109 // V = HMAC(Key, V) | |
| 110 if(!hmac_.Sign(std::string(value_.begin(), value_.end()), | |
| 111 value_.data(), value_.size())) | |
| 112 NOTREACHED(); | |
| 113 } | |
| 114 uint8_t result = value_[requested_bytes_ % hmac_.DigestLength()]; | |
| 115 requested_bytes_++; | |
| 116 // Check max_number_of_bits_per_request (2^19) from 10.1 Table 2 | |
| 117 DCHECK_LT(requested_bytes_, 1U << 16); | |
|
ulfar
2014/02/08 01:51:33
Did you mean to conservatively say 2^16 here? If
Steven Holte
2014/02/08 02:19:45
No, it's bits vs. bytes. Expanded comment.
| |
| 118 return result; | |
| 119 } | |
| 120 | |
| 121 } // namespace rappor | |
| OLD | NEW |