blob: 4491f2bbbee5b309f9afe66ae8bc77cbef29a885 [file] [log] [blame]
Andreas Hubered3e3e02012-03-26 11:13:27 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "Crypto"
19#include <utils/Log.h>
20
21#include "Crypto.h"
22
Andreas Huber1bd139a2012-04-03 14:19:20 -070023#include <media/hardware/CryptoAPI.h>
24#include <media/stagefright/foundation/ADebug.h>
25#include <media/stagefright/foundation/hexdump.h>
Andreas Hubered3e3e02012-03-26 11:13:27 -070026#include <media/stagefright/MediaErrors.h>
27
Andreas Huber1bd139a2012-04-03 14:19:20 -070028#include <dlfcn.h>
29
Andreas Hubered3e3e02012-03-26 11:13:27 -070030namespace android {
31
Andreas Huber1bd139a2012-04-03 14:19:20 -070032Crypto::Crypto()
33 : mInitCheck(NO_INIT),
34 mLibHandle(NULL),
35 mPlugin(NULL) {
36 mInitCheck = init();
Andreas Hubered3e3e02012-03-26 11:13:27 -070037}
38
39Crypto::~Crypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -070040 delete mPlugin;
41 mPlugin = NULL;
42
43 delete mFactory;
44 mFactory = NULL;
45
46 if (mLibHandle != NULL) {
47 dlclose(mLibHandle);
48 mLibHandle = NULL;
49 }
Andreas Hubered3e3e02012-03-26 11:13:27 -070050}
51
Andreas Huber1bd139a2012-04-03 14:19:20 -070052status_t Crypto::initCheck() const {
53 return mInitCheck;
Andreas Hubered3e3e02012-03-26 11:13:27 -070054}
55
Andreas Huber1bd139a2012-04-03 14:19:20 -070056status_t Crypto::init() {
57 mLibHandle = dlopen("libdrmdecrypt.so", RTLD_NOW);
58
59 if (mLibHandle == NULL) {
60 return ERROR_UNSUPPORTED;
61 }
62
63 typedef CryptoFactory *(*CreateCryptoFactoryFunc)();
64 CreateCryptoFactoryFunc createCryptoFactory =
65 (CreateCryptoFactoryFunc)dlsym(mLibHandle, "createCryptoFactory");
66
67 if (createCryptoFactory == NULL
68 || ((mFactory = createCryptoFactory()) == NULL)) {
69 dlclose(mLibHandle);
70 mLibHandle = NULL;
71
72 return ERROR_UNSUPPORTED;
73 }
74
75 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -070076}
77
Andreas Huber1bd139a2012-04-03 14:19:20 -070078bool Crypto::isCryptoSchemeSupported(const uint8_t uuid[16]) const {
79 Mutex::Autolock autoLock(mLock);
80
81 if (mInitCheck != OK) {
82 return false;
83 }
84
85 return mFactory->isCryptoSchemeSupported(uuid);
Andreas Hubered3e3e02012-03-26 11:13:27 -070086}
87
Andreas Huber1bd139a2012-04-03 14:19:20 -070088status_t Crypto::createPlugin(
89 const uint8_t uuid[16], const void *data, size_t size) {
90 Mutex::Autolock autoLock(mLock);
91
92 if (mInitCheck != OK) {
93 return mInitCheck;
94 }
95
96 if (mPlugin != NULL) {
97 return -EINVAL;
98 }
99
100 return mFactory->createPlugin(uuid, data, size, &mPlugin);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700101}
102
Andreas Huber1bd139a2012-04-03 14:19:20 -0700103status_t Crypto::destroyPlugin() {
104 Mutex::Autolock autoLock(mLock);
105
106 if (mInitCheck != OK) {
107 return mInitCheck;
108 }
109
110 if (mPlugin == NULL) {
111 return -EINVAL;
112 }
113
114 delete mPlugin;
115 mPlugin = NULL;
116
117 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700118}
119
Andreas Huber1bd139a2012-04-03 14:19:20 -0700120bool Crypto::requiresSecureDecoderComponent(const char *mime) const {
121 Mutex::Autolock autoLock(mLock);
122
123 if (mInitCheck != OK) {
124 return mInitCheck;
125 }
126
127 if (mPlugin == NULL) {
128 return -EINVAL;
129 }
130
131 return mPlugin->requiresSecureDecoderComponent(mime);
132}
133
134status_t Crypto::decrypt(
135 bool secure,
136 const uint8_t key[16],
137 const uint8_t iv[16],
138 CryptoPlugin::Mode mode,
139 const void *srcPtr,
140 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
141 void *dstPtr) {
142 Mutex::Autolock autoLock(mLock);
143
144 if (mInitCheck != OK) {
145 return mInitCheck;
146 }
147
148 if (mPlugin == NULL) {
149 return -EINVAL;
150 }
151
152 return mPlugin->decrypt(
153 secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700154}
155
156} // namespace android