aboutgitcodebugslistschat
diff options
context:
space:
mode:
authorDavid Gibson <david@gibson.dropbear.id.au>2023-09-28 11:20:54 +1000
committerStefano Brivio <sbrivio@redhat.com>2023-09-30 12:40:34 +0200
commitf7b2be2d2121c403e39586d75af8c34ec545df21 (patch)
treecfd8eb8c6f0bc4b9996983659751fec58eef3d43
parentca6e94702cfbe29b279cf18f39a0fe492fae6f83 (diff)
downloadpasst-f7b2be2d2121c403e39586d75af8c34ec545df21.tar
passt-f7b2be2d2121c403e39586d75af8c34ec545df21.tar.gz
passt-f7b2be2d2121c403e39586d75af8c34ec545df21.tar.bz2
passt-f7b2be2d2121c403e39586d75af8c34ec545df21.tar.lz
passt-f7b2be2d2121c403e39586d75af8c34ec545df21.tar.xz
passt-f7b2be2d2121c403e39586d75af8c34ec545df21.tar.zst
passt-f7b2be2d2121c403e39586d75af8c34ec545df21.zip
siphash: Make sip round calculations an inline function rather than macro
The SIPROUND(n) macro implements n rounds of SipHash shuffling. It relies on 'v' and '__i' variables being available in the context it's used in which isn't great hygeine. Replace it with an inline function instead. Signed-off-by: David Gibson <david@gibson.dropbear.id.au> Signed-off-by: Stefano Brivio <sbrivio@redhat.com>
-rw-r--r--siphash.c51
1 files changed, 29 insertions, 22 deletions
diff --git a/siphash.c b/siphash.c
index 20009fe..e1fcf18 100644
--- a/siphash.c
+++ b/siphash.c
@@ -68,29 +68,36 @@
v[__i] = k[__i % 2]; \
} while (0)
-#define SIPROUND(n) \
- do { \
- 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); \
- } \
- } while (0)
+/**
+ * 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)
+{
+ 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);
+ }
+}
#define POSTAMBLE \
do { \
v[3] ^= b; \
- SIPROUND(2); \
+ sipround(v, 2); \
v[0] ^= b; \
v[2] ^= 0xff; \
- SIPROUND(4); \
+ sipround(v, 4); \
b = (v[0] ^ v[1]) ^ (v[2] ^ v[3]); \
} while (0)
@@ -117,7 +124,7 @@ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k)
{
PREAMBLE(8);
v[3] ^= *(uint64_t *)in;
- SIPROUND(2);
+ sipround(v, 2);
v[0] ^= *(uint64_t *)in;
POSTAMBLE;
@@ -143,7 +150,7 @@ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k)
PREAMBLE(12);
v[3] ^= combined;
- SIPROUND(2);
+ sipround(v, 2);
v[0] ^= combined;
b |= *(in32 + 2);
POSTAMBLE;
@@ -171,7 +178,7 @@ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k)
uint64_t combined = (uint64_t)(*(in32 + 1)) << 32 | *in32;
v[3] ^= combined;
- SIPROUND(2);
+ sipround(v, 2);
v[0] ^= combined;
}
@@ -200,7 +207,7 @@ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k)
for (i = 0; i < 4; i++, in64++) {
v[3] ^= *in64;
- SIPROUND(2);
+ sipround(v, 2);
v[0] ^= *in64;
}
@@ -229,7 +236,7 @@ uint64_t siphash_36b(const uint8_t *in, const uint64_t *k)
uint64_t combined = (uint64_t)(*(in32 + 1)) << 32 | *in32;
v[3] ^= combined;
- SIPROUND(2);
+ sipround(v, 2);
v[0] ^= combined;
}