diff options
author | Theo Buehler <tb@cvs.openbsd.org> | 2024-01-13 11:12:33 +0000 |
---|---|---|
committer | Theo Buehler <tb@cvs.openbsd.org> | 2024-01-13 11:12:33 +0000 |
commit | de344fe231d50379527e0ae8cde1e8381e62f30b (patch) | |
tree | 07cd2bf8897e8ac67b1e7c8dc49a20ff92147ad1 | |
parent | e318456942d637cf9d390fb56d43b87e73d54956 (diff) |
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
-rw-r--r-- | lib/libcrypto/evp/evp_names.c | 47 | ||||
-rw-r--r-- | lib/libcrypto/evp/names.c | 20 |
2 files changed, 47 insertions, 20 deletions
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 <tb@openbsd.org> * @@ -18,6 +18,9 @@ #include <openssl/evp.h> #include <openssl/objects.h> +#include <stdlib.h> +#include <string.h> + /* * 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) { |