libqaeda

Unnamed repository; edit this file 'description' to name the repository.
Info | Log | Files | Refs | README | LICENSE

commit 2735973c9b43a43d8efeccc3b04dcfb67c27b4c0
parent 40e878e3eff0be3f3a8c07352de05fb1ecd31e3a
Author: lash <dev@holbrook.no>
Date:   Sat, 29 Mar 2025 19:42:25 +0000

WIP implement privkey in store, default key

Diffstat:
Msrc/crypto/gcrypt.c | 32++++++++++++++++++++++++++++----
Msrc/lq/crypto.h | 4++++
Msrc/store/file.c | 21+++++++++------------
Msrc/test/Makefile | 2+-
Msrc/test/test_crypto.c | 2+-
5 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/src/crypto/gcrypt.c b/src/crypto/gcrypt.c @@ -59,7 +59,8 @@ static const char *gpg_version = NULL; static int gpg_cfg_idx_dir; /// default digest id. -static int gpg_passphrase_digest = GCRY_MD_SHA256; +//static int gpg_passphrase_digest = GCRY_MD_SHA256; +static int gpg_passphrase_digest = GCRY_MD_SHA512; /// digest length of hashed password. static int gpg_passphrase_digest_len; @@ -88,7 +89,8 @@ int lq_crypto_init(const char *base) { gpg_version = v; debug_x(LLOG_DEBUG, "gpg", "using gpg", 1, MORGEL_TYP_STR, 0, "version", gpg_version); - gpg_passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256); + //gpg_passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA256); + gpg_passphrase_digest_len = gcry_md_get_algo_dlen(GCRY_MD_SHA512); gpg_cfg_idx_dir = lq_config_register(LQ_TYP_STR, "CRYPTODIR"); v = path; @@ -268,7 +270,8 @@ static int calculate_digest_algo(const char *in, size_t in_len, char *out, enum static unsigned int digest_len; if (algo == GCRY_MD_NONE) { - algo = GCRY_MD_SHA256; + //algo = GCRY_MD_SHA256; + algo = GCRY_MD_SHA512; } digest_len = gcry_md_get_algo_dlen(algo); @@ -428,14 +431,35 @@ static int key_create_store(LQStore *store, struct gpg_store *gpg, const char *p b2h((unsigned char*)gpg->fingerprint, 20, (unsigned char*)buf_key+1); store = key_store_get(); if (store == NULL) { + lq_free(store); return debug_logerr(LLOG_ERROR, ERR_CRYPTO, "create store"); } - c = CHACHA20_NONCE_LENGTH_BYTES + 1; + c = LQ_FP_LEN + 1; + l += CHACHA20_NONCE_LENGTH_BYTES; r = store->put(LQ_CONTENT_KEY, store, buf_key, &c, buf_val, l); if (r) { + lq_free(store); return debug_logerr(LLOG_ERROR, ERR_CRYPTO, "put key in store"); } + + // check if already exists default, if not, set it + *buf_key = LQ_CONTENT_KEY; + c = LQ_STORE_VAL_MAX; + r = store->get(LQ_CONTENT_KEY, store, buf_key, 1, buf_val, &c); + if (r) { + if (r != ERR_NOENT) { + lq_free(store); + return debug_logerr(LLOG_ERROR, ERR_CRYPTO, "default key"); + } + c = 1; + r = store->put(LQ_CONTENT_KEY, store, buf_key, &c, buf_val, l); + if (r) { + lq_free(store); + return debug_logerr(LLOG_ERROR, ERR_CRYPTO, "write default key"); + } + } + lq_free(store); return ERR_OK; diff --git a/src/lq/crypto.h b/src/lq/crypto.h @@ -7,6 +7,10 @@ #define LQ_DIGEST_LEN 64 #endif +#ifndef LQ_DIGEST_SIG_LEN +#define LQ_DIGEST_SIG_LEN 64 +#endif + #ifndef LQ_PUBKEY_LEN #define LQ_PUBKEY_LEN 64 #endif diff --git a/src/store/file.c b/src/store/file.c @@ -4,12 +4,15 @@ #include <unistd.h> #include <fcntl.h> +#include <llog.h> +#include <hex.h> + #include "lq/crypto.h" #include "lq/io.h" #include "lq/store.h" #include "lq/err.h" #include "lq/mem.h" -#include "hex.h" +#include "debug.h" static const int store_typ_file = 3; @@ -47,14 +50,10 @@ int lq_file_content_get(enum payload_e typ, LQStore *store, const char *key, siz lq_cpy(path, p, strlen(p) + 1); p = path + strlen(path); b2h((const unsigned char*)key, (int)key_len, (unsigned char*)buf); - r = sprintf(p, "/%s", buf); - - if (r < 0) { - return ERR_READ; - } + sprintf(p, "/%s", buf); f = lq_open(path, O_RDONLY, S_IRUSR); if (f < 0) { - return ERR_READ; + return ERR_NOENT; } p = value; @@ -95,13 +94,10 @@ int lq_file_content_put(enum payload_e typ, LQStore *store, const char *key, siz lq_cpy(path, p, strlen(p) + 1); p = path + strlen(path); b2h((const unsigned char*)key, (int)*key_len, (unsigned char*)buf); - r = sprintf(p, "/%s", buf); - if (r < 0) { - return ERR_WRITE; - } + sprintf(p, "/%s", buf); f = lq_open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); if (f < 0) { - return ERR_WRITE; + return ERR_NOENT; } l = value_len; p = value; @@ -117,6 +113,7 @@ int lq_file_content_put(enum payload_e typ, LQStore *store, const char *key, siz l -= c; p += c; } + debug_x(LLOG_DEBUG, "store.file", "put file", 2, MORGEL_TYP_STR, 0, "path", path, MORGEL_TYP_NUM, 0, "bytes", c); lq_close(f); return ERR_OK; } diff --git a/src/test/Makefile b/src/test/Makefile @@ -24,7 +24,7 @@ build: #$(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../crypto/dummy.o ../mem/std.o ../store/dummy.o ../store/file.o ../io/std.o ../lq/msg.o ../lq/cert.o $(LDFLAGS) #$(CC) $(CFLAGS) test_trust.c -o test_trust_bin ../crypto/dummy.o ../mem/std.o ../store/mem.o ../lq/trust.o -lhashmap $(LDFLAGS) $(CC) $(CFLAGS) test_crypto.c -o test_crypto_bin ../store/file.o ../io/std.o ../crypto/gcrypt.o ../debug.o ../mem/std.o ../lq/config.o $(LDFLAGS) -lgcrypt - $(CC) $(CFLAGS) test_msg.c -o test_msg_bin ../store/file.o ../io/std.o ../crypto/gcrypt.o ../debug.o ../mem/std.o ../lq/config.o ../store/dummy.o ../lq/msg.o $(LDFLAGS) + $(CC) $(CFLAGS) test_msg.c -o test_msg_bin ../store/file.o ../store/dummy.o ../io/std.o ../crypto/gcrypt.o ../debug.o ../mem/std.o ../lq/config.o ../lq/msg.o $(LDFLAGS) $(CC) $(CFLAGS) test_cert.c -o test_cert_bin ../store/file.o ../io/std.o ../crypto/gcrypt.o ../debug.o ../lq/config.o ../mem/std.o ../store/dummy.o ../lq/msg.o ../lq/cert.o $(LDFLAGS) $(CC) $(CFLAGS) test_trust.c -o test_trust_bin ../mem/std.o ../store/mem.o ../crypto/gcrypt.o ../debug.o ../lq/config.o ../lq/trust.o -lhashmap $(LDFLAGS) $(CC) $(CFLAGS) test_store.c -o test_store_bin ../store/file.o ../io/std.o ../crypto/gcrypt.o ../debug.o ../lq/config.o ../mem/std.o -lhashmap $(LDFLAGS) diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c @@ -31,7 +31,7 @@ struct dummycrypto { START_TEST(check_digest) { int r; - char out[32]; + char out[LQ_DIGEST_LEN]; r = lq_digest(data, strlen(data), (char*)out); ck_assert(r == 0);