librz/hash: add murmur3 hash function (without keys support) (#6002)
This commit is contained in:
parent
cc8a02949e
commit
f6344debf8
14 changed files with 899 additions and 98 deletions
|
|
@ -52,16 +52,16 @@
|
|||
* \brief Rotates a 64bit value. Rotate \p x left by \p y bits.
|
||||
* \p y should be U8, \p x should be U32 and U64 respectively.
|
||||
*/
|
||||
#define ROTL64(x, y) LET("rotl64_x", x, \
|
||||
#define PPC_IL_ROTL64(x, y) LET("rotl64_x", x, \
|
||||
LET("rotl64_y", y, \
|
||||
(LOGOR(SHIFTL0(VARLP("rotl64_x"), VARLP("rotl64_y")), SHIFTR0(VARLP("rotl64_x"), SUB(U8(64), UNSIGNED(8, VARLP("rotl64_y"))))))))
|
||||
|
||||
/**
|
||||
* \brief Rotates a 32bit value. If the the VM is in 64bit mode "ROTL64(x||x, y)" is executed instead.
|
||||
*/
|
||||
#define ROTL32(x, y) LET("rotl32_x", x, \
|
||||
#define PPC_IL_ROTL32(x, y) LET("rotl32_x", x, \
|
||||
LET("rotl32_y", y, \
|
||||
(IN_64BIT_MODE ? ROTL64(APPEND(VARLP("rotl32_x"), VARLP("rotl32_x")), VARLP("rotl32_y")) \
|
||||
(IN_64BIT_MODE ? PPC_IL_ROTL64(APPEND(VARLP("rotl32_x"), VARLP("rotl32_x")), VARLP("rotl32_y")) \
|
||||
: LOGOR(SHIFTL0(VARLP("rotl32_x"), VARLP("rotl32_y")), SHIFTR0(VARLP("rotl32_x"), SUB(U8(32), UNSIGNED(8, VARLP("rotl32_y"))))))))
|
||||
|
||||
/**
|
||||
|
|
|
|||
|
|
@ -1204,7 +1204,7 @@ static RzILOpEffect *shift_and_rotate(RZ_BORROW csh handle, RZ_BORROW cs_insn *i
|
|||
} else {
|
||||
n = U8(sH);
|
||||
}
|
||||
r = ROTL32(UNSIGNED(32, VARG(rS)), n);
|
||||
r = PPC_IL_ROTL32(UNSIGNED(32, VARG(rS)), n);
|
||||
#if CS_NEXT_VERSION >= 6
|
||||
b = mB + 32;
|
||||
e = mE + 32;
|
||||
|
|
@ -1254,7 +1254,7 @@ static RzILOpEffect *shift_and_rotate(RZ_BORROW csh handle, RZ_BORROW cs_insn *i
|
|||
n = U8(sH);
|
||||
}
|
||||
n = LOGAND(U8(0x3f), n);
|
||||
r = ROTL64(VARG(rS), n);
|
||||
r = PPC_IL_ROTL64(VARG(rS), n);
|
||||
#if CS_NEXT_VERSION >= 6
|
||||
if (id == PPC_INS_RLDICR || id == PPC_INS_RLDCR) {
|
||||
b = 0;
|
||||
|
|
|
|||
386
librz/hash/algorithms/murmur3/murmur3.c
Normal file
386
librz/hash/algorithms/murmur3/murmur3.c
Normal file
|
|
@ -0,0 +1,386 @@
|
|||
// SPDX-FileCopyrightText: 2015 Andrey Jivsov <crypto@brainhub.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/**
|
||||
* \file MurmurHash3.cpp
|
||||
* High performance non cryptographic hashing algorithm.
|
||||
* Implementation of the MurmurHash3 algorithm.
|
||||
* 3 variants of this algo are provided to hash 32,64 and 128 bit data
|
||||
* MurmurHash3 was written by Austin Appleby, and is placed in the public domain.
|
||||
* is engineered for speed and high-quality distribution,
|
||||
* passing the full SMHasher test suite. This makes it a go-to choice for
|
||||
* hash tables, bloom filters, and large-scale data de-duplication where
|
||||
* performance is the primary constraint.
|
||||
*
|
||||
* Note - The x86 and x64 versions do _not_ produce the same results, as the
|
||||
* algorithms are optimized for their respective platforms. You can still
|
||||
* compile and run any of them on any platform, but your performance with the
|
||||
* non-native version will be less than optimal.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "murmur3.h"
|
||||
#include "rz_types.h"
|
||||
#include "rz_types_base.h"
|
||||
#include <string.h>
|
||||
|
||||
static RZ_INLINE ut32 rotl32(ut32 x, int8_t r) {
|
||||
return (x << r) | (x >> (32 - r));
|
||||
}
|
||||
|
||||
static RZ_INLINE ut64 rotl64(ut64 x, int8_t r) {
|
||||
return (x << r) | (x >> (64 - r));
|
||||
}
|
||||
|
||||
/**
|
||||
* \brief Block read - if your platform needs to do endian-swapping or can only
|
||||
* handle aligned reads, do the conversion here
|
||||
*/
|
||||
#define getblock(p, i) \
|
||||
(sizeof(*(p)) == 8 ? rz_read_le64(p + i) : rz_read_le32(p + i))
|
||||
|
||||
/*
|
||||
* \brief Finalization mix - force all bits of a hash block to avalanche
|
||||
*/
|
||||
static RZ_INLINE ut32 fmix32(ut32 h) {
|
||||
h ^= h >> 16;
|
||||
h *= 0x85ebca6b;
|
||||
h ^= h >> 13;
|
||||
h *= 0xc2b2ae35;
|
||||
h ^= h >> 16;
|
||||
|
||||
return h;
|
||||
}
|
||||
|
||||
static RZ_INLINE ut64 fmix64(ut64 k) {
|
||||
k ^= k >> 33;
|
||||
k *= BIG_CONSTANT(0xff51afd7ed558ccd);
|
||||
k ^= k >> 33;
|
||||
k *= BIG_CONSTANT(0xc4ceb9fe1a85ec53);
|
||||
k ^= k >> 33;
|
||||
|
||||
return k;
|
||||
}
|
||||
|
||||
RZ_IPI void MurmurHash3_x86_32(const void *key, RzRef len, size_t seed, void *out) {
|
||||
const ut8 *data = (const ut8 *)key;
|
||||
const RzRef nblocks = len / 4;
|
||||
RzRef i;
|
||||
|
||||
ut32 h1 = seed;
|
||||
|
||||
ut32 c1 = 0xcc9e2d51;
|
||||
ut32 c2 = 0x1b873593;
|
||||
const ut32 *blocks = (const ut32 *)(data + nblocks * 4);
|
||||
|
||||
for (i = -nblocks; i; i++) {
|
||||
ut32 k1 = getblock(blocks, i);
|
||||
|
||||
k1 *= c1;
|
||||
k1 = ROTL32_Murmur3(k1, 15);
|
||||
k1 *= c2;
|
||||
|
||||
h1 ^= k1;
|
||||
h1 = ROTL32_Murmur3(h1, 13);
|
||||
h1 = h1 * 5 + 0xe6546b64;
|
||||
}
|
||||
|
||||
const ut8 *tail = (const ut8 *)(data + nblocks * 4);
|
||||
|
||||
ut32 k1 = 0;
|
||||
|
||||
switch (len & 3) {
|
||||
case 3:
|
||||
k1 ^= tail[2] << 16;
|
||||
/* fall through */
|
||||
case 2:
|
||||
k1 ^= tail[1] << 8;
|
||||
/* fall through */
|
||||
case 1:
|
||||
k1 ^= tail[0];
|
||||
k1 *= c1;
|
||||
k1 = ROTL32_Murmur3(k1, 15);
|
||||
k1 *= c2;
|
||||
h1 ^= k1;
|
||||
};
|
||||
|
||||
// finalization
|
||||
h1 ^= len;
|
||||
h1 = fmix32(h1);
|
||||
*(ut32 *)out = h1;
|
||||
}
|
||||
|
||||
RZ_IPI void MurmurHash3_x86_128(const void *key, const RzRef len, size_t seed, void *out) {
|
||||
const ut8 *data = (const ut8 *)key;
|
||||
const RzRef nblocks = len / 16;
|
||||
RzRef i;
|
||||
|
||||
size_t h1 = seed;
|
||||
size_t h2 = seed;
|
||||
size_t h3 = seed;
|
||||
size_t h4 = seed;
|
||||
|
||||
const size_t c1 = 0x239b961b;
|
||||
const size_t c2 = 0xab0e9789;
|
||||
const size_t c3 = 0x38b34ae5;
|
||||
const size_t c4 = 0xa1e38b93;
|
||||
|
||||
const ut32 *blocks = (const ut32 *)(data + nblocks * 16);
|
||||
|
||||
for (i = -nblocks; i; i++) {
|
||||
ut32 k1 = getblock(blocks, i * 4 + 0);
|
||||
ut32 k2 = getblock(blocks, i * 4 + 1);
|
||||
ut32 k3 = getblock(blocks, i * 4 + 2);
|
||||
ut32 k4 = getblock(blocks, i * 4 + 3);
|
||||
|
||||
k1 *= c1;
|
||||
k1 = ROTL32_Murmur3(k1, 15);
|
||||
k1 *= c2;
|
||||
h1 ^= k1;
|
||||
|
||||
h1 = ROTL32_Murmur3(h1, 19);
|
||||
h1 += h2;
|
||||
h1 = h1 * 5 + 0x561ccd1b;
|
||||
|
||||
k2 *= c2;
|
||||
k2 = ROTL32_Murmur3(k2, 16);
|
||||
k2 *= c3;
|
||||
h2 ^= k2;
|
||||
|
||||
h2 = ROTL32_Murmur3(h2, 17);
|
||||
h2 += h3;
|
||||
h2 = h2 * 5 + 0x0bcaa747;
|
||||
|
||||
k3 *= c3;
|
||||
k3 = ROTL32_Murmur3(k3, 17);
|
||||
k3 *= c4;
|
||||
h3 ^= k3;
|
||||
|
||||
h3 = ROTL32_Murmur3(h3, 15);
|
||||
h3 += h4;
|
||||
h3 = h3 * 5 + 0x96cd1c35;
|
||||
|
||||
k4 *= c4;
|
||||
k4 = ROTL32_Murmur3(k4, 18);
|
||||
k4 *= c1;
|
||||
h4 ^= k4;
|
||||
|
||||
h4 = ROTL32_Murmur3(h4, 13);
|
||||
h4 += h1;
|
||||
h4 = h4 * 5 + 0x32ac3b17;
|
||||
}
|
||||
|
||||
const ut8 *tail = (const ut8 *)(data + nblocks * 16);
|
||||
|
||||
ut32 k1 = 0;
|
||||
ut32 k2 = 0;
|
||||
ut32 k3 = 0;
|
||||
ut32 k4 = 0;
|
||||
|
||||
switch (len & 15) {
|
||||
case 15:
|
||||
k4 ^= tail[14] << 16;
|
||||
/* fall through */
|
||||
case 14:
|
||||
k4 ^= tail[13] << 8;
|
||||
/* fall through */
|
||||
case 13:
|
||||
k4 ^= tail[12] << 0;
|
||||
k4 *= c4;
|
||||
k4 = ROTL32_Murmur3(k4, 18);
|
||||
k4 *= c1;
|
||||
h4 ^= k4;
|
||||
/* fall through */
|
||||
|
||||
case 12:
|
||||
k3 ^= tail[11] << 24;
|
||||
/* fall through */
|
||||
case 11:
|
||||
k3 ^= tail[10] << 16;
|
||||
/* fall through */
|
||||
case 10:
|
||||
k3 ^= tail[9] << 8;
|
||||
/* fall through */
|
||||
case 9:
|
||||
k3 ^= tail[8] << 0;
|
||||
k3 *= c3;
|
||||
k3 = ROTL32_Murmur3(k3, 17);
|
||||
k3 *= c4;
|
||||
h3 ^= k3;
|
||||
/* fall through */
|
||||
|
||||
case 8:
|
||||
k2 ^= tail[7] << 24;
|
||||
/* fall through */
|
||||
case 7:
|
||||
k2 ^= tail[6] << 16;
|
||||
/* fall through */
|
||||
case 6:
|
||||
k2 ^= tail[5] << 8;
|
||||
/* fall through */
|
||||
case 5:
|
||||
k2 ^= tail[4] << 0;
|
||||
k2 *= c2;
|
||||
k2 = ROTL32_Murmur3(k2, 16);
|
||||
k2 *= c3;
|
||||
h2 ^= k2;
|
||||
/* fall through */
|
||||
|
||||
case 4:
|
||||
k1 ^= tail[3] << 24;
|
||||
/* fall through */
|
||||
case 3:
|
||||
k1 ^= tail[2] << 16;
|
||||
/* fall through */
|
||||
case 2:
|
||||
k1 ^= tail[1] << 8;
|
||||
/* fall through */
|
||||
case 1:
|
||||
k1 ^= tail[0] << 0;
|
||||
k1 *= c1;
|
||||
k1 = ROTL32_Murmur3(k1, 15);
|
||||
k1 *= c2;
|
||||
h1 ^= k1;
|
||||
};
|
||||
|
||||
h1 ^= len;
|
||||
h2 ^= len;
|
||||
h3 ^= len;
|
||||
h4 ^= len;
|
||||
|
||||
h1 += h2;
|
||||
h1 += h3;
|
||||
h1 += h4;
|
||||
h2 += h1;
|
||||
h3 += h1;
|
||||
h4 += h1;
|
||||
|
||||
h1 = fmix32(h1);
|
||||
h2 = fmix32(h2);
|
||||
h3 = fmix32(h3);
|
||||
h4 = fmix32(h4);
|
||||
|
||||
h1 += h2;
|
||||
h1 += h3;
|
||||
h1 += h4;
|
||||
h2 += h1;
|
||||
h3 += h1;
|
||||
h4 += h1;
|
||||
|
||||
((ut32 *)out)[0] = h1;
|
||||
((ut32 *)out)[1] = h2;
|
||||
((ut32 *)out)[2] = h3;
|
||||
((ut32 *)out)[3] = h4;
|
||||
}
|
||||
|
||||
RZ_IPI void MurmurHash3_x64_128(const void *key, const RzRef len, const ut64 seed, void *out) {
|
||||
const ut8 *data = (const ut8 *)key;
|
||||
const RzRef nblocks = len / 16;
|
||||
RzRef i;
|
||||
|
||||
ut64 h1 = seed;
|
||||
ut64 h2 = seed;
|
||||
|
||||
const ut64 c1 = BIG_CONSTANT(0x87c37b91114253d5);
|
||||
const ut64 c2 = BIG_CONSTANT(0x4cf5ad432745937f);
|
||||
|
||||
const ut64 *blocks = (const ut64 *)(data);
|
||||
|
||||
for (i = 0; i < nblocks; i++) {
|
||||
ut64 k1 = getblock(blocks, i * 2 + 0);
|
||||
ut64 k2 = getblock(blocks, i * 2 + 1);
|
||||
|
||||
k1 *= c1;
|
||||
k1 = ROTL64_Murmur3(k1, 31);
|
||||
k1 *= c2;
|
||||
h1 ^= k1;
|
||||
|
||||
h1 = ROTL64_Murmur3(h1, 27);
|
||||
h1 += h2;
|
||||
h1 = h1 * 5 + 0x52dce729;
|
||||
|
||||
k2 *= c2;
|
||||
k2 = ROTL64_Murmur3(k2, 33);
|
||||
k2 *= c1;
|
||||
h2 ^= k2;
|
||||
|
||||
h2 = ROTL64_Murmur3(h2, 31);
|
||||
h2 += h1;
|
||||
h2 = h2 * 5 + 0x38495ab5;
|
||||
}
|
||||
|
||||
const ut8 *tail = (const ut8 *)(data + nblocks * 16);
|
||||
|
||||
ut64 k1 = 0;
|
||||
ut64 k2 = 0;
|
||||
|
||||
switch (len & 15) {
|
||||
case 15:
|
||||
k2 ^= (ut64)(tail[14]) << 48;
|
||||
/* fall through */
|
||||
case 14:
|
||||
k2 ^= (ut64)(tail[13]) << 40;
|
||||
/* fall through */
|
||||
case 13:
|
||||
k2 ^= (ut64)(tail[12]) << 32;
|
||||
/* fall through */
|
||||
case 12:
|
||||
k2 ^= (ut64)(tail[11]) << 24;
|
||||
/* fall through */
|
||||
case 11:
|
||||
k2 ^= (ut64)(tail[10]) << 16;
|
||||
/* fall through */
|
||||
case 10:
|
||||
k2 ^= (ut64)(tail[9]) << 8;
|
||||
/* fall through */
|
||||
case 9:
|
||||
k2 ^= (ut64)(tail[8]) << 0;
|
||||
k2 *= c2;
|
||||
k2 = ROTL64_Murmur3(k2, 33);
|
||||
k2 *= c1;
|
||||
h2 ^= k2;
|
||||
/* fall through */
|
||||
case 8:
|
||||
k1 ^= (ut64)(tail[7]) << 56;
|
||||
/* fall through */
|
||||
case 7:
|
||||
k1 ^= (ut64)(tail[6]) << 48;
|
||||
/* fall through */
|
||||
case 6:
|
||||
k1 ^= (ut64)(tail[5]) << 40;
|
||||
/* fall through */
|
||||
case 5:
|
||||
k1 ^= (ut64)(tail[4]) << 32;
|
||||
/* fall through */
|
||||
case 4:
|
||||
k1 ^= (ut64)(tail[3]) << 24;
|
||||
/* fall through */
|
||||
case 3:
|
||||
k1 ^= (ut64)(tail[2]) << 16;
|
||||
/* fall through */
|
||||
case 2:
|
||||
k1 ^= (ut64)(tail[1]) << 8;
|
||||
/* fall through */
|
||||
case 1:
|
||||
k1 ^= (ut64)(tail[0]) << 0;
|
||||
k1 *= c1;
|
||||
k1 = ROTL64_Murmur3(k1, 31);
|
||||
k1 *= c2;
|
||||
h1 ^= k1;
|
||||
};
|
||||
|
||||
h1 ^= len;
|
||||
h2 ^= len;
|
||||
|
||||
h1 += h2;
|
||||
h2 += h1;
|
||||
|
||||
h1 = fmix64(h1);
|
||||
h2 = fmix64(h2);
|
||||
|
||||
h1 += h2;
|
||||
h2 += h1;
|
||||
|
||||
((ut64 *)out)[0] = h1;
|
||||
((ut64 *)out)[1] = h2;
|
||||
}
|
||||
22
librz/hash/algorithms/murmur3/murmur3.h
Normal file
22
librz/hash/algorithms/murmur3/murmur3.h
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
// SPDX-FileCopyrightText: 2015 Andrey Jivsov <crypto@brainhub.org>
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
// MurmurHash3 was written by Austin Appleby, and is placed in the public
|
||||
// domain. The author hereby disclaims copyright to this source code.
|
||||
|
||||
#ifndef _MURMURHASH3_H_
|
||||
#define _MURMURHASH3_H_
|
||||
|
||||
#include <rz_types.h>
|
||||
|
||||
#define MURMUR3_32_DIGEST_LENGTH 4
|
||||
#define MURMUR3_128_DIGEST_LENGTH 16
|
||||
|
||||
#define ROTL32_Murmur3(x, y) rotl32(x, y)
|
||||
#define ROTL64_Murmur3(x, y) rotl64(x, y)
|
||||
|
||||
RZ_IPI void MurmurHash3_x86_32(const void *key, RzRef len, size_t seed, void *out);
|
||||
RZ_IPI void MurmurHash3_x86_128(const void *key, RzRef len, size_t seed, void *out);
|
||||
RZ_IPI void MurmurHash3_x64_128(const void *key, RzRef len, ut64 seed, void *out);
|
||||
|
||||
#endif // _MURMURHASH3_H_
|
||||
|
|
@ -13,6 +13,9 @@ hash_plugins_list = [
|
|||
'keccak_256',
|
||||
'keccak_384',
|
||||
'keccak_512',
|
||||
'murmur3_x86_32',
|
||||
'murmur3_x86_128',
|
||||
'murmur3_x64_128',
|
||||
'sm3',
|
||||
'blake2b',
|
||||
'blake2bp',
|
||||
|
|
@ -123,6 +126,9 @@ rz_hash_sources = [
|
|||
'p/algo_keccak_256.c',
|
||||
'p/algo_keccak_384.c',
|
||||
'p/algo_keccak_512.c',
|
||||
'p/algo_murmur3_x64_128.c',
|
||||
'p/algo_murmur3_x86_32.c',
|
||||
'p/algo_murmur3_x86_128.c',
|
||||
'p/algo_xor8.c',
|
||||
'p/algo_xor16.c',
|
||||
'p/algo_xxhash32.c',
|
||||
|
|
@ -141,7 +147,8 @@ rz_hash_sources = [
|
|||
'algorithms/fletcher/fletcher.c',
|
||||
'algorithms/ssdeep/ssdeep.c',
|
||||
'algorithms/md2/md2.c',
|
||||
'algorithms/sha3/sha3.c'
|
||||
'algorithms/sha3/sha3.c',
|
||||
'algorithms/murmur3/murmur3.c',
|
||||
]
|
||||
|
||||
dependencies = [mth, rz_util_dep, xxhash_dep, blake3_dep, blake2_dep]
|
||||
|
|
|
|||
119
librz/hash/p/algo_murmur3_x64_128.c
Normal file
119
librz/hash/p/algo_murmur3_x64_128.c
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
// SPDX-FileCopyrightText: 2026 Ashish Kumar
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
|
||||
#include <rz_hash.h>
|
||||
#include <rz_util/rz_assert.h>
|
||||
|
||||
#include "../algorithms/murmur3/murmur3.h"
|
||||
|
||||
typedef struct {
|
||||
ut8 *buffer;
|
||||
ut64 buffer_len;
|
||||
ut64 buffer_alloc;
|
||||
uint32_t seed;
|
||||
} Murmur3x64_128Context;
|
||||
|
||||
static void *plugin_murmur3_x64_128_context_new() {
|
||||
return RZ_NEW0(Murmur3x64_128Context);
|
||||
}
|
||||
|
||||
static void plugin_murmur3_x64_128_context_free(void *context) {
|
||||
if (context) {
|
||||
Murmur3x64_128Context *ctx = (Murmur3x64_128Context *)context;
|
||||
free(ctx->buffer);
|
||||
free(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static RzHashSize plugin_murmur3_x64_128_digest_size(void *context) {
|
||||
return MURMUR3_128_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
static RzHashSize plugin_murmur3_x64_128_block_size(void *context) {
|
||||
return 16;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x64_128_init(void *context) {
|
||||
rz_return_val_if_fail(context, false);
|
||||
Murmur3x64_128Context *ctx = (Murmur3x64_128Context *)context;
|
||||
free(ctx->buffer);
|
||||
ctx->buffer = NULL;
|
||||
ctx->buffer_len = 0;
|
||||
ctx->buffer_alloc = 0;
|
||||
ctx->seed = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x64_128_update(void *context, const ut8 *data, ut64 size) {
|
||||
rz_return_val_if_fail(context && data, false);
|
||||
Murmur3x64_128Context *ctx = (Murmur3x64_128Context *)context;
|
||||
|
||||
ut64 new_len = ctx->buffer_len + size;
|
||||
if (new_len > ctx->buffer_alloc) {
|
||||
ut64 new_alloc = RZ_MAX(new_len, ctx->buffer_alloc * 2);
|
||||
ut8 *tmp = realloc(ctx->buffer, new_alloc);
|
||||
if (!tmp) {
|
||||
return false;
|
||||
}
|
||||
ctx->buffer = tmp;
|
||||
ctx->buffer_alloc = new_alloc;
|
||||
}
|
||||
memcpy(ctx->buffer + ctx->buffer_len, data, size);
|
||||
ctx->buffer_len = new_len;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x64_128_final(void *context, ut8 *digest) {
|
||||
rz_return_val_if_fail(context && digest, false);
|
||||
Murmur3x64_128Context *ctx = (Murmur3x64_128Context *)context;
|
||||
|
||||
ut64 result[2] = { 0 };
|
||||
ut8 empty = 0;
|
||||
MurmurHash3_x64_128(ctx->buffer ? ctx->buffer : &empty, (int)ctx->buffer_len, ctx->seed, result);
|
||||
rz_write_be64(digest + 0, result[1]);
|
||||
rz_write_be64(digest + 8, result[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x64_128_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) {
|
||||
rz_return_val_if_fail(data && digest, false);
|
||||
ut8 *dgst = malloc(MURMUR3_128_DIGEST_LENGTH);
|
||||
if (!dgst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ut64 result[2] = { 0 };
|
||||
MurmurHash3_x64_128(data, (int)size, 0, result);
|
||||
rz_write_be64(dgst + 0, result[1]);
|
||||
rz_write_be64(dgst + 8, result[0]);
|
||||
|
||||
*digest = dgst;
|
||||
if (digest_size) {
|
||||
*digest_size = MURMUR3_128_DIGEST_LENGTH;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RzHashPlugin rz_hash_plugin_murmur3_x64_128 = {
|
||||
.name = "murmur3-x64-128",
|
||||
.author = "Austin Appleby",
|
||||
.license = "MIT",
|
||||
.description = "MurmurHash3 x64 128-bit non-cryptographic hash",
|
||||
.support_hmac = false,
|
||||
.context_new = plugin_murmur3_x64_128_context_new,
|
||||
.context_free = plugin_murmur3_x64_128_context_free,
|
||||
.digest_size = plugin_murmur3_x64_128_digest_size,
|
||||
.block_size = plugin_murmur3_x64_128_block_size,
|
||||
.init = plugin_murmur3_x64_128_init,
|
||||
.update = plugin_murmur3_x64_128_update,
|
||||
.final = plugin_murmur3_x64_128_final,
|
||||
.small_block = plugin_murmur3_x64_128_small_block,
|
||||
};
|
||||
|
||||
#ifndef RZ_PLUGIN_INCORE
|
||||
RZ_API RzLibStruct rizin_plugin = {
|
||||
.type = RZ_LIB_TYPE_HASH,
|
||||
.data = &rz_hash_plugin_murmur3_x64_128,
|
||||
.version = RZ_VERSION
|
||||
};
|
||||
#endif
|
||||
123
librz/hash/p/algo_murmur3_x86_128.c
Normal file
123
librz/hash/p/algo_murmur3_x86_128.c
Normal file
|
|
@ -0,0 +1,123 @@
|
|||
// SPDX-FileCopyrightText: 2026 Ashish Kumar
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
|
||||
#include <rz_hash.h>
|
||||
#include <rz_util/rz_assert.h>
|
||||
|
||||
#include "../algorithms/murmur3/murmur3.h"
|
||||
|
||||
typedef struct {
|
||||
ut8 *buffer;
|
||||
ut64 buffer_len;
|
||||
ut64 buffer_alloc;
|
||||
uint32_t seed;
|
||||
} Murmur3x86_128Context;
|
||||
|
||||
static void *plugin_murmur3_x86_128_context_new() {
|
||||
return RZ_NEW0(Murmur3x86_128Context);
|
||||
}
|
||||
|
||||
static void plugin_murmur3_x86_128_context_free(void *context) {
|
||||
if (context) {
|
||||
Murmur3x86_128Context *ctx = (Murmur3x86_128Context *)context;
|
||||
free(ctx->buffer);
|
||||
free(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static RzHashSize plugin_murmur3_x86_128_digest_size(void *context) {
|
||||
return MURMUR3_128_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
static RzHashSize plugin_murmur3_x86_128_block_size(void *context) {
|
||||
return 16;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_128_init(void *context) {
|
||||
rz_return_val_if_fail(context, false);
|
||||
Murmur3x86_128Context *ctx = (Murmur3x86_128Context *)context;
|
||||
free(ctx->buffer);
|
||||
ctx->buffer = NULL;
|
||||
ctx->buffer_len = 0;
|
||||
ctx->buffer_alloc = 0;
|
||||
ctx->seed = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_128_update(void *context, const ut8 *data, ut64 size) {
|
||||
rz_return_val_if_fail(context && data, false);
|
||||
Murmur3x86_128Context *ctx = (Murmur3x86_128Context *)context;
|
||||
|
||||
ut64 new_len = ctx->buffer_len + size;
|
||||
if (new_len > ctx->buffer_alloc) {
|
||||
ut64 new_alloc = RZ_MAX(new_len, ctx->buffer_alloc * 2);
|
||||
ut8 *tmp = realloc(ctx->buffer, new_alloc);
|
||||
if (!tmp) {
|
||||
return false;
|
||||
}
|
||||
ctx->buffer = tmp;
|
||||
ctx->buffer_alloc = new_alloc;
|
||||
}
|
||||
memcpy(ctx->buffer + ctx->buffer_len, data, size);
|
||||
ctx->buffer_len = new_len;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_128_final(void *context, ut8 *digest) {
|
||||
rz_return_val_if_fail(context && digest, false);
|
||||
Murmur3x86_128Context *ctx = (Murmur3x86_128Context *)context;
|
||||
|
||||
ut32 result[4] = { 0 };
|
||||
ut8 empty = 0;
|
||||
MurmurHash3_x86_128(ctx->buffer ? ctx->buffer : &empty, (int)ctx->buffer_len, ctx->seed, result);
|
||||
rz_write_be32(digest + 0, result[3]);
|
||||
rz_write_be32(digest + 4, result[2]);
|
||||
rz_write_be32(digest + 8, result[1]);
|
||||
rz_write_be32(digest + 12, result[0]);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_128_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) {
|
||||
rz_return_val_if_fail(data && digest, false);
|
||||
ut8 *dgst = malloc(MURMUR3_128_DIGEST_LENGTH);
|
||||
if (!dgst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ut32 result[4] = { 0 };
|
||||
MurmurHash3_x86_128(data, (int)size, 0, result);
|
||||
rz_write_be32(dgst + 0, result[3]);
|
||||
rz_write_be32(dgst + 4, result[2]);
|
||||
rz_write_be32(dgst + 8, result[1]);
|
||||
rz_write_be32(dgst + 12, result[0]);
|
||||
|
||||
*digest = dgst;
|
||||
if (digest_size) {
|
||||
*digest_size = MURMUR3_128_DIGEST_LENGTH;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RzHashPlugin rz_hash_plugin_murmur3_x86_128 = {
|
||||
.name = "murmur3-x86-128",
|
||||
.author = "Austin Appleby",
|
||||
.license = "MIT",
|
||||
.description = "MurmurHash3 x86 128-bit non-cryptographic hash",
|
||||
.support_hmac = false,
|
||||
.context_new = plugin_murmur3_x86_128_context_new,
|
||||
.context_free = plugin_murmur3_x86_128_context_free,
|
||||
.digest_size = plugin_murmur3_x86_128_digest_size,
|
||||
.block_size = plugin_murmur3_x86_128_block_size,
|
||||
.init = plugin_murmur3_x86_128_init,
|
||||
.update = plugin_murmur3_x86_128_update,
|
||||
.final = plugin_murmur3_x86_128_final,
|
||||
.small_block = plugin_murmur3_x86_128_small_block,
|
||||
};
|
||||
|
||||
#ifndef RZ_PLUGIN_INCORE
|
||||
RZ_API RzLibStruct rizin_plugin = {
|
||||
.type = RZ_LIB_TYPE_HASH,
|
||||
.data = &rz_hash_plugin_murmur3_x86_128,
|
||||
.version = RZ_VERSION
|
||||
};
|
||||
#endif
|
||||
117
librz/hash/p/algo_murmur3_x86_32.c
Normal file
117
librz/hash/p/algo_murmur3_x86_32.c
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
// SPDX-FileCopyrightText: 2026 Ashish Kumar
|
||||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
|
||||
#include <rz_hash.h>
|
||||
#include <rz_util/rz_assert.h>
|
||||
|
||||
#include "../algorithms/murmur3/murmur3.h"
|
||||
|
||||
typedef struct {
|
||||
ut8 *buffer;
|
||||
ut64 buffer_len;
|
||||
ut64 buffer_alloc;
|
||||
uint32_t seed;
|
||||
} Murmur3x86_32Context;
|
||||
|
||||
static void *plugin_murmur3_x86_32_context_new() {
|
||||
return RZ_NEW0(Murmur3x86_32Context);
|
||||
}
|
||||
|
||||
static void plugin_murmur3_x86_32_context_free(void *context) {
|
||||
if (context) {
|
||||
Murmur3x86_32Context *ctx = (Murmur3x86_32Context *)context;
|
||||
free(ctx->buffer);
|
||||
free(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static RzHashSize plugin_murmur3_x86_32_digest_size(void *context) {
|
||||
return MURMUR3_32_DIGEST_LENGTH;
|
||||
}
|
||||
|
||||
static RzHashSize plugin_murmur3_x86_32_block_size(void *context) {
|
||||
return 4;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_32_init(void *context) {
|
||||
rz_return_val_if_fail(context, false);
|
||||
Murmur3x86_32Context *ctx = (Murmur3x86_32Context *)context;
|
||||
free(ctx->buffer);
|
||||
ctx->buffer = NULL;
|
||||
ctx->buffer_len = 0;
|
||||
ctx->buffer_alloc = 0;
|
||||
ctx->seed = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_32_update(void *context, const ut8 *data, ut64 size) {
|
||||
rz_return_val_if_fail(context && data, false);
|
||||
Murmur3x86_32Context *ctx = (Murmur3x86_32Context *)context;
|
||||
|
||||
ut64 new_len = ctx->buffer_len + size;
|
||||
if (new_len > ctx->buffer_alloc) {
|
||||
ut64 new_alloc = RZ_MAX(new_len, ctx->buffer_alloc * 2);
|
||||
ut8 *tmp = realloc(ctx->buffer, new_alloc);
|
||||
if (!tmp) {
|
||||
return false;
|
||||
}
|
||||
ctx->buffer = tmp;
|
||||
ctx->buffer_alloc = new_alloc;
|
||||
}
|
||||
memcpy(ctx->buffer + ctx->buffer_len, data, size);
|
||||
ctx->buffer_len = new_len;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_32_final(void *context, ut8 *digest) {
|
||||
rz_return_val_if_fail(context && digest, false);
|
||||
Murmur3x86_32Context *ctx = (Murmur3x86_32Context *)context;
|
||||
|
||||
ut32 result = 0;
|
||||
ut8 empty = 0;
|
||||
MurmurHash3_x86_32(ctx->buffer ? ctx->buffer : &empty, (int)ctx->buffer_len, ctx->seed, &result);
|
||||
rz_write_be32(digest, result);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool plugin_murmur3_x86_32_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) {
|
||||
rz_return_val_if_fail(data && digest, false);
|
||||
ut8 *dgst = malloc(MURMUR3_32_DIGEST_LENGTH);
|
||||
if (!dgst) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ut32 result = 0;
|
||||
MurmurHash3_x86_32(data, (int)size, 0, &result);
|
||||
rz_write_be32(dgst, result);
|
||||
|
||||
*digest = dgst;
|
||||
if (digest_size) {
|
||||
*digest_size = MURMUR3_32_DIGEST_LENGTH;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
RzHashPlugin rz_hash_plugin_murmur3_x86_32 = {
|
||||
.name = "murmur3-x86-32",
|
||||
.author = "Austin Appleby",
|
||||
.license = "MIT",
|
||||
.description = "MurmurHash3 x86 32-bit non-cryptographic hash",
|
||||
.support_hmac = false,
|
||||
.context_new = plugin_murmur3_x86_32_context_new,
|
||||
.context_free = plugin_murmur3_x86_32_context_free,
|
||||
.digest_size = plugin_murmur3_x86_32_digest_size,
|
||||
.block_size = plugin_murmur3_x86_32_block_size,
|
||||
.init = plugin_murmur3_x86_32_init,
|
||||
.update = plugin_murmur3_x86_32_update,
|
||||
.final = plugin_murmur3_x86_32_final,
|
||||
.small_block = plugin_murmur3_x86_32_small_block,
|
||||
};
|
||||
|
||||
#ifndef RZ_PLUGIN_INCORE
|
||||
RZ_API RzLibStruct rizin_plugin = {
|
||||
.type = RZ_LIB_TYPE_HASH,
|
||||
.data = &rz_hash_plugin_murmur3_x86_32,
|
||||
.version = RZ_VERSION
|
||||
};
|
||||
#endif
|
||||
|
|
@ -115,6 +115,9 @@ extern RzHashPlugin rz_hash_plugin_sha3_512;
|
|||
extern RzHashPlugin rz_hash_plugin_keccak_256;
|
||||
extern RzHashPlugin rz_hash_plugin_keccak_384;
|
||||
extern RzHashPlugin rz_hash_plugin_keccak_512;
|
||||
extern RzHashPlugin rz_hash_plugin_murmur3_x86_32;
|
||||
extern RzHashPlugin rz_hash_plugin_murmur3_x86_128;
|
||||
extern RzHashPlugin rz_hash_plugin_murmur3_x64_128;
|
||||
extern RzHashPlugin rz_hash_plugin_fletcher8;
|
||||
extern RzHashPlugin rz_hash_plugin_fletcher16;
|
||||
extern RzHashPlugin rz_hash_plugin_fletcher32;
|
||||
|
|
|
|||
|
|
@ -231,4 +231,12 @@ typedef struct _utX {
|
|||
#define RZ_STR_DEF(s) RZ_STR(s)
|
||||
#define RZ_STR(s) #s
|
||||
|
||||
#ifdef __GNUC__
|
||||
#define RZ_INLINE __attribute__((always_inline)) inline
|
||||
#else
|
||||
#define RZ_INLINE inline
|
||||
#endif
|
||||
|
||||
#define BIG_CONSTANT(x) (x##LLU)
|
||||
|
||||
#endif // RZ_TYPES_BASE_H
|
||||
|
|
|
|||
|
|
@ -20,8 +20,6 @@
|
|||
#endif
|
||||
#if RZ_SWIG
|
||||
#define S_API export
|
||||
#elif RZ_INLINE
|
||||
#define S_API inline
|
||||
#else
|
||||
#if defined(__GNUC__) && __GNUC__ >= 4
|
||||
#define S_API __attribute__((visibility("default")))
|
||||
|
|
|
|||
File diff suppressed because one or more lines are too long
|
|
@ -280,6 +280,9 @@ ____hm md2 LGPL3 swedenspy MD2 cryptographi
|
|||
____hm md4 LGPL3 deroad MD4 cryptographic hash
|
||||
____hm md5 LGPL2 Alan DeKok MD5 cryptographic hash
|
||||
____h_ mod255 LGPL3 deroad Modulo 255 checksum
|
||||
____h_ murmur3-x64-128 MIT Austin Appleby MurmurHash3 x64 128-bit non-cryptographic hash
|
||||
____h_ murmur3-x86-128 MIT Austin Appleby MurmurHash3 x86 128-bit non-cryptographic hash
|
||||
____h_ murmur3-x86-32 MIT Austin Appleby MurmurHash3 x86 32-bit non-cryptographic hash
|
||||
____h_ parity LGPL3 deroad Parity checksum
|
||||
____hm sha1 LGPL3 deroad SHA-1 cryptographic hash
|
||||
____hm sha256 BSD-3 Aaron D. Gifford SHA-256 cryptographic hash
|
||||
|
|
@ -753,6 +756,9 @@ string: 0x00000000-0x00000005 md2: computed hash doesn't match the expected one
|
|||
string: 0x00000000-0x00000005 md4: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 md5: computed hash matches the expected one
|
||||
string: 0x00000000-0x00000005 mod255: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 murmur3-x64-128: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 murmur3-x86-128: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 murmur3-x86-32: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 parity: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 sha1: computed hash doesn't match the expected one
|
||||
string: 0x00000000-0x00000005 sha256: computed hash doesn't match the expected one
|
||||
|
|
|
|||
|
|
@ -52,6 +52,9 @@ static hash_data_t hashes_to_test[] = {
|
|||
{ INDATA("password"), .algo = "sha3-256", .expected = "c0067d4af4e87f00dbac63b6156828237059172d1bbeac67427345d6a9fda484" },
|
||||
{ INDATA("password"), .algo = "sha3-384", .expected = "9c1565e99afa2ce7800e96a73c125363c06697c5674d59f227b3368fd00b85ead506eefa90702673d873cb2c9357eafc" },
|
||||
{ INDATA("password"), .algo = "sha3-512", .expected = "e9a75486736a550af4fea861e2378305c4a555a05094dee1dca2f68afea49cc3a50e8de6ea131ea521311f4d6fb054a146e8282f8e35ff2e6368c1a62e909716" },
|
||||
{ INDATA("password"), .algo = "murmur3-x86-32", .expected = "7cc00d26" },
|
||||
{ INDATA("password"), .algo = "murmur3-x86-128", .expected = "695a03df695a03df5bcfb52d38c8196c" },
|
||||
{ INDATA("password"), .algo = "murmur3-x64-128", .expected = "4f345112bc5a9590d5bd0cde2957bf46" },
|
||||
{ INDATA("password"), .algo = "keccak-256", .expected = "b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b" },
|
||||
{ INDATA("password"), .algo = "keccak-384", .expected = "e0779e9bb200a589bc70e499a9f7db1006e181519394990ef41800bebe452c23b4a8372fd89df8d5e0d951af240be7bc" },
|
||||
{ INDATA("password"), .algo = "keccak-512", .expected = "a6818b8188b36c44d17784c5551f63accc5deaf8786f9d0ad1ae3cd8d887cbab4f777286dbb315fb14854c8774dc0d10b5567e4a705536cc2a1d61ec0a16a7a6" },
|
||||
|
|
|
|||
Loading…
Reference in a new issue