From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from gandalf.ozlabs.org (gandalf.ozlabs.org [150.107.74.76]) by passt.top (Postfix) with ESMTPS id 8AD385A0276 for ; Fri, 22 Sep 2023 16:06:45 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695391594; bh=FepakHm4Gno0yfXYzm8qSlQNBC8Zr+b32vUChv7TeWw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=VJEaQjZyLGW7/n47fYkdI8/gpxv4lk7xCl1VLTpldFIsXb6m+bv+/qJo48LdkNDql zKWxzdCgeUz2xCD3jpDjsnYkMpnZujV3ScB7R7IfpyxDRpD31DSfBA7nwazjeYLeSU dctZboYfFo9TKQ48qCQAwqIbTCyGAF/YzSsgmfi0= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RsYwV1H8bz4xLy; Sat, 23 Sep 2023 00:06:34 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 07/10] siphash: Use specific structure for internal state Date: Sat, 23 Sep 2023 00:06:27 +1000 Message-ID: <20230922140630.3184256-8-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: QF5IT6VQ766HMIU3YOMN4VKT6NOSOBVF X-Message-ID-Hash: QF5IT6VQ766HMIU3YOMN4VKT6NOSOBVF 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: To improve type safety, encapsulate the internal state of the SipHash algorithm into a dedicated structure type. Signed-off-by: David Gibson --- siphash.c | 80 +++++++++++++++++++++++++++++-------------------------- 1 file changed, 42 insertions(+), 38 deletions(-) diff --git a/siphash.c b/siphash.c index 21c560d..66174c7 100644 --- a/siphash.c +++ b/siphash.c @@ -58,33 +58,37 @@ #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) -#define SIPHASH_INIT(k) { \ +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(uint64_t *v, int n) +static inline void sipround(struct siphash_state *state, int n) { int i; for (i = 0; i < n; i++) { - v[0] += v[1]; - v[1] = ROTL(v[1], 13) ^ v[0]; - v[0] = ROTL(v[0], 32); - v[2] += v[3]; - v[3] = ROTL(v[3], 16) ^ v[2]; - v[0] += v[3]; - v[3] = ROTL(v[3], 21) ^ v[0]; - v[2] += v[1]; - v[1] = ROTL(v[1], 17) ^ v[2]; - v[2] = ROTL(v[2], 32); + 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); } } @@ -93,11 +97,11 @@ static inline void sipround(uint64_t *v, int n) * @v: siphash state (4 x 64-bit integers) * @in: New value to fold into hash */ -static inline void siphash_feed(uint64_t *v, uint64_t in) +static inline void siphash_feed(struct siphash_state *state, uint64_t in) { - v[3] ^= in; - sipround(v, 2); - v[0] ^= in; + state->v[3] ^= in; + sipround(state, 2); + state->v[0] ^= in; } /** @@ -106,14 +110,15 @@ static inline void siphash_feed(uint64_t *v, uint64_t in) * @len: Total length of input data * @tail: Final data for the hash (<= 7 bytes) */ -static inline uint64_t siphash_final(uint64_t *v, size_t len, uint64_t tail) +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(v, b); - v[2] ^= 0xff; - sipround(v, 4); - return v[0] ^ v[1] ^ v[2] ^ v[3]; + siphash_feed(state, b); + state->v[2] ^= 0xff; + sipround(state, 4); + return state->v[0] ^ state->v[1] ^ state->v[2] ^ state->v[3]; } /** @@ -137,12 +142,11 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* cppcheck-suppress unusedFunction */ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) { - uint64_t v[4] = SIPHASH_INIT(k); - - siphash_feed(v, *(uint64_t *)in); + struct siphash_state state = SIPHASH_INIT(k); + siphash_feed(&state, *(uint64_t *)in); - return siphash_final(v, 8, 0); + return siphash_final(&state, 8, 0); } /** @@ -157,12 +161,12 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ /* cppcheck-suppress unusedFunction */ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) { + struct siphash_state state = SIPHASH_INIT(k); uint32_t *in32 = (uint32_t *)in; - uint64_t v[4] = SIPHASH_INIT(k); - siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); + siphash_feed(&state, (uint64_t)(*(in32 + 1)) << 32 | *in32); - return siphash_final(v, 12, *(in32 + 2)); + return siphash_final(&state, 12, *(in32 + 2)); } /** @@ -176,14 +180,14 @@ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k) { + struct siphash_state state = SIPHASH_INIT(k); uint32_t *in32 = (uint32_t *)in; - uint64_t v[4] = SIPHASH_INIT(k); int i; for (i = 0; i < 2; i++, in32 += 2) - siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); + siphash_feed(&state, (uint64_t)(*(in32 + 1)) << 32 | *in32); - return siphash_final(v, 20, *in32); + return siphash_final(&state, 20, *in32); } /** @@ -198,14 +202,14 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ /* cppcheck-suppress unusedFunction */ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) { + struct siphash_state state = SIPHASH_INIT(k); uint64_t *in64 = (uint64_t *)in; - uint64_t v[4] = SIPHASH_INIT(k); int i; for (i = 0; i < 4; i++, in64++) - siphash_feed(v, *in64); + siphash_feed(&state, *in64); - return siphash_final(v, 32, 0); + return siphash_final(&state, 32, 0); } /** @@ -219,12 +223,12 @@ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_36b(const uint8_t *in, const uint64_t *k) { + struct siphash_state state = SIPHASH_INIT(k); uint32_t *in32 = (uint32_t *)in; - uint64_t v[4] = SIPHASH_INIT(k); int i; for (i = 0; i < 4; i++, in32 += 2) - siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); + siphash_feed(&state, (uint64_t)(*(in32 + 1)) << 32 | *in32); - return siphash_final(v, 36, *in32); + return siphash_final(&state, 36, *in32); } -- 2.41.0