blob: 780971dc8cfbfb492e111808e776ad2065444cfd [file] [log] [blame]
Selene Huang31ab4042020-04-29 04:22:39 -07001/*
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 Huang31ab4042020-04-29 04:22:39 -070017#pragma once
18
19#include <aidl/Gtest.h>
20#include <aidl/Vintf.h>
Selene Huang31ab4042020-04-29 04:22:39 -070021#include <binder/IServiceManager.h>
22#include <binder/ProcessState.h>
23#include <gtest/gtest.h>
24
Janis Danisevskis24c04702020-12-16 18:28:39 -080025#include <aidl/android/hardware/security/keymint/ErrorCode.h>
26#include <aidl/android/hardware/security/keymint/IKeyMintDevice.h>
Shawn Willden1d3f85e2020-12-09 14:18:44 -070027
Shawn Willden08a7e432020-12-11 13:05:27 +000028#include <keymint_support/authorization_set.h>
Selene Huang31ab4042020-04-29 04:22:39 -070029
Shawn Willden0e80b5d2020-12-17 09:07:27 -070030namespace aidl::android::hardware::security::keymint {
31
32::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set);
33
34namespace test {
Selene Huang31ab4042020-04-29 04:22:39 -070035
36using ::android::sp;
Janis Danisevskis24c04702020-12-16 18:28:39 -080037using Status = ::ndk::ScopedAStatus;
Selene Huang31ab4042020-04-29 04:22:39 -070038using ::std::shared_ptr;
39using ::std::string;
40using ::std::vector;
41
42constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
43
Selene Huang31ab4042020-04-29 04:22:39 -070044class KeyMintAidlTestBase : public ::testing::TestWithParam<string> {
45 public:
46 void SetUp() override;
47 void TearDown() override {
48 if (key_blob_.size()) {
49 CheckedDeleteKey();
50 }
51 AbortIfNeeded();
52 }
53
Janis Danisevskis24c04702020-12-16 18:28:39 -080054 void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint);
Selene Huang31ab4042020-04-29 04:22:39 -070055 IKeyMintDevice& keyMint() { return *keymint_; }
56 uint32_t os_version() { return os_version_; }
57 uint32_t os_patch_level() { return os_patch_level_; }
58
Janis Danisevskis24c04702020-12-16 18:28:39 -080059 ErrorCode GetReturnErrorCode(const Status& result);
Selene Huang31ab4042020-04-29 04:22:39 -070060 ErrorCode GenerateKey(const AuthorizationSet& key_desc, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -070061 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -070062
63 ErrorCode GenerateKey(const AuthorizationSet& key_desc);
64
65 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
66 const string& key_material, vector<uint8_t>* key_blob,
Shawn Willden7f424372021-01-10 18:06:50 -070067 vector<KeyCharacteristics>* key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -070068 ErrorCode ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
69 const string& key_material);
70
71 ErrorCode ImportWrappedKey(string wrapped_key, string wrapping_key,
72 const AuthorizationSet& wrapping_key_desc, string masking_key,
73 const AuthorizationSet& unwrapping_params);
74
75 ErrorCode DeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
76 ErrorCode DeleteKey(bool keep_key_blob = false);
77
78 ErrorCode DeleteAllKeys();
79
80 void CheckedDeleteKey(vector<uint8_t>* key_blob, bool keep_key_blob = false);
81 void CheckedDeleteKey();
82
83 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
84 const AuthorizationSet& in_params, AuthorizationSet* out_params,
Janis Danisevskis24c04702020-12-16 18:28:39 -080085 std::shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -070086 ErrorCode Begin(KeyPurpose purpose, const vector<uint8_t>& key_blob,
87 const AuthorizationSet& in_params, AuthorizationSet* out_params);
88 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
89 AuthorizationSet* out_params);
90 ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params);
91
92 ErrorCode Update(const AuthorizationSet& in_params, const string& input,
93 AuthorizationSet* out_params, string* output, int32_t* input_consumed);
94 ErrorCode Update(const string& input, string* out, int32_t* input_consumed);
95
96 ErrorCode Finish(const AuthorizationSet& in_params, const string& input,
97 const string& signature, AuthorizationSet* out_params, string* output);
98 ErrorCode Finish(const string& message, string* output);
99 ErrorCode Finish(const string& message, const string& signature, string* output);
100 ErrorCode Finish(string* output) { return Finish(string(), output); }
101
102 ErrorCode Abort();
Janis Danisevskis24c04702020-12-16 18:28:39 -0800103 ErrorCode Abort(const shared_ptr<IKeyMintOperation>& op);
Selene Huang31ab4042020-04-29 04:22:39 -0700104 void AbortIfNeeded();
105
106 string ProcessMessage(const vector<uint8_t>& key_blob, KeyPurpose operation,
107 const string& message, const AuthorizationSet& in_params,
108 AuthorizationSet* out_params);
109
110 string SignMessage(const vector<uint8_t>& key_blob, const string& message,
111 const AuthorizationSet& params);
112 string SignMessage(const string& message, const AuthorizationSet& params);
113
114 string MacMessage(const string& message, Digest digest, size_t mac_length);
115
116 void CheckHmacTestVector(const string& key, const string& message, Digest digest,
117 const string& expected_mac);
118
119 void CheckAesCtrTestVector(const string& key, const string& nonce, const string& message,
120 const string& expected_ciphertext);
121
122 void CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
123 PaddingMode padding_mode, const string& key, const string& iv,
124 const string& input, const string& expected_output);
125
126 void VerifyMessage(const vector<uint8_t>& key_blob, const string& message,
127 const string& signature, const AuthorizationSet& params);
128 void VerifyMessage(const string& message, const string& signature,
129 const AuthorizationSet& params);
130
131 string EncryptMessage(const vector<uint8_t>& key_blob, const string& message,
132 const AuthorizationSet& in_params, AuthorizationSet* out_params);
133 string EncryptMessage(const string& message, const AuthorizationSet& params,
134 AuthorizationSet* out_params);
135 string EncryptMessage(const string& message, const AuthorizationSet& params);
136 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding);
137 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
138 vector<uint8_t>* iv_out);
139 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
140 const vector<uint8_t>& iv_in);
141 string EncryptMessage(const string& message, BlockMode block_mode, PaddingMode padding,
142 uint8_t mac_length_bits, const vector<uint8_t>& iv_in);
143
144 string DecryptMessage(const vector<uint8_t>& key_blob, const string& ciphertext,
145 const AuthorizationSet& params);
146 string DecryptMessage(const string& ciphertext, const AuthorizationSet& params);
147 string DecryptMessage(const string& ciphertext, BlockMode block_mode, PaddingMode padding_mode,
148 const vector<uint8_t>& iv);
149
150 std::pair<ErrorCode, vector<uint8_t>> UpgradeKey(const vector<uint8_t>& key_blob);
151
Shawn Willden7f424372021-01-10 18:06:50 -0700152 bool IsSecure() const { return securityLevel_ != SecurityLevel::SOFTWARE; }
153 SecurityLevel SecLevel() const { return securityLevel_; }
Selene Huang31ab4042020-04-29 04:22:39 -0700154
155 vector<uint32_t> ValidKeySizes(Algorithm algorithm);
156 vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
157
158 vector<EcCurve> ValidCurves();
159 vector<EcCurve> InvalidCurves();
160
161 vector<Digest> ValidDigests(bool withNone, bool withMD5);
162
163 static vector<string> build_params() {
Janis Danisevskis24c04702020-12-16 18:28:39 -0800164 auto params = ::android::getAidlHalInstanceNames(IKeyMintDevice::descriptor);
Selene Huang31ab4042020-04-29 04:22:39 -0700165 return params;
166 }
167
Janis Danisevskis24c04702020-12-16 18:28:39 -0800168 std::shared_ptr<IKeyMintOperation> op_;
Shawn Willden7f424372021-01-10 18:06:50 -0700169 vector<Certificate> cert_chain_;
Selene Huang31ab4042020-04-29 04:22:39 -0700170 vector<uint8_t> key_blob_;
Shawn Willden7f424372021-01-10 18:06:50 -0700171 vector<KeyCharacteristics> key_characteristics_;
172
173 const vector<KeyParameter>& SecLevelAuthorizations(
174 const vector<KeyCharacteristics>& key_characteristics);
175 inline const vector<KeyParameter>& SecLevelAuthorizations() {
176 return SecLevelAuthorizations(key_characteristics_);
177 }
Qi Wubeefae42021-01-28 23:16:37 +0800178 const vector<KeyParameter>& SecLevelAuthorizations(
179 const vector<KeyCharacteristics>& key_characteristics, SecurityLevel securityLevel);
180
181 AuthorizationSet HwEnforcedAuthorizations(
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700182 const vector<KeyCharacteristics>& key_characteristics);
Qi Wubeefae42021-01-28 23:16:37 +0800183 AuthorizationSet SwEnforcedAuthorizations(
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700184 const vector<KeyCharacteristics>& key_characteristics);
Selene Huang31ab4042020-04-29 04:22:39 -0700185
186 private:
Janis Danisevskis24c04702020-12-16 18:28:39 -0800187 std::shared_ptr<IKeyMintDevice> keymint_;
Selene Huang31ab4042020-04-29 04:22:39 -0700188 uint32_t os_version_;
189 uint32_t os_patch_level_;
190
191 SecurityLevel securityLevel_;
192 string name_;
193 string author_;
194 long challenge_;
195};
196
197#define INSTANTIATE_KEYMINT_AIDL_TEST(name) \
198 INSTANTIATE_TEST_SUITE_P(PerInstance, name, \
199 testing::ValuesIn(KeyMintAidlTestBase::build_params()), \
Janis Danisevskis24c04702020-12-16 18:28:39 -0800200 ::android::PrintInstanceNameToString)
Selene Huang31ab4042020-04-29 04:22:39 -0700201
Shawn Willden0e80b5d2020-12-17 09:07:27 -0700202} // namespace test
203
204} // namespace aidl::android::hardware::security::keymint