began sleep.c, worked sleep_crypto.c, sleep_tree.c

renamed sleep.3 for conflicting
added mock visual for client extra/datthang.png
direction: need to save keys, save seckey ~/.datt/secret_keys
.datt datt with extra t decided for testing until port complete
This commit is contained in:
2025-10-06 18:41:50 -04:00
parent 23c037e02e
commit a1da7deba4
12 changed files with 151 additions and 4 deletions

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.

BIN
extra/datthang.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

View File

@@ -0,0 +1,46 @@
#include "sleep.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
static int write_header(const char *path, uint32_t magicka, uint16_t entry_size, const char *algo) {
FILE *f = fopen(path, "wb");
if (!f) return -1;
sleep_header_t h;
memset(&h, 0, sizeof(h));
h.magicka = ((magicka >> 24) & 0xff) | ((magicka >> 8) & 0xff00) | ((magicka << 8) & 0xff0000) | ((magicka << 24) & 0xff000000);
h.version = 0;
h.entry_size = (entry_size >> 8) | (entry_size << 8); // BE16
h.algo_len = algo ? (uint8_t)strlen(algo) : 0;
if (algo) strncopy(h.algo, algo, sizeof(h.algo));
if (fwrite(&h, 1, sizeof(h), f) != sizeof(h)) {
fclose(f);
return -1;
}
fclose(f);
return 0;
}
int sleep_init(const char *root) {
char path[512];
// makedir .dat(t) testing
sprintf(path, sizeof(path), "%s/.datt", root);
if (mkdir(path, 0700) && errno != EEXIST) return -1;
// content.* signatures, tree, bitfield, key
// metadata.* signatures, tree, bitfield, key
return 0;
}
int main(void) {
if (sleep_init(".")) {
perror("sleep_init failed");
return 1;
}
return 0;
}

View File

@@ -24,3 +24,34 @@ int sleep_load_pubkey(const char *path, uint8_t pubkey[SLEEP_PUBLIC_KEY_BYTES])
return (read == SLEEP_PUBLIC_KEY_BYTES) ? 0 : -1; return (read == SLEEP_PUBLIC_KEY_BYTES) ? 0 : -1;
} }
int sleep_blake2b(uint8_t out[SLEEP_BLAKE2B_BYTES], const uint8_t *in, size_t inlen) {
return crypto_generichash(out, SLEEP_BLAKE2B_BYTES, in, inlen, NULL, 0);
}
int sleep_sign(uint8_t sig[SLEEP_SIGNATURE_BYTES], const uint8_t *msg, size_t msglen, const sleep_keypair_t *kp) {
return crypto_sign_detached(sig, NULL, msg, msglen, kp->seckey);
}
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]) {
return crypto_sign_verify_detached(sig, msg, msglen, pubkey);
}
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) {
uint8_t bug[1024]; // FIXME dynamic
size_t ctxlen = strlen(SLEEP_CONTEXT);
memcpy(bug, SLEEP_CONTEXT, ctxlen);
size_t off = ctxlen; // off defined in scope?
off += encode_roots(bug + off, roots, nroots, indices, lengths);
return sleep_sign(sig, bug, off, 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]) {
uint8_t bug[1024]; // FIXME dynamic
size_t ctxlen = strlen(SLEEP_CONTEXT);
memcpy(bug, SLEEP_CONTEXT, ctxlen);
size_t off = ctxlen; // off defined in scope?
off += encode_roots(bug + off, roots, nroots, indices, lengths
);
return sleep_verify(sig, bug, off, kp);
}

View File

@@ -24,7 +24,7 @@ static inline unsigned int _ctzll_fallback(uint64_t x) {
} }
return 1; return 1;
} }
static inline unsigned int flattree_ctzll(uint64_t x) { static inline unsigned int tree_ctzll(uint64_t x) {
#if defined(__GNUC__) || defined(__clang__) || define(__MUSL__) #if defined(__GNUC__) || defined(__clang__) || define(__MUSL__)
if (x == 0) return 64; if (x == 0) return 64;
return (unsigned int)__builtin_ctzll(x); return (unsigned int)__builtin_ctzll(x);
@@ -33,6 +33,78 @@ static inline unsigned int flattree_ctzll(uint64_t x) {
#endif #endif
} }
static inline uint64_t sleep_tree_index(uint64_t depth, uint64_t offset) {
if (depth >= 63) {
return UINT64_MAX;
}
return (offset << (depth + 1)) | ((depth == 0) ? 0ULL : ((1ULL << depth) - 1ULL));
}
static inline uint64_t sleep_tree_depth(uint64_t i) {
uint64_t inverted = ~i;
return (uint64_t)tree_ctzll(inverted);
}
static inline int tree_is_even(uint64_t i) {
return (i & 1ULL) == 0ULL; }
static inline int tree_is_odd(uint64_t i) {
return (i & 1ULL) != 0ULL; }
static inline uint64_t sleep_tree_offset(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
if (tree_is_even(i)) {
return i / 2;
} else {
return i >> (d + 1);
}
}
static inline uint64_t sleep_tree_parent(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
return sleep_tree_index(d, sleep_tree_offset(i) ^ 1ULL);
}
static inline int sleep_tree_left_child(uint64_t i, uint64_t *out) { // set
if (tree_is_even(i)) return 0;
uint64_t d = sleep_tree_depth(i);
if (d == 0) {
*out = i;
return 1;
}
*out = sleep_tree_index(d - 1, sleep_tree_offset(i) << 1);
return 1;
}
static inline int sleep_tree_right_child(uint64_t i, uint64_t *out) { // horus
if (tree_is_even(i)) return 0;
uint64_t d = sleep_tree_depth(i);
if (d == 0) {
*out = i;
return 1;
}
*out = sleep_tree_index(d - 1, (slep_tree_offset(i) << 1) + 1ULL);
return 1;
}
static inline uint64_t sleep_tree_left_span(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
if (d == 0) return i;
return sleep_tree_offset(i) * (2ULL << d);
}
static inline uint64_t sleep_tree_right_span(uint64_t i) {
uint64_t d = sleep_tree_depth(i);
if (d == 0) return i;
return (sleep_tree_offset(i) + 1ULL) * (2ULL << d) - 2ULL;
}
static inline void sleep_tree_spans(uint64_t i, uint64_t *left, uint64_t *right) {
*left = sleep_tree_left_span(i);
*right = sleep_tree_right_span(i);
}
// TODO static inline int sleep_tree_full_roots
// भव न सन हृदयं // भव न सन हृदयं
int sleep_tree_hash_block(const uint8_t *data, size_t len, uint8_t out_hash[32]) { int sleep_tree_hash_block(const uint8_t *data, size_t len, uint8_t out_hash[32]) {
#ifndef SLEEP_SALT #ifndef SLEEP_SALT
@@ -86,7 +158,7 @@ int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks,
free(roots); free(roots);
return -1; return -1;
} }
leaf.index = flattree_index(0, (uint64_t)i); // 2 * i ... FIXME flattree naming conventions, defined in scope? leaf.index = sleep_tree_index(0, (uint64_t)i); // 2 * i ...
leaf.size = (uint64_t)block_lens[i]; leaf.size = (uint64_t)block_lens[i];
if (nodes_len >= max_nodes) { if (nodes_len >= max_nodes) {
size_t newcap = max_nodes * 2; size_t newcap = max_nodes * 2;
@@ -120,5 +192,3 @@ int sleep_tree_build(const uint8_t **blocks, size_t *block_lens, size_t nblocks,
free(roots); free(roots);
return 0; return 0;
} }