blob: 7166820d68b53f1c4f8d0d6031704beef1abe4d8 [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
136 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
137 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
138 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
139
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600140 ErrorCode GetDebugInfo(DebugInfo* debug_info);
141
Shawn Willden252233d2018-01-02 15:13:46 -0700142 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
143 AuthorizationSet* out_params, OperationHandle* op_handle);
144 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
145 AuthorizationSet* out_params);
146 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
147
148 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
149 const string& input, AuthorizationSet* out_params, string* output,
150 size_t* input_consumed);
151 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
152
153 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
154 const string& input, const string& signature, AuthorizationSet* out_params,
155 string* output);
156 ErrorCode Finish(const string& message, string* output);
157 ErrorCode Finish(const string& message, const string& signature, string* output);
158 ErrorCode Finish(string* output) { return Finish(string(), output); }
159
160 ErrorCode Abort(OperationHandle op_handle);
161
162 void AbortIfNeeded();
163
164 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
165 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
166 ErrorCode AttestKey(const AuthorizationSet& attest_params,
167 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
168
169 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
170 const AuthorizationSet& in_params, AuthorizationSet* out_params);
171
172 string SignMessage(const HidlBuf& key_blob, const string& message,
173 const AuthorizationSet& params);
174 string SignMessage(const string& message, const AuthorizationSet& params);
175
176 string MacMessage(const string& message, Digest digest, size_t mac_length);
177
178 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
179 const string& expected_mac);
180
181 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
182 const string& expected_ciphertext);
183
184 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
185 PaddingMode padding_mode, const string& key, const string& iv,
186 const string& input, const string& expected_output);
187
188 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
189 const AuthorizationSet& params);
190 void VerifyMessage(const string& message, const string& signature,
191 const AuthorizationSet& params);
192
193 string EncryptMessage(const HidlBuf& key_blob, const string& message,
194 const AuthorizationSet& in_params, AuthorizationSet* out_params);
195 string EncryptMessage(const string& message, const AuthorizationSet& params,
196 AuthorizationSet* out_params);
197 string EncryptMessage(const string& message, const AuthorizationSet& params);
198 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
199 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
200 HidlBuf* iv_out);
201 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
202 const HidlBuf& iv_in);
203
204 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
205 const AuthorizationSet& params);
206 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
207 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
208 const HidlBuf& iv);
209
210 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
211
212 static bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
Shawn Willden4fbc1d52018-01-05 09:16:58 -0700213 static SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700214
nagendra modadugubbe92632018-06-05 11:05:19 -0700215 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
216 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
217
218 std::vector<EcCurve> ValidCurves();
219 std::vector<EcCurve> InvalidCurves();
220
Yi Kong73921752018-09-21 15:30:45 -0700221 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700222 std::vector<Digest> InvalidDigests();
223
Shawn Willden252233d2018-01-02 15:13:46 -0700224 HidlBuf key_blob_;
225 KeyCharacteristics key_characteristics_;
226 OperationHandle op_handle_ = kOpHandleSentinel;
227
228 private:
229 static sp<IKeymasterDevice> keymaster_;
230 static std::vector<sp<IKeymasterDevice>> all_keymasters_;
231 static uint32_t os_version_;
232 static uint32_t os_patch_level_;
233
234 static SecurityLevel securityLevel_;
235 static hidl_string name_;
236 static hidl_string author_;
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600237 static string service_name_;
Shawn Willden252233d2018-01-02 15:13:46 -0700238};
239
240} // namespace test
241} // namespace V4_0
242} // namespace keymaster
243} // namespace hardware
244} // namespace android
245
246#endif // HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_