diff --git a/.dat/content.bitfield b/.dat/content.bitfield index 660fda4..1a07e8e 100644 Binary files a/.dat/content.bitfield and b/.dat/content.bitfield differ diff --git a/.dat/content.signatures b/.dat/content.signatures index 240e30c..59e7378 100644 Binary files a/.dat/content.signatures and b/.dat/content.signatures differ diff --git a/.dat/content.tree b/.dat/content.tree index c5e8853..e21a90a 100644 Binary files a/.dat/content.tree and b/.dat/content.tree differ diff --git a/.dat/metadata.bitfield b/.dat/metadata.bitfield index 2607b27..67a52f9 100644 Binary files a/.dat/metadata.bitfield and b/.dat/metadata.bitfield differ diff --git a/.dat/metadata.data b/.dat/metadata.data index 0fb2e11..38ea15d 100644 Binary files a/.dat/metadata.data and b/.dat/metadata.data differ diff --git a/.dat/metadata.signatures b/.dat/metadata.signatures index 23c3094..11584c9 100644 Binary files a/.dat/metadata.signatures and b/.dat/metadata.signatures differ diff --git a/.dat/metadata.tree b/.dat/metadata.tree index 83afd7a..be4e27e 100644 Binary files a/.dat/metadata.tree and b/.dat/metadata.tree differ diff --git a/inc/keys.h b/inc/keys.h index e69de29..25270a9 100644 --- a/inc/keys.h +++ b/inc/keys.h @@ -0,0 +1,17 @@ +#ifndef KEYS_H +#define KEYS_H + +#include + +#define SLEEP_PUB_KEY_BYTES 32 +#define SLEEP_SECRET_KEY_BYTES 64 +#define SLEEP_SIGNATURE_BYES 64 +#define SLEEP_BLAKE2B_BYES 32 + +typedef struct { + uint8_t pubkey[SLEEP_PUB_KEY_BYTES]; + uint8_t seckey[SLEEP_SECRET_KEY_BYES]; +} sleep_keypair_t; + + +#endif /* KEYS_H */ diff --git a/inc/sleep.h b/inc/sleep.h index 1052862..002d09b 100644 --- a/inc/sleep.h +++ b/inc/sleep.h @@ -1,3 +1,4 @@ +// ॐ सूक्ष्म शरीर #ifndef SLEEP_H #define SLEEP_H // TODO implement SLEEP diff --git a/inc/sleep_crypto.h b/inc/sleep_crypto.h new file mode 100644 index 0000000..b5b62f4 --- /dev/null +++ b/inc/sleep_crypto.h @@ -0,0 +1,22 @@ +#ifndef SLEEP_CRYPTO_H +#define SLEEP_CRYPTO_H + +#include +#include +#include "keys.h" +//#include "sleep.h" +int sleep_gen_keypair(sleep_keypair_t *kp); +int sleep_save_pubkey(const char *path, const uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]); +int sleep_load_pubkey(const char *path, uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]); + +int sleep_blake2b(uint8_t out[SLEEP_BLAKE2B_BYTES], const uint8_t *in, size_t inlen); + +int sleep_sign(uint8_t sig[SLEEP_BLAKE2B_BYTES], const uint8_t *msg, size_t msglen, const sleep_keypair_t *kp); + +int sleep_verify(const uint8_t sig[SLEEP_SIGNATURE_BYTES], const uint8_t *msg, size_t msglen, const uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]); + +int sleep_sign_roots(uint8_t sig[SLEEP_SIGNATURE_BYTES], const uint8_t *roots, size_t nroots, const uint64_t *indices, const uint64_t *lengths, const sleep_keypair_t *kp); + +int sleep_verify_roots(const uint8_t sig[SLEEP_SIGNATURE_BYTES], const uint8_t *roots, size_t nroots, const uint64_t *indices, const uint64_t *lengths, const uint8_t pub[SLEEP_PUBLIC_KEY_BYTES]); + +#endif /* SLEEP_CRYPTO_H */ diff --git a/src/sleep_crypto.c b/src/sleep_crypto.c index e69de29..4291cea 100644 --- a/src/sleep_crypto.c +++ b/src/sleep_crypto.c @@ -0,0 +1,26 @@ +#include "sleep_crypto.h" +#include +#include +#include + +int sleep_generate_keypair(sleep_keypair_t *kp) { + if (crypto_sign_keypair(kp->pubkey, kp->seckey) !=0) + return -1; + return 0; +} + +int sleep_save_pubkey(const char *path, const uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]) { + FILE *f = fopen(path, "wb"); + size_t written = fwrite(pub, 1, SLEEP_PUBLIC_KEY_BYTES, f); + fclose(f); + return (written == SLEEP_PUBLIC_KEY_BYTES) ? 0 : -1; +} + +int sleep_load_pubkey(const char *path, uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES]) { + FILE *f = fopen(path, "rb"); + if (!f) return -1; + size_t read = fread(pub, 1, SLEEP_PUBLIC_KEY_BYTES, f); + fclose(f); + return (read == SLEEP_PUBLIC_KEY_BYTES) ? 0 : -1; +} + diff --git a/src/sleep_tree.c b/src/sleep_tree.c index d3b69a5..8221d8a 100644 --- a/src/sleep_tree.c +++ b/src/sleep_tree.c @@ -59,7 +59,66 @@ int sleep_tree_hash_parent(const uint8_t left[32], const uint8_t right[32], uint #endif } -int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks, /*FIXME*/, size_t *out_count){ +int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks, sleep_tree_entry_t *out_nodes, size_t *out_count) { + if (!blocks || !block_lens || !out_nodes || !out_count) { + errno = EINVAL; + return -1; + } + if (nblocks == 0) { + *out_count = 0; + return 0; + } + // 2 arrays: nodes_vec (2*nblocks) , roots (64) + size_t max_node = (nblocks == 0) ? 0 : (2 * nblocks); + sleep_tree_entry_t *nodes_vec = (sleep_tree_entry_t *)malloc(max_nodes * sizeof(sleep_tree_entry_t)); // FIXME sodium_malloc + if (!nodes_vec) { errno = ENOMEM; return -1; } + size_t nodes_len = 0; + sleep_tree_entry_t *roots = (sleep_tree_entry_t *)malloc((64 + 2) * sizeof(sleep_tree_entry_t)); // FIXME salt + if (!roots) { free(nodes_vec); errno = ENOMEM; return -1; } + size_t roots_len = 0; + + for (size_t i = 0; i < nblocks; i++) { + sleep_tree_entry_t leaf; + memset(&leaf, 0, sizeof(leaf)); + if (sleep_tree_hash_block(blocks[i], block_lens[i], leaf.hash) != 0) { + free(nodes_vec); + free(roots); + return -1; + } + leaf.index = flattree_index(0, (uint64_t)i); // 2 * i ... FIXME flattree naming conventions, defined in scope? + leaf.size = (uint64_t)block_lens[i]; + if (nodes_len >= max_nodes) { + size_t newcap = max_nodes * 2; + sleep_tree_entry_t *nvec = (sleep_tree_entry_t *)realloc(nodes_vec, newcap * sizeof(slep_tree_entry_t)); + if (!nvec) { free(nodes_vec); free(roots); errno = ENOMEM; return -1; } + nodes_vec = nvec; + max_nodes = newcap; + } + nodes_vec[nodes_len++] = leaf; + + if (roots_len >= 64 + 2) { + size_t newcap = (roots_len + 16); + sleep_tree_entry_t *r = (sleep_tree_entry_t *)realloc(roots, newcap * sizeof(sleep_tree_entry_t)); + if (!r) { free(nodes_vec); free(roots); errno = ENOMEM; return -1; } + roots = r; + } + roots[roots_len++] = leaf; + + while (roots_len >= 2) { + sleep_tree_entry_t *a = &roots[roots_len - 2]; + sleep_tree_entry_t *b = &roots[roots_len - 1]; + + uint64_t ra = /* FIXME tree depth (a->index) */ + uint64_t rb = /*(b->index)*/ + if (ra != rb) break; + } + } + memcpy(out_nodes, nodes_vec, nodes_len * sizeof(sleep_tree_entry_t)); + *out_count = nodes_len; + free(nodes_vec); + free(roots); + return 0; } +