blob: 6d28f176bdfc853976df260a8d3ddf39f26835c6 [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;
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_; }
Shawn Willden3d943322018-01-02 18:55:47 -0700104 static const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700105 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
121 ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
122 const HidlBuf& app_data, HidlBuf* key_material);
123 ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
124
125 ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
126 ErrorCode DeleteKey(bool keep_key_blob = false);
127
128 ErrorCode DeleteAllKeys();
129
130 void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
131 void CheckedDeleteKey();
132
133 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
134 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
135 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
136
137 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
138 AuthorizationSet* out_params, OperationHandle* op_handle);
139 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
140 AuthorizationSet* out_params);
141 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
142
143 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
144 const string& input, AuthorizationSet* out_params, string* output,
145 size_t* input_consumed);
146 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
147
148 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
149 const string& input, const string& signature, AuthorizationSet* out_params,
150 string* output);
151 ErrorCode Finish(const string& message, string* output);
152 ErrorCode Finish(const string& message, const string& signature, string* output);
153 ErrorCode Finish(string* output) { return Finish(string(), output); }
154
155 ErrorCode Abort(OperationHandle op_handle);
156
157 void AbortIfNeeded();
158
159 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
160 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
161 ErrorCode AttestKey(const AuthorizationSet& attest_params,
162 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
163
164 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
165 const AuthorizationSet& in_params, AuthorizationSet* out_params);
166
167 string SignMessage(const HidlBuf& key_blob, const string& message,
168 const AuthorizationSet& params);
169 string SignMessage(const string& message, const AuthorizationSet& params);
170
171 string MacMessage(const string& message, Digest digest, size_t mac_length);
172
173 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
174 const string& expected_mac);
175
176 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
177 const string& expected_ciphertext);
178
179 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
180 PaddingMode padding_mode, const string& key, const string& iv,
181 const string& input, const string& expected_output);
182
183 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
184 const AuthorizationSet& params);
185 void VerifyMessage(const string& message, const string& signature,
186 const AuthorizationSet& params);
187
188 string EncryptMessage(const HidlBuf& key_blob, const string& message,
189 const AuthorizationSet& in_params, AuthorizationSet* out_params);
190 string EncryptMessage(const string& message, const AuthorizationSet& params,
191 AuthorizationSet* out_params);
192 string EncryptMessage(const string& message, const AuthorizationSet& params);
193 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
194 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
195 HidlBuf* iv_out);
196 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
197 const HidlBuf& iv_in);
198
199 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
200 const AuthorizationSet& params);
201 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
202 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
203 const HidlBuf& iv);
204
205 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
206
207 static bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
Shawn Willden4fbc1d52018-01-05 09:16:58 -0700208 static SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700209
210 HidlBuf key_blob_;
211 KeyCharacteristics key_characteristics_;
212 OperationHandle op_handle_ = kOpHandleSentinel;
213
214 private:
215 static sp<IKeymasterDevice> keymaster_;
216 static std::vector<sp<IKeymasterDevice>> all_keymasters_;
217 static uint32_t os_version_;
218 static uint32_t os_patch_level_;
219
220 static SecurityLevel securityLevel_;
221 static hidl_string name_;
222 static hidl_string author_;
223};
224
225} // namespace test
226} // namespace V4_0
227} // namespace keymaster
228} // namespace hardware
229} // namespace android
230
231#endif // HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_