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 #ifndef CRYPTO_HAL_H_ 18 19 #define CRYPTO_HAL_H_ 20 21 #include <android/hardware/drm/1.0/ICryptoFactory.h> 22 #include <android/hardware/drm/1.0/ICryptoPlugin.h> 23 #include <android/hardware/drm/1.1/ICryptoFactory.h> 24 #include <android/hardware/drm/1.2/ICryptoPlugin.h> 25 #include <android/hardware/drm/1.4/ICryptoPlugin.h> 26 27 #include <mediadrm/ICrypto.h> 28 #include <utils/KeyedVector.h> 29 #include <utils/threads.h> 30 31 namespace drm = ::android::hardware::drm; 32 using drm::V1_0::ICryptoFactory; 33 using drm::V1_0::ICryptoPlugin; 34 using drm::V1_0::SharedBuffer; 35 using drm::V1_0::DestinationBuffer; 36 37 using ::android::hardware::HidlMemory; 38 39 class IMemoryHeap; 40 41 namespace android { 42 43 struct CryptoHal : public ICrypto { 44 CryptoHal(); 45 virtual ~CryptoHal(); 46 47 virtual status_t initCheck() const; 48 49 virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]); 50 51 virtual status_t createPlugin( 52 const uint8_t uuid[16], const void *data, size_t size); 53 54 virtual status_t destroyPlugin(); 55 56 virtual bool requiresSecureDecoderComponent( 57 const char *mime) const; 58 59 virtual void notifyResolution(uint32_t width, uint32_t height); 60 61 virtual status_t setMediaDrmSession(const Vector<uint8_t> &sessionId); 62 63 virtual ssize_t decrypt(const uint8_t key[16], const uint8_t iv[16], 64 CryptoPlugin::Mode mode, const CryptoPlugin::Pattern &pattern, 65 const ::SharedBuffer &source, size_t offset, 66 const CryptoPlugin::SubSample *subSamples, size_t numSubSamples, 67 const ::DestinationBuffer &destination, 68 AString *errorDetailMsg); 69 setHeapCryptoHal70 virtual int32_t setHeap(const sp<HidlMemory>& heap) { 71 return setHeapBase(heap); 72 } unsetHeapCryptoHal73 virtual void unsetHeap(int32_t seqNum) { clearHeapBase(seqNum); } 74 75 virtual status_t getLogMessages(Vector<drm::V1_4::LogMessage> &logs) const; 76 77 private: 78 mutable Mutex mLock; 79 80 const Vector<sp<ICryptoFactory>> mFactories; 81 sp<ICryptoPlugin> mPlugin; 82 sp<drm::V1_2::ICryptoPlugin> mPluginV1_2; 83 84 /** 85 * mInitCheck is: 86 * NO_INIT if a plugin hasn't been created yet 87 * ERROR_UNSUPPORTED if a plugin can't be created for the uuid 88 * OK after a plugin has been created and mPlugin is valid 89 */ 90 status_t mInitCheck; 91 92 KeyedVector<int32_t, size_t> mHeapSizes; 93 int32_t mHeapSeqNum; 94 95 Vector<sp<ICryptoFactory>> makeCryptoFactories(); 96 sp<ICryptoPlugin> makeCryptoPlugin(const sp<ICryptoFactory>& factory, 97 const uint8_t uuid[16], const void *initData, size_t size); 98 99 int32_t setHeapBase(const sp<HidlMemory>& heap); 100 void clearHeapBase(int32_t seqNum); 101 102 status_t checkSharedBuffer(const ::SharedBuffer& buffer); 103 104 DISALLOW_EVIL_CONSTRUCTORS(CryptoHal); 105 }; 106 107 } // namespace android 108 109 #endif // CRYPTO_HAL_H_ 110