-/* $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>
*
#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
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();
+}
-/* $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.
*
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)
{