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> { |
| 41 | typedef hidl_vec<uint8_t> super; |
| 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) {} |
| 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 | |
| 68 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 69 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 70 | class KeymasterHidlTest : public ::testing::TestWithParam<std::string> { |
| 71 | public: |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 72 | void SetUp() override; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 73 | void TearDown() override { |
| 74 | if (key_blob_.size()) { |
| 75 | CheckedDeleteKey(); |
| 76 | } |
| 77 | AbortIfNeeded(); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 80 | void InitializeKeymaster(); |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 81 | IKeymasterDevice& keymaster() { return *keymaster_; } |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 82 | uint32_t os_version() { return os_version_; } |
| 83 | uint32_t os_patch_level() { return os_patch_level_; } |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 84 | |
| 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 Willden | 8d28efa | 2018-01-19 13:37:42 -0700 | [diff] [blame] | 96 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 97 | const AuthorizationSet& unwrapping_params); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 98 | |
| 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 Bires | 873d889 | 2019-02-08 12:29:58 -0800 | [diff] [blame] | 111 | void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id, |
| 112 | const HidlBuf& app_data, KeyCharacteristics* key_characteristics); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 113 | 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 Barnes | bd37c3b | 2019-09-06 12:53:17 -0600 | [diff] [blame] | 117 | ErrorCode GetDebugInfo(DebugInfo* debug_info); |
| 118 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 119 | 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 Barnes | 8ddc1c7 | 2019-09-13 18:00:44 -0600 | [diff] [blame] | 180 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 181 | uint8_t mac_length_bits, const HidlBuf& iv_in); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 182 | |
| 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 Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 191 | bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 192 | SecurityLevel SecLevel() { return securityLevel_; } |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 193 | |
nagendra modadugu | bbe9263 | 2018-06-05 11:05:19 -0700 | [diff] [blame] | 194 | 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 Kong | 7392175 | 2018-09-21 15:30:45 -0700 | [diff] [blame] | 200 | std::vector<Digest> ValidDigests(bool withNone, bool withMD5); |
nagendra modadugu | bbe9263 | 2018-06-05 11:05:19 -0700 | [diff] [blame] | 201 | std::vector<Digest> InvalidDigests(); |
| 202 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 203 | HidlBuf key_blob_; |
| 204 | KeyCharacteristics key_characteristics_; |
| 205 | OperationHandle op_handle_ = kOpHandleSentinel; |
| 206 | |
Shawn Willden | 390825b | 2019-11-28 20:15:25 -0700 | [diff] [blame^] | 207 | static std::vector<std::string> build_params() { |
| 208 | auto params = android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor); |
| 209 | return params; |
| 210 | } |
| 211 | |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 212 | private: |
| 213 | sp<IKeymasterDevice> keymaster_; |
| 214 | uint32_t os_version_; |
| 215 | uint32_t os_patch_level_; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 216 | |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 217 | SecurityLevel securityLevel_; |
| 218 | hidl_string name_; |
| 219 | hidl_string author_; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
Shawn Willden | 390825b | 2019-11-28 20:15:25 -0700 | [diff] [blame^] | 222 | #define INSTANTIATE_KEYMASTER_HIDL_TEST(name) \ |
| 223 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 224 | testing::ValuesIn(KeymasterHidlTest::build_params()), \ |
Shawn Willden | ef28554 | 2019-11-26 15:05:51 -0700 | [diff] [blame] | 225 | android::hardware::PrintInstanceNameToString) |
| 226 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 227 | } // namespace test |
| 228 | } // namespace V4_0 |
| 229 | } // namespace keymaster |
| 230 | } // namespace hardware |
| 231 | } // namespace android |