Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 1 | /* |
| 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 Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 17 | #pragma once |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android/hardware/keymaster/4.0/IKeymasterDevice.h> |
| 20 | #include <android/hardware/keymaster/4.0/types.h> |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 21 | #include <gtest/gtest.h> |
| 22 | #include <hidl/GtestPrinter.h> |
| 23 | #include <hidl/ServiceManagement.h> |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 24 | |
| 25 | #include <keymasterV4_0/authorization_set.h> |
| 26 | |
| 27 | namespace android { |
| 28 | namespace hardware { |
| 29 | namespace keymaster { |
| 30 | namespace V4_0 { |
| 31 | |
| 32 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 33 | |
| 34 | namespace test { |
| 35 | |
| 36 | using ::android::sp; |
Rob Barnes | bd37c3b | 2019-09-06 12:53:17 -0600 | [diff] [blame] | 37 | using hidl::base::V1_0::DebugInfo; |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 38 | using ::std::string; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 39 | |
| 40 | class HidlBuf : public hidl_vec<uint8_t> { |
Shawn Willden | 3f7c80a | 2020-01-15 19:09:50 -0700 | [diff] [blame] | 41 | using super = hidl_vec<uint8_t>; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 42 | |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 43 | public: |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 44 | HidlBuf() {} |
| 45 | HidlBuf(const super& other) : super(other) {} |
Shawn Willden | 3f7c80a | 2020-01-15 19:09:50 -0700 | [diff] [blame] | 46 | 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 Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 49 | 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 Willden | 3f7c80a | 2020-01-15 19:09:50 -0700 | [diff] [blame] | 58 | 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 Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 70 | 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 | |
| 82 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 83 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 84 | class KeymasterHidlTest : public ::testing::TestWithParam<std::string> { |
| 85 | public: |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 86 | void SetUp() override; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 87 | void TearDown() override { |
| 88 | if (key_blob_.size()) { |
| 89 | CheckedDeleteKey(); |
| 90 | } |
| 91 | AbortIfNeeded(); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Shawn Willden | 3f7c80a | 2020-01-15 19:09:50 -0700 | [diff] [blame] | 94 | void InitializeKeymaster(sp<IKeymasterDevice> keymaster); |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 95 | IKeymasterDevice& keymaster() { return *keymaster_; } |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 96 | uint32_t os_version() { return os_version_; } |
| 97 | uint32_t os_patch_level() { return os_patch_level_; } |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 98 | |
| 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 Willden | 8d28efa | 2018-01-19 13:37:42 -0700 | [diff] [blame] | 110 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 111 | const AuthorizationSet& unwrapping_params); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 112 | |
| 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 Bires | 873d889 | 2019-02-08 12:29:58 -0800 | [diff] [blame] | 125 | void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id, |
| 126 | const HidlBuf& app_data, KeyCharacteristics* key_characteristics); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 127 | 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 Barnes | bd37c3b | 2019-09-06 12:53:17 -0600 | [diff] [blame] | 131 | ErrorCode GetDebugInfo(DebugInfo* debug_info); |
| 132 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 133 | 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.hiranniah | 19a4ca1 | 2022-03-03 17:39:30 +0530 | [diff] [blame] | 169 | void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size); |
| 170 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 171 | 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 Barnes | 8ddc1c7 | 2019-09-13 18:00:44 -0600 | [diff] [blame] | 196 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 197 | uint8_t mac_length_bits, const HidlBuf& iv_in); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 198 | |
| 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 Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 207 | bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 208 | SecurityLevel SecLevel() { return securityLevel_; } |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 209 | |
nagendra modadugu | bbe9263 | 2018-06-05 11:05:19 -0700 | [diff] [blame] | 210 | 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 Kong | 7392175 | 2018-09-21 15:30:45 -0700 | [diff] [blame] | 216 | std::vector<Digest> ValidDigests(bool withNone, bool withMD5); |
nagendra modadugu | bbe9263 | 2018-06-05 11:05:19 -0700 | [diff] [blame] | 217 | std::vector<Digest> InvalidDigests(); |
| 218 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 219 | HidlBuf key_blob_; |
| 220 | KeyCharacteristics key_characteristics_; |
| 221 | OperationHandle op_handle_ = kOpHandleSentinel; |
| 222 | |
Shawn Willden | 390825b | 2019-11-28 20:15:25 -0700 | [diff] [blame] | 223 | static std::vector<std::string> build_params() { |
| 224 | auto params = android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor); |
| 225 | return params; |
| 226 | } |
| 227 | |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 228 | private: |
| 229 | sp<IKeymasterDevice> keymaster_; |
| 230 | uint32_t os_version_; |
| 231 | uint32_t os_patch_level_; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 232 | |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 233 | SecurityLevel securityLevel_; |
| 234 | hidl_string name_; |
| 235 | hidl_string author_; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 236 | }; |
| 237 | |
Shawn Willden | 390825b | 2019-11-28 20:15:25 -0700 | [diff] [blame] | 238 | #define INSTANTIATE_KEYMASTER_HIDL_TEST(name) \ |
Dan Shi | ba4d532 | 2020-07-28 13:09:30 -0700 | [diff] [blame] | 239 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \ |
Shawn Willden | 390825b | 2019-11-28 20:15:25 -0700 | [diff] [blame] | 240 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 241 | testing::ValuesIn(KeymasterHidlTest::build_params()), \ |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 242 | android::hardware::PrintInstanceNameToString) |
| 243 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 244 | } // namespace test |
| 245 | } // namespace V4_0 |
| 246 | } // namespace keymaster |
| 247 | } // namespace hardware |
| 248 | } // namespace android |