• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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         kMode_AES_WV      = 2,
50         kMode_AES_CBC     = 3,
51     };
52 
53     struct SubSample {
54         uint32_t mNumBytesOfClearData;
55         uint32_t mNumBytesOfEncryptedData;
56     };
57 
58     struct Pattern {
59         // Number of blocks to be encrypted in the pattern. If zero, pattern
60         // encryption is inoperative.
61         uint32_t mEncryptBlocks;
62 
63         // Number of blocks to be skipped (left clear) in the pattern. If zero,
64         // pattern encryption is inoperative.
65         uint32_t mSkipBlocks;
66     };
67 
CryptoPluginCryptoPlugin68     CryptoPlugin() {}
~CryptoPluginCryptoPlugin69     virtual ~CryptoPlugin() {}
70 
71     // If this method returns false, a non-secure decoder will be used to
72     // decode the data after decryption. The decrypt API below will have
73     // to support insecure decryption of the data (secure = false) for
74     // media data of the given mime type.
75     virtual bool requiresSecureDecoderComponent(const char *mime) const = 0;
76 
77     // To implement resolution constraints, the crypto plugin needs to know
78     // the resolution of the video being decrypted.  The media player should
79     // call this method when the resolution is determined and any time it
80     // is subsequently changed.
81 
notifyResolutionCryptoPlugin82     virtual void notifyResolution(uint32_t /* width */, uint32_t /* height */) {}
83 
84     // A MediaDrm session may be associated with a MediaCrypto session.  The
85     // associated MediaDrm session is used to load decryption keys
86     // into the crypto/drm plugin.  The keys are then referenced by key-id
87     // in the 'key' parameter to the decrypt() method.
88     // Should return NO_ERROR on success, ERROR_DRM_SESSION_NOT_OPENED if
89     // the session is not opened and a code from MediaErrors.h otherwise.
setMediaDrmSessionCryptoPlugin90     virtual status_t setMediaDrmSession(const Vector<uint8_t> & /*sessionId */) {
91         return ERROR_UNSUPPORTED;
92     }
93 
94     // If the error returned falls into the range
95     // ERROR_DRM_VENDOR_MIN..ERROR_DRM_VENDOR_MAX, errorDetailMsg should be
96     // filled in with an appropriate string.
97     // At the java level these special errors will then trigger a
98     // MediaCodec.CryptoException that gives clients access to both
99     // the error code and the errorDetailMsg.
100     // Returns a non-negative result to indicate the number of bytes written
101     // to the dstPtr, or a negative result to indicate an error.
102     virtual ssize_t decrypt(
103             bool secure,
104             const uint8_t key[16],
105             const uint8_t iv[16],
106             Mode mode,
107             const Pattern &pattern,
108             const void *srcPtr,
109             const SubSample *subSamples, size_t numSubSamples,
110             void *dstPtr,
111             AString *errorDetailMsg) = 0;
112 
113 private:
114     CryptoPlugin(const CryptoPlugin &);
115     CryptoPlugin &operator=(const CryptoPlugin &);
116 };
117 
118 }  // namespace android
119 
120 extern "C" {
121     extern android::CryptoFactory *createCryptoFactory();
122 }
123 
124 #endif  // CRYPTO_API_H_
125