OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cryptauth/foreground_eid_generator.h" | 5 #include "components/cryptauth/foreground_eid_generator.h" |
6 | 6 |
7 #include <cstring> | 7 #include <cstring> |
8 | 8 |
9 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 std::string ForegroundEidGenerator::EidData::DataInHex() const { | 52 std::string ForegroundEidGenerator::EidData::DataInHex() const { |
53 std::string str = "[" + current_data.DataInHex(); | 53 std::string str = "[" + current_data.DataInHex(); |
54 | 54 |
55 if (adjacent_data) { | 55 if (adjacent_data) { |
56 return str + ", " + adjacent_data->DataInHex() + "]"; | 56 return str + ", " + adjacent_data->DataInHex() + "]"; |
57 } | 57 } |
58 | 58 |
59 return str + "]"; | 59 return str + "]"; |
60 } | 60 } |
61 | 61 |
62 ForegroundEidGenerator::DataWithTimestamp::DataWithTimestamp( | |
63 const std::string& data, | |
64 const int64_t start_timestamp_ms, | |
65 const int64_t end_timestamp_ms) | |
66 : data(data), | |
67 start_timestamp_ms(start_timestamp_ms), | |
68 end_timestamp_ms(end_timestamp_ms) { | |
69 DCHECK(start_timestamp_ms < end_timestamp_ms); | |
70 DCHECK(data.size()); | |
71 } | |
72 | |
73 ForegroundEidGenerator::DataWithTimestamp::DataWithTimestamp( | |
74 const DataWithTimestamp& other) | |
75 : data(other.data), | |
76 start_timestamp_ms(other.start_timestamp_ms), | |
77 end_timestamp_ms(other.end_timestamp_ms) { | |
78 DCHECK(start_timestamp_ms < end_timestamp_ms); | |
79 DCHECK(data.size()); | |
80 } | |
81 | |
82 bool ForegroundEidGenerator::DataWithTimestamp::ContainsTime( | |
83 const int64_t timestamp_ms) const { | |
84 return start_timestamp_ms <= timestamp_ms && timestamp_ms < end_timestamp_ms; | |
85 } | |
86 | |
87 std::string ForegroundEidGenerator::DataWithTimestamp::DataInHex() const { | |
88 std::stringstream ss; | |
89 ss << "0x" << std::hex; | |
90 | |
91 for (size_t i = 0; i < data.size(); i++) { | |
92 ss << static_cast<int>(data.data()[i]); | |
93 } | |
94 | |
95 return ss.str(); | |
96 } | |
97 | |
98 ForegroundEidGenerator::ForegroundEidGenerator() | 62 ForegroundEidGenerator::ForegroundEidGenerator() |
99 : ForegroundEidGenerator(base::MakeUnique<RawEidGeneratorImpl>(), | 63 : ForegroundEidGenerator(base::MakeUnique<RawEidGeneratorImpl>(), |
100 base::MakeUnique<base::DefaultClock>()) {} | 64 base::MakeUnique<base::DefaultClock>()) {} |
101 | 65 |
102 ForegroundEidGenerator::ForegroundEidGenerator( | 66 ForegroundEidGenerator::ForegroundEidGenerator( |
103 std::unique_ptr<RawEidGenerator> raw_eid_generator, | 67 std::unique_ptr<RawEidGenerator> raw_eid_generator, |
104 std::unique_ptr<base::Clock> clock) | 68 std::unique_ptr<base::Clock> clock) |
105 : clock_(std::move(clock)), | 69 : clock_(std::move(clock)), |
106 raw_eid_generator_(std::move(raw_eid_generator)) {} | 70 raw_eid_generator_(std::move(raw_eid_generator)) {} |
107 | 71 |
(...skipping 20 matching lines...) Expand all Loading... |
128 } | 92 } |
129 | 93 |
130 std::unique_ptr<DataWithTimestamp> adjacent_eid = | 94 std::unique_ptr<DataWithTimestamp> adjacent_eid = |
131 GenerateEidDataWithTimestamp( | 95 GenerateEidDataWithTimestamp( |
132 scanning_device_beacon_seeds, | 96 scanning_device_beacon_seeds, |
133 timestamps->adjacent_period_start_timestamp_ms, | 97 timestamps->adjacent_period_start_timestamp_ms, |
134 timestamps->adjacent_period_end_timestamp_ms); | 98 timestamps->adjacent_period_end_timestamp_ms); |
135 return base::WrapUnique(new EidData(*current_eid, std::move(adjacent_eid))); | 99 return base::WrapUnique(new EidData(*current_eid, std::move(adjacent_eid))); |
136 } | 100 } |
137 | 101 |
138 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> | 102 std::unique_ptr<DataWithTimestamp> |
139 ForegroundEidGenerator::GenerateAdvertisement( | 103 ForegroundEidGenerator::GenerateAdvertisement( |
140 const std::string& advertising_device_public_key, | 104 const std::string& advertising_device_public_key, |
141 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const { | 105 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const { |
142 std::unique_ptr<EidPeriodTimestamps> timestamps = | 106 std::unique_ptr<EidPeriodTimestamps> timestamps = |
143 GetEidPeriodTimestamps(scanning_device_beacon_seeds); | 107 GetEidPeriodTimestamps(scanning_device_beacon_seeds); |
144 if (!timestamps) { | 108 if (!timestamps) { |
145 return nullptr; | 109 return nullptr; |
146 } | 110 } |
147 | 111 |
148 return GenerateAdvertisement(advertising_device_public_key, | 112 return GenerateAdvertisement(advertising_device_public_key, |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 scanning_device_beacon_seeds, | 168 scanning_device_beacon_seeds, |
205 timestamps->adjacent_period_start_timestamp_ms, | 169 timestamps->adjacent_period_start_timestamp_ms, |
206 timestamps->adjacent_period_end_timestamp_ms); | 170 timestamps->adjacent_period_end_timestamp_ms); |
207 if (adjacent_advertisement) { | 171 if (adjacent_advertisement) { |
208 possible_advertisements.push_back(adjacent_advertisement->data); | 172 possible_advertisements.push_back(adjacent_advertisement->data); |
209 } | 173 } |
210 | 174 |
211 return possible_advertisements; | 175 return possible_advertisements; |
212 } | 176 } |
213 | 177 |
214 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> | 178 std::unique_ptr<DataWithTimestamp> |
215 ForegroundEidGenerator::GenerateAdvertisement( | 179 ForegroundEidGenerator::GenerateAdvertisement( |
216 const std::string& advertising_device_public_key, | 180 const std::string& advertising_device_public_key, |
217 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 181 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
218 const int64_t start_of_period_timestamp_ms, | 182 const int64_t start_of_period_timestamp_ms, |
219 const int64_t end_of_period_timestamp_ms) const { | 183 const int64_t end_of_period_timestamp_ms) const { |
220 std::unique_ptr<DataWithTimestamp> advertising_device_identifying_data = | 184 std::unique_ptr<DataWithTimestamp> advertising_device_identifying_data = |
221 GenerateEidDataWithTimestamp( | 185 GenerateEidDataWithTimestamp( |
222 scanning_device_beacon_seeds, start_of_period_timestamp_ms, | 186 scanning_device_beacon_seeds, start_of_period_timestamp_ms, |
223 end_of_period_timestamp_ms, &advertising_device_public_key); | 187 end_of_period_timestamp_ms, &advertising_device_public_key); |
224 std::unique_ptr<DataWithTimestamp> scanning_device_identifying_data = | 188 std::unique_ptr<DataWithTimestamp> scanning_device_identifying_data = |
225 GenerateEidDataWithTimestamp(scanning_device_beacon_seeds, | 189 GenerateEidDataWithTimestamp(scanning_device_beacon_seeds, |
226 start_of_period_timestamp_ms, | 190 start_of_period_timestamp_ms, |
227 end_of_period_timestamp_ms); | 191 end_of_period_timestamp_ms); |
228 if (!advertising_device_identifying_data || | 192 if (!advertising_device_identifying_data || |
229 !scanning_device_identifying_data) { | 193 !scanning_device_identifying_data) { |
230 return nullptr; | 194 return nullptr; |
231 } | 195 } |
232 | 196 |
233 std::string full_advertisement = scanning_device_identifying_data->data + | 197 std::string full_advertisement = scanning_device_identifying_data->data + |
234 advertising_device_identifying_data->data; | 198 advertising_device_identifying_data->data; |
235 return base::WrapUnique(new DataWithTimestamp(full_advertisement, | 199 return base::WrapUnique(new DataWithTimestamp(full_advertisement, |
236 start_of_period_timestamp_ms, | 200 start_of_period_timestamp_ms, |
237 end_of_period_timestamp_ms)); | 201 end_of_period_timestamp_ms)); |
238 } | 202 } |
239 | 203 |
240 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> | 204 std::unique_ptr<DataWithTimestamp> |
241 ForegroundEidGenerator::GenerateEidDataWithTimestamp( | 205 ForegroundEidGenerator::GenerateEidDataWithTimestamp( |
242 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 206 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
243 const int64_t start_of_period_timestamp_ms, | 207 const int64_t start_of_period_timestamp_ms, |
244 const int64_t end_of_period_timestamp_ms) const { | 208 const int64_t end_of_period_timestamp_ms) const { |
245 return GenerateEidDataWithTimestamp(scanning_device_beacon_seeds, | 209 return GenerateEidDataWithTimestamp(scanning_device_beacon_seeds, |
246 start_of_period_timestamp_ms, | 210 start_of_period_timestamp_ms, |
247 end_of_period_timestamp_ms, nullptr); | 211 end_of_period_timestamp_ms, nullptr); |
248 } | 212 } |
249 | 213 |
250 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> | 214 std::unique_ptr<DataWithTimestamp> |
251 ForegroundEidGenerator::GenerateEidDataWithTimestamp( | 215 ForegroundEidGenerator::GenerateEidDataWithTimestamp( |
252 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 216 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
253 const int64_t start_of_period_timestamp_ms, | 217 const int64_t start_of_period_timestamp_ms, |
254 const int64_t end_of_period_timestamp_ms, | 218 const int64_t end_of_period_timestamp_ms, |
255 std::string const* extra_entropy) const { | 219 std::string const* extra_entropy) const { |
256 std::unique_ptr<std::string> eid_seed = GetEidSeedForPeriod( | 220 std::unique_ptr<std::string> eid_seed = GetEidSeedForPeriod( |
257 scanning_device_beacon_seeds, start_of_period_timestamp_ms); | 221 scanning_device_beacon_seeds, start_of_period_timestamp_ms); |
258 if (!eid_seed) { | 222 if (!eid_seed) { |
259 return nullptr; | 223 return nullptr; |
260 } | 224 } |
(...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
418 const int64_t end_of_period_timestamp_ms, | 382 const int64_t end_of_period_timestamp_ms, |
419 const int64_t current_timestamp_ms) { | 383 const int64_t current_timestamp_ms) { |
420 DCHECK(start_of_period_timestamp_ms <= current_timestamp_ms); | 384 DCHECK(start_of_period_timestamp_ms <= current_timestamp_ms); |
421 DCHECK(current_timestamp_ms < end_of_period_timestamp_ms); | 385 DCHECK(current_timestamp_ms < end_of_period_timestamp_ms); |
422 | 386 |
423 return current_timestamp_ms < | 387 return current_timestamp_ms < |
424 start_of_period_timestamp_ms + kNumMsInBeginningOfEidPeriod; | 388 start_of_period_timestamp_ms + kNumMsInBeginningOfEidPeriod; |
425 } | 389 } |
426 | 390 |
427 } // namespace cryptauth | 391 } // namespace cryptauth |
OLD | NEW |