libswarm-ng

C implementation of BMT hasher, Swarmhash and Single Owner Chunk for swarm
git clone git://git.defalsify.org/libswarm-ng.git
Log | Files | Refs | Submodules | README

commit 9f5b275010fc04d453baa62c5a7ec214eef03593
parent d0c0f9711fbbfecb2ae67bd5f319609e71a471e1
Author: nolash <dev@holbrook.no>
Date:   Thu, 16 Sep 2021 11:06:23 +0200

Add keystore getput

Diffstat:
MMakefile.dev | 4++--
Msrc/keystore.c | 24+++++++++++++++++++++++-
Msrc/keystore.h | 19+++++++++++--------
Mtest/check_keystore.c | 30+++++++++++++++++++++++++++++-
Mtest/common.c | 11++++++++---
Mtest/common.h | 9++-------
6 files changed, 75 insertions(+), 22 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -45,12 +45,12 @@ build_check_common: $(CC) -c -o build/common.o $(CFLAGS) test/common.c ar -rvs build/test/libtestcommon.a build/common.o build/hex.o -build_check: build build_check_common +build_check: build_base build_check_common $(CC) -I./src -o build/test/check_bmt build/swarm.o build/bmt.o build/endian.o build/swarmfile.o test/check_bmt.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon $(CC) -I./src -o build/test/check_file build/swarm.o build/bmt.o build/endian.o build/swarmfile.o test/check_file.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon -build_check_keystore: build build_check_common +build_check_keystore: build_base build_keystore build_check_common $(CC) -I./src -o build/test/check_keystore build/swarm.o build/keystore.o test/check_keystore.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon -lsecp256k1 diff --git a/src/keystore.c b/src/keystore.c @@ -1,10 +1,32 @@ +#include <string.h> + #include "secp256k1.h" #include "keystore.h" -unsigned char* sign_digest(unsigned char *z, const keystore_t *keystore, const unsigned char *label, const unsigned char *digest, const size_t digest_sz) { +unsigned char* keystore_sign_digest(const keystore_t *keystore, unsigned char *z, const unsigned char *label, const unsigned char *digest, const size_t digest_sz) { keystore_key_t key; return 0; } + + +// will _copy_ key to keystore +keystore_key_t* keystore_put(keystore_t *keystore, const keystore_key_t *key_in, const char *passphrase, size_t passphrase_sz) { + keystore_key_t *key_out; + + key_out = keystore->keys + keystore->keys_count; + + memcpy(key_out, key_in, sizeof(keystore_key_t)); + + keystore->keys_count++; + + return key_out; +} + + +keystore_key_t* keystore_get(const keystore_t *keystore, keystore_key_t *z, const int idx) { + memcpy(z, keystore->keys+idx, sizeof(keystore_key_t)); + return z; +} diff --git a/src/keystore.h b/src/keystore.h @@ -1,22 +1,25 @@ #ifndef _LIBSWARM_SIGN_H #define _LIBSWARM_SIGN_H +#define SWARM_PRIVATE_KEY_SIZE 32 +#define SWARM_KEY_LABEL_SIZE 20 + typedef struct keystore_key { - unsigned char *pk; - unsigned char *label; - size_t pk_sz; - size_t label_sz; + unsigned char pk[SWARM_PRIVATE_KEY_SIZE]; + unsigned char label[SWARM_KEY_LABEL_SIZE]; } keystore_key_t; typedef struct keystore { size_t pk_sz; size_t label_sz; size_t digest_sz; - void *store; + keystore_key_t *keys; + size_t keys_count; + keystore_key_t* (*label)(keystore_key_t *key); } keystore_t; -keystore_key_t* put(keystore_key_t *keystore, const char *pk, const char *passphrase, size_t passphrase_sz); -keystore_key_t* get(keystore_key_t *z, const char *label); -unsigned char* sign_digest(unsigned char *z, const keystore_t *keystore, const unsigned char *label, const unsigned char *digest, const size_t digest_sz); +keystore_key_t* keystore_put(keystore_t *keystore, const keystore_key_t *z, const char *passphrase, size_t passphrase_sz); +keystore_key_t* keystore_get(const keystore_t *keystore, keystore_key_t *z, const int idx); +unsigned char* keystore_sign_digest(const keystore_t *keystore, unsigned char *z, const unsigned char *label, const unsigned char *digest, const size_t digest_sz); #endif // _LIBSWARM_SIGN_H diff --git a/test/check_keystore.c b/test/check_keystore.c @@ -9,8 +9,35 @@ START_TEST(check_keystore_init) { keystore_t keystore; - keystore_init(&keystore); + keystore_init(&keystore); +} +END_TEST + +START_TEST(check_keystore_getput) { + keystore_t keystore; + keystore_key_t key_one; + keystore_key_t key_two; + keystore_key_t key_returned; + struct block_generator bg; + + bg.v = 0; + bg.m = 256; + block_generate(&bg, key_one.pk, 32); + block_generate(&bg, key_two.pk, 32); + + keystore_init(&keystore); + + keystore_put(&keystore, &key_one, NULL, 0); + keystore_put(&keystore, &key_two, NULL, 0); + + ck_assert_int_eq(keystore.keys_count, 2); + + keystore_get(&keystore, &key_returned, 1); + keystore_free(&keystore); + + ck_assert_mem_eq(&key_two, &key_returned, sizeof(keystore_key_t)); + } END_TEST @@ -21,6 +48,7 @@ Suite * common_suite(void) { s = suite_create("keystore"); tc = tcase_create("core"); tcase_add_test(tc, check_keystore_init); + tcase_add_test(tc, check_keystore_getput); suite_add_tcase(s, tc); return s; diff --git a/test/common.c b/test/common.c @@ -1,6 +1,7 @@ #include <stdlib.h> #include <stddef.h> +#include "swarm.h" #include "keystore.h" #include "common.h" @@ -19,11 +20,15 @@ int block_generate(struct block_generator *bg, char *buf, size_t l) { keystore_t* keystore_init(keystore_t *keystore) { struct keystore_backend *backend; - keystore->store = malloc(sizeof(struct keystore_backend)); + keystore->keys = malloc(sizeof(keystore_key_t) * 32); + keystore->pk_sz = SWARM_PRIVATE_KEY_SIZE; + keystore->label_sz = SWARM_KEY_LABEL_SIZE; + keystore->digest_sz = SWARM_WORD_SIZE; + keystore->keys_count = 0; - return keystore; + return keystore; } void keystore_free(keystore_t *keystore) { - free(keystore->store); + free(keystore->keys); } diff --git a/test/common.h b/test/common.h @@ -1,19 +1,14 @@ #include <stddef.h> -struct keystore_backend { - unsigned char *labels; - unsigned char *pks; - size_t count; -}; - - +// mockdata struct block_generator { int v; int m; }; int block_generate(struct block_generator *bg, char *buf, size_t l); + keystore_t* keystore_init(keystore_t *keystore); void keystore_free(keystore_t *keystore);