Skip to content
Snippets Groups Projects
  • Jason A. Donenfeld's avatar
    ba7df77b
    siphash: implement HalfSipHash1-3 for hash tables · ba7df77b
    Jason A. Donenfeld authored
    
    commit 1ae2324f upstream.
    
    HalfSipHash, or hsiphash, is a shortened version of SipHash, which
    generates 32-bit outputs using a weaker 64-bit key. It has *much* lower
    security margins, and shouldn't be used for anything too sensitive, but
    it could be used as a hashtable key function replacement, if the output
    is never exposed, and if the security requirement is not too high.
    
    The goal is to make this something that performance-critical jhash users
    would be willing to use.
    
    On 64-bit machines, HalfSipHash1-3 is slower than SipHash1-3, so we alias
    SipHash1-3 to HalfSipHash1-3 on those systems.
    
    64-bit x86_64:
    [    0.509409] test_siphash:     SipHash2-4 cycles: 4049181
    [    0.510650] test_siphash:     SipHash1-3 cycles: 2512884
    [    0.512205] test_siphash: HalfSipHash1-3 cycles: 3429920
    [    0.512904] test_siphash:    JenkinsHash cycles:  978267
    So, we map hsiphash() -> SipHash1-3
    
    32-bit x86:
    [    0.509868] test_siphash:     SipHash2-4 cycles: 14812892
    [    0.513601] test_siphash:     SipHash1-3 cycles:  9510710
    [    0.515263] test_siphash: HalfSipHash1-3 cycles:  3856157
    [    0.515952] test_siphash:    JenkinsHash cycles:  1148567
    So, we map hsiphash() -> HalfSipHash1-3
    
    hsiphash() is roughly 3 times slower than jhash(), but comes with a
    considerable security improvement.
    
    Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
    Reviewed-by: default avatarJean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
    Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
    [bwh: Backported to 3.16 to avoid a build regression for WireGuard with
     only part of the siphash API available]
    Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
    ba7df77b
    History
    siphash: implement HalfSipHash1-3 for hash tables
    Jason A. Donenfeld authored
    
    commit 1ae2324f upstream.
    
    HalfSipHash, or hsiphash, is a shortened version of SipHash, which
    generates 32-bit outputs using a weaker 64-bit key. It has *much* lower
    security margins, and shouldn't be used for anything too sensitive, but
    it could be used as a hashtable key function replacement, if the output
    is never exposed, and if the security requirement is not too high.
    
    The goal is to make this something that performance-critical jhash users
    would be willing to use.
    
    On 64-bit machines, HalfSipHash1-3 is slower than SipHash1-3, so we alias
    SipHash1-3 to HalfSipHash1-3 on those systems.
    
    64-bit x86_64:
    [    0.509409] test_siphash:     SipHash2-4 cycles: 4049181
    [    0.510650] test_siphash:     SipHash1-3 cycles: 2512884
    [    0.512205] test_siphash: HalfSipHash1-3 cycles: 3429920
    [    0.512904] test_siphash:    JenkinsHash cycles:  978267
    So, we map hsiphash() -> SipHash1-3
    
    32-bit x86:
    [    0.509868] test_siphash:     SipHash2-4 cycles: 14812892
    [    0.513601] test_siphash:     SipHash1-3 cycles:  9510710
    [    0.515263] test_siphash: HalfSipHash1-3 cycles:  3856157
    [    0.515952] test_siphash:    JenkinsHash cycles:  1148567
    So, we map hsiphash() -> HalfSipHash1-3
    
    hsiphash() is roughly 3 times slower than jhash(), but comes with a
    considerable security improvement.
    
    Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
    Reviewed-by: default avatarJean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
    Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
    [bwh: Backported to 3.16 to avoid a build regression for WireGuard with
     only part of the siphash API available]
    Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
siphash.c 11.70 KiB
/* Copyright (C) 2016 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
 *
 * This file is provided under a dual BSD/GPLv2 license.
 *
 * SipHash: a fast short-input PRF
 * https://131002.net/siphash/
 *
 * This implementation is specifically for SipHash2-4 for a secure PRF
 * and HalfSipHash1-3/SipHash1-3 for an insecure PRF only suitable for
 * hashtables.
 */

#include <linux/siphash.h>
#include <asm/unaligned.h>

#if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
#include <linux/dcache.h>
#include <asm/word-at-a-time.h>
#endif

#define SIPROUND \
	do { \
	v0 += v1; v1 = rol64(v1, 13); v1 ^= v0; v0 = rol64(v0, 32); \
	v2 += v3; v3 = rol64(v3, 16); v3 ^= v2; \
	v0 += v3; v3 = rol64(v3, 21); v3 ^= v0; \
	v2 += v1; v1 = rol64(v1, 17); v1 ^= v2; v2 = rol64(v2, 32); \
	} while (0)

#define PREAMBLE(len) \
	u64 v0 = 0x736f6d6570736575ULL; \
	u64 v1 = 0x646f72616e646f6dULL; \
	u64 v2 = 0x6c7967656e657261ULL; \
	u64 v3 = 0x7465646279746573ULL; \
	u64 b = ((u64)(len)) << 56; \
	v3 ^= key->key[1]; \
	v2 ^= key->key[0]; \
	v1 ^= key->key[1]; \
	v0 ^= key->key[0];

#define POSTAMBLE \
	v3 ^= b; \
	SIPROUND; \
	SIPROUND; \
	v0 ^= b; \
	v2 ^= 0xff; \
	SIPROUND; \
	SIPROUND; \
	SIPROUND; \
	SIPROUND; \
	return (v0 ^ v1) ^ (v2 ^ v3);

u64 __siphash_aligned(const void *data, size_t len, const siphash_key_t *key)
{
	const u8 *end = data + len - (len % sizeof(u64));
	const u8 left = len & (sizeof(u64) - 1);
	u64 m;
	PREAMBLE(len)
	for (; data != end; data += sizeof(u64)) {
		m = le64_to_cpup(data);
		v3 ^= m;
		SIPROUND;
		SIPROUND;
		v0 ^= m;
	}
#if defined(CONFIG_DCACHE_WORD_ACCESS) && BITS_PER_LONG == 64
	if (left)
		b |= le64_to_cpu((__force __le64)(load_unaligned_zeropad(data) &
						  bytemask_from_count(left)));
#else
	switch (left) {