Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <aidl/Gtest.h> |
| 20 | #include <aidl/Vintf.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 21 | #include <binder/IServiceManager.h> |
| 22 | #include <binder/ProcessState.h> |
| 23 | #include <gtest/gtest.h> |
| 24 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 25 | #include <aidl/android/hardware/security/keymint/ErrorCode.h> |
| 26 | #include <aidl/android/hardware/security/keymint/IKeyMintDevice.h> |
Shawn Willden | 1d3f85e | 2020-12-09 14:18:44 -0700 | [diff] [blame] | 27 | |
Shawn Willden | 08a7e43 | 2020-12-11 13:05:27 +0000 | [diff] [blame] | 28 | #include <keymint_support/authorization_set.h> |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 29 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 30 | namespace aidl::android::hardware::security::keymint { |
| 31 | |
| 32 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 33 | |
| 34 | namespace test { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 35 | |
| 36 | using ::android::sp; |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 37 | using Status = ::ndk::ScopedAStatus; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 38 | using ::std::shared_ptr; |
| 39 | using ::std::string; |
| 40 | using ::std::vector; |
| 41 | |
| 42 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 43 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 44 | class KeyMintAidlTestBase : public ::testing::TestWithParam<string> { |
| 45 | public: |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 46 | struct KeyData { |
| 47 | vector<uint8_t> blob; |
| 48 | vector<KeyCharacteristics> characteristics; |
| 49 | }; |
| 50 | |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 51 | void SetUp() override; |
| 52 | void TearDown() override { |
| 53 | if (key_blob_.size()) { |
| 54 | CheckedDeleteKey(); |
| 55 | } |
| 56 | AbortIfNeeded(); |
| 57 | } |
| 58 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 59 | void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 60 | IKeyMintDevice& keyMint() { return *keymint_; } |
| 61 | uint32_t os_version() { return os_version_; } |
| 62 | uint32_t os_patch_level() { return os_patch_level_; } |
| 63 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 64 | ErrorCode GetReturnErrorCode(const Status& result); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 65 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 66 | vector<KeyCharacteristics>* key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 67 | |
| 68 | ErrorCode GenerateKey(const AuthorizationSet& key_desc); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 69 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 70 | const string& key_material, vector<uint8_t>* key_blob, |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 71 | vector<KeyCharacteristics>* key_characteristics); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 72 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 73 | const string& key_material); |
| 74 | |
| 75 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 76 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 77 | const AuthorizationSet& unwrapping_params); |
| 78 | |
| 79 | ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 80 | ErrorCode DeleteKey(bool keep_key_blob = false); |
| 81 | |
| 82 | ErrorCode DeleteAllKeys(); |
| 83 | |
| 84 | void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 85 | void CheckedDeleteKey(); |
| 86 | |
| 87 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 88 | const AuthorizationSet& in_params, AuthorizationSet* out_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 89 | std::shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 90 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 91 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 92 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 93 | AuthorizationSet* out_params); |
| 94 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params); |
| 95 | |
| 96 | ErrorCode Update(const AuthorizationSet& in_params, const string& input, |
| 97 | AuthorizationSet* out_params, string* output, int32_t* input_consumed); |
| 98 | ErrorCode Update(const string& input, string* out, int32_t* input_consumed); |
| 99 | |
| 100 | ErrorCode Finish(const AuthorizationSet& in_params, const string& input, |
| 101 | const string& signature, AuthorizationSet* out_params, string* output); |
| 102 | ErrorCode Finish(const string& message, string* output); |
| 103 | ErrorCode Finish(const string& message, const string& signature, string* output); |
| 104 | ErrorCode Finish(string* output) { return Finish(string(), output); } |
| 105 | |
| 106 | ErrorCode Abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 107 | ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 108 | void AbortIfNeeded(); |
| 109 | |
| 110 | string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 111 | const string& message, const AuthorizationSet& in_params, |
| 112 | AuthorizationSet* out_params); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 113 | std::tuple<ErrorCode, std::string /* processedMessage */, AuthorizationSet /* out_params */> |
| 114 | ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 115 | const std::string& message, const AuthorizationSet& in_params); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 116 | string SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 117 | const AuthorizationSet& params); |
| 118 | string SignMessage(const string& message, const AuthorizationSet& params); |
| 119 | |
| 120 | string MacMessage(const string& message, Digest digest, size_t mac_length); |
| 121 | |
| 122 | void CheckHmacTestVector(const string& key, const string& message, Digest digest, |
| 123 | const string& expected_mac); |
| 124 | |
| 125 | void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, |
| 126 | const string& expected_ciphertext); |
| 127 | |
| 128 | void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 129 | PaddingMode padding_mode, const string& key, const string& iv, |
| 130 | const string& input, const string& expected_output); |
| 131 | |
| 132 | void VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 133 | const string& signature, const AuthorizationSet& params); |
| 134 | void VerifyMessage(const string& message, const string& signature, |
| 135 | const AuthorizationSet& params); |
| 136 | |
| 137 | string EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 138 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 139 | string EncryptMessage(const string& message, const AuthorizationSet& params, |
| 140 | AuthorizationSet* out_params); |
| 141 | string EncryptMessage(const string& message, const AuthorizationSet& params); |
| 142 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); |
| 143 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 144 | vector<uint8_t>* iv_out); |
| 145 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 146 | const vector<uint8_t>& iv_in); |
| 147 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 148 | uint8_t mac_length_bits, const vector<uint8_t>& iv_in); |
| 149 | |
| 150 | string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext, |
| 151 | const AuthorizationSet& params); |
| 152 | string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); |
| 153 | string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, |
| 154 | const vector<uint8_t>& iv); |
| 155 | |
| 156 | std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob); |
| 157 | |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 158 | template <typename TagType> |
| 159 | std::tuple<KeyData /* aesKey */, KeyData /* hmacKey */, KeyData /* rsaKey */, |
| 160 | KeyData /* ecdsaKey */> |
| 161 | CreateTestKeys(TagType tagToTest, ErrorCode expectedReturn) { |
| 162 | /* AES */ |
| 163 | KeyData aesKeyData; |
| 164 | ErrorCode errorCode = GenerateKey(AuthorizationSetBuilder() |
| 165 | .AesEncryptionKey(128) |
| 166 | .Authorization(tagToTest) |
| 167 | .BlockMode(BlockMode::ECB) |
| 168 | .Padding(PaddingMode::NONE) |
| 169 | .Authorization(TAG_NO_AUTH_REQUIRED), |
| 170 | &aesKeyData.blob, &aesKeyData.characteristics); |
| 171 | EXPECT_EQ(expectedReturn, errorCode); |
| 172 | |
| 173 | /* HMAC */ |
| 174 | KeyData hmacKeyData; |
| 175 | errorCode = GenerateKey(AuthorizationSetBuilder() |
| 176 | .HmacKey(128) |
| 177 | .Authorization(tagToTest) |
| 178 | .Digest(Digest::SHA_2_256) |
| 179 | .Authorization(TAG_MIN_MAC_LENGTH, 128) |
| 180 | .Authorization(TAG_NO_AUTH_REQUIRED), |
| 181 | &hmacKeyData.blob, &hmacKeyData.characteristics); |
| 182 | EXPECT_EQ(expectedReturn, errorCode); |
| 183 | |
| 184 | /* RSA */ |
| 185 | KeyData rsaKeyData; |
| 186 | errorCode = GenerateKey(AuthorizationSetBuilder() |
| 187 | .RsaSigningKey(2048, 65537) |
| 188 | .Authorization(tagToTest) |
| 189 | .Digest(Digest::NONE) |
| 190 | .Padding(PaddingMode::NONE) |
| 191 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 192 | .SetDefaultValidity(), |
| 193 | &rsaKeyData.blob, &rsaKeyData.characteristics); |
| 194 | EXPECT_EQ(expectedReturn, errorCode); |
| 195 | |
| 196 | /* ECDSA */ |
| 197 | KeyData ecdsaKeyData; |
| 198 | errorCode = GenerateKey(AuthorizationSetBuilder() |
| 199 | .EcdsaSigningKey(256) |
| 200 | .Authorization(tagToTest) |
| 201 | .Digest(Digest::SHA_2_256) |
| 202 | .Authorization(TAG_NO_AUTH_REQUIRED) |
| 203 | .SetDefaultValidity(), |
| 204 | &ecdsaKeyData.blob, &ecdsaKeyData.characteristics); |
| 205 | EXPECT_EQ(expectedReturn, errorCode); |
| 206 | return {aesKeyData, hmacKeyData, rsaKeyData, ecdsaKeyData}; |
| 207 | } |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 208 | bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 209 | SecurityLevel SecLevel() const { return securityLevel_; } |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 210 | |
| 211 | vector<uint32_t> ValidKeySizes(Algorithm algorithm); |
| 212 | vector<uint32_t> InvalidKeySizes(Algorithm algorithm); |
| 213 | |
| 214 | vector<EcCurve> ValidCurves(); |
| 215 | vector<EcCurve> InvalidCurves(); |
| 216 | |
| 217 | vector<Digest> ValidDigests(bool withNone, bool withMD5); |
| 218 | |
| 219 | static vector<string> build_params() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 220 | auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 221 | return params; |
| 222 | } |
| 223 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 224 | std::shared_ptr<IKeyMintOperation> op_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 225 | vector<Certificate> cert_chain_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 226 | vector<uint8_t> key_blob_; |
Shawn Willden | 7f42437 | 2021-01-10 18:06:50 -0700 | [diff] [blame] | 227 | vector<KeyCharacteristics> key_characteristics_; |
| 228 | |
| 229 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 230 | const vector<KeyCharacteristics>& key_characteristics); |
| 231 | inline const vector<KeyParameter>& SecLevelAuthorizations() { |
| 232 | return SecLevelAuthorizations(key_characteristics_); |
| 233 | } |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 234 | const vector<KeyParameter>& SecLevelAuthorizations( |
| 235 | const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel); |
| 236 | |
| 237 | AuthorizationSet HwEnforcedAuthorizations( |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 238 | const vector<KeyCharacteristics>& key_characteristics); |
Qi Wu | beefae4 | 2021-01-28 23:16:37 +0800 | [diff] [blame] | 239 | AuthorizationSet SwEnforcedAuthorizations( |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 240 | const vector<KeyCharacteristics>& key_characteristics); |
Chirag Pathak | 9ea6a0a | 2021-02-01 23:54:27 +0000 | [diff] [blame] | 241 | ErrorCode UseAesKey(const vector<uint8_t>& aesKeyBlob); |
| 242 | ErrorCode UseHmacKey(const vector<uint8_t>& hmacKeyBlob); |
| 243 | ErrorCode UseRsaKey(const vector<uint8_t>& rsaKeyBlob); |
| 244 | ErrorCode UseEcdsaKey(const vector<uint8_t>& ecdsaKeyBlob); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 245 | |
| 246 | private: |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 247 | std::shared_ptr<IKeyMintDevice> keymint_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 248 | uint32_t os_version_; |
| 249 | uint32_t os_patch_level_; |
| 250 | |
| 251 | SecurityLevel securityLevel_; |
| 252 | string name_; |
| 253 | string author_; |
| 254 | long challenge_; |
| 255 | }; |
| 256 | |
| 257 | #define INSTANTIATE_KEYMINT_AIDL_TEST(name) \ |
| 258 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 259 | testing::ValuesIn(KeyMintAidlTestBase::build_params()), \ |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame] | 260 | ::android::PrintInstanceNameToString) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 261 | |
Shawn Willden | 0e80b5d | 2020-12-17 09:07:27 -0700 | [diff] [blame] | 262 | } // namespace test |
| 263 | |
| 264 | } // namespace aidl::android::hardware::security::keymint |