]> git.ipfire.org Git - thirdparty/openssl.git/commitdiff
de-duplicate fnv1a_hash
authorNeil Horman <nhorman@openssl.org>
Sat, 1 Feb 2025 16:42:22 +0000 (11:42 -0500)
committerNeil Horman <nhorman@openssl.org>
Mon, 17 Feb 2025 16:27:34 +0000 (11:27 -0500)
I cloned a copy of fnv1a_hash from hashtable.c.  Deduplicate that so we
have common source code.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Saša Nedvědický <sashan@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/26517)

crypto/hashtable/build.info
crypto/hashtable/hashfunc.c [new file with mode: 0644]
crypto/hashtable/hashtable.c
include/internal/hashfunc.h [new file with mode: 0644]
ssl/build.info
ssl/quic/quic_impl.c

index 514fcff6cf1787a286e26966aad79bc20b061898..b13615d5ab71920f37231a6c1c3c6134dde07638 100644 (file)
@@ -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 (file)
index 0000000..9f3226c
--- /dev/null
@@ -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;
+}
index 8d7f4751b2e2248d070c46a004a5fb5a5f55f25a..9203a2809229a9a7abaed7ca72c7429a773536df 100644 (file)
@@ -53,6 +53,7 @@
 #include <string.h>
 #include <internal/rcu.h>
 #include <internal/hashtable.h>
+#include <internal/hashfunc.h>
 #include <openssl/rand.h>
 
 /*
 # 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 (file)
index 0000000..2423fc8
--- /dev/null
@@ -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 <openssl/e_os2.h>
+/**
+ * Generalized fnv1a 64 bit hash function
+ */
+ossl_unused uint64_t fnv1a_hash(uint8_t *key, size_t len);
+
+#endif
index 3443bf2818ae8329be02d7b08b00fbdcb295e7f1..e4b2aa8f1303eb6739b185067b9c679aa67ea2c4 100644 (file)
@@ -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
index 5a73cfe486286100dc696cde5b40d770bf46ef3d..d37ddc39c56b93457892aebcba76a4320e0812e9 100644 (file)
@@ -12,6 +12,7 @@
 #include <openssl/sslerr.h>
 #include <crypto/rand.h>
 #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)