1 /*
2  **
3  ** Copyright 2016, The Android Open Source Project
4  **
5  ** Licensed under the Apache License, Version 2.0 (the "License");
6  ** you may not use this file except in compliance with the License.
7  ** You may obtain a copy of the License at
8  **
9  **     http://www.apache.org/licenses/LICENSE-2.0
10  **
11  ** Unless required by applicable law or agreed to in writing, software
12  ** distributed under the License is distributed on an "AS IS" BASIS,
13  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  ** See the License for the specific language governing permissions and
15  ** limitations under the License.
16  */
17 
18 #pragma once
19 
20 #include <android/hardware/keymaster/3.0/IKeymasterDevice.h>
21 
22 #include <hidl/MQDescriptor.h>
23 #include <hidl/Status.h>
24 
25 typedef struct keymaster0_device keymaster0_device_t;
26 typedef struct keymaster1_device keymaster1_device_t;
27 typedef struct keymaster2_device keymaster2_device_t;
28 
29 namespace keymaster {
30 class AndroidKeymaster;
31 class KeymasterContext;
32 
33 namespace ng {
34 
35 using ::android::sp;
36 using ::android::hardware::hidl_string;
37 using ::android::hardware::hidl_vec;
38 using ::android::hardware::Return;
39 using ::android::hardware::Void;
40 using ::android::hardware::keymaster::V3_0::ErrorCode;
41 using ::android::hardware::keymaster::V3_0::IKeymasterDevice;
42 using ::android::hardware::keymaster::V3_0::KeyCharacteristics;
43 using ::android::hardware::keymaster::V3_0::KeyFormat;
44 using ::android::hardware::keymaster::V3_0::KeyParameter;
45 using ::android::hardware::keymaster::V3_0::KeyPurpose;
46 
47 enum class KeymasterHardwareProfile : uint32_t {
48     SW,
49     KM0,
50     KM1,
51     KM2,
52 };
53 
54 class AndroidKeymaster3Device : public IKeymasterDevice {
55   public:
56     AndroidKeymaster3Device();
57     AndroidKeymaster3Device(KeymasterContext* context, KeymasterHardwareProfile profile);
58     virtual ~AndroidKeymaster3Device();
59 
60     // Methods from ::android::hardware::keymaster::V3_0::IKeymasterDevice follow.
61     Return<void> getHardwareFeatures(getHardwareFeatures_cb _hidl_cb);
62     Return<ErrorCode> addRngEntropy(const hidl_vec<uint8_t>& data) override;
63     Return<void> generateKey(const hidl_vec<KeyParameter>& keyParams,
64                              generateKey_cb _hidl_cb) override;
65     Return<void> getKeyCharacteristics(const hidl_vec<uint8_t>& keyBlob,
66                                        const hidl_vec<uint8_t>& clientId,
67                                        const hidl_vec<uint8_t>& appData,
68                                        getKeyCharacteristics_cb _hidl_cb) override;
69     Return<void> importKey(const hidl_vec<KeyParameter>& params, KeyFormat keyFormat,
70                            const hidl_vec<uint8_t>& keyData, importKey_cb _hidl_cb) override;
71     Return<void> exportKey(KeyFormat exportFormat, const hidl_vec<uint8_t>& keyBlob,
72                            const hidl_vec<uint8_t>& clientId, const hidl_vec<uint8_t>& appData,
73                            exportKey_cb _hidl_cb) override;
74     Return<void> attestKey(const hidl_vec<uint8_t>& keyToAttest,
75                            const hidl_vec<KeyParameter>& attestParams,
76                            attestKey_cb _hidl_cb) override;
77     Return<void> upgradeKey(const hidl_vec<uint8_t>& keyBlobToUpgrade,
78                             const hidl_vec<KeyParameter>& upgradeParams,
79                             upgradeKey_cb _hidl_cb) override;
80     Return<ErrorCode> deleteKey(const hidl_vec<uint8_t>& keyBlob) override;
81     Return<ErrorCode> deleteAllKeys() override;
82     Return<ErrorCode> destroyAttestationIds() override;
83     Return<void> begin(KeyPurpose purpose, const hidl_vec<uint8_t>& key,
84                        const hidl_vec<KeyParameter>& inParams, begin_cb _hidl_cb) override;
85     Return<void> update(uint64_t operationHandle, const hidl_vec<KeyParameter>& inParams,
86                         const hidl_vec<uint8_t>& input, update_cb _hidl_cb) override;
87     Return<void> finish(uint64_t operationHandle, const hidl_vec<KeyParameter>& inParams,
88                         const hidl_vec<uint8_t>& input, const hidl_vec<uint8_t>& signature,
89                         finish_cb _hidl_cb) override;
90     Return<ErrorCode> abort(uint64_t operationHandle) override;
91 
92   private:
93     std::unique_ptr<::keymaster::AndroidKeymaster> impl_;
94     KeymasterHardwareProfile profile_;
95 };
96 
97 IKeymasterDevice* CreateKeymasterDevice();
98 
99 IKeymasterDevice* CreateKeymasterDevice(keymaster2_device_t* km2_device);
100 IKeymasterDevice* CreateKeymasterDevice(keymaster1_device_t* km1_device);
101 IKeymasterDevice* CreateKeymasterDevice(keymaster0_device_t* km0_device);
102 
103 }  // namespace ng
104 }  // namespace keymaster
105