feat(libft_ssl): X-macro digest algo registry

This commit is contained in:
lohhiiccc 2026-04-30 11:01:46 +02:00
parent f21811b66f
commit 35c32a9c32
6 changed files with 64 additions and 21 deletions

View file

@ -5,7 +5,9 @@ SUBDIRS = src
pkgincludedir = $(includedir)/$(PACKAGE_NAME) pkgincludedir = $(includedir)/$(PACKAGE_NAME)
pkginclude_HEADERS = \ pkginclude_HEADERS = \
include/libft_ssl.h \ include/libft_ssl.h \
include/compiler.h include/compiler.h \
include/md5.h \
include/sha256.h
pkgconfigdir = $(libdir)/pkgconfig pkgconfigdir = $(libdir)/pkgconfig
pkgconfig_DATA = libft_ssl.pc pkgconfig_DATA = libft_ssl.pc

2
include/digest_algos.h Normal file
View file

@ -0,0 +1,2 @@
DIGEST_ALGO(md5, 16, 64)
DIGEST_ALGO(sha256, 32, 64)

View file

@ -4,18 +4,28 @@
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "md5.h"
#include "sha256.h"
struct digest_algo struct digest_algo
{ {
const char *name; const char *name;
size_t digest_size; size_t digest_size;
size_t block_size; size_t block_size;
size_t ctx_size;
void (*init) (void *ctx); void (*init) (void *ctx);
void (*update)(void *ctx, const uint8_t *data, size_t len); void (*update)(void *ctx, const uint8_t *data, size_t len);
void (*final) (void *ctx, uint8_t *out); void (*final) (void *ctx, uint8_t *out);
}; };
extern const struct digest_algo g_md5; union digest_ctx
extern const struct digest_algo g_sha256; {
#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 #endif

13
include/md5.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef MD5_H
#define MD5_H
#include <stdint.h>
struct md5_ctx
{
uint32_t state[4];
uint64_t count;
uint8_t buf[64];
};
#endif

13
include/sha256.h Normal file
View file

@ -0,0 +1,13 @@
#ifndef SHA256_H
#define SHA256_H
#include <stdint.h>
struct sha256_ctx
{
uint32_t state[8];
uint64_t count;
uint8_t buf[64];
};
#endif

View file

@ -1,21 +1,24 @@
#include "compiler.h"
#include "libft_ssl.h" #include "libft_ssl.h"
const struct digest_algo g_md5 = { #define DIGEST_ALGO(lower, ds, bs) \
.name = "md5", static void lower##_init(__unused void *ctx) {} \
.digest_size = 16, static void lower##_update(__unused void *ctx, \
.block_size = 64, __unused const uint8_t *data, \
.ctx_size = 0, __unused size_t len) {} \
.init = NULL, static void lower##_final(__unused void *ctx, \
.update = NULL, __unused uint8_t *out) {}
.final = NULL, #include "digest_algos.h"
}; #undef DIGEST_ALGO
const struct digest_algo g_sha256 = { #define DIGEST_ALGO(lower, ds, bs) \
.name = "sha256", const struct digest_algo g_##lower = { \
.digest_size = 32, .name = #lower, \
.block_size = 64, .digest_size = (ds), \
.ctx_size = 0, .block_size = (bs), \
.init = NULL, .init = lower##_init, \
.update = NULL, .update = lower##_update, \
.final = NULL, .final = lower##_final, \
}; };
#include "digest_algos.h"
#undef DIGEST_ALGO