blob: adceead2e73ef719e08cc7f651119220c72ff4ea [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>
Dan Shi3bacd7f2019-12-10 15:41:18 -080021#include <gtest/gtest.h>
22#include <hidl/GtestPrinter.h>
23#include <hidl/ServiceManagement.h>
Shawn Willden252233d2018-01-02 15:13:46 -070024#include <keymaster/keymaster_configuration.h>
25
26#include <keymasterV4_0/authorization_set.h>
27
28namespace android {
29namespace hardware {
30namespace keymaster {
31namespace V4_0 {
32
33::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
34
35namespace test {
36
37using ::android::sp;
38using ::std::string;
Rob Barnesbd37c3b2019-09-06 12:53:17 -060039using hidl::base::V1_0::DebugInfo;
Shawn Willden252233d2018-01-02 15:13:46 -070040
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
Dan Shi3bacd7f2019-12-10 15:41:18 -080071class KeymasterHidlTest : public ::testing::TestWithParam<std::string> {
72 public:
73 void SetUp();
Shawn Willden252233d2018-01-02 15:13:46 -070074 void TearDown() override {
75 if (key_blob_.size()) {
76 CheckedDeleteKey();
77 }
78 AbortIfNeeded();
Shawn Willden252233d2018-01-02 15:13:46 -070079 keymaster_.clear();
80 all_keymasters_.clear();
81 }
82
Dan Shi3bacd7f2019-12-10 15:41:18 -080083 void InitializeKeymaster();
84
85 IKeymasterDevice& keymaster() { return *keymaster_; }
86 const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
87 uint32_t os_version() { return os_version_; }
88 uint32_t os_patch_level() { return os_patch_level_; }
Shawn Willden252233d2018-01-02 15:13:46 -070089
90 ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
91 KeyCharacteristics* key_characteristics);
92 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
93
94 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
95 const string& key_material, HidlBuf* key_blob,
96 KeyCharacteristics* key_characteristics);
97 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
98 const string& key_material);
99
100 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
Shawn Willden8d28efa2018-01-19 13:37:42 -0700101 const AuthorizationSet& wrapping_key_desc, string masking_key,
102 const AuthorizationSet& unwrapping_params);
Shawn Willden252233d2018-01-02 15:13:46 -0700103
104 ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id,
105 const HidlBuf& app_data, HidlBuf* key_material);
106 ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material);
107
108 ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
109 ErrorCode DeleteKey(bool keep_key_blob = false);
110
111 ErrorCode DeleteAllKeys();
112
113 void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false);
114 void CheckedDeleteKey();
115
Max Bires873d8892019-02-08 12:29:58 -0800116 static void CheckCreationDateTime(const AuthorizationSet& sw_enforced,
117 std::chrono::time_point<std::chrono::system_clock> creation);
118
119 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
120 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
Shawn Willden252233d2018-01-02 15:13:46 -0700121 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
122 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
123 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
124
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600125 ErrorCode GetDebugInfo(DebugInfo* debug_info);
126
Shawn Willden252233d2018-01-02 15:13:46 -0700127 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
128 AuthorizationSet* out_params, OperationHandle* op_handle);
129 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
130 AuthorizationSet* out_params);
131 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
132
133 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
134 const string& input, AuthorizationSet* out_params, string* output,
135 size_t* input_consumed);
136 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
137
138 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
139 const string& input, const string& signature, AuthorizationSet* out_params,
140 string* output);
141 ErrorCode Finish(const string& message, string* output);
142 ErrorCode Finish(const string& message, const string& signature, string* output);
143 ErrorCode Finish(string* output) { return Finish(string(), output); }
144
145 ErrorCode Abort(OperationHandle op_handle);
146
147 void AbortIfNeeded();
148
149 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
150 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
151 ErrorCode AttestKey(const AuthorizationSet& attest_params,
152 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
153
154 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
155 const AuthorizationSet& in_params, AuthorizationSet* out_params);
156
157 string SignMessage(const HidlBuf& key_blob, const string& message,
158 const AuthorizationSet& params);
159 string SignMessage(const string& message, const AuthorizationSet& params);
160
161 string MacMessage(const string& message, Digest digest, size_t mac_length);
162
163 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
164 const string& expected_mac);
165
166 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
167 const string& expected_ciphertext);
168
169 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
170 PaddingMode padding_mode, const string& key, const string& iv,
171 const string& input, const string& expected_output);
172
173 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
174 const AuthorizationSet& params);
175 void VerifyMessage(const string& message, const string& signature,
176 const AuthorizationSet& params);
177
178 string EncryptMessage(const HidlBuf& key_blob, const string& message,
179 const AuthorizationSet& in_params, AuthorizationSet* out_params);
180 string EncryptMessage(const string& message, const AuthorizationSet& params,
181 AuthorizationSet* out_params);
182 string EncryptMessage(const string& message, const AuthorizationSet& params);
183 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
184 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
185 HidlBuf* iv_out);
186 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
187 const HidlBuf& iv_in);
Rob Barnes8ddc1c72019-09-13 18:00:44 -0600188 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
189 uint8_t mac_length_bits, const HidlBuf& iv_in);
Shawn Willden252233d2018-01-02 15:13:46 -0700190
191 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
192 const AuthorizationSet& params);
193 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
194 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
195 const HidlBuf& iv);
196
197 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
198
Dan Shi3bacd7f2019-12-10 15:41:18 -0800199 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
200 SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700201
nagendra modadugubbe92632018-06-05 11:05:19 -0700202 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
203 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
204
205 std::vector<EcCurve> ValidCurves();
206 std::vector<EcCurve> InvalidCurves();
207
Yi Kong73921752018-09-21 15:30:45 -0700208 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700209 std::vector<Digest> InvalidDigests();
210
Shawn Willden252233d2018-01-02 15:13:46 -0700211 HidlBuf key_blob_;
212 KeyCharacteristics key_characteristics_;
213 OperationHandle op_handle_ = kOpHandleSentinel;
214
215 private:
Dan Shi3bacd7f2019-12-10 15:41:18 -0800216 sp<IKeymasterDevice> keymaster_;
217 std::vector<sp<IKeymasterDevice>> all_keymasters_;
218 uint32_t os_version_;
219 uint32_t os_patch_level_;
Shawn Willden252233d2018-01-02 15:13:46 -0700220
Dan Shi3bacd7f2019-12-10 15:41:18 -0800221 SecurityLevel securityLevel_;
222 hidl_string name_;
223 hidl_string author_;
224 string service_name_;
Shawn Willden252233d2018-01-02 15:13:46 -0700225};
226
227} // namespace test
228} // namespace V4_0
229} // namespace keymaster
230} // namespace hardware
231} // namespace android