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 CRYPTO_sysrand benchmarks to speed.cc #1978

Merged
merged 2 commits into from
Nov 13, 2024
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
4 changes: 2 additions & 2 deletions crypto/fipsmodule/rand/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ void RAND_bytes_with_additional_data(uint8_t *out, size_t out_len,

// CRYPTO_sysrand fills |len| bytes at |buf| with entropy from the operating
// system.
void CRYPTO_sysrand(uint8_t *buf, size_t len);
OPENSSL_EXPORT void CRYPTO_sysrand(uint8_t *buf, size_t len);

// CRYPTO_sysrand_for_seed fills |len| bytes at |buf| with entropy from the
// operating system. It may draw from the |GRND_RANDOM| pool on Android,
// depending on the vendor's configuration.
void CRYPTO_sysrand_for_seed(uint8_t *buf, size_t len);
OPENSSL_EXPORT void CRYPTO_sysrand_for_seed(uint8_t *buf, size_t len);

#if defined(OPENSSL_RAND_URANDOM) || defined(OPENSSL_RAND_WINDOWS)
// CRYPTO_init_sysrand initializes long-lived resources needed to draw entropy
Expand Down
20 changes: 13 additions & 7 deletions tool/speed.cc
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ static inline void *align_pointer(void *ptr, size_t alignment) {
}
#endif

#if defined(INTERNAL_TOOL)
#include "../crypto/fipsmodule/rand/internal.h"
#endif


#if defined(OPENSSL_IS_AWSLC) && defined(AARCH64_DIT_SUPPORTED) && (AWSLC_API_VERSION > 30)
Expand Down Expand Up @@ -1254,16 +1257,17 @@ static bool SpeedHmacOneShot(const EVP_MD *md, const std::string &name,
return true;
}

static bool SpeedRandomChunk(std::string name, size_t chunk_len) {
using RandomFunction = std::function<void(uint8_t *, size_t)>;
static bool SpeedRandomChunk(RandomFunction function, std::string name, size_t chunk_len) {
uint8_t scratch[16384];

if (chunk_len > sizeof(scratch)) {
return false;
}

TimeResults results;
if (!TimeFunction(&results, [chunk_len, &scratch]() -> bool {
RAND_bytes(scratch, chunk_len);
if (!TimeFunction(&results, [chunk_len, &scratch, &function]() -> bool {
function(scratch, chunk_len);
return true;
})) {
return false;
Expand All @@ -1273,13 +1277,13 @@ static bool SpeedRandomChunk(std::string name, size_t chunk_len) {
return true;
}

static bool SpeedRandom(const std::string &selected) {
if (!selected.empty() && selected != "RNG") {
static bool SpeedRandom(RandomFunction function, const std::string &name, const std::string &selected) {
if (!selected.empty() && name.find(selected) == std::string::npos) {
return true;
}

for (size_t chunk_len : g_chunk_lengths) {
if (!SpeedRandomChunk("RNG", chunk_len)) {
if (!SpeedRandomChunk(function, name, chunk_len)) {
return false;
}
}
Expand Down Expand Up @@ -2824,7 +2828,7 @@ 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) ||
!SpeedRandom(selected) ||
!SpeedRandom(RAND_bytes, "RNG", selected) ||
!SpeedECDH(selected) ||
!SpeedECDSA(selected) ||
!SpeedECKeyGen(selected) ||
Expand Down Expand Up @@ -2883,6 +2887,8 @@ bool Speed(const std::vector<std::string> &args) {
!SpeedRefcount(selected) ||
#endif
#if defined(INTERNAL_TOOL)
!SpeedRandom(CRYPTO_sysrand, "CRYPTO_sysrand", selected) ||
!SpeedRandom(CRYPTO_sysrand_for_seed, "CRYPTO_sysrand_for_seed", selected) ||
!SpeedHashToCurve(selected) ||
!SpeedTrustToken("TrustToken-Exp1-Batch1", TRUST_TOKEN_experiment_v1(), 1, selected) ||
!SpeedTrustToken("TrustToken-Exp1-Batch10", TRUST_TOKEN_experiment_v1(), 10, selected) ||
Expand Down
Loading