Skip to content

Commit

Permalink
fuzz: use explicit fetching for digest algorithms
Browse files Browse the repository at this point in the history
For better performance it is recommended to use the modern OpenSSL
EVP_MD_fetch API to load digest algorithms (i.e. explicit fetching),
instead of the older implicit fetching API.

As a side effect, using this API seems to avoid memory leaks with some
versions of OpenSSL.
  • Loading branch information
morehouse committed Feb 2, 2024
1 parent 777f715 commit 375210e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
15 changes: 10 additions & 5 deletions tests/fuzz/fuzz-ripemd160.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <openssl/ripemd.h>
#include <tests/fuzz/libfuzz.h>

static EVP_MD *ripemd160_algo;

/* Some versions of OpenSSL removed ripemd160 from the default provider. Check
* and load the legacy provider if necessary. */
void init(int *argc, char ***argv)
Expand All @@ -18,12 +20,15 @@ void init(int *argc, char ***argv)
u8 openssl_hash[RIPEMD160_DIGEST_LENGTH];
unsigned hash_size;

if (!EVP_Digest(data, sizeof(data), openssl_hash, &hash_size,
EVP_ripemd160(), NULL)) {
ripemd160_algo = EVP_MD_fetch(NULL, "RIPEMD-160", NULL);
if (!ripemd160_algo) {
OSSL_PROVIDER_load(NULL, "legacy");
assert(EVP_Digest(data, sizeof(data), openssl_hash, &hash_size,
EVP_ripemd160(), NULL));
ripemd160_algo = EVP_MD_fetch(NULL, "RIPEMD-160", NULL);
assert(ripemd160_algo);
}

assert(EVP_Digest(data, sizeof(data), openssl_hash, &hash_size,
ripemd160_algo, NULL));
assert(hash_size == RIPEMD160_DIGEST_LENGTH);
}

Expand Down Expand Up @@ -54,7 +59,7 @@ static void test_vs_openssl(const struct ripemd160 *expected, const u8 *data,
u8 openssl_hash[RIPEMD160_DIGEST_LENGTH];
unsigned hash_size;

assert(EVP_Digest(data, size, openssl_hash, &hash_size, EVP_ripemd160(),
assert(EVP_Digest(data, size, openssl_hash, &hash_size, ripemd160_algo,
NULL));
assert(hash_size == RIPEMD160_DIGEST_LENGTH);
assert(memeq(expected, sizeof(*expected), openssl_hash,
Expand Down
10 changes: 8 additions & 2 deletions tests/fuzz/fuzz-sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,13 @@
#include <openssl/sha.h>
#include <tests/fuzz/libfuzz.h>

void init(int *argc, char ***argv) {}
static EVP_MD *sha256_algo;

void init(int *argc, char ***argv)
{
sha256_algo = EVP_MD_fetch(NULL, "SHA-256", NULL);
assert(sha256_algo);
}

/* Test that splitting the data and hashing via multiple updates yields the same
* result as not splitting the data. */
Expand Down Expand Up @@ -38,7 +44,7 @@ static void test_vs_openssl(const struct sha256 *expected, const u8 *data,
u8 openssl_hash[SHA256_DIGEST_LENGTH];
unsigned hash_size;

assert(EVP_Digest(data, size, openssl_hash, &hash_size, EVP_sha256(),
assert(EVP_Digest(data, size, openssl_hash, &hash_size, sha256_algo,
NULL));
assert(hash_size == SHA256_DIGEST_LENGTH);
assert(memeq(expected, sizeof(*expected), openssl_hash,
Expand Down

0 comments on commit 375210e

Please sign in to comment.