From: "plaisthos (Code Review)" Date: Wed, 11 Mar 2026 17:48:37 +0000 In-Reply-To: References: Reply-To: arne-openvpn@...1227..., openvpn-devel@lists.sourceforge.net, frank@...2641... Message-ID: <8fe79f6f2bc34d1e8f85c5256e31a3b9402e65c4-EmailReplacePatchSet-HTML@...2715...> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Disposition: inline Content-Type: multipart/alternative; boundary="B6IxDl6lul0="; charset=UTF-8 Subject: [Openvpn-devel] [L] Change in openvpn[master]: Allow to use OpenSSL's SIPHASH implementation when available List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: flichtenheld Cc: openvpn-devel --B6IxDl6lul0= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Attention is currently required from: flichtenheld. Hello flichtenheld,=20 I'd like you to reexamine a change. Please visit http://gerrit.openvpn.net/c/openvpn/+/31?usp=3Demail to look at the new patch set (#10). Change subject: Allow to use OpenSSL's SIPHASH implementation when available ...................................................................... Allow to use OpenSSL's SIPHASH implementation when available OpenSSL library is currently slower than the reference implementation (about 1.5x to 2x depending of the compiler). The OpenSSL API for using the SIPHASH MAC is different enough from using normal HMAC or Digest that we already implement that combining them into one API does not make sense. SIPHASH is only available on OpenSSL 3.1 and later. We still check for support on 3.0 and later as the whole API to allow using the SIPHASH alrady exists in OpenSSL 3.0. Some of the later OpenSSL 3.0.x might get support for it. Theoretically, a provider can be loaded in OpenSSL 3.0 that implements SIPHASH. With OpenSSL's implementation being slower, we currently only use it to check that our reference implementation and the OpenSSL implementation yield the same result in unit tests Change-Id: I09aa27caa1a3aab0d1be6118b26d54a1c1bf7aa0 Signed-off-by: Arne Schwabe --- M CMakeLists.txt M src/openvpn/Makefile.am M src/openvpn/siphash.h A src/openvpn/siphash_openssl.c M src/openvpn/siphash_reference.c M tests/unit_tests/openvpn/Makefile.am M tests/unit_tests/openvpn/test_crypto.c 7 files changed, 272 insertions(+), 12 deletions(-) git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/31/31/10 diff --git a/CMakeLists.txt b/CMakeLists.txt index 198c98f..8eb05f6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -556,6 +556,9 @@ src/openvpn/shaper.h src/openvpn/sig.c src/openvpn/sig.h + src/openvpn/siphash.h + src/openvpn/siphash_openssl.c + src/openvpn/siphash_reference.c src/openvpn/socket.c src/openvpn/socket.h src/openvpn/socket_util.c @@ -774,8 +777,12 @@ src/openvpn/packet_id.c src/openvpn/mtu.c src/openvpn/mss.c + src/openvpn/siphash_reference.c + src/openvpn/siphash_openssl.c ) + target_compile_definitions(test_crypto PRIVATE PREFER_OPENSSL_SIPHASH) + target_sources(test_ssl PRIVATE tests/unit_tests/openvpn/mock_management.c tests/unit_tests/openvpn/mock_ssl_dependencies.c diff --git a/src/openvpn/Makefile.am b/src/openvpn/Makefile.am index ff8cc54..08c8250 100644 --- a/src/openvpn/Makefile.am +++ b/src/openvpn/Makefile.am @@ -128,6 +128,8 @@ session_id.c session_id.h \ shaper.c shaper.h \ sig.c sig.h \ + siphash_reference.c siphash.h \ + siphash_openssl.c \ socket.c socket.h \ socket_util.c socket_util.h \ socks.c socks.h \ diff --git a/src/openvpn/siphash.h b/src/openvpn/siphash.h index 3943c76..973596b 100644 --- a/src/openvpn/siphash.h +++ b/src/openvpn/siphash.h @@ -18,12 +18,55 @@ #ifndef SIPHASH_H #define SIPHASH_H -#include +#include +#include +#include /* siphash always uses 128-bit keys */ -#define SIPHASH_KEY_SIZE 16 +#define SIPHASH_KEY_SIZE 16 -int siphash(const void *in, size_t inlen, const void *k, uint8_t *out, - size_t outlen); +/* Prototypes for an implementation of SIPHASH in a crypto library */ -#endif +/** + * Calculates SIPHASH using the crypto library function. + */ +int +siphash_cryptolib(void *sip_context, const void *in, size_t inlen, + const void *k, uint8_t *out, size_t outlen); + +/** + * Calculates SIPHASH using the reference implementation + */ +int +siphash_reference(const void *in, size_t inlen, const void *k, + uint8_t *out, size_t outlen); + +/** + * + * @param hash_size the size of the output hash size + * @return initialised context for siphash + */ +void * +siphash_cryptolib_init(size_t hash_size); + +void +siphash_cryptolib_uninit(void *sip_context); + +bool +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) +{ + if (siphash_cryptolib_available(ctx) && false) + { + return siphash_cryptolib(ctx, in, inlen, k, out, outlen); + } + else + { + return siphash_reference(in, inlen, k, out, outlen); + } +} + +#endif /* ifndef SIPHASH_H */ \ No newline at end of file diff --git a/src/openvpn/siphash_openssl.c b/src/openvpn/siphash_openssl.c new file mode 100644 index 0000000..61d9501 --- /dev/null +++ b/src/openvpn/siphash_openssl.c @@ -0,0 +1,151 @@ +/* + * 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) 2023 OpenVPN Inc + * Copyright (C) 2023 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 "syshead.h" + +#include "siphash.h" +#include "buffer.h" + + +#ifdef ENABLE_CRYPTO_OPENSSL +#include +#endif + +/* OpenSSL siphash is currently 2-3 times slower than the reference + * implementation, so only enable it when PREFER_OPENSSL_SIPHASH is defined + * for now */ +#if defined(ENABLE_CRYPTO_OPENSSL) && OPENSSL_VERSION_NUMBER >=3D 0x300000= 00L && defined(PREFER_OPENSSL_SIPHASH) +#include +#include "crypto_openssl.h" + +struct siphash_context +{ + EVP_MAC *mac; + EVP_MAC_CTX *ctx; + size_t size; + OSSL_PARAM params[3]; +}; + +/* + * Computes a SipHash value + * in: pointer to input data (read-only) + * inlen: input data length in bytes (any size_t value) + * k: pointer to the key data (read-only), must be 16 bytes + * out: pointer to output data (write-only), outlen bytes must be allocated + * outlen: length of the output in bytes, must be 8 or 16 + */ +int +siphash_cryptolib(void *sip_context, const void *in, const size_t inlen, + const void *k, uint8_t *out, const size_t outlen) +{ + struct siphash_context *sip =3D sip_context; + + + sip->params[1] =3D OSSL_PARAM_construct_octet_string("key", (void *)k, + SIPHASH_KEY_SIZE); + if (!EVP_MAC_init(sip->ctx, NULL, 0, sip->params)) + { + crypto_msg(M_FATAL, "EVP_MAC_init failed"); + } + EVP_MAC_update(sip->ctx, in, inlen); + + size_t outl =3D 0; + EVP_MAC_final(sip->ctx, out, &outl, outlen); + return 0; +} + +void * +siphash_cryptolib_init(size_t hash_size) +{ + struct siphash_context *sip; + ALLOC_OBJ(sip, struct siphash_context); + + sip->mac =3D EVP_MAC_fetch(NULL, "SIPHASH", NULL); + if (!sip->mac) + { + /* Our OpenSSL library does not support SIPHASH */ + return sip; + } + sip->ctx =3D EVP_MAC_CTX_new(sip->mac); + + /* OpenSSL will truly hold a pointer to an int in that parameter */ + sip->size =3D hash_size; + sip->params[0] =3D OSSL_PARAM_construct_size_t("size", &sip->size); + /* params[1] will hold the key that changes which each invocation */ + sip->params[2] =3D OSSL_PARAM_construct_end(); + return sip; +} + +bool +siphash_cryptolib_available(void *sip_context) +{ + struct siphash_context *sip =3D sip_context; + + return (bool)(sip->mac); +} + +void +siphash_cryptolib_uninit(void *sip_context) +{ + struct siphash_context *sip =3D sip_context; + EVP_MAC_CTX_free(sip->ctx); + EVP_MAC_free(sip->mac); + free(sip_context); +} + +#else /* if defined(ENABLE_CRYPTO_OPENSSL) && OPENSSL_VERSION_NUMBER >=3D = 0x30000000L */ +/* for now, we only have one implementation of SIPHASH in a libray, so put= the + * dummy functions also here */ +int +siphash_cryptolib(void *sip_context, const void *in, const size_t inlen, + const void *k, uint8_t *out, const size_t outlen) +{ + return -1; +} + +bool +siphash_cryptolib_available(void *sip_context) +{ + return false; +} + +void * +siphash_cryptolib_init(size_t hash_size) +{ + return NULL; +} + +void +siphash_cryptolib_uninit(void *sip_context) +{ +} + + +#endif /* if defined(ENABLE_CRYPTO_OPENSSL) && OPENSSL_VERSION_NUMBER >=3D= 0x30000000L */ diff --git a/src/openvpn/siphash_reference.c b/src/openvpn/siphash_referenc= e.c index b21a86e..a06599f 100644 --- a/src/openvpn/siphash_reference.c +++ b/src/openvpn/siphash_reference.c @@ -100,8 +100,8 @@ * outlen: length of the output in bytes, must be 8 or 16 */ int -siphash(const void *in, const size_t inlen, const void *k, uint8_t *out, - const size_t outlen) +siphash_reference(const void *in, const size_t inlen, const void *k, + uint8_t *out, const size_t outlen) { const unsigned char *ni =3D (const unsigned char *)in; const unsigned char *kk =3D (const unsigned char *)k; diff --git a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvp= n/Makefile.am index 1128eb4..4ed113a 100644 --- a/tests/unit_tests/openvpn/Makefile.am +++ b/tests/unit_tests/openvpn/Makefile.am @@ -84,7 +84,9 @@ $(top_srcdir)/src/openvpn/platform.c \ $(top_srcdir)/src/openvpn/mtu.c \ $(top_srcdir)/src/openvpn/win32-util.c \ - $(top_srcdir)/src/openvpn/mss.c + $(top_srcdir)/src/openvpn/mss.c \ + $(top_srcdir)/src/openvpn/siphash_openssl.c \ + $(top_srcdir)/src/openvpn/siphash_reference.c dhcp_testdriver_CFLAGS =3D -I$(top_srcdir)/src/openvpn -I$(top_srcdir)/sr= c/compat @TEST_CFLAGS@ -DDHCP_UNIT_TEST dhcp_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ -L$(top_srcdir)/src/openvpn @@ -97,6 +99,7 @@ ssl_testdriver_CFLAGS =3D \ -I$(top_srcdir)/include -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/op= envpn \ + -DPREFER_OPENSSL_SIPHASH \ @TEST_CFLAGS@ ssl_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ $(OPTIONAL_CRYPTO_LIBS) ssl_testdriver_SOURCES =3D test_ssl.c \ diff --git a/tests/unit_tests/openvpn/test_crypto.c b/tests/unit_tests/open= vpn/test_crypto.c index 1679d88..0126093 100644 --- a/tests/unit_tests/openvpn/test_crypto.c +++ b/tests/unit_tests/openvpn/test_crypto.c @@ -31,12 +31,14 @@ #include #include #include +#include #include #include "crypto.h" #include "crypto_epoch.h" #include "options.h" #include "ssl_backend.h" +#include "siphash.h" #include "mss.h" #include "test_common.h" @@ -923,6 +925,59 @@ assert_memory_equal(key_parameters.hmac, exp_impl_iv, sizeof(exp_impl_= iv)); } +/* Use a define here since some c compilers don't like array initialisation + * with an integer */ +#define UT_SIPHASH_HASH_SIZE 16 + +static void +test_siphash(void **state) +{ + const char *message =3D "Look behind you, a Three-Headed Monkey!"; + + uint8_t out[UT_SIPHASH_HASH_SIZE] =3D { 0 }; + const uint8_t key[SIPHASH_KEY_SIZE] =3D { 0x11, 0x22, 0x33, 0x44, 0x55= , 0x66 }; + + siphash_reference(message, strlen(message), key, out, UT_SIPHASH_HASH_= SIZE); + + const uint8_t expected_out[UT_SIPHASH_HASH_SIZE] =3D { 0x3e, 0xea, 0x9= 5, 0xb2, 0x6d, 0x5c, 0x4e, 0xfa, + 0x20, 0x47, 0x65,= 0x7e, 0xdd, 0xcd, 0x62, 0x51 }; + assert_memory_equal(out, expected_out, UT_SIPHASH_HASH_SIZE); +} + +static void +test_siphash_openssl(void **state) +{ + void *sipctx =3D siphash_cryptolib_init(UT_SIPHASH_HASH_SIZE); + + if (!siphash_cryptolib_available(sipctx)) + { + siphash_cryptolib_uninit(sipctx); + skip(); + } + + const char *message =3D "Look behind you, a Three-Headed Monkey!"; + + uint8_t out[UT_SIPHASH_HASH_SIZE] =3D { 0 }; + const uint8_t key[SIPHASH_KEY_SIZE] =3D { 0x11, 0x22, 0x33, 0x44, 0x55= , 0x66 }; + + + const uint8_t expected_out[UT_SIPHASH_HASH_SIZE] =3D { 0x3e, 0xea, 0x9= 5, 0xb2, 0x6d, 0x5c, 0x4e, 0xfa, + 0x20, 0x47, 0x65,= 0x7e, 0xdd, 0xcd, 0x62, 0x51 }; + + + siphash_cryptolib(sipctx, message, strlen(message), key, out, + UT_SIPHASH_HASH_SIZE); + assert_memory_equal(out, out, UT_SIPHASH_HASH_SIZE); + + /* check that calling the function twice is safe */ + siphash_cryptolib(sipctx, message, strlen(message), key, out, + UT_SIPHASH_HASH_SIZE); + assert_memory_equal(out, expected_out, UT_SIPHASH_HASH_SIZE); + + siphash_cryptolib_uninit(sipctx); +} + + int main(void) { @@ -957,10 +1012,9 @@ cmocka_unit_test_prestate_setup_teardown(crypto_test_epoch_key_ove= rflow, crypto_test_epoch_setup, crypto_test_epoch_teardow= n, &prestate_num32), - cmocka_unit_test_prestate_setup_teardown(crypto_test_epoch_edge, - crypto_test_epoch_setup, - crypto_test_epoch_teardow= n, &prestate_num13), - cmocka_unit_test(epoch_test_derive_data_key) + cmocka_unit_test(epoch_test_derive_data_key), + cmocka_unit_test(test_siphash), + cmocka_unit_test(test_siphash_openssl) }; #if defined(ENABLE_CRYPTO_OPENSSL) -- To view, visit http://gerrit.openvpn.net/c/openvpn/+/31?usp=3Demail To unsubscribe, or for help writing mail filters, visit http://gerrit.openv= pn.net/settings?usp=3Demail Gerrit-MessageType: newpatchset Gerrit-Project: openvpn Gerrit-Branch: master Gerrit-Change-Id: I09aa27caa1a3aab0d1be6118b26d54a1c1bf7aa0 Gerrit-Change-Number: 31 Gerrit-PatchSet: 10 Gerrit-Owner: plaisthos Gerrit-Reviewer: flichtenheld Gerrit-CC: openvpn-devel Gerrit-Attention: flichtenheld --B6IxDl6lul0= Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

Attention is cur= rently required from: flichtenheld.

plaisthos uploaded patch set #10 to this change.

= View Chang= e

Allow to use OpenSSL's SIPHASH implementation wh=
en available

OpenSSL library is currently slower than the reference = implementation
(about 1.5x to 2x depending of the compiler).

The = OpenSSL API for using the SIPHASH MAC is different enough from using
nor= mal HMAC or Digest that we already implement that combining them into
on= e API does not make sense.

SIPHASH is only available on OpenSSL 3.1 = and later. We still check for
support on 3.0 and later as the whole API = to allow using the SIPHASH alrady
exists in OpenSSL 3.0. Some of the lat= er OpenSSL 3.0.x might get support
for it. Theoretically, a provider can= be loaded in OpenSSL 3.0 that
implements SIPHASH.

With OpenSSL&#= 39;s implementation being slower, we currently only use it to
check that= our reference implementation and the OpenSSL implementation
yield the s= ame result in unit tests

Change-Id: I09aa27caa1a3aab0d1be6118b26d54a= 1c1bf7aa0
Signed-off-by: Arne Schwabe <arne@...1227...>
---
= M CMakeLists.txt
M src/openvpn/Makefile.am
M src/openvpn/siphash.hA src/openvpn/siphash_openssl.c
M src/openvpn/siphash_reference.c
M = tests/unit_tests/openvpn/Makefile.am
M tests/unit_tests/openvpn/test_cry= pto.c
7 files changed, 272 insertions(+), 12 deletions(-)

<= pre class=3D"blocks" style=3D"font-family: monospace,monospace; white-space= : pre-wrap;">git pull ssh://gerrit.openvpn.net:29418/openvpn refs/changes/3= 1/31/10
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 198c98f..8eb05f6 100644
--- a/CMakeLists.txt=
+++ b/CMakeLists.txt
@@ -556,6 +556,9 @@ src/openvpn/shaper.h
src/openvpn/sig.c
src/openvpn/sig.h
+ src/openvpn/siphash.h
+ src/openvpn/siphash_openssl.c
+ src/openvpn/siphash_reference.c
src/openvpn/socket.c
src/openvpn/so= cket.h
src/openvpn/socket_util.c
@@ -7= 74,8 +777,12 @@
src/openvpn/packet_id.c
= src/openvpn/mtu.c
src/openvpn/mss.c=
+ src/openvpn= /siphash_reference.c
= + src/openvpn/siphash_openssl.c
)
+ target_= compile_definitions(test_crypto PRIVATE PREFER_OPENSSL_SIPHASH)
<= span style=3D"color: hsl(120, 100%, 40%);">+

target_so= urces(test_ssl PRIVATE
tests/unit_tests/openvp= n/mock_management.c
tests/unit_tests/openvpn/m= ock_ssl_dependencies.c
diff --git a/src/openvpn/Makefile.am= b/src/openvpn/Makefile.am
index ff8cc54..08c8250 100644
--- a/src/openvpn/Makefile.am
+++ b/src/openv= pn/Makefile.am
@@ -128,6 +128,8 @@
sessio= n_id.c session_id.h \
shaper.c shaper.h \
sig.c sig.h \

+ si= phash_reference.c siphash.h \
+ siphash_openssl.c \
socket.c socket.h \=
socket_util.c socket_util.h \
socks.c socks.h= \
diff --git a/src/openvpn/siphash.h b/src/openvpn/siphash= .h
index 3943c76..973596b 100644
--- a/src/= openvpn/siphash.h
+++ b/src/openvpn/siphash.h
@@ -18,12 +18,55 @@

#ifndef SIPHASH_H
= #define SIPHASH_H

-#include <inttypes.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdbool.h>

/* siphash always uses 128-bit keys */
-#define SIPHASH_KEY_SIZE 16+#define SIPHASH_KEY_SIZE 16<= /span>

-int = siphash(const void *in, size_t inlen, const void *k, uint8_t *out,- size_t outlen);
+/* Prototypes for an i= mplementation of SIPHASH in a crypto library */
-#endif
+/**
+ * Calculates SIPHASH using the crypto library function.
+ */
+int
+siphash_cryptolib(void *sip_context, const void *in, si= ze_t inlen,
+ = const void *k, uint8_t *out, size_t outlen);
+
+/**
+= * Calculates SIPHASH using the reference implementation
+ */
+int
+siphash_reference(const void *in, size_t inlen, const void *k,<= br>+ uint8_t *= out, size_t outlen);
= +
+/**
+ *

+ * @param hash_size the size of the output hash siz= e
+ * @return initial= ised context for siphash
+ */
+void *

+siphash_cryptolib_init(s= ize_t hash_size);
+
+void
+siphash_cryptolib_uninit(void *sip_c= ontext);
+
= +bool
+siphash_cryptolib_available(void *sip_context= );
+
+static inline int
+siphash(void *ctx, const void *in, siz= e_t inlen, const void *k,
+ uint8_t *out, size_t outlen)
+{
+ if (siphash_cryptolib_available(ctx) && false)
<= span style=3D"color: hsl(120, 100%, 40%);">+ {

+ return siphash_cryptolib(ctx, in, inl= en, k, out, outlen);
= + }
+ else

+ {
+ return siphash_reference(in, i= nlen, k, out, outlen);
+}+
+#endif /* ifndef SIPHASH_H */
\ = No newline at end of file
diff --git a/src/openvpn/siphash_= openssl.c b/src/openvpn/siphash_openssl.c
new file mode 100= 644
index 0000000..61d9501
--- /dev/null
+++ b/src/openvpn/siphash_openssl.c
@@ -0,0 += 1,151 @@
+/*+ * OpenVPN -- An applicatio= n to securely tunnel IP networks
+ * over a single TCP/UDP port, with support for SS= L/TLS-based
+ * = session authentication and key exchange,
+ * packet encryption, packet authen= tication, and
+ * = packet compression.
+ *
+ * Copy= right (C) 2023 OpenVPN Inc <sales@...515...>
+ * Copyright (C) 2023 Arne Schwabe <arne= @...1227...>
+ *
+ * This program is f= ree 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 Genera= l Public License along
+ * 51 Franklin = Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifdef = HAVE_CONFIG_H
+#inclu= de "config.h"
+#elif defined(_MSC_VER)
+#include "config-msvc.h"
+#endif
+
+#include = "syshead.h"
+
+#include "si= phash.h"
+#inclu= de "buffer.h"
+
+
+#ifdef ENABLE_CRYPTO_OPENSSL
+#include <openssl/open= sslv.h>
+#endif
+
+/* OpenSSL siphash is currently 2-3 times= slower than the reference
+ * implementation, so only enable it when PREFER_OPENSSL_SIPHASH is = defined
+ * for now *= /
+#if defined(ENABLE= _CRYPTO_OPENSSL) && OPENSSL_VERSION_NUMBER >=3D 0x30000000L &= ;& defined(PREFER_OPENSSL_SIPHASH)
+#include <openssl/evp.h>
+#include "crypto_openssl.h"<= br>+
+struct siphash_context
+{
+ EVP_MAC *mac;
+ EVP_MAC_CTX *ctx;
+ size_t size;
+ OSSL_PARAM params[3];
+};
+
+/*
+ * Computes a SipHash value
+ * in: pointer to input d= ata (read-only)
+ * = inlen: input data length in bytes (any size_t value)
+ * k: pointer to the key data (read-only)= , must be 16 bytes
+ = * out: pointer to output data (write-only), outlen bytes must be allocated<= /span>
+ * outlen: length o= f the output in bytes, must be 8 or 16
+ */
+siphash_crypt= olib(void *sip_context, const void *in, const size_t inlen,
+ const void *k, ui= nt8_t *out, const size_t outlen)

+{
+ s= truct siphash_context *sip =3D sip_context;
+
+
+ sip->pa= rams[1] =3D OSSL_PARAM_construct_octet_string("key", (void *)k,
+ = SIPHASH_KEY_SIZE);
+ if (!EVP_MAC_init(sip->ctx, NULL, = 0, sip->params))
+= {
+ crypto= _msg(M_FATAL, "EVP_MAC_init failed");
+ }
+ EVP_MAC_update(sip->ctx, in, inlen);
+
+ size_t outl =3D 0;
+ EVP_MAC_final(sip->ctx, out, &outl, outlen= );
+ return 0;
+}
+
+void *
+siphash_cryptolib_init(size_t hash_size)
+{
+ struct siphash_context *sip;
+ ALLOC_OBJ(sip, struct siphash_context);
+

+ sip->mac =3D EVP_MAC_fetch(NULL, "SIPHAS= H", NULL);
+ = if (!sip->mac)
+ = {
+ /* Our O= penSSL library does not support SIPHASH */
+ return sip;
+ }
+ sip->ctx =3D EVP_MAC_CTX_new(sip->mac);
+
+ /* OpenSSL will truly hold a pointer to an int in t= hat parameter */
+ = sip->size =3D hash_size;
+ sip->params[0] =3D OSSL_PARAM_construct_size_t("size&q= uot;, &sip->size);
+ /* params[1] will hold the key that changes which each invocation= */
+ sip->para= ms[2] =3D OSSL_PARAM_construct_end();
+ return sip;
+}
+
+bool
+siphash_cryptolib_available(void *sip_= context)
+{+ struct siphash_context *s= ip =3D sip_context;
+=
+ return (bool)(s= ip->mac);
+}
+
+void
+siphash_cryptolib_uninit(void *sip_context)
+{

+ struct siphash_context *sip =3D sip_context;

+ EVP_MAC_CTX_free(sip= ->ctx);
+ EVP_M= AC_free(sip->mac);
+ free(sip_context);
+}
+
+#else /* if defined(ENABLE_CRYPT= O_OPENSSL) && OPENSSL_VERSION_NUMBER >=3D 0x30000000L */
<= br>+/* for now, we only have on= e implementation of SIPHASH in a libray, so put the
+ * dummy functions also here */+int
+siphash_cryptolib(void *sip_context, const vo= id *in, const size_t inlen,
+ const void *k, uint8_t *out, const size_t outlen)=
+{
+ return -1;
+}
+
+boo= l
+siphash_cryptolib_= available(void *sip_context)
+{
+ retur= n false;
+}+
+void *
+siphash_cryptolib_init(size_t hash_size)
+{
+ return NULL;
+}
+
+void
+siphash_cryptolib_uninit(void *sip_con= text)
+{
+}

+
+
+#endif /* if = defined(ENABLE_CRYPTO_OPENSSL) && OPENSSL_VERSION_NUMBER >=3D 0x= 30000000L */
diff --git a/src/openvpn/siphash_reference.c b= /src/openvpn/siphash_reference.c
index b21a86e..a06599f 100= 644
--- a/src/openvpn/siphash_reference.c
+= ++ b/src/openvpn/siphash_reference.c
@@ -100,8 +100,8 @@
* outlen: length of the output in bytes, must be 8 or 16
*/
int
-siphash(const void *in, const size_t inlen, const void *= k, uint8_t *out,
- = const size_t outlen)
+siphash_reference(const void *in, const size_t inlen, const void *k,
+ uint= 8_t *out, const size_t outlen)
{
cons= t unsigned char *ni =3D (const unsigned char *)in;
con= st unsigned char *kk =3D (const unsigned char *)k;
diff --g= it a/tests/unit_tests/openvpn/Makefile.am b/tests/unit_tests/openvpn/Makefi= le.am
index 1128eb4..4ed113a 100644
--- a/t= ests/unit_tests/openvpn/Makefile.am
+++ b/tests/unit_tests/= openvpn/Makefile.am
@@ -84,7 +84,9 @@
$(t= op_srcdir)/src/openvpn/platform.c \
$(top_srcdir)/src/ope= nvpn/mtu.c \
$(top_srcdir)/src/openvpn/win32-util.c \
- $(top_srcdir)/src/openvp= n/mss.c
+ $(top_srcdi= r)/src/openvpn/mss.c \
+ $(top_srcdir)/src/openvpn/siphash_reference.= c

dhcp_testdriver_CFLAGS =3D -I$(top_sr= cdir)/src/openvpn -I$(top_srcdir)/src/compat @TEST_CFLAGS@ -DDHCP_UNIT_TEST=
dhcp_testdriver_LDFLAGS =3D @TEST_LDFLAGS@ -L$(top_srcdir= )/src/openvpn
@@ -97,6 +99,7 @@
ssl_testdriver_CFLAGS =3D \
-I$(top_srcdir)/incl= ude -I$(top_srcdir)/src/compat -I$(top_srcdir)/src/openvpn \
+ -DPREFER_OPENSSL_SIPHASH \
=
@TEST_CFLAGS@
ssl_testdriver_LDFLAGS =3D @TEST= _LDFLAGS@ $(OPTIONAL_CRYPTO_LIBS)
ssl_testdriver_SOURCES = =3D test_ssl.c \
diff --git a/tests/unit_tests/openvpn/test= _crypto.c b/tests/unit_tests/openvpn/test_crypto.c
index 16= 79d88..0126093 100644
--- a/tests/unit_tests/openvpn/test_c= rypto.c
+++ b/tests/unit_tests/openvpn/test_crypto.c=
@@ -31,12 +31,14 @@
#include <stdarg.h>
#include <string.h>
#include <setj= mp.h>
+#include &l= t;inttypes.h>
#include <cmocka.h>

#include "crypto.h"
#include= "crypto_epoch.h"
#include "options.h"=
#include "ssl_backend.h"
+#include "siphash.h"
=
#include "mss.h"
#inclu= de "test_common.h"
@@ -923,6 +925,59 @@ assert_memory_equal(key_parameters.hmac, exp_impl_iv, sizeof(ex= p_impl_iv));
}

+/* Use a define here since some c compilers d= on't like array initialisation
+ * with an integer */
+#define UT_SIPHASH_HASH_SIZE 16
+
+static void
+test_siphash(void **state)
+{
+ const= char *message =3D "Look behind you, a Three-Headed Monkey!";
+
+ uint8_t out[UT_SIPHASH_HASH_SIZE] =3D= { 0 };
+ const ui= nt8_t key[SIPHASH_KEY_SIZE] =3D { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };
+
+ siphash_reference(message, strlen(mes= sage), key, out, UT_SIPHASH_HASH_SIZE);
+
= + const uint8_t expected_out[UT_SIPHASH_HASH_SIZE] =3D { 0x3e, 0xea, 0x9= 5, 0xb2, 0x6d, 0x5c, 0x4e, 0xfa,
+ 0x20,= 0x47, 0x65, 0x7e, 0xdd, 0xcd, 0x62, 0x51 };
+ assert_memory_equal(out, expected_out, UT_SIPH= ASH_HASH_SIZE);
+}
+
+static void
+test_siphash_openssl(void **state)
+{

+ void *sipctx =3D siphash_cryptolib_init(UT_SIPHA= SH_HASH_SIZE);
+

+ if (!siphash_cryptol= ib_available(sipctx))
+ {
+ siph= ash_cryptolib_uninit(sipctx);
+ skip();
+ }
+<= br>+ const char *message =3D= "Look behind you, a Three-Headed Monkey!";
+
+ uint8_t out[UT_SIPHASH_HASH_SIZE] =3D { 0 };
<= span style=3D"color: hsl(120, 100%, 40%);">+ const uint8_t key[SIPHASH_K= EY_SIZE] =3D { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66 };

+
+
+ = const uint8_t expected_out[UT_SIPHASH_HASH_SIZE] =3D { 0x3e, 0xea, 0x95, 0x= b2, 0x6d, 0x5c, 0x4e, 0xfa,
+ 0x20, 0x47= , 0x65, 0x7e, 0xdd, 0xcd, 0x62, 0x51 };
+
= +
+ siphash_crypto= lib(sipctx, message, strlen(message), key, out,
+ UT_SIPHASH_HASH_SIZE);
+ assert_memory_equa= l(out, out, UT_SIPHASH_HASH_SIZE);
+
+ = /* check that calling the function twice is safe */
+ siphash_cryptolib(sipctx, message, st= rlen(message), key, out,
+ UT_SIPHASH_HASH_SIZE);
+ assert_memory_equal(out, expected_out, UT= _SIPHASH_HASH_SIZE);
= +
+ siphash_crypto= lib_uninit(sipctx);
+= }
+
+
int
main(void)

{
@@ -957,10 +1012,9 @@
cmocka_unit_test_prestate_setup_teardown(crypto_test_ep= och_key_overflow,
= crypto_test_epoch_setup,
= crypto_test_epoch_teardown, &prestate_num32),<= /span>
- cmocka_unit_te= st_prestate_setup_teardown(crypto_test_epoch_edge,
- = crypto_test_epoch_setup,
- crypto_test_epoch_te= ardown, &prestate_num13),
- cmocka_unit_test(epoch_test_derive_data_key)
+ cmocka_unit_test(epoch_tes= t_derive_data_key),

+= cmocka_unit_test(test_siphash),
+ cmocka_unit_test(test_siphash_openssl) };

#if defined(ENABLE_CRYPT= O_OPENSSL)

To view, visit change 31. To unsub= scribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: newpatchset
Gerrit-Project: openvpn
Gerrit-Branch: master
Gerrit-Change-Id: I09aa27caa1a3aab0d1be6118b26= d54a1c1bf7aa0
Gerrit-Change-Number: 31
Gerrit-PatchSet: 10
Gerrit-Owner: plaisthos <arne-openvpn@...1711...= 27...>
Gerrit-Reviewer: flichtenheld <frank@...2716...= 1...>
Gerrit-CC: openvpn-devel <openvpn-devel@...1484...= ts.sourceforge.net>
Gerrit-Attention: flichtenheld <frank@...2375...= 41...>
= --B6IxDl6lul0=--