From 35fb39da76a802f068f24b418555d39700f6068c Mon Sep 17 00:00:00 2001 From: Neil Horman Date: Sat, 1 Feb 2025 11:42:22 -0500 Subject: [PATCH] de-duplicate fnv1a_hash MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit I cloned a copy of fnv1a_hash from hashtable.c. Deduplicate that so we have common source code. Reviewed-by: Matt Caswell Reviewed-by: Saša Nedvědický (Merged from https://github.com/openssl/openssl/pull/26517) --- crypto/hashtable/build.info | 2 +- crypto/hashtable/hashfunc.c | 23 +++++++++++++++++++++++ crypto/hashtable/hashtable.c | 13 +------------ include/internal/hashfunc.h | 19 +++++++++++++++++++ ssl/build.info | 3 ++- ssl/quic/quic_impl.c | 15 ++------------- 6 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 crypto/hashtable/hashfunc.c create mode 100644 include/internal/hashfunc.h diff --git a/crypto/hashtable/build.info b/crypto/hashtable/build.info index 514fcff6cf1..b13615d5ab7 100644 --- a/crypto/hashtable/build.info +++ b/crypto/hashtable/build.info @@ -1,5 +1,5 @@ LIBS=../../libcrypto -$COMMON=hashtable.c +$COMMON=hashtable.c hashfunc.c SOURCE[../../libcrypto]=$COMMON SOURCE[../../providers/libfips.a]=$COMMON diff --git a/crypto/hashtable/hashfunc.c b/crypto/hashtable/hashfunc.c new file mode 100644 index 00000000000..9f3226c9ea7 --- /dev/null +++ b/crypto/hashtable/hashfunc.c @@ -0,0 +1,23 @@ +/* + * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + * + */ + +#include "internal/hashfunc.h" + +ossl_unused uint64_t fnv1a_hash(uint8_t *key, size_t len) +{ + uint64_t hash = 0xcbf29ce484222325ULL; + size_t i; + + for (i = 0; i < len; i++) { + hash ^= key[i]; + hash *= 0x00000100000001B3ULL; + } + return hash; +} diff --git a/crypto/hashtable/hashtable.c b/crypto/hashtable/hashtable.c index 8d7f4751b2e..9203a280922 100644 --- a/crypto/hashtable/hashtable.c +++ b/crypto/hashtable/hashtable.c @@ -53,6 +53,7 @@ #include #include #include +#include #include /* @@ -86,18 +87,6 @@ # define PREFETCH(x) #endif -static ossl_unused uint64_t fnv1a_hash(uint8_t *key, size_t len) -{ - uint64_t hash = 0xcbf29ce484222325ULL; - size_t i; - - for (i = 0; i < len; i++) { - hash ^= key[i]; - hash *= 0x00000100000001B3ULL; - } - return hash; -} - /* * Define our neighborhood list length * Note: It should always be a power of 2 diff --git a/include/internal/hashfunc.h b/include/internal/hashfunc.h new file mode 100644 index 00000000000..2423fc81bcd --- /dev/null +++ b/include/internal/hashfunc.h @@ -0,0 +1,19 @@ +/* + * Copyright 2025 The OpenSSL Project Authors. All Rights Reserved. + * + * Licensed under the Apache License 2.0 (the "License"). You may not use + * this file except in compliance with the License. You can obtain a copy + * in the file LICENSE in the source distribution or at + * https://www.openssl.org/source/license.html + */ + +#ifndef OPENSSL_HASHFUNC_H +# define OPENSSL_HASHFUNC_H + +# include +/** + * Generalized fnv1a 64 bit hash function + */ +ossl_unused uint64_t fnv1a_hash(uint8_t *key, size_t len); + +#endif diff --git a/ssl/build.info b/ssl/build.info index 3443bf2818a..e4b2aa8f130 100644 --- a/ssl/build.info +++ b/ssl/build.info @@ -21,7 +21,8 @@ SOURCE[../libssl]=\ # For shared builds we need to include the libcrypto packet.c and quic_vlint.c # in libssl as well. SHARED_SOURCE[../libssl]=\ - ../crypto/packet.c ../crypto/quic_vlint.c ../crypto/time.c + ../crypto/packet.c ../crypto/quic_vlint.c ../crypto/time.c \ + ../crypto/hashtable/hashfunc.c IF[{- !$disabled{'deprecated-3.0'} -}] SOURCE[../libssl]=ssl_rsa_legacy.c diff --git a/ssl/quic/quic_impl.c b/ssl/quic/quic_impl.c index 5a73cfe4862..d37ddc39c56 100644 --- a/ssl/quic/quic_impl.c +++ b/ssl/quic/quic_impl.c @@ -12,6 +12,7 @@ #include #include #include "quic_local.h" +#include "internal/hashfunc.h" #include "internal/ssl_unwrap.h" #include "internal/quic_tls.h" #include "internal/quic_rx_depack.h" @@ -4641,21 +4642,9 @@ struct ssl_token_store_st { CRYPTO_MUTEX *mutex; }; -static uint64_t fnv1a_hash_token(uint8_t *key, size_t len) -{ - uint64_t hash = 0xcbf29ce484222325ULL; - size_t i; - - for (i = 0; i < len; i++) { - hash ^= key[i]; - hash *= 0x00000100000001B3ULL; - } - return hash; -} - static unsigned long quic_token_hash(const QUIC_TOKEN *item) { - return (unsigned long)fnv1a_hash_token(item->hashkey, item->hashkey_len); + return (unsigned long)fnv1a_hash(item->hashkey, item->hashkey_len); } static int quic_token_cmp(const QUIC_TOKEN *a, const QUIC_TOKEN *b) -- 2.47.2