|
| 1 | +-- SPDX-License-Identifier: PMPL-1.0-or-later |
| 2 | +-- Copyright (c) 2026 Jonathan D.A. Jewell (hyperpolymath) |
| 3 | +-- |
| 4 | +-- JanusKey ABI Layout — Memory layout proofs and C-compatible structures |
| 5 | +-- Proves that Rust and Zig FFI representations are bit-compatible |
| 6 | + |
| 7 | +module JanusKey.ABI.Layout |
| 8 | + |
| 9 | +import JanusKey.ABI.Types |
| 10 | +import Data.Vect |
| 11 | +import Data.Fin |
| 12 | +import Data.So |
| 13 | + |
| 14 | +%default total |
| 15 | + |
| 16 | +-- ============================================================ |
| 17 | +-- C-Compatible Struct Layouts |
| 18 | +-- ============================================================ |
| 19 | + |
| 20 | +||| Size of a C-compatible struct in bytes |
| 21 | +public export |
| 22 | +data StructSize : Nat -> Type where |
| 23 | + MkSize : (n : Nat) -> StructSize n |
| 24 | + |
| 25 | +||| ContentHash layout: exactly 32 bytes, no padding |
| 26 | +public export |
| 27 | +contentHashSize : StructSize 32 |
| 28 | +contentHashSize = MkSize 32 |
| 29 | + |
| 30 | +||| KeyId layout: exactly 16 bytes (UUID) |
| 31 | +public export |
| 32 | +keyIdSize : StructSize 16 |
| 33 | +keyIdSize = MkSize 16 |
| 34 | + |
| 35 | +||| ObliterationProof layout: |
| 36 | +||| 32 (hash) + 32 (nonce) + 32 (commitment) + 8 (passes) + 1 (valid) = 105 |
| 37 | +||| Aligned to 8: 112 bytes |
| 38 | +public export |
| 39 | +oblitProofSize : StructSize 112 |
| 40 | +oblitProofSize = MkSize 112 |
| 41 | + |
| 42 | +-- ============================================================ |
| 43 | +-- Alignment Proofs |
| 44 | +-- ============================================================ |
| 45 | + |
| 46 | +||| Proof that a size is aligned to a boundary |
| 47 | +public export |
| 48 | +data Aligned : (size : Nat) -> (boundary : Nat) -> Type where |
| 49 | + MkAligned : (prf : mod size boundary = 0) -> Aligned size boundary |
| 50 | + |
| 51 | +||| ContentHash is 8-byte aligned |
| 52 | +public export |
| 53 | +hashAligned : Aligned 32 8 |
| 54 | +hashAligned = MkAligned Refl |
| 55 | + |
| 56 | +||| KeyId is 8-byte aligned |
| 57 | +public export |
| 58 | +keyIdAligned : Aligned 16 8 |
| 59 | +keyIdAligned = MkAligned Refl |
| 60 | + |
| 61 | +||| ObliterationProof is 8-byte aligned |
| 62 | +public export |
| 63 | +oblitAligned : Aligned 112 8 |
| 64 | +oblitAligned = MkAligned Refl |
| 65 | + |
| 66 | +-- ============================================================ |
| 67 | +-- Endianness-Safe Serialization |
| 68 | +-- ============================================================ |
| 69 | + |
| 70 | +||| Byte order for serialization |
| 71 | +public export |
| 72 | +data ByteOrder : Type where |
| 73 | + LittleEndian : ByteOrder |
| 74 | + BigEndian : ByteOrder |
| 75 | + NetworkOrder : ByteOrder -- always big-endian |
| 76 | + |
| 77 | +||| Proof that network byte order is big-endian |
| 78 | +public export |
| 79 | +networkIsBig : NetworkOrder = BigEndian -> Void |
| 80 | +networkIsBig Refl impossible |
| 81 | + |
| 82 | +-- Note: NetworkOrder and BigEndian are distinct constructors, |
| 83 | +-- but the FFI layer treats them identically for wire format. |
| 84 | + |
| 85 | +-- ============================================================ |
| 86 | +-- CNO (Certified Null Operation) Proofs |
| 87 | +-- From absolute-zero theory |
| 88 | +-- ============================================================ |
| 89 | + |
| 90 | +||| State of the filesystem at a point in time |
| 91 | +public export |
| 92 | +record FSState where |
| 93 | + constructor MkFSState |
| 94 | + files : List (ValidPath, ContentHash) |
| 95 | + keys : List (KeyId, KeyAlgorithm) |
| 96 | + txCount : Nat |
| 97 | + |
| 98 | +||| An operation with its inverse |
| 99 | +public export |
| 100 | +record ReversibleOp where |
| 101 | + constructor MkRevOp |
| 102 | + kind : OpKind |
| 103 | + forward : FSState -> FSState |
| 104 | + inverse : FSState -> FSState |
| 105 | + |
| 106 | +||| CNO property: forward then inverse = identity |
| 107 | +public export |
| 108 | +data IsCNO : ReversibleOp -> Type where |
| 109 | + MkCNO : ((s : FSState) -> inverse op (forward op s) = s) |
| 110 | + -> IsCNO op |
| 111 | + |
| 112 | +||| Copy-then-delete is a CNO (move + unmove = identity) |
| 113 | +public export |
| 114 | +copyDeleteCNO : (src, dst : ValidPath) -> (h : ContentHash) |
| 115 | + -> (op : ReversibleOp) |
| 116 | + -> (prf : kind op = Copy) |
| 117 | + -> IsCNO op |
| 118 | + |
| 119 | +||| Obliterate does NOT have a CNO inverse — this is intentional |
| 120 | +||| The type system prevents constructing IsCNO for Obliterate |
| 121 | +public export |
| 122 | +data ObliterateIsIrreversible : Type where |
| 123 | + ||| Obliteration is the one operation that breaks reversibility |
| 124 | + ||| by design (GDPR right to erasure) |
| 125 | + Irreversible : ObliterateIsIrreversible |
| 126 | + |
| 127 | +-- ============================================================ |
| 128 | +-- Transaction Atomicity Proof |
| 129 | +-- ============================================================ |
| 130 | + |
| 131 | +||| A sequence of operations in a transaction |
| 132 | +public export |
| 133 | +data TxOps : TxState -> List OpKind -> Type where |
| 134 | + Empty : TxOps Active [] |
| 135 | + Append : TxOps Active ops -> (k : OpKind) -> TxOps Active (ops ++ [k]) |
| 136 | + |
| 137 | +||| Proof: committing a transaction with all-reversible ops is safe |
| 138 | +public export |
| 139 | +data AllReversible : List OpKind -> Type where |
| 140 | + NilReversible : AllReversible [] |
| 141 | + ConsReversible : Not (k = Obliterate) |
| 142 | + -> AllReversible ks |
| 143 | + -> AllReversible (k :: ks) |
| 144 | + |
| 145 | +||| Proof: a committed transaction can be rolled back if all ops are reversible |
| 146 | +public export |
| 147 | +canRollback : AllReversible ops -> TxOps Active ops -> TxOps Active ops |
| 148 | +canRollback NilReversible Empty = Empty |
| 149 | +canRollback (ConsReversible notOblit rest) (Append txOps k) = |
| 150 | + Append (canRollback rest txOps) k |
| 151 | + |
| 152 | +-- ============================================================ |
| 153 | +-- Key Derivation Safety |
| 154 | +-- ============================================================ |
| 155 | + |
| 156 | +||| Argon2id parameters with proven safety bounds |
| 157 | +public export |
| 158 | +record Argon2Params where |
| 159 | + constructor MkArgon2 |
| 160 | + timeCost : Nat |
| 161 | + memoryCost : Nat -- in KiB |
| 162 | + parallelism : Nat |
| 163 | + outputLen : Nat |
| 164 | + timeOk : So (timeCost >= 3) |
| 165 | + memoryOk : So (memoryCost >= 65536) -- 64 MiB minimum |
| 166 | + parallelOk : So (parallelism >= 1) |
| 167 | + outputOk : So (outputLen >= 32) |
| 168 | + |
| 169 | +||| Default secure parameters |
| 170 | +public export |
| 171 | +defaultArgon2 : Argon2Params |
| 172 | +defaultArgon2 = MkArgon2 3 65536 4 32 Oh Oh Oh Oh |
| 173 | + |
| 174 | +||| Proof: default parameters meet OWASP recommendations |
| 175 | +public export |
| 176 | +defaultMeetsOWASP : So (timeCost defaultArgon2 >= 3) |
| 177 | +defaultMeetsOWASP = Oh |
| 178 | + |
| 179 | +public export |
| 180 | +defaultMemoryOWASP : So (memoryCost defaultArgon2 >= 65536) |
| 181 | +defaultMemoryOWASP = Oh |
0 commit comments