CryptoAPI.h revision cf0db31c323676ba9cfab81c7032a37c09256e48
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 <utils/Errors.h>
18
19#ifndef CRYPTO_API_H_
20
21#define CRYPTO_API_H_
22
23namespace android {
24
25struct CryptoPlugin;
26
27struct CryptoFactory {
28    CryptoFactory() {}
29    virtual ~CryptoFactory() {}
30
31    virtual bool isCryptoSchemeSupported(const uint8_t uuid[16]) const = 0;
32
33    virtual status_t createPlugin(
34            const uint8_t uuid[16], const void *data, size_t size,
35            CryptoPlugin **plugin) = 0;
36
37private:
38    CryptoFactory(const CryptoFactory &);
39    CryptoFactory &operator=(const CryptoFactory &);
40};
41
42struct CryptoPlugin {
43    enum Mode {
44        kMode_Unencrypted = 0,
45        kMode_AES_CTR     = 1,
46
47        // Neither key nor iv are being used in this mode.
48        // Each subsample is encrypted w/ an iv of all zeroes.
49        kMode_AES_WV      = 2,  // FIX constant
50    };
51
52    struct SubSample {
53        size_t mNumBytesOfClearData;
54        size_t mNumBytesOfEncryptedData;
55    };
56
57    CryptoPlugin() {}
58    virtual ~CryptoPlugin() {}
59
60    // If this method returns false, a non-secure decoder will be used to
61    // decode the data after decryption. The decrypt API below will have
62    // to support insecure decryption of the data (secure = false) for
63    // media data of the given mime type.
64    virtual bool requiresSecureDecoderComponent(const char *mime) const = 0;
65
66    virtual status_t decrypt(
67            bool secure,
68            const uint8_t key[16],
69            const uint8_t iv[16],
70            Mode mode,
71            const void *srcPtr,
72            const SubSample *subSamples, size_t numSubSamples,
73            void *dstPtr) = 0;
74
75private:
76    CryptoPlugin(const CryptoPlugin &);
77    CryptoPlugin &operator=(const CryptoPlugin &);
78};
79
80}  // namespace android
81
82extern "C" {
83    extern android::CryptoFactory *createCryptoFactory();
84}
85
86#endif  // CRYPTO_API_H_
87