1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "KeymasterHidlTest.h"
namespace android {
namespace hardware {
namespace keymaster {
namespace V4_0 {
namespace test {
/**
* HmacKeySharingTest extends KeymasterHidlTest with some utilities that make writing HMAC sharing
* tests easier.
*/
class HmacKeySharingTest : public KeymasterHidlTest {
protected:
const std::vector<sp<IKeymasterDevice>>& allKeymasters() {
if (all_keymasters_.empty()) {
auto names = android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor);
for (const auto& name : names) {
all_keymasters_.push_back(IKeymasterDevice::getService(name));
}
}
return all_keymasters_;
}
struct GetParamsResult {
ErrorCode error;
HmacSharingParameters params;
auto tie() { return std::tie(error, params); }
};
struct ComputeHmacResult {
ErrorCode error;
HidlBuf sharing_check;
auto tie() { return std::tie(error, sharing_check); }
};
using KeymasterVec = std::vector<sp<IKeymasterDevice>>;
using ByteString = std::basic_string<uint8_t>;
// using NonceVec = std::vector<HidlBuf>;
GetParamsResult getHmacSharingParameters(IKeymasterDevice& keymaster) {
GetParamsResult result;
EXPECT_TRUE(keymaster
.getHmacSharingParameters([&](auto error, auto params) {
result.tie() = std::tie(error, params);
})
.isOk());
return result;
}
hidl_vec<HmacSharingParameters> getHmacSharingParameters(const KeymasterVec& keymasters) {
std::vector<HmacSharingParameters> paramsVec;
for (auto& keymaster : keymasters) {
auto result = getHmacSharingParameters(*keymaster);
EXPECT_EQ(ErrorCode::OK, result.error);
if (result.error == ErrorCode::OK) paramsVec.push_back(std::move(result.params));
}
return paramsVec;
}
ComputeHmacResult computeSharedHmac(IKeymasterDevice& keymaster,
const hidl_vec<HmacSharingParameters>& params) {
ComputeHmacResult result;
EXPECT_TRUE(keymaster
.computeSharedHmac(params,
[&](auto error, auto params) {
result.tie() = std::tie(error, params);
})
.isOk());
return result;
}
std::vector<ComputeHmacResult> computeSharedHmac(
const KeymasterVec& keymasters, const hidl_vec<HmacSharingParameters>& paramsVec) {
std::vector<ComputeHmacResult> resultVec;
for (auto& keymaster : keymasters) {
resultVec.push_back(computeSharedHmac(*keymaster, paramsVec));
}
return resultVec;
}
std::vector<ByteString> copyNonces(const hidl_vec<HmacSharingParameters>& paramsVec) {
std::vector<ByteString> nonces;
for (auto& param : paramsVec) {
nonces.emplace_back(param.nonce.data(), param.nonce.size());
}
return nonces;
}
void verifyResponses(const HidlBuf& expected, const std::vector<ComputeHmacResult>& responses) {
for (auto& response : responses) {
EXPECT_EQ(ErrorCode::OK, response.error);
EXPECT_EQ(expected, response.sharing_check) << "Sharing check values should match.";
}
}
private:
static std::vector<sp<IKeymasterDevice>> all_keymasters_;
};
std::vector<sp<IKeymasterDevice>> HmacKeySharingTest::all_keymasters_;
TEST_P(HmacKeySharingTest, GetParameters) {
auto result1 = getHmacSharingParameters(keymaster());
EXPECT_EQ(ErrorCode::OK, result1.error);
auto result2 = getHmacSharingParameters(keymaster());
EXPECT_EQ(ErrorCode::OK, result2.error);
ASSERT_EQ(result1.params.seed, result2.params.seed)
<< "A given keymaster should always return the same seed.";
ASSERT_EQ(result1.params.nonce, result2.params.nonce)
<< "A given keymaster should always return the same nonce until restart.";
}
TEST_P(HmacKeySharingTest, ComputeSharedHmac) {
auto params = getHmacSharingParameters(allKeymasters());
ASSERT_EQ(allKeymasters().size(), params.size())
<< "One or more keymasters failed to provide parameters.";
auto nonces = copyNonces(params);
EXPECT_EQ(allKeymasters().size(), nonces.size());
std::sort(nonces.begin(), nonces.end());
std::unique(nonces.begin(), nonces.end());
EXPECT_EQ(allKeymasters().size(), nonces.size());
auto responses = computeSharedHmac(allKeymasters(), params);
ASSERT_GT(responses.size(), 0U);
verifyResponses(responses[0].sharing_check, responses);
// Do it a second time. Should get the same answers.
params = getHmacSharingParameters(allKeymasters());
ASSERT_EQ(allKeymasters().size(), params.size())
<< "One or more keymasters failed to provide parameters.";
responses = computeSharedHmac(allKeymasters(), params);
ASSERT_GT(responses.size(), 0U);
ASSERT_EQ(32U, responses[0].sharing_check.size());
verifyResponses(responses[0].sharing_check, responses);
}
template <class F>
class final_action {
public:
explicit final_action(F f) : f_(std::move(f)) {}
~final_action() { f_(); }
private:
F f_;
};
template <class F>
inline final_action<F> finally(const F& f) {
return final_action<F>(f);
}
TEST_P(HmacKeySharingTest, ComputeSharedHmacCorruptNonce) {
// Important: The execution of this test gets the keymaster implementations on the device out of
// sync with respect to the HMAC key. Granted that VTS tests aren't run on in-use production
// devices, this still has the potential to cause confusion. To mitigate that, we always
// (barring crashes :-/) re-run the unmodified agreement process on our way out.
auto fixup_hmac = finally([&]() {
computeSharedHmac(allKeymasters(), getHmacSharingParameters(allKeymasters()));
});
auto params = getHmacSharingParameters(allKeymasters());
ASSERT_EQ(allKeymasters().size(), params.size())
<< "One or more keymasters failed to provide parameters.";
// All should be well in the normal case
auto responses = computeSharedHmac(allKeymasters(), params);
ASSERT_GT(responses.size(), 0U);
HidlBuf correct_response = responses[0].sharing_check;
verifyResponses(correct_response, responses);
// Pick a random param, a random byte within the param's nonce, and a random bit within
// the byte. Flip that bit.
size_t param_to_tweak = rand() % params.size();
uint8_t byte_to_tweak = rand() % sizeof(params[param_to_tweak].nonce);
uint8_t bit_to_tweak = rand() % 8;
params[param_to_tweak].nonce[byte_to_tweak] ^= (1 << bit_to_tweak);
responses = computeSharedHmac(allKeymasters(), params);
for (size_t i = 0; i < responses.size(); ++i) {
if (i == param_to_tweak) {
EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, responses[i].error)
<< "Keymaster that provided tweaked param should fail to compute HMAC key";
} else {
EXPECT_EQ(ErrorCode::OK, responses[i].error) << "Others should succeed";
EXPECT_NE(correct_response, responses[i].sharing_check)
<< "Others should calculate a different HMAC key, due to the tweaked nonce.";
}
}
}
TEST_P(HmacKeySharingTest, ComputeSharedHmacCorruptSeed) {
// Important: The execution of this test gets the keymaster implementations on the device out of
// sync with respect to the HMAC key. Granted that VTS tests aren't run on in-use production
// devices, this still has the potential to cause confusion. To mitigate that, we always
// (barring crashes :-/) re-run the unmodified agreement process on our way out.
auto fixup_hmac = finally([&]() {
computeSharedHmac(allKeymasters(), getHmacSharingParameters(allKeymasters()));
});
auto params = getHmacSharingParameters(allKeymasters());
ASSERT_EQ(allKeymasters().size(), params.size())
<< "One or more keymasters failed to provide parameters.";
// All should be well in the normal case
auto responses = computeSharedHmac(allKeymasters(), params);
ASSERT_GT(responses.size(), 0U);
HidlBuf correct_response = responses[0].sharing_check;
verifyResponses(correct_response, responses);
// Pick a random param and modify the seed. We just increase the seed length by 1. It doesn't
// matter what value is in the additional byte; it changes the seed regardless.
auto param_to_tweak = rand() % params.size();
auto& to_tweak = params[param_to_tweak].seed;
ASSERT_TRUE(to_tweak.size() == 32 || to_tweak.size() == 0);
if (!to_tweak.size()) {
to_tweak.resize(32); // Contents don't matter; a little randomization is nice.
}
to_tweak[0]++;
responses = computeSharedHmac(allKeymasters(), params);
for (size_t i = 0; i < responses.size(); ++i) {
if (i == param_to_tweak) {
EXPECT_EQ(ErrorCode::INVALID_ARGUMENT, responses[i].error)
<< "Keymaster that provided tweaked param should fail to compute HMAC key ";
} else {
EXPECT_EQ(ErrorCode::OK, responses[i].error) << "Others should succeed";
EXPECT_NE(correct_response, responses[i].sharing_check)
<< "Others should calculate a different HMAC key, due to the tweaked nonce.";
}
}
}
INSTANTIATE_KEYMASTER_HIDL_TEST(HmacKeySharingTest);
} // namespace test
} // namespace V4_0
} // namespace keymaster
} // namespace hardware
} // namespace android
|