From 982bec196b655299c206bd2979825bef32795a2d Mon Sep 17 00:00:00 2001 From: Onkar Shinde Date: Tue, 12 Dec 2023 08:34:01 +0000 Subject: [PATCH] Updated crypto_session_fuzzer Implemented google c++ code style changes for crypto_session_fuzzer exec/s: 136 Test: ./crypto_session_fuzzer Bug: 312374669 Change-Id: Ie490914858a35dfe0f8bfdd4a40f9be65d41b6bd --- fuzzer/crypto_session_fuzzer.cpp | 873 ++++++++++++++++--------------- 1 file changed, 437 insertions(+), 436 deletions(-) diff --git a/fuzzer/crypto_session_fuzzer.cpp b/fuzzer/crypto_session_fuzzer.cpp index 2f049c4f..f5a29129 100644 --- a/fuzzer/crypto_session_fuzzer.cpp +++ b/fuzzer/crypto_session_fuzzer.cpp @@ -15,12 +15,13 @@ * */ +#include + #include "crypto_session.h" #include "entitlement_key_session.h" #include "properties.h" #include "string_conversions.h" #include "wv_cdm_constants.h" -#include using namespace wvcdm; @@ -33,470 +34,470 @@ constexpr int32_t kIvSize = 16; constexpr int32_t kDefaultSampleSize = 0; class CryptoSessionFuzzer { -public: - CryptoSessionFuzzer(const uint8_t* data, size_t size) : mFdp(data, size) {}; - void process(); - void FillDecryptParams(CdmDecryptionParametersV16& params); - void SetKeyParams(CryptoKey& inputKey); - void setUp(CryptoSession* cryptoSession, const std::string& keyId, - std::string& message, std::string& signature, - std::string& coreMessage); - void initializeStripeBuffer(std::vector* buffer, size_t size); + public: + CryptoSessionFuzzer(const uint8_t* data, size_t size) : fdp_(data, size){}; + void Process(); + void FillDecryptParams(CdmDecryptionParametersV16& params); + void SetKeyParams(CryptoKey& input_key); + void SetUp(CryptoSession* crypto_session, const std::string& key_id, + std::string& message, std::string& signature, + std::string& core_message); + void InitializeStripeBuffer(std::vector* buffer, size_t size); -private: - FuzzedDataProvider mFdp; + private: + FuzzedDataProvider fdp_; }; -void CryptoSessionFuzzer::initializeStripeBuffer(std::vector* buffer, - size_t size) { - buffer->assign(size, 0); - for (size_t i = 0; i < size; ++i) { - (*buffer)[i] = mFdp.ConsumeIntegral(); - } +void CryptoSessionFuzzer::InitializeStripeBuffer(std::vector* buffer, + size_t size) { + buffer->assign(size, 0); + for (size_t i = 0; i < size; ++i) { + (*buffer)[i] = fdp_.ConsumeIntegral(); + } } -void CryptoSessionFuzzer::SetKeyParams(CryptoKey& inputKey) { - inputKey.set_key_data(mFdp.ConsumeRandomLengthString(kStringLength)); - inputKey.set_key_data_iv(mFdp.ConsumeRandomLengthString(kStringLength)); - inputKey.set_key_control(mFdp.ConsumeRandomLengthString(kStringLength)); - inputKey.set_key_control_iv(mFdp.ConsumeRandomLengthString(kStringLength)); - inputKey.set_cipher_mode(mFdp.ConsumeBool() ? kCipherModeCbc - : kCipherModeCtr); - inputKey.set_track_label(mFdp.ConsumeRandomLengthString(kStringLength)); - inputKey.set_entitlement_key_id( - mFdp.ConsumeRandomLengthString(kStringLength)); +void CryptoSessionFuzzer::SetKeyParams(CryptoKey& input_key) { + input_key.set_key_data(fdp_.ConsumeRandomLengthString(kStringLength)); + input_key.set_key_data_iv(fdp_.ConsumeRandomLengthString(kStringLength)); + input_key.set_key_control(fdp_.ConsumeRandomLengthString(kStringLength)); + input_key.set_key_control_iv(fdp_.ConsumeRandomLengthString(kStringLength)); + input_key.set_cipher_mode(fdp_.ConsumeBool() ? kCipherModeCbc + : kCipherModeCtr); + input_key.set_track_label(fdp_.ConsumeRandomLengthString(kStringLength)); + input_key.set_entitlement_key_id( + fdp_.ConsumeRandomLengthString(kStringLength)); } void CryptoSessionFuzzer::FillDecryptParams( CdmDecryptionParametersV16& params) { - params.cipher_mode = mFdp.ConsumeBool() ? kCipherModeCbc : kCipherModeCtr; - params.observe_legacy_fields = mFdp.ConsumeBool(); - uint32_t noOfSamples = - mFdp.ConsumeIntegralInRange(kMinSamplesCount, kMaxSamplesCount); + params.cipher_mode = fdp_.ConsumeBool() ? kCipherModeCbc : kCipherModeCtr; + params.observe_legacy_fields = fdp_.ConsumeBool(); + uint32_t no_of_samples = + fdp_.ConsumeIntegralInRange(kMinSamplesCount, kMaxSamplesCount); - while (--noOfSamples) { - size_t sampleSize = 0; - uint32_t noOfSubSamples = mFdp.ConsumeIntegralInRange( - kMinSamplesCount, kMaxSamplesCount); - std::vector subSampleVector; - while (--noOfSubSamples) { - size_t clearBytes = - mFdp.ConsumeIntegralInRange(kMinByte, kMaxByte); - size_t protectedBytes = - mFdp.ConsumeIntegralInRange(kMinByte, kMaxByte); - CdmDecryptionSubsample subsample(clearBytes, protectedBytes); - sampleSize += clearBytes + protectedBytes; - subSampleVector.push_back(subsample); - } - std::vector iv; - iv = mFdp.ConsumeBytes(kIvSize); - if (iv.size() != kIvSize) { - iv.resize(kIvSize, 0); - } - void* decryptBuffer; - std::vector eBuffer; - for (int i = 0; i < sampleSize; ++i) { - eBuffer.push_back( - mFdp.ConsumeIntegralInRange(kMinByte, kMaxByte)); - } - size_t decryptBufferOffset = - mFdp.ConsumeIntegralInRange(kMinByte, kMaxByte); - CdmDecryptionSample sample(eBuffer.data(), decryptBuffer, - decryptBufferOffset, sampleSize, iv); - sample.subsamples = subSampleVector; - params.samples.push_back(sample); + while (--no_of_samples) { + size_t sample_size = 0; + uint32_t no_of_sub_samples = fdp_.ConsumeIntegralInRange( + kMinSamplesCount, kMaxSamplesCount); + std::vector sub_sample_vector; + while (--no_of_sub_samples) { + size_t clear_bytes = + fdp_.ConsumeIntegralInRange(kMinByte, kMaxByte); + size_t protected_bytes = + fdp_.ConsumeIntegralInRange(kMinByte, kMaxByte); + CdmDecryptionSubsample sub_sample(clear_bytes, protected_bytes); + sample_size += clear_bytes + protected_bytes; + sub_sample_vector.push_back(sub_sample); } + std::vector iv; + iv = fdp_.ConsumeBytes(kIvSize); + if (iv.size() != kIvSize) { + iv.resize(kIvSize, 0); + } + void* decrypt_buffer; + std::vector e_buffer; + for (int i = 0; i < sample_size; ++i) { + e_buffer.push_back( + fdp_.ConsumeIntegralInRange(kMinByte, kMaxByte)); + } + size_t decrypt_buffer_offset = + fdp_.ConsumeIntegralInRange(kMinByte, kMaxByte); + CdmDecryptionSample sample(e_buffer.data(), decrypt_buffer, + decrypt_buffer_offset, sample_size, iv); + sample.subsamples = sub_sample_vector; + params.samples.push_back(sample); + } } -void CryptoSessionFuzzer::setUp(CryptoSession* cryptoSession, - const std::string& keyId, std::string& message, - std::string& signature, - std::string& coreMessage) { - bool shouldSpecifyAlgorithm; - OEMCrypto_SignatureHashAlgorithm algorithm; - CdmLicenseKeyType keyType1 = kLicenseKeyTypeEntitlement; - cryptoSession->PrepareAndSignProvisioningRequest( - message, &coreMessage, &signature, shouldSpecifyAlgorithm, algorithm); - cryptoSession->PrepareAndSignLicenseRequest( - message, &coreMessage, &signature, shouldSpecifyAlgorithm, algorithm); - cryptoSession->LoadLicense(message, coreMessage, signature, keyType1); +void CryptoSessionFuzzer::SetUp(CryptoSession* crypto_session, + const std::string& key_id, std::string& message, + std::string& signature, + std::string& core_message) { + bool should_specify_algorithm; + OEMCrypto_SignatureHashAlgorithm algorithm; + CdmLicenseKeyType key_type_1 = kLicenseKeyTypeEntitlement; + crypto_session->PrepareAndSignProvisioningRequest( + message, &core_message, &signature, should_specify_algorithm, algorithm); + crypto_session->PrepareAndSignLicenseRequest( + message, &core_message, &signature, should_specify_algorithm, algorithm); + crypto_session->LoadLicense(message, core_message, signature, key_type_1); - CryptoKey inputKey; - inputKey.set_key_id(keyId); - CryptoSessionFuzzer::SetKeyParams(inputKey); - std::vector keys = { inputKey }; - cryptoSession->LoadEntitledContentKeys(keys); + CryptoKey input_key; + input_key.set_key_id(key_id); + CryptoSessionFuzzer::SetKeyParams(input_key); + std::vector keys = {input_key}; + crypto_session->LoadEntitledContentKeys(keys); } -void CryptoSessionFuzzer::process() { - metrics::CryptoMetrics cryptoMetrics; - std::unique_ptr cryptoSession( - CryptoSession::MakeCryptoSession(&cryptoMetrics)); +void CryptoSessionFuzzer::Process() { + metrics::CryptoMetrics crypto_metrics; + std::unique_ptr crypto_session( + CryptoSession::MakeCryptoSession(&crypto_metrics)); - CdmLicenseKeyType keyType; - OEMCrypto_SignatureHashAlgorithm algorithm; - Properties::Init(); + CdmLicenseKeyType key_type; + OEMCrypto_SignatureHashAlgorithm algorithm; + Properties::Init(); - RequestedSecurityLevel requestedSecurityLevel = - mFdp.ConsumeBool() ? kLevelDefault : kLevel3; - cryptoSession->Open(requestedSecurityLevel); + RequestedSecurityLevel requested_security_level = + fdp_.ConsumeBool() ? kLevelDefault : kLevel3; + crypto_session->Open(requested_security_level); - while (mFdp.remaining_bytes()) { - auto invokeCryptoSessionAPI = mFdp.PickValueInArray< - const std::function>({ - [&]() { - std::string token = mFdp.ConsumeRandomLengthString(kStringLength); - std::string additionalToken = - mFdp.ConsumeRandomLengthString(kStringLength); - cryptoSession->GetProvisioningToken( - mFdp.ConsumeBool() ? &token : nullptr, - mFdp.ConsumeBool() ? &additionalToken : nullptr); - }, - [&]() { - cryptoSession->SetSystemId( - mFdp.ConsumeIntegral() /*system_id*/); - }, - [&]() { - std::string provisioningId; - cryptoSession->GetProvisioningId(&provisioningId); - cryptoSession->GetExternalDeviceUniqueId(&provisioningId); - }, - [&]() { - cryptoSession->LoadLicense( - mFdp.ConsumeRandomLengthString(kStringLength) /*signed_message*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*coreMessage*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*signature*/, - keyType); - }, - [&]() { - std::string coreMessage = - mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature = mFdp.ConsumeRandomLengthString(kStringLength); - cryptoSession->PrepareAndSignRenewalRequest( - mFdp.ConsumeRandomLengthString(kStringLength) /*message*/, - mFdp.ConsumeBool() ? &coreMessage : nullptr, - mFdp.ConsumeBool() ? &signature : nullptr); - }, - [&]() { - cryptoSession->LoadRenewal( - mFdp.ConsumeRandomLengthString(kStringLength) /*signed_message*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*coreMessage*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*signature*/); - }, - [&]() { - const std::string& message = - mFdp.ConsumeRandomLengthString(kStringLength); - std::string coreMessage = - mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature = mFdp.ConsumeRandomLengthString(kStringLength); - OEMCrypto_SignatureHashAlgorithm algorithm; - bool shouldSpecifyAlgorithm = mFdp.ConsumeBool(); - cryptoSession->PrepareAndSignProvisioningRequest( - message, mFdp.ConsumeBool() ? &coreMessage : nullptr, - mFdp.ConsumeBool() ? &signature : nullptr, shouldSpecifyAlgorithm, - algorithm); - }, - [&]() { - if (mFdp.ConsumeBool()) { - const CryptoWrappedKey privateKey; - cryptoSession->LoadCertificatePrivateKey(privateKey); - } else { - CryptoWrappedKey::Type type = - (CryptoWrappedKey::Type)mFdp.ConsumeIntegral(); - std::string key = mFdp.ConsumeRandomLengthString(kStringLength); - const CryptoWrappedKey privateKey(type, key); - cryptoSession->LoadCertificatePrivateKey(privateKey); - } - }, - [&]() { - std::string publicKey; - std::string publicKeySignature; - std::string wrappedPrivateKey; - CryptoWrappedKey::Type keyType; - cryptoSession->GenerateCertificateKeyPair( - &publicKey, &publicKeySignature, &wrappedPrivateKey, &keyType); - }, - [&]() { - if (mFdp.ConsumeBool()) { - const CryptoWrappedKey privateKey; - cryptoSession->LoadOemCertificatePrivateKey(privateKey); - } else { - CryptoWrappedKey::Type type = - (CryptoWrappedKey::Type)mFdp.ConsumeIntegral(); - std::string key = mFdp.ConsumeRandomLengthString(kStringLength); - const CryptoWrappedKey privateKey(type, key); - cryptoSession->LoadOemCertificatePrivateKey(privateKey); - } - }, - [&]() { - size_t out; - RequestedSecurityLevel security_level = - (RequestedSecurityLevel)mFdp.ConsumeIntegral(); - cryptoSession->GetNumberOfOpenSessions(security_level, &out); - }, - [&]() { - std::string info = mFdp.ConsumeRandomLengthString(kStringLength); - cryptoSession->GetBuildInformation(&info); - }, - [&]() { - CdmWatermarkingSupport support = - (CdmWatermarkingSupport)mFdp.ConsumeIntegral(); - cryptoSession->GetWatermarkingSupport(&support); - }, - [&]() { - CdmProductionReadiness readiness = - (CdmProductionReadiness)mFdp.ConsumeIntegral(); - cryptoSession->GetProductionReadiness(&readiness); - }, - [&]() { - RequestedSecurityLevel security_level = - (RequestedSecurityLevel)mFdp.ConsumeIntegral(); - size_t number_of_entries = mFdp.ConsumeIntegral(); - cryptoSession->GetMaximumUsageTableEntries(security_level, + while (fdp_.remaining_bytes()) { + auto invoke_crypto_session_API = fdp_.PickValueInArray< + const std::function>({ + [&]() { + std::string token = fdp_.ConsumeRandomLengthString(kStringLength); + std::string additional_token = + fdp_.ConsumeRandomLengthString(kStringLength); + crypto_session->GetProvisioningToken( + fdp_.ConsumeBool() ? &token : nullptr, + fdp_.ConsumeBool() ? &additional_token : nullptr); + }, + [&]() { + crypto_session->SetSystemId( + fdp_.ConsumeIntegral() /*system_id*/); + }, + [&]() { + std::string provisioning_id; + crypto_session->GetProvisioningId(&provisioning_id); + crypto_session->GetExternalDeviceUniqueId(&provisioning_id); + }, + [&]() { + crypto_session->LoadLicense( + fdp_.ConsumeRandomLengthString(kStringLength) /*signed_message*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*core_message*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*signature*/, + key_type); + }, + [&]() { + std::string core_message = + fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature = fdp_.ConsumeRandomLengthString(kStringLength); + crypto_session->PrepareAndSignRenewalRequest( + fdp_.ConsumeRandomLengthString(kStringLength) /*message*/, + fdp_.ConsumeBool() ? &core_message : nullptr, + fdp_.ConsumeBool() ? &signature : nullptr); + }, + [&]() { + crypto_session->LoadRenewal( + fdp_.ConsumeRandomLengthString(kStringLength) /*signed_message*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*core_message*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*signature*/); + }, + [&]() { + const std::string& message = + fdp_.ConsumeRandomLengthString(kStringLength); + std::string core_message = + fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature = fdp_.ConsumeRandomLengthString(kStringLength); + OEMCrypto_SignatureHashAlgorithm algorithm; + bool should_specify_algorithm = fdp_.ConsumeBool(); + crypto_session->PrepareAndSignProvisioningRequest( + message, fdp_.ConsumeBool() ? &core_message : nullptr, + fdp_.ConsumeBool() ? &signature : nullptr, should_specify_algorithm, + algorithm); + }, + [&]() { + if (fdp_.ConsumeBool()) { + const CryptoWrappedKey private_key; + crypto_session->LoadCertificatePrivateKey(private_key); + } else { + CryptoWrappedKey::Type type = + (CryptoWrappedKey::Type)fdp_.ConsumeIntegral(); + std::string key = fdp_.ConsumeRandomLengthString(kStringLength); + const CryptoWrappedKey private_key(type, key); + crypto_session->LoadCertificatePrivateKey(private_key); + } + }, + [&]() { + std::string public_key; + std::string public_key_signature; + std::string wrapped_private_key; + CryptoWrappedKey::Type key_type; + crypto_session->GenerateCertificateKeyPair( + &public_key, &public_key_signature, &wrapped_private_key, &key_type); + }, + [&]() { + if (fdp_.ConsumeBool()) { + const CryptoWrappedKey private_key; + crypto_session->LoadOemCertificatePrivateKey(private_key); + } else { + CryptoWrappedKey::Type type = + (CryptoWrappedKey::Type)fdp_.ConsumeIntegral(); + std::string key = fdp_.ConsumeRandomLengthString(kStringLength); + const CryptoWrappedKey private_key(type, key); + crypto_session->LoadOemCertificatePrivateKey(private_key); + } + }, + [&]() { + size_t out; + RequestedSecurityLevel security_level = + (RequestedSecurityLevel)fdp_.ConsumeIntegral(); + crypto_session->GetNumberOfOpenSessions(security_level, &out); + }, + [&]() { + std::string info = fdp_.ConsumeRandomLengthString(kStringLength); + crypto_session->GetBuildInformation(&info); + }, + [&]() { + CdmWatermarkingSupport support = + (CdmWatermarkingSupport)fdp_.ConsumeIntegral(); + crypto_session->GetWatermarkingSupport(&support); + }, + [&]() { + CdmProductionReadiness readiness = + (CdmProductionReadiness)fdp_.ConsumeIntegral(); + crypto_session->GetProductionReadiness(&readiness); + }, + [&]() { + RequestedSecurityLevel security_level = + (RequestedSecurityLevel)fdp_.ConsumeIntegral(); + size_t number_of_entries = fdp_.ConsumeIntegral(); + crypto_session->GetMaximumUsageTableEntries(security_level, &number_of_entries); - }, - [&]() { - RequestedSecurityLevel security_level = - (RequestedSecurityLevel)mFdp.ConsumeIntegral(); - uint32_t decrypt_hash_support = mFdp.ConsumeIntegral(); - cryptoSession->GetDecryptHashSupport(security_level, + }, + [&]() { + RequestedSecurityLevel security_level = + (RequestedSecurityLevel)fdp_.ConsumeIntegral(); + uint32_t decrypt_hash_support = fdp_.ConsumeIntegral(); + crypto_session->GetDecryptHashSupport(security_level, &decrypt_hash_support); - }, - [&]() { - bool hasSupport = mFdp.ConsumeBool(); - cryptoSession->HasUsageTableSupport(&hasSupport); - }, - [&]() { - cryptoSession->DeactivateUsageInformation( - mFdp.ConsumeRandomLengthString( - kStringLength) /*provider_session_token*/); - }, - [&]() { - std::string usageReport = - mFdp.ConsumeRandomLengthString(kStringLength); - CryptoSession::UsageDurationStatus usageDurationStatus; - int64_t secondsSinceStarted = mFdp.ConsumeIntegral(); - int64_t secondsSinceLastPlayed = mFdp.ConsumeIntegral(); - cryptoSession->GenerateUsageReport( - mFdp.ConsumeRandomLengthString( - kStringLength) /*provider_session_token*/, - &usageReport, &usageDurationStatus, &secondsSinceStarted, - &secondsSinceLastPlayed); - }, - [&]() { - uint32_t nonce = mFdp.ConsumeIntegral(); - cryptoSession->GenerateNonce(&nonce); - }, - [&]() { - std::string signedMessage, coreMessage, signature; - std::string wrappedPrivateKey; - cryptoSession->LoadProvisioning( - signedMessage, coreMessage, signature, - mFdp.ConsumeBool() ? &wrappedPrivateKey : nullptr); - }, - [&]() { - cryptoSession->SetDecryptHash( - mFdp.ConsumeIntegral() /*frame_number*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*hash*/); - }, - [&]() { - CdmEncryptionAlgorithm algorithm; - std::string outBuffer; - cryptoSession->GenericDecrypt( - mFdp.ConsumeRandomLengthString(kStringLength) /*in_buffer*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*key_id*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*iv*/, algorithm, - &outBuffer); - }, - [&]() { - CdmSigningAlgorithm algorithm = mFdp.ConsumeBool() - ? kSigningAlgorithmHmacSha256 - : kSigningAlgorithmUnknown; - std::string signature; - cryptoSession->GenericSign( - mFdp.ConsumeRandomLengthString(kStringLength) /*message*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*key_id*/, - algorithm, &signature); - }, - [&]() { - CdmSigningAlgorithm algorithm = kSigningAlgorithmHmacSha256; - const std::string signature; - cryptoSession->GenericVerify( - mFdp.ConsumeRandomLengthString(kStringLength) /*message*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*key_id*/, - algorithm, signature); - }, - [&]() { - UsageTableHeader usageTableHeader; - cryptoSession->CreateUsageTableHeader(requestedSecurityLevel, - &usageTableHeader); + }, + [&]() { + bool has_support = fdp_.ConsumeBool(); + crypto_session->HasUsageTableSupport(&has_support); + }, + [&]() { + crypto_session->DeactivateUsageInformation( + fdp_.ConsumeRandomLengthString( + kStringLength) /*provider_session_token*/); + }, + [&]() { + std::string usage_report = + fdp_.ConsumeRandomLengthString(kStringLength); + CryptoSession::UsageDurationStatus usage_duration_status; + int64_t seconds_since_started = fdp_.ConsumeIntegral(); + int64_t seconds_since_last_played = fdp_.ConsumeIntegral(); + crypto_session->GenerateUsageReport( + fdp_.ConsumeRandomLengthString( + kStringLength) /*provider_session_token*/, + &usage_report, &usage_duration_status, &seconds_since_started, + &seconds_since_last_played); + }, + [&]() { + uint32_t nonce = fdp_.ConsumeIntegral(); + crypto_session->GenerateNonce(&nonce); + }, + [&]() { + std::string signed_message, core_message, signature; + std::string wrapped_private_key; + crypto_session->LoadProvisioning( + signed_message, core_message, signature, + fdp_.ConsumeBool() ? &wrapped_private_key : nullptr); + }, + [&]() { + crypto_session->SetDecryptHash( + fdp_.ConsumeIntegral() /*frame_number*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*hash*/); + }, + [&]() { + CdmEncryptionAlgorithm algorithm; + std::string out_buffer; + crypto_session->GenericDecrypt( + fdp_.ConsumeRandomLengthString(kStringLength) /*in_buffer*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*key_id*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*iv*/, algorithm, + &out_buffer); + }, + [&]() { + CdmSigningAlgorithm algorithm = fdp_.ConsumeBool() + ? kSigningAlgorithmHmacSha256 + : kSigningAlgorithmUnknown; + std::string signature; + crypto_session->GenericSign( + fdp_.ConsumeRandomLengthString(kStringLength) /*message*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*key_id*/, + algorithm, &signature); + }, + [&]() { + CdmSigningAlgorithm algorithm = kSigningAlgorithmHmacSha256; + const std::string signature; + crypto_session->GenericVerify( + fdp_.ConsumeRandomLengthString(kStringLength) /*message*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*key_id*/, + algorithm, signature); + }, + [&]() { + UsageTableHeader usage_table_header; + crypto_session->CreateUsageTableHeader(requested_security_level, + &usage_table_header); - cryptoSession->LoadUsageTableHeader(requestedSecurityLevel, - usageTableHeader); - uint32_t newEntryCount; - cryptoSession->ShrinkUsageTableHeader( - requestedSecurityLevel, newEntryCount, &usageTableHeader); - }, - [&]() { - const UsageTableHeader usageTableHeader; - cryptoSession->LoadUsageTableHeader(requestedSecurityLevel, - usageTableHeader); - }, - [&]() { - uint32_t entryNumber; - cryptoSession->CreateUsageEntry(&entryNumber); - cryptoSession->MoveUsageEntry(entryNumber); - }, - [&]() { - const UsageEntry usageEntry; - cryptoSession->LoadUsageEntry( - mFdp.ConsumeIntegral() /*entry_number*/, usageEntry); - }, - [&]() { - UsageTableHeader usageTableHeader; - UsageEntry usageEntry; - cryptoSession->UpdateUsageEntry(&usageTableHeader, &usageEntry); - }, - [&]() { - bool canSupportOutput = mFdp.ConsumeBool(); - bool canDisableOutput = mFdp.ConsumeBool(); - bool canSupportCgmsA = mFdp.ConsumeBool(); - cryptoSession->GetAnalogOutputCapabilities( - &canSupportOutput, &canDisableOutput, &canSupportCgmsA); - }, - [&]() { - cryptoSession->SetDebugIgnoreKeyboxCount( - mFdp.ConsumeIntegral() /*count*/); - }, - [&]() { cryptoSession->GetOkpFallbackPolicy(); }, - [&]() { - std::string request = mFdp.ConsumeRandomLengthString(kStringLength); - cryptoSession->PrepareOtaProvisioningRequest( - mFdp.ConsumeBool() /*use_test_key*/, &request); - }, - [&]() { - cryptoSession->LoadOtaProvisioning( - mFdp.ConsumeBool() /*use_test_key*/, - mFdp.ConsumeRandomLengthString(kStringLength) /*response*/); - }, - [&]() { - uint32_t tier = mFdp.ConsumeIntegral(); - cryptoSession->GetResourceRatingTier(&tier); - }, - [&]() { - size_t max; - cryptoSession->GetMaxNumberOfSessions(requestedSecurityLevel, &max); - }, - [&]() { - std::string keyData = mFdp.ConsumeRandomLengthString(kStringLength); - cryptoSession->GetTokenFromKeybox(requestedSecurityLevel, &keyData); - }, - [&]() { - std::string bcc = mFdp.ConsumeRandomLengthString(kStringLength); - std::string additionalSignature = - mFdp.ConsumeRandomLengthString(kStringLength); - cryptoSession->GetBootCertificateChain(&bcc, &additionalSignature); - }, - [&]() { - cryptoSession->GenerateDerivedKeys( - mFdp.ConsumeRandomLengthString(kStringLength) /*message*/); - }, - [&]() { - CdmDecryptionParametersV16 params2; - cryptoSession->Decrypt(params2); - }, - [&]() { - const std::string& message = - mFdp.ConsumeRandomLengthString(kStringLength); - std::string coreMessage, signature; - bool shouldSpecifyAlgorithm; - OEMCrypto_SignatureHashAlgorithm algorithm; + crypto_session->LoadUsageTableHeader(requested_security_level, + usage_table_header); + uint32_t new_entry_count; + crypto_session->ShrinkUsageTableHeader( + requested_security_level, new_entry_count, &usage_table_header); + }, + [&]() { + const UsageTableHeader usage_table_header; + crypto_session->LoadUsageTableHeader(requested_security_level, + usage_table_header); + }, + [&]() { + uint32_t entry_number; + crypto_session->CreateUsageEntry(&entry_number); + crypto_session->MoveUsageEntry(entry_number); + }, + [&]() { + const UsageEntry usage_entry; + crypto_session->LoadUsageEntry( + fdp_.ConsumeIntegral() /*entry_number*/, usage_entry); + }, + [&]() { + UsageTableHeader usage_table_header; + UsageEntry usage_entry; + crypto_session->UpdateUsageEntry(&usage_table_header, &usage_entry); + }, + [&]() { + bool can_support_output = fdp_.ConsumeBool(); + bool can_disable_output = fdp_.ConsumeBool(); + bool can_support_cgmsa = fdp_.ConsumeBool(); + crypto_session->GetAnalogOutputCapabilities( + &can_support_output, &can_disable_output, &can_support_cgmsa); + }, + [&]() { + crypto_session->SetDebugIgnoreKeyboxCount( + fdp_.ConsumeIntegral() /*count*/); + }, + [&]() { crypto_session->GetOkpFallbackPolicy(); }, + [&]() { + std::string request = fdp_.ConsumeRandomLengthString(kStringLength); + crypto_session->PrepareOtaProvisioningRequest( + fdp_.ConsumeBool() /*use_test_key*/, &request); + }, + [&]() { + crypto_session->LoadOtaProvisioning( + fdp_.ConsumeBool() /*use_test_key*/, + fdp_.ConsumeRandomLengthString(kStringLength) /*response*/); + }, + [&]() { + uint32_t tier = fdp_.ConsumeIntegral(); + crypto_session->GetResourceRatingTier(&tier); + }, + [&]() { + size_t max; + crypto_session->GetMaxNumberOfSessions(requested_security_level, &max); + }, + [&]() { + std::string key_data = fdp_.ConsumeRandomLengthString(kStringLength); + crypto_session->GetTokenFromKeybox(requested_security_level, &key_data); + }, + [&]() { + std::string bcc = fdp_.ConsumeRandomLengthString(kStringLength); + std::string additional_signature = + fdp_.ConsumeRandomLengthString(kStringLength); + crypto_session->GetBootCertificateChain(&bcc, &additional_signature); + }, + [&]() { + crypto_session->GenerateDerivedKeys( + fdp_.ConsumeRandomLengthString(kStringLength) /*message*/); + }, + [&]() { + CdmDecryptionParametersV16 params_2; + crypto_session->Decrypt(params_2); + }, + [&]() { + const std::string& message = + fdp_.ConsumeRandomLengthString(kStringLength); + std::string core_message, signature; + bool should_specify_algorithm; + OEMCrypto_SignatureHashAlgorithm algorithm; - cryptoSession->PrepareAndSignProvisioningRequest( - message, &coreMessage, &signature, shouldSpecifyAlgorithm, - algorithm); - cryptoSession->PrepareAndSignLicenseRequest( - message, &coreMessage, &signature, shouldSpecifyAlgorithm, - algorithm); - }, - [&]() { - std::string keyId = mFdp.ConsumeRandomLengthString(kStringLength); - std::string message = mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature, coreMessage; - setUp(cryptoSession.get(), keyId, message, signature, coreMessage); + crypto_session->PrepareAndSignProvisioningRequest( + message, &core_message, &signature, should_specify_algorithm, + algorithm); + crypto_session->PrepareAndSignLicenseRequest( + message, &core_message, &signature, should_specify_algorithm, + algorithm); + }, + [&]() { + std::string key_id = fdp_.ConsumeRandomLengthString(kStringLength); + std::string message = fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature, core_message; + SetUp(crypto_session.get(), key_id, message, signature, core_message); - std::vector inVector; - std::vector ivVector; - std::string inBuffer; - std::string iv; - CdmEncryptionAlgorithm algorithmEncrypt = - kEncryptionAlgorithmAesCbc128; - std::string outBuffer; - initializeStripeBuffer(&inVector, CONTENT_KEY_SIZE * 15); - inBuffer = std::string(inVector.begin(), inVector.end()); - initializeStripeBuffer(&ivVector, KEY_IV_SIZE); - iv = std::string(ivVector.begin(), ivVector.end()); + std::vector in_vector; + std::vector iv_vector; + std::string in_buffer; + std::string iv; + CdmEncryptionAlgorithm algorithm_encrypt = + kEncryptionAlgorithmAesCbc128; + std::string out_buffer; + InitializeStripeBuffer(&in_vector, CONTENT_KEY_SIZE * 15); + in_buffer = std::string(in_vector.begin(), in_vector.end()); + InitializeStripeBuffer(&iv_vector, KEY_IV_SIZE); + iv = std::string(iv_vector.begin(), iv_vector.end()); - cryptoSession->GenericEncrypt(inBuffer, keyId, iv, algorithmEncrypt, - &outBuffer); - }, - [&]() { - std::string keyId = mFdp.ConsumeRandomLengthString(kStringLength); - std::string message = mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature, coreMessage; - setUp(cryptoSession.get(), keyId, message, signature, coreMessage); + crypto_session->GenericEncrypt(in_buffer, key_id, iv, algorithm_encrypt, + &out_buffer); + }, + [&]() { + std::string key_id = fdp_.ConsumeRandomLengthString(kStringLength); + std::string message = fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature, core_message; + SetUp(crypto_session.get(), key_id, message, signature, core_message); - std::vector inVector; - std::vector ivVector; - std::string inBuffer; - std::string iv; - CdmEncryptionAlgorithm algorithmEncrypt = - kEncryptionAlgorithmAesCbc128; - std::string outBuffer; - initializeStripeBuffer(&inVector, CONTENT_KEY_SIZE * 15); - inBuffer = std::string(inVector.begin(), inVector.end()); - initializeStripeBuffer(&ivVector, KEY_IV_SIZE); - iv = std::string(ivVector.begin(), ivVector.end()); + std::vector in_vector; + std::vector iv_vector; + std::string in_buffer; + std::string iv; + CdmEncryptionAlgorithm algorithm_encrypt = + kEncryptionAlgorithmAesCbc128; + std::string out_buffer; + InitializeStripeBuffer(&in_vector, CONTENT_KEY_SIZE * 15); + in_buffer = std::string(in_vector.begin(), in_vector.end()); + InitializeStripeBuffer(&iv_vector, KEY_IV_SIZE); + iv = std::string(iv_vector.begin(), iv_vector.end()); - cryptoSession->GenericDecrypt(inBuffer, keyId, iv, algorithmEncrypt, - &outBuffer); - }, - [&]() { - std::string keyId = mFdp.ConsumeRandomLengthString(kStringLength); - std::string message = mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature, coreMessage; - setUp(cryptoSession.get(), keyId, message, signature, coreMessage); + crypto_session->GenericDecrypt(in_buffer, key_id, iv, algorithm_encrypt, + &out_buffer); + }, + [&]() { + std::string key_id = fdp_.ConsumeRandomLengthString(kStringLength); + std::string message = fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature, core_message; + SetUp(crypto_session.get(), key_id, message, signature, core_message); - CdmSigningAlgorithm algorithm2 = kSigningAlgorithmHmacSha256; - cryptoSession->GenericSign(message, keyId, algorithm2, &signature); - }, - [&]() { - std::string keyId = mFdp.ConsumeRandomLengthString(kStringLength); - std::string message = mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature, coreMessage; - setUp(cryptoSession.get(), keyId, message, signature, coreMessage); + CdmSigningAlgorithm algorithm_2 = kSigningAlgorithmHmacSha256; + crypto_session->GenericSign(message, key_id, algorithm_2, &signature); + }, + [&]() { + std::string key_id = fdp_.ConsumeRandomLengthString(kStringLength); + std::string message = fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature, core_message; + SetUp(crypto_session.get(), key_id, message, signature, core_message); - CdmSigningAlgorithm algorithm2 = kSigningAlgorithmHmacSha256; - cryptoSession->GenericVerify(message, keyId, algorithm2, signature); - }, - [&]() { - const std::string keyId = - mFdp.ConsumeRandomLengthString(kStringLength); - std::string message = mFdp.ConsumeRandomLengthString(kStringLength); - std::string signature, coreMessage; - setUp(cryptoSession.get(), keyId, message, signature, coreMessage); - CdmDecryptionParametersV16 params(keyId); - CryptoSessionFuzzer::FillDecryptParams(params); - cryptoSession->Decrypt(params); - }, - }); - invokeCryptoSessionAPI(); - } - cryptoSession->Close(); + CdmSigningAlgorithm algorithm_2 = kSigningAlgorithmHmacSha256; + crypto_session->GenericVerify(message, key_id, algorithm_2, signature); + }, + [&]() { + const std::string key_id = + fdp_.ConsumeRandomLengthString(kStringLength); + std::string message = fdp_.ConsumeRandomLengthString(kStringLength); + std::string signature, core_message; + SetUp(crypto_session.get(), key_id, message, signature, core_message); + CdmDecryptionParametersV16 params(key_id); + CryptoSessionFuzzer::FillDecryptParams(params); + crypto_session->Decrypt(params); + }, + }); + invoke_crypto_session_API(); + } + crypto_session->Close(); } -extern "C" int LLVMFuzzerTestOneInput(const uint8_t * data, size_t size) { - CryptoSessionFuzzer cryptoSessionFuzzer(data, size); - cryptoSessionFuzzer.process(); - return 0; +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + CryptoSessionFuzzer crypto_session_fuzzer(data, size); + crypto_session_fuzzer.Process(); + return 0; }