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