blob: faa7c75409e5eca3ec23699e0166f8b55b274f50 [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> {
41 typedef hidl_vec<uint8_t> super;
42
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) {}
46 HidlBuf(super&& other) : super(std::move(other)) {}
47 explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; }
48
49 HidlBuf& operator=(const super& other) {
50 super::operator=(other);
51 return *this;
52 }
53
54 HidlBuf& operator=(super&& other) {
55 super::operator=(std::move(other));
56 return *this;
57 }
58
59 HidlBuf& operator=(const string& other) {
60 resize(other.size());
61 std::copy(other.begin(), other.end(), begin());
62 return *this;
63 }
64
65 string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); }
66};
67
68constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
69
Dan Shi3bacd7f2019-12-10 15:41:18 -080070class KeymasterHidlTest : public ::testing::TestWithParam<std::string> {
71 public:
Shawn Willdenef285542019-11-26 15:05:51 -070072 void SetUp() override;
Shawn Willden252233d2018-01-02 15:13:46 -070073 void TearDown() override {
74 if (key_blob_.size()) {
75 CheckedDeleteKey();
76 }
77 AbortIfNeeded();
Shawn Willden252233d2018-01-02 15:13:46 -070078 }
79
Dan Shi3bacd7f2019-12-10 15:41:18 -080080 void InitializeKeymaster();
Dan Shi3bacd7f2019-12-10 15:41:18 -080081 IKeymasterDevice& keymaster() { return *keymaster_; }
Dan Shi3bacd7f2019-12-10 15:41:18 -080082 uint32_t os_version() { return os_version_; }
83 uint32_t os_patch_level() { return os_patch_level_; }
Shawn Willden252233d2018-01-02 15:13:46 -070084
85 ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
86 KeyCharacteristics* key_characteristics);
87 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
88
89 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
90 const string& key_material, HidlBuf* key_blob,
91 KeyCharacteristics* key_characteristics);
92 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
93 const string& key_material);
94
95 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
Shawn Willden8d28efa2018-01-19 13:37:42 -070096 const AuthorizationSet& wrapping_key_desc, string masking_key,
97 const AuthorizationSet& unwrapping_params);
Shawn Willden252233d2018-01-02 15:13:46 -070098
99 ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
100 const HidlBuf& app_data, HidlBuf* key_material);
101 ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
102
103 ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
104 ErrorCode DeleteKey(bool keep_key_blob = false);
105
106 ErrorCode DeleteAllKeys();
107
108 void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
109 void CheckedDeleteKey();
110
Max Bires873d8892019-02-08 12:29:58 -0800111 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
112 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
Shawn Willden252233d2018-01-02 15:13:46 -0700113 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
114 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
115 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
116
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600117 ErrorCode GetDebugInfo(DebugInfo* debug_info);
118
Shawn Willden252233d2018-01-02 15:13:46 -0700119 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
120 AuthorizationSet* out_params, OperationHandle* op_handle);
121 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
122 AuthorizationSet* out_params);
123 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
124
125 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
126 const string& input, AuthorizationSet* out_params, string* output,
127 size_t* input_consumed);
128 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
129
130 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
131 const string& input, const string& signature, AuthorizationSet* out_params,
132 string* output);
133 ErrorCode Finish(const string& message, string* output);
134 ErrorCode Finish(const string& message, const string& signature, string* output);
135 ErrorCode Finish(string* output) { return Finish(string(), output); }
136
137 ErrorCode Abort(OperationHandle op_handle);
138
139 void AbortIfNeeded();
140
141 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
142 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
143 ErrorCode AttestKey(const AuthorizationSet& attest_params,
144 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
145
146 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
147 const AuthorizationSet& in_params, AuthorizationSet* out_params);
148
149 string SignMessage(const HidlBuf& key_blob, const string& message,
150 const AuthorizationSet& params);
151 string SignMessage(const string& message, const AuthorizationSet& params);
152
153 string MacMessage(const string& message, Digest digest, size_t mac_length);
154
155 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
156 const string& expected_mac);
157
158 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
159 const string& expected_ciphertext);
160
161 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
162 PaddingMode padding_mode, const string& key, const string& iv,
163 const string& input, const string& expected_output);
164
165 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
166 const AuthorizationSet& params);
167 void VerifyMessage(const string& message, const string& signature,
168 const AuthorizationSet& params);
169
170 string EncryptMessage(const HidlBuf& key_blob, const string& message,
171 const AuthorizationSet& in_params, AuthorizationSet* out_params);
172 string EncryptMessage(const string& message, const AuthorizationSet& params,
173 AuthorizationSet* out_params);
174 string EncryptMessage(const string& message, const AuthorizationSet& params);
175 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
176 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
177 HidlBuf* iv_out);
178 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
179 const HidlBuf& iv_in);
Rob Barnes8ddc1c72019-09-13 18:00:44 -0600180 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
181 uint8_t mac_length_bits, const HidlBuf& iv_in);
Shawn Willden252233d2018-01-02 15:13:46 -0700182
183 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
184 const AuthorizationSet& params);
185 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
186 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
187 const HidlBuf& iv);
188
189 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
190
Dan Shi3bacd7f2019-12-10 15:41:18 -0800191 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
192 SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700193
nagendra modadugubbe92632018-06-05 11:05:19 -0700194 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
195 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
196
197 std::vector<EcCurve> ValidCurves();
198 std::vector<EcCurve> InvalidCurves();
199
Yi Kong73921752018-09-21 15:30:45 -0700200 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700201 std::vector<Digest> InvalidDigests();
202
Shawn Willden252233d2018-01-02 15:13:46 -0700203 HidlBuf key_blob_;
204 KeyCharacteristics key_characteristics_;
205 OperationHandle op_handle_ = kOpHandleSentinel;
206
Shawn Willdenef285542019-11-26 15:05:51 -0700207 private:
208 sp<IKeymasterDevice> keymaster_;
209 uint32_t os_version_;
210 uint32_t os_patch_level_;
Shawn Willden252233d2018-01-02 15:13:46 -0700211
Shawn Willdenef285542019-11-26 15:05:51 -0700212 SecurityLevel securityLevel_;
213 hidl_string name_;
214 hidl_string author_;
Shawn Willden252233d2018-01-02 15:13:46 -0700215};
216
Shawn Willdenef285542019-11-26 15:05:51 -0700217#define INSTANTIATE_KEYMASTER_HIDL_TEST(name) \
218 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
219 testing::ValuesIn(android::hardware::getAllHalInstanceNames( \
220 IKeymasterDevice::descriptor)), \
221 android::hardware::PrintInstanceNameToString)
222
Shawn Willden252233d2018-01-02 15:13:46 -0700223} // namespace test
224} // namespace V4_0
225} // namespace keymaster
226} // namespace hardware
227} // namespace android