From: tb Date: Sat, 13 Jan 2024 11:12:32 +0000 (+0000) Subject: Reimplement EVP_get_{cipher,digest}byname() X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=61d548172637ec71422a7613ccb642eaa1bd8230;p=openbsd 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 --- diff --git a/lib/libcrypto/evp/evp_names.c b/lib/libcrypto/evp/evp_names.c index b5d3b95b453..660e23f66e5 100644 --- a/lib/libcrypto/evp/evp_names.c +++ b/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/lib/libcrypto/evp/names.c b/lib/libcrypto/evp/names.c index 7f5b4550885..8cc6d04ce38 100644 --- a/lib/libcrypto/evp/names.c +++ b/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) {