Add siphash reference implementation
PATCH 1/3 Add siphash reference implementation
OpenSSL only supports SIPHASH with OpenSSL 3.1 and newer. The source code of siphash is quite small and has very liberal CC0 license, so include it instead of pulling an extra library for it.
--- src/openvpn/siphash.c | 212 ++++++++++++++++++++++++++++++++++++++++++ src/openvpn/siphash.h | 31 ++++++ 2 files changed, 243 insertions(+) create mode 100644 src/openvpn/siphash.c create mode 100644 src/openvpn/siphash.h
src/openvpn/siphash.c new file +212 -0
@@ -0,0 +1,212 @@ +/* +* SipHash reference C implementation +* +* Copyright (c) 2012-2022 Jean-Philippe Aumasson +* <[email protected]...> +* Copyright (c) 2012-2014 Daniel J. Bernstein <[email protected]...> +* +* To the extent possible under law, the author(s) have dedicated all copyright +* and related and neighboring rights to this software to the public domain +* worldwide. This software is distributed without any warranty. +* +* You should have received a copy of the CC0 Public Domain Dedication along +* with +* this software. If not, see +* <http://creativecommons.org/publicdomain/zero/1.0/>. +*/ + +#include "siphash.h" +#include <assert.h> +#include <stddef.h> +#include <stdint.h> + +/* default: SipHash-2-4 */ +#ifndef cROUNDS +#define cROUNDS 2 +#endif +#ifndef dROUNDS +#define dROUNDS 4 +#endif + +#define ROTL (x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) + +#define U32TO8_LE (p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); + +#define U64TO8_LE (p, v) \ + U32TO8_LE((p), (uint32_t)((v))); \ + U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); + +#define U8TO64_LE (p) \ + (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) \ + |((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) \ + |((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) \ + |((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) + +#define SIPROUND \ + do { \ + v0 += v1; \ + v1 = ROTL(v1, 13); \ + v1 ^= v0; \ + v0 = ROTL(v0, 32); \ + v2 += v3; \ + v3 = ROTL(v3, 16); \ + v3 ^= v2; \ + v0 += v3; \ + v3 = ROTL(v3, 21); \ + v3 ^= v0; \ + v2 += v1; \ + v1 = ROTL(v1, 17); \ + v1 ^= v2; \ + v2 = ROTL(v2, 32); \ + } while (0) + +#ifdef DEBUG_SIPHASH +#include <stdio.h> + +#define TRACE \ + do { \ + printf("(%3zu) v0 %016" PRIx64 "\n", inlen, v0); \ + printf("(%3zu) v1 %016" PRIx64 "\n", inlen, v1); \ + printf("(%3zu) v2 %016" PRIx64 "\n", inlen, v2); \ + printf("(%3zu) v3 %016" PRIx64 "\n", inlen, v3); \ + } while (0) +#else /* ifdef DEBUG_SIPHASH */ +#define TRACE +#endif + +/* +* 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 (const void *in,const size_t inlen,const void *k,uint8_t *out, +const size_t outlen) +{ + +const unsigned char *ni = (const unsigned char *)in; +const unsigned char *kk = (const unsigned char *)k; + +assert ((outlen ==8 ) || (outlen ==16 )); +uint64_t v0 =UINT64_C (0x736f6d6570736575 ); +uint64_t v1 =UINT64_C (0x646f72616e646f6d ); +uint64_t v2 =UINT64_C (0x6c7967656e657261 ); +uint64_t v3 =UINT64_C (0x7465646279746573 ); +uint64_t k0 =U8TO64_LE (kk); +uint64_t k1 =U8TO64_LE (kk +8 ); +uint64_t m; +int i; +const unsigned char *end = ni + inlen - (inlen %sizeof (uint64_t )); +const int left = inlen &7 ; +uint64_t b = ((uint64_t )inlen) <<56 ; + v3 ^= k1; + v2 ^= k0; + v1 ^= k1; + v0 ^= k0; + +if (outlen ==16 ) + { + v1 ^=0xee ; + } + +for (; ni != end; ni +=8 ) + { + m =U8TO64_LE (ni); + v3 ^= m; + +TRACE ; +for (i =0 ; i < cROUNDS; ++i) + { +SIPROUND ; + } + + v0 ^= m; + } + +switch (left) + { +case 7 : + b |= ((uint64_t )ni[6 ]) <<48 ; + +/* FALLTHRU */ +case 6 : + b |= ((uint64_t )ni[5 ]) <<40 ; + +/* FALLTHRU */ +case 5 : + b |= ((uint64_t )ni[4 ]) <<32 ; + +/* FALLTHRU */ +case 4 : + b |= ((uint64_t )ni[3 ]) <<24 ; + +/* FALLTHRU */ +case 3 : + b |= ((uint64_t )ni[2 ]) <<16 ; + +/* FALLTHRU */ +case 2 : + b |= ((uint64_t )ni[1 ]) <<8 ; + +/* FALLTHRU */ +case 1 : + b |= ((uint64_t )ni[0 ]); +break ; + +case 0 : +break ; + } + + v3 ^= b; + +TRACE ; +for (i =0 ; i < cROUNDS; ++i) + { +SIPROUND ; + } + + v0 ^= b; + +if (outlen ==16 ) + { + v2 ^=0xee ; + } +else + { + v2 ^=0xff ; + } + +TRACE ; +for (i =0 ; i < dROUNDS; ++i) + { +SIPROUND ; + } + + b = v0 ^ v1 ^ v2 ^ v3; +U64TO8_LE (out, b); + +if (outlen ==8 ) + { +return 0 ; + } + + v1 ^=0xdd ; + +TRACE ; +for (i =0 ; i < dROUNDS; ++i) + { +SIPROUND ; + } + + b = v0 ^ v1 ^ v2 ^ v3; +U64TO8_LE (out +8 , b); + +return 0 ; +}
src/openvpn/siphash.h new file +31 -0
@@ -0,0 +1,31 @@ +/* +* SipHash reference C implementation +* +* Copyright (c) 2012-2021 Jean-Philippe Aumasson +* <[email protected]...> +* Copyright (c) 2012-2014 Daniel J. Bernstein <[email protected]...> +* +* To the extent possible under law, the author(s) have dedicated all copyright +* and related and neighboring rights to this software to the public domain +* worldwide. This software is distributed without any warranty. +* +* You should have received a copy of the CC0 Public Domain Dedication along +* with +* this software. If not, see +* <http://creativecommons.org/publicdomain/zero/1.0/>. +*/ + +#ifndef SIPHASH_H +#define SIPHASH_H + +#include <inttypes.h> +#include <string.h> + +int siphash (const void *in,size_t inlen,const void *k,uint8_t *out, +size_t outlen); + +/* siphash always uses 128-bit keys */ +#define SIPHASH_KEY_SIZE 16 +#define SIPHASH_HASH_SIZE 16 + +#endif
Signature · 1 line
2.37.1 (Apple Git-137.1)
PATCH 2/3 Implement initial packet reflection protection using bloom filter
When an OpenVPN server is used/tried to be usedc in a reflection attack the protection with the simple --connect-freq-initial also block legimitate client from other networks that are not attacked by a reflection attack.
To allow a server to still reply to these clients, we need to make the counts rather more detailed and count per subnet or IP address. On the other hand when we keep all this state, we eliminate the advantage of having a stateless cookie based initial packet handshake.
As compromoise we use a bloom filter to store the information. This data structure is probabilistic and can have false positive and more packets being dropped but since it is a constant size and the size of this map is small enough for non-embedded systems (tests were done with a 2MB bloom filter), it is a good compromise.
The code is split into the bloom filter implementation and the actual logic implementing tracking the subnets, so the bloom filter should be relatively easily be exchangable by another data structure.
As hash funtion SIPHASH has been chosen since it was designed for this kind of application.
--- .github/workflows/build.yaml | 21 +- doc/man-sections/server-options.rst | 71 +++++ src/openvpn/Makefile.am | 2 + src/openvpn/bloom.c | 253 +++++++++++++++++ src/openvpn/bloom.h | 97 +++++++ src/openvpn/mudp.c | 5 +- src/openvpn/multi.c | 3 +- src/openvpn/openvpn.vcxproj | 3 + src/openvpn/options.c | 93 ++++++ src/openvpn/options.h | 3 + src/openvpn/reflect_filter.c | 361 +++++++++++++++++++++++- src/openvpn/reflect_filter.h | 54 +++- tests/unit_tests/openvpn/Makefile.am | 18 +- tests/unit_tests/openvpn/test_reflect.c | 323 +++++++++++++++++++++ 14 files changed, 1284 insertions(+), 23 deletions(-) create mode 100644 src/openvpn/bloom.c create mode 100644 src/openvpn/bloom.h create mode 100644 tests/unit_tests/openvpn/test_reflect.c
.github/workflows/build.yaml +12 -9
@@ -234,36 +234,39 @@ jobs: - name: List unittests directory run: "dir unittests" - - name: Run argvunit test + - name: Run argv unit test run: ./unittests/argv_testdriver.exe - - name: Run auth_tokenunit test + - name: Run auth_token unit test run: ./unittests/auth_token_testdriver.exe - - name: Run bufferunit test + - name: Run buffer unit test run: ./unittests/buffer_testdriver.exe - name: Run cryptoapi unit test run: ./unittests/cryptoapi_testdriver.exe - - name: Run cryptounit test + - name: Run crypto unit test run: ./unittests/crypto_testdriver.exe - - name: Run miscunit test + - name: Run misc unit test run: ./unittests/misc_testdriver.exe - - name: Run ncpunit test + - name: Run ncp unit test run: ./unittests/ncp_testdriver.exe - - name: Run packet idunit test + - name: Run packet id unit test run: ./unittests/packet_id_testdriver.exe - - name: Run pktunit test + - name: Run pkt unit test run: ./unittests/pkt_testdriver.exe - - name: Run providerunit test + - name: Run provider unit test run: ./unittests/provider_testdriver.exe + - name: Run reflect unit test + run: ./unittests/reflect_testdriver.exe + ubuntu: strategy: fail-fast: false
doc/man-sections/server-options.rst +71 -0
77 lines not on this page · raw message
src/openvpn/Makefile.am +2 -0
@@ -43,6 +43,7 @@ openvpn_SOURCES = \ auth_token.c auth_token.h \ base64.c base64.h \ basic.h \ + bloom.c bloom.h \ buffer.c buffer.h \ circ_list.h \ clinat.c clinat.h \
@@ -122,6 +123,7 @@ openvpn_SOURCES = \ session_id.c session_id.h \ shaper.c shaper.h \ sig.c sig.h \ + siphash.c siphash.h \ socket.c socket.h \ socks.c socks.h \ ssl.c ssl.h ssl_backend.h \
src/openvpn/bloom.c new file +253 -0
253 lines not on this page · raw message
src/openvpn/bloom.h new file +97 -0
97 lines not on this page · raw message
src/openvpn/mudp.c +3 -2
@@ -86,7 +86,7 @@ do_pre_decrypt_check(struct multi_context *m, {/* Check if we are still below our limit for sending out * responses */ -if (!reflect_filter_rate_limit_check (m->initial_rate_limiter)) +if (!reflect_filter_check (m->initial_rate_limiter, from)) {return false; }
@@ -256,7 +256,8 @@ multi_get_create_instance_udp(struct multi_context *m, bool *floated) {/* a successful three-way handshake only counts against * connect-freq but not against connect-freq-initial */ -reflect_filter_rate_limit_decrease (m->initial_rate_limiter); +reflect_filter_rate_limit_decrease (m->initial_rate_limiter, + &m->top.c2.from.dest); mi =multi_create_instance (m, &real);if (mi)
src/openvpn/multi.c +2 -1
@@ -370,7 +370,8 @@ multi_init(struct multi_context *m, struct context *t, bool tcp_mode) m->new_connection_limiter =frequency_limit_init (t->options.cf_max, t->options.cf_per); m->initial_rate_limiter =initial_rate_limit_init (t->options.cf_initial_max, - t->options.cf_initial_per); + t->options.cf_initial_per, + &t->options.initial_cf_bloom_config);/* * Allocate broadcast/multicast buffer list
src/openvpn/openvpn.vcxproj +3 -0
@@ -266,6 +266,7 @@ <ClCompile Include="auth_token.c" /> <ClCompile Include="base64.c" /> <ClCompile Include="block_dns.c" /> + <ClCompile Include="bloom.c" /> <ClCompile Include="buffer.c" /> <ClCompile Include="clinat.c" /> <ClCompile Include="comp-lz4.c" />
@@ -330,6 +331,7 @@ <ClCompile Include="session_id.c" /> <ClCompile Include="shaper.c" /> <ClCompile Include="sig.c" /> + <ClCompile Include="siphash.c" /> <ClCompile Include="socket.c" /> <ClCompile Include="socks.c" /> <ClCompile Include="ssl.c" />
@@ -427,6 +429,7 @@ <ClInclude Include="session_id.h" /> <ClInclude Include="shaper.h" /> <ClInclude Include="sig.h" /> + <ClInclude Include="siphash.h" /> <ClInclude Include="socket.h" /> <ClInclude Include="socks.h" /> <ClInclude Include="ssl.h" />
src/openvpn/options.c +93 -0
111 lines not on this page · raw message
src/openvpn/options.h +3 -0
@@ -43,6 +43,7 @@#include "clinat.h" #include "crypto_backend.h" #include "dns.h" +#include "reflect_filter.h" /*
@@ -520,6 +521,8 @@ struct optionsint cf_initial_max;int cf_initial_per; +struct bloom_filter_conf initial_cf_bloom_config; +int max_clients;int max_routes_per_client;int stale_routes_check_interval;
src/openvpn/reflect_filter.c +355 -6
397 lines not on this page · raw message
src/openvpn/reflect_filter.h +50 -4
94 lines not on this page · raw message
tests/unit_tests/openvpn/Makefile.am +17 -1
@@ -7,7 +7,7 @@ test_binaries += argv_testdriver buffer_testdriver endif test_binaries += crypto_testdriver packet_id_testdriver auth_token_testdriver ncp_testdriver misc_testdriver \ - pkt_testdriver + pkt_testdriver reflect_testdriver if HAVE_LD_WRAP_SUPPORT if !WIN32 test_binaries += tls_crypt_testdriver
@@ -99,6 +99,22 @@ pkt_testdriver_SOURCES = test_pkt.c mock_msg.c mock_msg.h \ $(openvpn_srcdir)/win32-util.c \ $(openvpn_srcdir)/tls_crypt.c +reflect_testdriver_CFLAGS = @TEST_CFLAGS@ \ + -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir) +reflect_testdriver_LDFLAGS = @TEST_LDFLAGS@ +reflect_testdriver_SOURCES = test_reflect.c mock_msg.c mock_msg.h \ + $(openvpn_srcdir)/reflect_filter.c \ + $(openvpn_srcdir)/bloom.c \ + $(openvpn_srcdir)/buffer.c \ + $(openvpn_srcdir)/crypto.c \ + $(openvpn_srcdir)/crypto_mbedtls.c \ + $(openvpn_srcdir)/crypto_openssl.c \ + $(openvpn_srcdir)/otime.c \ + $(openvpn_srcdir)/packet_id.c \ + $(openvpn_srcdir)/platform.c \ + $(openvpn_srcdir)/siphash.c \ + $(openvpn_srcdir)/win32-util.c + if !WIN32 tls_crypt_testdriver_CFLAGS = @TEST_CFLAGS@ \ -I$(openvpn_includedir) -I$(compat_srcdir) -I$(openvpn_srcdir)
tests/unit_tests/openvpn/test_reflect.c new file +323 -0
323 lines not on this page · raw message
Signature · 1 line
2.37.1 (Apple Git-137.1)
PATCH 3/3 Prefer OpenSSL's SIPHASH implementation when available
In reply to Arne SchwabeOpenSSL library is significantly faster than the reference implementation (almost 2x). Prefer using this when available. The 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.
--- src/openvpn/Makefile.am | 1 + src/openvpn/bloom.c | 14 ++- src/openvpn/bloom.h | 6 + src/openvpn/openvpn.vcxproj | 1 + src/openvpn/reflect_filter.c | 1 + src/openvpn/siphash.c | 4 +- src/openvpn/siphash.h | 48 +++++++- src/openvpn/siphash_openssl.c | 149 ++++++++++++++++++++++++ tests/unit_tests/openvpn/Makefile.am | 1 + tests/unit_tests/openvpn/test_reflect.c | 36 ++++++ 10 files changed, 253 insertions(+), 8 deletions(-) create mode 100644 src/openvpn/siphash_openssl.c
src/openvpn/Makefile.am +1 -0
@@ -124,6 +124,7 @@ openvpn_SOURCES = \ shaper.c shaper.h \ sig.c sig.h \ siphash.c siphash.h \ + siphash_openssl.c \ socket.c socket.h \ socks.c socks.h \ ssl.c ssl.h ssl_backend.h \
src/openvpn/bloom.c +12 -2
@@ -169,11 +169,20 @@ bloom_create(size_t size, size_t num_hashes, struct gc_arena *gc) bf->num_siphash =calculate_num_sip_hash_hashes (bf); ALLOC_ARRAY_GC(bf->siphash_keys, struct siphash_key, bf->num_siphash, gc); - + + bf->siphash_ctx =siphash_cryptolib_init (); +bloom_clear (bf);return bf; } +void +bloom_free (struct bloom_filter *bf) +{ +siphash_cryptolib_uninit (bf->siphash_ctx); +} + +/** * Clear the bloom filter, making it empty again as if it were freshly created * @param bf the bloom structure to clear
@@ -209,7 +218,8 @@ bloom_add_test(struct bloom_filter *bf, const uint8_t *item, size_t len, bloom_cif (idx ==0 ) {/* We have no longer unused bytes in result, generate the next hash */ -siphash (item, len, bf->siphash_keys[j++].key, result,SIPHASH_HASH_SIZE ); +siphash (bf->siphash_ctx, item, len, bf->siphash_keys[j++].key, + result,SIPHASH_HASH_SIZE ); } bucket = bucket <<8 ;
src/openvpn/bloom.h +6 -0
@@ -75,6 +75,9 @@ struct bloom_filter {/** keys for the siphash functions */ struct siphash_key *siphash_keys; +/** (opaque) context for the siphash implementation */ +void *siphash_ctx; +/** the actual buckets that hold the data */ bloom_counter_t buckets[]; };
@@ -83,6 +86,9 @@ struct bloom_filter {struct bloom_filter *bloom_create (size_t size,size_t num_hashes,struct gc_arena *gc); +void +bloom_free (struct bloom_filter *bf); +bloom_counter_t bloom_test (struct bloom_filter *bf,const uint8_t *item,size_t len);
src/openvpn/openvpn.vcxproj +1 -0
@@ -332,6 +332,7 @@ <ClCompile Include="shaper.c" /> <ClCompile Include="sig.c" /> <ClCompile Include="siphash.c" /> + <ClCompile Include="siphash_openssl.c" /> <ClCompile Include="socket.c" /> <ClCompile Include="socks.c" /> <ClCompile Include="ssl.c" />
src/openvpn/reflect_filter.c +1 -0
@@ -450,6 +450,7 @@ initial_rate_limit_init(int max_per_period, int period_length,void initial_rate_limit_free (struct initial_packet_rate_limit *irl) { +bloom_free (irl->bf);gc_free (&irl->gc);free (irl); irl =NULL ;
src/openvpn/siphash.c +2 -2
@@ -87,8 +87,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 = (const unsigned char *)in;
src/openvpn/siphash.h +44 -4
56 lines not on this page · raw message
src/openvpn/siphash_openssl.c new file +149 -0
149 lines not on this page · raw message
tests/unit_tests/openvpn/Makefile.am +1 -0
@@ -113,6 +113,7 @@ reflect_testdriver_SOURCES = test_reflect.c mock_msg.c mock_msg.h \ $(openvpn_srcdir)/packet_id.c \ $(openvpn_srcdir)/platform.c \ $(openvpn_srcdir)/siphash.c \ + $(openvpn_srcdir)/siphash_openssl.c \ $(openvpn_srcdir)/win32-util.c if !WIN32
tests/unit_tests/openvpn/test_reflect.c +36 -0
@@ -40,8 +40,43 @@#include <stdio.h> +static void +test_siphash (void **state) +{ +const char *message ="Look behind you, a Three-Headed Monkey!" ; + +uint8_t out[SIPHASH_HASH_SIZE ]; +const uint8_t key[SIPHASH_KEY_SIZE ] = {0x11 ,0x22 ,0x33 ,0x44 ,0x55 ,0x66 }; + +siphash_reference (message,strlen (message), key, out,SIPHASH_HASH_SIZE ); + +const uint8_t expected_out[SIPHASH_HASH_SIZE ] = + {0x3e ,0xea ,0x95 ,0xb2 ,0x6d ,0x5c ,0x4e ,0xfa , +0x20 ,0x47 ,0x65 ,0x7e ,0xdd ,0xcd ,0x62 ,0x51 }; +assert_memory_equal (out, expected_out,SIPHASH_HASH_SIZE ); + +struct gc_arena gc =gc_new (); +void *sipctx =siphash_cryptolib_init (); +uint8_t out2[SIPHASH_HASH_SIZE ]; + +if (siphash_cryptolib_available (sipctx)) + { +siphash_cryptolib (sipctx, message,strlen (message), key, out2, +SIPHASH_HASH_SIZE ); +assert_memory_equal (out, out2,SIPHASH_HASH_SIZE ); + +/* check that calling the function twice is safe */ +siphash_cryptolib (sipctx, message,strlen (message), key, out2, +SIPHASH_HASH_SIZE ); +assert_memory_equal (out, out2,SIPHASH_HASH_SIZE ); + } + +siphash_cryptolib_uninit (sipctx); +gc_free (&gc); +} +static void test_bloom (void **state) {
@@ -307,6 +342,7 @@ intmain (void) {const struct CMUnitTest tests[] = { +cmocka_unit_test (test_siphash),cmocka_unit_test (test_bloom_access_functions),cmocka_unit_test (test_bloom),cmocka_unit_test (test_bloom_minimal),
Signature · 1 line
2.37.1 (Apple Git-137.1)