From 6473768dcb4fa0cf80da5a000f9e79d71258b050 Mon Sep 17 00:00:00 2001 From: Andreas Huber Date: Mon, 23 Apr 2012 08:16:52 -0700 Subject: [PATCH] [WVDRM] Support new Java API crypto HAL Implements WV crypto plugin core lib build Change-Id: I4f3fb06a4a9cf7c9c663d3dcee9cfd2c2900ebc2 related-to-bug: 5986621 --- proprietary/cryptoPlugin/Android.mk | 13 ++ proprietary/cryptoPlugin/WVCryptoPlugin.cpp | 146 ++++++++++++++++++++ proprietary/cryptoPlugin/WVCryptoPlugin.h | 89 ++++++++++++ proprietary/cryptoPlugin/decrypt-core.mk | 14 ++ 4 files changed, 262 insertions(+) create mode 100644 proprietary/cryptoPlugin/Android.mk create mode 100644 proprietary/cryptoPlugin/WVCryptoPlugin.cpp create mode 100644 proprietary/cryptoPlugin/WVCryptoPlugin.h create mode 100644 proprietary/cryptoPlugin/decrypt-core.mk diff --git a/proprietary/cryptoPlugin/Android.mk b/proprietary/cryptoPlugin/Android.mk new file mode 100644 index 00000000..7367d19c --- /dev/null +++ b/proprietary/cryptoPlugin/Android.mk @@ -0,0 +1,13 @@ +LOCAL_PATH := $(call my-dir) +include $(CLEAR_VARS) + +LOCAL_SRC_FILES:= \ + WVCryptoPlugin.cpp + +LOCAL_C_INCLUDES := \ + $(TOP)/vendor/widevine/proprietary/wvm/include \ + +LOCAL_MODULE:= libwvdecryptcommon +LOCAL_MODULE_TAGS := optional + +include $(BUILD_STATIC_LIBRARY) diff --git a/proprietary/cryptoPlugin/WVCryptoPlugin.cpp b/proprietary/cryptoPlugin/WVCryptoPlugin.cpp new file mode 100644 index 00000000..54869cc8 --- /dev/null +++ b/proprietary/cryptoPlugin/WVCryptoPlugin.cpp @@ -0,0 +1,146 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//#define LOG_NDEBUG 0 +#define LOG_TAG "wv_crypto_plugin" +#include + +#include "WVCryptoPlugin.h" + +#include +#include + +#include + +#include + +namespace android { + +// static +const uint8_t WVCryptoFactory::kUUIDWidevine[16] = { + 0xED,0xEF,0x8B,0xA9,0x79,0xD6,0x4A,0xCE, + 0xA3,0xC8,0x27,0xDC,0xD5,0x1D,0x21,0xED +}; + +WVCryptoPlugin::WVCryptoPlugin(const void *data, size_t size) + : mInitCheck(NO_INIT) { + + // not using data at this time, require + // size to be zero. + if (size > 0) { + mInitCheck = -EINVAL; + } else { + OEMCryptoResult res = OEMCrypto_Initialize(); + if (res != OEMCrypto_SUCCESS) { + ALOGE("OEMCrypto_Initialize failed: %d", res); + mInitCheck = -EINVAL; + } else { + mInitCheck = OK; + } + } +} + +WVCryptoPlugin::~WVCryptoPlugin() { + if (mInitCheck == OK) { + OEMCryptoResult res = OEMCrypto_Terminate(); + if (res != OEMCrypto_SUCCESS) { + ALOGW("OEMCrypto_Terminate failed: %d", res); + } + } +} + +status_t WVCryptoPlugin::initCheck() const { + return mInitCheck; +} + +bool WVCryptoPlugin::requiresSecureDecoderComponent(const char *mime) const { + return !strncasecmp(mime, "video/", 6); +} + +status_t WVCryptoPlugin::decrypt( + bool secure, + const uint8_t key[16], + const uint8_t iv[16], + Mode mode, + const void *srcPtr, + const SubSample *subSamples, size_t numSubSamples, + void *dstPtr, + AString *errorDetailMsg) { + Mutex::Autolock autoLock(mLock); + + //ALOGD("mode=%d, secure=%d, numSubSamples=%d", mode, secure, numSubSamples); + CHECK(mode == kMode_Unencrypted || mode == kMode_AES_WV); + + size_t offset = 0; + for (size_t i = 0; i < numSubSamples; ++i) { + const SubSample &ss = subSamples[i]; + + size_t srcSize; + + if (mode == kMode_Unencrypted) { + srcSize = ss.mNumBytesOfClearData; + CHECK_EQ(ss.mNumBytesOfEncryptedData, 0u); + } else { + CHECK_EQ(ss.mNumBytesOfClearData, 0u); + srcSize = ss.mNumBytesOfEncryptedData; + } + + //ALOGD("size[%d]=%d", i, srcSize); + + OEMCrypto_UINT32 dstLength = srcSize; + + OEMCryptoResult res; + + OEMCrypto_UINT8 _iv[16]; + const OEMCrypto_UINT8 *iv = NULL; + + if (mode != kMode_Unencrypted) { + memset(_iv, 0, sizeof(_iv)); + iv = _iv; + } + + if (secure) { + //ALOGD("calling DecryptVideo, size=%d", srcSize); + res = OEMCrypto_DecryptVideo( + iv, + (const OEMCrypto_UINT8 *)srcPtr + offset, + srcSize, + (OEMCrypto_UINT32)dstPtr, + offset, + &dstLength); + } else { + //ALOGD("calling DecryptAudio: size=%d", srcSize); + res = OEMCrypto_DecryptAudio( + iv, + (const OEMCrypto_UINT8 *)srcPtr + offset, + srcSize, + (OEMCrypto_UINT8 *)dstPtr + offset, + &dstLength); + } + + if (res != OEMCrypto_SUCCESS) { + ALOGE("decrypt result: %d", res); + return -EINVAL; + } + + offset += dstLength; + } + + return OK; +} + +} // namespace android + diff --git a/proprietary/cryptoPlugin/WVCryptoPlugin.h b/proprietary/cryptoPlugin/WVCryptoPlugin.h new file mode 100644 index 00000000..acbf80ee --- /dev/null +++ b/proprietary/cryptoPlugin/WVCryptoPlugin.h @@ -0,0 +1,89 @@ +/* + * Copyright (C) 2012 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef WV_CRYPTO_PLUGIN_H_ + +#define WV_CRYPTO_PLUGIN_H_ + +#include +#include + +namespace android { + +struct WVCryptoPlugin : public CryptoPlugin { + WVCryptoPlugin(const void *data, size_t size); + virtual ~WVCryptoPlugin(); + + status_t initCheck() const; + + virtual bool requiresSecureDecoderComponent(const char *mime) const; + + virtual status_t decrypt( + bool secure, + const uint8_t key[16], + const uint8_t iv[16], + Mode mode, + const void *srcPtr, + const SubSample *subSamples, size_t numSubSamples, + void *dstPtr, + AString *errorDetailMsg); + +private: + Mutex mLock; + + status_t mInitCheck; + + WVCryptoPlugin(const WVCryptoPlugin &); + WVCryptoPlugin &operator=(const WVCryptoPlugin &); +}; + +struct WVCryptoFactory : public CryptoFactory { + static const uint8_t kUUIDWidevine[16]; + + virtual bool isCryptoSchemeSupported( + const uint8_t uuid[16]) const { + return !memcmp(uuid, kUUIDWidevine, 16); + } + + virtual status_t createPlugin( + const uint8_t uuid[16], const void *data, size_t size, + CryptoPlugin **out) { + *out = NULL; + + if (memcmp(uuid, kUUIDWidevine, 16)) { + return -ENOENT; + } + + WVCryptoPlugin *plugin = new WVCryptoPlugin(data, size); + + status_t err; + if ((err = plugin->initCheck()) != OK) { + delete plugin; + plugin = NULL; + + return err; + } + + *out = plugin; + + return OK; + } +}; + +} // namespace android + +#endif // WV_CRYPTO_PLUGIN_H_ + diff --git a/proprietary/cryptoPlugin/decrypt-core.mk b/proprietary/cryptoPlugin/decrypt-core.mk new file mode 100644 index 00000000..3911f1ac --- /dev/null +++ b/proprietary/cryptoPlugin/decrypt-core.mk @@ -0,0 +1,14 @@ +# +# To be included by platform-specific vendor Android.mk to build +# Widevine wvm static library. Sets up includes and defines the core libraries +# required. +# +include $(TOP)/vendor/widevine/proprietary/wvm/common.mk + +LOCAL_WHOLE_STATIC_LIBRARIES := \ + libwvdecryptcommon + +LOCAL_SHARED_LIBRARIES := \ + libstagefright_foundation \ + libutils \ +