From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by passt.top (Postfix) with ESMTPS id 19A0E5A0272 for ; Fri, 22 Sep 2023 16:06:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695391594; bh=G/MH2ISaqrM8TyWww6kEFgPQalmwWPmJw4pL55oyi1c=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LhCX+dI+TVJnCbUEXITIWUwREFv/SBvUXopk2bu0wEoWDfhZQjO4+dx+m16NqnxVP 3zteEQHrXrykAWt6CQCpQ9olb66hMMqTRbpQ7glwA1tV85fdS+S6X6s9PbsS4vj3PW WKdjUJM94n5EzCwlS/km09vqO+8ctwtoHzHxTm7w= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RsYwV1N14z4xPL; Sat, 23 Sep 2023 00:06:34 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 08/10] siphash: Make internal helpers public Date: Sat, 23 Sep 2023 00:06:28 +1000 Message-ID: <20230922140630.3184256-9-david@gibson.dropbear.id.au> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230922140630.3184256-1-david@gibson.dropbear.id.au> References: <20230922140630.3184256-1-david@gibson.dropbear.id.au> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: LOBY543IKEPBGJCFBYQJPZ576YXCYPBP X-Message-ID-Hash: LOBY543IKEPBGJCFBYQJPZ576YXCYPBP X-MailFrom: dgibson@gandalf.ozlabs.org X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Move a bunch of code from siphash.c to siphash.h, making it available to other modules. This will allow places which need hashes of more complex objects to construct them incrementally. Signed-off-by: David Gibson --- siphash.c | 104 ------------------------------------------------- siphash.h | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 111 insertions(+), 106 deletions(-) diff --git a/siphash.c b/siphash.c index 66174c7..91bcc5d 100644 --- a/siphash.c +++ b/siphash.c @@ -10,45 +10,6 @@ * * Copyright (c) 2020-2021 Red Hat GmbH * Author: Stefano Brivio - * - * This is an implementation of the SipHash-2-4-64 functions needed for TCP - * initial sequence numbers and socket lookup table hash for IPv4 and IPv6, see: - * - * Aumasson, J.P. and Bernstein, D.J., 2012, December. SipHash: a fast - * short-input PRF. In International Conference on Cryptology in India - * (pp. 489-508). Springer, Berlin, Heidelberg. - * - * http://cr.yp.to/siphash/siphash-20120918.pdf - * - * This includes code from the reference SipHash implementation at - * https://github.com/veorq/SipHash/ originally licensed as follows: - * - * -- - * SipHash reference C implementation - * - * Copyright (c) 2012-2021 Jean-Philippe Aumasson - * - * Copyright (c) 2012-2014 Daniel J. Bernstein - * - * 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 - * . - * -- - * - * and from the Linux kernel implementation (lib/siphash.c), originally licensed - * as follows: - * - * -- - * Copyright (C) 2016 Jason A. Donenfeld . All Rights Reserved. - * - * This file is provided under a dual BSD/GPLv2 license. - * -- - * */ #include @@ -56,71 +17,6 @@ #include "siphash.h" -#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) - -struct siphash_state { - uint64_t v[4]; -}; - -#define SIPHASH_INIT(k) { { \ - 0x736f6d6570736575ULL ^ (k)[0], \ - 0x646f72616e646f6dULL ^ (k)[1], \ - 0x6c7967656e657261ULL ^ (k)[0], \ - 0x7465646279746573ULL ^ (k)[1] \ - } } - -/** - * sipround() - Perform rounds of SipHash scrambling - * @v: siphash state (4 x 64-bit integers) - * @n: Number of rounds to apply - */ -static inline void sipround(struct siphash_state *state, int n) -{ - int i; - - for (i = 0; i < n; i++) { - state->v[0] += state->v[1]; - state->v[1] = ROTL(state->v[1], 13) ^ state->v[0]; - state->v[0] = ROTL(state->v[0], 32); - state->v[2] += state->v[3]; - state->v[3] = ROTL(state->v[3], 16) ^ state->v[2]; - state->v[0] += state->v[3]; - state->v[3] = ROTL(state->v[3], 21) ^ state->v[0]; - state->v[2] += state->v[1]; - state->v[1] = ROTL(state->v[1], 17) ^ state->v[2]; - state->v[2] = ROTL(state->v[2], 32); - } -} - -/** - * siphash_feed() - Fold 64-bits of data into the hash state - * @v: siphash state (4 x 64-bit integers) - * @in: New value to fold into hash - */ -static inline void siphash_feed(struct siphash_state *state, uint64_t in) -{ - state->v[3] ^= in; - sipround(state, 2); - state->v[0] ^= in; -} - -/** - * siphash_final - Finalize SipHash calculations - * @v: siphash state (4 x 64-bit integers) - * @len: Total length of input data - * @tail: Final data for the hash (<= 7 bytes) - */ -static inline uint64_t siphash_final(struct siphash_state *state, - size_t len, uint64_t tail) -{ - uint64_t b = (uint64_t)(len) << 56 | tail; - - siphash_feed(state, b); - state->v[2] ^= 0xff; - sipround(state, 4); - return state->v[0] ^ state->v[1] ^ state->v[2] ^ state->v[3]; -} - /** * siphash_8b() - Table index or timestamp offset for TCP over IPv4 (8 bytes in) * @in: Input data (remote address and two ports, or two addresses) diff --git a/siphash.h b/siphash.h index de04c56..f966cdb 100644 --- a/siphash.h +++ b/siphash.h @@ -1,11 +1,120 @@ /* SPDX-License-Identifier: GPL-2.0-or-later - * Copyright (c) 2021 Red Hat GmbH + * Copyright Red Hat * Author: Stefano Brivio - */ + * Author: David Gibson + * + * This is an implementation of the SipHash-2-4-64 functions needed for TCP + * initial sequence numbers and socket lookup table hash for IPv4 and IPv6, see: + * + * Aumasson, J.P. and Bernstein, D.J., 2012, December. SipHash: a fast + * short-input PRF. In International Conference on Cryptology in India + * (pp. 489-508). Springer, Berlin, Heidelberg. + * + * http://cr.yp.to/siphash/siphash-20120918.pdf + * + * This includes code from the reference SipHash implementation at + * https://github.com/veorq/SipHash/ originally licensed as follows: + * + * -- + * SipHash reference C implementation + * + * Copyright (c) 2012-2021 Jean-Philippe Aumasson + * Copyright (c) 2012-2014 Daniel J. Bernstein + * + * 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 + * . + * -- + * + * and from the Linux kernel implementation (lib/siphash.c), originally licensed + * as follows: + * + * -- + * Copyright (C) 2016 Jason A. Donenfeld . All Rights Reserved. + * + * This file is provided under a dual BSD/GPLv2 license. + * -- + * +*/ #ifndef SIPHASH_H #define SIPHASH_H +/** + * struct siphash_state - Internal state of siphash calculation + */ +struct siphash_state { + uint64_t v[4]; +}; + +#define SIPHASH_INIT(k) { { \ + 0x736f6d6570736575ULL ^ (k)[0], \ + 0x646f72616e646f6dULL ^ (k)[1], \ + 0x6c7967656e657261ULL ^ (k)[0], \ + 0x7465646279746573ULL ^ (k)[1] \ + } } + +/** + * sipround() - Perform rounds of SipHash scrambling + * @v: siphash state (4 x 64-bit integers) + * @n: Number of rounds to apply + */ +static inline void sipround(struct siphash_state *state, int n) +{ + int i; + +#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) + + for (i = 0; i < n; i++) { + + state->v[0] += state->v[1]; + state->v[1] = ROTL(state->v[1], 13) ^ state->v[0]; + state->v[0] = ROTL(state->v[0], 32); + state->v[2] += state->v[3]; + state->v[3] = ROTL(state->v[3], 16) ^ state->v[2]; + state->v[0] += state->v[3]; + state->v[3] = ROTL(state->v[3], 21) ^ state->v[0]; + state->v[2] += state->v[1]; + state->v[1] = ROTL(state->v[1], 17) ^ state->v[2]; + state->v[2] = ROTL(state->v[2], 32); + } + +#undef ROTL +} + +/** + * siphash_feed() - Fold 64-bits of data into the hash state + * @v: siphash state (4 x 64-bit integers) + * @in: New value to fold into hash + */ +static inline void siphash_feed(struct siphash_state *state, uint64_t in) +{ + state->v[3] ^= in; + sipround(state, 2); + state->v[0] ^= in; +} + +/** + * siphash_final - Finalize SipHash calculations + * @v: siphash state (4 x 64-bit integers) + * @len: Total length of input data + * @tail: Final data for the hash (<= 7 bytes) + */ +static inline uint64_t siphash_final(struct siphash_state *state, + size_t len, uint64_t tail) +{ + uint64_t b = (uint64_t)(len) << 56 | tail; + + siphash_feed(state, b); + state->v[2] ^= 0xff; + sipround(state, 4); + return state->v[0] ^ state->v[1] ^ state->v[2] ^ state->v[3]; +} + uint64_t siphash_8b(const uint8_t *in, const uint64_t *k); uint64_t siphash_12b(const uint8_t *in, const uint64_t *k); uint64_t siphash_20b(const uint8_t *in, const uint64_t *k); -- 2.41.0