1 parent
6a3326e9a5
commit
f3a8045ecc
8 files changed
+150
-168
No files matched your search
@@ -1,14 +1,14 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "siphash.h"
|
||||
#include "hasher.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/assert/assert.h"
|
||||
#include "../../../common/misc/misc_utils.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);
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out, u64 outlen);
|
||||
wp_intern inline void validateSipHasherData(const WpSipHasherData *io);
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherData *params);
|
||||
|
||||
#define _u32To8LE(p, v) \
|
||||
(p)[0] = (u8)((v)); \
|
||||
@@ -44,20 +44,24 @@ wp_intern inline b8 checkRounds(const WpSipHasherParams *params);
|
||||
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);
|
||||
WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) {
|
||||
WpSipHasherData *data = (WpSipHasherData *)hasher_data;
|
||||
validateSipHasherData(data);
|
||||
WpU128Hash output = {0};
|
||||
siphash(bytes, data, (u8 *)&output.hash, sizeof(u64) * 2);
|
||||
return output;
|
||||
}
|
||||
|
||||
void wpSipHasher64(WpStream *bytes, void *hasher_io) {
|
||||
WpSipHasher64IO *io = (WpSipHasher64IO *)hasher_io;
|
||||
validate64IO(io);
|
||||
siphash(bytes, &io->params, (u8 *)&io->hash, sizeof(u64));
|
||||
u64 wpSipHasher64(WpStream *bytes, void *hasher_data) {
|
||||
WpSipHasherData *data = (WpSipHasherData *)hasher_data;
|
||||
validateSipHasherData(data);
|
||||
u64 output = 0;
|
||||
siphash(bytes, data, (u8 *)&output, sizeof(u64));
|
||||
return output;
|
||||
}
|
||||
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherParams *params, u8 *out,
|
||||
u64 outlen) {
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherData *params, u8 *out,
|
||||
u64 outlen) {
|
||||
const u8 *kk = (const u8 *)¶ms->key;
|
||||
u64 inlen = bytes->count * bytes->item_size;
|
||||
|
||||
@@ -157,20 +161,14 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherParams *params,
|
||||
_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 validateSipHasherData(const WpSipHasherData *data) {
|
||||
b8 magic_match = data->magic == WP_SIP_HASHER_MAGIC;
|
||||
b8 rounds = checkRounds(data);
|
||||
wpRuntimeAssert(magic_match && rounds, "Invalid Siphash data");
|
||||
}
|
||||
|
||||
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;
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherData *data) {
|
||||
b8 rounds24 = data->compression == 2 && data->finalisation == 4;
|
||||
b8 rounds48 = data->compression == 4 && data->finalisation == 8;
|
||||
return rounds24 || rounds48;
|
||||
}
|
||||
Reference in new issue
Block a user