diff --git a/librz/hash/algorithms/sha3/sha3.h b/librz/hash/algorithms/sha3/sha3.h index fb0d9a99d0..59bb21a31e 100644 --- a/librz/hash/algorithms/sha3/sha3.h +++ b/librz/hash/algorithms/sha3/sha3.h @@ -28,7 +28,6 @@ /* ------------------------------------------------------------------------- * - * TODO: Implement sha3-224 * This is based (copied) of https://github.com/brainhub/SHA3IUF * * Works when compiled for either 32-bit or 64-bit targets, optimized for diff --git a/librz/hash/algorithms/shake/shake.c b/librz/hash/algorithms/shake/shake.c new file mode 100644 index 0000000000..317cd64fa4 --- /dev/null +++ b/librz/hash/algorithms/shake/shake.c @@ -0,0 +1,177 @@ +// SPDX-FileCopyrightText: 2015 Andrey Jivsov +// SPDX-License-Identifier: MIT + +#include +#include +#include + +#include "shake.h" + +#define SHAKE_CONST(x) x##L + +#ifndef SHAKE_ROTL64 +#define SHAKE_ROTL64(x, y) \ + (((x) << (y)) | ((x) >> ((sizeof(uint64_t) * 8) - (y)))) +#endif + +static const uint64_t keccakf_rndc[24] = { + SHAKE_CONST(0x0000000000000001UL), SHAKE_CONST(0x0000000000008082UL), + SHAKE_CONST(0x800000000000808aUL), SHAKE_CONST(0x8000000080008000UL), + SHAKE_CONST(0x000000000000808bUL), SHAKE_CONST(0x0000000080000001UL), + SHAKE_CONST(0x8000000080008081UL), SHAKE_CONST(0x8000000000008009UL), + SHAKE_CONST(0x000000000000008aUL), SHAKE_CONST(0x0000000000000088UL), + SHAKE_CONST(0x0000000080008009UL), SHAKE_CONST(0x000000008000000aUL), + SHAKE_CONST(0x000000008000808bUL), SHAKE_CONST(0x800000000000008bUL), + SHAKE_CONST(0x8000000000008089UL), SHAKE_CONST(0x8000000000008003UL), + SHAKE_CONST(0x8000000000008002UL), SHAKE_CONST(0x8000000000000080UL), + SHAKE_CONST(0x000000000000800aUL), SHAKE_CONST(0x800000008000000aUL), + SHAKE_CONST(0x8000000080008081UL), SHAKE_CONST(0x8000000000008080UL), + SHAKE_CONST(0x0000000080000001UL), SHAKE_CONST(0x8000000080008008UL) +}; + +static const unsigned keccakf_rotc[24] = { + 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 2, 14, 27, 41, 56, 8, 25, 43, 62, + 18, 39, 61, 20, 44 +}; + +static const unsigned keccakf_piln[24] = { + 10, 7, 11, 17, 18, 3, 5, 16, 8, 21, 24, 4, 15, 23, 19, 13, 12, 2, 20, + 14, 22, 9, 6, 1 +}; + +static void keccakf(uint64_t s[25]) { + int i, j, round; + uint64_t t, bc[5]; +#define KECCAK_ROUNDS 24 + + for (round = 0; round < KECCAK_ROUNDS; round++) { + + /* Theta */ + for (i = 0; i < 5; i++) + bc[i] = s[i] ^ s[i + 5] ^ s[i + 10] ^ s[i + 15] ^ s[i + 20]; + + for (i = 0; i < 5; i++) { + t = bc[(i + 4) % 5] ^ SHAKE_ROTL64(bc[(i + 1) % 5], 1); + for (j = 0; j < 25; j += 5) + s[j + i] ^= t; + } + + /* Rho Pi */ + t = s[1]; + for (i = 0; i < 24; i++) { + j = keccakf_piln[i]; + bc[0] = s[j]; + s[j] = SHAKE_ROTL64(t, keccakf_rotc[i]); + t = bc[0]; + } + + /* Chi */ + for (j = 0; j < 25; j += 5) { + for (i = 0; i < 5; i++) + bc[i] = s[j + i]; + for (i = 0; i < 5; i++) + s[j + i] ^= (~bc[(i + 1) % 5]) & bc[(i + 2) % 5]; + } + + /* Iota */ + s[0] ^= keccakf_rndc[round]; + } +} + +void shake_Init128(void *priv) { + shake_context *ctx = (shake_context *)priv; + memset(ctx, 0, sizeof(*ctx)); + ctx->capacityWords = 4; +} + +void shake_Init256(void *priv) { + shake_context *ctx = (shake_context *)priv; + memset(ctx, 0, sizeof(*ctx)); + ctx->capacityWords = 8; +} + +void shake_Update(void *priv, void const *bufIn, size_t len) { + shake_context *ctx = (shake_context *)priv; + + /* 0...7 -- how much is needed to have a word */ + unsigned old_tail = (8 - ctx->byteIndex) & 7; + + size_t words; + unsigned tail; + size_t i; + + const uint8_t *buf = bufIn; + + if (len < old_tail) { + while (len--) + ctx->saved |= (uint64_t)(*(buf++)) << ((ctx->byteIndex++) * 8); + return; + } + + if (old_tail) { /* will have one word to process */ + len -= old_tail; + while (old_tail--) + ctx->saved |= (uint64_t)(*(buf++)) << ((ctx->byteIndex++) * 8); + + /* now ready to add saved to the sponge */ + ctx->u.s[ctx->wordIndex] ^= ctx->saved; + ctx->byteIndex = 0; + ctx->saved = 0; + if (++ctx->wordIndex == (SHAKE_KECCAK_SPONGE_WORDS - ctx->capacityWords)) { + keccakf(ctx->u.s); + ctx->wordIndex = 0; + } + } + + /* now work in full words directly from input */ + + words = len / sizeof(uint64_t); + tail = len - words * sizeof(uint64_t); + + for (i = 0; i < words; i++, buf += sizeof(uint64_t)) { + const uint64_t t = (uint64_t)(buf[0]) | + ((uint64_t)(buf[1]) << 8 * 1) | + ((uint64_t)(buf[2]) << 8 * 2) | + ((uint64_t)(buf[3]) << 8 * 3) | + ((uint64_t)(buf[4]) << 8 * 4) | + ((uint64_t)(buf[5]) << 8 * 5) | + ((uint64_t)(buf[6]) << 8 * 6) | + ((uint64_t)(buf[7]) << 8 * 7); + ctx->u.s[ctx->wordIndex] ^= t; + if (++ctx->wordIndex == (SHAKE_KECCAK_SPONGE_WORDS - ctx->capacityWords)) { + keccakf(ctx->u.s); + ctx->wordIndex = 0; + } + } + + /* finally, save the partial word */ + while (tail--) { + ctx->saved |= (uint64_t)(*(buf++)) << ((ctx->byteIndex++) * 8); + } +} + +void shake_Finalize(void *priv) { + shake_context *ctx = (shake_context *)priv; + uint64_t t = (uint64_t)(((uint64_t)0x1f) << (ctx->byteIndex * 8)); + ctx->u.s[ctx->wordIndex] ^= ctx->saved ^ t; + ctx->u.s[SHAKE_KECCAK_SPONGE_WORDS - ctx->capacityWords - 1] ^= + SHAKE_CONST(0x8000000000000000UL); + keccakf(ctx->u.s); + ctx->byteIndex = 0; +} + +void shake_Squeeze(void *priv, uint8_t *output, size_t outlen) { + shake_context *ctx = (shake_context *)priv; + size_t rate_bytes = (SHAKE_KECCAK_SPONGE_WORDS - ctx->capacityWords) * 8; + size_t i; + for (i = 0; i < outlen; i++) { + if (ctx->byteIndex >= rate_bytes) { + keccakf(ctx->u.s); + ctx->byteIndex = 0; + } + unsigned word_idx = ctx->byteIndex / 8; + unsigned byte_in_word = ctx->byteIndex % 8; + output[i] = (uint8_t)(ctx->u.s[word_idx] >> (byte_in_word * 8)); + ctx->byteIndex++; + } +} diff --git a/librz/hash/algorithms/shake/shake.h b/librz/hash/algorithms/shake/shake.h new file mode 100644 index 0000000000..6c1f38eaf5 --- /dev/null +++ b/librz/hash/algorithms/shake/shake.h @@ -0,0 +1,32 @@ +// SPDX-FileCopyrightText: 2015 Andrey Jivsov +// SPDX-License-Identifier: MIT + +#ifndef __SHAKE_H__ +#define __SHAKE_H__ + +#include +#include + +#define SHAKE_KECCAK_SPONGE_WORDS 25 + +typedef struct shake_context_ { + uint64_t saved; /* the portion of the input message that we + * didn't consume yet */ + union { /* Keccak's state */ + uint64_t s[SHAKE_KECCAK_SPONGE_WORDS]; + uint8_t sb[SHAKE_KECCAK_SPONGE_WORDS * 8]; + } u; + unsigned byteIndex; /* 0..7--the next byte after the set one + * (starts from 0; 0--none are buffered) */ + unsigned wordIndex; /* 0..24--the next word to integrate input + * (starts from 0) */ + unsigned capacityWords; /* capacity in words (e.g. 4 for SHAKE-128, 8 for SHAKE-256) */ +} shake_context; + +RZ_IPI void shake_Init128(void *priv); +RZ_IPI void shake_Init256(void *priv); +RZ_IPI void shake_Update(void *priv, void const *bufIn, size_t len); +RZ_IPI void shake_Finalize(void *priv); +RZ_IPI void shake_Squeeze(void *priv, uint8_t *output, size_t outlen); + +#endif diff --git a/librz/hash/meson.build b/librz/hash/meson.build index abbfc84f56..ede2801b15 100644 --- a/librz/hash/meson.build +++ b/librz/hash/meson.build @@ -13,6 +13,8 @@ hash_plugins_list = [ 'keccak_256', 'keccak_384', 'keccak_512', + 'shake_128', + 'shake_256', 'murmur3_x86_32', 'murmur3_x86_128', 'murmur3_x64_128', @@ -134,6 +136,8 @@ rz_hash_sources = [ 'p/algo_keccak_256.c', 'p/algo_keccak_384.c', 'p/algo_keccak_512.c', + 'p/algo_shake_128.c', + 'p/algo_shake_256.c', 'p/algo_murmur3_x64_128.c', 'p/algo_murmur3_x86_32.c', 'p/algo_murmur3_x86_128.c', @@ -166,6 +170,7 @@ rz_hash_sources = [ 'algorithms/ssdeep/ssdeep.c', 'algorithms/md2/md2.c', 'algorithms/sha3/sha3.c', + 'algorithms/shake/shake.c', 'algorithms/murmur3/murmur3.c', ] diff --git a/librz/hash/p/algo_shake_128.c b/librz/hash/p/algo_shake_128.c new file mode 100644 index 0000000000..43a13c65c6 --- /dev/null +++ b/librz/hash/p/algo_shake_128.c @@ -0,0 +1,89 @@ +// SPDX-FileCopyrightText: 2026 Ashish Kumar <15678ashishk@gmail.com> +// SPDX-License-Identifier: LGPL-3.0-only + +#include +#include + +#include "../algorithms/shake/shake.h" + +#define shake_128_BLOCK_LENGTH 168 +#define shake_128_DIGEST_LENGTH 32 + +static void *plugin_shake_128_context_new() { + return RZ_NEW0(shake_context); +} + +static void plugin_shake_128_context_free(void *context) { + free(context); +} + +static RzHashSize plugin_shake_128_digest_size(void *context) { + return shake_128_DIGEST_LENGTH; +} + +static RzHashSize plugin_shake_128_block_size(void *context) { + return shake_128_BLOCK_LENGTH; +} + +static bool plugin_shake_128_init(void *context) { + rz_return_val_if_fail(context, false); + shake_Init128((shake_context *)context); + return true; +} + +static bool plugin_shake_128_update(void *context, const ut8 *data, ut64 size) { + rz_return_val_if_fail(context, false); + shake_Update((shake_context *)context, data, (size_t)size); + return true; +} + +static bool plugin_shake_128_final(void *context, ut8 *digest) { + rz_return_val_if_fail(context && digest, false); + shake_Finalize((shake_context *)context); + shake_Squeeze((shake_context *)context, digest, shake_128_DIGEST_LENGTH); + return true; +} + +static bool plugin_shake_128_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) { + rz_return_val_if_fail(data && digest, false); + ut8 *dgst = malloc(shake_128_DIGEST_LENGTH); + if (!dgst) { + return false; + } + + shake_context ctx; + shake_Init128(&ctx); + shake_Update(&ctx, data, size); + shake_Finalize(&ctx); + shake_Squeeze(&ctx, dgst, shake_128_DIGEST_LENGTH); + + *digest = dgst; + if (digest_size) { + *digest_size = shake_128_DIGEST_LENGTH; + } + return true; +} + +RzHashPlugin rz_hash_plugin_shake_128 = { + .name = "shake-128", + .author = "Andrey Jivsov", + .license = "BSD-3", + .description = "SHAKE-128 extendable-output function", + .support_hmac = false, + .context_new = plugin_shake_128_context_new, + .context_free = plugin_shake_128_context_free, + .digest_size = plugin_shake_128_digest_size, + .block_size = plugin_shake_128_block_size, + .init = plugin_shake_128_init, + .update = plugin_shake_128_update, + .final = plugin_shake_128_final, + .small_block = plugin_shake_128_small_block, +}; + +#ifndef RZ_PLUGIN_INCORE +RZ_API RzLibStruct rizin_plugin = { + .type = RZ_LIB_TYPE_HASH, + .data = &rz_hash_plugin_shake_128, + .version = RZ_VERSION +}; +#endif diff --git a/librz/hash/p/algo_shake_256.c b/librz/hash/p/algo_shake_256.c new file mode 100644 index 0000000000..ea563f43bf --- /dev/null +++ b/librz/hash/p/algo_shake_256.c @@ -0,0 +1,89 @@ +// SPDX-FileCopyrightText: 2026 Ashish Kumar <15678ashishk@gmail.com> +// SPDX-License-Identifier: LGPL-3.0-only + +#include +#include + +#include "../algorithms/shake/shake.h" + +#define shake_256_BLOCK_LENGTH 136 +#define shake_256_DIGEST_LENGTH 64 + +static void *plugin_shake_256_context_new() { + return RZ_NEW0(shake_context); +} + +static void plugin_shake_256_context_free(void *context) { + free(context); +} + +static RzHashSize plugin_shake_256_digest_size(void *context) { + return shake_256_DIGEST_LENGTH; +} + +static RzHashSize plugin_shake_256_block_size(void *context) { + return shake_256_BLOCK_LENGTH; +} + +static bool plugin_shake_256_init(void *context) { + rz_return_val_if_fail(context, false); + shake_Init256((shake_context *)context); + return true; +} + +static bool plugin_shake_256_update(void *context, const ut8 *data, ut64 size) { + rz_return_val_if_fail(context, false); + shake_Update((shake_context *)context, data, (size_t)size); + return true; +} + +static bool plugin_shake_256_final(void *context, ut8 *digest) { + rz_return_val_if_fail(context && digest, false); + shake_Finalize((shake_context *)context); + shake_Squeeze((shake_context *)context, digest, shake_256_DIGEST_LENGTH); + return true; +} + +static bool plugin_shake_256_small_block(const ut8 *data, ut64 size, ut8 **digest, RzHashSize *digest_size) { + rz_return_val_if_fail(data && digest, false); + ut8 *dgst = malloc(shake_256_DIGEST_LENGTH); + if (!dgst) { + return false; + } + + shake_context ctx; + shake_Init256(&ctx); + shake_Update(&ctx, data, size); + shake_Finalize(&ctx); + shake_Squeeze(&ctx, dgst, shake_256_DIGEST_LENGTH); + + *digest = dgst; + if (digest_size) { + *digest_size = shake_256_DIGEST_LENGTH; + } + return true; +} + +RzHashPlugin rz_hash_plugin_shake_256 = { + .name = "shake-256", + .author = "Andrey Jivsov", + .license = "BSD-3", + .description = "SHAKE-256 extendable-output function", + .support_hmac = false, + .context_new = plugin_shake_256_context_new, + .context_free = plugin_shake_256_context_free, + .digest_size = plugin_shake_256_digest_size, + .block_size = plugin_shake_256_block_size, + .init = plugin_shake_256_init, + .update = plugin_shake_256_update, + .final = plugin_shake_256_final, + .small_block = plugin_shake_256_small_block, +}; + +#ifndef RZ_PLUGIN_INCORE +RZ_API RzLibStruct rizin_plugin = { + .type = RZ_LIB_TYPE_HASH, + .data = &rz_hash_plugin_shake_256, + .version = RZ_VERSION +}; +#endif diff --git a/librz/include/rz_hash.h b/librz/include/rz_hash.h index a8ba59ccd6..7eb38e7c2e 100644 --- a/librz/include/rz_hash.h +++ b/librz/include/rz_hash.h @@ -119,6 +119,8 @@ 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_shake_128; +extern RzHashPlugin rz_hash_plugin_shake_256; extern RzHashPlugin rz_hash_plugin_murmur3_x86_32; extern RzHashPlugin rz_hash_plugin_murmur3_x86_128; extern RzHashPlugin rz_hash_plugin_murmur3_x64_128; diff --git a/test/db/cmd/cmd_list b/test/db/cmd/cmd_list index 1c46312314..08a5332423 100644 --- a/test/db/cmd/cmd_list +++ b/test/db/cmd/cmd_list @@ -480,6 +480,8 @@ sha3-384 BSD-3 Andrey Jivsov SHA3-384 cryptographic sha3-512 BSD-3 Andrey Jivsov SHA3-512 cryptographic hash sha384 BSD-3 Aaron D. Gifford SHA-384 cryptographic hash sha512 BSD-3 Aaron D. Gifford SHA-512 cryptographic hash +shake-128 BSD-3 Andrey Jivsov SHAKE-128 extendable-output function +shake-256 BSD-3 Andrey Jivsov SHAKE-256 extendable-output function sm3 LGPL2 FSF/deroad ShangMi 3 (SM3) cryptographic hash ssdeep LGPL3 deroad SSDeep Context Triggered Piecewise Hash (CTPH, fuzzy hash) temperature LGPL3 Seva Binary temperature [0.0-1.0] range @@ -487,7 +489,7 @@ xor16 LGPL3 deroad XOR-16 checksum xor8 LGPL3 deroad XOR-8 checksum xxhash32 LGPL3 deroad xxHash32 non-cryptographic hash xxhash64 LGPL3 cheese-cakee xxHash64 non-cryptographic hash -[{"name":"adler32","license":"LGPL3","author":"deroad","description":"Adler-32 checksum"},{"name":"blake2b","license":"LGPL3","author":"Farhan-25","description":"BLAKE2b cryptographic hash"},{"name":"blake2bp","license":"LGPL3","author":"Farhan-25","description":"BLAKE2bp cryptographic hash"},{"name":"blake2s","license":"LGPL3","author":"Farhan-25","description":"BLAKE2s cryptographic hash"},{"name":"blake2sp","license":"LGPL3","author":"Farhan-25","description":"BLAKE2sp cryptographic hash"},{"name":"blake2xb","license":"LGPL3","author":"Farhan-25","description":"BLAKE2xb cryptographic hash"},{"name":"blake2xs","license":"LGPL3","author":"Farhan-25","description":"BLAKE2xs cryptographic hash"},{"name":"blake3","license":"CC0","author":"Samuel Neves,Jack O'Connor","description":"BLAKE3 cryptographic hash"},{"name":"chisquare","license":"LGPL3","author":"xvilka","description":"Chi-square goodness-of-fit vs uniform"},{"name":"crc15can","license":"LGPL3","author":"deroad","description":"CRC-15 CAN checksum"},{"name":"crc16","license":"LGPL3","author":"deroad","description":"CRC-16 IBM/ARC checksum"},{"name":"crc16augccitt","license":"LGPL3","author":"deroad","description":"CRC-16 AUG-CCITT checksum"},{"name":"crc16buypass","license":"LGPL3","author":"deroad","description":"CRC-16 BUYPASS checksum"},{"name":"crc16cdma2000","license":"LGPL3","author":"deroad","description":"CRC-16 CDMA-2000 checksum"},{"name":"crc16citt","license":"LGPL3","author":"deroad","description":"CRC-16 CITT checksum"},{"name":"crc16dds110","license":"LGPL3","author":"deroad","description":"CRC-16 DDS110 checksum"},{"name":"crc16dectr","license":"LGPL3","author":"deroad","description":"CRC-16 DECT-R checksum"},{"name":"crc16dectx","license":"LGPL3","author":"deroad","description":"CRC-16 DECT-X checksum"},{"name":"crc16dnp","license":"LGPL3","author":"deroad","description":"CRC-16 DNP checksum"},{"name":"crc16en13757","license":"LGPL3","author":"deroad","description":"CRC-16 EN-13757 checksum"},{"name":"crc16genibus","license":"LGPL3","author":"deroad","description":"CRC-16 GENIBUS checksum"},{"name":"crc16hdlc","license":"LGPL3","author":"deroad","description":"CRC-16 HDLC checksum"},{"name":"crc16kermit","license":"LGPL3","author":"deroad","description":"CRC-16 KERMIT checksum"},{"name":"crc16maxim","license":"LGPL3","author":"deroad","description":"CRC-16 MAXIM checksum"},{"name":"crc16mcrf4xx","license":"LGPL3","author":"deroad","description":"CRC-16 MCRF4XX checksum"},{"name":"crc16modbus","license":"LGPL3","author":"deroad","description":"CRC-16 MODBUS checksum"},{"name":"crc16riello","license":"LGPL3","author":"deroad","description":"CRC-16 RIELLO checksum"},{"name":"crc16t10dif","license":"LGPL3","author":"deroad","description":"CRC-16 T10-DIF checksum"},{"name":"crc16teledisk","license":"LGPL3","author":"deroad","description":"CRC-16 TELEDISK checksum"},{"name":"crc16tms37157","license":"LGPL3","author":"deroad","description":"CRC-16 TMS37157 checksum"},{"name":"crc16usb","license":"LGPL3","author":"deroad","description":"CRC-16 USB checksum"},{"name":"crc16x25","license":"LGPL3","author":"deroad","description":"CRC-16 X25 checksum"},{"name":"crc16xmodem","license":"LGPL3","author":"deroad","description":"CRC-16 XMODEM checksum"},{"name":"crc24","license":"LGPL3","author":"deroad","description":"CRC-24 checksum"},{"name":"crc32","license":"LGPL3","author":"deroad","description":"CRC-32 checksum"},{"name":"crc32bzip2","license":"LGPL3","author":"deroad","description":"CRC-32 BZIP2 checksum"},{"name":"crc32c","license":"LGPL3","author":"deroad","description":"CRC-32C checksum"},{"name":"crc32d","license":"LGPL3","author":"deroad","description":"CRC-32D checksum"},{"name":"crc32ecma267","license":"LGPL3","author":"deroad","description":"CRC-32 ECMA-267 checksum (EDC for DVD sectors)"},{"name":"crc32jamcrc","license":"LGPL3","author":"deroad","description":"CRC-32 JAMCRC checksum"},{"name":"crc32mpeg2","license":"LGPL3","author":"deroad","description":"CRC-32 MPEG2 checksum"},{"name":"crc32posix","license":"LGPL3","author":"deroad","description":"CRC-32 POSIX checksum"},{"name":"crc32q","license":"LGPL3","author":"deroad","description":"CRC-32Q checksum"},{"name":"crc32xfer","license":"LGPL3","author":"deroad","description":"CRC-32 XFER checksum"},{"name":"crc64","license":"LGPL3","author":"deroad","description":"CRC-64 checksum"},{"name":"crc64ecma182","license":"LGPL3","author":"deroad","description":"CRC-64 ECMA-182 checksum"},{"name":"crc64iso","license":"LGPL3","author":"deroad","description":"CRC-64 ISO checksum"},{"name":"crc64we","license":"LGPL3","author":"deroad","description":"CRC-64 WE checksum"},{"name":"crc64xz","license":"LGPL3","author":"deroad","description":"CRC-64 XZ checksum"},{"name":"crc8cdma2000","license":"LGPL3","author":"deroad","description":"CRC-8 CDMA-2000 checksum"},{"name":"crc8darc","license":"LGPL3","author":"deroad","description":"CRC-8 DARC checksum"},{"name":"crc8dvbs2","license":"LGPL3","author":"deroad","description":"CRC-8 DVB-S2 checksum"},{"name":"crc8ebu","license":"LGPL3","author":"deroad","description":"CRC-8 EBU checksum"},{"name":"crc8icode","license":"LGPL3","author":"deroad","description":"CRC-8 I-CODE checksum"},{"name":"crc8itu","license":"LGPL3","author":"deroad","description":"CRC-8 ITU checksum"},{"name":"crc8maxim","license":"LGPL3","author":"deroad","description":"CRC-8 MAXIM checksum"},{"name":"crc8rohc","license":"LGPL3","author":"deroad","description":"CRC-8 ROHC checksum"},{"name":"crc8smbus","license":"LGPL3","author":"deroad","description":"CRC-8 SMBUS checksum"},{"name":"crc8wcdma","license":"LGPL3","author":"deroad","description":"CRC-8 WCDMA checksum"},{"name":"crca","license":"LGPL3","author":"deroad","description":"CRC-16/CRCA checksum"},{"name":"entropy","license":"LGPL3","author":"deroad","description":"Entropy [0.0-8.0] range"},{"name":"entropy_fract","license":"LGPL3","author":"deroad","description":"Fractional entropy [0.0-1.0] range"},{"name":"fletcher16","license":"LGPL3","author":"deroad","description":"Fletcher16 checksum"},{"name":"fletcher32","license":"LGPL3","author":"deroad","description":"Fletcher32 checksum"},{"name":"fletcher64","license":"LGPL3","author":"deroad","description":"Fletcher64 checksum"},{"name":"fletcher8","license":"LGPL3","author":"deroad","description":"Fletcher8 checksum"},{"name":"fnv1a","license":"LGPL3","author":"cheese-cakee","description":"FNV-1a 32-bit non-cryptographic hash"},{"name":"ioc","license":"LGPL3","author":"xvilka","description":"Index of coincidence [0.0-1.0] range"},{"name":"jenkins","license":"LGPL3","author":"shessaanand","description":"Jenkins non-cryptographic hash"},{"name":"keccak-256","license":"BSD-3","author":"Andrey Jivsov","description":"KECCAK-256 cryptographic hash"},{"name":"keccak-384","license":"BSD-3","author":"Andrey Jivsov","description":"KECCAK-384 cryptographic hash"},{"name":"keccak-512","license":"BSD-3","author":"Andrey Jivsov","description":"KECCAK-512 cryptographic hash"},{"name":"md2","license":"LGPL3","author":"swedenspy","description":"MD2 cryptographic hash"},{"name":"md4","license":"LGPL3","author":"deroad","description":"MD4 cryptographic hash"},{"name":"md5","license":"LGPL2","author":"Alan DeKok","description":"MD5 cryptographic hash"},{"name":"minentropy","license":"LGPL3","author":"xvilka","description":"Min-entropy [0.0-8.0] range"},{"name":"mod255","license":"LGPL3","author":"deroad","description":"Modulo 255 checksum"},{"name":"murmur3-x64-128","license":"MIT","author":"Austin Appleby","description":"MurmurHash3 x64 128-bit non-cryptographic hash"},{"name":"murmur3-x86-128","license":"MIT","author":"Austin Appleby","description":"MurmurHash3 x86 128-bit non-cryptographic hash"},{"name":"murmur3-x86-32","license":"MIT","author":"Austin Appleby","description":"MurmurHash3 x86 32-bit non-cryptographic hash"},{"name":"parity","license":"LGPL3","author":"deroad","description":"Parity checksum"},{"name":"serialcorr","license":"LGPL3","author":"xvilka","description":"Serial correlation coefficient [-1.0-1.0]"},{"name":"sha1","license":"LGPL3","author":"deroad","description":"SHA-1 cryptographic hash"},{"name":"sha256","license":"BSD-3","author":"Aaron D. Gifford","description":"SHA-256 cryptographic hash"},{"name":"sha3-224","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-224 cryptographic hash"},{"name":"sha3-256","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-256 cryptographic hash"},{"name":"sha3-384","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-384 cryptographic hash"},{"name":"sha3-512","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-512 cryptographic hash"},{"name":"sha384","license":"BSD-3","author":"Aaron D. Gifford","description":"SHA-384 cryptographic hash"},{"name":"sha512","license":"BSD-3","author":"Aaron D. Gifford","description":"SHA-512 cryptographic hash"},{"name":"sm3","license":"LGPL2","author":"FSF/deroad","description":"ShangMi 3 (SM3) cryptographic hash"},{"name":"ssdeep","license":"LGPL3","author":"deroad","description":"SSDeep Context Triggered Piecewise Hash (CTPH, fuzzy hash)"},{"name":"temperature","license":"LGPL3","author":"Seva","description":"Binary temperature [0.0-1.0] range"},{"name":"xor16","license":"LGPL3","author":"deroad","description":"XOR-16 checksum"},{"name":"xor8","license":"LGPL3","author":"deroad","description":"XOR-8 checksum"},{"name":"xxhash32","license":"LGPL3","author":"deroad","description":"xxHash32 non-cryptographic hash"},{"name":"xxhash64","license":"LGPL3","author":"cheese-cakee","description":"xxHash64 non-cryptographic hash"}] +[{"name":"adler32","license":"LGPL3","author":"deroad","description":"Adler-32 checksum"},{"name":"blake2b","license":"LGPL3","author":"Farhan-25","description":"BLAKE2b cryptographic hash"},{"name":"blake2bp","license":"LGPL3","author":"Farhan-25","description":"BLAKE2bp cryptographic hash"},{"name":"blake2s","license":"LGPL3","author":"Farhan-25","description":"BLAKE2s cryptographic hash"},{"name":"blake2sp","license":"LGPL3","author":"Farhan-25","description":"BLAKE2sp cryptographic hash"},{"name":"blake2xb","license":"LGPL3","author":"Farhan-25","description":"BLAKE2xb cryptographic hash"},{"name":"blake2xs","license":"LGPL3","author":"Farhan-25","description":"BLAKE2xs cryptographic hash"},{"name":"blake3","license":"CC0","author":"Samuel Neves,Jack O'Connor","description":"BLAKE3 cryptographic hash"},{"name":"chisquare","license":"LGPL3","author":"xvilka","description":"Chi-square goodness-of-fit vs uniform"},{"name":"crc15can","license":"LGPL3","author":"deroad","description":"CRC-15 CAN checksum"},{"name":"crc16","license":"LGPL3","author":"deroad","description":"CRC-16 IBM/ARC checksum"},{"name":"crc16augccitt","license":"LGPL3","author":"deroad","description":"CRC-16 AUG-CCITT checksum"},{"name":"crc16buypass","license":"LGPL3","author":"deroad","description":"CRC-16 BUYPASS checksum"},{"name":"crc16cdma2000","license":"LGPL3","author":"deroad","description":"CRC-16 CDMA-2000 checksum"},{"name":"crc16citt","license":"LGPL3","author":"deroad","description":"CRC-16 CITT checksum"},{"name":"crc16dds110","license":"LGPL3","author":"deroad","description":"CRC-16 DDS110 checksum"},{"name":"crc16dectr","license":"LGPL3","author":"deroad","description":"CRC-16 DECT-R checksum"},{"name":"crc16dectx","license":"LGPL3","author":"deroad","description":"CRC-16 DECT-X checksum"},{"name":"crc16dnp","license":"LGPL3","author":"deroad","description":"CRC-16 DNP checksum"},{"name":"crc16en13757","license":"LGPL3","author":"deroad","description":"CRC-16 EN-13757 checksum"},{"name":"crc16genibus","license":"LGPL3","author":"deroad","description":"CRC-16 GENIBUS checksum"},{"name":"crc16hdlc","license":"LGPL3","author":"deroad","description":"CRC-16 HDLC checksum"},{"name":"crc16kermit","license":"LGPL3","author":"deroad","description":"CRC-16 KERMIT checksum"},{"name":"crc16maxim","license":"LGPL3","author":"deroad","description":"CRC-16 MAXIM checksum"},{"name":"crc16mcrf4xx","license":"LGPL3","author":"deroad","description":"CRC-16 MCRF4XX checksum"},{"name":"crc16modbus","license":"LGPL3","author":"deroad","description":"CRC-16 MODBUS checksum"},{"name":"crc16riello","license":"LGPL3","author":"deroad","description":"CRC-16 RIELLO checksum"},{"name":"crc16t10dif","license":"LGPL3","author":"deroad","description":"CRC-16 T10-DIF checksum"},{"name":"crc16teledisk","license":"LGPL3","author":"deroad","description":"CRC-16 TELEDISK checksum"},{"name":"crc16tms37157","license":"LGPL3","author":"deroad","description":"CRC-16 TMS37157 checksum"},{"name":"crc16usb","license":"LGPL3","author":"deroad","description":"CRC-16 USB checksum"},{"name":"crc16x25","license":"LGPL3","author":"deroad","description":"CRC-16 X25 checksum"},{"name":"crc16xmodem","license":"LGPL3","author":"deroad","description":"CRC-16 XMODEM checksum"},{"name":"crc24","license":"LGPL3","author":"deroad","description":"CRC-24 checksum"},{"name":"crc32","license":"LGPL3","author":"deroad","description":"CRC-32 checksum"},{"name":"crc32bzip2","license":"LGPL3","author":"deroad","description":"CRC-32 BZIP2 checksum"},{"name":"crc32c","license":"LGPL3","author":"deroad","description":"CRC-32C checksum"},{"name":"crc32d","license":"LGPL3","author":"deroad","description":"CRC-32D checksum"},{"name":"crc32ecma267","license":"LGPL3","author":"deroad","description":"CRC-32 ECMA-267 checksum (EDC for DVD sectors)"},{"name":"crc32jamcrc","license":"LGPL3","author":"deroad","description":"CRC-32 JAMCRC checksum"},{"name":"crc32mpeg2","license":"LGPL3","author":"deroad","description":"CRC-32 MPEG2 checksum"},{"name":"crc32posix","license":"LGPL3","author":"deroad","description":"CRC-32 POSIX checksum"},{"name":"crc32q","license":"LGPL3","author":"deroad","description":"CRC-32Q checksum"},{"name":"crc32xfer","license":"LGPL3","author":"deroad","description":"CRC-32 XFER checksum"},{"name":"crc64","license":"LGPL3","author":"deroad","description":"CRC-64 checksum"},{"name":"crc64ecma182","license":"LGPL3","author":"deroad","description":"CRC-64 ECMA-182 checksum"},{"name":"crc64iso","license":"LGPL3","author":"deroad","description":"CRC-64 ISO checksum"},{"name":"crc64we","license":"LGPL3","author":"deroad","description":"CRC-64 WE checksum"},{"name":"crc64xz","license":"LGPL3","author":"deroad","description":"CRC-64 XZ checksum"},{"name":"crc8cdma2000","license":"LGPL3","author":"deroad","description":"CRC-8 CDMA-2000 checksum"},{"name":"crc8darc","license":"LGPL3","author":"deroad","description":"CRC-8 DARC checksum"},{"name":"crc8dvbs2","license":"LGPL3","author":"deroad","description":"CRC-8 DVB-S2 checksum"},{"name":"crc8ebu","license":"LGPL3","author":"deroad","description":"CRC-8 EBU checksum"},{"name":"crc8icode","license":"LGPL3","author":"deroad","description":"CRC-8 I-CODE checksum"},{"name":"crc8itu","license":"LGPL3","author":"deroad","description":"CRC-8 ITU checksum"},{"name":"crc8maxim","license":"LGPL3","author":"deroad","description":"CRC-8 MAXIM checksum"},{"name":"crc8rohc","license":"LGPL3","author":"deroad","description":"CRC-8 ROHC checksum"},{"name":"crc8smbus","license":"LGPL3","author":"deroad","description":"CRC-8 SMBUS checksum"},{"name":"crc8wcdma","license":"LGPL3","author":"deroad","description":"CRC-8 WCDMA checksum"},{"name":"crca","license":"LGPL3","author":"deroad","description":"CRC-16/CRCA checksum"},{"name":"entropy","license":"LGPL3","author":"deroad","description":"Entropy [0.0-8.0] range"},{"name":"entropy_fract","license":"LGPL3","author":"deroad","description":"Fractional entropy [0.0-1.0] range"},{"name":"fletcher16","license":"LGPL3","author":"deroad","description":"Fletcher16 checksum"},{"name":"fletcher32","license":"LGPL3","author":"deroad","description":"Fletcher32 checksum"},{"name":"fletcher64","license":"LGPL3","author":"deroad","description":"Fletcher64 checksum"},{"name":"fletcher8","license":"LGPL3","author":"deroad","description":"Fletcher8 checksum"},{"name":"fnv1a","license":"LGPL3","author":"cheese-cakee","description":"FNV-1a 32-bit non-cryptographic hash"},{"name":"ioc","license":"LGPL3","author":"xvilka","description":"Index of coincidence [0.0-1.0] range"},{"name":"jenkins","license":"LGPL3","author":"shessaanand","description":"Jenkins non-cryptographic hash"},{"name":"keccak-256","license":"BSD-3","author":"Andrey Jivsov","description":"KECCAK-256 cryptographic hash"},{"name":"keccak-384","license":"BSD-3","author":"Andrey Jivsov","description":"KECCAK-384 cryptographic hash"},{"name":"keccak-512","license":"BSD-3","author":"Andrey Jivsov","description":"KECCAK-512 cryptographic hash"},{"name":"md2","license":"LGPL3","author":"swedenspy","description":"MD2 cryptographic hash"},{"name":"md4","license":"LGPL3","author":"deroad","description":"MD4 cryptographic hash"},{"name":"md5","license":"LGPL2","author":"Alan DeKok","description":"MD5 cryptographic hash"},{"name":"minentropy","license":"LGPL3","author":"xvilka","description":"Min-entropy [0.0-8.0] range"},{"name":"mod255","license":"LGPL3","author":"deroad","description":"Modulo 255 checksum"},{"name":"murmur3-x64-128","license":"MIT","author":"Austin Appleby","description":"MurmurHash3 x64 128-bit non-cryptographic hash"},{"name":"murmur3-x86-128","license":"MIT","author":"Austin Appleby","description":"MurmurHash3 x86 128-bit non-cryptographic hash"},{"name":"murmur3-x86-32","license":"MIT","author":"Austin Appleby","description":"MurmurHash3 x86 32-bit non-cryptographic hash"},{"name":"parity","license":"LGPL3","author":"deroad","description":"Parity checksum"},{"name":"serialcorr","license":"LGPL3","author":"xvilka","description":"Serial correlation coefficient [-1.0-1.0]"},{"name":"sha1","license":"LGPL3","author":"deroad","description":"SHA-1 cryptographic hash"},{"name":"sha256","license":"BSD-3","author":"Aaron D. Gifford","description":"SHA-256 cryptographic hash"},{"name":"sha3-224","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-224 cryptographic hash"},{"name":"sha3-256","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-256 cryptographic hash"},{"name":"sha3-384","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-384 cryptographic hash"},{"name":"sha3-512","license":"BSD-3","author":"Andrey Jivsov","description":"SHA3-512 cryptographic hash"},{"name":"sha384","license":"BSD-3","author":"Aaron D. Gifford","description":"SHA-384 cryptographic hash"},{"name":"sha512","license":"BSD-3","author":"Aaron D. Gifford","description":"SHA-512 cryptographic hash"},{"name":"shake-128","license":"BSD-3","author":"Andrey Jivsov","description":"SHAKE-128 extendable-output function"},{"name":"shake-256","license":"BSD-3","author":"Andrey Jivsov","description":"SHAKE-256 extendable-output function"},{"name":"sm3","license":"LGPL2","author":"FSF/deroad","description":"ShangMi 3 (SM3) cryptographic hash"},{"name":"ssdeep","license":"LGPL3","author":"deroad","description":"SSDeep Context Triggered Piecewise Hash (CTPH, fuzzy hash)"},{"name":"temperature","license":"LGPL3","author":"Seva","description":"Binary temperature [0.0-1.0] range"},{"name":"xor16","license":"LGPL3","author":"deroad","description":"XOR-16 checksum"},{"name":"xor8","license":"LGPL3","author":"deroad","description":"XOR-8 checksum"},{"name":"xxhash32","license":"LGPL3","author":"deroad","description":"xxHash32 non-cryptographic hash"},{"name":"xxhash64","license":"LGPL3","author":"cheese-cakee","description":"xxHash64 non-cryptographic hash"}] algorithm license author description -------------------------------------------------------------------------------------------------------------- adler32 LGPL3 deroad Adler-32 checksum @@ -580,6 +582,8 @@ sha3-384 BSD-3 Andrey Jivsov SHA3-384 cryptographic hash sha3-512 BSD-3 Andrey Jivsov SHA3-512 cryptographic hash sha384 BSD-3 Aaron D. Gifford SHA-384 cryptographic hash sha512 BSD-3 Aaron D. Gifford SHA-512 cryptographic hash +shake-128 BSD-3 Andrey Jivsov SHAKE-128 extendable-output function +shake-256 BSD-3 Andrey Jivsov SHAKE-256 extendable-output function sm3 LGPL2 FSF/deroad ShangMi 3 (SM3) cryptographic hash ssdeep LGPL3 deroad SSDeep Context Triggered Piecewise Hash (CTPH, fuzzy hash) temperature LGPL3 Seva Binary temperature [0.0-1.0] range @@ -677,6 +681,8 @@ sha3-384 sha3-512 sha384 sha512 +shake-128 +shake-256 sm3 ssdeep temperature diff --git a/test/db/tools/rz_hash b/test/db/tools/rz_hash index 53acb924f1..36fcf0725c 100644 --- a/test/db/tools/rz_hash +++ b/test/db/tools/rz_hash @@ -338,6 +338,8 @@ ____hm sha3-384 BSD-3 Andrey Jivsov SHA3-384 cryptog ____hm sha3-512 BSD-3 Andrey Jivsov SHA3-512 cryptographic hash ____hm sha384 BSD-3 Aaron D. Gifford SHA-384 cryptographic hash ____hm sha512 BSD-3 Aaron D. Gifford SHA-512 cryptographic hash +____h_ shake-128 BSD-3 Andrey Jivsov SHAKE-128 extendable-output function +____h_ shake-256 BSD-3 Andrey Jivsov SHAKE-256 extendable-output function ____hm sm3 LGPL2 FSF/deroad ShangMi 3 (SM3) cryptographic hash ____h_ ssdeep LGPL3 deroad SSDeep Context Triggered Piecewise Hash (CTPH, fuzzy hash) ____h_ temperature LGPL3 Seva Binary temperature [0.0-1.0] range @@ -821,6 +823,8 @@ string: 0x00000000-0x00000005 sha3-384: computed hash doesn't match the expected string: 0x00000000-0x00000005 sha3-512: computed hash doesn't match the expected one string: 0x00000000-0x00000005 sha384: computed hash doesn't match the expected one string: 0x00000000-0x00000005 sha512: computed hash doesn't match the expected one +string: 0x00000000-0x00000005 shake-128: computed hash doesn't match the expected one +string: 0x00000000-0x00000005 shake-256: computed hash doesn't match the expected one string: 0x00000000-0x00000005 sm3: computed hash doesn't match the expected one string: 0x00000000-0x00000005 ssdeep: computed hash doesn't match the expected one string: 0x00000000-0x00000005 temperature: computed hash doesn't match the expected one diff --git a/test/unit/test_hash.c b/test/unit/test_hash.c index cbd70c7cd3..c6cf5006d6 100644 --- a/test/unit/test_hash.c +++ b/test/unit/test_hash.c @@ -58,6 +58,8 @@ static hash_data_t hashes_to_test[] = { { INDATA("password"), .algo = "keccak-256", .expected = "b68fe43f0d1a0d7aef123722670be50268e15365401c442f8806ef83b612976b" }, { INDATA("password"), .algo = "keccak-384", .expected = "e0779e9bb200a589bc70e499a9f7db1006e181519394990ef41800bebe452c23b4a8372fd89df8d5e0d951af240be7bc" }, { INDATA("password"), .algo = "keccak-512", .expected = "a6818b8188b36c44d17784c5551f63accc5deaf8786f9d0ad1ae3cd8d887cbab4f777286dbb315fb14854c8774dc0d10b5567e4a705536cc2a1d61ec0a16a7a6" }, + { INDATA("password"), .algo = "shake-128", .expected = "0ac28ef634f3a8415ae5ef6e614bf11f1a18df4d1fa05a4dd1e6a0acd93bfc57" }, + { INDATA("password"), .algo = "shake-256", .expected = "a5ee08f8e3abe7d592f6de77f1d3298a1149eba68b97f091c90b7736a1be63ab2d425f94c5346cac64807f20f654c5ad9063a4d12902c5e45533491215754883" }, { INDATA("password"), .algo = "sm3", .expected = "08594e140bcc046e345325435218f67a85c38c63de6443b197b544d70ee62f26" }, { INDATA("password"), .algo = "blake3", .expected = "7f2611ba158b6dcea4a69c229c303358c5e04493abeadee106a4bfa464d55787" }, { INDATA("password"), .algo = "blake2b", .expected = "7c863950ac93c93692995e4732ce1e1466ad74a775352ffbaaf2a4a4ce9b549d0b414a1f3150452be6c7c72c694a7cb46f76452917298d33e67611f0a42addb8" }, @@ -276,11 +278,84 @@ bool test_message_digest_small_block_stringified() { mu_end; } +bool test_shake_edge_cases() { + RzHash *rh = rz_hash_new(); + mu_assert_notnull(rh, "rz_hash_new"); + + { + ut8 data[170]; + for (size_t i = 0; i < 170; i++) { + data[i] = (ut8)i; + } + + RzHashCfg *md = rz_hash_cfg_new_with_algo2(rh, "shake-128"); + mu_assert_notnull(md, "shake-128 cfg"); + rz_hash_cfg_update(md, data, 3); + rz_hash_cfg_update(md, data + 3, 2); + rz_hash_cfg_update(md, data + 5, 6); + rz_hash_cfg_update(md, data + 11, 152); + rz_hash_cfg_update(md, data + 163, 6); + rz_hash_cfg_update(md, data + 169, 1); + rz_hash_cfg_final(md); + + RzHashCfg *md_ref = rz_hash_cfg_new_with_algo2(rh, "shake-128"); + mu_assert_notnull(md_ref, "shake-128 ref cfg"); + rz_hash_cfg_update(md_ref, data, 170); + rz_hash_cfg_final(md_ref); + + RzHashSize size, size_ref; + char *res = rz_hash_cfg_get_result_string(md, "shake-128", &size, false); + char *res_ref = rz_hash_cfg_get_result_string(md_ref, "shake-128", &size_ref, false); + mu_assert_streq(res, res_ref, "shake-128 chunked matches reference"); + + free(res); + free(res_ref); + rz_hash_cfg_free(md); + rz_hash_cfg_free(md_ref); + } + + { + ut8 data[138]; + for (size_t i = 0; i < 138; i++) { + data[i] = (ut8)i; + } + + RzHashCfg *md = rz_hash_cfg_new_with_algo2(rh, "shake-256"); + mu_assert_notnull(md, "shake-256 cfg"); + rz_hash_cfg_update(md, data, 3); + rz_hash_cfg_update(md, data + 3, 2); + rz_hash_cfg_update(md, data + 5, 6); + rz_hash_cfg_update(md, data + 11, 120); + rz_hash_cfg_update(md, data + 131, 6); + rz_hash_cfg_update(md, data + 137, 1); + rz_hash_cfg_final(md); + + RzHashCfg *md_ref = rz_hash_cfg_new_with_algo2(rh, "shake-256"); + mu_assert_notnull(md_ref, "shake-256 ref cfg"); + rz_hash_cfg_update(md_ref, data, 138); + rz_hash_cfg_final(md_ref); + + RzHashSize size, size_ref; + char *res = rz_hash_cfg_get_result_string(md, "shake-256", &size, false); + char *res_ref = rz_hash_cfg_get_result_string(md_ref, "shake-256", &size_ref, false); + mu_assert_streq(res, res_ref, "shake-256 chunked matches reference"); + + free(res); + free(res_ref); + rz_hash_cfg_free(md); + rz_hash_cfg_free(md_ref); + } + + rz_hash_free(rh); + mu_end; +} + bool all_tests() { mu_run_test(test_message_digest_configure); mu_run_test(test_message_digest_api_stringified); mu_run_test(test_message_digest_hmac_stringified); mu_run_test(test_message_digest_small_block_stringified); + mu_run_test(test_shake_edge_cases); return tests_passed != tests_run; }