From 37f741cf06560ba2a621430e3aa5fe2bc391c5a2 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Tue, 5 May 2026 00:17:52 +0200 Subject: [PATCH] feat: whirlpool --- include/digest_algos.h | 5 +- include/libft_ssl.h | 1 + include/whirlpool.h | 18 ++++ include/whirlpool_internal.h | 12 +++ src/Makefile.am | 7 +- src/whirlpool/whirlpool.c | 44 ++++++++ src/whirlpool/whirlpool_compress.c | 157 +++++++++++++++++++++++++++++ src/whirlpool/whirlpool_final.c | 63 ++++++++++++ src/whirlpool/whirlpool_init.c | 10 ++ src/whirlpool/whirlpool_update.c | 4 + 10 files changed, 318 insertions(+), 3 deletions(-) create mode 100644 include/whirlpool.h create mode 100644 include/whirlpool_internal.h create mode 100644 src/whirlpool/whirlpool.c create mode 100644 src/whirlpool/whirlpool_compress.c create mode 100644 src/whirlpool/whirlpool_final.c create mode 100644 src/whirlpool/whirlpool_init.c create mode 100644 src/whirlpool/whirlpool_update.c diff --git a/include/digest_algos.h b/include/digest_algos.h index effc4eb..5b9517c 100644 --- a/include/digest_algos.h +++ b/include/digest_algos.h @@ -1,2 +1,3 @@ -DIGEST_ALGO(md5, 16, 64) -DIGEST_ALGO(sha256, 32, 64) +DIGEST_ALGO(md5, 16, 64) +DIGEST_ALGO(sha256, 32, 64) +DIGEST_ALGO(whirlpool, 64, 64) diff --git a/include/libft_ssl.h b/include/libft_ssl.h index 63733fd..9db7c45 100644 --- a/include/libft_ssl.h +++ b/include/libft_ssl.h @@ -6,6 +6,7 @@ #include "md5.h" #include "sha256.h" +#include "whirlpool.h" struct digest_algo { diff --git a/include/whirlpool.h b/include/whirlpool.h new file mode 100644 index 0000000..cabc004 --- /dev/null +++ b/include/whirlpool.h @@ -0,0 +1,18 @@ +#ifndef WHIRLPOOL_H +#define WHIRLPOOL_H + +#include +#include + +struct whirlpool_ctx +{ + uint64_t state[8]; + uint64_t count; + uint8_t buf[64]; +}; + +void whirlpool_init(void *ctx); +void whirlpool_update(void *ctx, const uint8_t *data, size_t len); +void whirlpool_final(void *ctx, uint8_t *out); + +#endif diff --git a/include/whirlpool_internal.h b/include/whirlpool_internal.h new file mode 100644 index 0000000..e74b10f --- /dev/null +++ b/include/whirlpool_internal.h @@ -0,0 +1,12 @@ +#ifndef WHIRLPOOL_INTERNAL_H +#define WHIRLPOOL_INTERNAL_H + +#include "whirlpool.h" + +extern const uint8_t g_whirlpool_S[256]; +extern const uint8_t g_whirlpool_MDS_ROW[8]; +extern const uint64_t g_whirlpool_RC[11][8]; + +void whirlpool_compress(struct whirlpool_ctx *ctx, const uint8_t block[64]); + +#endif diff --git a/src/Makefile.am b/src/Makefile.am index 4babaa9..a9e83c3 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -10,7 +10,12 @@ libft_ssl_la_SOURCES = libft_ssl.c \ sha256/sha256_init.c \ sha256/sha256_compress.c \ sha256/sha256_update.c \ - sha256/sha256_final.c + sha256/sha256_final.c \ + whirlpool/whirlpool.c \ + whirlpool/whirlpool_init.c \ + whirlpool/whirlpool_compress.c \ + whirlpool/whirlpool_update.c \ + whirlpool/whirlpool_final.c AM_CFLAGS = -std=c99 $(STRICT_CFLAGS) libft_ssl_la_CPPFLAGS = -I$(top_srcdir)/include diff --git a/src/whirlpool/whirlpool.c b/src/whirlpool/whirlpool.c new file mode 100644 index 0000000..8ae97ac --- /dev/null +++ b/src/whirlpool/whirlpool.c @@ -0,0 +1,44 @@ +#include "whirlpool_internal.h" + +const uint8_t g_whirlpool_S[256] = { + 0x18, 0x23, 0xC6, 0xE8, 0x87, 0xB8, 0x01, 0x4F, 0x36, 0xA6, 0xD2, 0xF5, + 0x79, 0x6F, 0x91, 0x52, 0x60, 0xBC, 0x9B, 0x8E, 0xA3, 0x0C, 0x7B, 0x35, + 0x1D, 0xE0, 0xD7, 0xC2, 0x2E, 0x4B, 0xFE, 0x57, 0x15, 0x77, 0x37, 0xE5, + 0x9F, 0xF0, 0x4A, 0xDA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03, 0x56, 0x44, 0x7F, 0xA9, 0x2A, 0xBB, 0xC1, 0x53, + 0xDC, 0x0B, 0x9D, 0x6C, 0x31, 0x74, 0xF6, 0x46, 0xAC, 0x89, 0x14, 0xE1, + 0x16, 0x3A, 0x69, 0x09, 0x70, 0xB6, 0xD0, 0xED, 0xCC, 0x42, 0x98, 0xA4, + 0x28, 0x5C, 0xF8, 0x86, +}; + +const uint8_t g_whirlpool_MDS_ROW[8] = { + 0x01, 0x01, 0x04, 0x01, 0x08, 0x05, 0x02, 0x09 +}; + +const uint64_t g_whirlpool_RC[11][8] = { + { 0x0000000000000000ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0x1823C6E887B8014FULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0x36A6D2F5796F9152ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0x60BC9B8EA30C7B35ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0x1DE0D7C22E4BFE57ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0x157737E59FF04ADAULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0x58C9290AB1A06B85ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0xBD5D10F4CB3E0567ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0xE427418BA77D95D8ULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0xFBEE7C66DD17479EULL, 0, 0, 0, 0, 0, 0, 0 }, + { 0xCA2DBF07AD5A8333ULL, 0, 0, 0, 0, 0, 0, 0 }, +}; diff --git a/src/whirlpool/whirlpool_compress.c b/src/whirlpool/whirlpool_compress.c new file mode 100644 index 0000000..2af8393 --- /dev/null +++ b/src/whirlpool/whirlpool_compress.c @@ -0,0 +1,157 @@ +#include "whirlpool_internal.h" +#include +#include +#include + +/* Forward declarations */ +static inline uint8_t gf_mul2(uint8_t a); +static uint8_t gf_mul(uint8_t a, uint8_t b); +static void sub_bytes(uint64_t state[8]); +static void shift_columns(uint64_t state[8]); +static void mix_rows(uint64_t state[8]); +static inline void add_round_key(uint64_t state[8], const uint64_t key[8]); +static void decode_block(const uint8_t block[64], uint64_t W[8]); +static void round_function(uint64_t state[8], const uint64_t + key[8]); +/* ---------------- */ + +void whirlpool_compress(struct whirlpool_ctx *ctx, const uint8_t block[64]) +{ + uint64_t W[8]; + uint64_t L[8]; + uint64_t K[11][8]; + + decode_block(block, W); + + memcpy(K[0], ctx->state, sizeof(ctx->state)); + + for (uint8_t r = 1; r <= 10; ++r) + { + memcpy(K[r], K[r - 1], sizeof(uint64_t) * 8); + round_function(K[r], g_whirlpool_RC[r]); + } + + memcpy(L, W, sizeof(W)); + add_round_key(L, K[0]); + + for (uint8_t r = 1; r <= 10; ++r) + round_function(L, K[r]); + + for (uint8_t i = 0; i <= 7; ++i) + ctx->state[i] ^= L[i] ^ W[i]; + +} + +static void +round_function(uint64_t state[8], const uint64_t key[8]) +{ + sub_bytes(state); + shift_columns(state); + mix_rows(state); + add_round_key(state, key); +} + +static void +decode_block(const uint8_t block[64], uint64_t W[8]) +{ + for (uint8_t i = 0; i < 8; ++i) + { + const uint8_t i8 = i * 8; + W[i] = ((uint64_t)block[i8 + 0] << 56) + | ((uint64_t)block[i8 + 1] << 48) + | ((uint64_t)block[i8 + 2] << 40) + | ((uint64_t)block[i8 + 3] << 32) + | ((uint64_t)block[i8 + 4] << 24) + | ((uint64_t)block[i8 + 5] << 16) + | ((uint64_t)block[i8 + 6] << 8) + | ((uint64_t)block[i8 + 7] << 0); + } +} + +static inline uint8_t +gf_mul2(uint8_t a) +{ + return (uint8_t)((a << 1) ^ (a & 0x80 ? 0x1D : 0x00)); +} + +static uint8_t +gf_mul(uint8_t a, uint8_t b) +{ + uint8_t result = 0; + + while (0 != b) + { + if (b & 1) + result ^= a; + a = gf_mul2(a); + b >>= 1; + } + return result; +} + +static void +sub_bytes(uint64_t state[8]) +{ + for (uint8_t i = 0; i < 8; ++i) + { + uint64_t w = 0; + for (uint8_t j = 0; j < 8; ++j) + { + uint8_t byte = (state[i] >> (56 - 8 * j)) & 0xFF; + w |= (uint64_t)g_whirlpool_S[byte] << (56 - 8 * j); + } + state[i] = w; + } +} + +static void +shift_columns(uint64_t state[8]) +{ + uint64_t tmp[8]; + + for (uint8_t i = 0; i < 8; ++i) + { + tmp[i] = (state[(i - 0 + 8) & 7] & 0xFF00000000000000ULL) + | (state[(i - 1 + 8) & 7] & 0x00FF000000000000ULL) + | (state[(i - 2 + 8) & 7] & 0x0000FF0000000000ULL) + | (state[(i - 3 + 8) & 7] & 0x000000FF00000000ULL) + | (state[(i - 4 + 8) & 7] & 0x00000000FF000000ULL) + | (state[(i - 5 + 8) & 7] & 0x0000000000FF0000ULL) + | (state[(i - 6 + 8) & 7] & 0x000000000000FF00ULL) + | (state[(i - 7 + 8) & 7] & 0x00000000000000FFULL); + } + + for(uint8_t i = 0; i < 8; ++i) + state[i] = tmp[i]; +} + +static inline void +add_round_key(uint64_t state[8], const uint64_t key[8]) +{ + for (uint8_t i = 0; i < 8; ++i) + state[i] ^= key[i]; +} + +static void +mix_rows(uint64_t state[8]) +{ + uint8_t line[8]; + uint8_t out[8]; + + for (uint8_t i = 0; i < 8; ++i) + { + for (uint8_t j = 0; j < 8; ++j) + line[j] = (state[i] >> (56 - 8 * j)) & 0xFF; + + for (uint8_t j = 0; j < 8; ++j) + { + out[j] = 0; + for (uint8_t k = 0; k < 8; ++k) + out[j] ^= gf_mul(g_whirlpool_MDS_ROW[(j - k + 8) & 7], line[k]); + } + + state[i] = 0; + for (uint8_t j = 0; j < 8; ++j) + state[i] |= (uint64_t)out[j] << (56 - 8 * j); + } +} diff --git a/src/whirlpool/whirlpool_final.c b/src/whirlpool/whirlpool_final.c new file mode 100644 index 0000000..0be4b8a --- /dev/null +++ b/src/whirlpool/whirlpool_final.c @@ -0,0 +1,63 @@ +#include "whirlpool_internal.h" +#include + +/* Forward declarations */ +static void whirlpool_pad(struct whirlpool_ctx *ctx); +static void whirlpool_encode(const struct whirlpool_ctx *ctx, uint8_t out[64]); +static inline void whirlpool_write_len(struct whirlpool_ctx *ctx, uint64_t bitlen); +/* ---------------- */ + +void whirlpool_final(void *ctx, uint8_t *out) +{ + struct whirlpool_ctx *local_ctx = ctx; + + whirlpool_pad(local_ctx); + whirlpool_encode(local_ctx, out); +} + +static void +whirlpool_pad(struct whirlpool_ctx *ctx) +{ + uint64_t bitlen = ctx->count * 8; + + ctx->buf[ctx->count++ & 63] = 0x80; + if ((ctx->count & 63) > 32) + { + while ((ctx->count & 63) != 0) + ctx->buf[ctx->count++ & 63] = 0x00; + whirlpool_compress(ctx, ctx->buf); + } + while ((ctx->count & 63) < 32) + ctx->buf[ctx->count++ & 63] = 0x00; + whirlpool_write_len(ctx, bitlen); + whirlpool_compress(ctx, ctx->buf); +} + +static inline void +whirlpool_write_len(struct whirlpool_ctx *ctx, uint64_t bitlen) +{ + for (uint8_t i = 32; i < 56; ++i) + ctx->buf[i] = 0x00; + for (uint8_t i = 0; i < 8; ++i) + ctx->buf[56 + i] = (uint8_t)(bitlen >> (56 - 8 * i)); +} + + +static void +whirlpool_encode(const struct whirlpool_ctx *ctx, uint8_t out[64]) +{ + for (uint8_t i = 0; i < 8; ++i) + { + const uint8_t i8 = i * 8; + uint64_t v = ctx->state[i]; + + out[i8 + 0] = (uint8_t)(v >> 56); + out[i8 + 1] = (uint8_t)(v >> 48); + out[i8 + 2] = (uint8_t)(v >> 40); + out[i8 + 3] = (uint8_t)(v >> 32); + out[i8 + 4] = (uint8_t)(v >> 24); + out[i8 + 5] = (uint8_t)(v >> 16); + out[i8 + 6] = (uint8_t)(v >> 8); + out[i8 + 7] = (uint8_t)(v >> 0); + } +} diff --git a/src/whirlpool/whirlpool_init.c b/src/whirlpool/whirlpool_init.c new file mode 100644 index 0000000..31b539d --- /dev/null +++ b/src/whirlpool/whirlpool_init.c @@ -0,0 +1,10 @@ +#include "whirlpool.h" + +void whirlpool_init(void *ctx) +{ + struct whirlpool_ctx *local_ctx = ctx; + + for (uint8_t i = 0; i < 8; ++i) + local_ctx->state[i] = 0; + local_ctx->count = 0; +} diff --git a/src/whirlpool/whirlpool_update.c b/src/whirlpool/whirlpool_update.c new file mode 100644 index 0000000..888f434 --- /dev/null +++ b/src/whirlpool/whirlpool_update.c @@ -0,0 +1,4 @@ +#include "whirlpool_internal.h" +#include "digest_update.h" + +IMPL_DIGEST_UPDATE(whirlpool, 63, whirlpool_compress)