mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 14:46:12 -04:00
Add FakeFastPairDataEncryptor to help unit test FastPairHandshake
PiperOrigin-RevId: 518967535
This commit is contained in:
committed by
Copybara-Service
parent
f2331cfb42
commit
fd955d864f
@@ -33,8 +33,8 @@
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
std::array<uint8_t, kAesBlockByteSize> FastPairDecryption::DecryptBytes(
|
||||
namespace {
|
||||
std::array<uint8_t, kAesBlockByteSize> DecryptBytes(
|
||||
const std::array<uint8_t, kAesBlockByteSize>& aes_key_bytes,
|
||||
const std::array<uint8_t, kAesBlockByteSize>& encrypted_bytes) {
|
||||
AES_KEY aes_key;
|
||||
@@ -48,6 +48,7 @@ std::array<uint8_t, kAesBlockByteSize> FastPairDecryption::DecryptBytes(
|
||||
AES_decrypt(encrypted_bytes.data(), decrypted_bytes.data(), &aes_key);
|
||||
return decrypted_bytes;
|
||||
}
|
||||
} // namespace
|
||||
|
||||
// Decrypts the encrypted response
|
||||
// (https://developers.google.com/nearby/fast-pair/specifications/characteristics#table1.4)
|
||||
|
||||
@@ -29,10 +29,6 @@ class FastPairDecryption {
|
||||
public:
|
||||
static constexpr int kAesBlockByteSize = 16;
|
||||
|
||||
static std::array<uint8_t, kAesBlockByteSize> DecryptBytes(
|
||||
const std::array<uint8_t, kAesBlockByteSize>& aes_key_bytes,
|
||||
const std::array<uint8_t, kAesBlockByteSize>& encrypted_bytes);
|
||||
|
||||
static std::optional<DecryptedResponse> ParseDecryptResponse(
|
||||
const std::array<uint8_t, 16>& aes_key_bytes,
|
||||
const std::array<uint8_t, 16>& encrypted_response_bytes);
|
||||
|
||||
@@ -39,7 +39,22 @@ cc_library(
|
||||
"@boringssl//:crypto",
|
||||
"@com_google_absl//absl/functional:any_invocable",
|
||||
"@com_google_absl//absl/functional:bind_front",
|
||||
"@com_google_absl//absl/log:check",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
cc_library(
|
||||
name = "test_support",
|
||||
hdrs = [
|
||||
"fake_fast_pair_data_encryptor.h",
|
||||
],
|
||||
visibility = [
|
||||
"//fastpair:__subpackages__",
|
||||
],
|
||||
deps = [
|
||||
":handshake",
|
||||
"//fastpair/common",
|
||||
"@com_google_absl//absl/functional:any_invocable",
|
||||
"@com_google_absl//absl/strings",
|
||||
],
|
||||
)
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// https://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_
|
||||
#define THIRD_PARTY_NEARBY_FASTPAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_
|
||||
|
||||
#include <array>
|
||||
#include <optional>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "fastpair/common/constant.h"
|
||||
#include "fastpair/handshake/fast_pair_data_encryptor.h"
|
||||
|
||||
namespace nearby {
|
||||
namespace fastpair {
|
||||
|
||||
class FakeFastPairDataEncryptor : public FastPairDataEncryptor {
|
||||
public:
|
||||
FakeFastPairDataEncryptor() = default;
|
||||
|
||||
void SetPublickey(std::optional<std::array<uint8_t, 64>> public_key) {
|
||||
public_key_ = std::move(public_key);
|
||||
}
|
||||
|
||||
void SetEncryptedBytes(
|
||||
std::array<uint8_t, kAesBlockByteSize> encrypted_bytes) {
|
||||
encrypted_bytes_ = std::move(encrypted_bytes);
|
||||
}
|
||||
|
||||
void SetResponse(std::optional<DecryptedResponse> response) {
|
||||
response_ = std::move(response);
|
||||
}
|
||||
|
||||
void SetPasskey(std::optional<DecryptedPasskey> passkey) {
|
||||
passkey_ = std::move(passkey);
|
||||
}
|
||||
|
||||
// FastPairDataEncryptor:
|
||||
std::array<uint8_t, kAesBlockByteSize> EncryptBytes(
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt)
|
||||
const override {
|
||||
return encrypted_bytes_;
|
||||
}
|
||||
|
||||
std::optional<std::array<uint8_t, 64>> GetPublicKey() const override {
|
||||
return public_key_;
|
||||
}
|
||||
|
||||
void ParseDecryptResponse(
|
||||
const std::vector<uint8_t>& encrypted_response_bytes,
|
||||
absl::AnyInvocable<void(const std::optional<DecryptedResponse>)> callback)
|
||||
override {
|
||||
callback(response_);
|
||||
}
|
||||
|
||||
void ParseDecryptPasskey(
|
||||
const std::vector<uint8_t>& encrypted_passkey_bytes,
|
||||
absl::AnyInvocable<void(const std::optional<DecryptedPasskey>)> callback)
|
||||
override {
|
||||
callback(passkey_);
|
||||
}
|
||||
|
||||
private:
|
||||
std::optional<std::array<uint8_t, 64>> public_key_;
|
||||
std::array<uint8_t, kAesBlockByteSize> encrypted_bytes_;
|
||||
std::optional<DecryptedResponse> response_;
|
||||
std::optional<DecryptedPasskey> passkey_;
|
||||
};
|
||||
|
||||
} // namespace fastpair
|
||||
} // namespace nearby
|
||||
|
||||
#endif // THIRD_PARTY_NEARBY_FASTPAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_
|
||||
@@ -35,12 +35,14 @@ namespace fastpair {
|
||||
// response and decrypt passkey.
|
||||
class FastPairDataEncryptor {
|
||||
public:
|
||||
virtual ~FastPairDataEncryptor() = default;
|
||||
|
||||
// Encrypts bytes with the stored secret key.
|
||||
virtual std::array<uint8_t, kAesBlockByteSize> EncryptBytes(
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt) = 0;
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt) const = 0;
|
||||
|
||||
// Return stored PublicKey
|
||||
virtual std::optional<std::array<uint8_t, 64>>& GetPublicKey() = 0;
|
||||
virtual std::optional<std::array<uint8_t, 64>> GetPublicKey() const = 0;
|
||||
|
||||
// Decrypt and parse decrypted response bytes with the stored secret key.
|
||||
virtual void ParseDecryptResponse(
|
||||
@@ -53,8 +55,6 @@ class FastPairDataEncryptor {
|
||||
const std::vector<uint8_t>& encrypted_passkey_bytes,
|
||||
absl::AnyInvocable<void(const std::optional<DecryptedPasskey>)>
|
||||
callback) = 0;
|
||||
|
||||
virtual ~FastPairDataEncryptor() = default;
|
||||
};
|
||||
|
||||
} // namespace fastpair
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2022 Google LLC
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -87,15 +87,13 @@ void FastPairDataEncryptorImpl::Factory::CreateAsyncWithKeyExchange(
|
||||
// to generate the new secret key pair.
|
||||
NEARBY_LOGS(INFO) << __func__ << ": Attempting to get device metadata.";
|
||||
FastPairRepository::Get()->GetDeviceMetadata(
|
||||
device.model_id,
|
||||
[&device, &on_get_instance_callback](DeviceMetadata& metadata) {
|
||||
device.model_id, [&on_get_instance_callback](DeviceMetadata& metadata) {
|
||||
FastPairDataEncryptorImpl::Factory::DeviceMetadataRetrieved(
|
||||
device, std::move(on_get_instance_callback), metadata);
|
||||
std::move(on_get_instance_callback), metadata);
|
||||
});
|
||||
}
|
||||
|
||||
void FastPairDataEncryptorImpl::Factory::DeviceMetadataRetrieved(
|
||||
const FastPairDevice& device,
|
||||
absl::AnyInvocable<void(std::unique_ptr<FastPairDataEncryptor>)>
|
||||
on_get_instance_callback,
|
||||
DeviceMetadata& device_metadata) {
|
||||
@@ -126,12 +124,12 @@ FastPairDataEncryptorImpl::FastPairDataEncryptorImpl(
|
||||
FastPairDataEncryptorImpl::~FastPairDataEncryptorImpl() = default;
|
||||
|
||||
std::array<uint8_t, kAesBlockByteSize> FastPairDataEncryptorImpl::EncryptBytes(
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt) {
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt) const {
|
||||
return FastPairEncryption::EncryptBytes(shared_secret_key_, bytes_to_encrypt);
|
||||
}
|
||||
|
||||
std::optional<std::array<uint8_t, kPublicKeyByteSize>>&
|
||||
FastPairDataEncryptorImpl::GetPublicKey() {
|
||||
std::optional<std::array<uint8_t, kPublicKeyByteSize>>
|
||||
FastPairDataEncryptorImpl::GetPublicKey() const {
|
||||
return public_key_;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// Copyright 2022 Google LLC
|
||||
// Copyright 2023 Google LLC
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -15,7 +15,6 @@
|
||||
#ifndef THIRD_PARTY_NEARBY_FASTPAIR_HANDSHAKE_FAST_PAIR_DATA_ENCRYPTOR_IMPL_H_
|
||||
#define THIRD_PARTY_NEARBY_FASTPAIR_HANDSHAKE_FAST_PAIR_DATA_ENCRYPTOR_IMPL_H_
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <array>
|
||||
@@ -62,17 +61,17 @@ class FastPairDataEncryptorImpl : public FastPairDataEncryptor {
|
||||
on_get_instance_callback);
|
||||
|
||||
static void DeviceMetadataRetrieved(
|
||||
const FastPairDevice& device,
|
||||
absl::AnyInvocable<void(std::unique_ptr<FastPairDataEncryptor>)>
|
||||
on_get_instance_callback,
|
||||
DeviceMetadata& device_metadata);
|
||||
};
|
||||
|
||||
std::array<uint8_t, kAesBlockByteSize> EncryptBytes(
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt) override;
|
||||
const std::array<uint8_t, kAesBlockByteSize>& bytes_to_encrypt)
|
||||
const override;
|
||||
|
||||
std::optional<std::array<uint8_t, kPublicKeyByteSize>>& GetPublicKey()
|
||||
override;
|
||||
std::optional<std::array<uint8_t, kPublicKeyByteSize>> GetPublicKey()
|
||||
const override;
|
||||
|
||||
void ParseDecryptResponse(
|
||||
const std::vector<uint8_t>& encrypted_response_bytes,
|
||||
@@ -94,7 +93,7 @@ class FastPairDataEncryptorImpl : public FastPairDataEncryptor {
|
||||
|
||||
// The public key is only required during initial pairing and optional during
|
||||
// communication with paired devices.
|
||||
std::optional<std::array<uint8_t, kPublicKeyByteSize>> public_key_ =
|
||||
const std::optional<std::array<uint8_t, kPublicKeyByteSize>> public_key_ =
|
||||
std::nullopt;
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user