fast_pair_encryption_unittest.cc 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/quick_pair/fast_pair_handshake/fast_pair_encryption.h"
  5. #include <algorithm>
  6. #include <array>
  7. #include <iterator>
  8. #include "base/base64.h"
  9. #include "testing/gtest/include/gtest/gtest.h"
  10. namespace ash {
  11. namespace quick_pair {
  12. namespace fast_pair_encryption {
  13. std::array<uint8_t, kBlockByteSize> aes_key_bytes = {
  14. 0xA0, 0xBA, 0xF0, 0xBB, 0x95, 0x1F, 0xF7, 0xB6,
  15. 0xCF, 0x5E, 0x3F, 0x45, 0x61, 0xC3, 0x32, 0x1D};
  16. std::string DecodeKey(const std::string& encoded_key) {
  17. std::string key;
  18. base::Base64Decode(encoded_key, &key);
  19. return key;
  20. }
  21. class FastPairEncryptionTest : public testing::Test {};
  22. TEST_F(FastPairEncryptionTest, EncryptBytes_Success) {
  23. std::array<uint8_t, kBlockByteSize> input = {
  24. 0xF3, 0x0F, 0x4E, 0x78, 0x6C, 0x59, 0xA7, 0xBB,
  25. 0xF3, 0x87, 0x3B, 0x5A, 0x49, 0xBA, 0x97, 0xEA};
  26. std::array<uint8_t, kBlockByteSize> expected = {
  27. 0xAC, 0x9A, 0x16, 0xF0, 0x95, 0x3A, 0x3F, 0x22,
  28. 0x3D, 0xD1, 0x0C, 0xF5, 0x36, 0xE0, 0x9E, 0x9C};
  29. EXPECT_EQ(EncryptBytes(aes_key_bytes, input), expected);
  30. }
  31. TEST_F(FastPairEncryptionTest, EncryptBytes_Failure) {
  32. std::array<uint8_t, kBlockByteSize> input = {
  33. 0xF3, 0x0F, 0x4E, 0x78, 0x6C, 0x59, 0xA7, 0xBA,
  34. 0xF3, 0x87, 0x3B, 0x5A, 0x49, 0xBA, 0x97, 0xEA};
  35. std::array<uint8_t, kBlockByteSize> expected = {
  36. 0xAC, 0x9A, 0x16, 0xF0, 0x95, 0x3A, 0x3F, 0x22,
  37. 0x3D, 0xD1, 0x0C, 0xF5, 0x36, 0xE0, 0x9E, 0x9C};
  38. EXPECT_NE(EncryptBytes(aes_key_bytes, input), expected);
  39. }
  40. TEST_F(FastPairEncryptionTest, GenerateKeysWithEcdhKeyAgreement_EmptyKey) {
  41. EXPECT_FALSE(GenerateKeysWithEcdhKeyAgreement("").has_value());
  42. }
  43. TEST_F(FastPairEncryptionTest, GenerateKeysWithEcdhKeyAgreement_ShortKey) {
  44. EXPECT_FALSE(GenerateKeysWithEcdhKeyAgreement("too_short").has_value());
  45. }
  46. TEST_F(FastPairEncryptionTest, GenerateKeysWithEcdhKeyAgreement_InvalidKey) {
  47. EXPECT_FALSE(
  48. GenerateKeysWithEcdhKeyAgreement(
  49. DecodeKey("U2PWc3FHTxah/o0YT9n1VRvtm57SNIRSXOEBXm4fdtMo+06tNoFlt8D0/"
  50. "2BsN8auolz5ikwLRvQh+MiQ6oYveg=="))
  51. .has_value());
  52. }
  53. TEST_F(FastPairEncryptionTest, GenerateKeysWithEcdhKeyAgreement_ValidKey) {
  54. EXPECT_TRUE(
  55. GenerateKeysWithEcdhKeyAgreement(
  56. DecodeKey("U2PWc3FHTxah/o0YU9n1VRvtm57SNIRSXOEBXm4fdtMo+06tNoFlt8D0/"
  57. "2BsN8auolz5ikwLRvQh+MiQ6oYveg=="))
  58. .has_value());
  59. }
  60. } // namespace fast_pair_encryption
  61. } // namespace quick_pair
  62. } // namespace ash