Copied OEMCrypto utils to Android.

The OEMCrypto utils have been copied over from the CDM repo.
Tests have been excluded for this CL.

Files represent a snapshot taken from http://go/wvgerrit/148270
and http://go/wvgerrit/148372.

Bug: 205902021
Change-Id: I1a58952cd1436a48974367c5436bf7296163e6f1
This commit is contained in:
Alex Dale
2022-03-21 21:22:19 -07:00
parent cff6103321
commit 4a065adc33
15 changed files with 4113 additions and 0 deletions

View File

@@ -0,0 +1,154 @@
// Copyright 2021 Google LLC. All Rights Reserved. This file and proprietary
// source code may only be used and distributed under the Widevine License
// Agreement.
//
// Reference implementation utilities of OEMCrypto APIs
//
#include "oemcrypto_key_deriver.h"
#include "log.h"
namespace wvoec {
namespace util {
namespace {
bool Derive128KeyAppend(Cmac* cmac, uint8_t counter, const uint8_t* ctx,
size_t ctx_size, std::vector<uint8_t>* derived_key) {
cmac->Reset();
if (!cmac->Update(counter)) {
return false;
}
if (!cmac->Update(ctx, ctx_size)) {
return false;
}
if (!cmac->FinalizeAppend(derived_key)) {
return false;
}
return true;
}
bool Derive128Key(Cmac* cmac, uint8_t counter, const uint8_t* ctx,
size_t ctx_size, std::vector<uint8_t>* derived_key) {
derived_key->clear();
return Derive128KeyAppend(cmac, counter, ctx, ctx_size, derived_key);
}
bool Derive256Key(Cmac* cmac, uint8_t counter_base, const uint8_t* ctx,
size_t ctx_size, std::vector<uint8_t>* derived_key) {
derived_key->clear();
if (!Derive128KeyAppend(cmac, counter_base, ctx, ctx_size, derived_key)) {
return false;
}
return Derive128KeyAppend(cmac, counter_base + 1, ctx, ctx_size, derived_key);
}
} // namespace
// static
std::unique_ptr<KeyDeriver> KeyDeriver::Create(const uint8_t* key,
size_t key_size) {
if (key == nullptr) {
LOGE("Key deriver key is null");
return std::unique_ptr<KeyDeriver>();
}
std::unique_ptr<KeyDeriver> key_deriver(new KeyDeriver());
if (!key_deriver->Init(key, key_size)) {
key_deriver.reset();
}
return key_deriver;
}
// static
std::unique_ptr<KeyDeriver> KeyDeriver::Create(
const std::vector<uint8_t>& key) {
if (key.empty()) {
LOGE("Key deriver key is empty");
return std::unique_ptr<KeyDeriver>();
}
return Create(key.data(), key.size());
}
bool KeyDeriver::Init(const uint8_t* key, size_t key_size) {
cmac_ = Cmac::Create(key, key_size);
if (!cmac_) {
LOGE("Failed to create CMAC for key deriver");
return false;
}
return true;
}
bool KeyDeriver::DeriveServerMacKey(const uint8_t* mac_key_context,
size_t mac_key_context_size,
std::vector<uint8_t>* mac_key_server) {
if (mac_key_context == nullptr) {
LOGE("Server MAC key context is null");
return false;
}
if (mac_key_server == nullptr) {
LOGE("Output server MAC key buffer is null");
return false;
}
return Derive256Key(cmac_.get(), 0x01, mac_key_context, mac_key_context_size,
mac_key_server);
}
bool KeyDeriver::DeriveServerMacKey(const std::vector<uint8_t>& mac_key_context,
std::vector<uint8_t>* mac_key_server) {
if (mac_key_context.empty()) {
LOGE("Server MAC key context is empty");
return false;
}
return DeriveServerMacKey(mac_key_context.data(), mac_key_context.size(),
mac_key_server);
}
bool KeyDeriver::DeriveClientMacKey(const uint8_t* mac_key_context,
size_t mac_key_context_size,
std::vector<uint8_t>* mac_key_client) {
if (mac_key_context == nullptr) {
LOGE("Client MAC key context is null");
return false;
}
if (mac_key_client == nullptr) {
LOGE("Output client MAC key buffer is null");
return false;
}
return Derive256Key(cmac_.get(), 0x03, mac_key_context, mac_key_context_size,
mac_key_client);
}
bool KeyDeriver::DeriveClientMacKey(const std::vector<uint8_t>& mac_key_context,
std::vector<uint8_t>* mac_key_client) {
if (mac_key_context.empty()) {
LOGE("Client MAC key context is empty");
return false;
}
return DeriveClientMacKey(mac_key_context.data(), mac_key_context.size(),
mac_key_client);
}
bool KeyDeriver::DeriveEncryptionKey(const uint8_t* enc_key_context,
size_t enc_key_context_size,
std::vector<uint8_t>* enc_key) {
if (enc_key_context == nullptr) {
LOGE("Encryption key context is null");
return false;
}
if (enc_key == nullptr) {
LOGE("Output encryption key buffer is null");
return false;
}
return Derive128Key(cmac_.get(), 0x01, enc_key_context, enc_key_context_size,
enc_key);
}
bool KeyDeriver::DeriveEncryptionKey(
const std::vector<uint8_t>& enc_key_context,
std::vector<uint8_t>* enc_key) {
if (enc_key_context.empty()) {
LOGE("Encryption key context is empty");
return false;
}
return DeriveEncryptionKey(enc_key_context.data(), enc_key_context.size(),
enc_key);
}
} // namespace util
} // namespace wvoec