fast_pair_decryption_fuzzer.cc 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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 <fuzzer/FuzzedDataProvider.h>
  6. #include <stddef.h>
  7. #include <stdint.h>
  8. #include <array>
  9. #include "ash/quick_pair/common/logging.h"
  10. #include "ash/quick_pair/fast_pair_handshake/fast_pair_encryption.h"
  11. #include "base/no_destructor.h"
  12. namespace {
  13. struct Environment {
  14. Environment() {
  15. // Disable noisy logging for fuzzing.
  16. logging::SetMinLogLevel(logging::LOGGING_FATAL);
  17. }
  18. ash::quick_pair::ScopedDisableLoggingForTesting disable_logging_;
  19. };
  20. } // namespace
  21. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  22. // Enforce an exact input length so that we can AES encrypt the fuzzed input.
  23. // Prioritizes code exploration over testing every possible input.
  24. if (size != 2 * ash::quick_pair::fast_pair_decryption::kBlockByteSize)
  25. return 0;
  26. static base::NoDestructor<Environment> env;
  27. FuzzedDataProvider fuzzed_data(data, size);
  28. std::vector<uint8_t> aes_key_bytes = fuzzed_data.ConsumeBytes<uint8_t>(
  29. ash::quick_pair::fast_pair_decryption::kBlockByteSize);
  30. std::array<uint8_t, ash::quick_pair::fast_pair_decryption::kBlockByteSize>
  31. aes_key_arr;
  32. std::copy_n(aes_key_bytes.begin(),
  33. ash::quick_pair::fast_pair_decryption::kBlockByteSize,
  34. aes_key_arr.begin());
  35. std::vector<uint8_t> data_bytes = fuzzed_data.ConsumeBytes<uint8_t>(
  36. ash::quick_pair::fast_pair_decryption::kBlockByteSize);
  37. std::array<uint8_t, ash::quick_pair::fast_pair_decryption::kBlockByteSize>
  38. data_arr;
  39. std::copy_n(data_bytes.begin(),
  40. ash::quick_pair::fast_pair_decryption::kBlockByteSize,
  41. data_arr.begin());
  42. auto encrypted_arr = ash::quick_pair::fast_pair_encryption::EncryptBytes(
  43. aes_key_arr, data_arr);
  44. ash::quick_pair::fast_pair_decryption::ParseDecryptedResponse(aes_key_arr,
  45. encrypted_arr);
  46. ash::quick_pair::fast_pair_decryption::ParseDecryptedPasskey(aes_key_arr,
  47. encrypted_arr);
  48. return 0;
  49. }