From 1396fa7112c30195aa69e4205c2cf247639b9c72 Mon Sep 17 00:00:00 2001 From: Samuel Chiang Date: Tue, 3 May 2022 17:04:12 -0700 Subject: [PATCH] Fix openssl benchmark compatibility layer (#481) Addresses issues with building openssl with our benchmarking framework --- tool/file.cc | 5 +++-- tool/generate_ech.cc | 9 +++------ tool/generate_ed25519.cc | 5 +++-- tool/internal.h | 2 +- tool/ossl_bm.h | 2 ++ tool/speed.cc | 11 ++++++++--- 6 files changed, 20 insertions(+), 14 deletions(-) diff --git a/tool/file.cc b/tool/file.cc index 8d74e635bc..db3f9dc1b2 100644 --- a/tool/file.cc +++ b/tool/file.cc @@ -53,13 +53,14 @@ bool ReadAll(std::vector *out, FILE *file) { } } -bool WriteToFile(const std::string &path, bssl::Span in) { +bool WriteToFile(const std::string &path, const uint8_t *in, + size_t in_len) { ScopedFILE file(fopen(path.c_str(), "wb")); if (!file) { fprintf(stderr, "Failed to open '%s': %s\n", path.c_str(), strerror(errno)); return false; } - if (fwrite(in.data(), in.size(), 1, file.get()) != 1) { + if (fwrite(in, in_len, 1, file.get()) != 1) { fprintf(stderr, "Failed to write to '%s': %s\n", path.c_str(), strerror(errno)); return false; diff --git a/tool/generate_ech.cc b/tool/generate_ech.cc index dfde1c9c0a..4387b57a8a 100644 --- a/tool/generate_ech.cc +++ b/tool/generate_ech.cc @@ -120,12 +120,9 @@ bool GenerateECH(const std::vector &args) { return false; } if (!WriteToFile( - args_map["-out-ech-config-list"], - bssl::MakeConstSpan(CBB_data(cbb.get()), CBB_len(cbb.get()))) || - !WriteToFile(args_map["-out-ech-config"], - bssl::MakeConstSpan(ech_config, ech_config_len)) || - !WriteToFile(args_map["-out-private-key"], - bssl::MakeConstSpan(private_key, private_key_len))) { + args_map["-out-ech-config-list"], CBB_data(cbb.get()), CBB_len(cbb.get())) || + !WriteToFile(args_map["-out-ech-config"], ech_config, ech_config_len) || + !WriteToFile(args_map["-out-private-key"], private_key, private_key_len)) { fprintf(stderr, "Failed to write ECHConfig or private key to file\n"); return false; } diff --git a/tool/generate_ed25519.cc b/tool/generate_ed25519.cc index c9b22abe43..fd90823bb1 100644 --- a/tool/generate_ed25519.cc +++ b/tool/generate_ed25519.cc @@ -45,6 +45,7 @@ bool GenerateEd25519Key(const std::vector &args) { uint8_t public_key[32], private_key[64]; ED25519_keypair(public_key, private_key); - return WriteToFile(args_map["-out-public"], public_key) && - WriteToFile(args_map["-out-private"], private_key); + return WriteToFile(args_map["-out-public"], public_key, sizeof(public_key)) && + WriteToFile(args_map["-out-private"], private_key, + sizeof(private_key)); } diff --git a/tool/internal.h b/tool/internal.h index 9333f1b685..5d940efd8b 100644 --- a/tool/internal.h +++ b/tool/internal.h @@ -123,7 +123,7 @@ void PrintUsage(const argument_t *templates); bool GetUnsigned(unsigned *out, const std::string &arg_name, unsigned default_value, const args_map_t &args); bool ReadAll(std::vector *out, FILE *in); -bool WriteToFile(const std::string &path, bssl::Span in); +bool WriteToFile(const std::string &path, const uint8_t *in, size_t in_len); bool Ciphers(const std::vector &args); bool Client(const std::vector &args); diff --git a/tool/ossl_bm.h b/tool/ossl_bm.h index a7257353f8..74d3d77154 100644 --- a/tool/ossl_bm.h +++ b/tool/ossl_bm.h @@ -49,6 +49,8 @@ OSSL_MAKE_DELETER(EC_KEY, EC_KEY_free) OSSL_MAKE_DELETER(EC_POINT, EC_POINT_free) OSSL_MAKE_DELETER(BN_CTX, BN_CTX_free) OSSL_MAKE_DELETER(EVP_MD_CTX, EVP_MD_CTX_free) +OSSL_MAKE_DELETER(EVP_CIPHER_CTX, EVP_CIPHER_CTX_free) +OSSL_MAKE_DELETER(HMAC_CTX, HMAC_CTX_free) } // namespace ossl #endif //OPENSSL_HEADER_TOOL_OSSLBM_H diff --git a/tool/speed.cc b/tool/speed.cc index 8b4520998f..bc9dae81a0 100644 --- a/tool/speed.cc +++ b/tool/speed.cc @@ -266,8 +266,12 @@ static bool SpeedRSA(const std::string &selected) { } results.Print(name + " verify (fresh key)"); +// |RSA_private_key_from_bytes| is not available in OpenSSL. +// TODO: Add support for OpenSSL RSA private key parsing benchmarks. Tracked in +// CryptoAlg-1092. +#if !defined(OPENSSL_BENCHMARK) if (!TimeFunction(&results, [&]() -> bool { - return bssl::UniquePtr(RSA_private_key_from_bytes( + return BM_NAMESPACE::UniquePtr(RSA_private_key_from_bytes( kRSAKeys[i].key, kRSAKeys[i].key_len)) != nullptr; })) { fprintf(stderr, "Failed to parse %s key.\n", name.c_str()); @@ -275,6 +279,7 @@ static bool SpeedRSA(const std::string &selected) { return false; } results.Print(name + " private key parse"); +#endif } return true; @@ -573,7 +578,7 @@ static bool SpeedAES256XTS(const std::string &name, //const size_t in_len, return i++; }); - BM_NAMESPACE::ScopedEVP_CIPHER_CTX ctx; + BM_NAMESPACE::UniquePtr ctx(EVP_CIPHER_CTX_new()); // Benchmark initialisation and encryption for (size_t in_len : g_chunk_lengths) { in.resize(in_len); @@ -625,7 +630,7 @@ static bool SpeedAES256XTS(const std::string &name, //const size_t in_len, static bool SpeedHashChunk(const EVP_MD *md, std::string name, size_t chunk_len) { - bssl::ScopedEVP_MD_CTX ctx; + BM_NAMESPACE::UniquePtr ctx(EVP_MD_CTX_new()); uint8_t input[16384] = {0}; if (chunk_len > sizeof(input)) {