Pack records with a bounded gzip frame in record_codec
daochi_client/record_codec packs JSON before encryption and unpacks it only after authentication, within the app's record limit, beside the released identity ciphertext. Entity's records already import it; this is the part of that work that was left uncommitted.
This commit is contained in:
1 parent
62b15c6b8c
commit
8250ece58a
5 files changed
+125
No files matched your search
@@ -71,6 +71,20 @@ renewing the bearer session clears the hint. Hosts may save this public metadata
|
||||
with `WriteRegistration` and restore it with `ReadRegistration`; it contains no
|
||||
private key or bearer token.
|
||||
|
||||
`record_codec.zi` optionally packs JSON before encryption with a bounded gzip
|
||||
frame. Keep the released identity ciphertext and send an independently sealed
|
||||
`packed_nonce`/`packed_ciphertext` pair alongside it. Readers request
|
||||
`record_encoding:"gzip-v1"` on record pages; older nodes and readers continue to
|
||||
use the identity representation. After authenticating the ciphertext, validate
|
||||
the frame's claimed length before allocating and call `UnpackRecord` with the
|
||||
app's own record limit. Binary media bypass this codec. Native hosts link zlib
|
||||
(`-lz`); Emscripten hosts use `-sUSE_ZLIB=1`.
|
||||
|
||||
A transport may resend a signed POST when a reused connection drops after the
|
||||
node committed it. Sync recognizes the exact signed-transaction replay response
|
||||
and retries with a fresh nonce within its existing bounded recovery budget.
|
||||
The node's replay protection remains enforced.
|
||||
|
||||
Import the package modules with `#import "daochi_client/client"` and
|
||||
`#import "daochi_client/sync"`. Set `Client.app_id`, supply a `Device` with a
|
||||
persisted Ed25519 key and signer, and supply a cryptographically random 64-digit
|
||||
|
||||
@@ -0,0 +1,62 @@
|
||||
// Optional packed JSON record payloads. Hosts seal both the released JSON and
|
||||
// this frame with independent nonces. Nodes serve the packed copy only to a
|
||||
// reader that requests gzip-v1; older readers retain the released plaintext.
|
||||
#import "gzip_linux"
|
||||
|
||||
RecordPayloadMaximum :: 8388608
|
||||
RecordPackingHeader :: 12
|
||||
RecordPackingMagic :: "DAOGZIP1"
|
||||
|
||||
RecordIsPacked :: (input: []u8) -> bool {
|
||||
if input.count < RecordPackingHeader { return false }
|
||||
for byte, index: RecordPackingMagic {
|
||||
if input[index] != byte { return false }
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Zero means keep the identity representation; -1 means invalid bounds.
|
||||
// The caller owns disjoint input/output buffers and caps output to input size.
|
||||
PackRecord :: (input: []u8, output: []u8) -> s64 {
|
||||
if input.count < 1 || input.count > RecordPayloadMaximum || output.count < RecordPackingHeader {
|
||||
return -1
|
||||
}
|
||||
if input.count < 512 { return 0 }
|
||||
// Only structured app records use this frame; binary blobs keep their
|
||||
// existing content-addressed encryption and never enter this codec.
|
||||
if input[0] != 123 && input[0] != 91 { return 0 }
|
||||
count := GzipCompress(input, output[RecordPackingHeader:], 1)
|
||||
if count < 0 || count + RecordPackingHeader + 64 >= input.count {
|
||||
return 0
|
||||
}
|
||||
for byte, index: RecordPackingMagic { output[index] = byte }
|
||||
size := cast(u32)input.count
|
||||
for index: cast(s32)0..3 {
|
||||
output[8 + index] = cast(u8)((size >> cast(u32)(index * 8)) & 255)
|
||||
}
|
||||
return count + RecordPackingHeader
|
||||
}
|
||||
|
||||
// Validate the authenticated frame's claimed length before any allocation.
|
||||
PackedRecordSize :: (input: []u8, maximum: s64 = RecordPayloadMaximum) -> s64 {
|
||||
if !RecordIsPacked(input) || maximum < 1 || maximum > RecordPayloadMaximum {
|
||||
return -1
|
||||
}
|
||||
size: u32
|
||||
for index: cast(s32)0..3 {
|
||||
size |= cast(u32)input[8 + index] << cast(u32)(index * 8)
|
||||
}
|
||||
if size < 1 || cast(s64)size > maximum || input.count >= cast(s64)size {
|
||||
return -1
|
||||
}
|
||||
return cast(s64)size
|
||||
}
|
||||
|
||||
UnpackRecord :: (input: []u8, output: []u8, maximum: s64 = RecordPayloadMaximum) -> s64 {
|
||||
size := PackedRecordSize(input, maximum)
|
||||
if size < 0 || size > output.count || !GzipDecompress(input[RecordPackingHeader:], output, size) {
|
||||
return -1
|
||||
}
|
||||
if output[0] != 123 && output[0] != 91 { return -1 }
|
||||
return size
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "record_codec.h"
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char input[65536], packed[65536], opened[65536];
|
||||
memset(input, 'x', sizeof input);
|
||||
input[0]='{'; input[sizeof input-1]='}';
|
||||
Slice source={.data=input,.length=sizeof input};
|
||||
Slice encoded={.data=packed,.length=sizeof packed};
|
||||
Slice decoded={.data=opened,.length=sizeof opened};
|
||||
int64_t count=record_codec_PackRecord(source,encoded);
|
||||
assert(count>12 && count<sizeof input/10);
|
||||
encoded.length=count;
|
||||
assert(record_codec_RecordIsPacked(encoded));
|
||||
assert(record_codec_PackedRecordSize(encoded,sizeof input)==sizeof input);
|
||||
assert(record_codec_UnpackRecord(encoded,decoded,sizeof input)==sizeof input);
|
||||
assert(memcmp(input,opened,sizeof input)==0);
|
||||
assert(record_codec_UnpackRecord(encoded,decoded,sizeof input-1)==-1);
|
||||
decoded.length=sizeof opened-1;
|
||||
assert(record_codec_UnpackRecord(encoded,decoded,sizeof input)==-1);
|
||||
decoded.length=sizeof opened;
|
||||
((unsigned char*)packed)[8]=255; ((unsigned char*)packed)[9]=255;
|
||||
((unsigned char*)packed)[10]=255; ((unsigned char*)packed)[11]=255;
|
||||
assert(record_codec_PackedRecordSize(encoded,8388608)==-1);
|
||||
source.length=sizeof input;
|
||||
count=record_codec_PackRecord(source,(Slice){.data=packed,.length=sizeof packed});
|
||||
encoded.length=count;
|
||||
packed[count-1]^=1;
|
||||
assert(record_codec_UnpackRecord(encoded,decoded,sizeof input)==-1);
|
||||
source.length=2;
|
||||
assert(record_codec_PackRecord(source,(Slice){.data=packed,.length=sizeof packed})==0);
|
||||
memset(input,'x',sizeof input);
|
||||
assert(record_codec_PackRecord(source,(Slice){.data=packed,.length=sizeof packed})==0);
|
||||
puts("Packed records: bounded gzip, independent buffers, framing and corruption rejection passed");
|
||||
return 0;
|
||||
}
|
||||
@@ -54,6 +54,13 @@ done
|
||||
"$work/release-test"
|
||||
echo 'Daochi signed release parsing passed'
|
||||
|
||||
"$compiler" --no-main --root "$root" --module-path "$ziran/std" \
|
||||
-o "$work/record-codec" "$root/record_codec.zi"
|
||||
"${CC:-cc}" -std=c11 -O1 -ffunction-sections -fdata-sections -Wl,--gc-sections \
|
||||
-I"$ziran/include" -I"$work/record-codec" "$root/tests/record_codec_test.c" \
|
||||
"$work/record-codec"/*.c -lz -o "$work/record-codec-test"
|
||||
"$work/record-codec-test"
|
||||
|
||||
# The portable SHA-256, HMAC, PBKDF2 and ChaCha20-Poly1305 against known answers.
|
||||
"$compiler" --no-main --root "$root" --module-path "$ziran/std" --module-path "crypto=$crypto/src" -o "$work/crypto" "$root/crypto.zi"
|
||||
"${CC:-cc}" -std=c11 -O2 -Wall -Wextra -Werror -I"$ziran/include" -I"$work/crypto" \
|
||||
|
||||
@@ -24,6 +24,7 @@ crypto_native = "crypto_native.zi"
|
||||
events = "events.zi"
|
||||
keys = "keys.zi"
|
||||
record = "record.zi"
|
||||
record_codec = "record_codec.zi"
|
||||
entity = "entity.zi"
|
||||
social = "social.zi"
|
||||
rewards = "rewards.zi"
|
||||
|
||||
Reference in new issue
Block a user