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

Side by Side Diff: components/cryptauth/foreground_eid_generator.cc

Issue 2847233003: [EasyUnlock] Move DataWithTimestamp out of ForegroundEidGenerator so it can be shared. (Closed)
Patch Set: Created 3 years, 7 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
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 12 matching lines...) Expand all
23 const int64_t kMaxPositiveInt64TValue = 0x7FFFFFFF; 23 const int64_t kMaxPositiveInt64TValue = 0x7FFFFFFF;
24 } // namespace 24 } // namespace
25 25
26 const int64_t ForegroundEidGenerator::kNumMsInEidPeriod = 26 const int64_t ForegroundEidGenerator::kNumMsInEidPeriod =
27 base::TimeDelta::FromHours(8).InMilliseconds(); 27 base::TimeDelta::FromHours(8).InMilliseconds();
28 const int64_t ForegroundEidGenerator::kNumMsInBeginningOfEidPeriod = 28 const int64_t ForegroundEidGenerator::kNumMsInBeginningOfEidPeriod =
29 base::TimeDelta::FromHours(2).InMilliseconds(); 29 base::TimeDelta::FromHours(2).InMilliseconds();
30 const int8_t ForegroundEidGenerator::kBluetooth4Flag = 0x01; 30 const int8_t ForegroundEidGenerator::kBluetooth4Flag = 0x01;
31 31
32 ForegroundEidGenerator::EidData::EidData( 32 ForegroundEidGenerator::EidData::EidData(
33 const DataWithTimestamp current_data, 33 const EidDataWithTimestamp current_data,
34 std::unique_ptr<DataWithTimestamp> adjacent_data) 34 std::unique_ptr<EidDataWithTimestamp> adjacent_data)
35 : current_data(current_data), adjacent_data(std::move(adjacent_data)) {} 35 : current_data(current_data), adjacent_data(std::move(adjacent_data)) {}
36 36
37 ForegroundEidGenerator::EidData::~EidData() {} 37 ForegroundEidGenerator::EidData::~EidData() {}
38 38
39 ForegroundEidGenerator::EidData::AdjacentDataType 39 ForegroundEidGenerator::EidData::AdjacentDataType
40 ForegroundEidGenerator::EidData::GetAdjacentDataType() const { 40 ForegroundEidGenerator::EidData::GetAdjacentDataType() const {
41 if (!adjacent_data) { 41 if (!adjacent_data) {
42 return AdjacentDataType::NONE; 42 return AdjacentDataType::NONE;
43 } 43 }
44 44
45 if (adjacent_data->start_timestamp_ms < current_data.start_timestamp_ms) { 45 if (adjacent_data->start_timestamp_ms < current_data.start_timestamp_ms) {
46 return AdjacentDataType::PAST; 46 return AdjacentDataType::PAST;
47 } 47 }
48 48
49 return AdjacentDataType::FUTURE; 49 return AdjacentDataType::FUTURE;
50 } 50 }
51 51
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
108 ForegroundEidGenerator::~ForegroundEidGenerator() {} 72 ForegroundEidGenerator::~ForegroundEidGenerator() {}
109 73
110 std::unique_ptr<ForegroundEidGenerator::EidData> 74 std::unique_ptr<ForegroundEidGenerator::EidData>
111 ForegroundEidGenerator::GenerateBackgroundScanFilter( 75 ForegroundEidGenerator::GenerateBackgroundScanFilter(
112 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const { 76 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const {
113 std::unique_ptr<EidPeriodTimestamps> timestamps = 77 std::unique_ptr<EidPeriodTimestamps> timestamps =
114 GetEidPeriodTimestamps(scanning_device_beacon_seeds); 78 GetEidPeriodTimestamps(scanning_device_beacon_seeds);
115 if (!timestamps) { 79 if (!timestamps) {
116 // If the device does not have seeds for the correct period, no EIDs can be 80 // If the device does not have seeds for the correct period, no EIDs can be
117 // generated. 81 // generated.
118 return nullptr; 82 return nullptr;
119 } 83 }
120 84
121 std::unique_ptr<DataWithTimestamp> current_eid = GenerateEidDataWithTimestamp( 85 std::unique_ptr<EidDataWithTimestamp> current_eid =
122 scanning_device_beacon_seeds, 86 GenerateEidEidDataWithTimestamp(
123 timestamps->current_period_start_timestamp_ms, 87 scanning_device_beacon_seeds,
124 timestamps->current_period_end_timestamp_ms); 88 timestamps->current_period_start_timestamp_ms,
89 timestamps->current_period_end_timestamp_ms);
125 if (!current_eid) { 90 if (!current_eid) {
126 // The current EID could not be generated. 91 // The current EID could not be generated.
127 return nullptr; 92 return nullptr;
128 } 93 }
129 94
130 std::unique_ptr<DataWithTimestamp> adjacent_eid = 95 std::unique_ptr<EidDataWithTimestamp> adjacent_eid =
131 GenerateEidDataWithTimestamp( 96 GenerateEidEidDataWithTimestamp(
132 scanning_device_beacon_seeds, 97 scanning_device_beacon_seeds,
133 timestamps->adjacent_period_start_timestamp_ms, 98 timestamps->adjacent_period_start_timestamp_ms,
134 timestamps->adjacent_period_end_timestamp_ms); 99 timestamps->adjacent_period_end_timestamp_ms);
135 return base::WrapUnique(new EidData(*current_eid, std::move(adjacent_eid))); 100 return base::WrapUnique(new EidData(*current_eid, std::move(adjacent_eid)));
136 } 101 }
137 102
138 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> 103 std::unique_ptr<EidDataWithTimestamp>
139 ForegroundEidGenerator::GenerateAdvertisement( 104 ForegroundEidGenerator::GenerateAdvertisement(
140 const std::string& advertising_device_public_key, 105 const std::string& advertising_device_public_key,
141 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const { 106 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const {
142 std::unique_ptr<EidPeriodTimestamps> timestamps = 107 std::unique_ptr<EidPeriodTimestamps> timestamps =
143 GetEidPeriodTimestamps(scanning_device_beacon_seeds); 108 GetEidPeriodTimestamps(scanning_device_beacon_seeds);
144 if (!timestamps) { 109 if (!timestamps) {
145 return nullptr; 110 return nullptr;
146 } 111 }
147 112
148 return GenerateAdvertisement(advertising_device_public_key, 113 return GenerateAdvertisement(advertising_device_public_key,
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
182 147
183 std::unique_ptr<EidPeriodTimestamps> timestamps = 148 std::unique_ptr<EidPeriodTimestamps> timestamps =
184 GetEidPeriodTimestamps(scanning_device_beacon_seeds, true); 149 GetEidPeriodTimestamps(scanning_device_beacon_seeds, true);
185 if (!timestamps) { 150 if (!timestamps) {
186 // If the devices do not have seeds for the correct period, no 151 // If the devices do not have seeds for the correct period, no
187 // advertisements can be generated. 152 // advertisements can be generated.
188 return possible_advertisements; 153 return possible_advertisements;
189 } 154 }
190 155
191 if (timestamps->current_period_start_timestamp_ms != kNoTimestamp) { 156 if (timestamps->current_period_start_timestamp_ms != kNoTimestamp) {
192 std::unique_ptr<DataWithTimestamp> current_advertisement = 157 std::unique_ptr<EidDataWithTimestamp> current_advertisement =
193 GenerateAdvertisement(advertising_device_public_key, 158 GenerateAdvertisement(advertising_device_public_key,
194 scanning_device_beacon_seeds, 159 scanning_device_beacon_seeds,
195 timestamps->current_period_start_timestamp_ms, 160 timestamps->current_period_start_timestamp_ms,
196 timestamps->current_period_end_timestamp_ms); 161 timestamps->current_period_end_timestamp_ms);
197 if (current_advertisement) { 162 if (current_advertisement) {
198 possible_advertisements.push_back(current_advertisement->data); 163 possible_advertisements.push_back(current_advertisement->data);
199 } 164 }
200 } 165 }
201 166
202 std::unique_ptr<DataWithTimestamp> adjacent_advertisement = 167 std::unique_ptr<EidDataWithTimestamp> adjacent_advertisement =
203 GenerateAdvertisement(advertising_device_public_key, 168 GenerateAdvertisement(advertising_device_public_key,
204 scanning_device_beacon_seeds, 169 scanning_device_beacon_seeds,
205 timestamps->adjacent_period_start_timestamp_ms, 170 timestamps->adjacent_period_start_timestamp_ms,
206 timestamps->adjacent_period_end_timestamp_ms); 171 timestamps->adjacent_period_end_timestamp_ms);
207 if (adjacent_advertisement) { 172 if (adjacent_advertisement) {
208 possible_advertisements.push_back(adjacent_advertisement->data); 173 possible_advertisements.push_back(adjacent_advertisement->data);
209 } 174 }
210 175
211 return possible_advertisements; 176 return possible_advertisements;
212 } 177 }
213 178
214 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> 179 std::unique_ptr<EidDataWithTimestamp>
215 ForegroundEidGenerator::GenerateAdvertisement( 180 ForegroundEidGenerator::GenerateAdvertisement(
216 const std::string& advertising_device_public_key, 181 const std::string& advertising_device_public_key,
217 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, 182 const std::vector<BeaconSeed>& scanning_device_beacon_seeds,
218 const int64_t start_of_period_timestamp_ms, 183 const int64_t start_of_period_timestamp_ms,
219 const int64_t end_of_period_timestamp_ms) const { 184 const int64_t end_of_period_timestamp_ms) const {
220 std::unique_ptr<DataWithTimestamp> advertising_device_identifying_data = 185 std::unique_ptr<EidDataWithTimestamp> advertising_device_identifying_data =
221 GenerateEidDataWithTimestamp( 186 GenerateEidEidDataWithTimestamp(
222 scanning_device_beacon_seeds, start_of_period_timestamp_ms, 187 scanning_device_beacon_seeds, start_of_period_timestamp_ms,
223 end_of_period_timestamp_ms, &advertising_device_public_key); 188 end_of_period_timestamp_ms, &advertising_device_public_key);
224 std::unique_ptr<DataWithTimestamp> scanning_device_identifying_data = 189 std::unique_ptr<EidDataWithTimestamp> scanning_device_identifying_data =
225 GenerateEidDataWithTimestamp(scanning_device_beacon_seeds, 190 GenerateEidEidDataWithTimestamp(scanning_device_beacon_seeds,
226 start_of_period_timestamp_ms, 191 start_of_period_timestamp_ms,
227 end_of_period_timestamp_ms); 192 end_of_period_timestamp_ms);
228 if (!advertising_device_identifying_data || 193 if (!advertising_device_identifying_data ||
229 !scanning_device_identifying_data) { 194 !scanning_device_identifying_data) {
230 return nullptr; 195 return nullptr;
231 } 196 }
232 197
233 std::string full_advertisement = scanning_device_identifying_data->data + 198 std::string full_advertisement = scanning_device_identifying_data->data +
234 advertising_device_identifying_data->data; 199 advertising_device_identifying_data->data;
235 return base::WrapUnique(new DataWithTimestamp(full_advertisement, 200 return base::WrapUnique(new EidDataWithTimestamp(full_advertisement,
236 start_of_period_timestamp_ms, 201 start_of_period_timestamp_ms,
237 end_of_period_timestamp_ms)); 202 end_of_period_timestamp_ms));
238 } 203 }
239 204
240 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> 205 std::unique_ptr<EidDataWithTimestamp>
241 ForegroundEidGenerator::GenerateEidDataWithTimestamp( 206 ForegroundEidGenerator::GenerateEidEidDataWithTimestamp(
242 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, 207 const std::vector<BeaconSeed>& scanning_device_beacon_seeds,
243 const int64_t start_of_period_timestamp_ms, 208 const int64_t start_of_period_timestamp_ms,
244 const int64_t end_of_period_timestamp_ms) const { 209 const int64_t end_of_period_timestamp_ms) const {
245 return GenerateEidDataWithTimestamp(scanning_device_beacon_seeds, 210 return GenerateEidEidDataWithTimestamp(scanning_device_beacon_seeds,
246 start_of_period_timestamp_ms, 211 start_of_period_timestamp_ms,
247 end_of_period_timestamp_ms, nullptr); 212 end_of_period_timestamp_ms, nullptr);
248 } 213 }
249 214
250 std::unique_ptr<ForegroundEidGenerator::DataWithTimestamp> 215 std::unique_ptr<EidDataWithTimestamp>
251 ForegroundEidGenerator::GenerateEidDataWithTimestamp( 216 ForegroundEidGenerator::GenerateEidEidDataWithTimestamp(
252 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, 217 const std::vector<BeaconSeed>& scanning_device_beacon_seeds,
253 const int64_t start_of_period_timestamp_ms, 218 const int64_t start_of_period_timestamp_ms,
254 const int64_t end_of_period_timestamp_ms, 219 const int64_t end_of_period_timestamp_ms,
255 std::string const* extra_entropy) const { 220 std::string const* extra_entropy) const {
256 std::unique_ptr<std::string> eid_seed = GetEidSeedForPeriod( 221 std::unique_ptr<std::string> eid_seed = GetEidSeedForPeriod(
257 scanning_device_beacon_seeds, start_of_period_timestamp_ms); 222 scanning_device_beacon_seeds, start_of_period_timestamp_ms);
258 if (!eid_seed) { 223 if (!eid_seed) {
259 return nullptr; 224 return nullptr;
260 } 225 }
261 226
262 std::string eid_data = raw_eid_generator_->GenerateEid( 227 std::string eid_data = raw_eid_generator_->GenerateEid(
263 *eid_seed, start_of_period_timestamp_ms, extra_entropy); 228 *eid_seed, start_of_period_timestamp_ms, extra_entropy);
264 229
265 return base::WrapUnique(new DataWithTimestamp( 230 return base::WrapUnique(new EidDataWithTimestamp(
266 eid_data, start_of_period_timestamp_ms, end_of_period_timestamp_ms)); 231 eid_data, start_of_period_timestamp_ms, end_of_period_timestamp_ms));
267 } 232 }
268 233
269 std::unique_ptr<std::string> ForegroundEidGenerator::GetEidSeedForPeriod( 234 std::unique_ptr<std::string> ForegroundEidGenerator::GetEidSeedForPeriod(
270 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, 235 const std::vector<BeaconSeed>& scanning_device_beacon_seeds,
271 const int64_t start_of_period_timestamp_ms) const { 236 const int64_t start_of_period_timestamp_ms) const {
272 for (auto seed : scanning_device_beacon_seeds) { 237 for (auto seed : scanning_device_beacon_seeds) {
273 if (seed.start_time_millis() <= start_of_period_timestamp_ms && 238 if (seed.start_time_millis() <= start_of_period_timestamp_ms &&
274 start_of_period_timestamp_ms < seed.end_time_millis()) { 239 start_of_period_timestamp_ms < seed.end_time_millis()) {
275 return base::WrapUnique(new std::string(seed.data())); 240 return base::WrapUnique(new std::string(seed.data()));
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 const int64_t end_of_period_timestamp_ms, 383 const int64_t end_of_period_timestamp_ms,
419 const int64_t current_timestamp_ms) { 384 const int64_t current_timestamp_ms) {
420 DCHECK(start_of_period_timestamp_ms <= current_timestamp_ms); 385 DCHECK(start_of_period_timestamp_ms <= current_timestamp_ms);
421 DCHECK(current_timestamp_ms < end_of_period_timestamp_ms); 386 DCHECK(current_timestamp_ms < end_of_period_timestamp_ms);
422 387
423 return current_timestamp_ms < 388 return current_timestamp_ms <
424 start_of_period_timestamp_ms + kNumMsInBeginningOfEidPeriod; 389 start_of_period_timestamp_ms + kNumMsInBeginningOfEidPeriod;
425 } 390 }
426 391
427 } // namespace cryptauth 392 } // namespace cryptauth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698