summaryrefslogtreecommitdiffstats
blob: e266a86ed9742e05ecb53d415eec35568cc6ff0e (plain) (blame)
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
/*
 * 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"

#include <android/hidl/manager/1.0/IServiceManager.h>

#include <keymasterV4_0/key_param_output.h>

namespace android {
namespace hardware {
namespace keymaster {
namespace V4_0 {

::std::ostream& operator<<(::std::ostream& os, const AuthorizationSet& set) {
    if (set.size() == 0)
        os << "(Empty)" << ::std::endl;
    else {
        os << "\n";
        for (size_t i = 0; i < set.size(); ++i) os << set[i] << ::std::endl;
    }
    return os;
}

namespace test {

sp<IKeymasterDevice> KeymasterHidlTest::keymaster_;
std::vector<sp<IKeymasterDevice>> KeymasterHidlTest::all_keymasters_;
uint32_t KeymasterHidlTest::os_version_;
uint32_t KeymasterHidlTest::os_patch_level_;
SecurityLevel KeymasterHidlTest::securityLevel_;
hidl_string KeymasterHidlTest::name_;
hidl_string KeymasterHidlTest::author_;

void KeymasterHidlTest::SetUpTestCase() {
    string service_name = KeymasterHidlEnvironment::Instance()->getServiceName<IKeymasterDevice>();
    keymaster_ = ::testing::VtsHalHidlTargetTestBase::getService<IKeymasterDevice>(service_name);
    ASSERT_NE(keymaster_, nullptr);

    ASSERT_TRUE(keymaster_
                    ->getHardwareInfo([&](SecurityLevel securityLevel, const hidl_string& name,
                                          const hidl_string& author) {
                        securityLevel_ = securityLevel;
                        name_ = name;
                        author_ = author;
                    })
                    .isOk());

    os_version_ = ::keymaster::GetOsVersion();
    os_patch_level_ = ::keymaster::GetOsPatchlevel();

    auto service_manager = android::hidl::manager::V1_0::IServiceManager::getService();
    ASSERT_NE(nullptr, service_manager.get());

    all_keymasters_.push_back(keymaster_);
    service_manager->listByInterface(
        IKeymasterDevice::descriptor, [&](const hidl_vec<hidl_string>& names) {
            for (auto& name : names) {
                if (name == service_name) continue;
                auto keymaster =
                    ::testing::VtsHalHidlTargetTestBase::getService<IKeymasterDevice>(name);
                ASSERT_NE(keymaster, nullptr);
                all_keymasters_.push_back(keymaster);
            }
        });
}

ErrorCode KeymasterHidlTest::GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
                                         KeyCharacteristics* key_characteristics) {
    EXPECT_NE(key_blob, nullptr) << "Key blob pointer must not be null.  Test bug";
    EXPECT_EQ(0U, key_blob->size()) << "Key blob not empty before generating key.  Test bug.";
    EXPECT_NE(key_characteristics, nullptr)
        << "Previous characteristics not deleted before generating key.  Test bug.";

    ErrorCode error;
    EXPECT_TRUE(keymaster_
                    ->generateKey(key_desc.hidl_data(),
                                  [&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
                                      const KeyCharacteristics& hidl_key_characteristics) {
                                      error = hidl_error;
                                      *key_blob = hidl_key_blob;
                                      *key_characteristics = hidl_key_characteristics;
                                  })
                    .isOk());
    // On error, blob & characteristics should be empty.
    if (error != ErrorCode::OK) {
        EXPECT_EQ(0U, key_blob->size());
        EXPECT_EQ(0U, (key_characteristics->softwareEnforced.size() +
                       key_characteristics->hardwareEnforced.size()));
    }
    return error;
}

ErrorCode KeymasterHidlTest::GenerateKey(const AuthorizationSet& key_desc) {
    return GenerateKey(key_desc, &key_blob_, &key_characteristics_);
}

ErrorCode KeymasterHidlTest::ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
                                       const string& key_material, HidlBuf* key_blob,
                                       KeyCharacteristics* key_characteristics) {
    ErrorCode error;
    EXPECT_TRUE(keymaster_
                    ->importKey(key_desc.hidl_data(), format, HidlBuf(key_material),
                                [&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
                                    const KeyCharacteristics& hidl_key_characteristics) {
                                    error = hidl_error;
                                    *key_blob = hidl_key_blob;
                                    *key_characteristics = hidl_key_characteristics;
                                })
                    .isOk());
    // On error, blob & characteristics should be empty.
    if (error != ErrorCode::OK) {
        EXPECT_EQ(0U, key_blob->size());
        EXPECT_EQ(0U, (key_characteristics->softwareEnforced.size() +
                       key_characteristics->hardwareEnforced.size()));
    }
    return error;
}

ErrorCode KeymasterHidlTest::ImportKey(const AuthorizationSet& key_desc, KeyFormat format,
                                       const string& key_material) {
    return ImportKey(key_desc, format, key_material, &key_blob_, &key_characteristics_);
}

ErrorCode KeymasterHidlTest::ImportWrappedKey(string wrapped_key, string wrapping_key,
                                              const AuthorizationSet& wrapping_key_desc,
                                              string masking_key,
                                              const AuthorizationSet& unwrapping_params) {
    ErrorCode error;
    ImportKey(wrapping_key_desc, KeyFormat::PKCS8, wrapping_key);
    EXPECT_TRUE(keymaster_
                    ->importWrappedKey(HidlBuf(wrapped_key), key_blob_, HidlBuf(masking_key),
                                       unwrapping_params.hidl_data(), 0 /* passwordSid */,
                                       0 /* biometricSid */,
                                       [&](ErrorCode hidl_error, const HidlBuf& hidl_key_blob,
                                           const KeyCharacteristics& hidl_key_characteristics) {
                                           error = hidl_error;
                                           key_blob_ = hidl_key_blob;
                                           key_characteristics_ = hidl_key_characteristics;
                                       })
                    .isOk());
    return error;
}

ErrorCode KeymasterHidlTest::ExportKey(KeyFormat format, const HidlBuf& key_blob,
                                       const HidlBuf& client_id, const HidlBuf& app_data,
                                       HidlBuf* key_material) {
    ErrorCode error;
    EXPECT_TRUE(keymaster_
                    ->exportKey(format, key_blob, client_id, app_data,
                                [&](ErrorCode hidl_error_code, const HidlBuf& hidl_key_material) {
                                    error = hidl_error_code;
                                    *key_material = hidl_key_material;
                                })
                    .isOk());
    // On error, blob should be empty.
    if (error != ErrorCode::OK) {
        EXPECT_EQ(0U, key_material->size());
    }
    return error;
}

ErrorCode KeymasterHidlTest::ExportKey(KeyFormat format, HidlBuf* key_material) {
    HidlBuf client_id, app_data;
    return ExportKey(format, key_blob_, client_id, app_data, key_material);
}

ErrorCode KeymasterHidlTest::DeleteKey(HidlBuf* key_blob, bool keep_key_blob) {
    auto rc = keymaster_->deleteKey(*key_blob);
    if (!keep_key_blob) *key_blob = HidlBuf();
    if (!rc.isOk()) return ErrorCode::UNKNOWN_ERROR;
    return rc;
}

ErrorCode KeymasterHidlTest::DeleteKey(bool keep_key_blob) {
    return DeleteKey(&key_blob_, keep_key_blob);
}

ErrorCode KeymasterHidlTest::DeleteAllKeys() {
    ErrorCode error = keymaster_->deleteAllKeys();
    return error;
}

void KeymasterHidlTest::CheckedDeleteKey(HidlBuf* key_blob, bool keep_key_blob) {
    auto rc = DeleteKey(key_blob, keep_key_blob);
    EXPECT_TRUE(rc == ErrorCode::OK || rc == ErrorCode::UNIMPLEMENTED);
}

void KeymasterHidlTest::CheckedDeleteKey() {
    CheckedDeleteKey(&key_blob_);
}

ErrorCode KeymasterHidlTest::GetCharacteristics(const HidlBuf& key_blob, const HidlBuf& client_id,
                                                const HidlBuf& app_data,
                                                KeyCharacteristics* key_characteristics) {
    ErrorCode error = ErrorCode::UNKNOWN_ERROR;
    EXPECT_TRUE(
        keymaster_
            ->getKeyCharacteristics(
                key_blob, client_id, app_data,
                [&](ErrorCode hidl_error, const KeyCharacteristics& hidl_key_characteristics) {
                    error = hidl_error, *key_characteristics = hidl_key_characteristics;
                })
            .isOk());
    return error;
}

ErrorCode KeymasterHidlTest::GetCharacteristics(const HidlBuf& key_blob,
                                                KeyCharacteristics* key_characteristics) {
    HidlBuf client_id, app_data;
    return GetCharacteristics(key_blob, client_id, app_data, key_characteristics);
}

ErrorCode KeymasterHidlTest::Begin(KeyPurpose purpose, const HidlBuf& key_blob,
                                   const AuthorizationSet& in_params, AuthorizationSet* out_params,
                                   OperationHandle* op_handle) {
    SCOPED_TRACE("Begin");
    ErrorCode error;
    OperationHandle saved_handle = *op_handle;
    EXPECT_TRUE(keymaster_
                    ->begin(purpose, key_blob, in_params.hidl_data(), HardwareAuthToken(),
                            [&](ErrorCode hidl_error, const hidl_vec<KeyParameter>& hidl_out_params,
                                uint64_t hidl_op_handle) {
                                error = hidl_error;
                                *out_params = hidl_out_params;
                                *op_handle = hidl_op_handle;
                            })
                    .isOk());
    if (error != ErrorCode::OK) {
        // Some implementations may modify *op_handle on error.
        *op_handle = saved_handle;
    }
    return error;
}

ErrorCode KeymasterHidlTest::Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
                                   AuthorizationSet* out_params) {
    SCOPED_TRACE("Begin");
    EXPECT_EQ(kOpHandleSentinel, op_handle_);
    return Begin(purpose, key_blob_, in_params, out_params, &op_handle_);
}

ErrorCode KeymasterHidlTest::Begin(KeyPurpose purpose, const AuthorizationSet& in_params) {
    SCOPED_TRACE("Begin");
    AuthorizationSet out_params;
    ErrorCode error = Begin(purpose, in_params, &out_params);
    EXPECT_TRUE(out_params.empty());
    return error;
}

ErrorCode KeymasterHidlTest::Update(OperationHandle op_handle, const AuthorizationSet& in_params,
                                    const string& input, AuthorizationSet* out_params,
                                    string* output, size_t* input_consumed) {
    SCOPED_TRACE("Update");
    ErrorCode error;
    EXPECT_TRUE(keymaster_
                    ->update(op_handle, in_params.hidl_data(), HidlBuf(input), HardwareAuthToken(),
                             VerificationToken(),
                             [&](ErrorCode hidl_error, uint32_t hidl_input_consumed,
                                 const hidl_vec<KeyParameter>& hidl_out_params,
                                 const HidlBuf& hidl_output) {
                                 error = hidl_error;
                                 out_params->push_back(AuthorizationSet(hidl_out_params));
                                 output->append(hidl_output.to_string());
                                 *input_consumed = hidl_input_consumed;
                             })
                    .isOk());
    return error;
}

ErrorCode KeymasterHidlTest::Update(const string& input, string* out, size_t* input_consumed) {
    SCOPED_TRACE("Update");
    AuthorizationSet out_params;
    ErrorCode error = Update(op_handle_, AuthorizationSet() /* in_params */, input, &out_params,
                             out, input_consumed);
    EXPECT_TRUE(out_params.empty());
    return error;
}

ErrorCode KeymasterHidlTest::Finish(OperationHandle op_handle, const AuthorizationSet& in_params,
                                    const string& input, const string& signature,
                                    AuthorizationSet* out_params, string* output) {
    SCOPED_TRACE("Finish");
    ErrorCode error;
    EXPECT_TRUE(
        keymaster_
            ->finish(op_handle, in_params.hidl_data(), HidlBuf(input), HidlBuf(signature),
                     HardwareAuthToken(), VerificationToken(),
                     [&](ErrorCode hidl_error, const hidl_vec<KeyParameter>& hidl_out_params,
                         const HidlBuf& hidl_output) {
                         error = hidl_error;
                         *out_params = hidl_out_params;
                         output->append(hidl_output.to_string());
                     })
            .isOk());
    op_handle_ = kOpHandleSentinel;  // So dtor doesn't Abort().
    return error;
}

ErrorCode KeymasterHidlTest::Finish(const string& message, string* output) {
    SCOPED_TRACE("Finish");
    AuthorizationSet out_params;
    string finish_output;
    ErrorCode error = Finish(op_handle_, AuthorizationSet() /* in_params */, message,
                             "" /* signature */, &out_params, output);
    if (error != ErrorCode::OK) {
        return error;
    }
    EXPECT_EQ(0U, out_params.size());
    return error;
}

ErrorCode KeymasterHidlTest::Finish(const string& message, const string& signature,
                                    string* output) {
    SCOPED_TRACE("Finish");
    AuthorizationSet out_params;
    ErrorCode error = Finish(op_handle_, AuthorizationSet() /* in_params */, message, signature,
                             &out_params, output);
    op_handle_ = kOpHandleSentinel;  // So dtor doesn't Abort().
    if (error != ErrorCode::OK) {
        return error;
    }
    EXPECT_EQ(0U, out_params.size());
    return error;
}

ErrorCode KeymasterHidlTest::Abort(OperationHandle op_handle) {
    SCOPED_TRACE("Abort");
    auto retval = keymaster_->abort(op_handle);
    EXPECT_TRUE(retval.isOk());
    return retval;
}

void KeymasterHidlTest::AbortIfNeeded() {
    SCOPED_TRACE("AbortIfNeeded");
    if (op_handle_ != kOpHandleSentinel) {
        EXPECT_EQ(ErrorCode::OK, Abort(op_handle_));
        op_handle_ = kOpHandleSentinel;
    }
}

ErrorCode KeymasterHidlTest::AttestKey(const HidlBuf& key_blob,
                                       const AuthorizationSet& attest_params,
                                       hidl_vec<hidl_vec<uint8_t>>* cert_chain) {
    SCOPED_TRACE("AttestKey");
    ErrorCode error;
    auto rc = keymaster_->attestKey(
        key_blob, attest_params.hidl_data(),
        [&](ErrorCode hidl_error, const hidl_vec<hidl_vec<uint8_t>>& hidl_cert_chain) {
            error = hidl_error;
            *cert_chain = hidl_cert_chain;
        });

    EXPECT_TRUE(rc.isOk()) << rc.description();
    if (!rc.isOk()) return ErrorCode::UNKNOWN_ERROR;

    return error;
}

ErrorCode KeymasterHidlTest::AttestKey(const AuthorizationSet& attest_params,
                                       hidl_vec<hidl_vec<uint8_t>>* cert_chain) {
    SCOPED_TRACE("AttestKey");
    return AttestKey(key_blob_, attest_params, cert_chain);
}

string KeymasterHidlTest::ProcessMessage(const HidlBuf& key_blob, KeyPurpose operation,
                                         const string& message, const AuthorizationSet& in_params,
                                         AuthorizationSet* out_params) {
    SCOPED_TRACE("ProcessMessage");
    AuthorizationSet begin_out_params;
    EXPECT_EQ(ErrorCode::OK, Begin(operation, key_blob, in_params, &begin_out_params, &op_handle_));

    string unused;
    AuthorizationSet finish_params;
    AuthorizationSet finish_out_params;
    string output;
    EXPECT_EQ(ErrorCode::OK,
              Finish(op_handle_, finish_params, message, unused, &finish_out_params, &output));
    op_handle_ = kOpHandleSentinel;

    out_params->push_back(begin_out_params);
    out_params->push_back(finish_out_params);
    return output;
}

string KeymasterHidlTest::SignMessage(const HidlBuf& key_blob, const string& message,
                                      const AuthorizationSet& params) {
    SCOPED_TRACE("SignMessage");
    AuthorizationSet out_params;
    string signature = ProcessMessage(key_blob, KeyPurpose::SIGN, message, params, &out_params);
    EXPECT_TRUE(out_params.empty());
    return signature;
}

string KeymasterHidlTest::SignMessage(const string& message, const AuthorizationSet& params) {
    SCOPED_TRACE("SignMessage");
    return SignMessage(key_blob_, message, params);
}

string KeymasterHidlTest::MacMessage(const string& message, Digest digest, size_t mac_length) {
    SCOPED_TRACE("MacMessage");
    return SignMessage(
        key_blob_, message,
        AuthorizationSetBuilder().Digest(digest).Authorization(TAG_MAC_LENGTH, mac_length));
}

void KeymasterHidlTest::CheckHmacTestVector(const string& key, const string& message, Digest digest,
                                            const string& expected_mac) {
    SCOPED_TRACE("CheckHmacTestVector");
    ASSERT_EQ(ErrorCode::OK,
              ImportKey(AuthorizationSetBuilder()
                            .Authorization(TAG_NO_AUTH_REQUIRED)
                            .HmacKey(key.size() * 8)
                            .Authorization(TAG_MIN_MAC_LENGTH, expected_mac.size() * 8)
                            .Digest(digest),
                        KeyFormat::RAW, key));
    string signature = MacMessage(message, digest, expected_mac.size() * 8);
    EXPECT_EQ(expected_mac, signature)
        << "Test vector didn't match for key of size " << key.size() << " message of size "
        << message.size() << " and digest " << digest;
    CheckedDeleteKey();
}

void KeymasterHidlTest::CheckAesCtrTestVector(const string& key, const string& nonce,
                                              const string& message,
                                              const string& expected_ciphertext) {
    SCOPED_TRACE("CheckAesCtrTestVector");
    ASSERT_EQ(ErrorCode::OK, ImportKey(AuthorizationSetBuilder()
                                           .Authorization(TAG_NO_AUTH_REQUIRED)
                                           .AesEncryptionKey(key.size() * 8)
                                           .BlockMode(BlockMode::CTR)
                                           .Authorization(TAG_CALLER_NONCE)
                                           .Padding(PaddingMode::NONE),
                                       KeyFormat::RAW, key));

    auto params = AuthorizationSetBuilder()
                      .Authorization(TAG_NONCE, nonce.data(), nonce.size())
                      .BlockMode(BlockMode::CTR)
                      .Padding(PaddingMode::NONE);
    AuthorizationSet out_params;
    string ciphertext = EncryptMessage(key_blob_, message, params, &out_params);
    EXPECT_EQ(expected_ciphertext, ciphertext);
}

void KeymasterHidlTest::CheckTripleDesTestVector(KeyPurpose purpose, BlockMode block_mode,
                                                 PaddingMode padding_mode, const string& key,
                                                 const string& iv, const string& input,
                                                 const string& expected_output) {
    auto authset = AuthorizationSetBuilder()
                       .TripleDesEncryptionKey(key.size() * 7)
                       .BlockMode(block_mode)
                       .Authorization(TAG_NO_AUTH_REQUIRED)
                       .Padding(padding_mode);
    if (iv.size()) authset.Authorization(TAG_CALLER_NONCE);
    ASSERT_EQ(ErrorCode::OK, ImportKey(authset, KeyFormat::RAW, key));
    auto begin_params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding_mode);
    if (iv.size()) begin_params.Authorization(TAG_NONCE, iv.data(), iv.size());
    AuthorizationSet output_params;
    string output = ProcessMessage(key_blob_, purpose, input, begin_params, &output_params);
    EXPECT_EQ(expected_output, output);
}

void KeymasterHidlTest::VerifyMessage(const HidlBuf& key_blob, const string& message,
                                      const string& signature, const AuthorizationSet& params) {
    SCOPED_TRACE("VerifyMessage");
    AuthorizationSet begin_out_params;
    ASSERT_EQ(ErrorCode::OK,
              Begin(KeyPurpose::VERIFY, key_blob, params, &begin_out_params, &op_handle_));

    string unused;
    AuthorizationSet finish_params;
    AuthorizationSet finish_out_params;
    string output;
    EXPECT_EQ(ErrorCode::OK,
              Finish(op_handle_, finish_params, message, signature, &finish_out_params, &output));
    op_handle_ = kOpHandleSentinel;
    EXPECT_TRUE(output.empty());
}

void KeymasterHidlTest::VerifyMessage(const string& message, const string& signature,
                                      const AuthorizationSet& params) {
    SCOPED_TRACE("VerifyMessage");
    VerifyMessage(key_blob_, message, signature, params);
}

string KeymasterHidlTest::EncryptMessage(const HidlBuf& key_blob, const string& message,
                                         const AuthorizationSet& in_params,
                                         AuthorizationSet* out_params) {
    SCOPED_TRACE("EncryptMessage");
    return ProcessMessage(key_blob, KeyPurpose::ENCRYPT, message, in_params, out_params);
}

string KeymasterHidlTest::EncryptMessage(const string& message, const AuthorizationSet& params,
                                         AuthorizationSet* out_params) {
    SCOPED_TRACE("EncryptMessage");
    return EncryptMessage(key_blob_, message, params, out_params);
}

string KeymasterHidlTest::EncryptMessage(const string& message, const AuthorizationSet& params) {
    SCOPED_TRACE("EncryptMessage");
    AuthorizationSet out_params;
    string ciphertext = EncryptMessage(message, params, &out_params);
    EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params;
    return ciphertext;
}

string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
                                         PaddingMode padding) {
    SCOPED_TRACE("EncryptMessage");
    auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding);
    AuthorizationSet out_params;
    string ciphertext = EncryptMessage(message, params, &out_params);
    EXPECT_TRUE(out_params.empty()) << "Output params should be empty. Contained: " << out_params;
    return ciphertext;
}

string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
                                         PaddingMode padding, HidlBuf* iv_out) {
    SCOPED_TRACE("EncryptMessage");
    auto params = AuthorizationSetBuilder().BlockMode(block_mode).Padding(padding);
    AuthorizationSet out_params;
    string ciphertext = EncryptMessage(message, params, &out_params);
    EXPECT_EQ(1U, out_params.size());
    auto ivVal = out_params.GetTagValue(TAG_NONCE);
    EXPECT_TRUE(ivVal.isOk());
    if (ivVal.isOk()) *iv_out = ivVal.value();
    return ciphertext;
}

string KeymasterHidlTest::EncryptMessage(const string& message, BlockMode block_mode,
                                         PaddingMode padding, const HidlBuf& iv_in) {
    SCOPED_TRACE("EncryptMessage");
    auto params = AuthorizationSetBuilder()
                      .BlockMode(block_mode)
                      .Padding(padding)
                      .Authorization(TAG_NONCE, iv_in);
    AuthorizationSet out_params;
    string ciphertext = EncryptMessage(message, params, &out_params);
    return ciphertext;
}

string KeymasterHidlTest::DecryptMessage(const HidlBuf& key_blob, const string& ciphertext,
                                         const AuthorizationSet& params) {
    SCOPED_TRACE("DecryptMessage");
    AuthorizationSet out_params;
    string plaintext =
        ProcessMessage(key_blob, KeyPurpose::DECRYPT, ciphertext, params, &out_params);
    EXPECT_TRUE(out_params.empty());
    return plaintext;
}

string KeymasterHidlTest::DecryptMessage(const string& ciphertext, const AuthorizationSet& params) {
    SCOPED_TRACE("DecryptMessage");
    return DecryptMessage(key_blob_, ciphertext, params);
}

string KeymasterHidlTest::DecryptMessage(const string& ciphertext, BlockMode block_mode,
                                         PaddingMode padding_mode, const HidlBuf& iv) {
    SCOPED_TRACE("DecryptMessage");
    auto params = AuthorizationSetBuilder()
                      .BlockMode(block_mode)
                      .Padding(padding_mode)
                      .Authorization(TAG_NONCE, iv);
    return DecryptMessage(key_blob_, ciphertext, params);
}

std::pair<ErrorCode, HidlBuf> KeymasterHidlTest::UpgradeKey(const HidlBuf& key_blob) {
    std::pair<ErrorCode, HidlBuf> retval;
    keymaster_->upgradeKey(key_blob, hidl_vec<KeyParameter>(),
                           [&](ErrorCode error, const hidl_vec<uint8_t>& upgraded_blob) {
                               retval = std::tie(error, upgraded_blob);
                           });
    return retval;
}

}  // namespace test
}  // namespace V4_0
}  // namespace keymaster
}  // namespace hardware
}  // namespace android