added keys.h, sleep_crypto.h; worked sleep.h, sleep_tree.c, sleep_crypto.c
This commit is contained in:
1 parent
e51f5c7bc8
commit
23c037e02e
12 files changed
+126
-1
No files matched your search
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
+17
@@ -0,0 +1,17 @@
|
||||
#ifndef KEYS_H
|
||||
#define KEYS_H
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#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 */
|
||||
@@ -1,3 +1,4 @@
|
||||
// ॐ सूक्ष्म शरीर
|
||||
#ifndef SLEEP_H
|
||||
#define SLEEP_H
|
||||
// TODO implement SLEEP
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#ifndef SLEEP_CRYPTO_H
|
||||
#define SLEEP_CRYPTO_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
#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 */
|
||||
@@ -0,0 +1,26 @@
|
||||
#include "sleep_crypto.h"
|
||||
#include <sodium.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
+60
-1
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user