OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 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 <assert.h> |
| 6 #include <limits.h> |
| 7 #include <math.h> |
| 8 |
| 9 #include <bitset> |
| 10 #include <map> |
| 11 #include <string> |
| 12 #include <vector> |
| 13 |
| 14 #include "base/logging.h" |
| 15 #include "base/strings/string_number_conversions.h" |
| 16 #include "components/rappor/bytevector.h" |
| 17 #include "components/rappor/rappor.h" |
| 18 #include "components/rappor/rappor_reporter.h" |
| 19 #include "crypto/hmac.h" |
| 20 #include "crypto/random.h" |
| 21 |
| 22 #define FAKE_PROB_INDEX 4 |
| 23 #define FAKE_ONE_PROB_INDEX 2 |
| 24 |
| 25 #define ONE_HONESTY_PROB_INDEX 4 |
| 26 #define ZERO_HONESTY_PROB_INDEX 2 |
| 27 |
| 28 #define HMAC_SEED 10 |
| 29 |
| 30 namespace rappor { |
| 31 |
| 32 namespace { |
| 33 |
| 34 // A utility object for generating random binary data with different |
| 35 // likelihood of bits being true. |
| 36 class ByteVectorGenerator { |
| 37 public: |
| 38 explicit ByteVectorGenerator(size_t byte_count); |
| 39 ByteVector GetRandomByteVector(); |
| 40 ByteVector GetWeightedRandomByteVector(int probIndex); |
| 41 |
| 42 protected: |
| 43 virtual uint8_t RandByte(); |
| 44 |
| 45 private: |
| 46 size_t byte_count_; |
| 47 }; |
| 48 |
| 49 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count) |
| 50 : byte_count_(byte_count) {} |
| 51 |
| 52 uint8_t ByteVectorGenerator::RandByte() { |
| 53 uint8_t randomBits; |
| 54 crypto::RandBytes(&randomBits, sizeof(uint8_t)); |
| 55 return randomBits; |
| 56 } |
| 57 |
| 58 ByteVector ByteVectorGenerator::GetRandomByteVector() { |
| 59 ByteVector bytes(byte_count_); |
| 60 for (size_t i = 0; i < byte_count_; i++) { |
| 61 bytes[i] = RandByte(); |
| 62 } |
| 63 return bytes; |
| 64 } |
| 65 |
| 66 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(int probIndex) { |
| 67 switch (probIndex) { |
| 68 // 87.5% ones |
| 69 case -8: |
| 70 return ~(GetRandomByteVector() & GetRandomByteVector() & |
| 71 GetRandomByteVector()); |
| 72 // 75% ones |
| 73 case -4: |
| 74 return ~(GetRandomByteVector() & GetRandomByteVector()); |
| 75 // 50% ones |
| 76 case 2: |
| 77 return GetRandomByteVector(); |
| 78 // 25% ones |
| 79 case 4: |
| 80 return GetRandomByteVector() & GetRandomByteVector(); |
| 81 // 12.5% ones |
| 82 case 8: |
| 83 return GetRandomByteVector() & GetRandomByteVector() & |
| 84 GetRandomByteVector(); |
| 85 default: |
| 86 // Invalid probability index "probIndex" for coin flips |
| 87 abort(); |
| 88 } |
| 89 } |
| 90 |
| 91 // A ByteVectorGenerator that uses a psuedo-random function to generate |
| 92 // deterministicly random bits. |
| 93 class HmacByteVectorGenerator : public ByteVectorGenerator { |
| 94 public: |
| 95 HmacByteVectorGenerator(size_t byte_count, const std::string& secret); |
| 96 |
| 97 protected: |
| 98 virtual uint8_t RandByte(); |
| 99 |
| 100 private: |
| 101 crypto::HMAC hmac_; |
| 102 uint64_t hmac_state_; |
| 103 }; |
| 104 |
| 105 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, |
| 106 const std::string& secret) |
| 107 : ByteVectorGenerator(byte_count), hmac_(crypto::HMAC::SHA256) { |
| 108 if (!hmac_.Init(secret)) { |
| 109 abort(); |
| 110 }; |
| 111 assert(hmac_.DigestLength() > sizeof(uint64_t)); |
| 112 hmac_state_ = uint64_t(HMAC_SEED); |
| 113 } |
| 114 |
| 115 uint8_t HmacByteVectorGenerator::RandByte() { |
| 116 uint8_t randomBits; |
| 117 std::string state = base::Uint64ToString(hmac_state_); |
| 118 if (!hmac_.Sign(state, &randomBits, sizeof(uint8_t))) { |
| 119 abort(); |
| 120 } |
| 121 ++hmac_state_; |
| 122 return randomBits; |
| 123 } |
| 124 |
| 125 } // namespace |
| 126 |
| 127 RapporReporter::RapporReporter(const std::string& secret) : secret_(secret) {} |
| 128 |
| 129 std::vector<uint8_t> RapporReporter::GetReport(const Rappor& rappor) { |
| 130 const ByteVector realbits(rappor.GetBytes()); |
| 131 size_t size = realbits.size(); |
| 132 HmacByteVectorGenerator hmacGenerator(size, secret_ + rappor.rappor_name()); |
| 133 const ByteVector fakebits = |
| 134 hmacGenerator.GetWeightedRandomByteVector(rappor.fake_prob_index()); |
| 135 const ByteVector fakeones = |
| 136 hmacGenerator.GetWeightedRandomByteVector(rappor.fake_one_prob_index()); |
| 137 const ByteVector onebits = (realbits & ~fakebits) | fakeones; |
| 138 |
| 139 ByteVectorGenerator coinGenerator(size); |
| 140 const ByteVector zero_coins = coinGenerator.GetWeightedRandomByteVector( |
| 141 rappor.zero_honesty_prob_index()); |
| 142 const ByteVector one_coins = coinGenerator.GetWeightedRandomByteVector( |
| 143 rappor.one_honesty_prob_index()); |
| 144 return (zero_coins & (~onebits)) | (one_coins & onebits); |
| 145 |
| 146 return std::vector<uint8_t>(); |
| 147 } |
| 148 |
| 149 } // namespace rappor |
OLD | NEW |