blob: ad30aa7792392dfc1d1bb2b583ad0bf8ad1e63c4 [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
Dan Shi3bacd7f2019-12-10 15:41:18 -080017#pragma once
Shawn Willden252233d2018-01-02 15:13:46 -070018
19#include <android/hardware/keymaster/4.0/IKeymasterDevice.h>
20#include <android/hardware/keymaster/4.0/types.h>
Shawn Willdenef285542019-11-26 15:05:51 -070021#include <gtest/gtest.h>
22#include <hidl/GtestPrinter.h>
23#include <hidl/ServiceManagement.h>
Shawn Willden252233d2018-01-02 15:13:46 -070024
25#include <keymasterV4_0/authorization_set.h>
26
27namespace android {
28namespace hardware {
29namespace keymaster {
30namespace V4_0 {
31
32::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
33
34namespace test {
35
36using ::android::sp;
Rob Barnesbd37c3b2019-09-06 12:53:17 -060037using hidl::base::V1_0::DebugInfo;
Shawn Willdenef285542019-11-26 15:05:51 -070038using ::std::string;
Shawn Willden252233d2018-01-02 15:13:46 -070039
40class HidlBuf : public hidl_vec<uint8_t> {
Shawn Willden3f7c80a2020-01-15 19:09:50 -070041 using super = hidl_vec<uint8_t>;
Shawn Willden252233d2018-01-02 15:13:46 -070042
Shawn Willdenef285542019-11-26 15:05:51 -070043 public:
Shawn Willden252233d2018-01-02 15:13:46 -070044 HidlBuf() {}
45 HidlBuf(const super& other) : super(other) {}
Shawn Willden3f7c80a2020-01-15 19:09:50 -070046 HidlBuf(super&& other) : super(std::move(other)) { other = {}; }
47 HidlBuf(const HidlBuf& other) : super(other) {}
48 HidlBuf(HidlBuf&& other) : super(std::move(other)) { other = HidlBuf(); }
Shawn Willden252233d2018-01-02 15:13:46 -070049 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));
Shawn Willden3f7c80a2020-01-15 19:09:50 -070058 other = {};
59 return *this;
60 }
61
62 HidlBuf& operator=(const HidlBuf& other) {
63 super::operator=(other);
64 return *this;
65 }
66
67 HidlBuf& operator=(HidlBuf&& other) {
68 super::operator=(std::move(other));
69 other.super::operator=({});
Shawn Willden252233d2018-01-02 15:13:46 -070070 return *this;
71 }
72
73 HidlBuf& operator=(const string& other) {
74 resize(other.size());
75 std::copy(other.begin(), other.end(), begin());
76 return *this;
77 }
78
79 string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); }
80};
81
82constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
83
Dan Shi3bacd7f2019-12-10 15:41:18 -080084class KeymasterHidlTest : public ::testing::TestWithParam<std::string> {
85 public:
Shawn Willdenef285542019-11-26 15:05:51 -070086 void SetUp() override;
Shawn Willden252233d2018-01-02 15:13:46 -070087 void TearDown() override {
88 if (key_blob_.size()) {
89 CheckedDeleteKey();
90 }
91 AbortIfNeeded();
Shawn Willden252233d2018-01-02 15:13:46 -070092 }
93
Shawn Willden3f7c80a2020-01-15 19:09:50 -070094 void InitializeKeymaster(sp<IKeymasterDevice> keymaster);
Dan Shi3bacd7f2019-12-10 15:41:18 -080095 IKeymasterDevice& keymaster() { return *keymaster_; }
Dan Shi3bacd7f2019-12-10 15:41:18 -080096 uint32_t os_version() { return os_version_; }
97 uint32_t os_patch_level() { return os_patch_level_; }
Shawn Willden252233d2018-01-02 15:13:46 -070098
99 ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
100 KeyCharacteristics* key_characteristics);
101 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
102
103 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
104 const string& key_material, HidlBuf* key_blob,
105 KeyCharacteristics* key_characteristics);
106 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
107 const string& key_material);
108
109 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
Shawn Willden8d28efa2018-01-19 13:37:42 -0700110 const AuthorizationSet& wrapping_key_desc, string masking_key,
111 const AuthorizationSet& unwrapping_params);
Shawn Willden252233d2018-01-02 15:13:46 -0700112
113 ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
114 const HidlBuf& app_data, HidlBuf* key_material);
115 ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
116
117 ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
118 ErrorCode DeleteKey(bool keep_key_blob = false);
119
120 ErrorCode DeleteAllKeys();
121
122 void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
123 void CheckedDeleteKey();
124
Max Bires873d8892019-02-08 12:29:58 -0800125 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
126 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
Shawn Willden252233d2018-01-02 15:13:46 -0700127 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
128 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
129 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
130
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600131 ErrorCode GetDebugInfo(DebugInfo* debug_info);
132
Shawn Willden252233d2018-01-02 15:13:46 -0700133 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
134 AuthorizationSet* out_params, OperationHandle* op_handle);
135 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
136 AuthorizationSet* out_params);
137 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
138
139 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
140 const string& input, AuthorizationSet* out_params, string* output,
141 size_t* input_consumed);
142 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
143
144 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
145 const string& input, const string& signature, AuthorizationSet* out_params,
146 string* output);
147 ErrorCode Finish(const string& message, string* output);
148 ErrorCode Finish(const string& message, const string& signature, string* output);
149 ErrorCode Finish(string* output) { return Finish(string(), output); }
150
151 ErrorCode Abort(OperationHandle op_handle);
152
153 void AbortIfNeeded();
154
155 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
156 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
157 ErrorCode AttestKey(const AuthorizationSet& attest_params,
158 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
159
160 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
161 const AuthorizationSet& in_params, AuthorizationSet* out_params);
162
163 string SignMessage(const HidlBuf& key_blob, const string& message,
164 const AuthorizationSet& params);
165 string SignMessage(const string& message, const AuthorizationSet& params);
166
167 string MacMessage(const string& message, Digest digest, size_t mac_length);
168
anil.hiranniah19a4ca12022-03-03 17:39:30 +0530169 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size);
170
Shawn Willden252233d2018-01-02 15:13:46 -0700171 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
172 const string& expected_mac);
173
174 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
175 const string& expected_ciphertext);
176
177 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
178 PaddingMode padding_mode, const string& key, const string& iv,
179 const string& input, const string& expected_output);
180
181 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
182 const AuthorizationSet& params);
183 void VerifyMessage(const string& message, const string& signature,
184 const AuthorizationSet& params);
185
186 string EncryptMessage(const HidlBuf& key_blob, const string& message,
187 const AuthorizationSet& in_params, AuthorizationSet* out_params);
188 string EncryptMessage(const string& message, const AuthorizationSet& params,
189 AuthorizationSet* out_params);
190 string EncryptMessage(const string& message, const AuthorizationSet& params);
191 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
192 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
193 HidlBuf* iv_out);
194 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
195 const HidlBuf& iv_in);
Rob Barnes8ddc1c72019-09-13 18:00:44 -0600196 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
197 uint8_t mac_length_bits, const HidlBuf& iv_in);
Shawn Willden252233d2018-01-02 15:13:46 -0700198
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
Dan Shi3bacd7f2019-12-10 15:41:18 -0800207 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
208 SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700209
nagendra modadugubbe92632018-06-05 11:05:19 -0700210 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
211 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
212
213 std::vector<EcCurve> ValidCurves();
214 std::vector<EcCurve> InvalidCurves();
215
Yi Kong73921752018-09-21 15:30:45 -0700216 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700217 std::vector<Digest> InvalidDigests();
218
Shawn Willden252233d2018-01-02 15:13:46 -0700219 HidlBuf key_blob_;
220 KeyCharacteristics key_characteristics_;
221 OperationHandle op_handle_ = kOpHandleSentinel;
222
Shawn Willden390825b2019-11-28 20:15:25 -0700223 static std::vector<std::string> build_params() {
224 auto params = android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor);
225 return params;
226 }
227
Shawn Willdenef285542019-11-26 15:05:51 -0700228 private:
229 sp<IKeymasterDevice> keymaster_;
230 uint32_t os_version_;
231 uint32_t os_patch_level_;
Shawn Willden252233d2018-01-02 15:13:46 -0700232
Shawn Willdenef285542019-11-26 15:05:51 -0700233 SecurityLevel securityLevel_;
234 hidl_string name_;
235 hidl_string author_;
Shawn Willden252233d2018-01-02 15:13:46 -0700236};
237
Shawn Willden390825b2019-11-28 20:15:25 -0700238#define INSTANTIATE_KEYMASTER_HIDL_TEST(name) \
Dan Shiba4d5322020-07-28 13:09:30 -0700239 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \
Shawn Willden390825b2019-11-28 20:15:25 -0700240 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
241 testing::ValuesIn(KeymasterHidlTest::build_params()), \
Shawn Willdenef285542019-11-26 15:05:51 -0700242 android::hardware::PrintInstanceNameToString)
243
Shawn Willden252233d2018-01-02 15:13:46 -0700244} // namespace test
245} // namespace V4_0
246} // namespace keymaster
247} // namespace hardware
248} // namespace android