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 #include <binder/IMemory.h>
17 #include <binder/IServiceManager.h>
18 #include <binder/MemoryBase.h>
19 #include <binder/MemoryHeapBase.h>
20 #include <media/hardware/CryptoAPI.h>
21 #include <mediadrm/ICrypto.h>
22 #include <mediadrm/IMediaDrmService.h>
23 #include <sys/types.h>
24 #include <sys/wait.h>
25 #include <utils/StrongPointer.h>
26 
27 #include <stdio.h>
28 #include <unistd.h>
29 
30 using namespace android;
31 
main()32 int main()
33 {
34   sp<IServiceManager> sm = defaultServiceManager();
35   sp<IBinder> binder = sm->getService(String16("media.drm"));
36   sp<IMediaDrmService> service = interface_cast<IMediaDrmService>(binder);
37   if (service == NULL) {
38     printf("Failed to retrieve 'media.drm' service.\n");
39     return 1;
40   }
41   sp<ICrypto> crypto = service->makeCrypto();
42   if (crypto == NULL) {
43     printf("makeCrypto failed.\n");
44     return 1;
45   }
46 
47   const uint8_t clearkey_uuid[16] = {
48     0x10, 0x77, 0xEF, 0xEC, 0xC0, 0xB2, 0x4D, 0x02,
49     0xAC, 0xE3, 0x3C, 0x1E, 0x52, 0xE2, 0xFB, 0x4B
50   };
51   if (crypto->createPlugin(clearkey_uuid, NULL, 0) != OK) {
52     printf("createPlugin failed.\n");
53     return 1;
54   }
55 
56   sp<MemoryHeapBase> heap = new MemoryHeapBase(0x2000);
57   memset(heap->getBase(), 'A', 0x2000);
58   sp<MemoryBase> sourceMemory = new MemoryBase(heap, 0, 0x2000);
59   sp<MemoryBase> destMemory = new MemoryBase(heap, 0x1fff, 1);
60   int heapSeqNum = crypto->setHeap(heap);
61   if (heapSeqNum < 0) {
62     printf("setHeap failed.\n");
63     return 1;
64   }
65 
66   CryptoPlugin::Pattern pattern = { .mEncryptBlocks = 0, .mSkipBlocks = 1 };
67   ICrypto::SourceBuffer source = { .mSharedMemory = sourceMemory,
68     .mHeapSeqNum = heapSeqNum };
69   CryptoPlugin::SubSample subSamples[1] = { { .mNumBytesOfClearData = 0x2000,
70       .mNumBytesOfEncryptedData = 0 } };
71   ICrypto::DestinationBuffer destination = {
72     .mType = ICrypto::kDestinationTypeSharedMemory, .mHandle = NULL, .mSharedMemory = destMemory
73   };
74 
75   int val = crypto->decrypt(NULL, NULL,
76       CryptoPlugin::kMode_Unencrypted, pattern, source, 0, subSamples, 1,
77       destination, NULL);
78 
79   if (val != BAD_VALUE) {
80     printf("OVERFLOW DETECTED\n");
81   }
82 
83   return 0;
84 }
85