blob: 574ae7175ba007547858895322aa83e1977f876a [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),
Andreas Huber16087352012-04-13 14:54:36 -070035 mFactory(NULL),
Andreas Huber1bd139a2012-04-03 14:19:20 -070036 mPlugin(NULL) {
37 mInitCheck = init();
Andreas Hubered3e3e02012-03-26 11:13:27 -070038}
39
40Crypto::~Crypto() {
Andreas Huber1bd139a2012-04-03 14:19:20 -070041 delete mPlugin;
42 mPlugin = NULL;
43
44 delete mFactory;
45 mFactory = NULL;
46
47 if (mLibHandle != NULL) {
48 dlclose(mLibHandle);
49 mLibHandle = NULL;
50 }
Andreas Hubered3e3e02012-03-26 11:13:27 -070051}
52
Andreas Huber1bd139a2012-04-03 14:19:20 -070053status_t Crypto::initCheck() const {
54 return mInitCheck;
Andreas Hubered3e3e02012-03-26 11:13:27 -070055}
56
Andreas Huber1bd139a2012-04-03 14:19:20 -070057status_t Crypto::init() {
58 mLibHandle = dlopen("libdrmdecrypt.so", RTLD_NOW);
59
60 if (mLibHandle == NULL) {
Andreas Huber16087352012-04-13 14:54:36 -070061 ALOGE("Unable to locate libdrmdecrypt.so");
62
Andreas Huber1bd139a2012-04-03 14:19:20 -070063 return ERROR_UNSUPPORTED;
64 }
65
66 typedef CryptoFactory *(*CreateCryptoFactoryFunc)();
67 CreateCryptoFactoryFunc createCryptoFactory =
68 (CreateCryptoFactoryFunc)dlsym(mLibHandle, "createCryptoFactory");
69
70 if (createCryptoFactory == NULL
71 || ((mFactory = createCryptoFactory()) == NULL)) {
Andreas Huber16087352012-04-13 14:54:36 -070072 if (createCryptoFactory == NULL) {
73 ALOGE("Unable to find symbol 'createCryptoFactory'.");
74 } else {
75 ALOGE("createCryptoFactory() failed.");
76 }
77
Andreas Huber1bd139a2012-04-03 14:19:20 -070078 dlclose(mLibHandle);
79 mLibHandle = NULL;
80
81 return ERROR_UNSUPPORTED;
82 }
83
84 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -070085}
86
Andreas Huber1bd139a2012-04-03 14:19:20 -070087bool Crypto::isCryptoSchemeSupported(const uint8_t uuid[16]) const {
88 Mutex::Autolock autoLock(mLock);
89
90 if (mInitCheck != OK) {
91 return false;
92 }
93
94 return mFactory->isCryptoSchemeSupported(uuid);
Andreas Hubered3e3e02012-03-26 11:13:27 -070095}
96
Andreas Huber1bd139a2012-04-03 14:19:20 -070097status_t Crypto::createPlugin(
98 const uint8_t uuid[16], const void *data, size_t size) {
99 Mutex::Autolock autoLock(mLock);
100
101 if (mInitCheck != OK) {
102 return mInitCheck;
103 }
104
105 if (mPlugin != NULL) {
106 return -EINVAL;
107 }
108
109 return mFactory->createPlugin(uuid, data, size, &mPlugin);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700110}
111
Andreas Huber1bd139a2012-04-03 14:19:20 -0700112status_t Crypto::destroyPlugin() {
113 Mutex::Autolock autoLock(mLock);
114
115 if (mInitCheck != OK) {
116 return mInitCheck;
117 }
118
119 if (mPlugin == NULL) {
120 return -EINVAL;
121 }
122
123 delete mPlugin;
124 mPlugin = NULL;
125
126 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700127}
128
Andreas Huber1bd139a2012-04-03 14:19:20 -0700129bool Crypto::requiresSecureDecoderComponent(const char *mime) const {
130 Mutex::Autolock autoLock(mLock);
131
132 if (mInitCheck != OK) {
133 return mInitCheck;
134 }
135
136 if (mPlugin == NULL) {
137 return -EINVAL;
138 }
139
140 return mPlugin->requiresSecureDecoderComponent(mime);
141}
142
143status_t Crypto::decrypt(
144 bool secure,
145 const uint8_t key[16],
146 const uint8_t iv[16],
147 CryptoPlugin::Mode mode,
148 const void *srcPtr,
149 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
150 void *dstPtr) {
151 Mutex::Autolock autoLock(mLock);
152
153 if (mInitCheck != OK) {
154 return mInitCheck;
155 }
156
157 if (mPlugin == NULL) {
158 return -EINVAL;
159 }
160
161 return mPlugin->decrypt(
162 secure, key, iv, mode, srcPtr, subSamples, numSubSamples, dstPtr);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700163}
164
165} // namespace android