Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754801AbcLNEAB (ORCPT ); Tue, 13 Dec 2016 23:00:01 -0500 Received: from frisell.zx2c4.com ([192.95.5.64]:60423 "EHLO frisell.zx2c4.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753230AbcLND7z (ORCPT ); Tue, 13 Dec 2016 22:59:55 -0500 From: "Jason A. Donenfeld" To: Netdev , kernel-hardening@lists.openwall.com, LKML , linux-crypto@vger.kernel.org Cc: "Jason A. Donenfeld" Subject: [PATCH v2 2/4] siphash: add convenience functions for jhash converts Date: Wed, 14 Dec 2016 04:59:25 +0100 Message-Id: <20161214035927.30004-2-Jason@zx2c4.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20161214035927.30004-1-Jason@zx2c4.com> References: <20161214035927.30004-1-Jason@zx2c4.com> Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 1516 Lines: 58 Many jhash users currently rely on the Nwords functions. In order to make transitions to siphash fit something people already know about, we provide analog functions here. This also winds up being nice for the networking stack, where hashing 32-bit fields is common. Signed-off-by: Jason A. Donenfeld --- Changes from v1->v2: - None in this patch, but see elsewhere in series. include/linux/siphash.h | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/include/linux/siphash.h b/include/linux/siphash.h index 6623b3090645..1391054c4c29 100644 --- a/include/linux/siphash.h +++ b/include/linux/siphash.h @@ -17,4 +17,37 @@ enum siphash_lengths { u64 siphash24(const u8 *data, size_t len, const u8 key[SIPHASH24_KEY_LEN]); +static inline u64 siphash24_1word(const u32 a, const u8 key[SIPHASH24_KEY_LEN]) +{ + return siphash24((u8 *)&a, sizeof(a), key); +} + +static inline u64 siphash24_2words(const u32 a, const u32 b, const u8 key[SIPHASH24_KEY_LEN]) +{ + const struct { + u32 a; + u32 b; + } __packed combined = { + .a = a, + .b = b + }; + + return siphash24((const u8 *)&combined, sizeof(combined), key); +} + +static inline u64 siphash24_3words(const u32 a, const u32 b, const u32 c, const u8 key[SIPHASH24_KEY_LEN]) +{ + const struct { + u32 a; + u32 b; + u32 c; + } __packed combined = { + .a = a, + .b = b, + .c = c + }; + + return siphash24((const u8 *)&combined, sizeof(combined), key); +} + #endif /* _LINUX_SIPHASH_H */ -- 2.11.0