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