Creating a new RNG and replacing rand().

[ Merge of http://go/wvgerrit/84607 ]
[ Merge of http://go/wvgerrit/84608 ]

The primary goal is to replace the use of `rand()` with the random
number generators provided with the C++11 standard.

This simplified generator wraps some of the technical aspects of the
<random> library and provides an interface for uniformly distributed
integers.

As part of the `rand()` purge in the CDM, all uses of the C random int
function in `core()` have been removed.  Places that previously used
`rand()` now use `CdmRandom` facilities.

Test: Linux unittest and Android unittest
Bug: 130680365
Change-Id: Ica383870536ed462dbb80e630c2d66845e38b937
This commit is contained in:
Alex Dale
2019-08-26 14:39:50 -07:00
parent 9da0617606
commit ee56d93454
12 changed files with 406 additions and 78 deletions

View File

@@ -392,8 +392,6 @@ class CdmEngine {
Clock clock_;
std::string spoid_;
static bool seeded_;
// usage related variables
std::unique_ptr<CdmSession> usage_session_;
std::unique_ptr<UsagePropertySet> usage_property_set_;

View File

@@ -83,10 +83,6 @@ class UsageTableHeader {
// for the objects that DeleteEntry depends on.
void DeleteEntryForTest(uint32_t usage_entry_number);
// TODO(rfrias): Move to utility class
static int64_t GetRandomInRange(size_t upper_bound_exclusive);
static int64_t GetRandomInRangeWithExclusion(size_t upper_bound_exclusive,
size_t exclude);
size_t size() { return usage_entry_info_.size(); }
private:

View File

@@ -12,6 +12,7 @@
#include <memory>
#include <sstream>
#include "cdm_random.h"
#include "cdm_session.h"
#include "cdm_session_map.h"
#include "clock.h"
@@ -69,8 +70,6 @@ class UsagePropertySet : public CdmClientPropertySet {
const std::string empty_;
};
bool CdmEngine::seeded_ = false;
CdmEngine::CdmEngine(FileSystem* file_system,
std::shared_ptr<metrics::EngineMetrics> metrics,
const std::string& spoid)
@@ -83,11 +82,7 @@ CdmEngine::CdmEngine(FileSystem* file_system,
usage_property_set_(),
last_usage_information_update_time_(0) {
assert(file_system);
if (!seeded_) {
Properties::Init();
srand(clock_.GetCurrentTime());
seeded_ = true;
}
Properties::Init();
}
CdmEngine::~CdmEngine() {
@@ -1260,7 +1255,8 @@ CdmResponseType CdmEngine::GetUsageInfo(const std::string& app_id,
CdmUsageInfo* usage_info) {
LOGI("Getting usage info: app_id = %s", app_id.c_str());
// Return a random usage report from a random security level
SecurityLevel security_level = ((rand() % 2) == 0) ? kLevelDefault : kLevel3;
SecurityLevel security_level =
CdmRandom::RandomBool() ? kLevelDefault : kLevel3;
CdmResponseType status = UNKNOWN_ERROR;
if (!usage_info) {
LOGE("No usage info destination");
@@ -1328,7 +1324,7 @@ CdmResponseType CdmEngine::GetUsageInfo(const std::string& app_id,
usage_info->resize(kUsageReportsPerRequest);
size_t index = rand() % usage_data.size();
size_t index = CdmRandom::RandomInRange(usage_data.size() - 1);
status = usage_session_->RestoreUsageSession(usage_data[index], error_detail);
if (KEY_ADDED != status) {
LOGE("RestoreUsageSession failed: index = %zu, status = %d", index,

View File

@@ -4,6 +4,7 @@
#include "usage_table_header.h"
#include "cdm_random.h"
#include "crypto_session.h"
#include "license.h"
#include "log.h"
@@ -21,6 +22,7 @@ std::string kOldUsageEntryServerMacKey(wvcdm::MAC_KEY_SIZE, 0);
std::string kOldUsageEntryClientMacKey(wvcdm::MAC_KEY_SIZE, 0);
std::string kOldUsageEntryPoviderSessionToken =
"nahZ6achSheiqua3TohQuei0ahwohv";
} // namespace
namespace wvcdm {
@@ -152,8 +154,11 @@ CdmResponseType UsageTableHeader::AddEntry(
for (uint32_t retry_count = 0; retry_count < kMaxCryptoRetries &&
status == INSUFFICIENT_CRYPTO_RESOURCES_3;
++retry_count) {
int64_t entry_number_to_delete = GetRandomInRange(usage_entry_info_.size());
if (entry_number_to_delete < 0) break;
if (usage_entry_info_.size() == 0) {
break;
}
uint32_t entry_number_to_delete =
CdmRandom::RandomInRange(usage_entry_info_.size() - 1);
DeleteEntry(entry_number_to_delete, file_handle_.get(), metrics);
status = crypto_session->CreateUsageEntry(usage_entry_number);
@@ -226,10 +231,14 @@ CdmResponseType UsageTableHeader::LoadEntry(CryptoSession* crypto_session,
for (uint32_t retry_count = 0; retry_count < kMaxCryptoRetries &&
status == INSUFFICIENT_CRYPTO_RESOURCES_3;
++retry_count) {
if (usage_entry_info_.size() <= 1) break;
// Get a random entry from the other entries.
int64_t entry_number_to_delete = GetRandomInRangeWithExclusion(
usage_entry_info_.size(), usage_entry_number);
if (entry_number_to_delete < 0) break;
uint32_t entry_number_to_delete =
CdmRandom::RandomInRange(usage_entry_info_.size() - 2);
if (entry_number_to_delete >= usage_entry_number) {
// Exclude |usage_entry_number|.
++entry_number_to_delete;
}
DeleteEntry(entry_number_to_delete, file_handle_.get(), metrics);
status = crypto_session->LoadUsageEntry(usage_entry_number, usage_entry);
}
@@ -723,27 +732,6 @@ bool UsageTableHeader::UpgradeUsageInfoFromUsageTable(
return NO_ERROR;
}
int64_t UsageTableHeader::GetRandomInRange(size_t upper_bound_exclusive) {
if (upper_bound_exclusive == 0) {
LOGE("|upper_bound_exclusive| must be > 0");
return -1;
}
return (int)((double)rand() / ((double)RAND_MAX + 1) * upper_bound_exclusive);
}
int64_t UsageTableHeader::GetRandomInRangeWithExclusion(
size_t upper_bound_exclusive, size_t exclude) {
if ((upper_bound_exclusive <= 1) || (exclude >= upper_bound_exclusive)) {
LOGE(
"|upper_bound_exclusive| must be > 1 and |exclude| must be < "
"|upper_bound_exclusive|");
return -1;
}
uint32_t random = GetRandomInRange(upper_bound_exclusive - 1);
if (random >= exclude) random++;
return random;
}
// TODO(fredgc): remove when b/65730828 is addressed
bool UsageTableHeader::CreateDummyOldUsageEntry(CryptoSession* crypto_session) {
return crypto_session->CreateOldUsageEntry(

View File

@@ -11,6 +11,7 @@
#include <string>
#include "arraysize.h"
#include "cdm_random.h"
#include "file_store.h"
#include "properties.h"
#include "string_conversions.h"
@@ -2014,14 +2015,6 @@ class DeviceFilesTest : public ::testing::Test {
&device_base_path_));
}
std::string GenerateRandomData(uint32_t len) {
std::string data(len, 0);
for (size_t i = 0; i < len; i++) {
data[i] = rand() % 256;
}
return data;
}
size_t GetLicenseDataSize(LicenseInfo& data) {
CdmAppParameterMap app_parameters = GetAppParameters(data.app_parameters);
size_t app_parameters_len = 0;
@@ -2169,8 +2162,8 @@ MATCHER_P8(Contains, str1, str2, str3, str4, str5, str6, map7, str8, "") {
TEST_F(DeviceCertificateStoreTest, StoreCertificate) {
MockFileSystem file_system;
std::string certificate(GenerateRandomData(kCertificateLen));
std::string wrapped_private_key(GenerateRandomData(kWrappedKeyLen));
std::string certificate(CdmRandom::RandomData(kCertificateLen));
std::string wrapped_private_key(CdmRandom::RandomData(kWrappedKeyLen));
std::string device_certificate_path =
device_base_path_ + DeviceFiles::GetCertificateFileName();
@@ -2243,8 +2236,8 @@ TEST_F(DeviceCertificateTest, HasCertificate) {
TEST_P(DeviceFilesSecurityLevelTest, SecurityLevel) {
MockFileSystem file_system;
std::string certificate(GenerateRandomData(kCertificateLen));
std::string wrapped_private_key(GenerateRandomData(kWrappedKeyLen));
std::string certificate(CdmRandom::RandomData(kCertificateLen));
std::string wrapped_private_key(CdmRandom::RandomData(kWrappedKeyLen));
CdmSecurityLevel security_level = GetParam();
std::string device_base_path;

View File

@@ -5,6 +5,7 @@
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "cdm_random.h"
#include "file_store.h"
#include "test_vectors.h"
@@ -30,14 +31,6 @@ class FileTest : public testing::Test {
EXPECT_TRUE(file_system.Remove(test_vectors::kTestDir));
}
std::string GenerateRandomData(uint32_t len) {
std::string data(len, 0);
for (size_t i = 0; i < len; i++) {
data[i] = rand() % 256;
}
return data;
}
FileSystem file_system;
};
@@ -104,7 +97,7 @@ TEST_F(FileTest, FileSize) {
std::string path = test_vectors::kTestDir + kTestFileName;
file_system.Remove(path);
std::string write_data = GenerateRandomData(600);
std::string write_data = CdmRandom::RandomData(600);
size_t write_data_size = write_data.size();
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
ASSERT_TRUE(file);
@@ -119,7 +112,7 @@ TEST_F(FileTest, WriteReadBinaryFile) {
std::string path = test_vectors::kTestDir + kTestFileName;
file_system.Remove(path);
std::string write_data = GenerateRandomData(600);
std::string write_data = CdmRandom::RandomData(600);
size_t write_data_size = write_data.size();
std::unique_ptr<File> file = file_system.Open(path, FileSystem::kCreate);
ASSERT_TRUE(file);

View File

@@ -10,6 +10,7 @@
#include <string>
#include "arraysize.h"
#include "cdm_random.h"
#include "crypto_session.h"
#include "device_files.h"
#include "file_store.h"
@@ -990,10 +991,9 @@ TEST_F(UsageTableHeaderTest,
// We try to load a usage entry from the first 9 entries, since DeleteEntry
// can't delete an entry if the last one is in use.
int64_t usage_entry_number_to_load = MockUsageTableHeader::GetRandomInRange(
k10UsageEntryInfoVector.size() - 1);
ASSERT_THAT(usage_entry_number_to_load,
AllOf(Ge(0), Lt((int64_t)k10UsageEntryInfoVector.size() - 1)));
uint32_t usage_entry_number_to_load =
CdmRandom::RandomInRange(k10UsageEntryInfoVector.size() - 2);
CdmUsageEntry usage_entry_to_load = kUsageEntry;
// Setup expectations
@@ -1025,10 +1025,8 @@ TEST_F(UsageTableHeaderTest,
// We try to load a usage entry from the first 8 entries, since DeleteEntry
// can't delete an entry if the last one is in use.
int64_t usage_entry_number_to_load = MockUsageTableHeader::GetRandomInRange(
k10UsageEntryInfoVector.size() - 2);
ASSERT_THAT(usage_entry_number_to_load,
AllOf(Ge(0), Lt((int64_t)k10UsageEntryInfoVector.size() - 2)));
uint32_t usage_entry_number_to_load =
CdmRandom::RandomInRange(k10UsageEntryInfoVector.size() - 3);
CdmUsageEntry usage_entry_to_load = kUsageEntry;
// Setup expectations
@@ -1060,10 +1058,8 @@ TEST_F(UsageTableHeaderTest, LoadEntry_LoadUsageEntryFailsThrice) {
// We try to load a usage entry from the first 7 entries, since DeleteEntry
// can't delete an entry if the last one is in use.
int64_t usage_entry_number_to_load = MockUsageTableHeader::GetRandomInRange(
k10UsageEntryInfoVector.size() - 3);
ASSERT_THAT(usage_entry_number_to_load,
AllOf(Ge(0), Lt((int64_t)k10UsageEntryInfoVector.size() - 3)));
uint32_t usage_entry_number_to_load =
CdmRandom::RandomInRange(k10UsageEntryInfoVector.size() - 4);
CdmUsageEntry usage_entry_to_load = kUsageEntry;
// Setup expectations