blob: 9173d5b6e412bf2fd357b5535ba3b66254242d36 [file] [log] [blame]
Jeff Tinkerb075caa2016-12-06 23:15:20 -08001/*
2 * Copyright (C) 2016 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#include "CryptoPlugin.h"
18#include "TypeConvert.h"
19
20#include <media/stagefright/foundation/AString.h>
21
22#include <hidlmemory/mapping.h>
23#include <android/hidl/memory/1.0/IMemory.h>
24
25using android::hidl::memory::V1_0::IMemory;
26
27
28namespace android {
29namespace hardware {
30namespace drm {
31namespace crypto {
32namespace V1_0 {
33namespace implementation {
34
35 // Methods from ::android::hardware::drm::crypto::V1_0::ICryptoPlugin follow
36 Return<bool> CryptoPlugin::requiresSecureDecoderComponent(
37 const hidl_string& mime) {
38 return mLegacyPlugin->requiresSecureDecoderComponent(mime);
39 }
40
41 Return<void> CryptoPlugin::notifyResolution(uint32_t width,
42 uint32_t height) {
43 mLegacyPlugin->notifyResolution(width, height);
44 return Void();
45 }
46
47 Return<Status> CryptoPlugin::setMediaDrmSession(
48 const hidl_vec<uint8_t>& sessionId) {
49 return toStatus(mLegacyPlugin->setMediaDrmSession(toVector(sessionId)));
50 }
51
52 Return<void> CryptoPlugin::decrypt(bool secure,
53 const hidl_array<uint8_t, 16>& keyId,
54 const hidl_array<uint8_t, 16>& iv, Mode mode,
55 const Pattern& pattern, const hidl_vec<SubSample>& subSamples,
56 const hidl_memory &source, const DestinationBuffer& destination,
57 decrypt_cb _hidl_cb) {
58
59 android::CryptoPlugin::Mode legacyMode;
60 switch(mode) {
61 case Mode::UNENCRYPTED:
62 legacyMode = android::CryptoPlugin::kMode_Unencrypted;
63 break;
64 case Mode::AES_CTR:
65 legacyMode = android::CryptoPlugin::kMode_AES_CTR;
66 break;
67 case Mode::AES_CBC_CTS:
68 legacyMode = android::CryptoPlugin::kMode_AES_WV;
69 break;
70 case Mode::AES_CBC:
71 legacyMode = android::CryptoPlugin::kMode_AES_CBC;
72 break;
73 }
74 android::CryptoPlugin::Pattern legacyPattern;
75 legacyPattern.mEncryptBlocks = pattern.encryptBlocks;
76 legacyPattern.mSkipBlocks = pattern.skipBlocks;
77
78 android::CryptoPlugin::SubSample *legacySubSamples =
79 new android::CryptoPlugin::SubSample[subSamples.size()];
80
81 for (size_t i = 0; i < subSamples.size(); i++) {
82 legacySubSamples[i].mNumBytesOfClearData
83 = subSamples[i].numBytesOfClearData;
84 legacySubSamples[i].mNumBytesOfEncryptedData
85 = subSamples[i].numBytesOfEncryptedData;
86 }
87
88 AString detailMessage;
89
90 void *destPtr = NULL;
91 sp<IMemory> sharedMemory;
92
93 if (destination.type == BufferType::SHARED_MEMORY) {
94 sharedMemory = mapMemory(source);
95 destPtr = sharedMemory->getPointer();
96 sharedMemory->update();
97 } else if (destination.type == BufferType::NATIVE_HANDLE) {
98 native_handle_t *handle = const_cast<native_handle_t *>(
99 destination.secureMemory.getNativeHandle());
100 destPtr = static_cast<void *>(handle);
101 }
102 ssize_t result = mLegacyPlugin->decrypt(secure, keyId.data(), iv.data(),
103 legacyMode, legacyPattern, sharedMemory->getPointer(),
104 legacySubSamples, subSamples.size(), destPtr, &detailMessage);
105
106 if (destination.type == BufferType::SHARED_MEMORY) {
107 sharedMemory->commit();
108 }
109 delete[] legacySubSamples;
110
111 uint32_t status;
112 uint32_t bytesWritten;
113
114 if (result >= 0) {
115 status = android::OK;
116 bytesWritten = result;
117 } else {
118 status = -result;
119 bytesWritten = 0;
120 }
121
Jeff Tinker01f0a5a2017-01-12 09:22:18 -0800122 _hidl_cb(toStatus(status), bytesWritten, detailMessage.c_str());
Jeff Tinkerb075caa2016-12-06 23:15:20 -0800123 return Void();
124 }
125
126} // namespace implementation
127} // namespace V1_0
128} // namespace crypto
129} // namespace drm
130} // namespace hardware
131} // namespace android