diff --git a/ChangeLog b/ChangeLog index cf860feebe17a78e05781440cf8ba59b114cd751..29b04c016ba9993b56bdf026e463c60aa9b48c8d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,15 @@ +2017-01-12 Niels Möller <nisse@lysator.liu.se> + + * nettle-meta.h (nettle_hashes): New macro, expanding to a call to + nettle_get_hashes. Direct access to the array causes the array + size to leak into the ABI, since a plain un-relocatable executable + linking with libnettle.so gets copy relocations for any referenced + data items in the shared library. + + * nettle-meta-hashes.c (_nettle_hashes): Renamed array, from... + (nettle_hashes): ... old name. + (nettle_get_hashes): New function. + 2016-10-10 Niels Möller <nisse@lysator.liu.se> * write-be32.c (_nettle_write_be32): Use const for source argument. diff --git a/Makefile.in b/Makefile.in index 135542f588df50170e29c978dc7d94cc89c75484..5288662290bffb45b120136146d0a0a5b8aa219d 100644 --- a/Makefile.in +++ b/Makefile.in @@ -110,6 +110,7 @@ nettle_SOURCES = aes-decrypt-internal.c aes-decrypt.c \ md2.c md2-meta.c md4.c md4-meta.c \ md5.c md5-compress.c md5-compat.c md5-meta.c \ memeql-sec.c memxor.c memxor3.c \ + nettle-lookup-hash.c \ nettle-meta-aeads.c nettle-meta-armors.c \ nettle-meta-ciphers.c nettle-meta-hashes.c \ pbkdf2.c pbkdf2-hmac-sha1.c pbkdf2-hmac-sha256.c \ diff --git a/nettle-lookup-hash.c b/nettle-lookup-hash.c new file mode 100644 index 0000000000000000000000000000000000000000..adf9188b62dafd5396b87f0b258d0a8019e8fc94 --- /dev/null +++ b/nettle-lookup-hash.c @@ -0,0 +1,53 @@ +/* nettle-lookup-hash.c + + Copyright (C) 2016 Niels Möller. + + This file is part of GNU Nettle. + + GNU Nettle is free software: you can redistribute it and/or + modify it under the terms of either: + + * the GNU Lesser General Public License as published by the Free + Software Foundation; either version 3 of the License, or (at your + option) any later version. + + or + + * the GNU General Public License as published by the Free + Software Foundation; either version 2 of the License, or (at your + option) any later version. + + or both in parallel, as here. + + GNU Nettle is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received copies of the GNU General Public License and + the GNU Lesser General Public License along with this program. If + not, see http://www.gnu.org/licenses/. +*/ + +#if HAVE_CONFIG_H +# include "config.h" +#endif + +#include <stddef.h> +#include <string.h> + +#include "nettle-meta.h" + +/* Direct access to the array. */ +#undef nettle_hashes +#define nettle_hashes _nettle_hashes + +const struct nettle_hash * +nettle_lookup_hash (const char *name) +{ + unsigned i; + for (i = 0; nettle_hashes[i]; i++) + if (!strcmp (name, nettle_hashes[i]->name)) + return nettle_hashes[i]; + return NULL; +} diff --git a/nettle-meta-hashes.c b/nettle-meta-hashes.c index 2220968c070b41e2e27cfd9d735f693656543e55..37552edec5caca37a8bd43e7e4734205cb78e7eb 100644 --- a/nettle-meta-hashes.c +++ b/nettle-meta-hashes.c @@ -34,9 +34,10 @@ #endif #include <stddef.h> + #include "nettle-meta.h" -const struct nettle_hash * const nettle_hashes[] = { +const struct nettle_hash * const _nettle_hashes[] = { &nettle_md2, &nettle_md4, &nettle_md5, @@ -52,3 +53,9 @@ const struct nettle_hash * const nettle_hashes[] = { &nettle_sha3_512, NULL }; + +const struct nettle_hash * const * +nettle_get_hashes (void) +{ + return _nettle_hashes; +} diff --git a/nettle-meta.h b/nettle-meta.h index 14b5e48ed0c166c1fa30059f5c1235c69127c4d4..fd1667172c7602d1b309dde4669282caac5b3423 100644 --- a/nettle-meta.h +++ b/nettle-meta.h @@ -115,7 +115,18 @@ struct nettle_hash } /* null-terminated list of digests implemented by this version of nettle */ -extern const struct nettle_hash * const nettle_hashes[]; +extern const struct nettle_hash * const _nettle_hashes[]; + +const struct nettle_hash * const * +#ifdef __GNUC__ +__attribute__((pure)) +#endif +nettle_get_hashes (void); + +#define nettle_hashes (nettle_get_hashes()) + +const struct nettle_hash * +nettle_lookup_hash (const char *name); extern const struct nettle_hash nettle_md2; extern const struct nettle_hash nettle_md4;