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 17 #pragma once 18 19 #include <android/hardware/keymaster/4.0/IKeymasterDevice.h> 20 #include <android/hardware/keymaster/4.0/types.h> 21 #include <gtest/gtest.h> 22 #include <hidl/GtestPrinter.h> 23 #include <hidl/ServiceManagement.h> 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; 37 using hidl::base::V1_0::DebugInfo; 38 using ::std::string; 39 40 class HidlBuf : public hidl_vec<uint8_t> { 41 using super = hidl_vec<uint8_t>; 42 43 public: HidlBuf()44 HidlBuf() {} HidlBuf(const super & other)45 HidlBuf(const super& other) : super(other) {} HidlBuf(super && other)46 HidlBuf(super&& other) : super(std::move(other)) { other = {}; } HidlBuf(const HidlBuf & other)47 HidlBuf(const HidlBuf& other) : super(other) {} HidlBuf(HidlBuf && other)48 HidlBuf(HidlBuf&& other) : super(std::move(other)) { other = HidlBuf(); } HidlBuf(const std::string & other)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)); 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=({}); 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 to_string()79 string to_string() const { return string(reinterpret_cast<const char*>(data()), size()); } 80 }; 81 82 constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; 83 84 class KeymasterHidlTest : public ::testing::TestWithParam<std::string> { 85 public: 86 void SetUp() override; TearDown()87 void TearDown() override { 88 if (key_blob_.size()) { 89 CheckedDeleteKey(); 90 } 91 AbortIfNeeded(); 92 } 93 94 void InitializeKeymaster(sp<IKeymasterDevice> keymaster); keymaster()95 IKeymasterDevice& keymaster() { return *keymaster_; } os_version()96 uint32_t os_version() { return os_version_; } os_patch_level()97 uint32_t os_patch_level() { return os_patch_level_; } 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, 110 const AuthorizationSet& wrapping_key_desc, string masking_key, 111 const AuthorizationSet& unwrapping_params); 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 125 void CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id, 126 const HidlBuf& app_data, KeyCharacteristics* key_characteristics); 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 131 ErrorCode GetDebugInfo(DebugInfo* debug_info); 132 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); Finish(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 169 void CheckAesIncrementalEncryptOperation(BlockMode block_mode, int message_size); 170 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); 196 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, 197 uint8_t mac_length_bits, const HidlBuf& iv_in); 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 IsSecure()207 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; } SecLevel()208 SecurityLevel SecLevel() { return securityLevel_; } 209 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 216 std::vector<Digest> ValidDigests(bool withNone, bool withMD5); 217 std::vector<Digest> InvalidDigests(); 218 219 HidlBuf key_blob_; 220 KeyCharacteristics key_characteristics_; 221 OperationHandle op_handle_ = kOpHandleSentinel; 222 build_params()223 static std::vector<std::string> build_params() { 224 auto params = android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor); 225 return params; 226 } 227 228 private: 229 sp<IKeymasterDevice> keymaster_; 230 uint32_t os_version_; 231 uint32_t os_patch_level_; 232 233 SecurityLevel securityLevel_; 234 hidl_string name_; 235 hidl_string author_; 236 }; 237 238 #define INSTANTIATE_KEYMASTER_HIDL_TEST(name) \ 239 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(name); \ 240 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ 241 testing::ValuesIn(KeymasterHidlTest::build_params()), \ 242 android::hardware::PrintInstanceNameToString) 243 244 } // namespace test 245 } // namespace V4_0 246 } // namespace keymaster 247 } // namespace hardware 248 } // namespace android 249