chiark / gitweb /
More superblock work!
authorDan Sheppard <dan.sheppard.circle@gmail.com>
Fri, 11 Apr 2025 15:17:46 +0000 (16:17 +0100)
committerDan Sheppard <dan.sheppard.circle@gmail.com>
Fri, 11 Apr 2025 15:19:45 +0000 (16:19 +0100)
.gitignore
constants.h
coquet
superblock.c
superblock.h

index 4c50c9767caf75620867e08f580f7824bfe52d8d..93b9aff2e50b29f21d9cf70e3152513cd91155f7 100644 (file)
@@ -1,3 +1,3 @@
 *.o
-*.coquet
+*.coquet.*
 a.out
index 4a7fc0594239c4d9c55cb8db98303e8dd35e3db6..9b68597e9aff5a6a7c31833959d2d1852a07d3e4 100644 (file)
@@ -3,6 +3,9 @@
 
 #include <stdint.h>
 
+#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 2a2d542b019c61c2c8838dc232f8088f1d23d7e4..e1cd0bb4be760199edf70c9d93b183e16cd65089 100755 (executable)
Binary files a/coquet and b/coquet differ
index 6409fbf05fef6e2e616b2c052d9723c90b516adc..1c9a146aefb196d357f13f7af6b35da03a778f39 100644 (file)
  *
  *                       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
  * +-------------------+
  *                       offset    length
  * +-------------------+
  * | global_iv         |  0           32
- * +-------------------+
  * | block_size        |  32           1
+ * | nursery_size      |  33           1
  * +-------------------+
- * | unused            |  33         478
+ * | unused            |  34         477
  * +-------------------+
  * | reserved          | 511           1
  * +-------------------+
  */
 
 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,
index 1cffe05752f07d6c359a7f3d44777166a8f16409..e95008f2c2b8606476ee8b1395a7edc6f23e8942 100644 (file)
@@ -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 */