fast_pair_data_encryptor_impl.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_DATA_ENCRYPTOR_IMPL_H_
  5. #define ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_DATA_ENCRYPTOR_IMPL_H_
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <array>
  9. #include "ash/quick_pair/common/device.h"
  10. #include "ash/quick_pair/fast_pair_handshake/fast_pair_data_encryptor.h"
  11. #include "ash/quick_pair/fast_pair_handshake/fast_pair_key_pair.h"
  12. #include "ash/services/quick_pair/quick_pair_process_manager.h"
  13. #include "base/memory/scoped_refptr.h"
  14. #include "base/memory/weak_ptr.h"
  15. namespace ash {
  16. namespace quick_pair {
  17. class DeviceMetadata;
  18. // Holds a secret key for a device and has methods to encrypt bytes, decrypt
  19. // response and decrypt passkey.
  20. class FastPairDataEncryptorImpl : public FastPairDataEncryptor {
  21. public:
  22. class Factory {
  23. public:
  24. static void CreateAsync(
  25. scoped_refptr<Device> device,
  26. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  27. on_get_instance_callback);
  28. static void SetFactoryForTesting(Factory* test_factory);
  29. protected:
  30. virtual ~Factory();
  31. virtual void CreateInstance(
  32. scoped_refptr<Device> device,
  33. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  34. on_get_instance_callback) = 0;
  35. private:
  36. static void CreateAsyncWithKeyExchange(
  37. scoped_refptr<Device> device,
  38. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  39. on_get_instance_callback);
  40. static void CreateAsyncWithAccountKey(
  41. scoped_refptr<Device> device,
  42. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  43. on_get_instance_callback);
  44. static void DeviceMetadataRetrieved(
  45. scoped_refptr<Device> device,
  46. base::OnceCallback<void(std::unique_ptr<FastPairDataEncryptor>)>
  47. on_get_instance_callback,
  48. DeviceMetadata* device_metadata,
  49. bool has_retryable_error);
  50. };
  51. const std::array<uint8_t, kBlockSizeBytes> EncryptBytes(
  52. const std::array<uint8_t, kBlockSizeBytes>& bytes_to_encrypt) override;
  53. const absl::optional<std::array<uint8_t, kPublicKeyByteSize>>& GetPublicKey()
  54. override;
  55. void ParseDecryptedResponse(
  56. const std::vector<uint8_t>& encrypted_response_bytes,
  57. base::OnceCallback<void(const absl::optional<DecryptedResponse>&)>
  58. callback) override;
  59. void ParseDecryptedPasskey(
  60. const std::vector<uint8_t>& encrypted_passkey_bytes,
  61. base::OnceCallback<void(const absl::optional<DecryptedPasskey>&)>
  62. callback) override;
  63. ~FastPairDataEncryptorImpl() override;
  64. protected:
  65. explicit FastPairDataEncryptorImpl(
  66. const fast_pair_encryption::KeyPair& key_pair);
  67. explicit FastPairDataEncryptorImpl(
  68. const std::array<uint8_t, kPrivateKeyByteSize>& secret_key);
  69. FastPairDataEncryptorImpl(const FastPairDataEncryptorImpl&) = delete;
  70. FastPairDataEncryptorImpl& operator=(const FastPairDataEncryptorImpl&) =
  71. delete;
  72. private:
  73. void QuickPairProcessStoppedOnResponse(
  74. QuickPairProcessManager::ShutdownReason shutdown_reason);
  75. void QuickPairProcessStoppedOnPasskey(
  76. QuickPairProcessManager::ShutdownReason shutdown_reason);
  77. std::array<uint8_t, kPrivateKeyByteSize> secret_key_;
  78. // The public key is only required during initial pairing and optional during
  79. // communication with paired devices.
  80. absl::optional<std::array<uint8_t, kPublicKeyByteSize>> public_key_ =
  81. absl::nullopt;
  82. base::WeakPtrFactory<FastPairDataEncryptorImpl> weak_ptr_factory_{this};
  83. };
  84. } // namespace quick_pair
  85. } // namespace ash
  86. #endif // ASH_QUICK_PAIR_FAST_PAIR_HANDSHAKE_FAST_PAIR_DATA_ENCRYPTOR_IMPL_H_