OLD | NEW |
---|---|
(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) { | |
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 ByteVector ByteVectorGenerator::GetRandomByteVector() { | |
51 ByteVector bytes(byte_count_); | |
52 crypto::RandBytes(&bytes[0], bytes.size()); | |
53 return bytes; | |
54 } | |
55 | |
56 ByteVector ByteVectorGenerator::GetWeightedRandomByteVector( | |
57 Probability probability) { | |
58 ByteVector bytes = GetRandomByteVector(); | |
59 switch (probability) { | |
60 case PROBABILITY_75: | |
61 return *ByteVectorOr(GetRandomByteVector(), &bytes); | |
62 case PROBABILITY_50: | |
63 return bytes; | |
64 } | |
65 NOTREACHED(); | |
66 return bytes; | |
67 } | |
68 | |
69 HmacByteVectorGenerator::HmacByteVectorGenerator(size_t byte_count, | |
edknapp
2014/02/11 17:28:00
Please pass in entropy_input, nonce, and personali
Steven Holte
2014/02/11 22:08:14
Separated entropy_input, personalization_string, a
| |
70 const std::string& secret) | |
71 : ByteVectorGenerator(byte_count), | |
72 hmac_(crypto::HMAC::SHA256), | |
73 value_(hmac_.DigestLength(), 0x01), | |
74 requested_bytes_(0) { | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
Why does this need to be a class member, rather th
wtc
2014/02/11 20:55:29
requested_bytes_ is the total number of bytes gene
Steven Holte
2014/02/11 22:08:14
This is the number of bytes streamed from the HMAC
| |
75 // HMAC_DRBG Instantiate Process | |
76 // 2. Key = 0x00 00...00 | |
77 // 3. V = 0x01 01...01 | |
78 ByteVector key(hmac_.DigestLength(), 0x00); | |
79 std::string initial_value(value_.begin(), value_.end()); | |
80 | |
81 // 4. (Key, V) = HMAC_DRBG_Update(seed_material, Key, V) | |
82 // HMAC_DRBG Update Process inlined | |
edknapp
2014/02/11 17:28:00
Please move this to a private function:
Update(pro
Steven Holte
2014/02/11 22:08:14
Done.
| |
83 // 1. K = HMAC(K, V || 0x00 || provided_data) | |
84 crypto::HMAC keygen_hmac(crypto::HMAC::SHA256); | |
85 if (!keygen_hmac.Init(std::string(key.begin(), key.end()))) | |
86 NOTREACHED(); | |
87 if (!keygen_hmac.Sign(initial_value + char(0x00) + secret, | |
88 &key[0], key.size())) | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
Nit: Add {}'s. Align params.
Alternatively, make
Steven Holte
2014/02/11 22:08:14
Done.
| |
89 NOTREACHED(); | |
90 // 2. V = HMAC(K, V) | |
91 if (!hmac_.Init(std::string(key.begin(), key.end()))) | |
92 NOTREACHED(); | |
93 if (!hmac_.Sign(initial_value, &value_[0], value_.size())) | |
94 NOTREACHED(); | |
95 } | |
96 | |
97 HmacByteVectorGenerator::~HmacByteVectorGenerator() {} | |
98 | |
99 ByteVector HmacByteVectorGenerator::GetRandomByteVector() { | |
100 ByteVector bytes(byte_count_); | |
101 uint8_t* data = bytes.data(); | |
102 size_t bytes_to_go = byte_count_; | |
103 while (bytes_to_go) { | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
Nit: bytes_to_go > 0
Steven Holte
2014/02/11 22:08:14
Done.
| |
104 size_t requested_byte_in_digest = requested_bytes_ % hmac_.DigestLength(); | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
The digest length is constant between iterations,
Steven Holte
2014/02/11 22:08:14
Done.
| |
105 if (requested_byte_in_digest == 0) { | |
106 // Do step 4.1 of the HMAC_DRBG Generate Process for more bits. | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
This function is now pretty complex. Given it's im
wtc
2014/02/11 20:55:29
I don't know of such a class in the Chromium sourc
| |
107 // V = HMAC(Key, V) | |
108 if (!hmac_.Sign(std::string(value_.begin(), value_.end()), | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
Can the param be a StringPiece rather than a std::
Steven Holte
2014/02/11 22:08:14
value_ is a ByteVector, so StringPiece doesn't lik
| |
109 &value_[0], value_.size())) | |
Alexei Svitkine (slow)
2014/02/11 16:59:06
Nit: Same comment as above about {}'s and aligning
Steven Holte
2014/02/11 22:08:14
Done.
| |
110 NOTREACHED(); | |
111 } | |
112 size_t n = std::min(bytes_to_go, | |
113 hmac_.DigestLength() - requested_byte_in_digest); | |
114 memcpy(data, &value_[requested_byte_in_digest], n); | |
115 data += n; | |
116 bytes_to_go -= n; | |
117 requested_bytes_ += n; | |
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 } | |
122 return bytes; | |
123 } | |
124 | |
125 } // namespace rappor | |
OLD | NEW |