blob: 1fe6bede8c192245925aead7260daaa7fcf6cd64 [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 "ICrypto"
19#include <utils/Log.h>
20
21#include <binder/Parcel.h>
22#include <media/ICrypto.h>
23#include <media/stagefright/foundation/ADebug.h>
24
25namespace android {
26
27enum {
Andreas Huber1bd139a2012-04-03 14:19:20 -070028 INIT_CHECK = IBinder::FIRST_CALL_TRANSACTION,
29 IS_CRYPTO_SUPPORTED,
30 CREATE_PLUGIN,
31 DESTROY_PLUGIN,
32 REQUIRES_SECURE_COMPONENT,
33 DECRYPT,
Andreas Hubered3e3e02012-03-26 11:13:27 -070034};
35
36struct BpCrypto : public BpInterface<ICrypto> {
37 BpCrypto(const sp<IBinder> &impl)
38 : BpInterface<ICrypto>(impl) {
39 }
40
Andreas Huber1bd139a2012-04-03 14:19:20 -070041 virtual status_t initCheck() const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070042 Parcel data, reply;
43 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070044 remote()->transact(INIT_CHECK, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070045
46 return reply.readInt32();
47 }
48
Andreas Huber1bd139a2012-04-03 14:19:20 -070049 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070050 Parcel data, reply;
51 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070052 data.write(uuid, 16);
53 remote()->transact(IS_CRYPTO_SUPPORTED, data, &reply);
54
55 return reply.readInt32() != 0;
56 }
57
58 virtual status_t createPlugin(
59 const uint8_t uuid[16], const void *opaqueData, size_t opaqueSize) {
60 Parcel data, reply;
61 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
62 data.write(uuid, 16);
63 data.writeInt32(opaqueSize);
64 data.write(opaqueData, opaqueSize);
65 remote()->transact(CREATE_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070066
67 return reply.readInt32();
68 }
69
Andreas Huber1bd139a2012-04-03 14:19:20 -070070 virtual status_t destroyPlugin() {
Andreas Hubered3e3e02012-03-26 11:13:27 -070071 Parcel data, reply;
72 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070073 remote()->transact(DESTROY_PLUGIN, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070074
75 return reply.readInt32();
76 }
77
Andreas Huber1bd139a2012-04-03 14:19:20 -070078 virtual bool requiresSecureDecoderComponent(
79 const char *mime) const {
Andreas Hubered3e3e02012-03-26 11:13:27 -070080 Parcel data, reply;
81 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070082 data.writeCString(mime);
83 remote()->transact(REQUIRES_SECURE_COMPONENT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -070084
Andreas Huber1bd139a2012-04-03 14:19:20 -070085 return reply.readInt32() != 0;
Andreas Hubered3e3e02012-03-26 11:13:27 -070086 }
87
Andreas Huber1bd139a2012-04-03 14:19:20 -070088 virtual status_t decrypt(
89 bool secure,
90 const uint8_t key[16],
91 const uint8_t iv[16],
92 CryptoPlugin::Mode mode,
93 const void *srcPtr,
94 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples,
95 void *dstPtr) {
Andreas Hubered3e3e02012-03-26 11:13:27 -070096 Parcel data, reply;
97 data.writeInterfaceToken(ICrypto::getInterfaceDescriptor());
Andreas Huber1bd139a2012-04-03 14:19:20 -070098 data.writeInt32(secure);
99 data.writeInt32(mode);
100 data.write(key, 16);
101 data.write(iv, 16);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700102
Andreas Huber1bd139a2012-04-03 14:19:20 -0700103 size_t totalSize = 0;
104 for (size_t i = 0; i < numSubSamples; ++i) {
105 totalSize += subSamples[i].mNumBytesOfEncryptedData;
106 totalSize += subSamples[i].mNumBytesOfClearData;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700107 }
108
Andreas Huber1bd139a2012-04-03 14:19:20 -0700109 data.writeInt32(totalSize);
110 data.write(srcPtr, totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700111
Andreas Huber1bd139a2012-04-03 14:19:20 -0700112 data.writeInt32(numSubSamples);
113 data.write(subSamples, sizeof(CryptoPlugin::SubSample) * numSubSamples);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700114
Andreas Huber1bd139a2012-04-03 14:19:20 -0700115 if (secure) {
116 data.writeIntPtr((intptr_t)dstPtr);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700117 }
118
Andreas Huber1bd139a2012-04-03 14:19:20 -0700119 remote()->transact(DECRYPT, data, &reply);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700120
Andreas Huber1bd139a2012-04-03 14:19:20 -0700121 status_t result = reply.readInt32();
Andreas Hubered3e3e02012-03-26 11:13:27 -0700122
Andreas Huber1bd139a2012-04-03 14:19:20 -0700123 if (result != OK) {
124 return result;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700125 }
126
Andreas Huber1bd139a2012-04-03 14:19:20 -0700127 if (!secure) {
128 reply.read(dstPtr, totalSize);
129 }
Andreas Hubered3e3e02012-03-26 11:13:27 -0700130
Andreas Huber1bd139a2012-04-03 14:19:20 -0700131 return OK;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700132 }
133
134private:
135 DISALLOW_EVIL_CONSTRUCTORS(BpCrypto);
136};
137
138IMPLEMENT_META_INTERFACE(Crypto, "android.hardware.ICrypto");
139
140////////////////////////////////////////////////////////////////////////////////
141
142status_t BnCrypto::onTransact(
143 uint32_t code, const Parcel &data, Parcel *reply, uint32_t flags) {
144 switch (code) {
Andreas Huber1bd139a2012-04-03 14:19:20 -0700145 case INIT_CHECK:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700146 {
147 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700148 reply->writeInt32(initCheck());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700149
150 return OK;
151 }
152
Andreas Huber1bd139a2012-04-03 14:19:20 -0700153 case IS_CRYPTO_SUPPORTED:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700154 {
155 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700156 uint8_t uuid[16];
157 data.read(uuid, sizeof(uuid));
158 reply->writeInt32(isCryptoSchemeSupported(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700159
160 return OK;
161 }
162
Andreas Huber1bd139a2012-04-03 14:19:20 -0700163 case CREATE_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700164 {
165 CHECK_INTERFACE(ICrypto, data, reply);
166
Andreas Huber1bd139a2012-04-03 14:19:20 -0700167 uint8_t uuid[16];
168 data.read(uuid, sizeof(uuid));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700169
Andreas Huber1bd139a2012-04-03 14:19:20 -0700170 size_t opaqueSize = data.readInt32();
171 void *opaqueData = malloc(opaqueSize);
172 data.read(opaqueData, opaqueSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700173
Andreas Huber1bd139a2012-04-03 14:19:20 -0700174 reply->writeInt32(createPlugin(uuid, opaqueData, opaqueSize));
175
176 free(opaqueData);
177 opaqueData = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700178
179 return OK;
180 }
181
Andreas Huber1bd139a2012-04-03 14:19:20 -0700182 case DESTROY_PLUGIN:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700183 {
184 CHECK_INTERFACE(ICrypto, data, reply);
Andreas Huber1bd139a2012-04-03 14:19:20 -0700185 reply->writeInt32(destroyPlugin());
Andreas Hubered3e3e02012-03-26 11:13:27 -0700186
187 return OK;
188 }
189
Andreas Huber1bd139a2012-04-03 14:19:20 -0700190 case REQUIRES_SECURE_COMPONENT:
Andreas Hubered3e3e02012-03-26 11:13:27 -0700191 {
192 CHECK_INTERFACE(ICrypto, data, reply);
193
Andreas Huber1bd139a2012-04-03 14:19:20 -0700194 const char *mime = data.readCString();
195 reply->writeInt32(requiresSecureDecoderComponent(mime));
Andreas Hubered3e3e02012-03-26 11:13:27 -0700196
Andreas Huber1bd139a2012-04-03 14:19:20 -0700197 return OK;
198 }
199
200 case DECRYPT:
201 {
202 CHECK_INTERFACE(ICrypto, data, reply);
203
204 bool secure = data.readInt32() != 0;
205 CryptoPlugin::Mode mode = (CryptoPlugin::Mode)data.readInt32();
206
207 uint8_t key[16];
208 data.read(key, sizeof(key));
209
210 uint8_t iv[16];
211 data.read(iv, sizeof(iv));
212
213 size_t totalSize = data.readInt32();
214 void *srcData = malloc(totalSize);
215 data.read(srcData, totalSize);
216
217 int32_t numSubSamples = data.readInt32();
218
219 CryptoPlugin::SubSample *subSamples =
220 new CryptoPlugin::SubSample[numSubSamples];
221
222 data.read(
223 subSamples,
224 sizeof(CryptoPlugin::SubSample) * numSubSamples);
225
226 void *dstPtr;
227 if (secure) {
228 dstPtr = (void *)data.readIntPtr();
229 } else {
230 dstPtr = malloc(totalSize);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700231 }
232
Andreas Huber1bd139a2012-04-03 14:19:20 -0700233 status_t err = decrypt(
234 secure,
235 key,
236 iv,
237 mode,
238 srcData,
239 subSamples, numSubSamples,
240 dstPtr);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700241
Andreas Huber1bd139a2012-04-03 14:19:20 -0700242 reply->writeInt32(err);
Andreas Hubered3e3e02012-03-26 11:13:27 -0700243
Andreas Huber1bd139a2012-04-03 14:19:20 -0700244 if (!secure) {
245 if (err == OK) {
246 reply->write(dstPtr, totalSize);
247 }
248
249 free(dstPtr);
250 dstPtr = NULL;
251 }
252
253 delete[] subSamples;
254 subSamples = NULL;
Andreas Hubered3e3e02012-03-26 11:13:27 -0700255
256 free(srcData);
257 srcData = NULL;
258
Andreas Hubered3e3e02012-03-26 11:13:27 -0700259 return OK;
260 }
261
262 default:
263 return BBinder::onTransact(code, data, reply, flags);
264 }
265}
266
267} // namespace android
268