From 35c32a9c323046f2e8d2c0380c3e512ee5a88dd4 Mon Sep 17 00:00:00 2001 From: lohhiiccc <96543753+lohhiiccc@users.noreply.github.com> Date: Thu, 30 Apr 2026 11:01:46 +0200 Subject: [PATCH] feat(libft_ssl): X-macro digest algo registry --- Makefile.am | 4 +++- include/digest_algos.h | 2 ++ include/libft_ssl.h | 16 +++++++++++++--- include/md5.h | 13 +++++++++++++ include/sha256.h | 13 +++++++++++++ src/libft_ssl.c | 37 ++++++++++++++++++++----------------- 6 files changed, 64 insertions(+), 21 deletions(-) create mode 100644 include/digest_algos.h create mode 100644 include/md5.h create mode 100644 include/sha256.h diff --git a/Makefile.am b/Makefile.am index 755af49..2ad04de 100644 --- a/Makefile.am +++ b/Makefile.am @@ -5,7 +5,9 @@ SUBDIRS = src pkgincludedir = $(includedir)/$(PACKAGE_NAME) pkginclude_HEADERS = \ include/libft_ssl.h \ - include/compiler.h + include/compiler.h \ + include/md5.h \ + include/sha256.h pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = libft_ssl.pc diff --git a/include/digest_algos.h b/include/digest_algos.h new file mode 100644 index 0000000..effc4eb --- /dev/null +++ b/include/digest_algos.h @@ -0,0 +1,2 @@ +DIGEST_ALGO(md5, 16, 64) +DIGEST_ALGO(sha256, 32, 64) diff --git a/include/libft_ssl.h b/include/libft_ssl.h index 82b9a4c..63733fd 100644 --- a/include/libft_ssl.h +++ b/include/libft_ssl.h @@ -4,18 +4,28 @@ #include #include +#include "md5.h" +#include "sha256.h" + struct digest_algo { const char *name; size_t digest_size; size_t block_size; - size_t ctx_size; void (*init) (void *ctx); void (*update)(void *ctx, const uint8_t *data, size_t len); void (*final) (void *ctx, uint8_t *out); }; -extern const struct digest_algo g_md5; -extern const struct digest_algo g_sha256; +union digest_ctx +{ +#define DIGEST_ALGO(lower, ds, bs) struct lower##_ctx lower; +#include "digest_algos.h" +#undef DIGEST_ALGO +}; + +#define DIGEST_ALGO(lower, ds, bs) extern const struct digest_algo g_##lower; +#include "digest_algos.h" +#undef DIGEST_ALGO #endif diff --git a/include/md5.h b/include/md5.h new file mode 100644 index 0000000..507fcff --- /dev/null +++ b/include/md5.h @@ -0,0 +1,13 @@ +#ifndef MD5_H +#define MD5_H + +#include + +struct md5_ctx +{ + uint32_t state[4]; + uint64_t count; + uint8_t buf[64]; +}; + +#endif diff --git a/include/sha256.h b/include/sha256.h new file mode 100644 index 0000000..995de15 --- /dev/null +++ b/include/sha256.h @@ -0,0 +1,13 @@ +#ifndef SHA256_H +#define SHA256_H + +#include + +struct sha256_ctx +{ + uint32_t state[8]; + uint64_t count; + uint8_t buf[64]; +}; + +#endif diff --git a/src/libft_ssl.c b/src/libft_ssl.c index 3ce4a89..5eedc96 100644 --- a/src/libft_ssl.c +++ b/src/libft_ssl.c @@ -1,21 +1,24 @@ +#include "compiler.h" #include "libft_ssl.h" -const struct digest_algo g_md5 = { - .name = "md5", - .digest_size = 16, - .block_size = 64, - .ctx_size = 0, - .init = NULL, - .update = NULL, - .final = NULL, -}; +#define DIGEST_ALGO(lower, ds, bs) \ +static void lower##_init(__unused void *ctx) {} \ +static void lower##_update(__unused void *ctx, \ + __unused const uint8_t *data, \ + __unused size_t len) {} \ +static void lower##_final(__unused void *ctx, \ + __unused uint8_t *out) {} +#include "digest_algos.h" +#undef DIGEST_ALGO -const struct digest_algo g_sha256 = { - .name = "sha256", - .digest_size = 32, - .block_size = 64, - .ctx_size = 0, - .init = NULL, - .update = NULL, - .final = NULL, +#define DIGEST_ALGO(lower, ds, bs) \ +const struct digest_algo g_##lower = { \ + .name = #lower, \ + .digest_size = (ds), \ + .block_size = (bs), \ + .init = lower##_init, \ + .update = lower##_update, \ + .final = lower##_final, \ }; +#include "digest_algos.h" +#undef DIGEST_ALGO