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 814435A0272 for ; Fri, 22 Sep 2023 16:06:41 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gibson.dropbear.id.au; s=201602; t=1695391594; bh=5derD9PBhQ8TVSOw/aX9btrjtPI+eqCxWj+UTsKcx30=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jWddMvTUrvMqtrms1A9errXSAqlaY0elWKFALNP6e5ytB/EaXsoQZudX7KpPRnqvw TTd/ION0DQS+8sdkjSE/A6bGJHotfn4gwr+0NiQKU7fVnydgxGuGOZ0fecQMbCSa+V 1d7P/Gx1JIHHTaZu37LyCoGv4gdL2UtUvD8C+Kck= Received: by gandalf.ozlabs.org (Postfix, from userid 1007) id 4RsYwV17Plz4xNq; Sat, 23 Sep 2023 00:06:34 +1000 (AEST) From: David Gibson To: Stefano Brivio , passt-dev@passt.top Subject: [PATCH 06/10] siphash: Use more hygienic state initialiser Date: Sat, 23 Sep 2023 00:06:26 +1000 Message-ID: <20230922140630.3184256-7-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: 2TNMHIDJ7XXAM5TAV3VAGP2DD7L3P3PQ X-Message-ID-Hash: 2TNMHIDJ7XXAM5TAV3VAGP2DD7L3P3PQ 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: The PREAMBLE macro sets up the SipHash initial internal state. It also defines that state as a variable, which isn't macro hygeinic. With previous changes simplifying this premable, it's now possible to replace it with a macro which simply expands to a C initialisedrfor that state. Signed-off-by: David Gibson --- siphash.c | 29 ++++++++++++----------------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/siphash.c b/siphash.c index 6932da2..21c560d 100644 --- a/siphash.c +++ b/siphash.c @@ -58,15 +58,12 @@ #define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) -#define PREAMBLE \ - uint64_t v[4] = { 0x736f6d6570736575ULL, 0x646f72616e646f6dULL, \ - 0x6c7967656e657261ULL, 0x7465646279746573ULL }; \ - int __i; \ - \ - do { \ - for (__i = sizeof(v) / sizeof(v[0]) - 1; __i >= 0; __i--) \ - v[__i] ^= k[__i % 2]; \ - } while (0) +#define SIPHASH_INIT(k) { \ + 0x736f6d6570736575ULL ^ (k)[0], \ + 0x646f72616e646f6dULL ^ (k)[1], \ + 0x6c7967656e657261ULL ^ (k)[0], \ + 0x7465646279746573ULL ^ (k)[1] \ + } /** * sipround() - Perform rounds of SipHash scrambling @@ -140,7 +137,8 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* cppcheck-suppress unusedFunction */ uint64_t siphash_8b(const uint8_t *in, const uint64_t *k) { - PREAMBLE; + uint64_t v[4] = SIPHASH_INIT(k); + siphash_feed(v, *(uint64_t *)in); @@ -160,8 +158,8 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_12b(const uint8_t *in, const uint64_t *k) { uint32_t *in32 = (uint32_t *)in; + uint64_t v[4] = SIPHASH_INIT(k); - PREAMBLE; siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); return siphash_final(v, 12, *(in32 + 2)); @@ -179,10 +177,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_20b(const uint8_t *in, const uint64_t *k) { uint32_t *in32 = (uint32_t *)in; + uint64_t v[4] = SIPHASH_INIT(k); int i; - PREAMBLE; - for (i = 0; i < 2; i++, in32 += 2) siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); @@ -202,10 +199,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_32b(const uint8_t *in, const uint64_t *k) { uint64_t *in64 = (uint64_t *)in; + uint64_t v[4] = SIPHASH_INIT(k); int i; - PREAMBLE; - for (i = 0; i < 4; i++, in64++) siphash_feed(v, *in64); @@ -224,10 +220,9 @@ __attribute__((optimize("-fno-strict-aliasing"))) /* See siphash_8b() */ uint64_t siphash_36b(const uint8_t *in, const uint64_t *k) { uint32_t *in32 = (uint32_t *)in; + uint64_t v[4] = SIPHASH_INIT(k); int i; - PREAMBLE; - for (i = 0; i < 4; i++, in32 += 2) siphash_feed(v, (uint64_t)(*(in32 + 1)) << 32 | *in32); -- 2.41.0