kee

Offline IOU signer with QR as transport
git clone https://holbrook.no/src/kee
Info | Log | Files | Refs | README | LICENSE

commit a465d02f7cd692138649501e856d85937ad9f97b
parent 22e4f7783ff40252cb51bdc112676159381d091a
Author: lash <dev@holbrook.no>
Date:   Thu, 28 Mar 2024 21:15:38 +0000

Add cadir simple prototype, test

Diffstat:
MMakefile | 10++++++++--
Asrc/cadir.c | 45+++++++++++++++++++++++++++++++++++++++++++++
Asrc/cadir.h | 10++++++++++
Msrc/digest.c | 15++++++++++++---
Msrc/digest.h | 1+
Asrc/tests/Makefile | 22++++++++++++++++++++++
Asrc/tests/cadir.c | 21+++++++++++++++++++++
Asrc/tests/debug.c | 7+++++++
Asrc/tests/testdata_resource/f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7 | 2++
9 files changed, 128 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -20,8 +20,14 @@ run: gtk all debug: gtk all G_DEBUG=3 G_MESSAGES_DEBUG=all ./src/gtk/a.out -test: gtk all - make -C src/gtk/tests +#test: gtk all test_src test_gtk +test: test_src + +test_src: all + make -C src/tests test + +test_gtk: gtk all test_src + make -C src/gtk/tests test testdata: rm -vrf testdata_mdb diff --git a/src/cadir.c b/src/cadir.c @@ -0,0 +1,45 @@ +#include <string.h> +#include <fcntl.h> +#include <unistd.h> + +#include "cadir.h" +#include "hex.h" + + +/// \todo check hex leftover space +int cadir_get(const char *dirpath, enum CadirKeyType key_type, const char *key, char *out, size_t *out_len) { + int r; + int c; + int fd; + char *p; + char path[1024]; + size_t l; + + strcpy(path, dirpath); + c = strlen(dirpath) - 1; + p = path + c; + if (*p != '/') { + *(p+1) = '/'; + *(p+2) = 0; + c++; + } + + p = path + c + 1; + l = 129; + r = bin_to_hex((unsigned char*)key, 64, (unsigned char*)p, &l); + if (r) { + return 1; + } + p += l; + *p = 0; + + fd = open(path, O_RDONLY); + if (fd < 0) { + return 1; + } + l = read(fd, out, *out_len); + close(fd); + *out_len = l; + + return 0; +} diff --git a/src/cadir.h b/src/cadir.h @@ -0,0 +1,10 @@ +#ifndef _CADIR_H +#define _CADIR_H + +enum CadirKeyType { + CADIR_KEYTYPE_ANY, +}; + +int cadir_get(const char *dirpath, enum CadirKeyType key_type, const char *key, char *out, size_t *out_len); + +#endif // _CADIR_H diff --git a/src/digest.c b/src/digest.c @@ -4,17 +4,20 @@ #include "digest.h" -int calculate_digest(const char *in, size_t in_len, char *out) { +int calculate_digest_algo(const char *in, size_t in_len, char *out, enum gcry_md_algos algo) { gcry_error_t e; gcry_md_hd_t h; unsigned char *v; static unsigned int digest_len; + if (algo == GCRY_MD_NONE) { + algo = GCRY_MD_SHA256; + } if (digest_len == 0) { - digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256); + digest_len = gcry_md_get_algo_dlen(algo); } - e = gcry_md_open(&h, GCRY_MD_SHA256, GCRY_MD_FLAG_SECURE); + e = gcry_md_open(&h, algo, GCRY_MD_FLAG_SECURE); if (e) { return ERR_DIGESTFAIL; } @@ -25,3 +28,9 @@ int calculate_digest(const char *in, size_t in_len, char *out) { gcry_md_close(h); return ERR_OK; } + +int calculate_digest(const char *in, size_t in_len, char *out) { + return calculate_digest_algo(in, in_len, out, GCRY_MD_NONE); +} + + diff --git a/src/digest.h b/src/digest.h @@ -19,6 +19,7 @@ extern "C" { * */ int calculate_digest(const char *in, size_t in_len, char *out); + int calculate_digest_algo(const char *in, size_t in_len, char *out, enum gcry_md_algos algo); #ifdef __cplusplus } diff --git a/src/tests/Makefile b/src/tests/Makefile @@ -0,0 +1,22 @@ +OBJS := $(patsubst %.c,%.o,$(wildcard *.c)) +LINKOBJS := $(wildcard ../*.o) ../aux/varint/varint.o +INCLUDES := -I.. +CFLAGS += `pkg-config --cflags gtk4 gstreamer-1.0` $(INCLUDES) -g3 -Wall +LIBS := `pkg-config --libs gtk4 zlib lmdb libgcrypt libxdg-basedir gstreamer-1.0` -lb64 +LDFLAGS += $(LIBS) + +all: obj_debug $(OBJS) + +obj_debug: + $(CC) $(CFLAGS) -c debug.c -o debug.o + +%.o: %.c + $(CC) $(CFLAGS) $< -o test_$* debug.o $(LINKOBJS) $(LDFLAGS) + +test_run: $(wildcard test_*) + ./$< + +test: all test_run + +clean: + rm -vf test_* diff --git a/src/tests/cadir.c b/src/tests/cadir.c @@ -0,0 +1,21 @@ +#include <gcrypt.h> + +#include "digest.h" +#include "cadir.h" + +int main(int argc, char **argv) { + int r; + const char *data = "foo"; + char digest[64]; + char result[256]; + size_t l; + calculate_digest_algo(data, 3, digest, GCRY_MD_SHA512); + + l = 256; + r = cadir_get("./testdata_resource", CADIR_KEYTYPE_ANY, digest, result, &l); + if (r) { + return 1; + } + + return strcmp(result, data); +} diff --git a/src/tests/debug.c b/src/tests/debug.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +#include "debug.h" + +void debug_log(enum debugLevel level, const char *s) { + fprintf(stderr, s); +} diff --git a/src/tests/testdata_resource/f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7 b/src/tests/testdata_resource/f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7 @@ -0,0 +1 @@ +foo +\ No newline at end of file