blob: b09da45cf28e3b1129bfe8243343c73931f503bf [file] [log] [blame]
Shawn Willden252233d2018-01-02 15:13:46 -07001/*
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;
Rob Barnesbd37c3b2019-09-06 12:53:17 -060040using hidl::base::V1_0::DebugInfo;
Shawn Willden252233d2018-01-02 15:13:46 -070041
42class HidlBuf : public hidl_vec<uint8_t> {
43 typedef hidl_vec<uint8_t> super;
44
45 public:
46 HidlBuf() {}
47 HidlBuf(const super& other) : super(other) {}
48 HidlBuf(super&& other) : super(std::move(other)) {}
49 explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; }
50
51 HidlBuf& operator=(const super& other) {
52 super::operator=(other);
53 return *this;
54 }
55
56 HidlBuf& operator=(super&& other) {
57 super::operator=(std::move(other));
58 return *this;
59 }
60
61 HidlBuf& operator=(const string& other) {
62 resize(other.size());
63 std::copy(other.begin(), other.end(), begin());
64 return *this;
65 }
66
67 string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); }
68};
69
70constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
71
72class KeymasterHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
73 public:
74 // get the test environment singleton
75 static KeymasterHidlEnvironment* Instance() {
76 static KeymasterHidlEnvironment* instance = new KeymasterHidlEnvironment;
77 return instance;
78 }
79
80 void registerTestServices() override { registerTestService<IKeymasterDevice>(); }
81
82 private:
83 KeymasterHidlEnvironment(){};
84
85 GTEST_DISALLOW_COPY_AND_ASSIGN_(KeymasterHidlEnvironment);
86};
87
88class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
89 public:
90 void TearDown() override {
91 if (key_blob_.size()) {
92 CheckedDeleteKey();
93 }
94 AbortIfNeeded();
95 }
96
97 // SetUpTestCase runs only once per test case, not once per test.
98 static void SetUpTestCase();
Rob Barnesbd37c3b2019-09-06 12:53:17 -060099 static void InitializeKeymaster();
Shawn Willden252233d2018-01-02 15:13:46 -0700100 static void TearDownTestCase() {
101 keymaster_.clear();
102 all_keymasters_.clear();
103 }
104
105 static IKeymasterDevice& keymaster() { return *keymaster_; }
Shawn Willden3d943322018-01-02 18:55:47 -0700106 static const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700107 static uint32_t os_version() { return os_version_; }
108 static uint32_t os_patch_level() { return os_patch_level_; }
109
110 ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
111 KeyCharacteristics* key_characteristics);
112 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
113
114 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
115 const string& key_material, HidlBuf* key_blob,
116 KeyCharacteristics* key_characteristics);
117 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
118 const string& key_material);
119
120 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
Shawn Willden8d28efa2018-01-19 13:37:42 -0700121 const AuthorizationSet& wrapping_key_desc, string masking_key,
122 const AuthorizationSet& unwrapping_params);
Shawn Willden252233d2018-01-02 15:13:46 -0700123
124 ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
125 const HidlBuf& app_data, HidlBuf* key_material);
126 ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
127
128 ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
129 ErrorCode DeleteKey(bool keep_key_blob = false);
130
131 ErrorCode DeleteAllKeys();
132
133 void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
134 void CheckedDeleteKey();
135
Max Bires873d8892019-02-08 12:29:58 -0800136 static void CheckCreationDateTime(const AuthorizationSet& sw_enforced,
137 std::chrono::time_point<std::chrono::system_clock> creation);
138
139 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
140 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
Shawn Willden252233d2018-01-02 15:13:46 -0700141 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
142 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
143 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
144
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600145 ErrorCode GetDebugInfo(DebugInfo* debug_info);
146
Shawn Willden252233d2018-01-02 15:13:46 -0700147 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
148 AuthorizationSet* out_params, OperationHandle* op_handle);
149 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
150 AuthorizationSet* out_params);
151 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
152
153 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
154 const string& input, AuthorizationSet* out_params, string* output,
155 size_t* input_consumed);
156 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
157
158 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
159 const string& input, const string& signature, AuthorizationSet* out_params,
160 string* output);
161 ErrorCode Finish(const string& message, string* output);
162 ErrorCode Finish(const string& message, const string& signature, string* output);
163 ErrorCode Finish(string* output) { return Finish(string(), output); }
164
165 ErrorCode Abort(OperationHandle op_handle);
166
167 void AbortIfNeeded();
168
169 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
170 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
171 ErrorCode AttestKey(const AuthorizationSet& attest_params,
172 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
173
174 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
175 const AuthorizationSet& in_params, AuthorizationSet* out_params);
176
177 string SignMessage(const HidlBuf& key_blob, const string& message,
178 const AuthorizationSet& params);
179 string SignMessage(const string& message, const AuthorizationSet& params);
180
181 string MacMessage(const string& message, Digest digest, size_t mac_length);
182
183 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
184 const string& expected_mac);
185
186 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
187 const string& expected_ciphertext);
188
189 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
190 PaddingMode padding_mode, const string& key, const string& iv,
191 const string& input, const string& expected_output);
192
193 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
194 const AuthorizationSet& params);
195 void VerifyMessage(const string& message, const string& signature,
196 const AuthorizationSet& params);
197
198 string EncryptMessage(const HidlBuf& key_blob, const string& message,
199 const AuthorizationSet& in_params, AuthorizationSet* out_params);
200 string EncryptMessage(const string& message, const AuthorizationSet& params,
201 AuthorizationSet* out_params);
202 string EncryptMessage(const string& message, const AuthorizationSet& params);
203 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
204 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
205 HidlBuf* iv_out);
206 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
207 const HidlBuf& iv_in);
Rob Barnes8ddc1c72019-09-13 18:00:44 -0600208 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
209 uint8_t mac_length_bits, const HidlBuf& iv_in);
Shawn Willden252233d2018-01-02 15:13:46 -0700210
211 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
212 const AuthorizationSet& params);
213 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
214 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
215 const HidlBuf& iv);
216
217 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
218
219 static bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
Shawn Willden4fbc1d52018-01-05 09:16:58 -0700220 static SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700221
nagendra modadugubbe92632018-06-05 11:05:19 -0700222 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
223 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
224
225 std::vector<EcCurve> ValidCurves();
226 std::vector<EcCurve> InvalidCurves();
227
Yi Kong73921752018-09-21 15:30:45 -0700228 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700229 std::vector<Digest> InvalidDigests();
230
Shawn Willden252233d2018-01-02 15:13:46 -0700231 HidlBuf key_blob_;
232 KeyCharacteristics key_characteristics_;
233 OperationHandle op_handle_ = kOpHandleSentinel;
234
235 private:
236 static sp<IKeymasterDevice> keymaster_;
237 static std::vector<sp<IKeymasterDevice>> all_keymasters_;
238 static uint32_t os_version_;
239 static uint32_t os_patch_level_;
240
241 static SecurityLevel securityLevel_;
242 static hidl_string name_;
243 static hidl_string author_;
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600244 static string service_name_;
Shawn Willden252233d2018-01-02 15:13:46 -0700245};
246
247} // namespace test
248} // namespace V4_0
249} // namespace keymaster
250} // namespace hardware
251} // namespace android
252
253#endif // HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_