1/*
2 * Copyright (C) 2017 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#ifndef HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
18#define HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
19
20#include <android/hardware/keymaster/4.0/IKeymasterDevice.h>
21#include <android/hardware/keymaster/4.0/types.h>
22
23#include <VtsHalHidlTargetTestBase.h>
24
25#include <keymaster/keymaster_configuration.h>
26
27#include <keymasterV4_0/authorization_set.h>
28
29namespace android {
30namespace hardware {
31namespace keymaster {
32namespace V4_0 {
33
34::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
35
36namespace test {
37
38using ::android::sp;
39using ::std::string;
40
41class HidlBuf : public hidl_vec<uint8_t> {
42    typedef hidl_vec<uint8_t> super;
43
44   public:
45    HidlBuf() {}
46    HidlBuf(const super& other) : super(other) {}
47    HidlBuf(super&& other) : super(std::move(other)) {}
48    explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; }
49
50    HidlBuf& operator=(const super& other) {
51        super::operator=(other);
52        return *this;
53    }
54
55    HidlBuf& operator=(super&& other) {
56        super::operator=(std::move(other));
57        return *this;
58    }
59
60    HidlBuf& operator=(const string& other) {
61        resize(other.size());
62        std::copy(other.begin(), other.end(), begin());
63        return *this;
64    }
65
66    string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); }
67};
68
69constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
70
71class KeymasterHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
72   public:
73    // get the test environment singleton
74    static KeymasterHidlEnvironment* Instance() {
75        static KeymasterHidlEnvironment* instance = new KeymasterHidlEnvironment;
76        return instance;
77    }
78
79    void registerTestServices() override { registerTestService<IKeymasterDevice>(); }
80
81   private:
82    KeymasterHidlEnvironment(){};
83
84    GTEST_DISALLOW_COPY_AND_ASSIGN_(KeymasterHidlEnvironment);
85};
86
87class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
88   public:
89    void TearDown() override {
90        if (key_blob_.size()) {
91            CheckedDeleteKey();
92        }
93        AbortIfNeeded();
94    }
95
96    // SetUpTestCase runs only once per test case, not once per test.
97    static void SetUpTestCase();
98    static void TearDownTestCase() {
99        keymaster_.clear();
100        all_keymasters_.clear();
101    }
102
103    static IKeymasterDevice& keymaster() { return *keymaster_; }
104    static const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
105    static uint32_t os_version() { return os_version_; }
106    static uint32_t os_patch_level() { return os_patch_level_; }
107
108    ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
109                          KeyCharacteristics* key_characteristics);
110    ErrorCode GenerateKey(const AuthorizationSet& key_desc);
111
112    ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
113                        const string& key_material, HidlBuf* key_blob,
114                        KeyCharacteristics* key_characteristics);
115    ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
116                        const string& key_material);
117
118    ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
119                               const AuthorizationSet& wrapping_key_desc, string masking_key,
120                               const AuthorizationSet& unwrapping_params);
121
122    ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
123                        const HidlBuf& app_data, HidlBuf* key_material);
124    ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
125
126    ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
127    ErrorCode DeleteKey(bool keep_key_blob = false);
128
129    ErrorCode DeleteAllKeys();
130
131    void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
132    void CheckedDeleteKey();
133
134    ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
135                                 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
136    ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
137
138    ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
139                    AuthorizationSet* out_params, OperationHandle* op_handle);
140    ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
141                    AuthorizationSet* out_params);
142    ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
143
144    ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
145                     const string& input, AuthorizationSet* out_params, string* output,
146                     size_t* input_consumed);
147    ErrorCode Update(const string& input, string* out, size_t* input_consumed);
148
149    ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
150                     const string& input, const string& signature, AuthorizationSet* out_params,
151                     string* output);
152    ErrorCode Finish(const string& message, string* output);
153    ErrorCode Finish(const string& message, const string& signature, string* output);
154    ErrorCode Finish(string* output) { return Finish(string(), output); }
155
156    ErrorCode Abort(OperationHandle op_handle);
157
158    void AbortIfNeeded();
159
160    ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
161                        hidl_vec<hidl_vec<uint8_t>>* cert_chain);
162    ErrorCode AttestKey(const AuthorizationSet& attest_params,
163                        hidl_vec<hidl_vec<uint8_t>>* cert_chain);
164
165    string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
166                          const AuthorizationSet& in_params, AuthorizationSet* out_params);
167
168    string SignMessage(const HidlBuf& key_blob, const string& message,
169                       const AuthorizationSet& params);
170    string SignMessage(const string& message, const AuthorizationSet& params);
171
172    string MacMessage(const string& message, Digest digest, size_t mac_length);
173
174    void CheckHmacTestVector(const string& key, const string& message, Digest digest,
175                             const string& expected_mac);
176
177    void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
178                               const string& expected_ciphertext);
179
180    void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
181                                  PaddingMode padding_mode, const string& key, const string& iv,
182                                  const string& input, const string& expected_output);
183
184    void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
185                       const AuthorizationSet& params);
186    void VerifyMessage(const string& message, const string& signature,
187                       const AuthorizationSet& params);
188
189    string EncryptMessage(const HidlBuf& key_blob, const string& message,
190                          const AuthorizationSet& in_params, AuthorizationSet* out_params);
191    string EncryptMessage(const string& message, const AuthorizationSet& params,
192                          AuthorizationSet* out_params);
193    string EncryptMessage(const string& message, const AuthorizationSet& params);
194    string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
195    string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
196                          HidlBuf* iv_out);
197    string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
198                          const HidlBuf& iv_in);
199
200    string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
201                          const AuthorizationSet& params);
202    string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
203    string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
204                          const HidlBuf& iv);
205
206    std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
207
208    static bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
209    static SecurityLevel SecLevel() { return securityLevel_; }
210
211    std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
212    std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
213
214    std::vector<EcCurve> ValidCurves();
215    std::vector<EcCurve> InvalidCurves();
216
217    std::initializer_list<Digest> ValidDigests(bool withNone, bool withMD5);
218    std::vector<Digest> InvalidDigests();
219
220    HidlBuf key_blob_;
221    KeyCharacteristics key_characteristics_;
222    OperationHandle op_handle_ = kOpHandleSentinel;
223
224   private:
225    static sp<IKeymasterDevice> keymaster_;
226    static std::vector<sp<IKeymasterDevice>> all_keymasters_;
227    static uint32_t os_version_;
228    static uint32_t os_patch_level_;
229
230    static SecurityLevel securityLevel_;
231    static hidl_string name_;
232    static hidl_string author_;
233};
234
235}  // namespace test
236}  // namespace V4_0
237}  // namespace keymaster
238}  // namespace hardware
239}  // namespace android
240
241#endif  // HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
242