From 2842a977979ee21e562bb9b8b6c1196664d03e6b Mon Sep 17 00:00:00 2001 From: Marko Kreen Date: Sat, 29 Dec 2012 18:37:52 +0200 Subject: [PATCH] usual/hashing/siphash: SipHash-2-4 New secure hash. --- Makefile | 1 + usual/hashing/siphash.c | 89 +++++++++++++++++++++++++++++++++++++++++ usual/hashing/siphash.h | 33 +++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 usual/hashing/siphash.c create mode 100644 usual/hashing/siphash.h diff --git a/Makefile b/Makefile index 54f9824..36b9698 100644 --- a/Makefile +++ b/Makefile @@ -35,6 +35,7 @@ libusual_la_SOURCES = usual/config.h.in \ usual/getopt.h usual/getopt.c \ usual/hashing/crc32.h usual/hashing/crc32.c \ usual/hashing/lookup3.h usual/hashing/lookup3.c \ + usual/hashing/siphash.h usual/hashing/siphash.c \ usual/hashtab-impl.h \ usual/heap.h usual/heap.c \ usual/list.h usual/list.c \ diff --git a/usual/hashing/siphash.c b/usual/hashing/siphash.c new file mode 100644 index 0000000..fcbdab7 --- /dev/null +++ b/usual/hashing/siphash.c @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2012 Marko Kreen + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include +#include + +#define SIP_ROUND1 \ + 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) +#define SIP_ROUND2 SIP_ROUND1; SIP_ROUND1 +#define SIP_ROUND4 SIP_ROUND2; SIP_ROUND2 +#define SIP_ROUNDS(n) SIP_ROUND ## n + +#define sip_compress(n) \ + do { \ + v3 ^= m; \ + SIP_ROUNDS(n); \ + v0 ^= m; \ + } while (0) + +#define sip_finalize(n) \ + do { \ + v2 ^= 0xff; \ + SIP_ROUNDS(n); \ + } while (0) + +uint64_t siphash24(const void *data, size_t len, uint64_t k0, uint64_t k1) +{ + const uint8_t *s = data; + const uint8_t *end = s + len - (len % 8); + uint64_t v0 = k0 ^ UINT64_C(0x736f6d6570736575); + uint64_t v1 = k1 ^ UINT64_C(0x646f72616e646f6d); + uint64_t v2 = k0 ^ UINT64_C(0x6c7967656e657261); + uint64_t v3 = k1 ^ UINT64_C(0x7465646279746573); + uint64_t m; + + for (; s < end; s += 8) { + m = le64dec(s); + sip_compress(2); + } + + m = (uint64_t)len << 56; + switch (len & 7) { + case 7: m |= (uint64_t)s[6] << 48; + case 6: m |= (uint64_t)s[5] << 40; + case 5: m |= (uint64_t)s[4] << 32; + case 4: m |= (uint64_t)s[3] << 24; + case 3: m |= (uint64_t)s[2] << 16; + case 2: m |= (uint64_t)s[1] << 8; + case 1: m |= (uint64_t)s[0]; break; + case 0: break; + } + sip_compress(2); + + sip_finalize(4); + return (v0 ^ v1 ^ v2 ^ v3); +} + +uint64_t siphash24_secure(const void *data, size_t len) +{ + static bool initialized; + static uint64_t k0, k1; + + if (!initialized) { + k0 = ((uint64_t)random() << 32) | random(); + k1 = ((uint64_t)random() << 32) | random(); + initialized = true; + } + + return siphash24(data, len, k0, k1); +} + diff --git a/usual/hashing/siphash.h b/usual/hashing/siphash.h new file mode 100644 index 0000000..60a9247 --- /dev/null +++ b/usual/hashing/siphash.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2012 Marko Kreen + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +/** + * @file + * + * SipHash-2-4 + */ +#ifndef _USUAL_HASHING_SIPHASH_H_ +#define _USUAL_HASHING_SIPHASH_H_ + +#include + +/** Calculate SipHash-2-4 checksum */ +uint64_t siphash24(const void *data, size_t len, uint64_t k0, uint64_t k1); + +uint64_t siphash24_secure(const void *data, size_t len); + +#endif + -- 2.39.5