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 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 30 | namespace aidl::android::hardware::security::keymint::test { |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 31 | |
| 32 | using ::android::sp; |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 33 | using Status = ::ndk::ScopedAStatus; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 34 | using ::std::shared_ptr; |
| 35 | using ::std::string; |
| 36 | using ::std::vector; |
| 37 | |
| 38 | constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF; |
| 39 | |
| 40 | ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set); |
| 41 | |
| 42 | class KeyMintAidlTestBase : public ::testing::TestWithParam<string> { |
| 43 | public: |
| 44 | void SetUp() override; |
| 45 | void TearDown() override { |
| 46 | if (key_blob_.size()) { |
| 47 | CheckedDeleteKey(); |
| 48 | } |
| 49 | AbortIfNeeded(); |
| 50 | } |
| 51 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 52 | void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 53 | IKeyMintDevice& keyMint() { return *keymint_; } |
| 54 | uint32_t os_version() { return os_version_; } |
| 55 | uint32_t os_patch_level() { return os_patch_level_; } |
| 56 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 57 | ErrorCode GetReturnErrorCode(const Status& result); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 58 | ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob, |
| 59 | KeyCharacteristics* key_characteristics); |
| 60 | |
| 61 | ErrorCode GenerateKey(const AuthorizationSet& key_desc); |
| 62 | |
| 63 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 64 | const string& key_material, vector<uint8_t>* key_blob, |
| 65 | KeyCharacteristics* key_characteristics); |
| 66 | ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format, |
| 67 | const string& key_material); |
| 68 | |
| 69 | ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key, |
| 70 | const AuthorizationSet& wrapping_key_desc, string masking_key, |
| 71 | const AuthorizationSet& unwrapping_params); |
| 72 | |
| 73 | ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 74 | ErrorCode DeleteKey(bool keep_key_blob = false); |
| 75 | |
| 76 | ErrorCode DeleteAllKeys(); |
| 77 | |
| 78 | void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false); |
| 79 | void CheckedDeleteKey(); |
| 80 | |
| 81 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 82 | const AuthorizationSet& in_params, AuthorizationSet* out_params, |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 83 | std::shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 84 | ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob, |
| 85 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 86 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params, |
| 87 | AuthorizationSet* out_params); |
| 88 | ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params); |
| 89 | |
| 90 | ErrorCode Update(const AuthorizationSet& in_params, const string& input, |
| 91 | AuthorizationSet* out_params, string* output, int32_t* input_consumed); |
| 92 | ErrorCode Update(const string& input, string* out, int32_t* input_consumed); |
| 93 | |
| 94 | ErrorCode Finish(const AuthorizationSet& in_params, const string& input, |
| 95 | const string& signature, AuthorizationSet* out_params, string* output); |
| 96 | ErrorCode Finish(const string& message, string* output); |
| 97 | ErrorCode Finish(const string& message, const string& signature, string* output); |
| 98 | ErrorCode Finish(string* output) { return Finish(string(), output); } |
| 99 | |
| 100 | ErrorCode Abort(); |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 101 | ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 102 | void AbortIfNeeded(); |
| 103 | |
| 104 | string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation, |
| 105 | const string& message, const AuthorizationSet& in_params, |
| 106 | AuthorizationSet* out_params); |
| 107 | |
| 108 | string SignMessage(const vector<uint8_t>& key_blob, const string& message, |
| 109 | const AuthorizationSet& params); |
| 110 | string SignMessage(const string& message, const AuthorizationSet& params); |
| 111 | |
| 112 | string MacMessage(const string& message, Digest digest, size_t mac_length); |
| 113 | |
| 114 | void CheckHmacTestVector(const string& key, const string& message, Digest digest, |
| 115 | const string& expected_mac); |
| 116 | |
| 117 | void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message, |
| 118 | const string& expected_ciphertext); |
| 119 | |
| 120 | void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode, |
| 121 | PaddingMode padding_mode, const string& key, const string& iv, |
| 122 | const string& input, const string& expected_output); |
| 123 | |
| 124 | void VerifyMessage(const vector<uint8_t>& key_blob, const string& message, |
| 125 | const string& signature, const AuthorizationSet& params); |
| 126 | void VerifyMessage(const string& message, const string& signature, |
| 127 | const AuthorizationSet& params); |
| 128 | |
| 129 | string EncryptMessage(const vector<uint8_t>& key_blob, const string& message, |
| 130 | const AuthorizationSet& in_params, AuthorizationSet* out_params); |
| 131 | string EncryptMessage(const string& message, const AuthorizationSet& params, |
| 132 | AuthorizationSet* out_params); |
| 133 | string EncryptMessage(const string& message, const AuthorizationSet& params); |
| 134 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding); |
| 135 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 136 | vector<uint8_t>* iv_out); |
| 137 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 138 | const vector<uint8_t>& iv_in); |
| 139 | string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding, |
| 140 | uint8_t mac_length_bits, const vector<uint8_t>& iv_in); |
| 141 | |
| 142 | string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext, |
| 143 | const AuthorizationSet& params); |
| 144 | string DecryptMessage(const string& ciphertext, const AuthorizationSet& params); |
| 145 | string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode, |
| 146 | const vector<uint8_t>& iv); |
| 147 | |
| 148 | std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob); |
| 149 | |
| 150 | bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; } |
| 151 | SecurityLevel SecLevel() { return securityLevel_; } |
| 152 | |
| 153 | vector<uint32_t> ValidKeySizes(Algorithm algorithm); |
| 154 | vector<uint32_t> InvalidKeySizes(Algorithm algorithm); |
| 155 | |
| 156 | vector<EcCurve> ValidCurves(); |
| 157 | vector<EcCurve> InvalidCurves(); |
| 158 | |
| 159 | vector<Digest> ValidDigests(bool withNone, bool withMD5); |
| 160 | |
| 161 | static vector<string> build_params() { |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 162 | auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor); |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 163 | return params; |
| 164 | } |
| 165 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 166 | std::shared_ptr<IKeyMintOperation> op_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 167 | vector<Certificate> certChain_; |
| 168 | vector<uint8_t> key_blob_; |
| 169 | KeyCharacteristics key_characteristics_; |
| 170 | |
| 171 | private: |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 172 | std::shared_ptr<IKeyMintDevice> keymint_; |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 173 | uint32_t os_version_; |
| 174 | uint32_t os_patch_level_; |
| 175 | |
| 176 | SecurityLevel securityLevel_; |
| 177 | string name_; |
| 178 | string author_; |
| 179 | long challenge_; |
| 180 | }; |
| 181 | |
| 182 | #define INSTANTIATE_KEYMINT_AIDL_TEST(name) \ |
| 183 | INSTANTIATE_TEST_SUITE_P(PerInstance, name, \ |
| 184 | testing::ValuesIn(KeyMintAidlTestBase::build_params()), \ |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 185 | ::android::PrintInstanceNameToString) |
Selene Huang | 31ab404 | 2020-04-29 04:22:39 -0700 | [diff] [blame] | 186 | |
Janis Danisevskis | 24c0470 | 2020-12-16 18:28:39 -0800 | [diff] [blame^] | 187 | } // namespace aidl::android::hardware::security::keymint::test |