| Index: components/rappor/rappor_reporter.cc | 
| diff --git a/components/rappor/rappor_reporter.cc b/components/rappor/rappor_reporter.cc | 
| new file mode 100644 | 
| index 0000000000000000000000000000000000000000..7dc3f905b49d1c5280432d90651ee96b5ba33fe9 | 
| --- /dev/null | 
| +++ b/components/rappor/rappor_reporter.cc | 
| @@ -0,0 +1,149 @@ | 
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. | 
| +// Use of this source code is governed by a BSD-style license that can be | 
| +// found in the LICENSE file. | 
| + | 
| +#include <assert.h> | 
| +#include <limits.h> | 
| +#include <math.h> | 
| + | 
| +#include <bitset> | 
| +#include <map> | 
| +#include <string> | 
| +#include <vector> | 
| + | 
| +#include "base/logging.h" | 
| +#include "base/strings/string_number_conversions.h" | 
| +#include "components/rappor/bytevector.h" | 
| +#include "components/rappor/rappor.h" | 
| +#include "components/rappor/rappor_reporter.h" | 
| +#include "crypto/hmac.h" | 
| +#include "crypto/random.h" | 
| + | 
| +#define FAKE_PROB_INDEX 4 | 
| +#define FAKE_ONE_PROB_INDEX 2 | 
| + | 
| +#define ONE_HONESTY_PROB_INDEX 4 | 
| +#define ZERO_HONESTY_PROB_INDEX 2 | 
| + | 
| +#define HMAC_SEED 10 | 
| + | 
| +namespace rappor { | 
| + | 
| +namespace { | 
| + | 
| +// A utility object for generating random binary data with different | 
| +// likelihood of bits being true. | 
| +class ByteVectorGenerator { | 
| + public: | 
| +  explicit ByteVectorGenerator(size_t byte_count); | 
| +  ByteVector GetRandomByteVector(); | 
| +  ByteVector GetWeightedRandomByteVector(int probIndex); | 
| + | 
| + protected: | 
| +  virtual uint8_t RandByte(); | 
| + | 
| + private: | 
| +  size_t byte_count_; | 
| +}; | 
| + | 
| +ByteVectorGenerator::ByteVectorGenerator(size_t byte_count) | 
| +    : byte_count_(byte_count) {} | 
| + | 
| +uint8_t ByteVectorGenerator::RandByte() { | 
| +  uint8_t randomBits; | 
| +  crypto::RandBytes(&randomBits, sizeof(uint8_t)); | 
| +  return randomBits; | 
| +} | 
| + | 
| +ByteVector ByteVectorGenerator::GetRandomByteVector() { | 
| +  ByteVector bytes(byte_count_); | 
| +  for (size_t i = 0; i < byte_count_; i++) { | 
| +    bytes[i] = RandByte(); | 
| +  } | 
| +  return bytes; | 
| +} | 
| + | 
| +ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(int probIndex) { | 
| +  switch (probIndex) { | 
| +    // 87.5% ones | 
| +    case -8: | 
| +      return ~(GetRandomByteVector() & GetRandomByteVector() & | 
| +               GetRandomByteVector()); | 
| +    // 75% ones | 
| +    case -4: | 
| +      return ~(GetRandomByteVector() & GetRandomByteVector()); | 
| +    // 50% ones | 
| +    case 2: | 
| +      return GetRandomByteVector(); | 
| +    // 25% ones | 
| +    case 4: | 
| +      return GetRandomByteVector() & GetRandomByteVector(); | 
| +    // 12.5% ones | 
| +    case 8: | 
| +      return GetRandomByteVector() & GetRandomByteVector() & | 
| +             GetRandomByteVector(); | 
| +    default: | 
| +      // Invalid probability index "probIndex" for coin flips | 
| +      abort(); | 
| +  } | 
| +} | 
| + | 
| +// A ByteVectorGenerator that uses a psuedo-random function to generate | 
| +// deterministicly random bits. | 
| +class HmacByteVectorGenerator : public ByteVectorGenerator { | 
| + public: | 
| +  HmacByteVectorGenerator(size_t byte_count, const std::string& secret); | 
| + | 
| + protected: | 
| +  virtual uint8_t RandByte(); | 
| + | 
| + private: | 
| +  crypto::HMAC hmac_; | 
| +  uint64_t hmac_state_; | 
| +}; | 
| + | 
| +HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, | 
| +                                                 const std::string& secret) | 
| +    : ByteVectorGenerator(byte_count), hmac_(crypto::HMAC::SHA256) { | 
| +  if (!hmac_.Init(secret)) { | 
| +    abort(); | 
| +  }; | 
| +  assert(hmac_.DigestLength() > sizeof(uint64_t)); | 
| +  hmac_state_ = uint64_t(HMAC_SEED); | 
| +} | 
| + | 
| +uint8_t HmacByteVectorGenerator::RandByte() { | 
| +  uint8_t randomBits; | 
| +  std::string state = base::Uint64ToString(hmac_state_); | 
| +  if (!hmac_.Sign(state, &randomBits, sizeof(uint8_t))) { | 
| +    abort(); | 
| +  } | 
| +  ++hmac_state_; | 
| +  return randomBits; | 
| +} | 
| + | 
| +}  // namespace | 
| + | 
| +RapporReporter::RapporReporter(const std::string& secret) : secret_(secret) {} | 
| + | 
| +std::vector<uint8_t> RapporReporter::GetReport(const Rappor& rappor) { | 
| +  const ByteVector realbits(rappor.GetBytes()); | 
| +  size_t size = realbits.size(); | 
| +  HmacByteVectorGenerator hmacGenerator(size, secret_ + rappor.rappor_name()); | 
| +  const ByteVector fakebits = | 
| +      hmacGenerator.GetWeightedRandomByteVector(rappor.fake_prob_index()); | 
| +  const ByteVector fakeones = | 
| +      hmacGenerator.GetWeightedRandomByteVector(rappor.fake_one_prob_index()); | 
| +  const ByteVector onebits = (realbits & ~fakebits) | fakeones; | 
| + | 
| +  ByteVectorGenerator coinGenerator(size); | 
| +  const ByteVector zero_coins = coinGenerator.GetWeightedRandomByteVector( | 
| +      rappor.zero_honesty_prob_index()); | 
| +  const ByteVector one_coins = coinGenerator.GetWeightedRandomByteVector( | 
| +      rappor.one_honesty_prob_index()); | 
| +  return (zero_coins & (~onebits)) | (one_coins & onebits); | 
| + | 
| +  return std::vector<uint8_t>(); | 
| +} | 
| + | 
| +}  // namespace rappor | 
|  |