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

View File

@@ -36,6 +36,10 @@ test_src_dir := .
test_main := ../core/test/test_main.cpp
include $(LOCAL_PATH)/integration-test.mk
test_name := cdm_random_unittest
test_src_dir := ../util/test
include $(LOCAL_PATH)/unit-test.mk
test_name := cdm_session_unittest
test_src_dir := ../core/test
test_main := ../core/test/test_main.cpp

View File

@@ -0,0 +1,109 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#ifndef WVCDM_CORE_CDM_RANDOM_H_
#define WVCDM_CORE_CDM_RANDOM_H_
#include <mutex>
#include <random>
#include <string>
namespace wvcdm {
// CdmRandomGenerator is a thread safe, pseudo-random number generator.
// It's purpose is to simplified interface for C++11's <random> library.
// Some of the methods use a "device specific" random seed, if the
// compiler/device does not support device specific randomizers, then the
// actual value supplied may not be random.
class CdmRandomGenerator {
public:
// The maximum number of bytes that can be generated at once for
// `RandomData()`.
static constexpr size_t kMaxRandomDataLength = 8192; // 8 kB
// Initializes the pseudo-random generator with a value from a device
// specific random number generator.
CdmRandomGenerator();
// Initializes the pseudo-random generator with the specified seed value.
explicit CdmRandomGenerator(unsigned int seed) : generator_(seed) {}
// All of these methods are thread-safe.
// Seeds the pseudo-random generator with a value from a device specific
// random number generator.
void Seed();
// Seeds the pseudo-random generator with the specified seed value.
// This is somewhat similar to `srand()` from the C standard library;
// except that the sequence generated from successive calls to `Rand()`
// will not necessarily be the same as they would be from the
// standard library `rand()`. This is due to the underlying pseudo-random
// generator that is used.
void Seed(unsigned int seed);
// Returns a pseudo-random integer.
// This is similar to `rand()` from the C standard library. The integer
// returned is in the range of [0, RAND_MAX].
int Rand();
// Allows for RNG to be callable, this is to make it similar to the
// C++11 generator interfaces.
int operator()() { return Rand(); }
// Returns a pseudo-random integer within the provided inclusive range.
uint64_t RandomInRange(uint64_t lower, uint64_t upper);
uint64_t RandomInRange(uint64_t upper) { return RandomInRange(0, upper); }
// Returns a byte string containing randomized bytes of the specified
// length.
// If |length| is greater than |CdmRandomGenerator::kMaxRandomDataLength|,
// then an error is logged and an empty string is returned.
std::string RandomData(size_t length);
// Random true/false using Bernoulli distribution of equal probability.
bool RandomBool();
private:
// Mutex is used to lock the object, and allowing it to be used
// concurrently in different threads.
std::mutex generator_lock_;
// The `default_random_engine` depends on the compiler used and
// potentially its version. This is important to know if you need to
// create reproducible tests between platforms.
std::default_random_engine generator_;
};
// Provides a static interface to a process-wide instance of
// CdmRandomGenerator.
class CdmRandom {
public:
static int Rand() { return GetInstance()->Rand(); }
static uint64_t RandomInRange(uint64_t lower, uint64_t upper) {
return GetInstance()->RandomInRange(lower, upper);
}
static uint64_t RandomInRange(uint64_t upper) {
return GetInstance()->RandomInRange(upper);
}
static std::string RandomData(size_t length) {
return GetInstance()->RandomData(length);
}
static bool RandomBool() { return GetInstance()->RandomBool(); }
private:
// These are intended to be used by tests if needed.
static void Seed(unsigned int seed) { GetInstance()->Seed(seed); }
static void Seed() { GetInstance()->Seed(); }
// Returns the process-wide instance of CdmRandomGenerator.
// It the global instance has not yet been created, then a new instance
// is created using a device-specific random seed.
static CdmRandomGenerator* GetInstance();
};
} // namespace wvcdm
#endif // WVCDM_CORE_CDM_RANDOM_H_

View File

@@ -0,0 +1,107 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#include "cdm_random.h"
#include <stdlib.h>
#include <algorithm>
#include "log.h"
// This type alias is for convenience.
using CdmRandomLock = std::unique_lock<std::mutex>;
namespace wvcdm {
namespace {
// More information about C++11's random number generators can be found
// from the introductory paper https://isocpp.org/files/papers/n3551.pdf
// Attemps to get random data in a device specific manner. If the device
// does not support true random data, then a pseudo-random sequence might
// be used instead. The exact behaviour depends on the compiler and
// platform combination.
unsigned int GetDeviceRandomSeed() {
static std::random_device rdev;
static std::mutex rdev_mutex;
CdmRandomLock rdev_lock(rdev_mutex);
return rdev();
}
} // namespace
// CdmRandomGenerator.
CdmRandomGenerator::CdmRandomGenerator() : generator_(GetDeviceRandomSeed()) {}
void CdmRandomGenerator::Seed() {
CdmRandomLock lock(generator_lock_);
generator_.seed(GetDeviceRandomSeed());
}
void CdmRandomGenerator::Seed(unsigned int s) {
CdmRandomLock lock(generator_lock_);
generator_.seed(s);
}
int CdmRandomGenerator::Rand() {
CdmRandomLock lock(generator_lock_);
std::uniform_int_distribution<int> dist(0, RAND_MAX);
return dist(generator_);
}
uint64_t CdmRandomGenerator::RandomInRange(uint64_t lower, uint64_t upper) {
if (lower == upper) {
return lower;
}
CdmRandomLock lock(generator_lock_);
if (lower > upper) {
LOGW(
"Lower bound is larger than upper bound, swapping bounds: "
"lower = %llu, upper = %llu",
// Casting to insure this will work on 32-bit systems.
static_cast<unsigned long long>(lower),
static_cast<unsigned long long>(upper));
std::swap(lower, upper);
}
std::uniform_int_distribution<uint64_t> dist(lower, upper);
return dist(generator_);
}
std::string CdmRandomGenerator::RandomData(size_t length) {
if (length > kMaxRandomDataLength) {
LOGE("Maximum random data length exceeded: length = %zu, max_length = %zu",
length, kMaxRandomDataLength);
return std::string();
}
CdmRandomLock lock(generator_lock_);
std::uniform_int_distribution<uint8_t> dist; // Range of [0, 255].
std::string random_data(length, '\0');
std::generate(random_data.begin(), random_data.end(),
[&]() { return dist(generator_); });
return random_data;
}
bool CdmRandomGenerator::RandomBool() {
CdmRandomLock lock(generator_lock_);
std::bernoulli_distribution dist; // 50/50.
return dist(generator_);
}
// CdmRandom.
// static
CdmRandomGenerator* CdmRandom::GetInstance() {
static std::mutex g_instance_lock;
static CdmRandomGenerator* g_instance = nullptr;
CdmRandomLock lock(g_instance_lock);
if (g_instance == nullptr) {
LOGV("Initalizing CDM random number generator");
g_instance = new CdmRandomGenerator(GetDeviceRandomSeed());
}
return g_instance;
}
} // namespace wvcdm

View File

@@ -0,0 +1,147 @@
// Copyright 2019 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine Master
// License Agreement.
#include <stdlib.h>
#include <chrono>
#include <limits>
#include <string>
#include <thread>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "cdm_random.h"
namespace wvcdm {
namespace {
// Random data vector lengths.
constexpr size_t kVectorLength = 1024;
constexpr size_t kMaxRandomDataLength =
CdmRandomGenerator::kMaxRandomDataLength;
constexpr size_t kAboveMaxRandomDataLength = std::numeric_limits<size_t>::max();
constexpr size_t kRandomTrialCount = 100;
constexpr size_t kThreadCount = 16;
constexpr unsigned int kSeeds[] = {0, 1337, 1565904109, 776964657};
class CdmRandomGeneratorTest : public testing::TestWithParam<unsigned int> {};
} // namespace
TEST_P(CdmRandomGeneratorTest, AllMethods) {
const unsigned int seed = GetParam();
CdmRandomGenerator rng;
rng.Seed();
rng.Seed(seed);
rng.Rand();
rng();
rng.RandomInRange(1234, 1000000);
rng.RandomInRange(1000000);
rng.RandomData(kVectorLength);
rng.RandomBool();
}
TEST_P(CdmRandomGeneratorTest, RandomInRange) {
const unsigned int seed = GetParam();
CdmRandomGenerator rng(seed);
for (size_t i = 0; i < kRandomTrialCount; ++i) {
const int rand_int = rng.Rand();
EXPECT_GE(rand_int, 0);
EXPECT_LE(rand_int, RAND_MAX);
}
// Range size of 1.
const uint64_t rand_u64_1 = rng.RandomInRange(100, 100);
EXPECT_EQ(rand_u64_1, 100ul);
// Range size of 2.
const uint64_t rand_u64_2 = rng.RandomInRange(1234, 1235);
EXPECT_GE(rand_u64_2, 1234ul);
EXPECT_LE(rand_u64_2, 1235ul);
// Small range.
const uint64_t rand_u64_3 = rng.RandomInRange(10);
EXPECT_LE(rand_u64_3, 10ul);
// Max range, mainly checking that nothing crashes.
rng.RandomInRange(0, std::numeric_limits<uint64_t>::max());
// Invalid range representation. Should swap the bounds.
const uint64_t rand_u64_4 = rng.RandomInRange(1235, 1234);
EXPECT_GE(rand_u64_4, 1234ul);
EXPECT_LE(rand_u64_4, 1235ul);
}
TEST_P(CdmRandomGeneratorTest, RandomDataLength) {
const unsigned int seed = GetParam();
CdmRandomGenerator rng(seed);
const std::string empty_data = rng.RandomData(0);
EXPECT_EQ(empty_data.size(), 0ul);
const std::string data = rng.RandomData(kVectorLength);
EXPECT_EQ(data.size(), kVectorLength);
const std::string max_data = rng.RandomData(kMaxRandomDataLength);
EXPECT_EQ(max_data.size(), kMaxRandomDataLength);
// Requesting data above the maximum length will result in an error,
// returning an empty string.
const std::string error_data = rng.RandomData(kAboveMaxRandomDataLength);
EXPECT_EQ(error_data.size(), 0ul);
}
TEST_P(CdmRandomGeneratorTest, Reproducibility) {
const unsigned int seed = GetParam();
CdmRandomGenerator rng(seed);
const std::string random_data_1 = rng.RandomData(kVectorLength);
// Reset generator.
rng.Seed(seed);
const std::string random_data_2 = rng.RandomData(kVectorLength);
EXPECT_EQ(random_data_1, random_data_2);
}
TEST_P(CdmRandomGeneratorTest, ThreadSafety) {
const unsigned int seed = GetParam();
CdmRandomGenerator rng(seed);
bool barrier = true;
auto thread_job = [&]() {
while (barrier) {
std::this_thread::sleep_for(std::chrono::microseconds(1));
}
for (size_t i = 0; i < kRandomTrialCount; ++i) {
rng.Rand();
}
};
std::vector<std::thread> threads;
for (size_t i = 0; i < kThreadCount; ++i) {
threads.push_back(std::thread(thread_job));
}
std::this_thread::sleep_for(std::chrono::microseconds(100));
barrier = false;
for (auto& thread : threads) {
thread.join();
}
}
INSTANTIATE_TEST_CASE_P(VariousSeeds, CdmRandomGeneratorTest,
testing::ValuesIn(kSeeds));
TEST(CdmRandomTest, AllMethods) {
CdmRandom::Rand();
CdmRandom::RandomInRange(1234, 1000000);
CdmRandom::RandomInRange(1000000);
CdmRandom::RandomData(kVectorLength);
CdmRandom::RandomBool();
}
} // namespace wvcdm