Skip to content

Commit 51c6c84

Browse files
authored
Add CMAC benchmark for AWS-LC (aws#2218)
### Description of changes: Adds a benchmark for CMAC, OpenSSL 1.0.2 does not support CMAC. By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license and the ISC license.
1 parent 9b814f0 commit 51c6c84

File tree

2 files changed

+71
-0
lines changed

2 files changed

+71
-0
lines changed

tool/ossl_bm.h

+1
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ OSSL_MAKE_DELETER(BN_CTX, BN_CTX_free)
6464
OSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free)
6565
OSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free)
6666
OSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
67+
OSSL_MAKE_DELETER(CMAC_CTX, CMAC_CTX_free)
6768

6869
// OpenSSL 1.0.x has different APIs for EVP_MD_CTX and HMAC
6970
// We need to add more custom logic to HMAC to let it properly delete the

tool/speed.cc

+70
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#include "internal.h"
3535

3636
#include <openssl/crypto.h>
37+
38+
#include "openssl/cmac.h"
3739
#if defined(OPENSSL_IS_AWSLC)
3840
#include "bssl_bm.h"
3941
#include "../crypto/internal.h"
@@ -1262,6 +1264,70 @@ static bool SpeedHmacOneShot(const EVP_MD *md, const std::string &name,
12621264
return true;
12631265
}
12641266

1267+
#if !defined(OPENSSL_1_0_BENCHMARK)
1268+
static bool SpeedCmacChunk(const EVP_CIPHER *cipher, std::string name,
1269+
size_t chunk_len) {
1270+
BM_NAMESPACE::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new());
1271+
std::unique_ptr<uint8_t[]> input(new uint8_t[chunk_len]);
1272+
const size_t key_len = EVP_CIPHER_key_length(cipher);
1273+
std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
1274+
BM_memset(key.get(), 0, key_len);
1275+
1276+
if (!CMAC_Init(ctx.get(), key.get(), key_len, cipher, NULL /* ENGINE */)) {
1277+
fprintf(stderr, "Failed to create CMAC_CTX.\n");
1278+
}
1279+
TimeResults results;
1280+
if (!TimeFunction(&results, [&ctx, chunk_len, &input]() -> bool {
1281+
uint8_t digest[EVP_MAX_MD_SIZE];
1282+
size_t out_len;
1283+
1284+
return
1285+
#if defined(OPENSSL_IS_AWSLC) || defined(OPENSSL_IS_BORINGSSL)
1286+
CMAC_Reset(ctx.get()) &&
1287+
#else
1288+
CMAC_Init(ctx.get(), nullptr, 0, nullptr, nullptr /* ENGINE */) &&
1289+
#endif
1290+
CMAC_Update(ctx.get(), input.get(), chunk_len) &&
1291+
CMAC_Final(ctx.get(), digest, &out_len);
1292+
})) {
1293+
fprintf(stderr, "CMAC_Final failed.\n");
1294+
ERR_print_errors_fp(stderr);
1295+
return false;
1296+
}
1297+
1298+
results.PrintWithBytes(name, chunk_len);
1299+
return true;
1300+
}
1301+
1302+
static bool SpeedCmac(const EVP_CIPHER *cipher, const std::string &name, const std::string &selected) {
1303+
if (!selected.empty() && name.find(selected) == std::string::npos) {
1304+
return true;
1305+
}
1306+
TimeResults results;
1307+
const size_t key_len = EVP_CIPHER_key_length(cipher);
1308+
std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
1309+
BM_memset(key.get(), 0, key_len);
1310+
BM_NAMESPACE::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new());
1311+
if (!TimeFunction(&results, [&]() -> bool {
1312+
return CMAC_Init(ctx.get(), key.get(), key_len, cipher,
1313+
nullptr /* ENGINE */);
1314+
})) {
1315+
fprintf(stderr, "CMAC_Init failed.\n");
1316+
ERR_print_errors_fp(stderr);
1317+
return false;
1318+
}
1319+
results.Print(name + " init");
1320+
1321+
for (size_t chunk_len : g_chunk_lengths) {
1322+
if (!SpeedCmacChunk(cipher, name, chunk_len)) {
1323+
return false;
1324+
}
1325+
}
1326+
1327+
return true;
1328+
}
1329+
1330+
#endif
12651331

12661332
using RandomFunction = std::function<void(uint8_t *, size_t)>;
12671333
static bool SpeedRandomChunk(RandomFunction function, std::string name, size_t chunk_len) {
@@ -2832,6 +2898,10 @@ bool Speed(const std::vector<std::string> &args) {
28322898
!SpeedHmacOneShot(EVP_sha256(), "HMAC-SHA256-OneShot", selected) ||
28332899
!SpeedHmacOneShot(EVP_sha384(), "HMAC-SHA384-OneShot", selected) ||
28342900
!SpeedHmacOneShot(EVP_sha512(), "HMAC-SHA512-OneShot", selected) ||
2901+
#if !defined(OPENSSL_1_0_BENCHMARK)
2902+
!SpeedCmac(EVP_aes_128_cbc(), "CMAC-AES-128-CBC", selected) ||
2903+
!SpeedCmac(EVP_aes_256_cbc(), "CMAC-AES-256-CBC", selected) ||
2904+
#endif
28352905
!SpeedRandom(RAND_bytes, "RNG", selected) ||
28362906
!SpeedECDH(selected) ||
28372907
!SpeedECDSA(selected) ||

0 commit comments

Comments
 (0)