1 /*
2  * Copyright (C) 2012 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 #include <media/stagefright/MediaErrors.h>
18 #include <utils/Errors.h>
19 #include <utils/Vector.h>
20 
21 #ifndef CRYPTO_API_H_
22 
23 #define CRYPTO_API_H_
24 
25 namespace android {
26 
27 struct AString;
28 struct CryptoPlugin;
29 
30 struct CryptoFactory {
CryptoFactoryCryptoFactory31     CryptoFactory() {}
~CryptoFactoryCryptoFactory32     virtual ~CryptoFactory() {}
33 
34     virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) const = 0;
35 
36     virtual status_t createPlugin(
37             const uint8_t uuid[16], const void *data, size_t size,
38             CryptoPlugin **plugin) = 0;
39 
40 private:
41     CryptoFactory(const CryptoFactory &);
42     CryptoFactory &operator=(const CryptoFactory &);
43 };
44 
45 struct CryptoPlugin {
46     enum Mode {
47         kMode_Unencrypted = 0,
48         kMode_AES_CTR     = 1,
49 
50         // Neither key nor iv are being used in this mode.
51         // Each subsample is encrypted w/ an iv of all zeroes.
52         kMode_AES_WV      = 2,  // FIX constant
53     };
54 
55     struct SubSample {
56         uint32_t mNumBytesOfClearData;
57         uint32_t mNumBytesOfEncryptedData;
58     };
59 
CryptoPluginCryptoPlugin60     CryptoPlugin() {}
~CryptoPluginCryptoPlugin61     virtual ~CryptoPlugin() {}
62 
63     // If this method returns false, a non-secure decoder will be used to
64     // decode the data after decryption. The decrypt API below will have
65     // to support insecure decryption of the data (secure = false) for
66     // media data of the given mime type.
67     virtual bool requiresSecureDecoderComponent(const char *mime) const = 0;
68 
69     // To implement resolution constraints, the crypto plugin needs to know
70     // the resolution of the video being decrypted.  The media player should
71     // call this method when the resolution is determined and any time it
72     // is subsequently changed.
73 
notifyResolutionCryptoPlugin74     virtual void notifyResolution(uint32_t /* width */, uint32_t /* height */) {}
75 
76     // A MediaDrm session may be associated with a MediaCrypto session.  The
77     // associated MediaDrm session is used to load decryption keys
78     // into the crypto/drm plugin.  The keys are then referenced by key-id
79     // in the 'key' parameter to the decrypt() method.
80     // Should return NO_ERROR on success, ERROR_DRM_SESSION_NOT_OPENED if
81     // the session is not opened and a code from MediaErrors.h otherwise.
setMediaDrmSessionCryptoPlugin82     virtual status_t setMediaDrmSession(const Vector<uint8_t> & /*sessionId */) {
83         return ERROR_UNSUPPORTED;
84     }
85 
86     // If the error returned falls into the range
87     // ERROR_DRM_VENDOR_MIN..ERROR_DRM_VENDOR_MAX, errorDetailMsg should be
88     // filled in with an appropriate string.
89     // At the java level these special errors will then trigger a
90     // MediaCodec.CryptoException that gives clients access to both
91     // the error code and the errorDetailMsg.
92     // Returns a non-negative result to indicate the number of bytes written
93     // to the dstPtr, or a negative result to indicate an error.
94     virtual ssize_t decrypt(
95             bool secure,
96             const uint8_t key[16],
97             const uint8_t iv[16],
98             Mode mode,
99             const void *srcPtr,
100             const SubSample *subSamples, size_t numSubSamples,
101             void *dstPtr,
102             AString *errorDetailMsg) = 0;
103 
104 private:
105     CryptoPlugin(const CryptoPlugin &);
106     CryptoPlugin &operator=(const CryptoPlugin &);
107 };
108 
109 }  // namespace android
110 
111 extern "C" {
112     extern android::CryptoFactory *createCryptoFactory();
113 }
114 
115 #endif  // CRYPTO_API_H_
116