| 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 #ifndef COMPONENTS_CRYPTAUTH_BLE_FOREGROUND_EID_GENERATOR_H_ | 5 #ifndef COMPONENTS_CRYPTAUTH_BLE_FOREGROUND_EID_GENERATOR_H_ |
| 6 #define COMPONENTS_CRYPTAUTH_BLE_FOREGROUND_EID_GENERATOR_H_ | 6 #define COMPONENTS_CRYPTAUTH_BLE_FOREGROUND_EID_GENERATOR_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/time/clock.h" | 14 #include "base/time/clock.h" |
| 15 #include "components/cryptauth/eid_data_with_timestamp.h" |
| 15 | 16 |
| 16 namespace cryptauth { | 17 namespace cryptauth { |
| 17 | 18 |
| 18 class BeaconSeed; | 19 class BeaconSeed; |
| 19 class RawEidGenerator; | 20 class RawEidGenerator; |
| 20 struct RemoteDevice; | 21 struct RemoteDevice; |
| 21 | 22 |
| 22 // Generates ephemeral ID (EID) values that are broadcast for foreground BLE | 23 // Generates ephemeral ID (EID) values that are broadcast for foreground BLE |
| 23 // advertisements in the ProximityAuth protocol. | 24 // advertisements in the ProximityAuth protocol. |
| 24 // | 25 // |
| 25 // When advertising in foreground mode, we don't care about battery consumption | 26 // When advertising in foreground mode, we don't care about battery consumption |
| 26 // while advertising. We assume, however, that the scanning side is | 27 // while advertising. We assume, however, that the scanning side is |
| 27 // battery-conscious, and is using hardware-based scanning. | 28 // battery-conscious, and is using hardware-based scanning. |
| 28 // | 29 // |
| 29 // For the inverse of this model, in which advertising is battery-sensitive, see | 30 // For the inverse of this model, in which advertising is battery-sensitive, see |
| 30 // BackgroundEidGenerator. | 31 // BackgroundEidGenerator. |
| 31 // | 32 // |
| 32 // A peripheral-role device advertises a 4-byte advertisement with two parts: a | 33 // A peripheral-role device advertises a 4-byte advertisement with two parts: a |
| 33 // 2-byte EID which is specific to the central-role device with which it intends | 34 // 2-byte EID which is specific to the central-role device with which it intends |
| 34 // to communicate, and a 2-byte EID which is specific to the peripheral-role | 35 // to communicate, and a 2-byte EID which is specific to the peripheral-role |
| 35 // device. | 36 // device. |
| 36 // | 37 // |
| 37 // This class uses EID seed values synced from the back-end to generate these | 38 // This class uses EID seed values synced from the back-end to generate these |
| 38 // EIDs. | 39 // EIDs. |
| 39 // | 40 // |
| 40 // See go/proximity-auth-ble-advertising. | 41 // See go/proximity-auth-ble-advertising. |
| 41 class ForegroundEidGenerator { | 42 class ForegroundEidGenerator { |
| 42 public: | 43 public: |
| 43 // Stores EID-related data and timestamps at which time this data becomes | |
| 44 // active or inactive. | |
| 45 struct DataWithTimestamp { | |
| 46 DataWithTimestamp(const std::string& data, | |
| 47 const int64_t start_timestamp_ms, | |
| 48 const int64_t end_timestamp_ms); | |
| 49 DataWithTimestamp(const DataWithTimestamp& other); | |
| 50 | |
| 51 bool ContainsTime(const int64_t timestamp_ms) const; | |
| 52 std::string DataInHex() const; | |
| 53 | |
| 54 const std::string data; | |
| 55 const int64_t start_timestamp_ms; | |
| 56 const int64_t end_timestamp_ms; | |
| 57 }; | |
| 58 | |
| 59 // Data for both a current and adjacent EID. The current EID *must* be | 44 // Data for both a current and adjacent EID. The current EID *must* be |
| 60 // supplied, but adjacent data may be null. Each EID consists of a 2-byte EID | 45 // supplied, but adjacent data may be null. Each EID consists of a 2-byte EID |
| 61 // value paired with the timestamp at which time this value becomes active or | 46 // value paired with the timestamp at which time this value becomes active or |
| 62 // inactive. | 47 // inactive. |
| 63 struct EidData { | 48 struct EidData { |
| 64 enum AdjacentDataType { NONE, PAST, FUTURE }; | 49 enum AdjacentDataType { NONE, PAST, FUTURE }; |
| 65 | 50 |
| 66 EidData(const DataWithTimestamp current_data, | 51 EidData(const EidDataWithTimestamp current_data, |
| 67 std::unique_ptr<DataWithTimestamp> adjacent_data); | 52 std::unique_ptr<EidDataWithTimestamp> adjacent_data); |
| 68 ~EidData(); | 53 ~EidData(); |
| 69 | 54 |
| 70 AdjacentDataType GetAdjacentDataType() const; | 55 AdjacentDataType GetAdjacentDataType() const; |
| 71 std::string DataInHex() const; | 56 std::string DataInHex() const; |
| 72 | 57 |
| 73 const DataWithTimestamp current_data; | 58 const EidDataWithTimestamp current_data; |
| 74 const std::unique_ptr<DataWithTimestamp> adjacent_data; | 59 const std::unique_ptr<EidDataWithTimestamp> adjacent_data; |
| 75 }; | 60 }; |
| 76 | 61 |
| 77 // The flag used to denote that a Bluetooth 4.0 device has sent an | 62 // The flag used to denote that a Bluetooth 4.0 device has sent an |
| 78 // advertisement. This flag indicates to the recipient that the sender cannot | 63 // advertisement. This flag indicates to the recipient that the sender cannot |
| 79 // act as both a central- and peripheral-role device simultaneously, so the | 64 // act as both a central- and peripheral-role device simultaneously, so the |
| 80 // recipient should advertise back instead of initializing a connection. | 65 // recipient should advertise back instead of initializing a connection. |
| 81 static const int8_t kBluetooth4Flag; | 66 static const int8_t kBluetooth4Flag; |
| 82 | 67 |
| 83 ForegroundEidGenerator(); | 68 ForegroundEidGenerator(); |
| 84 virtual ~ForegroundEidGenerator(); | 69 virtual ~ForegroundEidGenerator(); |
| 85 | 70 |
| 86 // Generates EID data for the given EID seeds to be used as a background scan | 71 // Generates EID data for the given EID seeds to be used as a background scan |
| 87 // filter. In the normal case, two DataWithTimestamp values are returned, one | 72 // filter. In the normal case, two EidDataWithTimestamp values are returned, |
| 88 // for each EID seed rotation period. If data has not been synced from the | 73 // one for each EID seed rotation period. If data has not been synced from the |
| 89 // backend recently and EID seeds are unavailable, nullptr is returned. | 74 // backend recently and EID seeds are unavailable, nullptr is returned. |
| 90 virtual std::unique_ptr<EidData> GenerateBackgroundScanFilter( | 75 virtual std::unique_ptr<EidData> GenerateBackgroundScanFilter( |
| 91 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const; | 76 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const; |
| 92 | 77 |
| 93 // Generates advertisement data for the given EID seeds. If data has not been | 78 // Generates advertisement data for the given EID seeds. If data has not been |
| 94 // synced from the back-end recently and EID seeds are unavailable, nullptr is | 79 // synced from the back-end recently and EID seeds are unavailable, nullptr is |
| 95 // returned. | 80 // returned. |
| 96 virtual std::unique_ptr<DataWithTimestamp> GenerateAdvertisement( | 81 virtual std::unique_ptr<EidDataWithTimestamp> GenerateAdvertisement( |
| 97 const std::string& advertising_device_public_key, | 82 const std::string& advertising_device_public_key, |
| 98 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const; | 83 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const; |
| 99 | 84 |
| 100 // Generates all possible advertisements that could be created by a device | 85 // Generates all possible advertisements that could be created by a device |
| 101 // given that device's public key and the beacon seeds of the device which is | 86 // given that device's public key and the beacon seeds of the device which is |
| 102 // intended to scan for the advertisement. | 87 // intended to scan for the advertisement. |
| 103 virtual std::vector<std::string> GeneratePossibleAdvertisements( | 88 virtual std::vector<std::string> GeneratePossibleAdvertisements( |
| 104 const std::string& advertising_device_public_key, | 89 const std::string& advertising_device_public_key, |
| 105 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const; | 90 const std::vector<BeaconSeed>& scanning_device_beacon_seeds) const; |
| 106 | 91 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 118 int64_t adjacent_period_start_timestamp_ms; | 103 int64_t adjacent_period_start_timestamp_ms; |
| 119 int64_t adjacent_period_end_timestamp_ms; | 104 int64_t adjacent_period_end_timestamp_ms; |
| 120 }; | 105 }; |
| 121 | 106 |
| 122 static const int64_t kNumMsInEidPeriod; | 107 static const int64_t kNumMsInEidPeriod; |
| 123 static const int64_t kNumMsInBeginningOfEidPeriod; | 108 static const int64_t kNumMsInBeginningOfEidPeriod; |
| 124 | 109 |
| 125 ForegroundEidGenerator(std::unique_ptr<RawEidGenerator> raw_eid_generator, | 110 ForegroundEidGenerator(std::unique_ptr<RawEidGenerator> raw_eid_generator, |
| 126 std::unique_ptr<base::Clock> clock); | 111 std::unique_ptr<base::Clock> clock); |
| 127 | 112 |
| 128 std::unique_ptr<DataWithTimestamp> GenerateAdvertisement( | 113 std::unique_ptr<EidDataWithTimestamp> GenerateAdvertisement( |
| 129 const std::string& advertising_device_public_key, | 114 const std::string& advertising_device_public_key, |
| 130 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 115 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
| 131 const int64_t start_of_period_timestamp_ms, | 116 const int64_t start_of_period_timestamp_ms, |
| 132 const int64_t end_of_period_timestamp_ms) const; | 117 const int64_t end_of_period_timestamp_ms) const; |
| 133 | 118 |
| 134 std::unique_ptr<DataWithTimestamp> GenerateEidDataWithTimestamp( | 119 std::unique_ptr<EidDataWithTimestamp> GenerateEidEidDataWithTimestamp( |
| 135 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 120 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
| 136 const int64_t start_of_period_timestamp_ms, | 121 const int64_t start_of_period_timestamp_ms, |
| 137 const int64_t end_of_period_timestamp_ms) const; | 122 const int64_t end_of_period_timestamp_ms) const; |
| 138 | 123 |
| 139 std::unique_ptr<DataWithTimestamp> GenerateEidDataWithTimestamp( | 124 std::unique_ptr<EidDataWithTimestamp> GenerateEidEidDataWithTimestamp( |
| 140 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 125 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
| 141 const int64_t start_of_period_timestamp_ms, | 126 const int64_t start_of_period_timestamp_ms, |
| 142 const int64_t end_of_period_timestamp_ms, | 127 const int64_t end_of_period_timestamp_ms, |
| 143 std::string const* extra_entropy) const; | 128 std::string const* extra_entropy) const; |
| 144 | 129 |
| 145 std::unique_ptr<std::string> GetEidSeedForPeriod( | 130 std::unique_ptr<std::string> GetEidSeedForPeriod( |
| 146 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, | 131 const std::vector<BeaconSeed>& scanning_device_beacon_seeds, |
| 147 const int64_t start_of_period_timestamp_ms) const; | 132 const int64_t start_of_period_timestamp_ms) const; |
| 148 | 133 |
| 149 std::unique_ptr<EidPeriodTimestamps> GetEidPeriodTimestamps( | 134 std::unique_ptr<EidPeriodTimestamps> GetEidPeriodTimestamps( |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 CryptAuthForegroundEidGeneratorTest, | 179 CryptAuthForegroundEidGeneratorTest, |
| 195 GeneratePossibleAdvertisements_NoAdvertisements_SeedsTooFarInPast); | 180 GeneratePossibleAdvertisements_NoAdvertisements_SeedsTooFarInPast); |
| 196 FRIEND_TEST_ALL_PREFIXES( | 181 FRIEND_TEST_ALL_PREFIXES( |
| 197 CryptAuthForegroundEidGeneratorTest, | 182 CryptAuthForegroundEidGeneratorTest, |
| 198 GeneratePossibleAdvertisements_NoAdvertisements_EmptySeeds); | 183 GeneratePossibleAdvertisements_NoAdvertisements_EmptySeeds); |
| 199 }; | 184 }; |
| 200 | 185 |
| 201 } // cryptauth | 186 } // cryptauth |
| 202 | 187 |
| 203 #endif // COMPONENTS_CRYPTAUTH_BLE_FOREGROUND_EID_GENERATOR_H_ | 188 #endif // COMPONENTS_CRYPTAUTH_BLE_FOREGROUND_EID_GENERATOR_H_ |
| OLD | NEW |