1 /*
2  * Copyright (C) 2018 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 #ifndef CLEARKEY_CRYPTO_PLUGIN_H_
18 #define CLEARKEY_CRYPTO_PLUGIN_H_
19 
20 #include <android/hardware/drm/1.4/ICryptoPlugin.h>
21 #include <android/hidl/memory/1.0/IMemory.h>
22 
23 #include <mutex>
24 
25 #include "ClearKeyTypes.h"
26 #include "Session.h"
27 #include "Utils.h"
28 
29 namespace {
30     static const size_t KEY_ID_SIZE = 16;
31     static const size_t KEY_IV_SIZE = 16;
32 }
33 
34 namespace android {
35 namespace hardware {
36 namespace drm {
37 namespace V1_4 {
38 namespace clearkey {
39 
40 namespace drm = ::android::hardware::drm;
41 using drm::V1_0::DestinationBuffer;
42 using drm::V1_0::Mode;
43 using drm::V1_0::Pattern;
44 using drm::V1_0::SharedBuffer;
45 using drm::V1_0::Status;
46 using drm::V1_0::SubSample;
47 
48 using ::android::hardware::hidl_array;
49 using ::android::hardware::hidl_memory;
50 using ::android::hardware::hidl_string;
51 using ::android::hardware::hidl_vec;
52 using ::android::hardware::Return;
53 using ::android::hardware::Void;
54 using ::android::hidl::memory::V1_0::IMemory;
55 using ::android::sp;
56 
57 typedef drm::V1_2::Status Status_V1_2;
58 
59 struct CryptoPlugin : public drm::V1_4::ICryptoPlugin {
CryptoPluginCryptoPlugin60     explicit CryptoPlugin(const hidl_vec<uint8_t>& sessionId) {
61         mInitStatus = setMediaDrmSession(sessionId);
62     }
~CryptoPluginCryptoPlugin63     virtual ~CryptoPlugin() {}
64 
requiresSecureDecoderComponentCryptoPlugin65     Return<bool> requiresSecureDecoderComponent(const hidl_string& mime) {
66         UNUSED(mime);
67         return false;
68     }
69 
notifyResolutionCryptoPlugin70     Return<void> notifyResolution(uint32_t width, uint32_t height) {
71         UNUSED(width);
72         UNUSED(height);
73         return Void();
74     }
75 
76     Return<void> decrypt(
77             bool secure,
78             const hidl_array<uint8_t, KEY_ID_SIZE>& keyId,
79             const hidl_array<uint8_t, KEY_IV_SIZE>& iv,
80             Mode mode,
81             const Pattern& pattern,
82             const hidl_vec<SubSample>& subSamples,
83             const SharedBuffer& source,
84             uint64_t offset,
85             const DestinationBuffer& destination,
86             decrypt_cb _hidl_cb);
87 
88     Return<void> decrypt_1_2(
89             bool secure,
90             const hidl_array<uint8_t, KEY_ID_SIZE>& keyId,
91             const hidl_array<uint8_t, KEY_IV_SIZE>& iv,
92             Mode mode,
93             const Pattern& pattern,
94             const hidl_vec<SubSample>& subSamples,
95             const SharedBuffer& source,
96             uint64_t offset,
97             const DestinationBuffer& destination,
98             decrypt_1_2_cb _hidl_cb) NO_THREAD_SAFETY_ANALYSIS; // use unique_lock
99 
100     Return<void> setSharedBufferBase(const hidl_memory& base,
101             uint32_t bufferId);
102 
103     Return<Status> setMediaDrmSession(const hidl_vec<uint8_t>& sessionId);
104 
getInitStatusCryptoPlugin105     Return<Status> getInitStatus() const { return mInitStatus; }
106 
107     Return<void> getLogMessages(
108             getLogMessages_cb _hidl_cb);
109 private:
110     CLEARKEY_DISALLOW_COPY_AND_ASSIGN(CryptoPlugin);
111 
112     std::mutex mSharedBufferLock;
113     std::map<uint32_t, sp<IMemory>> mSharedBufferMap GUARDED_BY(mSharedBufferLock);
114     sp<Session> mSession;
115     Status mInitStatus;
116 };
117 
118 } // namespace clearkey
119 } // namespace V1_4
120 } // namespace drm
121 } // namespace hardware
122 } // namespace android
123 
124 #endif // CLEARKEY_CRYPTO_PLUGIN_H_
125