blob: f7d6da2ca0808f767c786241ec6e717a793e54dc [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 Willdenfcc4c5c2019-11-26 09:00:08 -070021
22#include <VtsHalHidlTargetTestBase.h>
Shawn Willden252233d2018-01-02 15:13:46 -070023
24#include <keymasterV4_0/authorization_set.h>
25
26namespace android {
27namespace hardware {
28namespace keymaster {
29namespace V4_0 {
30
31::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
32
33namespace test {
34
35using ::android::sp;
36using ::std::string;
Rob Barnesbd37c3b2019-09-06 12:53:17 -060037using hidl::base::V1_0::DebugInfo;
Shawn Willden252233d2018-01-02 15:13:46 -070038
39class HidlBuf : public hidl_vec<uint8_t> {
40 typedef hidl_vec<uint8_t> super;
41
42 public:
43 HidlBuf() {}
44 HidlBuf(const super& other) : super(other) {}
45 HidlBuf(super&& other) : super(std::move(other)) {}
46 explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; }
47
48 HidlBuf& operator=(const super& other) {
49 super::operator=(other);
50 return *this;
51 }
52
53 HidlBuf& operator=(super&& other) {
54 super::operator=(std::move(other));
55 return *this;
56 }
57
58 HidlBuf& operator=(const string& other) {
59 resize(other.size());
60 std::copy(other.begin(), other.end(), begin());
61 return *this;
62 }
63
64 string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); }
65};
66
67constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
68
Dan Shi3bacd7f2019-12-10 15:41:18 -080069class KeymasterHidlTest : public ::testing::TestWithParam<std::string> {
70 public:
71 void SetUp();
Shawn Willden252233d2018-01-02 15:13:46 -070072 void TearDown() override {
73 if (key_blob_.size()) {
74 CheckedDeleteKey();
75 }
76 AbortIfNeeded();
Shawn Willden252233d2018-01-02 15:13:46 -070077 keymaster_.clear();
78 all_keymasters_.clear();
79 }
80
Dan Shi3bacd7f2019-12-10 15:41:18 -080081 void InitializeKeymaster();
82
83 IKeymasterDevice& keymaster() { return *keymaster_; }
84 const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
85 uint32_t os_version() { return os_version_; }
86 uint32_t os_patch_level() { return os_patch_level_; }
Shawn Willden252233d2018-01-02 15:13:46 -070087
88 ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
89 KeyCharacteristics* key_characteristics);
90 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
91
92 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
93 const string& key_material, HidlBuf* key_blob,
94 KeyCharacteristics* key_characteristics);
95 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
96 const string& key_material);
97
98 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
Shawn Willden8d28efa2018-01-19 13:37:42 -070099 const AuthorizationSet& wrapping_key_desc, string masking_key,
100 const AuthorizationSet& unwrapping_params);
Shawn Willden252233d2018-01-02 15:13:46 -0700101
102 ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
103 const HidlBuf& app_data, HidlBuf* key_material);
104 ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
105
106 ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
107 ErrorCode DeleteKey(bool keep_key_blob = false);
108
109 ErrorCode DeleteAllKeys();
110
111 void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
112 void CheckedDeleteKey();
113
Max Bires873d8892019-02-08 12:29:58 -0800114 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
115 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
Shawn Willden252233d2018-01-02 15:13:46 -0700116 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
117 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
118 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
119
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600120 ErrorCode GetDebugInfo(DebugInfo* debug_info);
121
Shawn Willden252233d2018-01-02 15:13:46 -0700122 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
123 AuthorizationSet* out_params, OperationHandle* op_handle);
124 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
125 AuthorizationSet* out_params);
126 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
127
128 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
129 const string& input, AuthorizationSet* out_params, string* output,
130 size_t* input_consumed);
131 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
132
133 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
134 const string& input, const string& signature, AuthorizationSet* out_params,
135 string* output);
136 ErrorCode Finish(const string& message, string* output);
137 ErrorCode Finish(const string& message, const string& signature, string* output);
138 ErrorCode Finish(string* output) { return Finish(string(), output); }
139
140 ErrorCode Abort(OperationHandle op_handle);
141
142 void AbortIfNeeded();
143
144 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
145 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
146 ErrorCode AttestKey(const AuthorizationSet& attest_params,
147 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
148
149 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
150 const AuthorizationSet& in_params, AuthorizationSet* out_params);
151
152 string SignMessage(const HidlBuf& key_blob, const string& message,
153 const AuthorizationSet& params);
154 string SignMessage(const string& message, const AuthorizationSet& params);
155
156 string MacMessage(const string& message, Digest digest, size_t mac_length);
157
158 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
159 const string& expected_mac);
160
161 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
162 const string& expected_ciphertext);
163
164 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
165 PaddingMode padding_mode, const string& key, const string& iv,
166 const string& input, const string& expected_output);
167
168 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
169 const AuthorizationSet& params);
170 void VerifyMessage(const string& message, const string& signature,
171 const AuthorizationSet& params);
172
173 string EncryptMessage(const HidlBuf& key_blob, const string& message,
174 const AuthorizationSet& in_params, AuthorizationSet* out_params);
175 string EncryptMessage(const string& message, const AuthorizationSet& params,
176 AuthorizationSet* out_params);
177 string EncryptMessage(const string& message, const AuthorizationSet& params);
178 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
179 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
180 HidlBuf* iv_out);
181 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
182 const HidlBuf& iv_in);
Rob Barnes8ddc1c72019-09-13 18:00:44 -0600183 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
184 uint8_t mac_length_bits, const HidlBuf& iv_in);
Shawn Willden252233d2018-01-02 15:13:46 -0700185
186 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
187 const AuthorizationSet& params);
188 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
189 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
190 const HidlBuf& iv);
191
192 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
193
Dan Shi3bacd7f2019-12-10 15:41:18 -0800194 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
195 SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700196
nagendra modadugubbe92632018-06-05 11:05:19 -0700197 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
198 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
199
200 std::vector<EcCurve> ValidCurves();
201 std::vector<EcCurve> InvalidCurves();
202
Yi Kong73921752018-09-21 15:30:45 -0700203 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700204 std::vector<Digest> InvalidDigests();
205
Shawn Willden252233d2018-01-02 15:13:46 -0700206 HidlBuf key_blob_;
207 KeyCharacteristics key_characteristics_;
208 OperationHandle op_handle_ = kOpHandleSentinel;
209
210 private:
Dan Shi3bacd7f2019-12-10 15:41:18 -0800211 sp<IKeymasterDevice> keymaster_;
212 std::vector<sp<IKeymasterDevice>> all_keymasters_;
213 uint32_t os_version_;
214 uint32_t os_patch_level_;
Shawn Willden252233d2018-01-02 15:13:46 -0700215
Dan Shi3bacd7f2019-12-10 15:41:18 -0800216 SecurityLevel securityLevel_;
217 hidl_string name_;
218 hidl_string author_;
219 string service_name_;
Shawn Willden252233d2018-01-02 15:13:46 -0700220};
221
222} // namespace test
223} // namespace V4_0
224} // namespace keymaster
225} // namespace hardware
226} // namespace android