Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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 "CryptoHal" |
| 19 | #include <utils/Log.h> |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 20 | |
| 21 | #include <android/hardware/drm/1.0/types.h> |
Steven Moreland | 7dcb4db | 2019-05-13 13:10:51 -0700 | [diff] [blame] | 22 | #include <android/hidl/manager/1.2/IServiceManager.h> |
Steven Moreland | 7dcb4db | 2019-05-13 13:10:51 -0700 | [diff] [blame] | 23 | #include <hidl/ServiceManagement.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 24 | #include <hidlmemory/FrameworkUtils.h> |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 25 | #include <media/hardware/CryptoAPI.h> |
| 26 | #include <media/stagefright/foundation/ADebug.h> |
| 27 | #include <media/stagefright/foundation/AString.h> |
| 28 | #include <media/stagefright/foundation/hexdump.h> |
| 29 | #include <media/stagefright/MediaErrors.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 30 | #include <mediadrm/CryptoHal.h> |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 31 | #include <mediadrm/DrmUtils.h> |
Jeff Tinker | 7d2c6e8 | 2018-02-16 16:14:59 -0800 | [diff] [blame] | 32 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 33 | using drm::V1_0::BufferType; |
| 34 | using drm::V1_0::DestinationBuffer; |
| 35 | using drm::V1_0::ICryptoFactory; |
| 36 | using drm::V1_0::ICryptoPlugin; |
| 37 | using drm::V1_0::Mode; |
| 38 | using drm::V1_0::Pattern; |
| 39 | using drm::V1_0::SharedBuffer; |
| 40 | using drm::V1_0::Status; |
| 41 | using drm::V1_0::SubSample; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 42 | |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 43 | using ::android::DrmUtils::toStatusT; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 44 | using ::android::hardware::hidl_array; |
| 45 | using ::android::hardware::hidl_handle; |
| 46 | using ::android::hardware::hidl_memory; |
| 47 | using ::android::hardware::hidl_string; |
| 48 | using ::android::hardware::hidl_vec; |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 49 | using ::android::hardware::HidlMemory; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 50 | using ::android::hardware::Return; |
| 51 | using ::android::hardware::Void; |
| 52 | using ::android::sp; |
| 53 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 54 | typedef drm::V1_2::Status Status_V1_2; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 55 | |
| 56 | namespace android { |
| 57 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 58 | static hidl_vec<uint8_t> toHidlVec(const Vector<uint8_t> &vector) { |
| 59 | hidl_vec<uint8_t> vec; |
| 60 | vec.setToExternal(const_cast<uint8_t *>(vector.array()), vector.size()); |
| 61 | return vec; |
| 62 | } |
| 63 | |
| 64 | static hidl_vec<uint8_t> toHidlVec(const void *ptr, size_t size) { |
| 65 | hidl_vec<uint8_t> vec; |
| 66 | vec.resize(size); |
| 67 | memcpy(vec.data(), ptr, size); |
| 68 | return vec; |
| 69 | } |
| 70 | |
| 71 | static hidl_array<uint8_t, 16> toHidlArray16(const uint8_t *ptr) { |
| 72 | if (!ptr) { |
| 73 | return hidl_array<uint8_t, 16>(); |
| 74 | } |
| 75 | return hidl_array<uint8_t, 16>(ptr); |
| 76 | } |
| 77 | |
| 78 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 79 | static String8 toString8(hidl_string hString) { |
| 80 | return String8(hString.c_str()); |
| 81 | } |
| 82 | |
| 83 | |
| 84 | CryptoHal::CryptoHal() |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 85 | : mFactories(makeCryptoFactories()), |
| 86 | mInitCheck((mFactories.size() == 0) ? ERROR_UNSUPPORTED : NO_INIT), |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 87 | mHeapSeqNum(0) { |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | CryptoHal::~CryptoHal() { |
| 91 | } |
| 92 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 93 | Vector<sp<ICryptoFactory>> CryptoHal::makeCryptoFactories() { |
| 94 | Vector<sp<ICryptoFactory>> factories; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 95 | |
Steven Moreland | 7dcb4db | 2019-05-13 13:10:51 -0700 | [diff] [blame] | 96 | auto manager = hardware::defaultServiceManager1_2(); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 97 | if (manager != NULL) { |
Steven Moreland | 7dcb4db | 2019-05-13 13:10:51 -0700 | [diff] [blame] | 98 | manager->listManifestByInterface(drm::V1_0::ICryptoFactory::descriptor, |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 99 | [&factories](const hidl_vec<hidl_string> ®istered) { |
| 100 | for (const auto &instance : registered) { |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 101 | auto factory = drm::V1_0::ICryptoFactory::getService(instance); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 102 | if (factory != NULL) { |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 103 | ALOGD("found drm@1.0 ICryptoFactory %s", instance.c_str()); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 104 | factories.push_back(factory); |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | ); |
Steven Moreland | 7dcb4db | 2019-05-13 13:10:51 -0700 | [diff] [blame] | 109 | manager->listManifestByInterface(drm::V1_1::ICryptoFactory::descriptor, |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 110 | [&factories](const hidl_vec<hidl_string> ®istered) { |
| 111 | for (const auto &instance : registered) { |
| 112 | auto factory = drm::V1_1::ICryptoFactory::getService(instance); |
| 113 | if (factory != NULL) { |
| 114 | ALOGD("found drm@1.1 ICryptoFactory %s", instance.c_str()); |
| 115 | factories.push_back(factory); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | } |
| 119 | ); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 120 | } |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 121 | |
| 122 | if (factories.size() == 0) { |
| 123 | // must be in passthrough mode, load the default passthrough service |
Jeff Tinker | e309b22 | 2017-04-05 08:01:28 -0700 | [diff] [blame] | 124 | auto passthrough = ICryptoFactory::getService(); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 125 | if (passthrough != NULL) { |
Jeff Tinker | e307dc4 | 2018-02-11 19:53:54 +0000 | [diff] [blame] | 126 | ALOGI("makeCryptoFactories: using default passthrough crypto instance"); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 127 | factories.push_back(passthrough); |
| 128 | } else { |
| 129 | ALOGE("Failed to find any crypto factories"); |
| 130 | } |
| 131 | } |
| 132 | return factories; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 133 | } |
| 134 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 135 | sp<ICryptoPlugin> CryptoHal::makeCryptoPlugin(const sp<ICryptoFactory>& factory, |
| 136 | const uint8_t uuid[16], const void *initData, size_t initDataSize) { |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 137 | |
| 138 | sp<ICryptoPlugin> plugin; |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 139 | Return<void> hResult = factory->createPlugin(toHidlArray16(uuid), |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 140 | toHidlVec(initData, initDataSize), |
| 141 | [&](Status status, const sp<ICryptoPlugin>& hPlugin) { |
| 142 | if (status != Status::OK) { |
| 143 | ALOGE("Failed to make crypto plugin"); |
| 144 | return; |
| 145 | } |
| 146 | plugin = hPlugin; |
| 147 | } |
| 148 | ); |
| 149 | return plugin; |
| 150 | } |
| 151 | |
| 152 | |
| 153 | status_t CryptoHal::initCheck() const { |
| 154 | return mInitCheck; |
| 155 | } |
| 156 | |
| 157 | |
| 158 | bool CryptoHal::isCryptoSchemeSupported(const uint8_t uuid[16]) { |
| 159 | Mutex::Autolock autoLock(mLock); |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 160 | |
| 161 | for (size_t i = 0; i < mFactories.size(); i++) { |
| 162 | if (mFactories[i]->isCryptoSchemeSupported(uuid)) { |
| 163 | return true; |
| 164 | } |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 165 | } |
| 166 | return false; |
| 167 | } |
| 168 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 169 | status_t CryptoHal::createPlugin(const uint8_t uuid[16], const void *data, |
| 170 | size_t size) { |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 171 | Mutex::Autolock autoLock(mLock); |
| 172 | |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 173 | for (size_t i = 0; i < mFactories.size(); i++) { |
| 174 | if (mFactories[i]->isCryptoSchemeSupported(uuid)) { |
| 175 | mPlugin = makeCryptoPlugin(mFactories[i], uuid, data, size); |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 176 | if (mPlugin != NULL) { |
| 177 | mPluginV1_2 = drm::V1_2::ICryptoPlugin::castFrom(mPlugin); |
| 178 | } |
Jeff Tinker | abeb36a | 2017-02-17 09:42:46 -0800 | [diff] [blame] | 179 | } |
| 180 | } |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 181 | |
| 182 | if (mPlugin == NULL) { |
| 183 | mInitCheck = ERROR_UNSUPPORTED; |
| 184 | } else { |
| 185 | mInitCheck = OK; |
| 186 | } |
| 187 | |
| 188 | return mInitCheck; |
| 189 | } |
| 190 | |
| 191 | status_t CryptoHal::destroyPlugin() { |
| 192 | Mutex::Autolock autoLock(mLock); |
| 193 | |
| 194 | if (mInitCheck != OK) { |
| 195 | return mInitCheck; |
| 196 | } |
| 197 | |
| 198 | mPlugin.clear(); |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 199 | mPluginV1_2.clear(); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 200 | return OK; |
| 201 | } |
| 202 | |
| 203 | bool CryptoHal::requiresSecureDecoderComponent(const char *mime) const { |
| 204 | Mutex::Autolock autoLock(mLock); |
| 205 | |
| 206 | if (mInitCheck != OK) { |
Jeff Tinker | cca2377 | 2018-05-07 11:41:56 -0700 | [diff] [blame] | 207 | return false; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 208 | } |
| 209 | |
Jeff Tinker | cca2377 | 2018-05-07 11:41:56 -0700 | [diff] [blame] | 210 | Return<bool> hResult = mPlugin->requiresSecureDecoderComponent(hidl_string(mime)); |
| 211 | if (!hResult.isOk()) { |
| 212 | return false; |
| 213 | } |
| 214 | return hResult; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | |
| 218 | /** |
Robert Shih | d3f9ba7 | 2019-11-20 17:25:26 -0800 | [diff] [blame] | 219 | * If the heap base isn't set, get the heap base from the HidlMemory |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 220 | * and send it to the HAL so it can map a remote heap of the same |
| 221 | * size. Once the heap base is established, shared memory buffers |
| 222 | * are sent by providing an offset into the heap and a buffer size. |
| 223 | */ |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 224 | int32_t CryptoHal::setHeapBase(const sp<HidlMemory>& heap) { |
Robert Shih | c9f7efa | 2019-07-16 16:19:59 -0700 | [diff] [blame] | 225 | if (heap == NULL || mHeapSeqNum < 0) { |
| 226 | ALOGE("setHeapBase(): heap %p mHeapSeqNum %d", heap.get(), mHeapSeqNum); |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 227 | return -1; |
| 228 | } |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 229 | |
| 230 | Mutex::Autolock autoLock(mLock); |
| 231 | |
| 232 | int32_t seqNum = mHeapSeqNum++; |
Robert Shih | c9f7efa | 2019-07-16 16:19:59 -0700 | [diff] [blame] | 233 | uint32_t bufferId = static_cast<uint32_t>(seqNum); |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 234 | mHeapSizes.add(seqNum, heap->size()); |
| 235 | Return<void> hResult = mPlugin->setSharedBufferBase(*heap, bufferId); |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 236 | ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed"); |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 237 | return seqNum; |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 238 | } |
| 239 | |
Chong Zhang | 6dcab2b | 2017-03-28 14:18:27 -0700 | [diff] [blame] | 240 | void CryptoHal::clearHeapBase(int32_t seqNum) { |
| 241 | Mutex::Autolock autoLock(mLock); |
| 242 | |
Jeff Tinker | 0db0fa1 | 2018-05-22 16:55:57 -0700 | [diff] [blame] | 243 | /* |
| 244 | * Clear the remote shared memory mapping by setting the shared |
| 245 | * buffer base to a null hidl_memory. |
| 246 | * |
| 247 | * TODO: Add a releaseSharedBuffer method in a future DRM HAL |
| 248 | * API version to make this explicit. |
| 249 | */ |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 250 | ssize_t index = mHeapSizes.indexOfKey(seqNum); |
Jeff Tinker | 20795c7 | 2018-05-30 18:20:09 -0700 | [diff] [blame] | 251 | if (index >= 0) { |
| 252 | if (mPlugin != NULL) { |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 253 | uint32_t bufferId = static_cast<uint32_t>(seqNum); |
Jeff Tinker | 20795c7 | 2018-05-30 18:20:09 -0700 | [diff] [blame] | 254 | Return<void> hResult = mPlugin->setSharedBufferBase(hidl_memory(), bufferId); |
| 255 | ALOGE_IF(!hResult.isOk(), "setSharedBufferBase(): remote call failed"); |
| 256 | } |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 257 | mHeapSizes.removeItem(seqNum); |
Jeff Tinker | 20795c7 | 2018-05-30 18:20:09 -0700 | [diff] [blame] | 258 | } |
Chong Zhang | d07c927 | 2017-03-28 11:02:06 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 261 | status_t CryptoHal::checkSharedBuffer(const ::SharedBuffer &buffer) { |
| 262 | int32_t seqNum = static_cast<int32_t>(buffer.bufferId); |
Jeff Tinker | b1f8c80 | 2018-04-19 16:23:21 -0700 | [diff] [blame] | 263 | // memory must be in one of the heaps that have been set |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 264 | if (mHeapSizes.indexOfKey(seqNum) < 0) { |
Jeff Tinker | b1f8c80 | 2018-04-19 16:23:21 -0700 | [diff] [blame] | 265 | return UNKNOWN_ERROR; |
| 266 | } |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 267 | |
Jeff Tinker | b1f8c80 | 2018-04-19 16:23:21 -0700 | [diff] [blame] | 268 | // memory must be within the address space of the heap |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 269 | size_t heapSize = mHeapSizes.valueFor(seqNum); |
| 270 | if (heapSize < buffer.offset + buffer.size || |
| 271 | SIZE_MAX - buffer.offset < buffer.size) { |
Jeff Tinker | b1f8c80 | 2018-04-19 16:23:21 -0700 | [diff] [blame] | 272 | android_errorWriteLog(0x534e4554, "76221123"); |
| 273 | return UNKNOWN_ERROR; |
| 274 | } |
| 275 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 276 | return OK; |
| 277 | } |
| 278 | |
| 279 | ssize_t CryptoHal::decrypt(const uint8_t keyId[16], const uint8_t iv[16], |
| 280 | CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern, |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 281 | const ::SharedBuffer &hSource, size_t offset, |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 282 | const CryptoPlugin::SubSample *subSamples, size_t numSubSamples, |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 283 | const ::DestinationBuffer &hDestination, AString *errorDetailMsg) { |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 284 | Mutex::Autolock autoLock(mLock); |
| 285 | |
| 286 | if (mInitCheck != OK) { |
| 287 | return mInitCheck; |
| 288 | } |
| 289 | |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 290 | Mode hMode; |
| 291 | switch(mode) { |
| 292 | case CryptoPlugin::kMode_Unencrypted: |
| 293 | hMode = Mode::UNENCRYPTED ; |
| 294 | break; |
| 295 | case CryptoPlugin::kMode_AES_CTR: |
| 296 | hMode = Mode::AES_CTR; |
| 297 | break; |
| 298 | case CryptoPlugin::kMode_AES_WV: |
| 299 | hMode = Mode::AES_CBC_CTS; |
| 300 | break; |
| 301 | case CryptoPlugin::kMode_AES_CBC: |
| 302 | hMode = Mode::AES_CBC; |
| 303 | break; |
| 304 | default: |
| 305 | return UNKNOWN_ERROR; |
| 306 | } |
| 307 | |
| 308 | Pattern hPattern; |
| 309 | hPattern.encryptBlocks = pattern.mEncryptBlocks; |
| 310 | hPattern.skipBlocks = pattern.mSkipBlocks; |
| 311 | |
| 312 | std::vector<SubSample> stdSubSamples; |
| 313 | for (size_t i = 0; i < numSubSamples; i++) { |
| 314 | SubSample subSample; |
| 315 | subSample.numBytesOfClearData = subSamples[i].mNumBytesOfClearData; |
| 316 | subSample.numBytesOfEncryptedData = subSamples[i].mNumBytesOfEncryptedData; |
| 317 | stdSubSamples.push_back(subSample); |
| 318 | } |
| 319 | auto hSubSamples = hidl_vec<SubSample>(stdSubSamples); |
| 320 | |
| 321 | bool secure; |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 322 | if (hDestination.type == BufferType::SHARED_MEMORY) { |
| 323 | status_t status = checkSharedBuffer(hDestination.nonsecureMemory); |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 324 | if (status != OK) { |
| 325 | return status; |
| 326 | } |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 327 | secure = false; |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 328 | } else if (hDestination.type == BufferType::NATIVE_HANDLE) { |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 329 | secure = true; |
Jeff Tinker | 2442047 | 2018-01-11 17:46:16 -0800 | [diff] [blame] | 330 | } else { |
| 331 | android_errorWriteLog(0x534e4554, "70526702"); |
| 332 | return UNKNOWN_ERROR; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Robert Shih | 895fba9 | 2019-07-16 16:29:44 -0700 | [diff] [blame] | 335 | status_t status = checkSharedBuffer(hSource); |
Jeff Tinker | 3cb5316 | 2017-02-16 12:06:32 -0800 | [diff] [blame] | 336 | if (status != OK) { |
| 337 | return status; |
| 338 | } |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 339 | |
| 340 | status_t err = UNKNOWN_ERROR; |
| 341 | uint32_t bytesWritten = 0; |
| 342 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 343 | Return<void> hResult; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 344 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 345 | if (mPluginV1_2 != NULL) { |
| 346 | hResult = mPluginV1_2->decrypt_1_2(secure, toHidlArray16(keyId), toHidlArray16(iv), |
| 347 | hMode, hPattern, hSubSamples, hSource, offset, hDestination, |
| 348 | [&](Status_V1_2 status, uint32_t hBytesWritten, hidl_string hDetailedError) { |
| 349 | if (status == Status_V1_2::OK) { |
| 350 | bytesWritten = hBytesWritten; |
| 351 | *errorDetailMsg = toString8(hDetailedError); |
| 352 | } |
Robert Shih | 5944a0b | 2021-02-10 04:26:33 -0800 | [diff] [blame] | 353 | err = toStatusT(status); |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 354 | } |
| 355 | ); |
| 356 | } else { |
| 357 | hResult = mPlugin->decrypt(secure, toHidlArray16(keyId), toHidlArray16(iv), |
| 358 | hMode, hPattern, hSubSamples, hSource, offset, hDestination, |
| 359 | [&](Status status, uint32_t hBytesWritten, hidl_string hDetailedError) { |
| 360 | if (status == Status::OK) { |
| 361 | bytesWritten = hBytesWritten; |
| 362 | *errorDetailMsg = toString8(hDetailedError); |
| 363 | } |
| 364 | err = toStatusT(status); |
| 365 | } |
| 366 | ); |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 367 | } |
| 368 | |
Jeff Tinker | b8684f3 | 2018-12-12 08:41:31 -0800 | [diff] [blame] | 369 | err = hResult.isOk() ? err : DEAD_OBJECT; |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 370 | if (err == OK) { |
| 371 | return bytesWritten; |
| 372 | } |
| 373 | return err; |
| 374 | } |
| 375 | |
| 376 | void CryptoHal::notifyResolution(uint32_t width, uint32_t height) { |
| 377 | Mutex::Autolock autoLock(mLock); |
| 378 | |
| 379 | if (mInitCheck != OK) { |
| 380 | return; |
| 381 | } |
| 382 | |
| 383 | mPlugin->notifyResolution(width, height); |
| 384 | } |
| 385 | |
| 386 | status_t CryptoHal::setMediaDrmSession(const Vector<uint8_t> &sessionId) { |
| 387 | Mutex::Autolock autoLock(mLock); |
| 388 | |
| 389 | if (mInitCheck != OK) { |
| 390 | return mInitCheck; |
| 391 | } |
| 392 | |
| 393 | return toStatusT(mPlugin->setMediaDrmSession(toHidlVec(sessionId))); |
| 394 | } |
| 395 | |
Robert Shih | 9afca95 | 2021-02-12 01:36:06 -0800 | [diff] [blame^] | 396 | status_t CryptoHal::getLogMessages(Vector<drm::V1_4::LogMessage> &logs) const { |
| 397 | Mutex::Autolock autoLock(mLock); |
| 398 | return DrmUtils::GetLogMessages<drm::V1_4::ICryptoPlugin>(mPlugin, logs); |
| 399 | } |
Jeff Tinker | a53d655 | 2017-01-20 00:31:46 -0800 | [diff] [blame] | 400 | } // namespace android |