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 d0c0f9711fbbfecb2ae67bd5f319609e71a471e1
parent 6a68d5546652a99241ee3f0473eb4f9f677a2550
Author: nolash <dev@holbrook.no>
Date:   Thu, 16 Sep 2021 09:54:31 +0200

Add keystore/key types, with test file

Diffstat:
MMakefile.dev | 18++++++++++++++++--
Asrc/keystore.c | 10++++++++++
Asrc/keystore.h | 22++++++++++++++++++++++
Msrc/soc.c | 28++++++++++++++++++++++++++++
Msrc/soc.h | 4+++-
Msrc/swarm.c | 15+++++++++++++++
Msrc/swarm.h | 2++
Atest/check_keystore.c | 43+++++++++++++++++++++++++++++++++++++++++++
Mtest/check_soc.c | 16++++++++++++++++
Mtest/common.c | 14++++++++++++++
Mtest/common.h | 10++++++++++
11 files changed, 179 insertions(+), 3 deletions(-)

diff --git a/Makefile.dev b/Makefile.dev @@ -29,7 +29,13 @@ build_base: prep build_keccak $(CC) -c -o build/swarmfile.o $(CFLAGS) src/swarmfile.c $(CC) -c -o build/swarm.o $(CFLAGS) src/swarm.c -build_soc: prep build_keccak +build_secp256k1: prep + +build_keystore: prep build_keccak build_secp256k1 + $(CC) -c -o build/keystore.o $(CFLAGS) src/keystore.c + + +build_soc: prep build_keccak build_keystore $(CC) -c -o build/soc.o src/soc.c $(CFLAGS) -lkeccak-tiny build: build_base @@ -43,7 +49,12 @@ build_check: build 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_soc: build build_check_common + +build_check_keystore: build 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 + + +build_check_soc: build build_check_common build_check_keystore $(CC) -I./src -o build/test/check_soc build/swarm.o build/soc.o test/check_soc.c $(CFLAGS_CHECK) -lcheck -lkeccak-tiny -ltestcommon @@ -57,6 +68,9 @@ check_base: build build_check LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_bmt LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_file +check_keystore: build build_check_keystore + LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_keystore + check_soc: build build_check_soc LD_LIBRARY_PATH=./build/:./build/test CK_FORK=no build/test/check_soc diff --git a/src/keystore.c b/src/keystore.c @@ -0,0 +1,10 @@ +#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) { + keystore_key_t key; + + return 0; +} diff --git a/src/keystore.h b/src/keystore.h @@ -0,0 +1,22 @@ +#ifndef _LIBSWARM_SIGN_H +#define _LIBSWARM_SIGN_H + +typedef struct keystore_key { + unsigned char *pk; + unsigned char *label; + size_t pk_sz; + size_t label_sz; +} keystore_key_t; + +typedef struct keystore { + size_t pk_sz; + size_t label_sz; + size_t digest_sz; + void *store; +} 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); + +#endif // _LIBSWARM_SIGN_H diff --git a/src/soc.c b/src/soc.c @@ -28,3 +28,31 @@ int soc_identifier(char *z, const char *topic, const char *index) { return 0; } + +// z must be minimum 84 bytes long (32 bytes for out, 20 bytes for topic, 32 bytes for index) +int soc_address(char *z, const char *identifier, const char *address) { + int r; + char *p; + char *src; + size_t src_sz; + + src_sz = SWARM_SOC_IDENTIFIER_SIZE + SWARM_ADDRESS_SIZE; + + p = z + SWARM_WORD_SIZE; + src = p; + memcpy(p, topic, SWARM_SOC_IDENTIFIER_SIZE); + + p += SWARM_SOC_IDENTIFIER_SIZE; + memcpy(p, index, SWARM_ADDRESS_SIZE); + + r = keccak_hash_btc(z, SWARM_WORD_SIZE, src, src_sz); + if (r < 0) { + return 1; + } + + return 0; +} + +int soc_sign() { + +} diff --git a/src/soc.h b/src/soc.h @@ -2,8 +2,10 @@ #define _LIBSWARM_SOC_H #define SWARM_SOC_TOPIC_SIZE 20 -#define SWARM_SOC_INDEX_SIZE 32 +#define SWARM_SOC_INDEX_SIZE SWARM_WORD_SIZE +#define SWARM_SOC_IDENTIFIER_SIZE SWARM_WORD_SIZE int soc_identifier(char *z, const char *topic, const char *index); +int soc_address(char *z, const char *identifier, const char *address); #endif // _LIBSWARM_SOC_H diff --git a/src/swarm.c b/src/swarm.c @@ -3,7 +3,22 @@ #include "keccak-tiny.h" #include "swarm.h" +#include "keystore.h" int keccak_hash_btc(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz) { return keccak_hash((uint8_t*)out, out_sz, (uint8_t*)in, in_sz, SWARM_KECCAK_RATE, SWARM_KECCAK_PADDING); } + +//int keccak_hash_btc_v(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz, const unsigned char *vector, size_t *vector_sz, int vector_count) { +// int i; +// int sz; +// int *p; +// +// src = vector; +// p = out + SWARM_WORD_SIZE; +// for (i = 0; i < vector_count; i++) { +// sz = *(vector_sz +i ); +// memcpy +// } +//} +// diff --git a/src/swarm.h b/src/swarm.h @@ -4,10 +4,12 @@ #define SWARM_WORD_SIZE 32 #define SWARM_DATA_LENGTH_TYPESIZE 8 #define SWARM_BLOCK_SIZE 4096 +#define SWARM_ADDRESS_SIZE 20 #define SWARM_KECCAK_RATE 200-64 #define SWARM_KECCAK_PADDING 0x01 int keccak_hash_btc(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz); +int keccak_hash_btc_v(unsigned char *out, size_t out_sz, const unsigned char *in, size_t in_sz, const unsigned char *vector, size_t vector_sz); #endif // _LIBSWARM_H diff --git a/test/check_keystore.c b/test/check_keystore.c @@ -0,0 +1,43 @@ +#include <check.h> +#include <stdlib.h> + +#include "keystore.h" +#include "hex.h" +#include "common.h" + + +START_TEST(check_keystore_init) { + keystore_t keystore; + + keystore_init(&keystore); + keystore_free(&keystore); +} +END_TEST + +Suite * common_suite(void) { + Suite *s; + TCase *tc; + + s = suite_create("keystore"); + tc = tcase_create("core"); + tcase_add_test(tc, check_keystore_init); + suite_add_tcase(s, tc); + + return s; +} + +int main(void) { + int n_fail; + + Suite *s; + SRunner *sr; + + s = common_suite(); + sr = srunner_create(s); + + srunner_run_all(sr, CK_VERBOSE); + n_fail = srunner_ntests_failed(sr); + srunner_free(sr); + + return (n_fail == 0) ? EXIT_SUCCESS : EXIT_FAILURE; +} diff --git a/test/check_soc.c b/test/check_soc.c @@ -21,6 +21,21 @@ START_TEST(check_soc_identifier) { } END_TEST +START_TEST(check_soc_address) { + int i; + int r; + char out[84]; + char in[52]; + + for (i = 51; i >= 0; i--) { + in[i] = i; + } + + r = soc_identifier(out, in, in+32); + ck_assert_int_eq(r, 0); +} +END_TEST + Suite * common_suite(void) { Suite *s; TCase *tc; @@ -28,6 +43,7 @@ Suite * common_suite(void) { s = suite_create("soc"); tc = tcase_create("core"); tcase_add_test(tc, check_soc_identifier); + tcase_add_test(tc, check_soc_address); suite_add_tcase(s, tc); return s; diff --git a/test/common.c b/test/common.c @@ -1,5 +1,7 @@ +#include <stdlib.h> #include <stddef.h> +#include "keystore.h" #include "common.h" int block_generate(struct block_generator *bg, char *buf, size_t l) { @@ -13,3 +15,15 @@ int block_generate(struct block_generator *bg, char *buf, size_t l) { return i; } + +keystore_t* keystore_init(keystore_t *keystore) { + struct keystore_backend *backend; + + keystore->store = malloc(sizeof(struct keystore_backend)); + + return keystore; +} + +void keystore_free(keystore_t *keystore) { + free(keystore->store); +} diff --git a/test/common.h b/test/common.h @@ -1,9 +1,19 @@ #include <stddef.h> + +struct keystore_backend { + unsigned char *labels; + unsigned char *pks; + size_t count; +}; + + 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);