diff options
Diffstat (limited to 'src/c-siphash')
| -rw-r--r-- | src/c-siphash/.editorconfig | 11 | ||||
| -rw-r--r-- | src/c-siphash/.github/workflows/ci.yml | 32 | ||||
| -rw-r--r-- | src/c-siphash/.gitmodules | 0 | ||||
| -rw-r--r-- | src/c-siphash/AUTHORS | 38 | ||||
| -rw-r--r-- | src/c-siphash/NEWS.md | 9 | ||||
| -rw-r--r-- | src/c-siphash/README.md | 53 | ||||
| -rw-r--r-- | src/c-siphash/meson.build | 21 | ||||
| -rw-r--r-- | src/c-siphash/src/libcsiphash.def | 9 | ||||
| -rw-r--r-- | src/c-siphash/src/libcsiphash.sym | 16 | ||||
| -rw-r--r-- | src/c-siphash/src/meson.build | 58 | ||||
| -rw-r--r-- | src/c-siphash/src/test-api.c | 31 | ||||
| -rw-r--r-- | src/c-siphash/src/test-basic.c | 120 | ||||
| l--------- | src/c-siphash/subprojects/libcstdaux-1 | 1 |
13 files changed, 399 insertions, 0 deletions
diff --git a/src/c-siphash/.editorconfig b/src/c-siphash/.editorconfig new file mode 100644 index 00000000..b10bb4f3 --- /dev/null +++ b/src/c-siphash/.editorconfig @@ -0,0 +1,11 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +charset = utf-8 + +[*.{c,h}] +indent_style = space +indent_size = 8 diff --git a/src/c-siphash/.github/workflows/ci.yml b/src/c-siphash/.github/workflows/ci.yml new file mode 100644 index 00000000..00bca960 --- /dev/null +++ b/src/c-siphash/.github/workflows/ci.yml @@ -0,0 +1,32 @@ +name: Continuous Integration + +on: + push: + pull_request: + schedule: + - cron: '0 0 * * *' + +jobs: + ci-linux: + name: Linux CI + uses: bus1/cabuild/.github/workflows/ci-c-util.yml@v1 + with: + cabuild_ref: "v1" + linux: true + m32: true + matrixmode: true + valgrind: true + ci-macos: + name: MacOS CI + uses: bus1/cabuild/.github/workflows/ci-c-util.yml@v1 + with: + cabuild_ref: "v1" + linux: false + macos: true + ci-windows: + name: Windows CI + uses: bus1/cabuild/.github/workflows/ci-c-util.yml@v1 + with: + cabuild_ref: "v1" + linux: false + windows: true diff --git a/src/c-siphash/.gitmodules b/src/c-siphash/.gitmodules new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/src/c-siphash/.gitmodules diff --git a/src/c-siphash/AUTHORS b/src/c-siphash/AUTHORS new file mode 100644 index 00000000..ee6d914f --- /dev/null +++ b/src/c-siphash/AUTHORS @@ -0,0 +1,38 @@ +LICENSE: + This project is dual-licensed under both the Apache License, Version + 2.0, and the GNU Lesser General Public License, Version 2.1+. + +AUTHORS-ASL: + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +AUTHORS-LGPL: + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU Lesser General Public License as published + by the Free Software Foundation; either version 2.1 of the License, or + (at your option) any later version. + + 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with this program; If not, see <http://www.gnu.org/licenses/>. + +COPYRIGHT: (ordered alphabetically) + Copyright (C) 2015-2022 Red Hat, Inc. + +AUTHORS: (ordered alphabetically) + Daniele Nicolodi <daniele@grinta.net> + David Rheinsberg <david.rheinsberg@gmail.com> + Tom Gundersen <teg@jklm.no> diff --git a/src/c-siphash/NEWS.md b/src/c-siphash/NEWS.md new file mode 100644 index 00000000..a0227c04 --- /dev/null +++ b/src/c-siphash/NEWS.md @@ -0,0 +1,9 @@ +# c-siphash - Streaming-capable SipHash Implementation + +## CHANGES WITH 1.0.0: + + * Initial release of c-siphash. + + Contributions from: David Rheinsberg, Tom Gundersen + + - Brno, 2022-06-22 diff --git a/src/c-siphash/README.md b/src/c-siphash/README.md new file mode 100644 index 00000000..bd97d06b --- /dev/null +++ b/src/c-siphash/README.md @@ -0,0 +1,53 @@ +c-siphash +========= + +Streaming-capable SipHash Implementation + +The c-siphash project is a standalone implementation of SipHash in Standard +ISO-C11. It provides a streaming-capable API to compute data hashes according +to the SipHash algorithm. For API documentation, see the c-siphash.h header +file, as well as the docbook comments for each function. + +### Project + + * **Website**: <https://c-util.github.io/c-siphash> + * **Bug Tracker**: <https://github.com/c-util/c-siphash/issues> + +### Requirements + +The requirements for this project are: + + * `libc` (e.g., `glibc >= 2.16`) + +At build-time, the following software is required: + + * `meson >= 0.60` + * `pkg-config >= 0.29` + +### Build + +The meson build-system is used for this project. Contact upstream +documentation for detailed help. In most situations the following +commands are sufficient to build and install from source: + +```sh +mkdir build +cd build +meson setup .. +ninja +meson test +ninja install +``` + +No custom configuration options are available. + +### Repository: + + - **web**: <https://github.com/c-util/c-siphash> + - **https**: `https://github.com/c-util/c-siphash.git` + - **ssh**: `git@github.com:c-util/c-siphash.git` + +### License: + + - **Apache-2.0** OR **LGPL-2.1-or-later** + - See AUTHORS file for details. diff --git a/src/c-siphash/meson.build b/src/c-siphash/meson.build new file mode 100644 index 00000000..ffcb6a76 --- /dev/null +++ b/src/c-siphash/meson.build @@ -0,0 +1,21 @@ +project( + 'c-siphash', + 'c', + default_options: [ + 'c_std=c11' + ], + license: 'Apache', + meson_version: '>=0.60.0', + version: '1.0.0', +) +major = meson.project_version().split('.')[0] +project_description = 'Streaming-capable SipHash Implementation' + +mod_pkgconfig = import('pkgconfig') + +dep_cstdaux = dependency('libcstdaux-1') +add_project_arguments(dep_cstdaux.get_variable('cflags').split(' '), language: 'c') + +subdir('src') + +meson.override_dependency('libcsiphash-'+major, libcsiphash_dep, static: true) diff --git a/src/c-siphash/src/libcsiphash.def b/src/c-siphash/src/libcsiphash.def new file mode 100644 index 00000000..445cb0c4 --- /dev/null +++ b/src/c-siphash/src/libcsiphash.def @@ -0,0 +1,9 @@ +LIBRARY csiphash-1-0 +EXPORTS + c_siphash_init + c_siphash_append + c_siphash_finalize + c_siphash_hash + c_siphash_append_13 + c_siphash_finalize_13 + c_siphash_hash_13 diff --git a/src/c-siphash/src/libcsiphash.sym b/src/c-siphash/src/libcsiphash.sym new file mode 100644 index 00000000..aad193d0 --- /dev/null +++ b/src/c-siphash/src/libcsiphash.sym @@ -0,0 +1,16 @@ +LIBCSIPHASH_1 { +global: + c_siphash_init; + c_siphash_append; + c_siphash_finalize; + c_siphash_hash; +local: + *; +}; + +LIBCSIPHASH_2 { +global: + c_siphash_append_13; + c_siphash_finalize_13; + c_siphash_hash_13; +} LIBCSIPHASH_1; diff --git a/src/c-siphash/src/meson.build b/src/c-siphash/src/meson.build new file mode 100644 index 00000000..f2cc1864 --- /dev/null +++ b/src/c-siphash/src/meson.build @@ -0,0 +1,58 @@ +# +# target: libcsiphash.so +# + +libcsiphash_deffile = join_paths(meson.current_source_dir(), 'libcsiphash.def') +libcsiphash_symfile = join_paths(meson.current_source_dir(), 'libcsiphash.sym') + +libcsiphash_deps = [ + dep_cstdaux, +] + +libcsiphash_both = both_libraries( + 'csiphash-'+major, + [ + 'c-siphash.c', + ], + c_args: [ + '-fvisibility=hidden', + '-fno-common', + ], + dependencies: libcsiphash_deps, + install: not meson.is_subproject(), + link_args: dep_cstdaux.get_variable('version-scripts') == 'yes' ? [ + '-Wl,--version-script=@0@'.format(libcsiphash_symfile), + ] : [], + link_depends: libcsiphash_symfile, + vs_module_defs: libcsiphash_deffile, + soversion: 0, +) + +libcsiphash_dep = declare_dependency( + dependencies: libcsiphash_deps, + include_directories: include_directories('.'), + link_with: libcsiphash_both.get_static_lib(), + version: meson.project_version(), +) + +if not meson.is_subproject() + install_headers('c-siphash.h') + + mod_pkgconfig.generate( + description: project_description, + filebase: 'libcsiphash-'+major, + libraries: libcsiphash_both.get_shared_lib(), + name: 'libcsiphash', + version: meson.project_version(), + ) +endif + +# +# target: test-* +# + +test_api = executable('test-api', ['test-api.c'], link_with: libcsiphash_both.get_shared_lib()) +test('API Symbol Visibility', test_api) + +test_basic = executable('test-basic', ['test-basic.c'], dependencies: libcsiphash_dep) +test('Basic API Behavior', test_basic) diff --git a/src/c-siphash/src/test-api.c b/src/c-siphash/src/test-api.c new file mode 100644 index 00000000..8075a17f --- /dev/null +++ b/src/c-siphash/src/test-api.c @@ -0,0 +1,31 @@ +/* + * Tests for Public API + * This test, unlikely the others, is linked against the real, distributed, + * shared library. Its sole purpose is to test for symbol availability. + */ + +#undef NDEBUG +#include <assert.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "c-siphash.h" + +static void test_api(void) { + CSipHash state = C_SIPHASH_NULL; + uint8_t seed[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; + uint64_t hash1, hash2; + + c_siphash_init(&state, seed); + c_siphash_append(&state, NULL, 0); + hash1 = c_siphash_finalize(&state); + assert(hash1 == 12552310112479190712ULL); + + hash2 = c_siphash_hash(seed, NULL, 0); + assert(hash1 == hash2); +} + +int main(int argc, char **argv) { + test_api(); + return 0; +} diff --git a/src/c-siphash/src/test-basic.c b/src/c-siphash/src/test-basic.c new file mode 100644 index 00000000..58442dac --- /dev/null +++ b/src/c-siphash/src/test-basic.c @@ -0,0 +1,120 @@ +/* + * Tests for Basic Hash Operations + * This test does some basic hash operations and verifies their correctness. It + * breaks up the data to be hashed in various ways to make sure it is stable. + */ + +#undef NDEBUG +#include <assert.h> +#include <c-stdaux.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "c-siphash.h" + +/* See https://131002.net/siphash/siphash.pdf, Appendix A. */ +static void do_reference_test(const uint8_t *in, size_t len, const uint8_t *key) { + CSipHash state = C_SIPHASH_NULL; + uint64_t out; + unsigned i, j; + + /* verify the internal state as given in the above paper */ + c_siphash_init(&state, key); + c_assert(state.v0 == 0x7469686173716475); + c_assert(state.v1 == 0x6b617f6d656e6665); + c_assert(state.v2 == 0x6b7f62616d677361); + c_assert(state.v3 == 0x7b6b696e727e6c7b); + c_siphash_append(&state, in, len); + c_assert(state.v0 == 0x4a017198de0a59e0); + c_assert(state.v1 == 0x0d52f6f62a4f59a4); + c_assert(state.v2 == 0x634cb3577b01fd3d); + c_assert(state.v3 == 0xa5224d6f55c7d9c8); + out = c_siphash_finalize(&state); + c_assert(out == 0xa129ca6149be45e5); + c_assert(state.v0 == 0xf6bcd53893fecff1); + c_assert(state.v1 == 0x54b9964c7ea0d937); + c_assert(state.v2 == 0x1b38329c099bb55a); + c_assert(state.v3 == 0x1814bb89ad7be679); + + /* verify that decomposing the input in three chunks gives the + same result */ + for (i = 0; i < len; i++) { + for (j = i; j < len; j++) { + c_siphash_init(&state, key); + c_siphash_append(&state, in, i); + c_siphash_append(&state, &in[i], j - i); + c_siphash_append(&state, &in[j], len - j); + out = c_siphash_finalize(&state); + c_assert(out == 0xa129ca6149be45e5); + } + } + + /* verify c_siphash_hash() produces the same result */ + c_assert(out == c_siphash_hash(key, in, len)); +} + +static void test_reference(void) { + + const uint8_t in[15] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e }; + const uint8_t key[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, + 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}; + uint8_t in_buf[20]; + + /* Test with same input but different alignments. */ + memcpy(in_buf, in, sizeof(in)); + do_reference_test(in_buf, sizeof(in), key); + memcpy(in_buf + 1, in, sizeof(in)); + do_reference_test(in_buf + 1, sizeof(in), key); + memcpy(in_buf + 2, in, sizeof(in)); + do_reference_test(in_buf + 2, sizeof(in), key); + memcpy(in_buf + 4, in, sizeof(in)); + do_reference_test(in_buf + 4, sizeof(in), key); +} + +static void test_short_hashes(void) { + const uint8_t one[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, + 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16 }; + const uint8_t key[16] = { 0x22, 0x24, 0x41, 0x22, 0x55, 0x77, 0x88, 0x07, + 0x23, 0x09, 0x23, 0x14, 0x0c, 0x33, 0x0e, 0x0f}; + uint8_t two[sizeof one] = {0}; + + CSipHash state1 = C_SIPHASH_NULL, state2 = C_SIPHASH_NULL; + unsigned i, j; + + c_siphash_init(&state1, key); + c_siphash_init(&state2, key); + + /* hashing 1, 2, 3, 4, 5, ..., 16 bytes, with the byte after the buffer different */ + for (i = 1; i <= sizeof one; i++) { + c_siphash_append(&state1, one, i); + + two[i-1] = one[i-1]; + c_siphash_append(&state2, two, i); + + c_assert(memcmp(&state1, &state2, sizeof state1) == 0); + } + + /* hashing n and 1, n and 2, n and 3, ..., n-1 and 1, n-2 and 2, ... */ + for (i = sizeof one; i > 0; i--) { + memset(two, 0, sizeof(two)); + + for (j = 1; j <= sizeof one; j++) { + c_siphash_append(&state1, one, i); + c_siphash_append(&state1, one, j); + + c_siphash_append(&state2, one, i); + two[j-1] = one[j-1]; + c_siphash_append(&state2, two, j); + + c_assert(memcmp(&state1, &state2, sizeof state1) == 0); + } + } +} + +int main(int argc, char *argv[]) { + test_reference(); + test_short_hashes(); + + return 0; +} diff --git a/src/c-siphash/subprojects/libcstdaux-1 b/src/c-siphash/subprojects/libcstdaux-1 new file mode 120000 index 00000000..589984f3 --- /dev/null +++ b/src/c-siphash/subprojects/libcstdaux-1 @@ -0,0 +1 @@ +../../c-stdaux \ No newline at end of file |