123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- /* 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.
- */
- #include <linux/module.h>
- #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) {
- case 7: b |= ((u64)end[6]) << 48;
- case 6: b |= ((u64)end[5]) << 40;
- case 5: b |= ((u64)end[4]) << 32;
- case 4: b |= le32_to_cpup(data); break;
- case 3: b |= ((u64)end[2]) << 16;
- case 2: b |= le16_to_cpup(data); break;
- case 1: b |= end[0];
- }
- #endif
- POSTAMBLE
- }
- EXPORT_SYMBOL(__siphash_aligned);
- #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
- u64 __siphash_unaligned(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 = get_unaligned_le64(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) {
- case 7: b |= ((u64)end[6]) << 48;
- case 6: b |= ((u64)end[5]) << 40;
- case 5: b |= ((u64)end[4]) << 32;
- case 4: b |= get_unaligned_le32(end); break;
- case 3: b |= ((u64)end[2]) << 16;
- case 2: b |= get_unaligned_le16(end); break;
- case 1: b |= end[0];
- }
- #endif
- POSTAMBLE
- }
- EXPORT_SYMBOL(__siphash_unaligned);
- #endif
- /**
- * siphash_1u64 - compute 64-bit siphash PRF value of a u64
- * @first: first u64
- * @key: the siphash key
- */
- u64 siphash_1u64(const u64 first, const siphash_key_t *key)
- {
- PREAMBLE(8)
- v3 ^= first;
- SIPROUND;
- SIPROUND;
- v0 ^= first;
- POSTAMBLE
- }
- EXPORT_SYMBOL(siphash_1u64);
- /**
- * siphash_2u64 - compute 64-bit siphash PRF value of 2 u64
- * @first: first u64
- * @second: second u64
- * @key: the siphash key
- */
- u64 siphash_2u64(const u64 first, const u64 second, const siphash_key_t *key)
- {
- PREAMBLE(16)
- v3 ^= first;
- SIPROUND;
- SIPROUND;
- v0 ^= first;
- v3 ^= second;
- SIPROUND;
- SIPROUND;
- v0 ^= second;
- POSTAMBLE
- }
- EXPORT_SYMBOL(siphash_2u64);
- /**
- * siphash_3u64 - compute 64-bit siphash PRF value of 3 u64
- * @first: first u64
- * @second: second u64
- * @third: third u64
- * @key: the siphash key
- */
- u64 siphash_3u64(const u64 first, const u64 second, const u64 third,
- const siphash_key_t *key)
- {
- PREAMBLE(24)
- v3 ^= first;
- SIPROUND;
- SIPROUND;
- v0 ^= first;
- v3 ^= second;
- SIPROUND;
- SIPROUND;
- v0 ^= second;
- v3 ^= third;
- SIPROUND;
- SIPROUND;
- v0 ^= third;
- POSTAMBLE
- }
- EXPORT_SYMBOL(siphash_3u64);
- /**
- * siphash_4u64 - compute 64-bit siphash PRF value of 4 u64
- * @first: first u64
- * @second: second u64
- * @third: third u64
- * @forth: forth u64
- * @key: the siphash key
- */
- u64 siphash_4u64(const u64 first, const u64 second, const u64 third,
- const u64 forth, const siphash_key_t *key)
- {
- PREAMBLE(32)
- v3 ^= first;
- SIPROUND;
- SIPROUND;
- v0 ^= first;
- v3 ^= second;
- SIPROUND;
- SIPROUND;
- v0 ^= second;
- v3 ^= third;
- SIPROUND;
- SIPROUND;
- v0 ^= third;
- v3 ^= forth;
- SIPROUND;
- SIPROUND;
- v0 ^= forth;
- POSTAMBLE
- }
- EXPORT_SYMBOL(siphash_4u64);
- u64 siphash_1u32(const u32 first, const siphash_key_t *key)
- {
- PREAMBLE(4)
- b |= first;
- POSTAMBLE
- }
- EXPORT_SYMBOL(siphash_1u32);
- u64 siphash_3u32(const u32 first, const u32 second, const u32 third,
- const siphash_key_t *key)
- {
- u64 combined = (u64)second << 32 | first;
- PREAMBLE(12)
- v3 ^= combined;
- SIPROUND;
- SIPROUND;
- v0 ^= combined;
- b |= third;
- POSTAMBLE
- }
- EXPORT_SYMBOL(siphash_3u32);
|