Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CMAC benchmark for AWS-LC #2218

Merged
merged 2 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tool/ossl_bm.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ OSSL_MAKE_DELETER(BN_CTX, BN_CTX_free)
OSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free)
OSSL_MAKE_DELETER(EVP_PKEY_CTX, EVP_PKEY_CTX_free)
OSSL_MAKE_DELETER(EVP_PKEY, EVP_PKEY_free)
OSSL_MAKE_DELETER(CMAC_CTX, CMAC_CTX_free)

// OpenSSL 1.0.x has different APIs for EVP_MD_CTX and HMAC
// We need to add more custom logic to HMAC to let it properly delete the
Expand Down
70 changes: 70 additions & 0 deletions tool/speed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "internal.h"

#include <openssl/crypto.h>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: extra space?

#include "openssl/cmac.h"
#if defined(OPENSSL_IS_AWSLC)
#include "bssl_bm.h"
#include "../crypto/internal.h"
Expand Down Expand Up @@ -1262,6 +1264,70 @@ static bool SpeedHmacOneShot(const EVP_MD *md, const std::string &name,
return true;
}

#if !defined(OPENSSL_1_0_BENCHMARK)
static bool SpeedCmacChunk(const EVP_CIPHER *cipher, std::string name,
size_t chunk_len) {
BM_NAMESPACE::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new());
std::unique_ptr<uint8_t[]> input(new uint8_t[chunk_len]);
const size_t key_len = EVP_CIPHER_key_length(cipher);
std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
BM_memset(key.get(), 0, key_len);

if (!CMAC_Init(ctx.get(), key.get(), key_len, cipher, NULL /* ENGINE */)) {
fprintf(stderr, "Failed to create CMAC_CTX.\n");
}
TimeResults results;
if (!TimeFunction(&results, [&ctx, chunk_len, &input]() -> bool {
uint8_t digest[EVP_MAX_MD_SIZE];
size_t out_len;

return
#if defined(OPENSSL_IS_AWSLC) || defined(OPENSSL_IS_BORINGSSL)
CMAC_Reset(ctx.get()) &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just checking the reset call is enough for LC, we don't need an init on top of this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct, reset is a AWS-LC/BroingSSL helper function https://github.com/aws/aws-lc/blob/main/include/openssl/cmac.h#L64 that keeps the key but resets the state so a new message can be authenticated.

#else
CMAC_Init(ctx.get(), nullptr, 0, nullptr, nullptr /* ENGINE */) &&
#endif
CMAC_Update(ctx.get(), input.get(), chunk_len) &&
CMAC_Final(ctx.get(), digest, &out_len);
})) {
fprintf(stderr, "CMAC_Final failed.\n");
ERR_print_errors_fp(stderr);
return false;
}

results.PrintWithBytes(name, chunk_len);
return true;
}

static bool SpeedCmac(const EVP_CIPHER *cipher, const std::string &name, const std::string &selected) {
if (!selected.empty() && name.find(selected) == std::string::npos) {
return true;
}
TimeResults results;
const size_t key_len = EVP_CIPHER_key_length(cipher);
std::unique_ptr<uint8_t[]> key(new uint8_t[key_len]);
BM_memset(key.get(), 0, key_len);
BM_NAMESPACE::UniquePtr<CMAC_CTX> ctx(CMAC_CTX_new());
if (!TimeFunction(&results, [&]() -> bool {
return CMAC_Init(ctx.get(), key.get(), key_len, cipher,
nullptr /* ENGINE */);
})) {
fprintf(stderr, "CMAC_Init failed.\n");
ERR_print_errors_fp(stderr);
return false;
}
results.Print(name + " init");

for (size_t chunk_len : g_chunk_lengths) {
if (!SpeedCmacChunk(cipher, name, chunk_len)) {
return false;
}
}

return true;
}

#endif

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