Files
android/libwvdrmengine/oemcrypto/test/fuzz_tests/oemcrypto_generic_encrypt_fuzz.cc
Ian Benz 53fe55cb72 Clean up fuzz helper classes
- Remove OEMCrypto state changes hidden in constructors and destructors.
- Use composition instead of inheritance to structure classes.
- Avoid calling non-trivial destructors for objects with static
  lifetime.

Merged from https://widevine-internal-review.googlesource.com/168497
Merged from https://widevine-internal-review.googlesource.com/171170
Merged from https://widevine-internal-review.googlesource.com/171171
Merged from https://widevine-internal-review.googlesource.com/171870

Change-Id: I20476a7b1132d11f011b8650ec01e3c2dc3fc0e8
2024-01-26 13:40:22 -08:00

66 lines
2.4 KiB
C++

// Copyright 2020 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine
// License Agreement.
#include <vector>
#include "FuzzedDataProvider.h"
#include "OEMCryptoCENC.h"
#include "oemcrypto_fuzz_helper.h"
#include "oemcrypto_fuzz_structs.h"
namespace {
// Avoid calling non-trivial destructor.
wvoec::OEMCryptoLicenseAPIFuzz& license_api_fuzz =
*new wvoec::OEMCryptoLicenseAPIFuzz;
} // namespace
extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv) {
wvoec::RedirectStdoutToFile();
license_api_fuzz.Initialize();
license_api_fuzz.LoadLicense();
return 0;
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
// Split data using separator.
const std::vector<wvoec::FuzzedData> inputs =
wvoec::SplitFuzzedData(data, size);
if (inputs.size() < 2) {
return 0;
}
wvoec::OEMCrypto_Generic_Api_Fuzz fuzzed_structure;
if (inputs[0].size < sizeof(fuzzed_structure)) {
return 0;
}
// Copy OEMCrypto_Generic_Api_Fuzz from input data.
FuzzedDataProvider fuzzed_data(inputs[0].data, inputs[0].size);
fuzzed_data.ConsumeData(&fuzzed_structure, sizeof(fuzzed_structure));
wvoec::ConvertDataToValidEnum(OEMCrypto_CipherMode_MaxValue,
&fuzzed_structure.cipher_mode);
wvoec::ConvertDataToValidEnum(OEMCrypto_Algorithm_MaxValue,
&fuzzed_structure.algorithm);
// Copy iv from input data.
const std::vector<uint8_t> iv = fuzzed_data.ConsumeRemainingBytes<uint8_t>();
// Initialize clear and encrypted buffers.
const std::vector<uint8_t> clear_buffer(inputs[1].data,
inputs[1].data + inputs[1].size);
std::vector<uint8_t> encrypted_buffer(clear_buffer.size());
wvoec::Session& session = license_api_fuzz.session();
std::vector<uint8_t> key_handle;
wvoec::GetKeyHandleIntoVector(session.session_id(),
session.license().keys[0].key_id,
session.license().keys[0].key_id_length,
fuzzed_structure.cipher_mode, key_handle);
OEMCrypto_Generic_Encrypt(key_handle.data(), key_handle.size(),
clear_buffer.data(), clear_buffer.size(), iv.data(),
fuzzed_structure.algorithm,
encrypted_buffer.data());
return 0;
}