This commit is contained in:
@@ -0,0 +1,177 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "siphash.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/assert/assert.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
#include <stdint.h>
|
||||
|
||||
wp_intern inline void _siphash(WpStream *bytes, const WpSipHasherParams *params, u8 *out, u64 outlen);
|
||||
wp_intern inline void _validate128IO(const WpSipHasher128IO *io);
|
||||
wp_intern inline void _validate64IO(const WpSipHasher64IO *io);
|
||||
wp_intern inline b8 _checkRounds(const WpSipHasherParams *params);
|
||||
|
||||
#define _u32To8LE(p, v) \
|
||||
(p)[0] = (u8)((v)); \
|
||||
(p)[1] = (u8)((v) >> 8); \
|
||||
(p)[2] = (u8)((v) >> 16); \
|
||||
(p)[3] = (u8)((v) >> 24);
|
||||
|
||||
#define _u64To8LE(p, v) \
|
||||
_u32To8LE((p), (u32)((v))); \
|
||||
_u32To8LE((p) + 4, (u32)((v) >> 32));
|
||||
|
||||
#define _u8To64LE(p) \
|
||||
(((u64)((p)[0])) | ((u64)((p)[1]) << 8) | \
|
||||
((u64)((p)[2]) << 16) | ((u64)((p)[3]) << 24) | \
|
||||
((u64)((p)[4]) << 32) | ((u64)((p)[5]) << 40) | \
|
||||
((u64)((p)[6]) << 48) | ((u64)((p)[7]) << 56))
|
||||
|
||||
#define _sipRound \
|
||||
do { \
|
||||
v0 += v1; \
|
||||
v1 = wpMiscUtilsRotl64(v1, 13); \
|
||||
v1 ^= v0; \
|
||||
v0 = wpMiscUtilsRotl64(v0, 32); \
|
||||
v2 += v3; \
|
||||
v3 = wpMiscUtilsRotl64(v3, 16); \
|
||||
v3 ^= v2; \
|
||||
v0 += v3; \
|
||||
v3 = wpMiscUtilsRotl64(v3, 21); \
|
||||
v3 ^= v0; \
|
||||
v2 += v1; \
|
||||
v1 = wpMiscUtilsRotl64(v1, 17); \
|
||||
v1 ^= v2; \
|
||||
v2 = wpMiscUtilsRotl64(v2, 32); \
|
||||
} while (0)
|
||||
|
||||
void wpSipHasher128(WpStream *bytes, void *hasher_io) {
|
||||
WpSipHasher128IO *io = (WpSipHasher128IO *)hasher_io;
|
||||
_validate128IO(io);
|
||||
_siphash(bytes, &io->params, (u8 *)&io->hash, sizeof(u64) * 2);
|
||||
}
|
||||
|
||||
void wpSipHasher64(WpStream *bytes, void *hasher_io) {
|
||||
WpSipHasher64IO *io = (WpSipHasher64IO *)hasher_io;
|
||||
_validate64IO(io);
|
||||
_siphash(bytes, &io->params, (u8 *)&io->hash, sizeof(u64));
|
||||
}
|
||||
|
||||
wp_intern inline void _siphash(WpStream *bytes, const WpSipHasherParams *params, u8 *out,
|
||||
u64 outlen) {
|
||||
const u8 *kk = (const u8 *)¶ms->key;
|
||||
u64 inlen = bytes->count * bytes->item_size;
|
||||
|
||||
wpRuntimeAssert((outlen == 8) || (outlen == 16), "outlen should be 8 or 16");
|
||||
|
||||
u64 v0 = UINT64_C(0x736f6d6570736575);
|
||||
u64 v1 = UINT64_C(0x646f72616e646f6d);
|
||||
u64 v2 = UINT64_C(0x6c7967656e657261);
|
||||
u64 v3 = UINT64_C(0x7465646279746573);
|
||||
u64 k0 = _u8To64LE(kk);
|
||||
u64 k1 = _u8To64LE(kk + 8);
|
||||
u64 m;
|
||||
u64 i;
|
||||
const i32 left = inlen & 7;
|
||||
u64 b = ((u64)inlen) << 56;
|
||||
v3 ^= k1;
|
||||
v2 ^= k0;
|
||||
v1 ^= k1;
|
||||
v0 ^= k0;
|
||||
|
||||
if (outlen == 16) {
|
||||
v1 ^= 0xee;
|
||||
}
|
||||
|
||||
u8 *data;
|
||||
while (wpStreamHasCount(bytes, sizeof(u64))) {
|
||||
data = wpStreamConsumeCount(u8, bytes, sizeof(u64));
|
||||
m = _u8To64LE(data);
|
||||
v3 ^= m;
|
||||
|
||||
for (i = 0; i < params->compression; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
v0 ^= m;
|
||||
}
|
||||
|
||||
data = wpStreamConsume(u8, bytes);
|
||||
switch (left) {
|
||||
case 7:
|
||||
b |= ((u64)data[6]) << 48;
|
||||
/* FALLTHRU */
|
||||
case 6:
|
||||
b |= ((u64)data[5]) << 40;
|
||||
/* FALLTHRU */
|
||||
case 5:
|
||||
b |= ((u64)data[4]) << 32;
|
||||
/* FALLTHRU */
|
||||
case 4:
|
||||
b |= ((u64)data[3]) << 24;
|
||||
/* FALLTHRU */
|
||||
case 3:
|
||||
b |= ((u64)data[2]) << 16;
|
||||
/* FALLTHRU */
|
||||
case 2:
|
||||
b |= ((u64)data[1]) << 8;
|
||||
/* FALLTHRU */
|
||||
case 1:
|
||||
b |= ((u64)data[0]);
|
||||
break;
|
||||
case 0:
|
||||
break;
|
||||
}
|
||||
|
||||
v3 ^= b;
|
||||
|
||||
for (i = 0; i < params->compression; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
v0 ^= b;
|
||||
|
||||
if (outlen == 16) {
|
||||
v2 ^= 0xee;
|
||||
} else {
|
||||
v2 ^= 0xff;
|
||||
}
|
||||
|
||||
for (i = 0; i < params->finalisation; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
b = v0 ^ v1 ^ v2 ^ v3;
|
||||
_u64To8LE(out, b);
|
||||
|
||||
if (outlen == 8) {
|
||||
return;
|
||||
}
|
||||
|
||||
v1 ^= 0xdd;
|
||||
|
||||
for (i = 0; i < params->finalisation; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
b = v0 ^ v1 ^ v2 ^ v3;
|
||||
_u64To8LE(out + 8, b);
|
||||
}
|
||||
|
||||
wp_intern inline void _validate128IO(const WpSipHasher128IO *io) {
|
||||
b8 magic_match = io->params.magic == WP_SIP_HASHER_128_MAGIC;
|
||||
b8 rounds = _checkRounds(&io->params);
|
||||
wpRuntimeAssert(magic_match && rounds, "Invalid Siphash 128 IO");
|
||||
}
|
||||
|
||||
wp_intern inline void _validate64IO(const WpSipHasher64IO *io) {
|
||||
b8 magic_match = io->params.magic == WP_SIP_HASHER_64_MAGIC;
|
||||
b8 rounds = _checkRounds(&io->params);
|
||||
wpRuntimeAssert(magic_match && rounds, "Invalid Siphash 64 IO");
|
||||
}
|
||||
|
||||
wp_intern inline b8 _checkRounds(const WpSipHasherParams *params) {
|
||||
b8 rounds24 = params->compression == 2 && params->finalisation == 4;
|
||||
b8 rounds48 = params->compression == 4 && params->finalisation == 8;
|
||||
return rounds24 || rounds48;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
/**
|
||||
* Implementations of the SipHash-2-4-128, SipHash4-8-128, SipHash-2-4-64 and SipHash4-8-64
|
||||
* algorithms based on https://github.com/veorq/SipHash/blob/master/siphash.c
|
||||
*/
|
||||
|
||||
#ifndef SIPHASH_H
|
||||
#define SIPHASH_H
|
||||
|
||||
#include "../../stream/stream.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
#define WP_SIP_HASHER_128_MAGIC ((u64)0x5750534950313238)
|
||||
#define WP_SIP_HASHER_64_MAGIC ((u64)0x57505349503634)
|
||||
|
||||
typedef struct {
|
||||
u64 lo;
|
||||
u64 hi;
|
||||
} WpSipHasherKey;
|
||||
|
||||
typedef struct {
|
||||
WpSipHasherKey key;
|
||||
u64 magic;
|
||||
u64 compression;
|
||||
u64 finalisation;
|
||||
} WpSipHasherParams;
|
||||
|
||||
typedef struct {
|
||||
WpSipHasherParams params;
|
||||
u64 hash[2];
|
||||
} WpSipHasher128IO;
|
||||
|
||||
typedef struct {
|
||||
WpSipHasherParams params;
|
||||
u64 hash;
|
||||
} WpSipHasher64IO;
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
#define wpSip24Hasher128IO(KEY) (WpSipHasher128IO{ \
|
||||
WpSipHasherParams{KEY, WP_SIP_HASHER_128_MAGIC, 2, 4}, \
|
||||
{}, \
|
||||
})
|
||||
#define wpSip48Hasher128IO(KEY) (WpSipHasher128IO{ \
|
||||
WpSipHasherParams{KEY, WP_SIP_HASHER_128_MAGIC, 4, 8}, \
|
||||
{}, \
|
||||
})
|
||||
#define wpSip24Hasher64IO(KEY) (WpSipHasher64IO{ \
|
||||
WpSipHasherParams{KEY, WP_SIP_HASHER_64_MAGIC, 2, 4}, \
|
||||
0, \
|
||||
})
|
||||
#define wpSip48Hasher64IO(KEY) (WpSipHasher64IO{ \
|
||||
WpSipHasherParams{KEY, WP_SIP_HASHER_64_MAGIC, 4, 8}, \
|
||||
0, \
|
||||
})
|
||||
#else
|
||||
#define wpSip24Hasher128IO(KEY) ((WpSipHasher128IO){ \
|
||||
.params = (WpSipHasherParams){ \
|
||||
.key = KEY, \
|
||||
.magic = WP_SIP_HASHER_128_MAGIC, \
|
||||
.compression = 2, \
|
||||
.finalisation = 4, \
|
||||
} \
|
||||
})
|
||||
#define wpSip48Hasher128IO(KEY) ((WpSipHasher128IO){ \
|
||||
.params = (WpSipHasherParams){ \
|
||||
.key = KEY, \
|
||||
.magic = WP_SIP_HASHER_128_MAGIC, \
|
||||
.compression = 4, \
|
||||
.finalisation = 8, \
|
||||
} \
|
||||
})
|
||||
#define wpSip24Hasher64IO(KEY) ((WpSipHasher64IO){ \
|
||||
.params = (WpSipHasherParams){ \
|
||||
.key = KEY, \
|
||||
.magic = WP_SIP_HASHER_64_MAGIC, \
|
||||
.compression = 2, \
|
||||
.finalisation = 4, \
|
||||
} \
|
||||
})
|
||||
#define wpSip48Hasher64IO(KEY) ((WpSipHasher64IO){ \
|
||||
.params = (WpSipHasherParams){ \
|
||||
.key = KEY, \
|
||||
.magic = WP_SIP_HASHER_64_MAGIC, \
|
||||
.compression = 4, \
|
||||
.finalisation = 8, \
|
||||
} \
|
||||
})
|
||||
#endif
|
||||
|
||||
void wpSipHasher128(WpU8Stream *bytes, void *hasher_io);
|
||||
void wpSipHasher64(WpU8Stream *bytes, void *hasher_io);
|
||||
|
||||
#ifdef WP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WP_PLATFORM_CPP
|
||||
|
||||
#endif // !SIPHASH_H
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "array/array.c"
|
||||
#include "dbl_list/dbl_list.c"
|
||||
#include "hash/hasher/murmur3.c"
|
||||
#include "hash/hasher/siphash.c"
|
||||
#include "queue/queue.c"
|
||||
#include "stream/stream.c"
|
||||
#include "mem/allocator/mem_allocator.c"
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "dbl_list/dbl_list.h"
|
||||
#include "hash/hasher/hasher.h"
|
||||
#include "hash/hasher/murmur3.h"
|
||||
#include "hash/hasher/siphash.h"
|
||||
#include "queue/queue.h"
|
||||
#include "stream/stream.h"
|
||||
#include "mem/allocator/mem_allocator.h"
|
||||
|
||||
Reference in New Issue
Block a user