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 CheckHmacTestVector(const string& key, const string& message, Digest digest, 170 const string& expected_mac); 171 172 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, 173 const string& expected_ciphertext); 174 175 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, 176 PaddingMode padding_mode, const string& key, const string& iv, 177 const string& input, const string& expected_output); 178 179 void VerifyMessage(const HidlBuf& key_blob, const string& message, const string& signature, 180 const AuthorizationSet& params); 181 void VerifyMessage(const string& message, const string& signature, 182 const AuthorizationSet& params); 183 184 string EncryptMessage(const HidlBuf& key_blob, const string& message, 185 const AuthorizationSet& in_params, AuthorizationSet* out_params); 186 string EncryptMessage(const string& message, const AuthorizationSet& params, 187 AuthorizationSet* out_params); 188 string EncryptMessage(const string& message, const AuthorizationSet& params); 189 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); 190 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, 191 HidlBuf* iv_out); 192 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, 193 const HidlBuf& iv_in); 194 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, 195 uint8_t mac_length_bits, const HidlBuf& iv_in); 196 197 string DecryptMessage(const HidlBuf& key_blob, const string& ciphertext, 198 const AuthorizationSet& params); 199 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); 200 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, 201 const HidlBuf& iv); 202 203 std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob); 204 IsSecure()205 bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; } SecLevel()206 SecurityLevel SecLevel() { return securityLevel_; } 207 208 std::vector<uint32_t> ValidKeySizes(Algorithm algorithm); 209 std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm); 210 211 std::vector<EcCurve> ValidCurves(); 212 std::vector<EcCurve> InvalidCurves(); 213 214 std::vector<Digest> ValidDigests(bool withNone, bool withMD5); 215 std::vector<Digest> InvalidDigests(); 216 217 HidlBuf key_blob_; 218 KeyCharacteristics key_characteristics_; 219 OperationHandle op_handle_ = kOpHandleSentinel; 220 build_params()221 static std::vector<std::string> build_params() { 222 auto params = android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor); 223 return params; 224 } 225 226 private: 227 sp<IKeymasterDevice> keymaster_; 228 uint32_t os_version_; 229 uint32_t os_patch_level_; 230 231 SecurityLevel securityLevel_; 232 hidl_string name_; 233 hidl_string author_; 234 }; 235 236 #define INSTANTIATE_KEYMASTER_HIDL_TEST(name) \ 237 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ 238 testing::ValuesIn(KeymasterHidlTest::build_params()), \ 239 android::hardware::PrintInstanceNameToString) 240 241 } // namespace test 242 } // namespace V4_0 243 } // namespace keymaster 244 } // namespace hardware 245 } // namespace android 246