This is the initial code drop of the reference implementation and test cases for the Widevine Whitebox API. In this drop, the full reference implementation for the AEAD white-box is provided and all test cases verifying the top-level behave have are enabled. Since the implementations can vary so much the testing is mostly left to verifying the return codes for specific parameter conditions. A full reference implementation for the license white-box is provided, however not all tests are implemented or enabled. A number of tests have been disabled as they required a loaded license and test licenses are still being worked on. The two license white-box API functions that are the further from competition are ProcessLicenseResponse() and MaskedDecryt(). ProcessLicenseResponse() is still being worked on and MaskedDecrypt() is waiting on Decrypt() to be fully functional. Most tests focus on verifying return code for specific parameter conditions, but as test licenses are created, tests looking to test the internal behaviour of license management will be added to ProcessLicenseResponse(), Decrypt(), and MaskedDecrypt().
64 lines
1.7 KiB
C++
64 lines
1.7 KiB
C++
// Copyright 2020 Google LLC. All Rights Reserved.
|
|
|
|
#include "base/base64.h"
|
|
|
|
#include <stddef.h>
|
|
#include <stdint.h>
|
|
|
|
namespace base {
|
|
|
|
namespace {
|
|
|
|
static const char kBase64Codes[] =
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
|
// Calculates a/b using round-up division (only works for numbers
|
|
// greater than 0).
|
|
constexpr size_t CeilDivide(size_t a, size_t b) {
|
|
return ((a - 1) / b) + 1;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
void Base64Encode(const std::string& input, std::string* output) {
|
|
if (input.empty()) {
|
|
output->clear();
|
|
return;
|
|
}
|
|
|
|
// |temp| stores a 24-bit block that is treated as an array where insertions
|
|
// occur from high to low.
|
|
uint32_t temp = 0;
|
|
size_t out_index = 0;
|
|
const size_t out_size = CeilDivide(input.size(), 3) * 4;
|
|
std::string result(out_size, '\0');
|
|
for (size_t i = 0; i < input.size(); i++) {
|
|
// "insert" 8-bits of data
|
|
temp = (temp << 8) | (input[i] & 0x0FF);
|
|
|
|
if (i % 3 == 2) {
|
|
result[out_index++] = kBase64Codes[(temp >> 18) & 0x3f];
|
|
result[out_index++] = kBase64Codes[(temp >> 12) & 0x3f];
|
|
result[out_index++] = kBase64Codes[(temp >> 6) & 0x3f];
|
|
result[out_index++] = kBase64Codes[temp & 0x3f];
|
|
temp = 0;
|
|
}
|
|
}
|
|
|
|
if (input.size() % 3 == 1) {
|
|
result[out_index++] = kBase64Codes[(temp >> 18) & 0x3f];
|
|
result[out_index++] = kBase64Codes[(temp >> 12) & 0x3f];
|
|
result[out_index++] = '=';
|
|
result[out_index++] = '=';
|
|
} else if (input.size() % 3 == 2) {
|
|
result[out_index++] = kBase64Codes[(temp >> 18) & 0x3f];
|
|
result[out_index++] = kBase64Codes[(temp >> 12) & 0x3f];
|
|
result[out_index++] = kBase64Codes[(temp >> 6) & 0x3f];
|
|
result[out_index++] = '=';
|
|
}
|
|
|
|
output->swap(result);
|
|
}
|
|
|
|
} // namespace base
|