From: David Gibson <david@gibson.dropbear.id.au>
To: Stefano Brivio <sbrivio@redhat.com>, passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 07/10] siphash: Use specific structure for internal state
Date: Sat, 23 Sep 2023 00:06:27 +1000 [thread overview]
Message-ID: <20230922140630.3184256-8-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20230922140630.3184256-1-david@gibson.dropbear.id.au>
To improve type safety, encapsulate the internal state of the SipHash
algorithm into a dedicated structure type.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
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);
}
--
@@ -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
next prev parent reply other threads:[~2023-09-22 14:06 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-09-22 14:06 [PATCH 00/10] siphash: cleanups and fixes David Gibson
2023-09-22 14:06 ` [PATCH 01/10] siphash: Make siphash functions consistently return 64-bit results David Gibson
2023-09-22 14:06 ` [PATCH 02/10] siphash: Make sip round calculations an inline function rather than macro David Gibson
2023-09-22 14:06 ` [PATCH 03/10] siphash: Add siphash_feed() helper David Gibson
2023-09-22 14:06 ` [PATCH 04/10] siphash: Clean up hash finalisation with posthash_final() function David Gibson
2023-09-22 14:06 ` [PATCH 05/10] siphash: Fix bug in state initialisation David Gibson
2023-09-22 14:06 ` [PATCH 06/10] siphash: Use more hygienic state initialiser David Gibson
2023-09-27 17:04 ` Stefano Brivio
2023-09-28 1:20 ` David Gibson
2023-09-29 15:19 ` Stefano Brivio
2023-09-22 14:06 ` David Gibson [this message]
2023-09-22 14:06 ` [PATCH 08/10] siphash: Make internal helpers public David Gibson
2023-09-22 14:06 ` [PATCH 09/10] siphash, checksum: Move TBAA explanation to checksum.c David Gibson
2023-09-22 14:06 ` [PATCH 10/10] siphash: Use incremental rather than all-at-once siphash functions David Gibson
2023-09-26 6:23 ` David Gibson
2023-09-26 7:02 ` David Gibson
2023-09-27 17:05 ` Stefano Brivio
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230922140630.3184256-8-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://passt.top/passt
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).