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)
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

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 <stdint.h>
#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

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"
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