Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(148)

Side by Side Diff: components/rappor/rappor_reporter.cc

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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 HMAC_SEED 10
Alexei Svitkine (slow) 2013/12/19 19:47:02 Use a const int in the anon namespace instead and
Steven Holte 2013/12/20 03:03:55 Done.
23
24 namespace rappor {
25
26 namespace {
27
28 // A utility object for generating random binary data with different
29 // likelihood of bits being true.
30 class ByteVectorGenerator {
31 public:
32 explicit ByteVectorGenerator(size_t byte_count);
33 // Generates a random byte vector where each bit is independently set with
34 // the given probability.
35 ByteVector GetWeightedRandomByteVector(Probability probability);
36
37 protected:
38 // Generates a random byte from a uniform distribution.
39 virtual uint8_t RandByte();
40
41 private:
42 // Generates a random vector of bytes from a uniform distribution.
43 ByteVector GetRandomByteVector();
44 size_t byte_count_;
45 };
46
47 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
48 : byte_count_(byte_count) {}
49
50 uint8_t ByteVectorGenerator::RandByte() {
51 uint8_t randomBits;
52 crypto::RandBytes(&randomBits, sizeof(uint8_t));
53 return randomBits;
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 switch (probability) {
67 case PROBABILITY_87_5:
68 return GetRandomByteVector().Or(GetRandomByteVector()).Or(
69 GetRandomByteVector());
70 case PROBABILITY_75:
71 return GetRandomByteVector().Or(GetRandomByteVector());
72 case PROBABILITY_50:
73 return GetRandomByteVector();
74 case PROBABILITY_25:
75 return GetRandomByteVector().And(GetRandomByteVector());
76 case PROBABILITY_12_5:
77 return GetRandomByteVector().And(GetRandomByteVector()).And(
78 GetRandomByteVector());
79 default:
80 // Invalid probability for coin flips
81 abort();
82 }
83 }
84
85 // A ByteVectorGenerator that uses a psuedo-random function to generate
86 // deterministicly random bits.
87 class HmacByteVectorGenerator : public ByteVectorGenerator {
88 public:
89 HmacByteVectorGenerator(size_t byte_count, const std::string& secret);
90
91 protected:
92 virtual uint8_t RandByte();
93
94 private:
95 crypto::HMAC hmac_;
96 uint64_t hmac_state_;
97 };
98
99 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count,
100 const std::string& secret)
101 : ByteVectorGenerator(byte_count), hmac_(crypto::HMAC::SHA256) {
102 if (!hmac_.Init(secret)) {
103 abort();
104 };
105 assert(hmac_.DigestLength() > sizeof(uint64_t));
106 hmac_state_ = uint64_t(HMAC_SEED);
107 }
108
109 uint8_t HmacByteVectorGenerator::RandByte() {
110 uint8_t randomBits;
Alexei Svitkine (slow) 2013/12/19 19:47:02 Nit: random_bits
Steven Holte 2013/12/20 03:03:55 Done.
111 std::string state = base::Uint64ToString(hmac_state_);
112 if (!hmac_.Sign(state, &randomBits, sizeof(uint8_t))) {
113 abort();
114 }
115 ++hmac_state_;
116 return randomBits;
117 }
118
119 } // namespace
120
121 RapporReporter::RapporReporter(const std::string& secret) : secret_(secret) {}
122
123 std::vector<uint8_t> RapporReporter::GetReport(const Rappor& rappor) {
124 const ByteVector realbits(rappor.GetBytes());
Alexei Svitkine (slow) 2013/12/19 19:47:02 Nit: real_bits, use this naming convention through
Steven Holte 2013/12/20 03:03:55 Done.
125 size_t size = realbits.size();
126 HmacByteVectorGenerator hmacGenerator(
Alexei Svitkine (slow) 2013/12/19 19:47:02 Nit: hmac_generator
Steven Holte 2013/12/20 03:03:55 Done.
127 size, secret_ + rappor.parameters()->rappor_name);
128 const ByteVector fakebits =
129 hmacGenerator.GetWeightedRandomByteVector(rappor.parameters()->fake_prob);
130 const ByteVector fakeones = hmacGenerator.GetWeightedRandomByteVector(
131 rappor.parameters()->fake_one_prob);
132 const ByteVector onebits =
133 (realbits.And(fakebits.Not())).Or(fakeones.And(fakebits));
Alexei Svitkine (slow) 2013/12/19 19:47:02 Nit: No need for outer ()'s.
Steven Holte 2013/12/20 03:03:55 Done.
134
135 ByteVectorGenerator coinGenerator(size);
136 const ByteVector zero_coins = coinGenerator.GetWeightedRandomByteVector(
137 rappor.parameters()->zero_coin_prob);
138 const ByteVector one_coins = coinGenerator.GetWeightedRandomByteVector(
139 rappor.parameters()->one_coin_prob);
140 return (zero_coins.And(onebits.Not())).Or(one_coins.And(onebits));
141
142 return std::vector<uint8_t>();
143 }
144
145 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698