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 | fcc4c5c | 2019-11-26 09:00:08 -0700 | [diff] [blame^] | 21 | |
| 22 | #include <VtsHalHidlTargetTestBase.h> |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 23 | |
| 24 | #include <keymasterV4_0/authorization_set.h> |
| 25 | |
| 26 | namespace android { |
| 27 | namespace hardware { |
| 28 | namespace keymaster { |
| 29 | namespace V4_0 { |
| 30 | |
| 31 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 32 | |
| 33 | namespace test { |
| 34 | |
| 35 | using ::android::sp; |
| 36 | using ::std::string; |
Rob Barnes | bd37c3b | 2019-09-06 12:53:17 -0600 | [diff] [blame] | 37 | using hidl::base::V1_0::DebugInfo; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 38 | |
| 39 | class HidlBuf : public hidl_vec<uint8_t> { |
| 40 | typedef hidl_vec<uint8_t> super; |
| 41 | |
| 42 | public: |
| 43 | HidlBuf() {} |
| 44 | HidlBuf(const super& other) : super(other) {} |
| 45 | HidlBuf(super&& other) : super(std::move(other)) {} |
| 46 | explicit HidlBuf(const std::string& other) : HidlBuf() { *this = other; } |
| 47 | |
| 48 | HidlBuf& operator=(const super& other) { |
| 49 | super::operator=(other); |
| 50 | return *this; |
| 51 | } |
| 52 | |
| 53 | HidlBuf& operator=(super&& other) { |
| 54 | super::operator=(std::move(other)); |
| 55 | return *this; |
| 56 | } |
| 57 | |
| 58 | HidlBuf& operator=(const string& other) { |
| 59 | resize(other.size()); |
| 60 | std::copy(other.begin(), other.end(), begin()); |
| 61 | return *this; |
| 62 | } |
| 63 | |
| 64 | string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); } |
| 65 | }; |
| 66 | |
| 67 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 68 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 69 | class KeymasterHidlTest : public ::testing::TestWithParam<std::string> { |
| 70 | public: |
| 71 | void SetUp(); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 72 | void TearDown() override { |
| 73 | if (key_blob_.size()) { |
| 74 | CheckedDeleteKey(); |
| 75 | } |
| 76 | AbortIfNeeded(); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 77 | keymaster_.clear(); |
| 78 | all_keymasters_.clear(); |
| 79 | } |
| 80 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 81 | void InitializeKeymaster(); |
| 82 | |
| 83 | IKeymasterDevice& keymaster() { return *keymaster_; } |
| 84 | const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; } |
| 85 | uint32_t os_version() { return os_version_; } |
| 86 | uint32_t os_patch_level() { return os_patch_level_; } |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 87 | |
| 88 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob, |
| 89 | KeyCharacteristics* key_characteristics); |
| 90 | ErrorCode GenerateKey(const AuthorizationSet& key_desc); |
| 91 | |
| 92 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 93 | const string& key_material, HidlBuf* key_blob, |
| 94 | KeyCharacteristics* key_characteristics); |
| 95 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 96 | const string& key_material); |
| 97 | |
| 98 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
Shawn Willden | 8d28efa | 2018-01-19 13:37:42 -0700 | [diff] [blame] | 99 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 100 | const AuthorizationSet& unwrapping_params); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 101 | |
| 102 | ErrorCode ExportKey(KeyFormat format, const HidlBuf& key_blob, const HidlBuf& client_id, |
| 103 | const HidlBuf& app_data, HidlBuf* key_material); |
| 104 | ErrorCode ExportKey(KeyFormat format, HidlBuf* key_material); |
| 105 | |
| 106 | ErrorCode DeleteKey(HidlBuf* key_blob, bool keep_key_blob = false); |
| 107 | ErrorCode DeleteKey(bool keep_key_blob = false); |
| 108 | |
| 109 | ErrorCode DeleteAllKeys(); |
| 110 | |
| 111 | void CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob = false); |
| 112 | void CheckedDeleteKey(); |
| 113 | |
Max Bires | 873d889 | 2019-02-08 12:29:58 -0800 | [diff] [blame] | 114 | void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id, |
| 115 | const HidlBuf& app_data, KeyCharacteristics* key_characteristics); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 116 | ErrorCode GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id, |
| 117 | const HidlBuf& app_data, KeyCharacteristics* key_characteristics); |
| 118 | ErrorCode GetCharacteristics(const HidlBuf& key_blob, KeyCharacteristics* key_characteristics); |
| 119 | |
Rob Barnes | bd37c3b | 2019-09-06 12:53:17 -0600 | [diff] [blame] | 120 | ErrorCode GetDebugInfo(DebugInfo* debug_info); |
| 121 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 122 | ErrorCode Begin(KeyPurpose purpose, const HidlBuf& key_blob, const AuthorizationSet& in_params, |
| 123 | AuthorizationSet* out_params, OperationHandle* op_handle); |
| 124 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 125 | AuthorizationSet* out_params); |
| 126 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params); |
| 127 | |
| 128 | ErrorCode Update(OperationHandle op_handle, const AuthorizationSet& in_params, |
| 129 | const string& input, AuthorizationSet* out_params, string* output, |
| 130 | size_t* input_consumed); |
| 131 | ErrorCode Update(const string& input, string* out, size_t* input_consumed); |
| 132 | |
| 133 | ErrorCode Finish(OperationHandle op_handle, const AuthorizationSet& in_params, |
| 134 | const string& input, const string& signature, AuthorizationSet* out_params, |
| 135 | string* output); |
| 136 | ErrorCode Finish(const string& message, string* output); |
| 137 | ErrorCode Finish(const string& message, const string& signature, string* output); |
| 138 | ErrorCode Finish(string* output) { return Finish(string(), output); } |
| 139 | |
| 140 | ErrorCode Abort(OperationHandle op_handle); |
| 141 | |
| 142 | void AbortIfNeeded(); |
| 143 | |
| 144 | ErrorCode AttestKey(const HidlBuf& key_blob, const AuthorizationSet& attest_params, |
| 145 | hidl_vec<hidl_vec<uint8_t>>* cert_chain); |
| 146 | ErrorCode AttestKey(const AuthorizationSet& attest_params, |
| 147 | hidl_vec<hidl_vec<uint8_t>>* cert_chain); |
| 148 | |
| 149 | string ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation, const string& message, |
| 150 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 151 | |
| 152 | string SignMessage(const HidlBuf& key_blob, const string& message, |
| 153 | const AuthorizationSet& params); |
| 154 | string SignMessage(const string& message, const AuthorizationSet& params); |
| 155 | |
| 156 | string MacMessage(const string& message, Digest digest, size_t mac_length); |
| 157 | |
| 158 | void CheckHmacTestVector(const string& key, const string& message, Digest digest, |
| 159 | const string& expected_mac); |
| 160 | |
| 161 | void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, |
| 162 | const string& expected_ciphertext); |
| 163 | |
| 164 | void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 165 | PaddingMode padding_mode, const string& key, const string& iv, |
| 166 | const string& input, const string& expected_output); |
| 167 | |
| 168 | void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature, |
| 169 | const AuthorizationSet& params); |
| 170 | void VerifyMessage(const string& message, const string& signature, |
| 171 | const AuthorizationSet& params); |
| 172 | |
| 173 | string EncryptMessage(const HidlBuf& key_blob, const string& message, |
| 174 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 175 | string EncryptMessage(const string& message, const AuthorizationSet& params, |
| 176 | AuthorizationSet* out_params); |
| 177 | string EncryptMessage(const string& message, const AuthorizationSet& params); |
| 178 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); |
| 179 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 180 | HidlBuf* iv_out); |
| 181 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 182 | const HidlBuf& iv_in); |
Rob Barnes | 8ddc1c7 | 2019-09-13 18:00:44 -0600 | [diff] [blame] | 183 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 184 | uint8_t mac_length_bits, const HidlBuf& iv_in); |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 185 | |
| 186 | string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext, |
| 187 | const AuthorizationSet& params); |
| 188 | string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); |
| 189 | string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, |
| 190 | const HidlBuf& iv); |
| 191 | |
| 192 | std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob); |
| 193 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 194 | bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 195 | SecurityLevel SecLevel() { return securityLevel_; } |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 196 | |
nagendra modadugu | bbe9263 | 2018-06-05 11:05:19 -0700 | [diff] [blame] | 197 | std::vector<uint32_t> ValidKeySizes(Algorithm algorithm); |
| 198 | std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm); |
| 199 | |
| 200 | std::vector<EcCurve> ValidCurves(); |
| 201 | std::vector<EcCurve> InvalidCurves(); |
| 202 | |
Yi Kong | 7392175 | 2018-09-21 15:30:45 -0700 | [diff] [blame] | 203 | std::vector<Digest> ValidDigests(bool withNone, bool withMD5); |
nagendra modadugu | bbe9263 | 2018-06-05 11:05:19 -0700 | [diff] [blame] | 204 | std::vector<Digest> InvalidDigests(); |
| 205 | |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 206 | HidlBuf key_blob_; |
| 207 | KeyCharacteristics key_characteristics_; |
| 208 | OperationHandle op_handle_ = kOpHandleSentinel; |
| 209 | |
| 210 | private: |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 211 | sp<IKeymasterDevice> keymaster_; |
| 212 | std::vector<sp<IKeymasterDevice>> all_keymasters_; |
| 213 | uint32_t os_version_; |
| 214 | uint32_t os_patch_level_; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 215 | |
Dan Shi | 3bacd7f | 2019-12-10 15:41:18 -0800 | [diff] [blame] | 216 | SecurityLevel securityLevel_; |
| 217 | hidl_string name_; |
| 218 | hidl_string author_; |
| 219 | string service_name_; |
Shawn Willden | 252233d | 2018-01-02 15:13:46 -0700 | [diff] [blame] | 220 | }; |
| 221 | |
| 222 | } // namespace test |
| 223 | } // namespace V4_0 |
| 224 | } // namespace keymaster |
| 225 | } // namespace hardware |
| 226 | } // namespace android |