Skip to content

Commit

Permalink
Fix openssl benchmark compatibility layer (#481)
Browse files Browse the repository at this point in the history
Addresses issues with building openssl with our benchmarking
framework
  • Loading branch information
samuel40791765 authored May 4, 2022
1 parent f266226 commit 1396fa7
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
5 changes: 3 additions & 2 deletions tool/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ bool ReadAll(std::vector<uint8_t> *out, FILE *file) {
}
}

bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> 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;
Expand Down
9 changes: 3 additions & 6 deletions tool/generate_ech.cc
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,9 @@ bool GenerateECH(const std::vector<std::string> &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;
}
Expand Down
5 changes: 3 additions & 2 deletions tool/generate_ed25519.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ bool GenerateEd25519Key(const std::vector<std::string> &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));
}
2 changes: 1 addition & 1 deletion tool/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint8_t> *out, FILE *in);
bool WriteToFile(const std::string &path, bssl::Span<const uint8_t> in);
bool WriteToFile(const std::string &path, const uint8_t *in, size_t in_len);

bool Ciphers(const std::vector<std::string> &args);
bool Client(const std::vector<std::string> &args);
Expand Down
2 changes: 2 additions & 0 deletions tool/ossl_bm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 8 additions & 3 deletions tool/speed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,20 @@ 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>(RSA_private_key_from_bytes(
return BM_NAMESPACE::UniquePtr<RSA>(RSA_private_key_from_bytes(
kRSAKeys[i].key, kRSAKeys[i].key_len)) != nullptr;
})) {
fprintf(stderr, "Failed to parse %s key.\n", name.c_str());
ERR_print_errors_fp(stderr);
return false;
}
results.Print(name + " private key parse");
#endif
}

return true;
Expand Down Expand Up @@ -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<EVP_CIPHER_CTX> ctx(EVP_CIPHER_CTX_new());
// Benchmark initialisation and encryption
for (size_t in_len : g_chunk_lengths) {
in.resize(in_len);
Expand Down Expand Up @@ -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<EVP_MD_CTX> ctx(EVP_MD_CTX_new());
uint8_t input[16384] = {0};

if (chunk_len > sizeof(input)) {
Expand Down

0 comments on commit 1396fa7

Please sign in to comment.