diff --git a/fastpair/crypto/fast_pair_decryption.cc b/fastpair/crypto/fast_pair_decryption.cc index c591332d..f16a3df9 100644 --- a/fastpair/crypto/fast_pair_decryption.cc +++ b/fastpair/crypto/fast_pair_decryption.cc @@ -33,8 +33,8 @@ namespace nearby { namespace fastpair { - -std::array FastPairDecryption::DecryptBytes( +namespace { +std::array DecryptBytes( const std::array& aes_key_bytes, const std::array& encrypted_bytes) { AES_KEY aes_key; @@ -48,6 +48,7 @@ std::array 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) diff --git a/fastpair/crypto/fast_pair_decryption.h b/fastpair/crypto/fast_pair_decryption.h index a7a4a7b3..b6cabe4b 100644 --- a/fastpair/crypto/fast_pair_decryption.h +++ b/fastpair/crypto/fast_pair_decryption.h @@ -29,10 +29,6 @@ class FastPairDecryption { public: static constexpr int kAesBlockByteSize = 16; - static std::array DecryptBytes( - const std::array& aes_key_bytes, - const std::array& encrypted_bytes); - static std::optional ParseDecryptResponse( const std::array& aes_key_bytes, const std::array& encrypted_response_bytes); diff --git a/fastpair/handshake/BUILD b/fastpair/handshake/BUILD index 86e6e22c..f964f3b4 100644 --- a/fastpair/handshake/BUILD +++ b/fastpair/handshake/BUILD @@ -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", ], ) diff --git a/fastpair/handshake/fake_fast_pair_data_encryptor.h b/fastpair/handshake/fake_fast_pair_data_encryptor.h new file mode 100644 index 00000000..bdedc954 --- /dev/null +++ b/fastpair/handshake/fake_fast_pair_data_encryptor.h @@ -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 +#include +#include +#include + +#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> public_key) { + public_key_ = std::move(public_key); + } + + void SetEncryptedBytes( + std::array encrypted_bytes) { + encrypted_bytes_ = std::move(encrypted_bytes); + } + + void SetResponse(std::optional response) { + response_ = std::move(response); + } + + void SetPasskey(std::optional passkey) { + passkey_ = std::move(passkey); + } + + // FastPairDataEncryptor: + std::array EncryptBytes( + const std::array& bytes_to_encrypt) + const override { + return encrypted_bytes_; + } + + std::optional> GetPublicKey() const override { + return public_key_; + } + + void ParseDecryptResponse( + const std::vector& encrypted_response_bytes, + absl::AnyInvocable)> callback) + override { + callback(response_); + } + + void ParseDecryptPasskey( + const std::vector& encrypted_passkey_bytes, + absl::AnyInvocable)> callback) + override { + callback(passkey_); + } + + private: + std::optional> public_key_; + std::array encrypted_bytes_; + std::optional response_; + std::optional passkey_; +}; + +} // namespace fastpair +} // namespace nearby + +#endif // THIRD_PARTY_NEARBY_FASTPAIR_HANDSHAKE_FAKE_FAST_PAIR_DATA_ENCRYPTOR_H_ diff --git a/fastpair/handshake/fast_pair_data_encryptor.h b/fastpair/handshake/fast_pair_data_encryptor.h index 6f968622..4287066c 100644 --- a/fastpair/handshake/fast_pair_data_encryptor.h +++ b/fastpair/handshake/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 EncryptBytes( - const std::array& bytes_to_encrypt) = 0; + const std::array& bytes_to_encrypt) const = 0; // Return stored PublicKey - virtual std::optional>& GetPublicKey() = 0; + virtual std::optional> 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& encrypted_passkey_bytes, absl::AnyInvocable)> callback) = 0; - - virtual ~FastPairDataEncryptor() = default; }; } // namespace fastpair diff --git a/fastpair/handshake/fast_pair_data_encryptor_impl.cc b/fastpair/handshake/fast_pair_data_encryptor_impl.cc index 3bdf001b..8975bc35 100644 --- a/fastpair/handshake/fast_pair_data_encryptor_impl.cc +++ b/fastpair/handshake/fast_pair_data_encryptor_impl.cc @@ -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)> on_get_instance_callback, DeviceMetadata& device_metadata) { @@ -126,12 +124,12 @@ FastPairDataEncryptorImpl::FastPairDataEncryptorImpl( FastPairDataEncryptorImpl::~FastPairDataEncryptorImpl() = default; std::array FastPairDataEncryptorImpl::EncryptBytes( - const std::array& bytes_to_encrypt) { + const std::array& bytes_to_encrypt) const { return FastPairEncryption::EncryptBytes(shared_secret_key_, bytes_to_encrypt); } -std::optional>& -FastPairDataEncryptorImpl::GetPublicKey() { +std::optional> +FastPairDataEncryptorImpl::GetPublicKey() const { return public_key_; } diff --git a/fastpair/handshake/fast_pair_data_encryptor_impl.h b/fastpair/handshake/fast_pair_data_encryptor_impl.h index ebdda92f..de567900 100644 --- a/fastpair/handshake/fast_pair_data_encryptor_impl.h +++ b/fastpair/handshake/fast_pair_data_encryptor_impl.h @@ -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 #include #include @@ -62,17 +61,17 @@ class FastPairDataEncryptorImpl : public FastPairDataEncryptor { on_get_instance_callback); static void DeviceMetadataRetrieved( - const FastPairDevice& device, absl::AnyInvocable)> on_get_instance_callback, DeviceMetadata& device_metadata); }; std::array EncryptBytes( - const std::array& bytes_to_encrypt) override; + const std::array& bytes_to_encrypt) + const override; - std::optional>& GetPublicKey() - override; + std::optional> GetPublicKey() + const override; void ParseDecryptResponse( const std::vector& 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> public_key_ = + const std::optional> public_key_ = std::nullopt; };