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

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
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 ByteVector GetRandomByteVector();
34 ByteVector GetWeightedRandomByteVector(int probIndex);
35
36 protected:
37 virtual uint8_t RandByte();
38
39 private:
40 size_t byte_count_;
41 };
42
43 ByteVectorGenerator::ByteVectorGenerator(size_t byte_count)
44 : byte_count_(byte_count) {}
45
46 uint8_t ByteVectorGenerator::RandByte() {
47 uint8_t randomBits;
48 crypto::RandBytes(&randomBits, sizeof(uint8_t));
49 return randomBits;
50 }
51
52 ByteVector ByteVectorGenerator::GetRandomByteVector() {
jwd 2013/12/17 23:00:25 Mention that it's expected 50% 1s or something lik
Steven Holte 2013/12/18 02:06:12 Added comments about RandByte and RandBytes being
53 ByteVector bytes(byte_count_);
54 for (size_t i = 0; i < byte_count_; i++) {
55 bytes[i] = RandByte();
56 }
57 return bytes;
58 }
59
60 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector(int probIndex) {
61 switch (probIndex) {
62 // 87.5% ones
63 case -8:
jwd 2013/12/17 23:00:25 What determines these constants? As in, why -8,...
Steven Holte 2013/12/18 02:06:12 Changed these to Probability enum values.
64 return GetRandomByteVector().Or(GetRandomByteVector()).Or(
65 GetRandomByteVector());
66 // 75% ones
67 case -4:
68 return GetRandomByteVector().Or(GetRandomByteVector());
69 // 50% ones
70 case 2:
71 return GetRandomByteVector();
72 // 25% ones
73 case 4:
74 return GetRandomByteVector().And(GetRandomByteVector());
75 // 12.5% ones
76 case 8:
77 return GetRandomByteVector().And(GetRandomByteVector()).And(
78 GetRandomByteVector());
79 default:
80 // Invalid probability index "probIndex" 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;
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());
125 size_t size = realbits.size();
126 HmacByteVectorGenerator hmacGenerator(size, secret_ + rappor.rappor_name());
127 const ByteVector fakebits =
128 hmacGenerator.GetWeightedRandomByteVector(rappor.fake_prob_index());
129 const ByteVector fakeones =
130 hmacGenerator.GetWeightedRandomByteVector(rappor.fake_one_prob_index());
131 const ByteVector onebits = (realbits.And(fakebits.Not())).Or(
132 fakeones.And(fakebits));
133
134 ByteVectorGenerator coinGenerator(size);
135 const ByteVector zero_coins = coinGenerator.GetWeightedRandomByteVector(
136 rappor.zero_honesty_prob_index());
137 const ByteVector one_coins = coinGenerator.GetWeightedRandomByteVector(
138 rappor.one_honesty_prob_index());
139 return (zero_coins.And(onebits.Not())).Or(one_coins.And(onebits));
140
141 return std::vector<uint8_t>();
142 }
143
144 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698