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

Simplify getting an EVP_MD for all OpenSSL forks #796

Merged
merged 2 commits into from
Mar 22, 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
53 changes: 9 additions & 44 deletions src/rs1.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,54 +10,20 @@

#include "fido.h"

#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3050200fL
static EVP_MD *
rs1_get_EVP_MD(void)
{
const EVP_MD *from;
EVP_MD *to = NULL;

if ((from = EVP_sha1()) != NULL && (to = malloc(sizeof(*to))) != NULL)
memcpy(to, from, sizeof(*to));

return (to);
}

static void
rs1_free_EVP_MD(EVP_MD *md)
{
freezero(md, sizeof(*md));
}
#elif OPENSSL_VERSION_NUMBER >= 0x30000000
static EVP_MD *
rs1_get_EVP_MD(void)
{
return (EVP_MD_fetch(NULL, "SHA-1", NULL));
}

static void
rs1_free_EVP_MD(EVP_MD *md)
{
EVP_MD_free(md);
}
#if defined(__GNUC__)
#define PRAGMA(s) _Pragma(s)
#else
#define PRAGMA(s)
#endif

static EVP_MD *
rs1_get_EVP_MD(void)
{
const EVP_MD *md;

if ((md = EVP_sha1()) == NULL)
return (NULL);

return (EVP_MD_meth_dup(md));
}

static void
rs1_free_EVP_MD(EVP_MD *md)
{
EVP_MD_meth_free(md);
PRAGMA("GCC diagnostic push")
PRAGMA("GCC diagnostic ignored \"-Wcast-qual\"")
return ((EVP_MD *)EVP_sha1());
PRAGMA("GCC diagnostic pop")
}
#endif /* LIBRESSL_VERSION_NUMBER */

int
rs1_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
Expand Down Expand Up @@ -94,7 +60,6 @@ rs1_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
ok = 0;
fail:
EVP_PKEY_CTX_free(pctx);
rs1_free_EVP_MD(md);

return (ok);
}
53 changes: 9 additions & 44 deletions src/rs256.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,20 @@
#define get0_RSA(x) EVP_PKEY_get0((x))
#endif

#if defined(LIBRESSL_VERSION_NUMBER) && LIBRESSL_VERSION_NUMBER < 0x3050200fL
static EVP_MD *
rs256_get_EVP_MD(void)
{
const EVP_MD *from;
EVP_MD *to = NULL;

if ((from = EVP_sha256()) != NULL && (to = malloc(sizeof(*to))) != NULL)
memcpy(to, from, sizeof(*to));

return (to);
}

static void
rs256_free_EVP_MD(EVP_MD *md)
{
freezero(md, sizeof(*md));
}
#elif OPENSSL_VERSION_NUMBER >= 0x30000000
static EVP_MD *
rs256_get_EVP_MD(void)
{
return (EVP_MD_fetch(NULL, "SHA2-256", NULL));
}

static void
rs256_free_EVP_MD(EVP_MD *md)
{
EVP_MD_free(md);
}
#if defined(__GNUC__)
#define PRAGMA(s) _Pragma(s)
#else
#define PRAGMA(s)
#endif

static EVP_MD *
rs256_get_EVP_MD(void)
{
const EVP_MD *md;

if ((md = EVP_sha256()) == NULL)
return (NULL);

return (EVP_MD_meth_dup(md));
}

static void
rs256_free_EVP_MD(EVP_MD *md)
{
EVP_MD_meth_free(md);
PRAGMA("GCC diagnostic push")
PRAGMA("GCC diagnostic ignored \"-Wcast-qual\"")
return ((EVP_MD *)EVP_sha256());
PRAGMA("GCC diagnostic pop")
}
#endif /* LIBRESSL_VERSION_NUMBER */

static int
decode_bignum(const cbor_item_t *item, void *ptr, size_t len)
Expand Down Expand Up @@ -290,7 +256,6 @@ rs256_verify_sig(const fido_blob_t *dgst, EVP_PKEY *pkey,
ok = 0;
fail:
EVP_PKEY_CTX_free(pctx);
rs256_free_EVP_MD(md);

return (ok);
}
Expand Down
Loading