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
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
|
/*
* Copyright (C) 2021 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.
*/
#define LOG_TAG "keymint_benchmark"
#include <base/command_line.h>
#include <benchmark/benchmark.h>
#include <iostream>
#include <aidl/Vintf.h>
#include <aidl/android/hardware/security/keymint/ErrorCode.h>
#include <aidl/android/hardware/security/keymint/IKeyMintDevice.h>
#include <android/binder_manager.h>
#include <binder/IServiceManager.h>
#include <keymint_support/authorization_set.h>
#define SMALL_MESSAGE_SIZE 64
#define MEDIUM_MESSAGE_SIZE 1024
#define LARGE_MESSAGE_SIZE 131072
namespace aidl::android::hardware::security::keymint::test {
::std::ostream& operator<<(::std::ostream& os, const keymint::AuthorizationSet& set);
using ::android::sp;
using Status = ::ndk::ScopedAStatus;
using ::std::optional;
using ::std::shared_ptr;
using ::std::string;
using ::std::vector;
class KeyMintBenchmarkTest {
public:
KeyMintBenchmarkTest() {
message_cache_.push_back(string(SMALL_MESSAGE_SIZE, 'x'));
message_cache_.push_back(string(MEDIUM_MESSAGE_SIZE, 'x'));
message_cache_.push_back(string(LARGE_MESSAGE_SIZE, 'x'));
}
static KeyMintBenchmarkTest* newInstance(const char* instanceName) {
if (AServiceManager_isDeclared(instanceName)) {
::ndk::SpAIBinder binder(AServiceManager_waitForService(instanceName));
KeyMintBenchmarkTest* test = new KeyMintBenchmarkTest();
test->InitializeKeyMint(IKeyMintDevice::fromBinder(binder));
return test;
} else {
return nullptr;
}
}
int getError() { return static_cast<int>(error_); }
const string& GenerateMessage(int size) {
for (const string& message : message_cache_) {
if (message.size() == size) {
return message;
}
}
string message = string(size, 'x');
message_cache_.push_back(message);
return std::move(message);
}
optional<BlockMode> getBlockMode(string transform) {
if (transform.find("/ECB") != string::npos) {
return BlockMode::ECB;
} else if (transform.find("/CBC") != string::npos) {
return BlockMode::CBC;
} else if (transform.find("/CTR") != string::npos) {
return BlockMode::CTR;
} else if (transform.find("/GCM") != string::npos) {
return BlockMode::GCM;
}
return {};
}
PaddingMode getPadding(string transform, bool sign) {
if (transform.find("/PKCS7") != string::npos) {
return PaddingMode::PKCS7;
} else if (transform.find("/PSS") != string::npos) {
return PaddingMode::RSA_PSS;
} else if (transform.find("/OAEP") != string::npos) {
return PaddingMode::RSA_OAEP;
} else if (transform.find("/PKCS1") != string::npos) {
return sign ? PaddingMode::RSA_PKCS1_1_5_SIGN : PaddingMode::RSA_PKCS1_1_5_ENCRYPT;
} else if (sign && transform.find("RSA") != string::npos) {
// RSA defaults to PKCS1 for sign
return PaddingMode::RSA_PKCS1_1_5_SIGN;
}
return PaddingMode::NONE;
}
optional<Algorithm> getAlgorithm(string transform) {
if (transform.find("AES") != string::npos) {
return Algorithm::AES;
} else if (transform.find("Hmac") != string::npos) {
return Algorithm::HMAC;
} else if (transform.find("DESede") != string::npos) {
return Algorithm::TRIPLE_DES;
} else if (transform.find("RSA") != string::npos) {
return Algorithm::RSA;
} else if (transform.find("EC") != string::npos) {
return Algorithm::EC;
}
std::cerr << "Can't find algorithm for " << transform << std::endl;
return {};
}
Digest getDigest(string transform) {
if (transform.find("MD5") != string::npos) {
return Digest::MD5;
} else if (transform.find("SHA1") != string::npos ||
transform.find("SHA-1") != string::npos) {
return Digest::SHA1;
} else if (transform.find("SHA224") != string::npos) {
return Digest::SHA_2_224;
} else if (transform.find("SHA256") != string::npos) {
return Digest::SHA_2_256;
} else if (transform.find("SHA384") != string::npos) {
return Digest::SHA_2_384;
} else if (transform.find("SHA512") != string::npos) {
return Digest::SHA_2_512;
} else if (transform.find("RSA") != string::npos &&
transform.find("OAEP") != string::npos) {
return Digest::SHA1;
} else if (transform.find("Hmac") != string::npos) {
return Digest::SHA_2_256;
}
return Digest::NONE;
}
bool GenerateKey(string transform, int keySize, bool sign = false) {
if (transform == key_transform_) {
return true;
} else if (key_transform_ != "") {
// Deleting old key first
key_transform_ = "";
if (DeleteKey() != ErrorCode::OK) {
return false;
}
}
std::optional<Algorithm> algorithm = getAlgorithm(transform);
if (!algorithm) {
std::cerr << "Error: invalid algorithm " << transform << std::endl;
return false;
}
key_transform_ = transform;
AuthorizationSetBuilder authSet = AuthorizationSetBuilder()
.Authorization(TAG_NO_AUTH_REQUIRED)
.Authorization(TAG_PURPOSE, KeyPurpose::ENCRYPT)
.Authorization(TAG_PURPOSE, KeyPurpose::DECRYPT)
.Authorization(TAG_PURPOSE, KeyPurpose::SIGN)
.Authorization(TAG_PURPOSE, KeyPurpose::VERIFY)
.Authorization(TAG_KEY_SIZE, keySize)
.Authorization(TAG_ALGORITHM, algorithm.value())
.Digest(getDigest(transform))
.Padding(getPadding(transform, sign));
std::optional<BlockMode> blockMode = getBlockMode(transform);
if (blockMode) {
authSet.BlockMode(blockMode.value());
if (blockMode == BlockMode::GCM) {
authSet.Authorization(TAG_MIN_MAC_LENGTH, 128);
}
}
if (algorithm == Algorithm::HMAC) {
authSet.Authorization(TAG_MIN_MAC_LENGTH, 128);
}
if (algorithm == Algorithm::RSA) {
authSet.Authorization(TAG_RSA_PUBLIC_EXPONENT, 65537U);
authSet.SetDefaultValidity();
}
if (algorithm == Algorithm::EC) {
authSet.SetDefaultValidity();
}
error_ = GenerateKey(authSet);
return error_ == ErrorCode::OK;
}
AuthorizationSet getOperationParams(string transform, bool sign = false) {
AuthorizationSetBuilder builder = AuthorizationSetBuilder()
.Padding(getPadding(transform, sign))
.Digest(getDigest(transform));
std::optional<BlockMode> blockMode = getBlockMode(transform);
if (sign && (transform.find("Hmac") != string::npos)) {
builder.Authorization(TAG_MAC_LENGTH, 128);
}
if (blockMode) {
builder.BlockMode(*blockMode);
if (blockMode == BlockMode::GCM) {
builder.Authorization(TAG_MAC_LENGTH, 128);
}
}
return std::move(builder);
}
optional<string> Process(const string& message, const string& signature = "") {
ErrorCode result;
string output;
result = Finish(message, signature, &output);
if (result != ErrorCode::OK) {
error_ = result;
return {};
}
return output;
}
ErrorCode DeleteKey() {
Status result = keymint_->deleteKey(key_blob_);
key_blob_ = vector<uint8_t>();
return GetReturnErrorCode(result);
}
ErrorCode Begin(KeyPurpose purpose, const AuthorizationSet& in_params,
AuthorizationSet* out_params) {
Status result;
BeginResult out;
result = keymint_->begin(purpose, key_blob_, in_params.vector_data(), std::nullopt, &out);
if (result.isOk()) {
*out_params = out.params;
op_ = out.operation;
}
return GetReturnErrorCode(result);
}
SecurityLevel securityLevel_;
string name_;
private:
ErrorCode GenerateKey(const AuthorizationSet& key_desc,
const optional<AttestationKey>& attest_key = std::nullopt) {
key_blob_.clear();
KeyCreationResult creationResult;
Status result = keymint_->generateKey(key_desc.vector_data(), attest_key, &creationResult);
if (result.isOk()) {
key_blob_ = std::move(creationResult.keyBlob);
creationResult.keyCharacteristics.clear();
creationResult.certificateChain.clear();
}
return GetReturnErrorCode(result);
}
void InitializeKeyMint(std::shared_ptr<IKeyMintDevice> keyMint) {
if (!keyMint) {
std::cerr << "Trying initialize nullptr in InitializeKeyMint" << std::endl;
return;
}
keymint_ = std::move(keyMint);
KeyMintHardwareInfo info;
Status result = keymint_->getHardwareInfo(&info);
if (!result.isOk()) {
std::cerr << "InitializeKeyMint: getHardwareInfo failed with "
<< result.getServiceSpecificError() << std::endl;
}
securityLevel_ = info.securityLevel;
name_.assign(info.keyMintName.begin(), info.keyMintName.end());
}
ErrorCode Finish(const string& input, const string& signature, string* output) {
if (!op_) {
std::cerr << "Finish: Operation is nullptr" << std::endl;
return ErrorCode::UNEXPECTED_NULL_POINTER;
}
vector<uint8_t> oPut;
Status result =
op_->finish(vector<uint8_t>(input.begin(), input.end()),
vector<uint8_t>(signature.begin(), signature.end()), {} /* authToken */,
{} /* timestampToken */, {} /* confirmationToken */, &oPut);
if (result.isOk()) output->append(oPut.begin(), oPut.end());
op_.reset();
return GetReturnErrorCode(result);
}
ErrorCode Update(const string& input, string* output) {
Status result;
if (!op_) {
std::cerr << "Update: Operation is nullptr" << std::endl;
return ErrorCode::UNEXPECTED_NULL_POINTER;
}
std::vector<uint8_t> o_put;
result = op_->update(vector<uint8_t>(input.begin(), input.end()), {} /* authToken */,
{} /* timestampToken */, &o_put);
if (result.isOk() && output) *output = {o_put.begin(), o_put.end()};
return GetReturnErrorCode(result);
}
ErrorCode GetReturnErrorCode(const Status& result) {
error_ = static_cast<ErrorCode>(result.getServiceSpecificError());
if (result.isOk()) return ErrorCode::OK;
if (result.getExceptionCode() == EX_SERVICE_SPECIFIC) {
return static_cast<ErrorCode>(result.getServiceSpecificError());
}
return ErrorCode::UNKNOWN_ERROR;
}
std::shared_ptr<IKeyMintOperation> op_;
vector<Certificate> cert_chain_;
vector<uint8_t> key_blob_;
vector<KeyCharacteristics> key_characteristics_;
std::shared_ptr<IKeyMintDevice> keymint_;
std::vector<string> message_cache_;
std::string key_transform_;
ErrorCode error_;
};
KeyMintBenchmarkTest* keymintTest;
static void settings(benchmark::internal::Benchmark* benchmark) {
benchmark->Unit(benchmark::kMillisecond);
}
static void addDefaultLabel(benchmark::State& state) {
std::string secLevel;
switch (keymintTest->securityLevel_) {
case SecurityLevel::STRONGBOX:
secLevel = "STRONGBOX";
break;
case SecurityLevel::SOFTWARE:
secLevel = "SOFTWARE";
break;
case SecurityLevel::TRUSTED_ENVIRONMENT:
secLevel = "TEE";
break;
case SecurityLevel::KEYSTORE:
secLevel = "KEYSTORE";
break;
}
state.SetLabel("hardware_name:" + keymintTest->name_ + " sec_level:" + secLevel);
}
// clang-format off
#define BENCHMARK_KM(func, transform, keySize) \
BENCHMARK_CAPTURE(func, transform/keySize, #transform "/" #keySize, keySize)->Apply(settings);
#define BENCHMARK_KM_MSG(func, transform, keySize, msgSize) \
BENCHMARK_CAPTURE(func, transform/keySize/msgSize, #transform "/" #keySize "/" #msgSize, \
keySize, msgSize) \
->Apply(settings);
#define BENCHMARK_KM_ALL_MSGS(func, transform, keySize) \
BENCHMARK_KM_MSG(func, transform, keySize, SMALL_MESSAGE_SIZE) \
BENCHMARK_KM_MSG(func, transform, keySize, MEDIUM_MESSAGE_SIZE) \
BENCHMARK_KM_MSG(func, transform, keySize, LARGE_MESSAGE_SIZE)
#define BENCHMARK_KM_CIPHER(transform, keySize, msgSize) \
BENCHMARK_KM_MSG(encrypt, transform, keySize, msgSize) \
BENCHMARK_KM_MSG(decrypt, transform, keySize, msgSize)
#define BENCHMARK_KM_CIPHER_ALL_MSGS(transform, keySize) \
BENCHMARK_KM_ALL_MSGS(encrypt, transform, keySize) \
BENCHMARK_KM_ALL_MSGS(decrypt, transform, keySize)
#define BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, keySize) \
BENCHMARK_KM_ALL_MSGS(sign, transform, keySize) \
BENCHMARK_KM_ALL_MSGS(verify, transform, keySize)
// clang-format on
/*
* ============= KeyGen TESTS ==================
*/
static void keygen(benchmark::State& state, string transform, int keySize) {
addDefaultLabel(state);
for (auto _ : state) {
if (!keymintTest->GenerateKey(transform, keySize)) {
state.SkipWithError(
("Key generation error, " + std::to_string(keymintTest->getError())).c_str());
}
state.PauseTiming();
keymintTest->DeleteKey();
state.ResumeTiming();
}
}
BENCHMARK_KM(keygen, AES, 128);
BENCHMARK_KM(keygen, AES, 256);
BENCHMARK_KM(keygen, RSA, 2048);
BENCHMARK_KM(keygen, RSA, 3072);
BENCHMARK_KM(keygen, RSA, 4096);
BENCHMARK_KM(keygen, EC, 224);
BENCHMARK_KM(keygen, EC, 256);
BENCHMARK_KM(keygen, EC, 384);
BENCHMARK_KM(keygen, EC, 521);
BENCHMARK_KM(keygen, DESede, 168);
BENCHMARK_KM(keygen, Hmac, 64);
BENCHMARK_KM(keygen, Hmac, 128);
BENCHMARK_KM(keygen, Hmac, 256);
BENCHMARK_KM(keygen, Hmac, 512);
/*
* ============= SIGNATURE TESTS ==================
*/
static void sign(benchmark::State& state, string transform, int keySize, int msgSize) {
addDefaultLabel(state);
if (!keymintTest->GenerateKey(transform, keySize, true)) {
state.SkipWithError(
("Key generation error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
auto in_params = keymintTest->getOperationParams(transform, true);
AuthorizationSet out_params;
string message = keymintTest->GenerateMessage(msgSize);
for (auto _ : state) {
state.PauseTiming();
ErrorCode error = keymintTest->Begin(KeyPurpose::SIGN, in_params, &out_params);
if (error != ErrorCode::OK) {
state.SkipWithError(
("Error beginning sign, " + std::to_string(keymintTest->getError())).c_str());
return;
}
state.ResumeTiming();
out_params.Clear();
if (!keymintTest->Process(message)) {
state.SkipWithError(("Sign error, " + std::to_string(keymintTest->getError())).c_str());
break;
}
}
}
static void verify(benchmark::State& state, string transform, int keySize, int msgSize) {
addDefaultLabel(state);
if (!keymintTest->GenerateKey(transform, keySize, true)) {
state.SkipWithError(
("Key generation error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
AuthorizationSet out_params;
auto in_params = keymintTest->getOperationParams(transform, true);
string message = keymintTest->GenerateMessage(msgSize);
ErrorCode error = keymintTest->Begin(KeyPurpose::SIGN, in_params, &out_params);
if (error != ErrorCode::OK) {
state.SkipWithError(
("Error beginning sign, " + std::to_string(keymintTest->getError())).c_str());
return;
}
std::optional<string> signature = keymintTest->Process(message);
if (!signature) {
state.SkipWithError(("Sign error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
out_params.Clear();
if (transform.find("Hmac") != string::npos) {
in_params = keymintTest->getOperationParams(transform, false);
}
for (auto _ : state) {
state.PauseTiming();
error = keymintTest->Begin(KeyPurpose::VERIFY, in_params, &out_params);
if (error != ErrorCode::OK) {
state.SkipWithError(
("Verify begin error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
state.ResumeTiming();
if (!keymintTest->Process(message, *signature)) {
state.SkipWithError(
("Verify error, " + std::to_string(keymintTest->getError())).c_str());
break;
}
}
}
// clang-format off
#define BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(transform) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 64) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 128) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 256) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 512)
BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(HmacSHA1)
BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(HmacSHA256)
BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(HmacSHA224)
BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(HmacSHA256)
BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(HmacSHA384)
BENCHMARK_KM_SIGNATURE_ALL_HMAC_KEYS(HmacSHA512)
#define BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(transform) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 224) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 256) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 384) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 521)
BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(NONEwithECDSA);
BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(SHA1withECDSA);
BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(SHA224withECDSA);
BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(SHA256withECDSA);
BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(SHA384withECDSA);
BENCHMARK_KM_SIGNATURE_ALL_ECDSA_KEYS(SHA512withECDSA);
#define BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(transform) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 2048) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 3072) \
BENCHMARK_KM_SIGNATURE_ALL_MSGS(transform, 4096)
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(MD5withRSA);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA1withRSA);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA224withRSA);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA384withRSA);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA512withRSA);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(MD5withRSA/PSS);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA1withRSA/PSS);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA224withRSA/PSS);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA384withRSA/PSS);
BENCHMARK_KM_SIGNATURE_ALL_RSA_KEYS(SHA512withRSA/PSS);
// clang-format on
/*
* ============= CIPHER TESTS ==================
*/
static void encrypt(benchmark::State& state, string transform, int keySize, int msgSize) {
addDefaultLabel(state);
if (!keymintTest->GenerateKey(transform, keySize)) {
state.SkipWithError(
("Key generation error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
auto in_params = keymintTest->getOperationParams(transform);
AuthorizationSet out_params;
string message = keymintTest->GenerateMessage(msgSize);
for (auto _ : state) {
state.PauseTiming();
auto error = keymintTest->Begin(KeyPurpose::ENCRYPT, in_params, &out_params);
if (error != ErrorCode::OK) {
state.SkipWithError(
("Encryption begin error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
out_params.Clear();
state.ResumeTiming();
if (!keymintTest->Process(message)) {
state.SkipWithError(
("Encryption error, " + std::to_string(keymintTest->getError())).c_str());
break;
}
}
}
static void decrypt(benchmark::State& state, string transform, int keySize, int msgSize) {
addDefaultLabel(state);
if (!keymintTest->GenerateKey(transform, keySize)) {
state.SkipWithError(
("Key generation error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
AuthorizationSet out_params;
AuthorizationSet in_params = keymintTest->getOperationParams(transform);
string message = keymintTest->GenerateMessage(msgSize);
auto error = keymintTest->Begin(KeyPurpose::ENCRYPT, in_params, &out_params);
if (error != ErrorCode::OK) {
state.SkipWithError(
("Encryption begin error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
auto encryptedMessage = keymintTest->Process(message);
if (!encryptedMessage) {
state.SkipWithError(
("Encryption error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
in_params.push_back(out_params);
out_params.Clear();
for (auto _ : state) {
state.PauseTiming();
error = keymintTest->Begin(KeyPurpose::DECRYPT, in_params, &out_params);
if (error != ErrorCode::OK) {
state.SkipWithError(
("Decryption begin error, " + std::to_string(keymintTest->getError())).c_str());
return;
}
state.ResumeTiming();
if (!keymintTest->Process(*encryptedMessage)) {
state.SkipWithError(
("Decryption error, " + std::to_string(keymintTest->getError())).c_str());
break;
}
}
}
// clang-format off
// AES
#define BENCHMARK_KM_CIPHER_ALL_AES_KEYS(transform) \
BENCHMARK_KM_CIPHER_ALL_MSGS(transform, 128) \
BENCHMARK_KM_CIPHER_ALL_MSGS(transform, 256)
BENCHMARK_KM_CIPHER_ALL_AES_KEYS(AES/CBC/NoPadding);
BENCHMARK_KM_CIPHER_ALL_AES_KEYS(AES/CBC/PKCS7Padding);
BENCHMARK_KM_CIPHER_ALL_AES_KEYS(AES/CTR/NoPadding);
BENCHMARK_KM_CIPHER_ALL_AES_KEYS(AES/ECB/NoPadding);
BENCHMARK_KM_CIPHER_ALL_AES_KEYS(AES/ECB/PKCS7Padding);
BENCHMARK_KM_CIPHER_ALL_AES_KEYS(AES/GCM/NoPadding);
// Triple DES
BENCHMARK_KM_CIPHER_ALL_MSGS(DESede/CBC/NoPadding, 168);
BENCHMARK_KM_CIPHER_ALL_MSGS(DESede/CBC/PKCS7Padding, 168);
BENCHMARK_KM_CIPHER_ALL_MSGS(DESede/ECB/NoPadding, 168);
BENCHMARK_KM_CIPHER_ALL_MSGS(DESede/ECB/PKCS7Padding, 168);
#define BENCHMARK_KM_CIPHER_ALL_RSA_KEYS(transform, msgSize) \
BENCHMARK_KM_CIPHER(transform, 2048, msgSize) \
BENCHMARK_KM_CIPHER(transform, 3072, msgSize) \
BENCHMARK_KM_CIPHER(transform, 4096, msgSize)
BENCHMARK_KM_CIPHER_ALL_RSA_KEYS(RSA/ECB/NoPadding, SMALL_MESSAGE_SIZE);
BENCHMARK_KM_CIPHER_ALL_RSA_KEYS(RSA/ECB/PKCS1Padding, SMALL_MESSAGE_SIZE);
BENCHMARK_KM_CIPHER_ALL_RSA_KEYS(RSA/ECB/OAEPPadding, SMALL_MESSAGE_SIZE);
// clang-format on
} // namespace aidl::android::hardware::security::keymint::test
int main(int argc, char** argv) {
::benchmark::Initialize(&argc, argv);
base::CommandLine::Init(argc, argv);
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
auto service_name = command_line->GetSwitchValueASCII("service_name");
if (service_name.empty()) {
service_name =
std::string(
aidl::android::hardware::security::keymint::IKeyMintDevice::descriptor) +
"/default";
}
std::cerr << service_name << std::endl;
aidl::android::hardware::security::keymint::test::keymintTest =
aidl::android::hardware::security::keymint::test::KeyMintBenchmarkTest::newInstance(
service_name.c_str());
if (!aidl::android::hardware::security::keymint::test::keymintTest) {
return 1;
}
::benchmark::RunSpecifiedBenchmarks();
}
|