From: Dan Sheppard Date: Fri, 11 Apr 2025 15:17:46 +0000 (+0100) Subject: More superblock work! X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~dans/git?a=commitdiff_plain;h=bb187c2fb75f3878f9ee539a61d9994f20be14d3;p=coquet.git More superblock work! --- diff --git a/.gitignore b/.gitignore index 4c50c97..93b9aff 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ *.o -*.coquet +*.coquet.* a.out diff --git a/constants.h b/constants.h index 4a7fc05..9b68597 100644 --- a/constants.h +++ b/constants.h @@ -3,6 +3,9 @@ #include +#define COQUET_MAGIC 0xC0CEF11ECABBA9E5ULL +#define COQUET_VERSION 0x0000000000000001ULL + #define COQUET_FILE_MAIN 0 #define COQUET_FILE_OLD 1 #define COQUET_FILE_TMP 2 diff --git a/coquet b/coquet index 2a2d542..e1cd0bb 100755 Binary files a/coquet and b/coquet differ diff --git a/superblock.c b/superblock.c index 6409fbf..1c9a146 100644 --- a/superblock.c +++ b/superblock.c @@ -11,10 +11,12 @@ * * offset length * +-------------------+ - * | sb_hash | 0 32 - * | sb_serial | 32 8 + * | magic | 0 8 + * | version | 8 8 + * | sb_hash | 16 32 + * | sb_serial | 48 8 * +-------------------+ - * | unused | 40 984 + * | unused | 56 968 * +-------------------+ * | current | 1024 512 * +-------------------+ @@ -26,10 +28,10 @@ * offset length * +-------------------+ * | global_iv | 0 32 - * +-------------------+ * | block_size | 32 1 + * | nursery_size | 33 1 * +-------------------+ - * | unused | 33 478 + * | unused | 34 477 * +-------------------+ * | reserved | 511 1 * +-------------------+ @@ -40,10 +42,11 @@ */ struct cq_super default_super = { - .current = {{0,},12}, - .desired = {{0,},12}, + .current = {{0,},12,10}, + .desired = {{0,},12,10}, .sb_serial = 0, .sb_hash = {0,}, + .version = COQUET_VERSION, .from_b = 1 }; @@ -53,21 +56,30 @@ struct cq_super default_super = { static void extract_config(uint8_t *data, struct cq_super_config *sc) { memcpy(sc->global_iv,data,HASH_LEN); sc->block_size = be_decode(data+32,1); + sc->nursery_size = be_decode(data+33,1); } static int extract_half(uint8_t *data, struct cq_super *super) { struct sha2_ctx_t sha2; uint8_t hash[HASH_LEN],cmp[HASH_LEN]; + uint64_t magic; /* extract */ + magic = be_decode(data,8); + if(magic!=COQUET_MAGIC) { + return 0; + } + super->version = be_decode(data+8,8); + /* All versions are fine for now. */ memset(super->sb_hash,0,HASH_LEN); - super->sb_serial = be_decode(data+32,8); + super->sb_serial = be_decode(data+48,8); + extract_config(data+1024,&(super->current)); extract_config(data+1536,&(super->desired)); /* verify */ - memcpy(hash,data,HASH_LEN); - memset(data,0,HASH_LEN); + memcpy(hash,data+16,HASH_LEN); + memset(data+16,0,HASH_LEN); sha2_init_hmac(&sha2, SHA2_VARIETY_512_256, super->current.global_iv, GLOBAL_IV_LEN); sha2_more(&sha2,data,HALF_BYTES); @@ -146,14 +158,18 @@ static void intract_config(uint8_t *data, struct cq_super_config *sc) { memcpy(data,sc->global_iv,HASH_LEN); be_encode(data+32,sc->block_size,1); + be_encode(data+33,sc->nursery_size,1); } static void intract(uint8_t *data, struct cq_super *super, uint64_t serial) { struct sha2_ctx_t sha2; /* intract */ - memset(data,0,HALF_BYTES); - be_encode(data+32,serial,8); + memset(data,0,HASH_LEN); + be_encode(data,COQUET_MAGIC,8); + be_encode(data+8,COQUET_VERSION,8); + be_encode(data+48,serial,8); + intract_config(data+1024,&(super->current)); intract_config(data+1536,&(super->desired)); @@ -161,7 +177,7 @@ static void intract(uint8_t *data, struct cq_super *super, uint64_t serial) { sha2_init_hmac(&sha2, SHA2_VARIETY_512_256, super->current.global_iv, GLOBAL_IV_LEN); sha2_more(&sha2,data,HALF_BYTES); - sha2_finish(&sha2,data,HASH_LEN); + sha2_finish(&sha2,data+16,HASH_LEN); } static int super_write(coquet_t *cq, struct cq_super *super, diff --git a/superblock.h b/superblock.h index 1cffe05..e95008f 100644 --- a/superblock.h +++ b/superblock.h @@ -9,12 +9,14 @@ #define SUPERBLOCK_LAST_OFFSET (SUPER_BYTES-1) struct cq_super_config { - uint8_t global_iv[GLOBAL_IV_LEN]; /* (0 : GLOBAL_IV_LEN) */ - uint8_t block_size; /* log bits (GLOBAL_IV_LEN: 1) */ + uint8_t global_iv[GLOBAL_IV_LEN]; + uint8_t block_size; /* log bits */ + uint8_t nursery_size; /* log blocks */ }; struct cq_super { struct cq_super_config current, desired; + uint64_t version; uint64_t sb_serial; uint8_t sb_hash[32]; /* HMAC SHA512-256 using global_iv */ int from_b; /* true if loaded from b, false if from a */