1 /*
2 * Copyright (C) 2017 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 #include "KeymasterHidlTest.h"
18
19 #include <chrono>
20 #include <vector>
21
22 #include <android-base/logging.h>
23 #include <android/hidl/manager/1.0/IServiceManager.h>
24
25 #include <keymasterV4_0/key_param_output.h>
26 #include <keymasterV4_0/keymaster_utils.h>
27
28 namespace android {
29 namespace hardware {
30 namespace keymaster {
31 namespace V4_0 {
32
operator <<(::std::ostream & os,const AuthorizationSet & set)33 ::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set) {
34 if (set.size() == 0)
35 os << "(Empty)" << ::std::endl;
36 else {
37 os << "\n";
38 for (size_t i = 0; i < set.size(); ++i) os << set[i] << ::std::endl;
39 }
40 return os;
41 }
42
43 namespace test {
44
45 using namespace std::literals::chrono_literals;
46
InitializeKeymaster(sp<IKeymasterDevice> keymaster)47 void KeymasterHidlTest::InitializeKeymaster(sp<IKeymasterDevice> keymaster) {
48 ASSERT_NE(keymaster, nullptr);
49 keymaster_ = keymaster;
50 ASSERT_TRUE(keymaster_
51 ->getHardwareInfo([&](SecurityLevel securityLevel, const hidl_string& name,
52 const hidl_string& author) {
53 securityLevel_ = securityLevel;
54 name_ = name;
55 author_ = author;
56 })
57 .isOk());
58
59 os_version_ = support::getOsVersion();
60 os_patch_level_ = support::getOsPatchlevel();
61 }
62
SetUp()63 void KeymasterHidlTest::SetUp() {
64 InitializeKeymaster(IKeymasterDevice::getService(GetParam()));
65 }
66
GenerateKey(const AuthorizationSet & key_desc,HidlBuf * key_blob,KeyCharacteristics * key_characteristics)67 ErrorCode KeymasterHidlTest::GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
68 KeyCharacteristics* key_characteristics) {
69 EXPECT_NE(key_blob, nullptr) << "Key blob pointer must not be null. Test bug";
70 EXPECT_EQ(0U, key_blob->size()) << "Key blob not empty before generating key. Test bug.";
71 EXPECT_NE(key_characteristics, nullptr)
72 << "Previous characteristics not deleted before generating key. Test bug.";
73
74 ErrorCode error;
75 EXPECT_TRUE(keymaster_
76 ->generateKey(key_desc.hidl_data(),
77 [&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
78 const KeyCharacteristics& hidl_key_characteristics) {
79 error = hidl_error;
80 *key_blob = hidl_key_blob;
81 *key_characteristics = hidl_key_characteristics;
82 })
83 .isOk());
84 // On error, blob & characteristics should be empty.
85 if (error != ErrorCode::OK) {
86 EXPECT_EQ(0U, key_blob->size());
87 EXPECT_EQ(0U, (key_characteristics->softwareEnforced.size() +
88 key_characteristics->hardwareEnforced.size()));
89 }
90 return error;
91 }
92
GenerateKey(const AuthorizationSet & key_desc)93 ErrorCode KeymasterHidlTest::GenerateKey(const AuthorizationSet& key_desc) {
94 return GenerateKey(key_desc, &key_blob_, &key_characteristics_);
95 }
96
ImportKey(const AuthorizationSet & key_desc,KeyFormat format,const string & key_material,HidlBuf * key_blob,KeyCharacteristics * key_characteristics)97 ErrorCode KeymasterHidlTest::ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
98 const string& key_material, HidlBuf* key_blob,
99 KeyCharacteristics* key_characteristics) {
100 ErrorCode error;
101 EXPECT_TRUE(keymaster_
102 ->importKey(key_desc.hidl_data(), format, HidlBuf(key_material),
103 [&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
104 const KeyCharacteristics& hidl_key_characteristics) {
105 error = hidl_error;
106 *key_blob = hidl_key_blob;
107 *key_characteristics = hidl_key_characteristics;
108 })
109 .isOk());
110 // On error, blob & characteristics should be empty.
111 if (error != ErrorCode::OK) {
112 EXPECT_EQ(0U, key_blob->size());
113 EXPECT_EQ(0U, (key_characteristics->softwareEnforced.size() +
114 key_characteristics->hardwareEnforced.size()));
115 }
116 return error;
117 }
118
ImportKey(const AuthorizationSet & key_desc,KeyFormat format,const string & key_material)119 ErrorCode KeymasterHidlTest::ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
120 const string& key_material) {
121 return ImportKey(key_desc, format, key_material, &key_blob_, &key_characteristics_);
122 }
123
ImportWrappedKey(string wrapped_key,string wrapping_key,const AuthorizationSet & wrapping_key_desc,string masking_key,const AuthorizationSet & unwrapping_params)124 ErrorCode KeymasterHidlTest::ImportWrappedKey(string wrapped_key, string wrapping_key,
125 const AuthorizationSet& wrapping_key_desc,
126 string masking_key,
127 const AuthorizationSet& unwrapping_params) {
128 ErrorCode error;
129 EXPECT_EQ(ErrorCode::OK, ImportKey(wrapping_key_desc, KeyFormat::PKCS8, wrapping_key));
130 EXPECT_TRUE(keymaster_
131 ->importWrappedKey(HidlBuf(wrapped_key), key_blob_, HidlBuf(masking_key),
132 unwrapping_params.hidl_data(), 0 /* passwordSid */,
133 0 /* biometricSid */,
134 [&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
135 const KeyCharacteristics& hidl_key_characteristics) {
136 error = hidl_error;
137 key_blob_ = hidl_key_blob;
138 key_characteristics_ = hidl_key_characteristics;
139 })
140 .isOk());
141 return error;
142 }
143
ExportKey(KeyFormat format,const HidlBuf & key_blob,const HidlBuf & client_id,const HidlBuf & app_data,HidlBuf * key_material)144 ErrorCode KeymasterHidlTest::ExportKey(KeyFormat format, const HidlBuf& key_blob,
145 const HidlBuf& client_id, const HidlBuf& app_data,
146 HidlBuf* key_material) {
147 ErrorCode error;
148 EXPECT_TRUE(keymaster_
149 ->exportKey(format, key_blob, client_id, app_data,
150 [&](ErrorCode hidl_error_code, const HidlBuf& hidl_key_material) {
151 error = hidl_error_code;
152 *key_material = hidl_key_material;
153 })
154 .isOk());
155 // On error, blob should be empty.
156 if (error != ErrorCode::OK) {
157 EXPECT_EQ(0U, key_material->size());
158 }
159 return error;
160 }
161
ExportKey(KeyFormat format,HidlBuf * key_material)162 ErrorCode KeymasterHidlTest::ExportKey(KeyFormat format, HidlBuf* key_material) {
163 HidlBuf client_id, app_data;
164 return ExportKey(format, key_blob_, client_id, app_data, key_material);
165 }
166
DeleteKey(HidlBuf * key_blob,bool keep_key_blob)167 ErrorCode KeymasterHidlTest::DeleteKey(HidlBuf* key_blob, bool keep_key_blob) {
168 auto rc = keymaster_->deleteKey(*key_blob);
169 if (!keep_key_blob) *key_blob = HidlBuf();
170 if (!rc.isOk()) return ErrorCode::UNKNOWN_ERROR;
171 return rc;
172 }
173
DeleteKey(bool keep_key_blob)174 ErrorCode KeymasterHidlTest::DeleteKey(bool keep_key_blob) {
175 return DeleteKey(&key_blob_, keep_key_blob);
176 }
177
DeleteAllKeys()178 ErrorCode KeymasterHidlTest::DeleteAllKeys() {
179 ErrorCode error = keymaster_->deleteAllKeys();
180 return error;
181 }
182
CheckedDeleteKey(HidlBuf * key_blob,bool keep_key_blob)183 void KeymasterHidlTest::CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob) {
184 auto rc = DeleteKey(key_blob, keep_key_blob);
185 EXPECT_TRUE(rc == ErrorCode::OK || rc == ErrorCode::UNIMPLEMENTED);
186 }
187
CheckedDeleteKey()188 void KeymasterHidlTest::CheckedDeleteKey() {
189 CheckedDeleteKey(&key_blob_);
190 }
191
CheckGetCharacteristics(const HidlBuf & key_blob,const HidlBuf & client_id,const HidlBuf & app_data,KeyCharacteristics * key_characteristics)192 void KeymasterHidlTest::CheckGetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
193 const HidlBuf& app_data,
194 KeyCharacteristics* key_characteristics) {
195 HidlBuf empty_buf = {};
196 EXPECT_EQ(ErrorCode::OK,
197 GetCharacteristics(key_blob, client_id, app_data, key_characteristics));
198 if (SecLevel() != SecurityLevel::SOFTWARE) {
199 EXPECT_GT(key_characteristics->hardwareEnforced.size(), 0);
200 }
201 EXPECT_GT(key_characteristics->softwareEnforced.size(), 0);
202
203 EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB,
204 GetCharacteristics(key_blob, empty_buf, app_data, key_characteristics));
205 EXPECT_EQ(key_characteristics->hardwareEnforced.size(), 0);
206 EXPECT_EQ(key_characteristics->softwareEnforced.size(), 0);
207
208 EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB,
209 GetCharacteristics(key_blob, client_id, empty_buf, key_characteristics));
210 EXPECT_EQ(key_characteristics->hardwareEnforced.size(), 0);
211 EXPECT_EQ(key_characteristics->softwareEnforced.size(), 0);
212
213 EXPECT_EQ(ErrorCode::INVALID_KEY_BLOB,
214 GetCharacteristics(key_blob, empty_buf, empty_buf, key_characteristics));
215 EXPECT_EQ(key_characteristics->hardwareEnforced.size(), 0);
216 EXPECT_EQ(key_characteristics->softwareEnforced.size(), 0);
217 }
218
GetCharacteristics(const HidlBuf & key_blob,const HidlBuf & client_id,const HidlBuf & app_data,KeyCharacteristics * key_characteristics)219 ErrorCode KeymasterHidlTest::GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
220 const HidlBuf& app_data,
221 KeyCharacteristics* key_characteristics) {
222 ErrorCode error = ErrorCode::UNKNOWN_ERROR;
223 EXPECT_TRUE(
224 keymaster_
225 ->getKeyCharacteristics(
226 key_blob, client_id, app_data,
227 [&](ErrorCode hidl_error, const KeyCharacteristics& hidl_key_characteristics) {
228 error = hidl_error, *key_characteristics = hidl_key_characteristics;
229 })
230 .isOk());
231 return error;
232 }
233
GetCharacteristics(const HidlBuf & key_blob,KeyCharacteristics * key_characteristics)234 ErrorCode KeymasterHidlTest::GetCharacteristics(const HidlBuf& key_blob,
235 KeyCharacteristics* key_characteristics) {
236 HidlBuf client_id, app_data;
237 return GetCharacteristics(key_blob, client_id, app_data, key_characteristics);
238 }
239
GetDebugInfo(DebugInfo * debug_info)240 ErrorCode KeymasterHidlTest::GetDebugInfo(DebugInfo* debug_info) {
241 EXPECT_TRUE(keymaster_->getDebugInfo([&](const DebugInfo& hidl_debug_info) {
242 *debug_info = hidl_debug_info;
243 }).isOk());
244 return ErrorCode::OK;
245 }
246
Begin(KeyPurpose purpose,const HidlBuf & key_blob,const AuthorizationSet & in_params,AuthorizationSet * out_params,OperationHandle * op_handle)247 ErrorCode KeymasterHidlTest::Begin(KeyPurpose purpose, const HidlBuf& key_blob,
248 const AuthorizationSet& in_params, AuthorizationSet* out_params,
249 OperationHandle* op_handle) {
250 SCOPED_TRACE("Begin");
251 ErrorCode error;
252 OperationHandle saved_handle = *op_handle;
253 EXPECT_TRUE(keymaster_
254 ->begin(purpose, key_blob, in_params.hidl_data(), HardwareAuthToken(),
255 [&](ErrorCode hidl_error, const hidl_vec<KeyParameter>& hidl_out_params,
256 uint64_t hidl_op_handle) {
257 error = hidl_error;
258 *out_params = hidl_out_params;
259 *op_handle = hidl_op_handle;
260 })
261 .isOk());
262 if (error != ErrorCode::OK) {
263 // Some implementations may modify *op_handle on error.
264 *op_handle = saved_handle;
265 }
266 return error;
267 }
268
Begin(KeyPurpose purpose,const AuthorizationSet & in_params,AuthorizationSet * out_params)269 ErrorCode KeymasterHidlTest::Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
270 AuthorizationSet* out_params) {
271 SCOPED_TRACE("Begin");
272 EXPECT_EQ(kOpHandleSentinel, op_handle_);
273 return Begin(purpose, key_blob_, in_params, out_params, &op_handle_);
274 }
275
Begin(KeyPurpose purpose,const AuthorizationSet & in_params)276 ErrorCode KeymasterHidlTest::Begin(KeyPurpose purpose, const AuthorizationSet& in_params) {
277 SCOPED_TRACE("Begin");
278 AuthorizationSet out_params;
279 ErrorCode error = Begin(purpose, in_params, &out_params);
280 EXPECT_TRUE(out_params.empty());
281 return error;
282 }
283
Update(OperationHandle op_handle,const AuthorizationSet & in_params,const string & input,AuthorizationSet * out_params,string * output,size_t * input_consumed)284 ErrorCode KeymasterHidlTest::Update(OperationHandle op_handle, const AuthorizationSet& in_params,
285 const string& input, AuthorizationSet* out_params,
286 string* output, size_t* input_consumed) {
287 SCOPED_TRACE("Update");
288 ErrorCode error;
289 EXPECT_TRUE(keymaster_
290 ->update(op_handle, in_params.hidl_data(), HidlBuf(input), HardwareAuthToken(),
291 VerificationToken(),
292 [&](ErrorCode hidl_error, uint32_t hidl_input_consumed,
293 const hidl_vec<KeyParameter>& hidl_out_params,
294 const HidlBuf& hidl_output) {
295 error = hidl_error;
296 out_params->push_back(AuthorizationSet(hidl_out_params));
297 output->append(hidl_output.to_string());
298 *input_consumed = hidl_input_consumed;
299 })
300 .isOk());
301 return error;
302 }
303
Update(const string & input,string * out,size_t * input_consumed)304 ErrorCode KeymasterHidlTest::Update(const string& input, string* out, size_t* input_consumed) {
305 SCOPED_TRACE("Update");
306 AuthorizationSet out_params;
307 ErrorCode error = Update(op_handle_, AuthorizationSet() /* in_params */, input, &out_params,
308 out, input_consumed);
309 EXPECT_TRUE(out_params.empty());
310 return error;
311 }
312
Finish(OperationHandle op_handle,const AuthorizationSet & in_params,const string & input,const string & signature,AuthorizationSet * out_params,string * output)313 ErrorCode KeymasterHidlTest::Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
314 const string& input, const string& signature,
315 AuthorizationSet* out_params, string* output) {
316 SCOPED_TRACE("Finish");
317 ErrorCode error;
318 EXPECT_TRUE(
319 keymaster_
320 ->finish(op_handle, in_params.hidl_data(), HidlBuf(input), HidlBuf(signature),
321 HardwareAuthToken(), VerificationToken(),
322 [&](ErrorCode hidl_error, const hidl_vec<KeyParameter>& hidl_out_params,
323 const HidlBuf& hidl_output) {
324 error = hidl_error;
325 *out_params = hidl_out_params;
326 output->append(hidl_output.to_string());
327 })
328 .isOk());
329 op_handle_ = kOpHandleSentinel; // So dtor doesn't Abort().
330 return error;
331 }
332
Finish(const string & message,string * output)333 ErrorCode KeymasterHidlTest::Finish(const string& message, string* output) {
334 SCOPED_TRACE("Finish");
335 AuthorizationSet out_params;
336 string finish_output;
337 ErrorCode error = Finish(op_handle_, AuthorizationSet() /* in_params */, message,
338 "" /* signature */, &out_params, output);
339 if (error != ErrorCode::OK) {
340 return error;
341 }
342 EXPECT_EQ(0U, out_params.size());
343 return error;
344 }
345
Finish(const string & message,const string & signature,string * output)346 ErrorCode KeymasterHidlTest::Finish(const string& message, const string& signature,
347 string* output) {
348 SCOPED_TRACE("Finish");
349 AuthorizationSet out_params;
350 ErrorCode error = Finish(op_handle_, AuthorizationSet() /* in_params */, message, signature,
351 &out_params, output);
352 op_handle_ = kOpHandleSentinel; // So dtor doesn't Abort().
353 if (error != ErrorCode::OK) {
354 return error;
355 }
356 EXPECT_EQ(0U, out_params.size());
357 return error;
358 }
359
Abort(OperationHandle op_handle)360 ErrorCode KeymasterHidlTest::Abort(OperationHandle op_handle) {
361 SCOPED_TRACE("Abort");
362 auto retval = keymaster_->abort(op_handle);
363 EXPECT_TRUE(retval.isOk());
364 return retval;
365 }
366
AbortIfNeeded()367 void KeymasterHidlTest::AbortIfNeeded() {
368 SCOPED_TRACE("AbortIfNeeded");
369 if (op_handle_ != kOpHandleSentinel) {
370 EXPECT_EQ(ErrorCode::OK, Abort(op_handle_));
371 op_handle_ = kOpHandleSentinel;
372 }
373 }
374
AttestKey(const HidlBuf & key_blob,const AuthorizationSet & attest_params,hidl_vec<hidl_vec<uint8_t>> * cert_chain)375 ErrorCode KeymasterHidlTest::AttestKey(const HidlBuf& key_blob,
376 const AuthorizationSet& attest_params,
377 hidl_vec<hidl_vec<uint8_t>>* cert_chain) {
378 SCOPED_TRACE("AttestKey");
379 ErrorCode error;
380 auto rc = keymaster_->attestKey(
381 key_blob, attest_params.hidl_data(),
382 [&](ErrorCode hidl_error, const hidl_vec<hidl_vec<uint8_t>>& hidl_cert_chain) {
383 error = hidl_error;
384 *cert_chain = hidl_cert_chain;
385 });
386
387 EXPECT_TRUE(rc.isOk()) << rc.description();
388 if (!rc.isOk()) return ErrorCode::UNKNOWN_ERROR;
389
390 return error;
391 }
392
AttestKey(const AuthorizationSet & attest_params,hidl_vec<hidl_vec<uint8_t>> * cert_chain)393 ErrorCode KeymasterHidlTest::AttestKey(const AuthorizationSet& attest_params,
394 hidl_vec<hidl_vec<uint8_t>>* cert_chain) {
395 SCOPED_TRACE("AttestKey");
396 return AttestKey(key_blob_, attest_params, cert_chain);
397 }
398
ProcessMessage(const HidlBuf & key_blob,KeyPurpose operation,const string & message,const AuthorizationSet & in_params,AuthorizationSet * out_params)399 string KeymasterHidlTest::ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation,
400 const string& message, const AuthorizationSet& in_params,
401 AuthorizationSet* out_params) {
402 SCOPED_TRACE("ProcessMessage");
403 AuthorizationSet begin_out_params;
404 EXPECT_EQ(ErrorCode::OK, Begin(operation, key_blob, in_params, &begin_out_params, &op_handle_));
405
406 string output;
407 size_t consumed = 0;
408 AuthorizationSet update_params;
409 AuthorizationSet update_out_params;
410 EXPECT_EQ(ErrorCode::OK,
411 Update(op_handle_, update_params, message, &update_out_params, &output, &consumed));
412
413 string unused;
414 AuthorizationSet finish_params;
415 AuthorizationSet finish_out_params;
416 EXPECT_EQ(ErrorCode::OK, Finish(op_handle_, finish_params, message.substr(consumed), unused,
417 &finish_out_params, &output));
418 op_handle_ = kOpHandleSentinel;
419
420 out_params->push_back(begin_out_params);
421 out_params->push_back(finish_out_params);
422 return output;
423 }
424
SignMessage(const HidlBuf & key_blob,const string & message,const AuthorizationSet & params)425 string KeymasterHidlTest::SignMessage(const HidlBuf& key_blob, const string& message,
426 const AuthorizationSet& params) {
427 SCOPED_TRACE("SignMessage");
428 AuthorizationSet out_params;
429 string signature = ProcessMessage(key_blob, KeyPurpose::SIGN, message, params, &out_params);
430 EXPECT_TRUE(out_params.empty());
431 return signature;
432 }
433
SignMessage(const string & message,const AuthorizationSet & params)434 string KeymasterHidlTest::SignMessage(const string& message, const AuthorizationSet& params) {
435 SCOPED_TRACE("SignMessage");
436 return SignMessage(key_blob_, message, params);
437 }
438
MacMessage(const string & message,Digest digest,size_t mac_length)439 string KeymasterHidlTest::MacMessage(const string& message, Digest digest, size_t mac_length) {
440 SCOPED_TRACE("MacMessage");
441 return SignMessage(
442 key_blob_, message,
443 AuthorizationSetBuilder().Digest(digest).Authorization(TAG_MAC_LENGTH, mac_length));
444 }
445
CheckHmacTestVector(const string & key,const string & message,Digest digest,const string & expected_mac)446 void KeymasterHidlTest::CheckHmacTestVector(const string& key, const string& message, Digest digest,
447 const string& expected_mac) {
448 SCOPED_TRACE("CheckHmacTestVector");
449 ASSERT_EQ(ErrorCode::OK,
450 ImportKey(AuthorizationSetBuilder()
451 .Authorization(TAG_NO_AUTH_REQUIRED)
452 .HmacKey(key.size() * 8)
453 .Authorization(TAG_MIN_MAC_LENGTH, expected_mac.size() * 8)
454 .Digest(digest),
455 KeyFormat::RAW, key));
456 string signature = MacMessage(message, digest, expected_mac.size() * 8);
457 EXPECT_EQ(expected_mac, signature)
458 << "Test vector didn't match for key of size " << key.size() << " message of size "
459 << message.size() << " and digest " << digest;
460 CheckedDeleteKey();
461 }
462
CheckAesCtrTestVector(const string & key,const string & nonce,const string & message,const string & expected_ciphertext)463 void KeymasterHidlTest::CheckAesCtrTestVector(const string& key, const string& nonce,
464 const string& message,
465 const string& expected_ciphertext) {
466 SCOPED_TRACE("CheckAesCtrTestVector");
467 ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder()
468 .Authorization(TAG_NO_AUTH_REQUIRED)
469 .AesEncryptionKey(key.size() * 8)
470 .BlockMode(BlockMode::CTR)
471 .Authorization(TAG_CALLER_NONCE)
472 .Padding(PaddingMode::NONE),
473 KeyFormat::RAW, key));
474
475 auto params = AuthorizationSetBuilder()
476 .Authorization(TAG_NONCE, nonce.data(), nonce.size())
477 .BlockMode(BlockMode::CTR)
478 .Padding(PaddingMode::NONE);
479 AuthorizationSet out_params;
480 string ciphertext = EncryptMessage(key_blob_, message, params, &out_params);
481 EXPECT_EQ(expected_ciphertext, ciphertext);
482 }
483
CheckTripleDesTestVector(KeyPurpose purpose,BlockMode block_mode,PaddingMode padding_mode,const string & key,const string & iv,const string & input,const string & expected_output)484 void KeymasterHidlTest::CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
485 PaddingMode padding_mode, const string& key,
486 const string& iv, const string& input,
487 const string& expected_output) {
488 auto authset = AuthorizationSetBuilder()
489 .TripleDesEncryptionKey(key.size() * 7)
490 .BlockMode(block_mode)
491 .Authorization(TAG_NO_AUTH_REQUIRED)
492 .Padding(padding_mode);
493 if (iv.size()) authset.Authorization(TAG_CALLER_NONCE);
494 ASSERT_EQ(ErrorCode::OK, ImportKey(authset, KeyFormat::RAW, key));
495 auto begin_params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode);
496 if (iv.size()) begin_params.Authorization(TAG_NONCE, iv.data(), iv.size());
497 AuthorizationSet output_params;
498 string output = ProcessMessage(key_blob_, purpose, input, begin_params, &output_params);
499 EXPECT_EQ(expected_output, output);
500 }
501
VerifyMessage(const HidlBuf & key_blob,const string & message,const string & signature,const AuthorizationSet & params)502 void KeymasterHidlTest::VerifyMessage(const HidlBuf& key_blob, const string& message,
503 const string& signature, const AuthorizationSet& params) {
504 SCOPED_TRACE("VerifyMessage");
505 AuthorizationSet begin_out_params;
506 ASSERT_EQ(ErrorCode::OK,
507 Begin(KeyPurpose::VERIFY, key_blob, params, &begin_out_params, &op_handle_));
508
509 string output;
510 AuthorizationSet update_params;
511 AuthorizationSet update_out_params;
512 size_t consumed;
513 ASSERT_EQ(ErrorCode::OK,
514 Update(op_handle_, update_params, message, &update_out_params, &output, &consumed));
515 EXPECT_TRUE(output.empty());
516 EXPECT_GT(consumed, 0U);
517
518 string unused;
519 AuthorizationSet finish_params;
520 AuthorizationSet finish_out_params;
521 EXPECT_EQ(ErrorCode::OK, Finish(op_handle_, finish_params, message.substr(consumed), signature,
522 &finish_out_params, &output));
523 op_handle_ = kOpHandleSentinel;
524 EXPECT_TRUE(output.empty());
525 }
526
VerifyMessage(const string & message,const string & signature,const AuthorizationSet & params)527 void KeymasterHidlTest::VerifyMessage(const string& message, const string& signature,
528 const AuthorizationSet& params) {
529 SCOPED_TRACE("VerifyMessage");
530 VerifyMessage(key_blob_, message, signature, params);
531 }
532
EncryptMessage(const HidlBuf & key_blob,const string & message,const AuthorizationSet & in_params,AuthorizationSet * out_params)533 string KeymasterHidlTest::EncryptMessage(const HidlBuf& key_blob, const string& message,
534 const AuthorizationSet& in_params,
535 AuthorizationSet* out_params) {
536 SCOPED_TRACE("EncryptMessage");
537 return ProcessMessage(key_blob, KeyPurpose::ENCRYPT, message, in_params, out_params);
538 }
539
EncryptMessage(const string & message,const AuthorizationSet & params,AuthorizationSet * out_params)540 string KeymasterHidlTest::EncryptMessage(const string& message, const AuthorizationSet& params,
541 AuthorizationSet* out_params) {
542 SCOPED_TRACE("EncryptMessage");
543 return EncryptMessage(key_blob_, message, params, out_params);
544 }
545
EncryptMessage(const string & message,const AuthorizationSet & params)546 string KeymasterHidlTest::EncryptMessage(const string& message, const AuthorizationSet& params) {
547 SCOPED_TRACE("EncryptMessage");
548 AuthorizationSet out_params;
549 string ciphertext = EncryptMessage(message, params, &out_params);
550 EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params;
551 return ciphertext;
552 }
553
EncryptMessage(const string & message,BlockMode block_mode,PaddingMode padding)554 string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
555 PaddingMode padding) {
556 SCOPED_TRACE("EncryptMessage");
557 auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding);
558 AuthorizationSet out_params;
559 string ciphertext = EncryptMessage(message, params, &out_params);
560 EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params;
561 return ciphertext;
562 }
563
EncryptMessage(const string & message,BlockMode block_mode,PaddingMode padding,HidlBuf * iv_out)564 string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
565 PaddingMode padding, HidlBuf* iv_out) {
566 SCOPED_TRACE("EncryptMessage");
567 auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding);
568 AuthorizationSet out_params;
569 string ciphertext = EncryptMessage(message, params, &out_params);
570 EXPECT_EQ(1U, out_params.size());
571 auto ivVal = out_params.GetTagValue(TAG_NONCE);
572 EXPECT_TRUE(ivVal.isOk());
573 if (ivVal.isOk()) *iv_out = ivVal.value();
574 return ciphertext;
575 }
576
EncryptMessage(const string & message,BlockMode block_mode,PaddingMode padding,const HidlBuf & iv_in)577 string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
578 PaddingMode padding, const HidlBuf& iv_in) {
579 SCOPED_TRACE("EncryptMessage");
580 auto params = AuthorizationSetBuilder()
581 .BlockMode(block_mode)
582 .Padding(padding)
583 .Authorization(TAG_NONCE, iv_in);
584 AuthorizationSet out_params;
585 string ciphertext = EncryptMessage(message, params, &out_params);
586 return ciphertext;
587 }
588
EncryptMessage(const string & message,BlockMode block_mode,PaddingMode padding,uint8_t mac_length_bits,const HidlBuf & iv_in)589 string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
590 PaddingMode padding, uint8_t mac_length_bits,
591 const HidlBuf& iv_in) {
592 SCOPED_TRACE("EncryptMessage");
593 auto params = AuthorizationSetBuilder()
594 .BlockMode(block_mode)
595 .Padding(padding)
596 .Authorization(TAG_MAC_LENGTH, mac_length_bits)
597 .Authorization(TAG_NONCE, iv_in);
598 AuthorizationSet out_params;
599 string ciphertext = EncryptMessage(message, params, &out_params);
600 return ciphertext;
601 }
602
DecryptMessage(const HidlBuf & key_blob,const string & ciphertext,const AuthorizationSet & params)603 string KeymasterHidlTest::DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
604 const AuthorizationSet& params) {
605 SCOPED_TRACE("DecryptMessage");
606 AuthorizationSet out_params;
607 string plaintext =
608 ProcessMessage(key_blob, KeyPurpose::DECRYPT, ciphertext, params, &out_params);
609 EXPECT_TRUE(out_params.empty());
610 return plaintext;
611 }
612
DecryptMessage(const string & ciphertext,const AuthorizationSet & params)613 string KeymasterHidlTest::DecryptMessage(const string& ciphertext, const AuthorizationSet& params) {
614 SCOPED_TRACE("DecryptMessage");
615 return DecryptMessage(key_blob_, ciphertext, params);
616 }
617
DecryptMessage(const string & ciphertext,BlockMode block_mode,PaddingMode padding_mode,const HidlBuf & iv)618 string KeymasterHidlTest::DecryptMessage(const string& ciphertext, BlockMode block_mode,
619 PaddingMode padding_mode, const HidlBuf& iv) {
620 SCOPED_TRACE("DecryptMessage");
621 auto params = AuthorizationSetBuilder()
622 .BlockMode(block_mode)
623 .Padding(padding_mode)
624 .Authorization(TAG_NONCE, iv);
625 return DecryptMessage(key_blob_, ciphertext, params);
626 }
627
UpgradeKey(const HidlBuf & key_blob)628 std::pair<ErrorCode, HidlBuf> KeymasterHidlTest::UpgradeKey(const HidlBuf& key_blob) {
629 std::pair<ErrorCode, HidlBuf> retval;
630 keymaster_->upgradeKey(key_blob, hidl_vec<KeyParameter>(),
631 [&](ErrorCode error, const hidl_vec<uint8_t>& upgraded_blob) {
632 retval = std::tie(error, upgraded_blob);
633 });
634 return retval;
635 }
ValidKeySizes(Algorithm algorithm)636 std::vector<uint32_t> KeymasterHidlTest::ValidKeySizes(Algorithm algorithm) {
637 switch (algorithm) {
638 case Algorithm::RSA:
639 switch (SecLevel()) {
640 case SecurityLevel::SOFTWARE:
641 case SecurityLevel::TRUSTED_ENVIRONMENT:
642 return {2048, 3072, 4096};
643 case SecurityLevel::STRONGBOX:
644 return {2048};
645 default:
646 ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel());
647 break;
648 }
649 break;
650 case Algorithm::EC:
651 switch (SecLevel()) {
652 case SecurityLevel::SOFTWARE:
653 case SecurityLevel::TRUSTED_ENVIRONMENT:
654 return {224, 256, 384, 521};
655 case SecurityLevel::STRONGBOX:
656 return {256};
657 default:
658 ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel());
659 break;
660 }
661 break;
662 case Algorithm::AES:
663 return {128, 256};
664 case Algorithm::TRIPLE_DES:
665 return {168};
666 case Algorithm::HMAC: {
667 std::vector<uint32_t> retval((512 - 64) / 8 + 1);
668 uint32_t size = 64 - 8;
669 std::generate(retval.begin(), retval.end(), [&]() { return (size += 8); });
670 return retval;
671 }
672 default:
673 ADD_FAILURE() << "Invalid Algorithm: " << algorithm;
674 return {};
675 }
676 ADD_FAILURE() << "Should be impossible to get here";
677 return {};
678 }
679
InvalidKeySizes(Algorithm algorithm)680 std::vector<uint32_t> KeymasterHidlTest::InvalidKeySizes(Algorithm algorithm) {
681 if (SecLevel() == SecurityLevel::STRONGBOX) {
682 switch (algorithm) {
683 case Algorithm::RSA:
684 return {3072, 4096};
685 case Algorithm::EC:
686 return {224, 384, 521};
687 case Algorithm::AES:
688 return {192};
689 default:
690 return {};
691 }
692 }
693 return {};
694 }
695
ValidCurves()696 std::vector<EcCurve> KeymasterHidlTest::ValidCurves() {
697 if (securityLevel_ == SecurityLevel::STRONGBOX) {
698 return {EcCurve::P_256};
699 } else {
700 return {EcCurve::P_224, EcCurve::P_256, EcCurve::P_384, EcCurve::P_521};
701 }
702 }
703
InvalidCurves()704 std::vector<EcCurve> KeymasterHidlTest::InvalidCurves() {
705 if (SecLevel() == SecurityLevel::TRUSTED_ENVIRONMENT) return {};
706 CHECK(SecLevel() == SecurityLevel::STRONGBOX);
707 return {EcCurve::P_224, EcCurve::P_384, EcCurve::P_521};
708 }
709
ValidDigests(bool withNone,bool withMD5)710 std::vector<Digest> KeymasterHidlTest::ValidDigests(bool withNone, bool withMD5) {
711 switch (SecLevel()) {
712 case SecurityLevel::SOFTWARE:
713 case SecurityLevel::TRUSTED_ENVIRONMENT:
714 if (withNone) {
715 if (withMD5)
716 return {Digest::NONE, Digest::MD5, Digest::SHA1,
717 Digest::SHA_2_224, Digest::SHA_2_256, Digest::SHA_2_384,
718 Digest::SHA_2_512};
719 else
720 return {Digest::NONE, Digest::SHA1, Digest::SHA_2_224,
721 Digest::SHA_2_256, Digest::SHA_2_384, Digest::SHA_2_512};
722 } else {
723 if (withMD5)
724 return {Digest::MD5, Digest::SHA1, Digest::SHA_2_224,
725 Digest::SHA_2_256, Digest::SHA_2_384, Digest::SHA_2_512};
726 else
727 return {Digest::SHA1, Digest::SHA_2_224, Digest::SHA_2_256, Digest::SHA_2_384,
728 Digest::SHA_2_512};
729 }
730 break;
731 case SecurityLevel::STRONGBOX:
732 if (withNone)
733 return {Digest::NONE, Digest::SHA_2_256};
734 else
735 return {Digest::SHA_2_256};
736 break;
737 default:
738 ADD_FAILURE() << "Invalid security level " << uint32_t(SecLevel());
739 break;
740 }
741 ADD_FAILURE() << "Should be impossible to get here";
742 return {};
743 }
744
InvalidDigests()745 std::vector<Digest> KeymasterHidlTest::InvalidDigests() {
746 return {};
747 }
748
749 } // namespace test
750 } // namespace V4_0
751 } // namespace keymaster
752 } // namespace hardware
753 } // namespace android
754