Skip to content

Commit

Permalink
[FastPair] FastPairHandshake class
Browse files Browse the repository at this point in the history
This changes add the FastPairHandshake class, a lookup class to manage
instances of it and related test classes.

The FastPairHandshake class performs the handshake logic that was
previously in FastPairPairer. Moving the logic into this class enables
sharing it between the Scanning and Pairing components.

This CL is part of a chain of CLs to refactor this area to enable the
handshake to be performed prior to showing the UX.

Change-Id: I3295e5fad89f58c063e6e9d3dbe8a125a7b8e49f
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3312015
Reviewed-by: Jon Mann <jonmann@chromium.org>
Commit-Queue: Shane Fitzpatrick <shanefitz@google.com>
Auto-Submit: Shane Fitzpatrick <shanefitz@google.com>
Cr-Commit-Position: refs/heads/main@{#949366}
  • Loading branch information
Shane Fitzpatrick authored and Chromium LUCI CQ committed Dec 8, 2021
1 parent 4718751 commit 3b605ac
Show file tree
Hide file tree
Showing 13 changed files with 813 additions and 34 deletions.
15 changes: 15 additions & 0 deletions ash/quick_pair/fast_pair_handshake/BUILD.gn
Expand Up @@ -18,6 +18,12 @@ source_set("fast_pair_handshake") {
"fast_pair_gatt_service_client.h",
"fast_pair_gatt_service_client_impl.cc",
"fast_pair_gatt_service_client_impl.h",
"fast_pair_handshake.cc",
"fast_pair_handshake.h",
"fast_pair_handshake_impl.cc",
"fast_pair_handshake_impl.h",
"fast_pair_handshake_lookup.cc",
"fast_pair_handshake_lookup.h",
"fast_pair_key_pair.cc",
"fast_pair_key_pair.h",
]
Expand All @@ -40,10 +46,16 @@ static_library("test_support") {
testonly = true

sources = [
"fake_fast_pair_data_encryptor.cc",
"fake_fast_pair_data_encryptor.h",
"fake_fast_pair_gatt_service_client.cc",
"fake_fast_pair_gatt_service_client.h",
"fake_fast_pair_handshake.cc",
"fake_fast_pair_handshake.h",
"fast_pair_data_encryptor.h",
"fast_pair_gatt_service_client.h",
"fast_pair_handshake.h",
"fast_pair_handshake_lookup.h",
]

deps = [
Expand All @@ -63,14 +75,17 @@ source_set("unit_tests") {
"fast_pair_data_encryptor_unittest.cc",
"fast_pair_encryption_unittest.cc",
"fast_pair_gatt_service_client_unittest.cc",
"fast_pair_handshake_impl_unittest.cc",
]

deps = [
":fast_pair_handshake",
":test_support",
"//ash/quick_pair/common",
"//ash/quick_pair/repository:test_support",
"//ash/services/quick_pair",
"//ash/services/quick_pair:test_support",
"//ash/services/quick_pair/public/cpp",
"//base",
"//base/test:test_support",
"//device/bluetooth",
Expand Down
@@ -0,0 +1,40 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/fast_pair_handshake/fake_fast_pair_data_encryptor.h"

namespace ash {
namespace quick_pair {

FakeFastPairDataEncryptor::FakeFastPairDataEncryptor() = default;

FakeFastPairDataEncryptor::~FakeFastPairDataEncryptor() = default;

const std::array<uint8_t, kBlockSizeBytes>
FakeFastPairDataEncryptor::EncryptBytes(
const std::array<uint8_t, kBlockSizeBytes>& bytes_to_encrypt) {
return encrypted_bytes_;
}

const absl::optional<std::array<uint8_t, 64>>&
FakeFastPairDataEncryptor::GetPublicKey() {
return public_key_;
}

void FakeFastPairDataEncryptor::ParseDecryptedResponse(
const std::vector<uint8_t>& encrypted_response_bytes,
base::OnceCallback<void(const absl::optional<DecryptedResponse>&)>
callback) {
std::move(callback).Run(response_);
}

void FakeFastPairDataEncryptor::ParseDecryptedPasskey(
const std::vector<uint8_t>& encrypted_passkey_bytes,
base::OnceCallback<void(const absl::optional<DecryptedPasskey>&)>
callback) {
std::move(callback).Run(passkey_);
}

} // namespace quick_pair
} // namespace ash
60 changes: 60 additions & 0 deletions ash/quick_pair/fast_pair_handshake/fake_fast_pair_data_encryptor.h
@@ -0,0 +1,60 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_
#define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_

#include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"

namespace ash {
namespace quick_pair {

class FakeFastPairDataEncryptor : public FastPairDataEncryptor {
public:
FakeFastPairDataEncryptor();
FakeFastPairDataEncryptor(const FakeFastPairDataEncryptor&) = delete;
FakeFastPairDataEncryptor& operator=(const FakeFastPairDataEncryptor&) =
delete;
~FakeFastPairDataEncryptor() override;

void public_key(absl::optional<std::array<uint8_t, 64>> public_key) {
public_key_ = std::move(public_key);
}

void encrypted_bytes(std::array<uint8_t, kBlockSizeBytes> encrypted_bytes) {
encrypted_bytes_ = std::move(encrypted_bytes);
}

void response(absl::optional<DecryptedResponse> response) {
response_ = std::move(response);
}

void passkey(absl::optional<DecryptedPasskey> passkey) {
passkey_ = std::move(passkey);
}

// FastPairDataEncryptor:
const std::array<uint8_t, kBlockSizeBytes> EncryptBytes(
const std::array<uint8_t, kBlockSizeBytes>& bytes_to_encrypt) override;
const absl::optional<std::array<uint8_t, 64>>& GetPublicKey() override;
void ParseDecryptedResponse(
const std::vector<uint8_t>& encrypted_response_bytes,
base::OnceCallback<void(const absl::optional<DecryptedResponse>&)>
callback) override;
void ParseDecryptedPasskey(
const std::vector<uint8_t>& encrypted_passkey_bytes,
base::OnceCallback<void(const absl::optional<DecryptedPasskey>&)>
callback) override;

private:
absl::optional<std::array<uint8_t, 64>> public_key_ = absl::nullopt;
std::array<uint8_t, kBlockSizeBytes> encrypted_bytes_ = {};
absl::optional<DecryptedResponse> response_ = absl::nullopt;
absl::optional<DecryptedPasskey> passkey_ = absl::nullopt;
};

} // namespace quick_pair
} // namespace ash

#endif // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_
42 changes: 42 additions & 0 deletions ash/quick_pair/fast_pair_handshake/fake_fast_pair_handshake.cc
@@ -0,0 +1,42 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/fast_pair_handshake/fake_fast_pair_handshake.h"

#include <cstdint>
#include <memory>
#include <utility>

#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/pair_failure.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_gatt_service_client.h"
#include "base/callback.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace ash {
namespace quick_pair {

FakeFastPairHandshake::FakeFastPairHandshake(
scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete,
std::unique_ptr<FastPairDataEncryptor> data_encryptor,
std::unique_ptr<FastPairGattServiceClient> gatt_service_client)
: FastPairHandshake(std::move(adapter),
std::move(device),
std::move(on_complete),
std::move(data_encryptor),
std::move(gatt_service_client)) {}

FakeFastPairHandshake::~FakeFastPairHandshake() = default;

void FakeFastPairHandshake::InvokeCallback(
absl::optional<PairFailure> failure) {
std::move(on_complete_callback_).Run(device_, std::move(failure));
completed_successfully_ = !failure.has_value();
}

} // namespace quick_pair
} // namespace ash
35 changes: 35 additions & 0 deletions ash/quick_pair/fast_pair_handshake/fake_fast_pair_handshake.h
@@ -0,0 +1,35 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAKE_FAST_PAIR_HANDSHAKE_H_
#define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAKE_FAST_PAIR_HANDSHAKE_H_

#include <memory>

#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake.h"

#include "base/memory/scoped_refptr.h"

namespace ash {
namespace quick_pair {

class FakeFastPairHandshake : public FastPairHandshake {
public:
FakeFastPairHandshake(
scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete,
std::unique_ptr<FastPairDataEncryptor> data_encryptor = nullptr,
std::unique_ptr<FastPairGattServiceClient> gatt_service_client = nullptr);
FakeFastPairHandshake(const FakeFastPairHandshake&) = delete;
FakeFastPairHandshake& operator=(const FakeFastPairHandshake&) = delete;
~FakeFastPairHandshake();

void InvokeCallback(absl::optional<PairFailure> failure = absl::nullopt);
};

} // namespace quick_pair
} // namespace ash

#endif // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAKE_FAST_PAIR_HANDSHAKE_H_
Expand Up @@ -9,6 +9,7 @@
#include "ash/quick_pair/common/constants.h"
#include "ash/quick_pair/common/logging.h"
#include "ash/quick_pair/common/pair_failure.h"
#include "ash/quick_pair/fast_pair_handshake/fake_fast_pair_data_encryptor.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor_impl.h"
#include "base/bind.h"
Expand Down Expand Up @@ -259,41 +260,10 @@ std::unique_ptr<FakeBluetoothDevice> CreateTestBluetoothDevice(
namespace ash {
namespace quick_pair {

class FakeFastPairDataEncryptor : public FastPairDataEncryptor {
public:
const std::array<uint8_t, kBlockSizeBytes> EncryptBytes(
const std::array<uint8_t, kBlockSizeBytes>& bytes_to_encrypt) override {
return encrypted_bytes_;
}

const absl::optional<std::array<uint8_t, 64>>& GetPublicKey() override {
static absl::optional<std::array<uint8_t, 64>> val = kPublicKey;
return val;
}

void ParseDecryptedResponse(
const std::vector<uint8_t>& encrypted_response_bytes,
base::OnceCallback<void(const absl::optional<DecryptedResponse>&)>
callback) override {}

void ParseDecryptedPasskey(
const std::vector<uint8_t>& encrypted_passkey_bytes,
base::OnceCallback<void(const absl::optional<DecryptedPasskey>&)>
callback) override {}

void SetEncryptedBytes(std::array<uint8_t, kBlockSizeBytes> encrypted_bytes) {
encrypted_bytes_ = std::move(encrypted_bytes);
}

FakeFastPairDataEncryptor() = default;
~FakeFastPairDataEncryptor() override = default;

private:
std::array<uint8_t, kBlockSizeBytes> encrypted_bytes_ = {};
};

class FastPairGattServiceClientTest : public testing::Test {
public:
void SetUp() override { fast_pair_data_encryptor_->public_key(kPublicKey); }

void SuccessfulGattConnectionSetUp() {
adapter_ = base::MakeRefCounted<FakeBluetoothAdapter>();
device_ = CreateTestBluetoothDevice(
Expand Down Expand Up @@ -549,7 +519,7 @@ class FastPairGattServiceClientTest : public testing::Test {
std::unique_ptr<FakeBluetoothDevice> device_;
std::unique_ptr<FakeBluetoothGattCharacteristic>
fake_key_based_characteristic_;
std::unique_ptr<FastPairDataEncryptor> fast_pair_data_encryptor_ =
std::unique_ptr<FakeFastPairDataEncryptor> fast_pair_data_encryptor_ =
std::make_unique<FakeFastPairDataEncryptor>();
std::unique_ptr<FakeBluetoothGattCharacteristic> fake_passkey_characteristic_;
std::unique_ptr<testing::NiceMock<device::MockBluetoothGattService>>
Expand Down
34 changes: 34 additions & 0 deletions ash/quick_pair/fast_pair_handshake/fast_pair_handshake.cc
@@ -0,0 +1,34 @@
// Copyright 2021 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "ash/quick_pair/fast_pair_handshake/fast_pair_handshake.h"

#include <cstdint>

#include "ash/quick_pair/common/device.h"
#include "ash/quick_pair/common/pair_failure.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor_impl.h"
#include "ash/quick_pair/fast_pair_handshake/fast_pair_gatt_service_client_impl.h"
#include "base/callback.h"
#include "device/bluetooth/bluetooth_adapter.h"

namespace ash {
namespace quick_pair {

FastPairHandshake::FastPairHandshake(
scoped_refptr<device::BluetoothAdapter> adapter,
scoped_refptr<Device> device,
OnCompleteCallback on_complete,
std::unique_ptr<FastPairDataEncryptor> data_encryptor,
std::unique_ptr<FastPairGattServiceClient> gatt_service_client)
: adapter_(std::move(adapter)),
device_(std::move(device)),
on_complete_callback_(std::move(on_complete)),
fast_pair_data_encryptor_(std::move(data_encryptor)),
fast_pair_gatt_service_client_(std::move(gatt_service_client)) {}

FastPairHandshake::~FastPairHandshake() = default;

} // namespace quick_pair
} // namespace ash

0 comments on commit 3b605ac

Please sign in to comment.