Source release 16.2.0
This commit is contained in:
46
util/include/advance_iv_ctr.h
Normal file
46
util/include/advance_iv_ctr.h
Normal file
@@ -0,0 +1,46 @@
|
||||
// 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_UTIL_ADVANCE_IV_CTR_H_
|
||||
#define WVCDM_UTIL_ADVANCE_IV_CTR_H_
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "string_conversions.h"
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// Advance an IV according to ISO-CENC's CTR modes. The lower half of the IV is
|
||||
// split off and treated as an unsigned 64-bit integer, then incremented by the
|
||||
// number of complete crypto blocks decrypted. The resulting value is then
|
||||
// copied back into the IV over the previous lower half.
|
||||
inline void AdvanceIvCtr(uint8_t (*subsample_iv)[16], size_t bytes) {
|
||||
constexpr size_t kAesBlockSize = 16;
|
||||
constexpr size_t kIvSize = kAesBlockSize;
|
||||
constexpr size_t kCounterIndex = kIvSize / 2;
|
||||
constexpr size_t kCounterSize = kIvSize / 2;
|
||||
|
||||
uint64_t counter;
|
||||
|
||||
static_assert(
|
||||
sizeof(*subsample_iv) == kIvSize,
|
||||
"The subsample_iv field is no longer the length of an AES-128 IV.");
|
||||
static_assert(sizeof(counter) == kCounterSize,
|
||||
"A uint64_t failed to be half the size of an AES-128 IV.");
|
||||
|
||||
// Defensive copy because the elements of the array may not be properly
|
||||
// aligned
|
||||
memcpy(&counter, &(*subsample_iv)[kCounterIndex], kCounterSize);
|
||||
|
||||
const size_t increment =
|
||||
bytes / kAesBlockSize; // The truncation here is intentional
|
||||
counter = htonll64(ntohll64(counter) + increment);
|
||||
|
||||
memcpy(&(*subsample_iv)[kCounterIndex], &counter, kCounterSize);
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_ADVANCE_IV_CTR_H_
|
||||
20
util/include/arraysize.h
Normal file
20
util/include/arraysize.h
Normal file
@@ -0,0 +1,20 @@
|
||||
// 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_UTIL_ARRAYSIZE_H_
|
||||
#define WVCDM_UTIL_ARRAYSIZE_H_
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
namespace wvcdm {
|
||||
|
||||
// Returns the size of a fixed-length array.
|
||||
template <typename T, size_t N>
|
||||
constexpr size_t ArraySize(const T (&)[N]) {
|
||||
return N;
|
||||
}
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_ARRAYSIZE_H_
|
||||
117
util/include/cdm_random.h
Normal file
117
util/include/cdm_random.h
Normal file
@@ -0,0 +1,117 @@
|
||||
// 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. The generator is designed to
|
||||
// meet the C++ named requirement UniformRandomBitGenerator to allow it to
|
||||
// be used with standard library functions / class which are designed to
|
||||
// work with the standard library generators.
|
||||
class CdmRandomGenerator {
|
||||
public:
|
||||
// Result type of operator().
|
||||
using result_type = unsigned int;
|
||||
// Inclusive boundaries of operator().
|
||||
static constexpr unsigned int min() { return 0; }
|
||||
static constexpr unsigned int max() { return RAND_MAX; }
|
||||
|
||||
// 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 [min(), max()].
|
||||
unsigned int Rand();
|
||||
|
||||
// Allows for RNG to be callable.
|
||||
unsigned 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 unsigned 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_
|
||||
@@ -11,7 +11,6 @@ namespace wvcdm {
|
||||
TypeName(const TypeName&); \
|
||||
void operator=(const TypeName&)
|
||||
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
#endif // WVCDM_UTIL_DISALLOW_COPY_AND_ASSIGN_H_
|
||||
|
||||
@@ -33,22 +33,21 @@ extern LogPriority g_cutoff;
|
||||
// unit tests.
|
||||
CORE_UTIL_EXPORT void InitLogging();
|
||||
|
||||
CORE_UTIL_EXPORT void Log(
|
||||
const char* file, const char* function, int line, LogPriority level,
|
||||
const char* fmt, ...);
|
||||
CORE_UTIL_EXPORT void Log(const char* file, const char* function, int line,
|
||||
LogPriority level, const char* fmt, ...);
|
||||
|
||||
// Log APIs
|
||||
#ifndef LOGE
|
||||
#define LOGE(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_ERROR, __VA_ARGS__)
|
||||
#define LOGW(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_WARN, __VA_ARGS__)
|
||||
#define LOGI(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_INFO, __VA_ARGS__)
|
||||
#define LOGD(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_DEBUG, __VA_ARGS__)
|
||||
#define LOGV(...) Log(__FILE__, __func__, __LINE__, \
|
||||
wvcdm::LOG_VERBOSE, __VA_ARGS__)
|
||||
# define LOGE(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_ERROR, __VA_ARGS__)
|
||||
# define LOGW(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_WARN, __VA_ARGS__)
|
||||
# define LOGI(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_INFO, __VA_ARGS__)
|
||||
# define LOGD(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_DEBUG, __VA_ARGS__)
|
||||
# define LOGV(...) \
|
||||
Log(__FILE__, __func__, __LINE__, wvcdm::LOG_VERBOSE, __VA_ARGS__)
|
||||
#endif
|
||||
} // namespace wvcdm
|
||||
|
||||
|
||||
@@ -10,24 +10,22 @@
|
||||
#include "util_common.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
# include <wtypes.h>
|
||||
# include <BaseTsd.h>
|
||||
# include <winsock2.h> // For htonl and ntohl.
|
||||
# define __PRETTY_FUNCTION__ __FUNCTION__
|
||||
# undef NO_ERROR
|
||||
# undef GetCurrentTime
|
||||
# undef DeleteFile
|
||||
# include <BaseTsd.h>
|
||||
# include <winsock2.h> // For htonl and ntohl.
|
||||
# include <wtypes.h>
|
||||
# define __PRETTY_FUNCTION__ __FUNCTION__
|
||||
# undef NO_ERROR
|
||||
# undef GetCurrentTime
|
||||
# undef DeleteFile
|
||||
|
||||
using ssize_t = SSIZE_T;
|
||||
|
||||
inline void sleep(int seconds) {
|
||||
Sleep(seconds * 1000);
|
||||
}
|
||||
inline void sleep(int seconds) { Sleep(seconds * 1000); }
|
||||
CORE_UTIL_EXPORT int setenv(const char* key, const char* value, int overwrite);
|
||||
#else
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
# include <arpa/inet.h>
|
||||
# include <sys/types.h>
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#endif // WVCDM_UTIL_PLATFORM_H_
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -35,6 +36,8 @@ CORE_UTIL_EXPORT std::string IntToString(int value);
|
||||
CORE_UTIL_EXPORT int64_t htonll64(int64_t x);
|
||||
CORE_UTIL_EXPORT inline int64_t ntohll64(int64_t x) { return htonll64(x); }
|
||||
CORE_UTIL_EXPORT std::string BytesToString(const uint8_t* bytes, unsigned size);
|
||||
// Encode unsigned integer into a big endian formatted string
|
||||
CORE_UTIL_EXPORT std::string EncodeUint32(unsigned int u);
|
||||
|
||||
} // namespace wvcdm
|
||||
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
#define WVCDM_UTIL_UTIL_COMMON_H_
|
||||
|
||||
#ifdef _WIN32
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __declspec(dllexport)
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT __declspec(dllimport)
|
||||
# endif
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __declspec(dllexport)
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT __declspec(dllimport)
|
||||
# endif
|
||||
#else
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT
|
||||
# endif
|
||||
# ifdef CORE_UTIL_IMPLEMENTATION
|
||||
# define CORE_UTIL_EXPORT __attribute__((visibility("default")))
|
||||
# else
|
||||
# define CORE_UTIL_EXPORT
|
||||
# endif
|
||||
#endif
|
||||
|
||||
#endif // WVCDM_UTIL_UTIL_COMMON_H_
|
||||
|
||||
Reference in New Issue
Block a user