From 0a0c4510c63b3ed05c4a35d0175ce78900516389 Mon Sep 17 00:00:00 2001 From: tb <> Date: Sat, 13 Jan 2024 11:12:32 +0000 Subject: Reimplement EVP_get_{cipher,digest}byname() Instead of a hashtable lookup do a bsearch() over the static table. This needs about the same number of strcmp and is a lot simpler. ok jsing --- src/lib/libcrypto/evp/evp_names.c | 47 ++++++++++++++++++++++++++++++++++++++- src/lib/libcrypto/evp/names.c | 20 +---------------- 2 files changed, 47 insertions(+), 20 deletions(-) (limited to 'src/lib') diff --git a/src/lib/libcrypto/evp/evp_names.c b/src/lib/libcrypto/evp/evp_names.c index b5d3b95b45..660e23f66e 100644 --- a/src/lib/libcrypto/evp/evp_names.c +++ b/src/lib/libcrypto/evp/evp_names.c @@ -1,4 +1,4 @@ -/* $OpenBSD: evp_names.c,v 1.3 2024/01/13 11:08:39 tb Exp $ */ +/* $OpenBSD: evp_names.c,v 1.4 2024/01/13 11:12:32 tb Exp $ */ /* * Copyright (c) 2023 Theo Buehler * @@ -18,6 +18,9 @@ #include #include +#include +#include + /* * In the following two structs, .name is the lookup name that is used * for EVP_get_cipherbyname() and EVP_get_digestbyname(), while .alias @@ -1715,3 +1718,45 @@ OBJ_NAME_do_all(int type, void (*fn)(const OBJ_NAME *, void *), void *arg) OBJ_NAME_do_all_sorted(type, fn, arg); } LCRYPTO_ALIAS(OBJ_NAME_do_all); + +static int +cipher_cmp(const void *a, const void *b) +{ + return strcmp(a, ((const struct cipher_name *)b)->name); +} + +const EVP_CIPHER * +EVP_get_cipherbyname(const char *name) +{ + const struct cipher_name *cipher; + + if (!OPENSSL_init_crypto(0, NULL)) + return NULL; + + if ((cipher = bsearch(name, cipher_names, N_CIPHER_NAMES, + sizeof(*cipher), cipher_cmp)) == NULL) + return NULL; + + return cipher->cipher(); +} + +static int +digest_cmp(const void *a, const void *b) +{ + return strcmp(a, ((const struct digest_name *)b)->name); +} + +const EVP_MD * +EVP_get_digestbyname(const char *name) +{ + const struct digest_name *digest; + + if (!OPENSSL_init_crypto(0, NULL)) + return NULL; + + if ((digest = bsearch(name, digest_names, N_DIGEST_NAMES, + sizeof(*digest), digest_cmp)) == NULL) + return NULL; + + return digest->digest(); +} diff --git a/src/lib/libcrypto/evp/names.c b/src/lib/libcrypto/evp/names.c index 7f5b455088..8cc6d04ce3 100644 --- a/src/lib/libcrypto/evp/names.c +++ b/src/lib/libcrypto/evp/names.c @@ -1,4 +1,4 @@ -/* $OpenBSD: names.c,v 1.23 2024/01/13 11:08:39 tb Exp $ */ +/* $OpenBSD: names.c,v 1.24 2024/01/13 11:12:32 tb Exp $ */ /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) * All rights reserved. * @@ -113,24 +113,6 @@ EVP_add_digest(const EVP_MD *md) return (r); } -const EVP_CIPHER * -EVP_get_cipherbyname(const char *name) -{ - if (!OPENSSL_init_crypto(0, NULL)) - return NULL; - - return (const EVP_CIPHER *)OBJ_NAME_get(name, OBJ_NAME_TYPE_CIPHER_METH); -} - -const EVP_MD * -EVP_get_digestbyname(const char *name) -{ - if (!OPENSSL_init_crypto(0, NULL)) - return NULL; - - return (const EVP_MD *)OBJ_NAME_get(name, OBJ_NAME_TYPE_MD_METH); -} - void EVP_cleanup(void) { -- cgit v1.2.3-55-g6feb