This commit is contained in:
1 parent
9881c0ac9a
commit
c215486442
9 files changed
+175
-144
No files matched your search
@@ -5,10 +5,11 @@
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/assert/assert.h"
|
||||
#include "../../../common/misc/misc_utils.h"
|
||||
#include "../../../prng/xorshift/xorshift.h"
|
||||
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out, u64 outlen);
|
||||
wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *io);
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherCtx *params);
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpHasherCtx *ctx, u8 *out, u64 outlen);
|
||||
wp_intern inline void validateSipHasherCtx(const WpHasherCtx *ctx);
|
||||
wp_intern inline b8 checkRounds(const WpHasherCtx *ctx);
|
||||
|
||||
#define _u32To8LE(p, v) \
|
||||
(p)[0] = (u8)((v)); \
|
||||
@@ -44,24 +45,50 @@ wp_intern inline b8 checkRounds(const WpSipHasherCtx *params);
|
||||
v2 = wpMiscUtilsRotl64(v2, 32); \
|
||||
} while (0)
|
||||
|
||||
WpHasher wpSipHasher(WpSipHasherCtx *data) {
|
||||
return (WpHasher){ .func = wpSipHasher128, .data = (void *)data };
|
||||
WpHasher wpSip24Hasher(void) {
|
||||
WpXor256State state = wpPrngXorshiftInit();
|
||||
WpU128 key = { .value = { wpPrngXorshift256(&state), wpPrngXorshift256(&state) } };
|
||||
return wpSip24HasherWithKey(key);
|
||||
}
|
||||
|
||||
WpU128Hash wpSipHasher128(WpStream *bytes, void *hasher_data) {
|
||||
WpSipHasherCtx *data = (WpSipHasherCtx *)hasher_data;
|
||||
validateSipHasherCtx(data);
|
||||
WpU128Hash output = {0};
|
||||
siphash(bytes, data, (u8 *)&output.hash, sizeof(u64) * 2);
|
||||
WpHasher wpSip48Hasher(void) {
|
||||
WpXor256State state = wpPrngXorshiftInit();
|
||||
WpU128 key = { .value = { wpPrngXorshift256(&state), wpPrngXorshift256(&state) } };
|
||||
return wpSip48HasherWithKey(key);
|
||||
}
|
||||
|
||||
WpHasher wpSip24HasherWithKey(WpU128 key) {
|
||||
return (WpHasher){
|
||||
.func = wpSipGetHash128,
|
||||
.ctx = (WpHasherCtx){
|
||||
.type = WP_HASHER_CTX_SIPHASH,
|
||||
.siphash = (WpSiphashCtx){ .key = key, .compression = 2, .finalisation = 4 },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
WpHasher wpSip48HasherWithKey(WpU128 key) {
|
||||
return (WpHasher){
|
||||
.func = wpSipGetHash128,
|
||||
.ctx = (WpHasherCtx){
|
||||
.type = WP_HASHER_CTX_SIPHASH,
|
||||
.siphash = (WpSiphashCtx){ .key = key, .compression = 4, .finalisation = 8 },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
WpU128 wpSipGetHash128(WpStream *bytes, WpHasherCtx ctx) {
|
||||
validateSipHasherCtx(&ctx);
|
||||
WpU128 output = {0};
|
||||
siphash(bytes, &ctx, (u8 *)&output.value, sizeof(u64) * 2);
|
||||
return output;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implementation of the 128-variant of the siphash algorithm
|
||||
*/
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8 *out,
|
||||
u64 outlen) {
|
||||
const u8 *kk = (const u8 *)¶ms->key;
|
||||
wp_intern inline void siphash(WpStream *bytes, const WpHasherCtx *ctx, u8 *out, u64 outlen) {
|
||||
const u8 *kk = (const u8 *)&ctx->siphash.key;
|
||||
u64 inlen = bytes->count * bytes->item_size;
|
||||
|
||||
wpRuntimeAssert(outlen == 16, "outlen should be 16");
|
||||
@@ -89,7 +116,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8
|
||||
m = _u8To64LE(data);
|
||||
v3 ^= m;
|
||||
|
||||
for (i = 0; i < params->compression; ++i) {
|
||||
for (i = 0; i < ctx->siphash.compression; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
@@ -125,7 +152,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8
|
||||
|
||||
v3 ^= b;
|
||||
|
||||
for (i = 0; i < params->compression; ++i) {
|
||||
for (i = 0; i < ctx->siphash.compression; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
@@ -133,7 +160,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8
|
||||
|
||||
v2 ^= 0xee;
|
||||
|
||||
for (i = 0; i < params->finalisation; ++i) {
|
||||
for (i = 0; i < ctx->siphash.finalisation; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
@@ -142,7 +169,7 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8
|
||||
|
||||
v1 ^= 0xdd;
|
||||
|
||||
for (i = 0; i < params->finalisation; ++i) {
|
||||
for (i = 0; i < ctx->siphash.finalisation; ++i) {
|
||||
_sipRound;
|
||||
}
|
||||
|
||||
@@ -150,14 +177,14 @@ wp_intern inline void siphash(WpStream *bytes, const WpSipHasherCtx *params, u8
|
||||
_u64To8LE(out + 8, b);
|
||||
}
|
||||
|
||||
wp_intern inline void validateSipHasherCtx(const WpSipHasherCtx *ctx) {
|
||||
b8 magic_match = ctx->magic == WP_SIP_HASHER_MAGIC;
|
||||
wp_intern inline void validateSipHasherCtx(const WpHasherCtx *ctx) {
|
||||
b8 magic_match = ctx->type == WP_HASHER_CTX_SIPHASH;
|
||||
b8 rounds = checkRounds(ctx);
|
||||
wpRuntimeAssert(magic_match && rounds, "Invalid Siphash context");
|
||||
}
|
||||
|
||||
wp_intern inline b8 checkRounds(const WpSipHasherCtx *ctx) {
|
||||
b8 rounds24 = ctx->compression == 2 && ctx->finalisation == 4;
|
||||
b8 rounds48 = ctx->compression == 4 && ctx->finalisation == 8;
|
||||
wp_intern inline b8 checkRounds(const WpHasherCtx *ctx) {
|
||||
b8 rounds24 = ctx->siphash.compression == 2 && ctx->siphash.finalisation == 4;
|
||||
b8 rounds48 = ctx->siphash.compression == 4 && ctx->siphash.finalisation == 8;
|
||||
return rounds24 || rounds48;
|
||||
}
|
||||
Reference in new issue
Block a user