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

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

Issue 49753002: RAPPOR implementation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 10 months 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 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) {
wtc 2014/02/10 20:12:54 This operation is called "population count" or "po
Steven Holte 2014/02/10 22:50:54 Since I'm only using this function for testing, I'
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() {
wtc 2014/02/10 20:12:54 Requesting only one byte at a time from the PRNG c
Steven Holte 2014/02/10 22:50:54 Done.
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();
wtc 2014/02/10 20:12:54 This error probably needs to be detected in releas
Steven Holte 2014/02/10 22:50:54 This should be prevented by compiler errors due to
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 + char(0x00) + secret,
96 key.data(), key.size()))
97 NOTREACHED();
98 // 2. V = HMAC(K, V)
99 if (!hmac_.Init(std::string(key.begin(), key.end())))
100 NOTREACHED();
101 if (!hmac_.Sign(initial_value, value_.data(), value_.size()))
102 NOTREACHED();
103 }
104
105 HmacByteVectorGenerator::~HmacByteVectorGenerator() {}
106
107 uint8_t HmacByteVectorGenerator::RandByte() {
108 size_t requested_byte_in_digest = requested_bytes_ % hmac_.DigestLength();
109 if (requested_byte_in_digest == 0) {
110 // Do step 4.1 of the HMAC_DRBG Generate Process for more bits.
111 // V = HMAC(Key, V)
112 if(!hmac_.Sign(std::string(value_.begin(), value_.end()),
113 value_.data(), value_.size()))
114 NOTREACHED();
115 }
116 uint8_t result = value_[requested_byte_in_digest];
117 requested_bytes_++;
118 // Check max_number_of_bits_per_request from 10.1 Table 2
119 // max_number_of_bits_per_request == 2^19 bits == 2^16 bytes
120 DCHECK_LT(requested_bytes_, 1U << 16);
121 return result;
122 }
123
124 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698