From: "plaisthos (Code Review)" Date: Wed, 11 Mar 2026 18:51:13 +0000 References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net Message-ID: <71c9f79b3f7ed83f6bb3d8898aaf6af179e44f61-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="G29gGWkJOJ0="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Replace custom hash_func with siphash List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: openvpn-devel --G29gGWkJOJ0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable plaisthos has uploaded this change for review. ( http://gerrit.openvpn.net/= c/openvpn/+/1573?usp=3Demail ) Change subject: Replace custom hash_func with siphash ...................................................................... Replace custom hash_func with siphash Change-Id: I807f398903ac2047530800c29949793c6f4f0ec9 Signed-off-by: Arne Schwabe --- M CMakeLists.txt M src/openvpn/Makefile.am M src/openvpn/list.c M src/openvpn/list.h M src/openvpn/mroute.c M src/openvpn/multi.c A src/openvpn/siphash.c M src/openvpn/siphash.h M tests/unit_tests/openvpn/Makefile.am M tests/unit_tests/openvpn/test_misc.c 10 files changed, 140 insertions(+), 208 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/73/1573/1 diff --git a/CMakeLists.txt b/CMakeLists.txt index 8eb05f6..cd7bafa 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -557,6 +557,7 @@ src/openvpn/sig.c src/openvpn/sig.h src/openvpn/siphash.h + src/openvpn/siphash.c src/openvpn/siphash_openssl.c src/openvpn/siphash_reference.c src/openvpn/socket.c @@ -821,7 +822,11 @@ src/openvpn/options_util.c src/openvpn/ssl_util.c src/openvpn/list.c - ) + src/openvpn/siphash.h + src/openvpn/siphash.c + src/openvpn/siphash_reference.c + src/openvpn/siphash_openssl.c + ) target_sources(test_ncp PRIVATE src/openvpn/crypto_epoch.c diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index 08c8250..4cb93e9 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -128,7 +128,8 @@ session_id.c session_id.h \ shaper.c shaper.h \ sig.c sig.h \ - siphash_reference.c siphash.h \ + siphash.c siphash.h \ + siphash_reference.c \ siphash_openssl.c \ socket.c socket.h \ socket_util.c socket_util.h \ diff --git a/src/openvpn/list.c b/src/openvpn/list.c index 36d8ede..1a7dd25 100644 --- a/src/openvpn/list.c +++ b/src/openvpn/list.c @@ -31,9 +31,6 @@ #include "list.h" #include "crypto.h" -#include "misc.h" - -#include "memdbg.h" struct hash * hash_init(const uint32_t n_buckets, void *ctx, @@ -315,187 +312,4 @@ ASSERT(hi->last); hi->last->key =3D NULL; hi->bucket_marked =3D true; -} - - -/* - * -------------------------------------------------------------------- - * hash() -- hash a variable-length key into a 32-bit value - * k : the key (the unaligned variable-length array of bytes) - * len : the length of the key, counting by bytes - * level : can be any 4-byte value - * Returns a 32-bit value. Every bit of the key affects every bit of - * the return value. Every 1-bit and 2-bit delta achieves avalanche. - * About 36+6len instructions. - * - * #define hashsize(n) ((uint32_t)1<<(n)) - * #define hashmask(n) (hashsize(n)-1) - * - * The best hash table sizes are powers of 2. There is no need to do - * mod a prime (mod is sooo slow!). If you need less than 32 bits, - * use a bitmask. For example, if you need only 10 bits, do - * h =3D (h & hashmask(10)); - * In which case, the hash table should have hashsize(10) elements. - * - * If you are hashing n strings (uint8_t **)k, do it like this: - * for (i=3D0, h=3D0; i>13); - * b -=3D c; a ^=3D x; - * b -=3D a; x =3D (a<<8); - * c -=3D a; b ^=3D x; - * c -=3D b; x =3D (b>>13); - * ... - * Unfortunately, superscalar Pentiums and Sparcs can't take advantage - * of that parallelism. They've also turned some of those single-cycle - * latency instructions into multi-cycle latency instructions. Still, - * this is the fastest good hash I could find. There were about 2^^68 - * to choose from. I only looked at a billion or so. - * - * James Yonan Notes: - * - * This function is faster than it looks, and appears to be - * appropriate for our usage in OpenVPN which is primarily - * for hash-table based address lookup (IPv4, IPv6, and Ethernet MAC). - * NOTE: This function is never used for cryptographic purposes, only - * to produce evenly-distributed indexes into hash tables. - * - * Benchmark results: 11.39 machine cycles per byte on a P2 266Mhz, - * and 12.1 machine cycles per byte on a - * 2.2 Ghz P4 when hashing a 6 byte string. - * -------------------------------------------------------------------- - */ - -#define mix(a, b, c) \ - { \ - a -=3D b; \ - a -=3D c; \ - a ^=3D (c >> 13); \ - b -=3D c; \ - b -=3D a; \ - b ^=3D (a << 8); \ - c -=3D a; \ - c -=3D b; \ - c ^=3D (b >> 13); \ - a -=3D b; \ - a -=3D c; \ - a ^=3D (c >> 12); \ - b -=3D c; \ - b -=3D a; \ - b ^=3D (a << 16); \ - c -=3D a; \ - c -=3D b; \ - c ^=3D (b >> 5); \ - a -=3D b; \ - a -=3D c; \ - a ^=3D (c >> 3); \ - b -=3D c; \ - b -=3D a; \ - b ^=3D (a << 10); \ - c -=3D a; \ - c -=3D b; \ - c ^=3D (b >> 15); \ - } - -void * -hash_func_new_ctx(void) -{ - uint32_t *ctx =3D malloc(sizeof(uint32_t)); - *ctx =3D (uint32_t)get_random(); - return ctx; -} - -uint64_t -hash_func(const uint8_t *k, uint32_t length, uint32_t initval) -{ - uint32_t a, b, c, len; - - /* Set up the internal state */ - len =3D length; - a =3D b =3D 0x9e3779b9; /* the golden ratio; an arbitrary value */ - c =3D initval; /* the previous hash value */ - - /*---------------------------------------- handle most of the key */ - while (len >=3D 12) - { - a +=3D (k[0] + ((uint32_t)k[1] << 8) + ((uint32_t)k[2] << 16) + ((= uint32_t)k[3] << 24)); - b +=3D (k[4] + ((uint32_t)k[5] << 8) + ((uint32_t)k[6] << 16) + ((= uint32_t)k[7] << 24)); - c +=3D (k[8] + ((uint32_t)k[9] << 8) + ((uint32_t)k[10] << 16) + (= (uint32_t)k[11] << 24)); - mix(a, b, c); - k +=3D 12; - len -=3D 12; - } - - /*------------------------------------- handle the last 11 bytes */ - c +=3D length; - switch (len) /* all the case statements fall through */ - { - case 11: - c +=3D ((uint32_t)k[10] << 24); - /* Intentional [[fallthrough]]; */ - - case 10: - c +=3D ((uint32_t)k[9] << 16); - /* Intentional [[fallthrough]]; */ - - case 9: - c +=3D ((uint32_t)k[8] << 8); - /* Intentional [[fallthrough]]; */ - - /* the first byte of c is reserved for the length */ - case 8: - b +=3D ((uint32_t)k[7] << 24); - /* Intentional [[fallthrough]]; */ - - case 7: - b +=3D ((uint32_t)k[6] << 16); - /* Intentional [[fallthrough]]; */ - - case 6: - b +=3D ((uint32_t)k[5] << 8); - /* Intentional [[fallthrough]]; */ - - case 5: - b +=3D k[4]; - /* Intentional [[fallthrough]]; */ - - case 4: - a +=3D ((uint32_t)k[3] << 24); - /* Intentional [[fallthrough]]; */ - - case 3: - a +=3D ((uint32_t)k[2] << 16); - /* Intentional [[fallthrough]]; */ - - case 2: - a +=3D ((uint32_t)k[1] << 8); - /* Intentional [[fallthrough]]; */ - - case 1: - a +=3D k[0]; - /* case 0: nothing left to add */ - } - mix(a, b, c); - /*-------------------------------------- report the result */ - return c; -} +} \ No newline at end of file diff --git a/src/openvpn/list.h b/src/openvpn/list.h index ea6f47a..160bb5d 100644 --- a/src/openvpn/list.h +++ b/src/openvpn/list.h @@ -101,10 +101,6 @@ void hash_iterator_free(struct hash_iterator *hi); -uint64_t hash_func(const uint8_t *k, uint32_t length, uint32_t initval); - -void *hash_func_new_ctx(void); - static inline uint64_t hash_value(const struct hash *hash, const void *key) { diff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c index 89b3ecc..e32894b 100644 --- a/src/openvpn/mroute.c +++ b/src/openvpn/mroute.c @@ -33,6 +33,7 @@ #include "socket_util.h" #include "memdbg.h" +#include "siphash.h" void mroute_addr_init(struct mroute_addr *addr) @@ -357,9 +358,8 @@ uint64_t mroute_addr_hash_function(const void *key, void *ctx) { - uint32_t *iv =3D ctx; - return hash_func(mroute_addr_hash_ptr((const struct mroute_addr *)key), - mroute_addr_hash_len((const struct mroute_addr *)key)= , *iv); + return siphash_hash_func(mroute_addr_hash_ptr((const struct mroute_add= r *)key), + mroute_addr_hash_len((const struct mroute_add= r *)key), ctx); } bool diff --git a/src/openvpn/multi.c b/src/openvpn/multi.c index 200e1e7..920ae7d 100644 --- a/src/openvpn/multi.c +++ b/src/openvpn/multi.c @@ -23,6 +23,7 @@ #ifdef HAVE_CONFIG_H #include "config.h" #endif +#include "siphash.h" #ifdef HAVE_SYS_INOTIFY_H #include @@ -290,14 +291,14 @@ * to determine which client sent an incoming packet * which is seen on the TCP/UDP socket. */ - m->hash =3D hash_init(t->options.real_hash_size, hash_func_new_ctx(), = free, + m->hash =3D hash_init(t->options.real_hash_size, siphash_new_context()= , siphash_free_context, mroute_addr_hash_function, mroute_addr_compare_fun= ction); /* * Virtual address hash table. Used to determine * which client to route a packet to. */ - m->vhash =3D hash_init(t->options.virtual_hash_size, hash_func_new_ctx= (), free, + m->vhash =3D hash_init(t->options.virtual_hash_size, siphash_new_conte= xt(), siphash_free_context, mroute_addr_hash_function, mroute_addr_compare_fu= nction); #ifdef ENABLE_MANAGEMENT diff --git a/src/openvpn/siphash.c b/src/openvpn/siphash.c new file mode 100644 index 0000000..dac7750 --- /dev/null +++ b/src/openvpn/siphash.c @@ -0,0 +1,77 @@ +/* + * OpenVPN -- An application to securely tunnel IP networks + * over a single TCP/UDP port, with support for SSL/TLS-based + * session authentication and key exchange, + * packet encryption, packet authentication, and + * packet compression. + * + * Copyright (C) 2025 OpenVPN Inc + * Copyright (C) 2025 Arne Schwabe + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 + * as published by the Free Software Foundation. + * + * This program 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 a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#elif defined(_MSC_VER) +#include "config-msvc.h" +#endif + +#include +#include "syshead.h" +#include "siphash.h" +#include "buffer.h" +#include "crypto.h" + +void * +siphash_new_context(void) +{ + struct siphash_ctx *ctx; + ALLOC_OBJ_CLEAR(ctx, struct siphash_ctx); + + prng_bytes(ctx->key, SIPHASH_KEY_SIZE); + + if (siphash_cryptolib_available(ctx) && false) + { + ctx->cryptlib_ctx =3D siphash_cryptolib_init(8); + } + return ctx; +} + +void +siphash_free_context(void *ctx) +{ + struct siphash_ctx *sctx =3D ctx; + + if (sctx->cryptlib_ctx) + { + siphash_cryptolib_uninit(sctx->cryptlib_ctx); + } + free(ctx); +} + +uint64_t +siphash_hash_func(const uint8_t *k, uint32_t length, void *ctx) +{ + struct siphash_ctx *sctx =3D ctx; + + union + { + uint8_t out[8]; + uint64_t hash; + } ret; + siphash(sctx, k, length, ret.out, sizeof(ret.out)); + return ret.hash; +} \ No newline at end of file diff --git a/src/openvpn/siphash.h b/src/openvpn/siphash.h index 973596b..739c43c 100644 --- a/src/openvpn/siphash.h +++ b/src/openvpn/siphash.h @@ -25,8 +25,15 @@ /* siphash always uses 128-bit keys */ #define SIPHASH_KEY_SIZE 16 +struct siphash_ctx +{ + uint8_t key[SIPHASH_KEY_SIZE]; + void *cryptlib_ctx; +}; + /* Prototypes for an implementation of SIPHASH in a crypto library */ + /** * Calculates SIPHASH using the crypto library function. */ @@ -56,17 +63,44 @@ siphash_cryptolib_available(void *sip_context); static inline int -siphash(void *ctx, const void *in, size_t inlen, const void *k, - uint8_t *out, size_t outlen) +siphash(void *ctx, const void *in, size_t inlen, uint8_t *out, size_t outl= en) { - if (siphash_cryptolib_available(ctx) && false) + struct siphash_ctx *sctx =3D ctx; + if (siphash_cryptolib_available(sctx->cryptlib_ctx) && false) { - return siphash_cryptolib(ctx, in, inlen, k, out, outlen); + return siphash_cryptolib(ctx, in, inlen, sctx->key, out, outlen); } else { - return siphash_reference(in, inlen, k, out, outlen); + return siphash_reference(in, inlen, sctx->key, out, outlen); } } +/** + * Allocates the resources for a siphash context and returns a pointer to + * this context. The context must be freed with siphash_free_context + */ +void * +siphash_new_context(void); + +/** + * Releases the resources assigned to a siphash context + * @param ctx the context previously allocated by siphash_new_context + */ +void +siphash_free_context(void *ctx); + +/** + * Wrapper of the siphash function to easily use it in the + * hash map. + * + * @param k the data to hash + * @param length length of the data to hash + * @param ctx the siphash context + * @return a unit64_t containing the result of the hashing + */ +uint64_t +siphash_hash_func(const uint8_t *k, uint32_t length, void *ctx); + + #endif /* ifndef SIPHASH_H */ \ No newline at end of file diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index 4ed113a..5f7f692 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -85,6 +85,7 @@ $(top_srcdir)/src/openvpn/mtu.c \ $(top_srcdir)/src/openvpn/win32-util.c \ $(top_srcdir)/src/openvpn/mss.c \ + $(top_srcdir)/src/openvpn/siphash.c \ $(top_srcdir)/src/openvpn/siphash_openssl.c \ $(top_srcdir)/src/openvpn/siphash_reference.c @@ -378,7 +379,10 @@ $(top_srcdir)/src/openvpn/ssl_util.c \ $(top_srcdir)/src/openvpn/win32-util.c \ $(top_srcdir)/src/openvpn/platform.c \ - $(top_srcdir)/src/openvpn/list.c + $(top_srcdir)/src/openvpn/list.c \ + $(top_srcdir)/src/openvpn/siphash.c \ + $(top_srcdir)/src/openvpn/siphash_openssl.c \ + $(top_srcdir)/src/openvpn/siphash_reference.c push_update_msg_testdriver_CFLAGS =3D -I$(top_srcdir)/src/openvpn \ -I$(top_srcdir)/src/compat \ diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tests/openvp= n/test_misc.c index 24bf22d..576630d 100644 --- a/tests/unit_tests/openvpn/test_misc.c +++ b/tests/unit_tests/openvpn/test_misc.c @@ -32,6 +32,7 @@ #include #include #include +#include #include "ssl_util.h" #include "options_util.h" @@ -127,10 +128,9 @@ static uint64_t word_hash_function(const void *key, void *ctx) { - uint32_t *iv =3D (uint32_t *)ctx; const char *str =3D (const char *)key; const uint32_t len =3D (uint32_t)strlen(str); - return hash_func((const uint8_t *)str, len, *iv); + return siphash_hash_func((const uint8_t *)str, len, ctx); } static bool @@ -173,8 +173,8 @@ * word frequency algorithm. */ struct gc_arena gc =3D gc_new(); - struct hash *hash =3D hash_init(10000, hash_func_new_ctx(), free, word= _hash_function, word_compare_function); - struct hash *nhash =3D hash_init(256, hash_func_new_ctx(), free, word_= hash_function, word_compare_function); + struct hash *hash =3D hash_init(10000, siphash_new_context(), siphash_= free_context, word_hash_function, word_compare_function); + struct hash *nhash =3D hash_init(256, siphash_new_context(), siphash_f= ree_context, word_hash_function, word_compare_function); printf("hash_init n_buckets=3D%u mask=3D0x%08x\n", hash->n_buckets, ha= sh->mask); -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/1573?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newchange Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I807f398903ac2047530800c29949793c6f4f0ec9 Gerrit-Change-Number: 1573 Gerrit-PatchSet: 1 Gerrit-Owner: plaisthos Gerrit-CC: openvpn-devel --G29gGWkJOJ0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

plaisthos has upl= oaded this change for review.

View Change

Replace custom hash_func with siphash

Change-Id: I807f398903ac20= 47530800c29949793c6f4f0ec9
Signed-off-by: Arne Schwabe <arne@...2639...= ..>
---
M CMakeLists.txt
M src/openvpn/Makefile.am
M src/ope= nvpn/list.c
M src/openvpn/list.h
M src/openvpn/mroute.c
M src/open= vpn/multi.c
A src/openvpn/siphash.c
M src/openvpn/siphash.h
M test= s/unit_tests/openvpn/Makefile.am
M tests/unit_tests/openvpn/test_misc.c<= br>10 files changed, 140 insertions(+), 208 deletions(-)

git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/73/15=
73/1
diff --git a/CMakeLists.txt b/CMakeLists.txt
i= ndex 8eb05f6..cd7bafa 100644
--- a/CMakeLists.txt+++ b/CMakeLists.txt
@@ -557,6 +557,7 @@
<= span> src/openvpn/sig.c
src/openvpn/sig.h src/openvpn/siphash.h
+ src/openvpn/siphash.c
src/openvpn/s= iphash_openssl.c
src/openvpn/siphash_reference.c
src/openvpn/socket.c
@@ -821,7 +822,11 @@
src/openvpn/options_util.c
= src/openvpn/ssl_util.c
src/openvpn/list.c<= br>- )
+ src/openvpn/siphash.h+ src/openvpn/siphash.c=
+ src/openvpn= /siphash_reference.c
= + src/openvpn/siphash_openssl.c
+ )

target_sour= ces(test_ncp PRIVATE
src/openvpn/crypto_epoch.c
diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.a= m
index 08c8250..4cb93e9 100644
--- a/src/o= penvpn/Makefile.am
+++ b/src/openvpn/Makefile.am
= @@ -128,7 +128,8 @@
session_id.c session_id.h \
shaper.c shaper.h \
sig.c sig.h \- siphash_reference.c siphash.h= \
+ siphash.c siphas= h.h \
+ siphash_refer= ence.c \
siphash_openssl.c \
socket.c s= ocket.h \
socket_util.c socket_util.h \
d= iff --git a/src/openvpn/list.c b/src/openvpn/list.c
index 3= 6d8ede..1a7dd25 100644
--- a/src/openvpn/list.c
<= span>+++ b/src/openvpn/list.c

@@ -31,9 +31,6 @@
<= span> #include "list.h"


#inclu= de "crypto.h"
-#include "misc.h"
-
-#include &quo= t;memdbg.h"

struct hash * hash_init(const uint32_t n_buckets, void *ctx,
@@ -= 315,187 +312,4 @@
ASSERT(hi->last);
hi->last->key =3D NULL;

hi->bucket_mark= ed =3D true;
-}<= br>-
-
-/*
- * ------------= --------------------------------------------------------
- * hash() -- hash a variable-length key = into a 32-bit value
- *= k : the key (the unaligned variable-length array of bytes)
<= span style=3D"color: hsl(0, 100%, 40%);">- * len : the length of the key,= counting by bytes

- * = level : can be any 4-byte value
- * Returns a 32-bit value. Every bit of the key affects every bi= t of
- * the return val= ue. Every 1-bit and 2-bit delta achieves avalanche.
- * About 36+6len instructions.
- *
- * #define hashsize(n) ((uint32_t)1<<(n))
- * #define hashmask(n) (has= hsize(n)-1)
- *<= br>- * The best hash table sizes = are powers of 2. There is no need to do
- * mod a prime (mod is sooo slow!). If you need less th= an 32 bits,
- * use a b= itmask. For example, if you need only 10 bits, do
- * h =3D (h & hashmask(10));
- * In which case, the hash table sh= ould have hashsize(10) elements.

- *
- * If yo= u are hashing n strings (uint8_t **)k, do it like this:
- * for (i=3D0, h=3D0; i<n; ++i) h =3D = hash( k[i], len[i], h);
- *
- * By Bob Jenkins= , 1996. bob_jenkins@...2942... You may use this
- * code any way you wish, private, educational,= or commercial. It's free.
- *
- * See ht= tps://burtleburtle.net/bob/hash/evahash.html
- * Use for hash table lookup, or anything where one = collision in 2^32 is
- = * acceptable. Do NOT use for cryptographic purposes.
- *
- * -----------------------------------------------------------= ---------
- *- * mix -- mix 3 32-bit values r= eversibly.
- * For ever= y delta with one or two bit set, and the deltas of all three
- * high bits or all three low bits, = whether the original value of a,b,c
- * is almost all zero or is uniformly distributed,
= - * If mix() is run forward or ba= ckward, at least 32 bits in a,b,c
- * have at least 1/4 probability of changing.
- * If mix() is run forward, every bit o= f c will change between 1/3 and
- * 2/3 of the time. (Well, 22/100 and 78/100 for some 2-bit delt= as.)
- * mix() was buil= t out of 36 single-cycle latency instructions in a
- * structure that could supported 2x paralleli= sm, like so:
- * a -= =3D b;
- * a -=3D c;= x =3D (c>>13);
-= * b -=3D c; a ^=3D x;
- * b -=3D a; x =3D (a<<8);
- * c -=3D a; b ^=3D x;
- * c -=3D b; x =3D (b>>13);
- * ...
- * Unfortunately, superscalar Pentiums and Sparcs = can't take advantage
- * latency instruc= tions into multi-cycle latency instructions. Still,
- * this is the fastest good hash I could fi= nd. There were about 2^^68
- * to choose from. I only looked at a billion or so.
- *
- * James Yonan Notes:
- *
-= * This function is faster than it looks, and appears to be
- * appropriate for our usage in OpenV= PN which is primarily
-= * for hash-table based address lookup (IPv4, IPv6, and Ethernet MAC).

- * NOTE: This function is = never used for cryptographic purposes, only
- * to produce evenly-distributed indexes into hash ta= bles.
- *
- * Benchmark results: 11.39 machine= cycles per byte on a P2 266Mhz,
- * and 12.1 machine cycles per byte on a
- * 2.2 Gh= z P4 when hashing a 6 byte string.
- * -----------------------------------------------------------= ---------
- */-
-#define mix(a, b, c) \
- { \
- a -=3D b; \
- a -=3D c; \
- a ^=3D (c >> 13);= \
- b -=3D c; = \
- b -= =3D a; \
- = b ^=3D (a << 8); \
- c -=3D a; \
- c -=3D b; \
- c ^=3D (b >> 13); \
- a -=3D b; \
- a -=3D c; \- a ^=3D (c >> 12);= \
- b -=3D c; = \
- b -= =3D a; \
- = b ^=3D (a << 16); \
- c -=3D a; \
- c -=3D b; \
- c ^=3D (b >> 5); \
- a -=3D b; \
- a -=3D c; \- a ^=3D (c >> 3); = \
- b -=3D c; = \
- b -= =3D a; \
- = b ^=3D (a << 10); \
- c -=3D a; \
- c -=3D b; \
- c ^=3D (b >> 15); \
- }
-
-void = *
-hash_func_new_ctx(vo= id)
-{
- uint32_t *ctx =3D malloc(sizeof(ui= nt32_t));
- *ctx =3D= (uint32_t)get_random();
-}
-
-uint64_t
-hash_func(const uint8_t *k, uint32_t length, uint32_t in= itval)
-{
- uint32_t a, b, c, len;
-
- /* Set up the internal state */
- len =3D length;
- a =3D b =3D 0x9e3779b9; /* the gold= en ratio; an arbitrary value */
- c =3D initval; /* the previous hash value */-
- /*---------------------------------------- handl= e most of the key */
- = while (len >=3D 12)
- {
- a += =3D (k[0] + ((uint32_t)k[1] << 8) + ((uint32_t)k[2] << 16) + ((= uint32_t)k[3] << 24));
- b +=3D (k[4] + ((uint32_t)k[5] << 8) + ((uint32_t)k[6]= << 16) + ((uint32_t)k[7] << 24));
- c +=3D (k[8] + ((uint32_t)k[9] << 8)= + ((uint32_t)k[10] << 16) + ((uint32_t)k[11] << 24));- mix(a, b, c);- k +=3D 12;
<= span style=3D"color: hsl(0, 100%, 40%);">- len -=3D 12;
- }
-
- /*------------------------------------- handle the last 11 bytes */=
- c +=3D length;

- switch (len) /* all = the case statements fall through */
- {
- = case 11:
- = c +=3D ((uint32_t)k[10] << 24);
- /* Intentional [[fallthrough]]; */<= br>-
- case 10:
- c +=3D ((uint32_t)k[9] << 16);=
- /* Intentional [= [fallthrough]]; */
-

- case 9:- c +=3D ((uint32_t)= k[8] << 8);
- = /* Intentional [[fallthrough]]; */
-
- = /* the first byte of c is reserved for the length */
- case 8:
- b +=3D ((uint32_t)k[7] << 24= );
- /* Inte= ntional [[fallthrough]]; */
-
- case 7:=
- b +=3D ((= uint32_t)k[6] << 16);
- /* Intentional [[fallthrough]]; */
-
- case 6:
- b +=3D ((uint32_t)k[5] << 8);
- /* Intentional [[fallthrough]];= */
-
- case 5:
- b +=3D k[4];
- /* Intentional [[fallthrough= ]]; */
-
- case 4:
- a +=3D ((uint32_t)k[3] <<= ; 24);
- /* = Intentional [[fallthrough]]; */
-
- cas= e 3:
- a += =3D ((uint32_t)k[2] << 16);
- /* Intentional [[fallthrough]]; */
-
- case 2:
- a +=3D ((uint32_t)k[1] << 8);
- /* Intentional [[fallthroug= h]]; */
-
- case 1:
- a +=3D k[0];
- /* case 0: nothing left to= add */
- }- mix(a, b, c);
- /*----------------------------= ---------- report the result */
- return c;
-}
+}
\ No newline at end of file
diff --git a/src/openvpn/list= .h b/src/openvpn/list.h
index ea6f47a..160bb5d 100644
--- a/src/openvpn/list.h
+++ b/src/openvpn/list.= h
@@ -101,10 +101,6 @@

v= oid hash_iterator_free(struct hash_iterator *hi);
<= br>-uint64_t hash_func(const uint= 8_t *k, uint32_t length, uint32_t initval);
-
= -void *hash_func_new_ctx(void);
-
static inline uint64_t
hash_va= lue(const struct hash *hash, const void *key)
{
= diff --git a/src/openvpn/mroute.c b/src/openvpn/mroute.c
index 89b3ecc..e32894b 100644

--- a/src/openvpn/mroute.= c
+++ b/src/openvpn/mroute.c
@@ -33,6 +33,7= @@
#include "socket_util.h"

#include "memdbg.h"
+#include "siphash.h"
<= /span>
void
mroute_addr_init(struct mroute_addr = *addr)
@@ -357,9 +358,8 @@
uint64_t=
mroute_addr_hash_function(const void *key, void *ctx)
= {
- uint32_t = *iv =3D ctx;
- retur= n hash_func(mroute_addr_hash_ptr((const struct mroute_addr *)key),- mroute_ad= dr_hash_len((const struct mroute_addr *)key), *iv);
+ return siphash_hash_func(mroute_addr_= hash_ptr((const struct mroute_addr *)key),
+ mroute_addr_hash_len((c= onst struct mroute_addr *)key), ctx);
}
<= /span>
bool
diff --git a/src/openvpn/multi.c b/sr= c/openvpn/multi.c
index 200e1e7..920ae7d 100644
<= span>--- a/src/openvpn/multi.c

+++ b/src/openvpn/multi.c

@@ -23,6 +23,7 @@
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif+#include "siphash.h"= ;

#ifdef HAVE_SYS_INOTIFY_H
#include <sys/inotify.h>

@@ -290,14 +291,14 @@
* to determine which client sent an incoming packet
* which is seen on the TCP/UDP socket.
= */
- m->has= h =3D hash_init(t->options.real_hash_size, hash_func_new_ctx(), free,
+ m->hash =3D has= h_init(t->options.real_hash_size, siphash_new_context(), siphash_free_co= ntext,
mroute_addr_hash_function, = mroute_addr_compare_function);

/*
* Virtual address hash table. Used to determine=
* which client to route a packet to.
= */
- m->vhash =3D= hash_init(t->options.virtual_hash_size, hash_func_new_ctx(), free,

+ m->vhash =3D hash= _init(t->options.virtual_hash_size, siphash_new_context(), siphash_free_= context,
mroute_addr_hash_functio= n, mroute_addr_compare_function);

#ifdef= ENABLE_MANAGEMENT
diff --git a/src/openvpn/siphash.c b/src= /openvpn/siphash.c
new file mode 100644
ind= ex 0000000..dac7750
--- /dev/null
+++ b/src= /openvpn/siphash.c
@@ -0,0 +1,77 @@
+/*
+ * OpenVPN -- An application to securely tunnel IP networ= ks
+ * ov= er a single TCP/UDP port, with support for SSL/TLS-based
+ * session authentication = and key exchange,
+ *= packet encryption, packet authentication, and
+ * packet compression.
+ *
+ * Copyright (C) 2025 OpenVPN Inc <= ;sales@...515...>
= + * Copyright (C) 2025 Arne Schwabe <arne@...1227...>
+ *

+ *
+ * This program is free software; you can redistribute it and/or mo= dify
+ * it under th= e terms of the GNU General Public License version 2
+ * as published by the Free Software Fou= ndation.
+ *+ * This program is distribu= ted in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied = warranty of
+ * MERC= HANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for mo= re details.
+ *
+ * You should have recei= ved a copy of the GNU General Public License along
+ * with this program; if not, write to the = Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.=
+ */
+

+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#elif defined(_MSC_VER)
+#include "config-msvc.h"
+#endif
+

+#include <stdlib.h>
+#include "syshead.h"
+#include "siphash.h"=
+#include "buffer.h&qu= ot;
+#include "c= rypto.h"
+
+void *
+siphash_new_context(void)
+{

+ struct siphash_ctx *ctx;
+ ALLOC_OBJ_CLEAR(ctx, struct siphash_c= tx);
+
+ prng_bytes(ctx->key, SIPHAS= H_KEY_SIZE);
+=
+ if (siphash_cryptolib_= available(ctx) && false)
+ {
+ = ctx->cryptlib_ctx =3D siphash_cryptolib_init(8);
+ }
+ return ctx;
+}
+void
<= span style=3D"color: hsl(120, 100%, 40%);">+siphash_free_context(void *ctx)=
+{
+ struct siphash_ctx *sctx =3D ctx;=
+
+ if (sctx->cryptlib_ctx)<= br>+ {
+ siphash_cryptolib_uninit(sctx->= ;cryptlib_ctx);
+ = }
+ free(ctx);
+}
+
+uint64_t
+siphash_hash_func(const uint8_t *k, uint32_t length, void *ctx)<= br>+{
+ struct siphash_ctx *sctx =3D ctx;<= br>+
+ union
+ {
+ uint8_t out[8];
+ uint64_t hash;
+ } ret;
+ return ret.hash;
+}

\ No newline at e= nd of file
diff --git a/src/openvpn/siphash.h b/src/openvpn= /siphash.h
index 973596b..739c43c 100644
--= - a/src/openvpn/siphash.h
+++ b/src/openvpn/siphash.h
@@ -25,8 +25,15 @@
/* siphash always uses 128-b= it keys */
#define SIPHASH_KEY_SIZE 16

+struct siphash_ctx
+{
+ uint8_t key[SIPHASH_KEY_SIZE];
+ void *cryptlib_ctx;
+};
+
/* Prototypes for an= implementation of SIPHASH in a crypto library */
<= br>+
/**
* Calculates SIPHASH using the crypto library function.=
*/
@@ -56,17 +63,44 @@
siphash= _cryptolib_available(void *sip_context);

= static inline int
-sip= hash(void *ctx, const void *in, size_t inlen, const void *k,
- uint8_t *out, size_t outlen)=

+siphash(void *ctx, = const void *in, size_t inlen, uint8_t *out, size_t outlen)
= {
- if (siphash_cry= ptolib_available(ctx) && false)
+ struct siphash_ctx *sctx =3D ctx;
+ if (siphash_cryptolib_available(s= ctx->cryptlib_ctx) && false)
{
- return siphash_cryptolib(c= tx, in, inlen, k, out, outlen);

+ return siphash_cryptolib(ctx, in, inlen, sctx->key, = out, outlen);
}
else
{

- ret= urn siphash_reference(in, inlen, k, out, outlen);
+ return siphash_reference(in, inlen, s= ctx->key, out, outlen);
}
}=

+/**

+ * Allocates the resourc= es for a siphash context and returns a pointer to
+ * this context. The context must be freed wi= th siphash_free_context
+ */
+void *
+siphash_new_context(void)= ;
+
+/**
+ * Releases the resources assigned to a siphash cont= ext
+ * @param ctx th= e context previously allocated by siphash_new_context
+ */
+void
+
+/**=
+ * Wrapper of the s= iphash function to easily use it in the
+ * hash map.
+ *
+ * @= param k the data to hash
+ * @param length length of the data to hash
+ * @param ctx the siphash context
= + * @return a unit64_t containi= ng the result of the hashing
+ */
+uint64_= t
+siphash_hash_func(= const uint8_t *k, uint32_t length, void *ctx);
+
+
#endif /* ifndef SIPHASH_H */
\ N= o newline at end of file
diff --git a/tests/unit_tests/open= vpn/Makefile.am b/tests/unit_tests/openvpn/Makefile.am
inde= x 4ed113a..5f7f692 100644
--- a/tests/unit_tests/openvpn/Ma= kefile.am
+++ b/tests/unit_tests/openvpn/Makefile.am=
@@ -85,6 +85,7 @@
$(top_srcdir)/src/openvpn/mtu= .c \
$(top_srcdir)/src/openvpn/win32-util.c \
<= span> $(top_srcdir)/src/openvpn/mss.c \

+ $(top_srcdir)/src/openvpn/siphash.c \
$(top_srcdir)/src/openvpn/siphash_openssl.c \
$(top_sr= cdir)/src/openvpn/siphash_reference.c

@@ = -378,7 +379,10 @@
$(top_srcdir)/src/openvpn/ssl_util.c \<= /span>
$(top_srcdir)/src/openvpn/win32-util.c \
= $(top_srcdir)/src/openvpn/platform.c \
- $(top_srcdir)/src/openvpn/list.c
+ $(top_srcdir)/src/openvpn/list.c \

+ $(top_srcdir)/src/openvp= n/siphash.c \
+ $(top= _srcdir)/src/openvpn/siphash_openssl.c \
+ $(top_srcdir)/src/openvpn/siphash_reference.c<= br>
push_update_msg_testdriver_CFLAGS =3D -I$(top_s= rcdir)/src/openvpn \
-I$(top_srcdir)/src/compat \<= br>diff --git a/tests/unit_tests/openvpn/test_misc.c b/tests/unit_tes= ts/openvpn/test_misc.c
index 24bf22d..576630d 100644=
--- a/tests/unit_tests/openvpn/test_misc.c
+++ b/= tests/unit_tests/openvpn/test_misc.c
@@ -32,6 +32,7 @@
#include <string.h>
#include <setjmp= .h>
#include <cmocka.h>
+#include <siphash.h>
<= /span>
#include "ssl_util.h"
#include = "options_util.h"
@@ -127,10 +128,9 @@
<= span> static uint64_t

word_hash_function(const void *key, = void *ctx)
{
- uint32_t *iv =3D (uint32_t *)ctx;
const ch= ar *str =3D (const char *)key;
const uint32_t len =3D = (uint32_t)strlen(str);
= - return hash_func((const uint8_t *)str, len, *iv);
+ return siphash_hash_func((const uint= 8_t *)str, len, ctx);
}

= static bool
@@ -173,8 +173,8 @@
* wo= rd frequency algorithm.
*/
struc= t gc_arena gc =3D gc_new();
- struct hash *hash =3D hash_init(10000, hash_func_new_ctx(), free,= word_hash_function, word_compare_function);
- struct hash *nhash =3D hash_init(256, hash_func_= new_ctx(), free, word_hash_function, word_compare_function);
+ struct hash *hash =3D hash_ini= t(10000, siphash_new_context(), siphash_free_context, word_hash_function, w= ord_compare_function);

change 1573. To unsubscribe, or for help= writing mail filters, visit settings.

Gerrit-MessageType: newchange
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I807f398903ac2047530800c2994= 9793c6f4f0ec9
Gerrit-Change-Number: 1573
Gerrit-PatchSet: 1
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
= --G29gGWkJOJ0=--