fast_pair_decryption_unittest.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #include "ash/services/quick_pair/fast_pair_decryption.h"
  5. #include <array>
  6. #include "ash/quick_pair/fast_pair_handshake/fast_pair_encryption.h"
  7. #include "ash/services/quick_pair/public/cpp/fast_pair_message_type.h"
  8. #include "testing/gtest/include/gtest/gtest.h"
  9. namespace ash {
  10. namespace quick_pair {
  11. namespace fast_pair_decryption {
  12. std::array<uint8_t, kBlockByteSize> aes_key_bytes = {
  13. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6,
  14. 0xCF, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D};
  15. class FastPairDecryptionTest : public testing::Test {};
  16. TEST_F(FastPairDecryptionTest, ParseDecryptedResponse_Success) {
  17. std::vector<uint8_t> response_bytes;
  18. // Message type.
  19. response_bytes.push_back(0x01);
  20. // Address bytes.
  21. std::array<uint8_t, 6> address_bytes = {0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
  22. std::copy(address_bytes.begin(), address_bytes.end(),
  23. std::back_inserter(response_bytes));
  24. // Random salt
  25. std::array<uint8_t, 9> salt = {0x08, 0x09, 0x0A, 0x0B, 0x0C,
  26. 0x0D, 0x0E, 0x0F, 0x00};
  27. std::copy(salt.begin(), salt.end(), std::back_inserter(response_bytes));
  28. std::array<uint8_t, kBlockByteSize> response_bytes_array;
  29. std::copy_n(response_bytes.begin(), kBlockByteSize,
  30. response_bytes_array.begin());
  31. auto encrypted_bytes =
  32. fast_pair_encryption::EncryptBytes(aes_key_bytes, response_bytes_array);
  33. auto response = ParseDecryptedResponse(aes_key_bytes, encrypted_bytes);
  34. EXPECT_TRUE(response.has_value());
  35. EXPECT_EQ(response->message_type,
  36. FastPairMessageType::kKeyBasedPairingResponse);
  37. EXPECT_EQ(response->address_bytes, address_bytes);
  38. EXPECT_EQ(response->salt, salt);
  39. }
  40. TEST_F(FastPairDecryptionTest, ParseDecryptedResponse_Failure) {
  41. std::array<uint8_t, kBlockByteSize> response_bytes = {/*message_type=*/0x02,
  42. /*address_bytes=*/0x02,
  43. 0x03,
  44. 0x04,
  45. 0x05,
  46. 0x06,
  47. 0x07,
  48. /*salt=*/0x08,
  49. 0x09,
  50. 0x0A,
  51. 0x0B,
  52. 0x0C,
  53. 0x0D,
  54. 0x0E,
  55. 0x0F,
  56. 0x00};
  57. auto encrypted_bytes =
  58. fast_pair_encryption::EncryptBytes(aes_key_bytes, response_bytes);
  59. auto response = ParseDecryptedResponse(aes_key_bytes, encrypted_bytes);
  60. EXPECT_FALSE(response.has_value());
  61. }
  62. TEST_F(FastPairDecryptionTest, ParseDecryptedPasskey_Success) {
  63. std::vector<uint8_t> passkey_bytes;
  64. // Message type.
  65. passkey_bytes.push_back(0x02);
  66. // Passkey bytes.
  67. uint32_t passkey = 5;
  68. passkey_bytes.push_back(passkey >> 16);
  69. passkey_bytes.push_back(passkey >> 8);
  70. passkey_bytes.push_back(passkey);
  71. // Random salt
  72. std::array<uint8_t, 12> salt = {0x08, 0x09, 0x0A, 0x08, 0x09, 0x0E,
  73. 0x0A, 0x0C, 0x0D, 0x0E, 0x05, 0x02};
  74. std::copy(salt.begin(), salt.end(), std::back_inserter(passkey_bytes));
  75. std::array<uint8_t, kBlockByteSize> passkey_bytes_array;
  76. std::copy_n(passkey_bytes.begin(), kBlockByteSize,
  77. passkey_bytes_array.begin());
  78. auto encrypted_bytes =
  79. fast_pair_encryption::EncryptBytes(aes_key_bytes, passkey_bytes_array);
  80. auto decrypted_passkey =
  81. ParseDecryptedPasskey(aes_key_bytes, encrypted_bytes);
  82. EXPECT_TRUE(decrypted_passkey.has_value());
  83. EXPECT_EQ(decrypted_passkey->message_type,
  84. FastPairMessageType::kSeekersPasskey);
  85. EXPECT_EQ(decrypted_passkey->passkey, passkey);
  86. EXPECT_EQ(decrypted_passkey->salt, salt);
  87. }
  88. TEST_F(FastPairDecryptionTest, ParseDecryptedPasskey_Failure) {
  89. std::array<uint8_t, kBlockByteSize> passkey_bytes = {/*message_type=*/0x04,
  90. /*passkey=*/0x02,
  91. 0x03,
  92. 0x04,
  93. /*salt=*/0x05,
  94. 0x06,
  95. 0x07,
  96. 0x08,
  97. 0x09,
  98. 0x0A,
  99. 0x0B,
  100. 0x0C,
  101. 0x0D,
  102. 0x0E,
  103. 0x0F,
  104. 0x0E};
  105. auto encrypted_bytes =
  106. fast_pair_encryption::EncryptBytes(aes_key_bytes, passkey_bytes);
  107. auto passkey = ParseDecryptedPasskey(aes_key_bytes, encrypted_bytes);
  108. EXPECT_FALSE(passkey.has_value());
  109. }
  110. } // namespace fast_pair_decryption
  111. } // namespace quick_pair
  112. } // namespace ash