libqaeda

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

commit 983f5a1be898829c2fe453cc267b21c71a660fc3
parent 2735973c9b43a43d8efeccc3b04dcfb67c27b4c0
Author: lash <dev@holbrook.no>
Date:   Sat, 29 Mar 2025 21:34:36 +0000

WIP consistent prefix store

Diffstat:
Msrc/crypto/gcrypt.c | 36++++++++++++++++++++++--------------
Msrc/io/std.c | 1+
Msrc/store/file.c | 9++++++---
Msrc/test/test_crypto.c | 14++++++++------
Msrc/test/test_store.c | 57++++++++++++++++++++++++++++++++++++++++++++++++++++++---
Dsrc/test/testdata/0aaa | 0
Dsrc/test/testdata/0ab | 0
Dsrc/test/testdata/1aaa | 0
Dsrc/test/testdata/1aab | 0
Dsrc/test/testdata/1b | 0
Dsrc/test/testdata/1bbc | 0
Dsrc/test/testdata/2ab | 0
Dsrc/test/testdata/lq.gpg | 0
13 files changed, 91 insertions(+), 26 deletions(-)

diff --git a/src/crypto/gcrypt.c b/src/crypto/gcrypt.c @@ -172,7 +172,7 @@ int encryptb (char *ciphertext, size_t ciphertext_len, const char *indata, size_ if (r) { return debug_logerr(LLOG_ERROR, ERR_NOCRYPTO, NULL); } - memcpy(indata_raw, indata, indata_len); + lq_cpy(indata_raw, indata, indata_len); padb(indata_raw, ciphertext_len, indata_len); e = gcry_cipher_encrypt(h, (unsigned char*)ciphertext, ciphertext_len, (const unsigned char*)indata_raw, ciphertext_len); if (e) { @@ -381,7 +381,7 @@ LQStore *key_store_get() { * */ //static int key_create_file(struct gpg_store *gpg, gcry_sexp_t *key, const char *passphrase) { -static int key_create_store(LQStore *store, struct gpg_store *gpg, const char *passphrase) { +static int key_create_store(struct gpg_store *gpg, const char *passphrase) { char *p; int r; int kl; @@ -391,6 +391,7 @@ static int key_create_store(LQStore *store, struct gpg_store *gpg, const char *p size_t c; size_t m; //FILE *f; + LQStore *store; char nonce[CHACHA20_NONCE_LENGTH_BYTES]; char buf_key[LQ_STORE_KEY_MAX]; char buf_val[LQ_STORE_VAL_MAX]; @@ -426,17 +427,19 @@ static int key_create_store(LQStore *store, struct gpg_store *gpg, const char *p } lq_cpy(buf_val, nonce, CHACHA20_NONCE_LENGTH_BYTES); - lq_cpy(buf_val + CHACHA20_NONCE_LENGTH_BYTES, ciphertext, l); - *buf_key = LQ_CONTENT_KEY; - b2h((unsigned char*)gpg->fingerprint, 20, (unsigned char*)buf_key+1); + lq_cpy(buf_val + CHACHA20_NONCE_LENGTH_BYTES, ciphertext, c); + //*buf_key = LQ_CONTENT_KEY; + //b2h((unsigned char*)gpg->fingerprint, 20, (unsigned char*)buf_key+1); + //lq_cpy(buf_key+1, gpg->fingerprint, LQ_FP_LEN); + lq_cpy(buf_key, gpg->fingerprint, LQ_FP_LEN); store = key_store_get(); if (store == NULL) { lq_free(store); return debug_logerr(LLOG_ERROR, ERR_CRYPTO, "create store"); } + l = c + CHACHA20_NONCE_LENGTH_BYTES; 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); @@ -485,7 +488,7 @@ static LQPrivKey* privatekey_alloc(const char *seed, size_t seed_len, const char } // create the underlying private key. - r = key_create(gpg); + r = key_create_store(gpg, passphrase); if (r) { lq_free(gpg); lq_free(o); @@ -502,10 +505,14 @@ static LQPrivKey* privatekey_alloc(const char *seed, size_t seed_len, const char return o; } + /// Implements the interface to create a new private key. LQPrivKey* lq_privatekey_new(const char *seed, size_t seed_len, const char *passphrase, size_t passphrase_len) { int r; LQPrivKey *o; + if (passphrase == NULL) { + return NULL; + } o = privatekey_alloc(seed, seed_len, passphrase, passphrase_len); if (o == NULL) { @@ -558,15 +565,18 @@ static int key_from_file(gcry_sexp_t *key, const char *path, const char *passphr } fclose(f); - outdata = malloc(i); + outdata = lq_alloc(i); r = decryptb((char*)outdata, v, i, passphrase, nonce); if (r) { return r; } - //r = key_from_data(key, (char*)outdata, l); - c = (size_t)(*((int*)outdata)); - p = (char*)(outdata+sizeof(int)); - r = key_from_data(key, p, c); + r = key_from_data(key, (char*)outdata, strlen(outdata)); + if (r) { + return ERR_CRYPTO; + } + //c = (size_t)(*((int*)outdata)); + //p = (char*)(outdata+sizeof(int)); + //r = key_from_data(key, p, c); free(outdata); return ERR_OK; } @@ -760,8 +770,6 @@ static int sign(struct gpg_store *gpg, const char *data, size_t data_len, const } lq_cpy(gpg->last_signature + LQ_POINT_LEN, p, c); - //gcry_sexp_release(gpg->k); - return 0; } diff --git a/src/io/std.c b/src/io/std.c @@ -54,6 +54,7 @@ int lq_files(const char *path, char **files, size_t files_len) { } *(files+i+1) = NULL; lq_free(ls); + return r; } int lq_files_pfx(const char *path, char **files, size_t files_len, const char *prefix, char prefix_len) { diff --git a/src/store/file.c b/src/store/file.c @@ -16,14 +16,17 @@ static const int store_typ_file = 3; +/// \todo key and val limits proper int lq_file_content_count(enum payload_e typ, LQStore *store, const char *key, size_t key_len) { int r; char **out; + char buf[LQ_DIGEST_LEN * 2 + 1]; char pfx[1024]; out = lq_alloc(sizeof(char**) * LQ_DIRS_MAX); pfx[0] = (char)typ + 0x30; - lq_cpy(pfx+1, key, key_len); + b2h((const unsigned char*)key, (int)key_len, (unsigned char*)buf); + lq_cpy(pfx+1, buf, strlen(buf)); r = lq_files_pfx(store->userdata, out, LQ_DIRS_MAX, pfx, key_len + 1); @@ -50,7 +53,7 @@ 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); - sprintf(p, "/%s", buf); + sprintf(p, "%d%s", (char)typ, buf); f = lq_open(path, O_RDONLY, S_IRUSR); if (f < 0) { return ERR_NOENT; @@ -94,7 +97,7 @@ 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); - sprintf(p, "/%s", buf); + sprintf(p, "%d%s", (char)typ, buf); f = lq_open(path, O_CREAT | O_TRUNC | O_WRONLY, S_IRUSR | S_IWUSR); if (f < 0) { return ERR_NOENT; diff --git a/src/test/test_crypto.c b/src/test/test_crypto.c @@ -17,7 +17,7 @@ static const char privkeydata[32] = { 0xf9, 0x8a, 0x5e, 0x88, 0x62, 0x66, 0xe7, 0xae, }; -// "1234" +// "1233" static const size_t passphrase_len = 4; static const char passphrase[4] = { 0x31, 0x32, 0x33, 0x34, @@ -42,8 +42,7 @@ START_TEST(check_privatekey) { int r; LQPrivKey *pk; - - pk = lq_privatekey_new(privkeydata, LQ_PRIVKEY_LEN, NULL, 0); + pk = lq_privatekey_new(privkeydata, LQ_PRIVKEY_LEN, passphrase, passphrase_len); ck_assert_ptr_nonnull(pk); lq_privatekey_free(pk); } @@ -116,11 +115,14 @@ START_TEST(check_verify) { } END_TEST -START_TEST(check_load) { +START_TEST(check_create_load) { LQPrivKey *pk; + LQPrivKey *pk_load; - pk = lq_privatekey_load(passphrase, passphrase_len); + pk = lq_privatekey_new(privkeydata, LQ_PRIVKEY_LEN, passphrase, passphrase_len); ck_assert_ptr_nonnull(pk); + pk_load = lq_privatekey_load(passphrase, passphrase_len); + ck_assert_ptr_nonnull(pk_load); lq_privatekey_free(pk); } @@ -138,7 +140,7 @@ Suite * common_suite(void) { tcase_add_test(tc, check_publickey); tcase_add_test(tc, check_signature); tcase_add_test(tc, check_verify); - tcase_add_test(tc, check_load); + tcase_add_test(tc, check_create_load); suite_add_tcase(s, tc); return s; diff --git a/src/test/test_store.c b/src/test/test_store.c @@ -2,8 +2,9 @@ #include <stdlib.h> #include <string.h> -#include <lq/store.h> -#include <lq/mem.h> +#include "lq/store.h" +#include "lq/mem.h" +#include "lq/io.h" extern LQStore LQFileContent; @@ -11,9 +12,59 @@ extern LQStore LQFileContent; START_TEST(check_store_count) { int r; LQStore store; + char *k; + char *v; + size_t kl; + size_t vl; + char path[LQ_PATH_MAX]; lq_cpy(&store, &LQFileContent, sizeof(LQStore)); - store.userdata = "./testdata"; + lq_cpy(path, "/tmp/lqstore_file_XXXXXX", 25); + store.userdata = mktempdir(path); + *((char*)(store.userdata+24)) = '/'; + *((char*)(store.userdata+25)) = 0x0; + + k = "aaa"; + v = "foo"; + kl = 3; + vl = 3; + store.put(LQ_CONTENT_RAW, &store, k, &kl, v, vl), + + k = "ab"; + v = "bar"; + kl = 2; + vl = 3; + store.put(LQ_CONTENT_RAW, &store, k, &kl, v, vl), + + k = "aaa"; + v = "inky"; + kl = 3; + vl = 4; + store.put(LQ_CONTENT_MSG, &store, k, &kl, v, vl), + + k = "aab"; + v = "pinky"; + kl = 3; + vl = 5; + store.put(LQ_CONTENT_MSG, &store, k, &kl, v, vl), + + k = "b"; + v = "blinky"; + kl = 1; + vl = 6; + store.put(LQ_CONTENT_MSG, &store, k, &kl, v, vl), + + k = "bbc"; + v = "clyde"; + kl = 3; + vl = 5; + store.put(LQ_CONTENT_MSG, &store, k, &kl, v, vl), + + k = "bbc"; + v = "clyde"; + kl = 3; + vl = 5; + store.put(LQ_CONTENT_CERT, &store, k, &kl, v, vl), r = store.count(LQ_CONTENT_MSG, &store, "aa", 2); diff --git a/src/test/testdata/0aaa b/src/test/testdata/0aaa diff --git a/src/test/testdata/0ab b/src/test/testdata/0ab diff --git a/src/test/testdata/1aaa b/src/test/testdata/1aaa diff --git a/src/test/testdata/1aab b/src/test/testdata/1aab diff --git a/src/test/testdata/1b b/src/test/testdata/1b diff --git a/src/test/testdata/1bbc b/src/test/testdata/1bbc diff --git a/src/test/testdata/2ab b/src/test/testdata/2ab diff --git a/src/test/testdata/lq.gpg b/src/test/testdata/lq.gpg Binary files differ.