blob: 4bd8b26db95c783ff4a463bd299cc67a3c06b61f [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 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
117 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
Shawn Willden252233d2018-01-02 15:13:46 -0700118 ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
119 const HidlBuf& app_data, KeyCharacteristics* key_characteristics);
120 ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics);
121
Rob Barnesbd37c3b2019-09-06 12:53:17 -0600122 ErrorCode GetDebugInfo(DebugInfo* debug_info);
123
Shawn Willden252233d2018-01-02 15:13:46 -0700124 ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params,
125 AuthorizationSet* out_params, OperationHandle* op_handle);
126 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
127 AuthorizationSet* out_params);
128 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
129
130 ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params,
131 const string& input, AuthorizationSet* out_params, string* output,
132 size_t* input_consumed);
133 ErrorCode Update(const string& input, string* out, size_t* input_consumed);
134
135 ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
136 const string& input, const string& signature, AuthorizationSet* out_params,
137 string* output);
138 ErrorCode Finish(const string& message, string* output);
139 ErrorCode Finish(const string& message, const string& signature, string* output);
140 ErrorCode Finish(string* output) { return Finish(string(), output); }
141
142 ErrorCode Abort(OperationHandle op_handle);
143
144 void AbortIfNeeded();
145
146 ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params,
147 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
148 ErrorCode AttestKey(const AuthorizationSet& attest_params,
149 hidl_vec<hidl_vec<uint8_t>>* cert_chain);
150
151 string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message,
152 const AuthorizationSet& in_params, AuthorizationSet* out_params);
153
154 string SignMessage(const HidlBuf& key_blob, const string& message,
155 const AuthorizationSet& params);
156 string SignMessage(const string& message, const AuthorizationSet& params);
157
158 string MacMessage(const string& message, Digest digest, size_t mac_length);
159
160 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
161 const string& expected_mac);
162
163 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
164 const string& expected_ciphertext);
165
166 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
167 PaddingMode padding_mode, const string& key, const string& iv,
168 const string& input, const string& expected_output);
169
170 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature,
171 const AuthorizationSet& params);
172 void VerifyMessage(const string& message, const string& signature,
173 const AuthorizationSet& params);
174
175 string EncryptMessage(const HidlBuf& key_blob, const string& message,
176 const AuthorizationSet& in_params, AuthorizationSet* out_params);
177 string EncryptMessage(const string& message, const AuthorizationSet& params,
178 AuthorizationSet* out_params);
179 string EncryptMessage(const string& message, const AuthorizationSet& params);
180 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
181 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
182 HidlBuf* iv_out);
183 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
184 const HidlBuf& iv_in);
Rob Barnes8ddc1c72019-09-13 18:00:44 -0600185 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
186 uint8_t mac_length_bits, const HidlBuf& iv_in);
Shawn Willden252233d2018-01-02 15:13:46 -0700187
188 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
189 const AuthorizationSet& params);
190 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
191 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
192 const HidlBuf& iv);
193
194 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
195
Dan Shi3bacd7f2019-12-10 15:41:18 -0800196 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
197 SecurityLevel SecLevel() { return securityLevel_; }
Shawn Willden252233d2018-01-02 15:13:46 -0700198
nagendra modadugubbe92632018-06-05 11:05:19 -0700199 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
200 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
201
202 std::vector<EcCurve> ValidCurves();
203 std::vector<EcCurve> InvalidCurves();
204
Yi Kong73921752018-09-21 15:30:45 -0700205 std::vector<Digest> ValidDigests(bool withNone, bool withMD5);
nagendra modadugubbe92632018-06-05 11:05:19 -0700206 std::vector<Digest> InvalidDigests();
207
Shawn Willden252233d2018-01-02 15:13:46 -0700208 HidlBuf key_blob_;
209 KeyCharacteristics key_characteristics_;
210 OperationHandle op_handle_ = kOpHandleSentinel;
211
212 private:
Dan Shi3bacd7f2019-12-10 15:41:18 -0800213 sp<IKeymasterDevice> keymaster_;
214 std::vector<sp<IKeymasterDevice>> all_keymasters_;
215 uint32_t os_version_;
216 uint32_t os_patch_level_;
Shawn Willden252233d2018-01-02 15:13:46 -0700217
Dan Shi3bacd7f2019-12-10 15:41:18 -0800218 SecurityLevel securityLevel_;
219 hidl_string name_;
220 hidl_string author_;
221 string service_name_;
Shawn Willden252233d2018-01-02 15:13:46 -0700222};
223
224} // namespace test
225} // namespace V4_0
226} // namespace keymaster
227} // namespace hardware
228} // namespace android