open-humanity
Privacy-first peer assistance network for sovereign autonomous agents
Brought by drakestapleton
open-humanity / om / trace_contract.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note protocol/open-humanity/trace_contract.om |
| 3 | note Pure seeded OM-2 implementation of: |
| 4 | note 1. Exact-byte Open Humanity UDP wire codec (crates/beacon-core/src/{wire,schema}.rs): |
| 5 | note - Frames: Signal (0x4f 0x48 0x01), Register (0x4f 0x52 0x01), Challenge (0x4f 0x51 0x01), |
| 6 | note Ack (0x4f 0x41 0x01), Capsule (0x4f 0x43 0x01), exact bincode 1.3 LE layout |
| 7 | note - Keyed BLAKE3 relay cookies (16 bytes, 120s window + previous window) |
| 8 | note - Ed25519 signing and verification over exact signable_bytes |
| 9 | note - Normalized BLAKE3 error fingerprinting (user home paths scrubbed to ~/, ws collapsed) |
| 10 | note 2. Exact-byte Consent/Export and Trace-Receiver Contract (OHTX v1 over AITR v1): |
| 11 | note - Strict AITR v1 trace segment reader/writer (16B header + 64B IEEE CRC-32 records, |
| 12 | note matching seed/trace/segment.om byte-for-byte) |
| 13 | note - Signed OHCN v1 consent manifest (160B, Ed25519, opt_in=1, cap_flags=0, allowed_type_mask) |
| 14 | note - Fail-closed exporter (oh_trace_export) and receiver (oh_trace_receive) enforcing |
| 15 | note consent, Personal Data Firewall, zero-authority boundary, BLAKE3 integrity, Ed25519 |
| 16 | note authentication, timestamp drift (<= 300s), and replay protection. |
| 17 | note Compose after: |
| 18 | note si/directives/om2.om protocol/open-humanity/lib.om |
| 19 | note protocol/omnet/om/sha512/sha512.om protocol/omnet/om/ed25519/ed25519.om |
| 20 | |
| 21 | note ---- Self-contained RFC 8032 Ed25519 key derivation & signing (reusing ed25519.om + sha512.om) ---- |
| 22 | |
| 23 | fn ohtx_ed_red x64 view uchar 64 dst edit uchar 32 to void |
| 24 | x = new long 64 0 |
| 25 | for i ulong 64 |
| 26 | put x i long get x64 i |
| 27 | end |
| 28 | edmodl x |
| 29 | for i ulong 32 |
| 30 | put dst i uchar get x i |
| 31 | end |
| 32 | end |
| 33 | |
| 34 | fn ohtx_ed_expand seed view uchar 32 ar edit uchar 32 pfx edit uchar 64 to void |
| 35 | m = new uchar 65536 0 |
| 36 | hd = new uchar 64 0 |
| 37 | t = new uchar 64 0 |
| 38 | for i ulong 32 |
| 39 | put m i get seed i |
| 40 | end |
| 41 | s512hash m 32 hd |
| 42 | for i ulong 32 |
| 43 | put t i get hd i |
| 44 | put pfx i get hd add i 32 |
| 45 | end |
| 46 | put t 0 and get t 0 uchar 248 |
| 47 | put t 31 and get t 31 uchar 127 |
| 48 | put t 31 or get t 31 uchar 64 |
| 49 | ohtx_ed_red t ar |
| 50 | end |
| 51 | |
| 52 | fn ohtx_ed_basemul s view uchar 32 dst edit uchar 32 to void |
| 53 | w = new ulong 1024 0 |
| 54 | z = new uchar 32 0 |
| 55 | edinit w |
| 56 | edsmul w s z |
| 57 | edpenc w edq dst |
| 58 | end |
| 59 | |
| 60 | fn ohtx_ed_pubkey seed view uchar 32 pk edit uchar 32 to void |
| 61 | ar = new uchar 32 0 |
| 62 | pfx = new uchar 64 0 |
| 63 | ohtx_ed_expand seed ar pfx |
| 64 | ohtx_ed_basemul ar pk |
| 65 | end |
| 66 | |
| 67 | fn ohtx_ed_muladd r view uchar 32 k view uchar 32 a view uchar 32 sg edit uchar 64 to void |
| 68 | x = new long 64 0 |
| 69 | for i ulong 32 |
| 70 | put x i long get r i |
| 71 | end |
| 72 | for i ulong 32 |
| 73 | ki = long get k i |
| 74 | for j ulong 32 |
| 75 | p = add i j |
| 76 | put x p add get x p mul ki long get a j |
| 77 | end |
| 78 | end |
| 79 | edmodl x |
| 80 | for i ulong 32 |
| 81 | put sg add 32 i uchar get x i |
| 82 | end |
| 83 | end |
| 84 | |
| 85 | fn ohtx_ed_hprefix pfx view uchar 64 m view uchar 65536 n ulong hd edit uchar 64 to void |
| 86 | if gt n 65504 |
| 87 | trap 3 |
| 88 | end |
| 89 | pm = new uchar 65536 0 |
| 90 | for i ulong 32 |
| 91 | put pm i get pfx i |
| 92 | end |
| 93 | for i ulong n bound 65504 |
| 94 | put pm add 32 i get m i |
| 95 | end |
| 96 | s512hash pm add n 32 hd |
| 97 | end |
| 98 | |
| 99 | fn ohtx_ed_sign seed view uchar 32 m view uchar 65536 n ulong sg edit uchar 64 to void |
| 100 | ar = new uchar 32 0 |
| 101 | pfx = new uchar 64 0 |
| 102 | pk = new uchar 32 0 |
| 103 | hd = new uchar 64 0 |
| 104 | r = new uchar 32 0 |
| 105 | rb = new uchar 32 0 |
| 106 | k = new uchar 32 0 |
| 107 | ohtx_ed_expand seed ar pfx |
| 108 | ohtx_ed_basemul ar pk |
| 109 | ohtx_ed_hprefix pfx m n hd |
| 110 | ohtx_ed_red hd r |
| 111 | ohtx_ed_basemul r rb |
| 112 | for i ulong 32 |
| 113 | put sg i get rb i |
| 114 | end |
| 115 | edhram sg pk m n k |
| 116 | ohtx_ed_muladd r k ar sg |
| 117 | end |
| 118 | |
| 119 | const OHW_MAGIC_0 uchar 0x4f |
| 120 | const OHW_VERSION uchar 1 |
| 121 | const OHW_KIND_SIGNAL uchar 0x48 |
| 122 | const OHW_KIND_REGISTER uchar 0x52 |
| 123 | const OHW_KIND_CHALLENGE uchar 0x51 |
| 124 | const OHW_KIND_ACK uchar 0x41 |
| 125 | const OHW_KIND_CAPSULE uchar 0x43 |
| 126 | |
| 127 | const OHW_MAX_DATAGRAM ulong 1200 |
| 128 | const OHW_MAX_CAPSULE ulong 8192 |
| 129 | const OHW_COOKIE_LEN ulong 16 |
| 130 | const OHW_COOKIE_WINDOW ulong 120 |
| 131 | const OHW_MAX_DRIFT_SECS ulong 300 |
| 132 | |
| 133 | note Exact-byte AITR v1 & OHTX v1 constants |
| 134 | const OHTX_AITR_HDR ulong 16 |
| 135 | const OHTX_AITR_REC ulong 64 |
| 136 | const OHTX_AITR_MAX_PAY ulong 52 |
| 137 | const OHTX_CONSENT_LEN ulong 160 |
| 138 | const OHTX_HDR_LEN ulong 296 |
| 139 | const OHTX_REPLAY_CAP ulong 64 |
| 140 | |
| 141 | note Error codes for wire / trace-receiver contract |
| 142 | const OHTX_OK ulong 0 |
| 143 | const OHTX_ERR_BAD_MAGIC ulong 101 |
| 144 | const OHTX_ERR_BAD_VERSION ulong 102 |
| 145 | const OHTX_ERR_TRUNCATED ulong 103 |
| 146 | const OHTX_ERR_TOO_LARGE ulong 104 |
| 147 | const OHTX_ERR_BAD_SIG ulong 105 |
| 148 | const OHTX_ERR_DRIFT ulong 106 |
| 149 | const OHTX_ERR_REPLAY ulong 107 |
| 150 | const OHTX_ERR_NO_CONSENT ulong 108 |
| 151 | const OHTX_ERR_CONSENT_EXPIRED ulong 109 |
| 152 | const OHTX_ERR_CONSENT_AUTH ulong 110 |
| 153 | const OHTX_ERR_WRONG_RECEIVER ulong 111 |
| 154 | const OHTX_ERR_BAD_DIGEST ulong 112 |
| 155 | const OHTX_ERR_AITR_HEADER ulong 113 |
| 156 | const OHTX_ERR_AITR_TORN ulong 114 |
| 157 | const OHTX_ERR_AITR_CRC ulong 115 |
| 158 | const OHTX_ERR_AITR_SEQ ulong 116 |
| 159 | const OHTX_ERR_AITR_PAYLEN ulong 117 |
| 160 | const OHTX_ERR_TYPE_NOT_CONSENTED ulong 118 |
| 161 | const OHTX_ERR_FIREWALL ulong 119 |
| 162 | const OHTX_ERR_ESCALATION ulong 120 |
| 163 | const OHTX_ERR_BAD_FIELD ulong 121 |
| 164 | |
| 165 | text ohtx_dom_consent "open-humanity.trace-consent.v1" |
| 166 | text ohtx_dom_export "open-humanity.trace-export.v1" |
| 167 | |
| 168 | note Receiver replay cache (up to 64 16-byte bundle_ids) |
| 169 | global ohtx_seen_ids uchar 1024 |
| 170 | global ohtx_seen_cnt ulong 1 |
| 171 | |
| 172 | fn ohtx_reset_replay to void |
| 173 | put ohtx_seen_cnt 0 0 |
| 174 | for i ulong 1024 |
| 175 | put ohtx_seen_ids i uchar 0 |
| 176 | end |
| 177 | end |
| 178 | |
| 179 | fn ohtx_put_u16_le dst edit uchar * off ulong v ulong to void |
| 180 | put dst off uchar and v 255 |
| 181 | put dst add off 1 uchar and shr v 8 255 |
| 182 | end |
| 183 | |
| 184 | fn ohtx_get_u16_le src view uchar * off ulong to ulong |
| 185 | ret or ulong get src off shl ulong get src add off 1 8 |
| 186 | end |
| 187 | |
| 188 | fn ohtx_put_u32_le dst edit uchar * off ulong v ulong to void |
| 189 | for i ulong 4 |
| 190 | put dst add off i uchar and shr v mul i 8 255 |
| 191 | end |
| 192 | end |
| 193 | |
| 194 | fn ohtx_get_u32_le src view uchar * off ulong to ulong |
| 195 | acc = ulong 0 |
| 196 | for i ulong 4 |
| 197 | acc = or acc shl ulong get src add off i mul i 8 |
| 198 | end |
| 199 | ret acc |
| 200 | end |
| 201 | |
| 202 | note ---- Self-contained BLAKE3 unkeyed & keyed (exact BLAKE3 spec parity with blake3 1.5) ---- |
| 203 | |
| 204 | fn ohtx_b3_initw w edit ulong 128 to void |
| 205 | put w 83 1779033703 |
| 206 | put w 84 3144134277 |
| 207 | put w 85 1013904242 |
| 208 | put w 86 2773480762 |
| 209 | put w 87 1359893119 |
| 210 | put w 88 2600822924 |
| 211 | put w 89 528734635 |
| 212 | put w 90 1541459225 |
| 213 | put w 91 2 |
| 214 | put w 92 6 |
| 215 | put w 93 3 |
| 216 | put w 94 10 |
| 217 | put w 95 7 |
| 218 | put w 96 0 |
| 219 | put w 97 4 |
| 220 | put w 98 13 |
| 221 | put w 99 1 |
| 222 | put w 100 11 |
| 223 | put w 101 12 |
| 224 | put w 102 5 |
| 225 | put w 103 9 |
| 226 | put w 104 14 |
| 227 | put w 105 15 |
| 228 | put w 106 8 |
| 229 | end |
| 230 | |
| 231 | fn ohtx_b3_mix w edit ulong 128 a ulong b ulong c ulong d ulong j ulong to void |
| 232 | x = get w add 16 mul j 2 |
| 233 | t = add get w a get w b |
| 234 | put w a and add t x 4294967295 |
| 235 | t = xor get w d get w a |
| 236 | put w d and or shr t 16 shl t 16 4294967295 |
| 237 | put w c and add get w c get w d 4294967295 |
| 238 | t = xor get w b get w c |
| 239 | put w b and or shr t 12 shl t 20 4294967295 |
| 240 | x = get w add 17 mul j 2 |
| 241 | t = add get w a get w b |
| 242 | put w a and add t x 4294967295 |
| 243 | t = xor get w d get w a |
| 244 | put w d and or shr t 8 shl t 24 4294967295 |
| 245 | put w c and add get w c get w d 4294967295 |
| 246 | t = xor get w b get w c |
| 247 | put w b and or shr t 7 shl t 25 4294967295 |
| 248 | end |
| 249 | |
| 250 | fn ohtx_b3_round w edit ulong 128 to void |
| 251 | ohtx_b3_mix w 0 4 8 12 0 |
| 252 | ohtx_b3_mix w 1 5 9 13 1 |
| 253 | ohtx_b3_mix w 2 6 10 14 2 |
| 254 | ohtx_b3_mix w 3 7 11 15 3 |
| 255 | ohtx_b3_mix w 0 5 10 15 4 |
| 256 | ohtx_b3_mix w 1 6 11 12 5 |
| 257 | ohtx_b3_mix w 2 7 8 13 6 |
| 258 | ohtx_b3_mix w 3 4 9 14 7 |
| 259 | end |
| 260 | |
| 261 | fn ohtx_b3_perm w edit ulong 128 to void |
| 262 | for i ulong 16 |
| 263 | put w add 32 i get w add 16 get w add 91 i |
| 264 | end |
| 265 | for i ulong 16 |
| 266 | put w add 16 i get w add 32 i |
| 267 | end |
| 268 | end |
| 269 | |
| 270 | fn ohtx_b3_compress w edit ulong 128 blk view uchar 64 to void |
| 271 | for i ulong 16 |
| 272 | o = mul i 4 |
| 273 | v = ulong get blk add o 3 |
| 274 | v = or shl v 8 ulong get blk add o 2 |
| 275 | v = or shl v 8 ulong get blk add o 1 |
| 276 | v = or shl v 8 ulong get blk o |
| 277 | put w add 16 i v |
| 278 | end |
| 279 | for i ulong 8 |
| 280 | put w i get w add 48 i |
| 281 | put w add i 8 get w add 83 i |
| 282 | end |
| 283 | put w 12 and get w 73 4294967295 |
| 284 | put w 13 and shr get w 73 32 4294967295 |
| 285 | put w 14 get w 72 |
| 286 | put w 15 get w 74 |
| 287 | for r ulong 7 |
| 288 | ohtx_b3_round w |
| 289 | ohtx_b3_perm w |
| 290 | end |
| 291 | for i ulong 8 |
| 292 | put w add 56 i xor get w i get w add i 8 |
| 293 | put w add 64 i xor get w add i 8 get w add 48 i |
| 294 | end |
| 295 | end |
| 296 | |
| 297 | fn ohtx_b3_outle w view ulong 128 dst edit uchar 32 to void |
| 298 | for i ulong 8 |
| 299 | v = get w add 56 i |
| 300 | for k ulong 4 |
| 301 | put dst add mul i 4 k uchar and shr v mul k 8 255 |
| 302 | end |
| 303 | end |
| 304 | end |
| 305 | |
| 306 | fn ohtx_b3_copy_cv w edit ulong 128 to void |
| 307 | for i ulong 8 |
| 308 | put w add 48 i get w add 56 i |
| 309 | end |
| 310 | end |
| 311 | |
| 312 | fn ohtx_b3_load_iv w edit ulong 128 to void |
| 313 | for i ulong 8 |
| 314 | put w add 48 i get w add 83 i |
| 315 | end |
| 316 | end |
| 317 | |
| 318 | fn ohtx_b3_fill_blk blk edit uchar 64 msg view uchar * pos ulong blen ulong to void |
| 319 | for i ulong 64 |
| 320 | put blk i uchar 0 |
| 321 | end |
| 322 | for i ulong blen bound 64 |
| 323 | put blk i get msg add pos i |
| 324 | end |
| 325 | end |
| 326 | |
| 327 | note Process one BLAKE3 chunk of length len(msg) (0..1024) with chunk counter ctr. |
| 328 | note Leaves CV in w[48..56) and if is_root=1 sets ROOT flag (8) on the last block. |
| 329 | fn ohtx_b3_chunk w edit ulong 128 msg view uchar * ctr ulong is_root ulong to void |
| 330 | blk = new uchar 64 0 |
| 331 | n = len msg |
| 332 | ohtx_b3_load_iv w |
| 333 | if eq n 0 |
| 334 | put w 72 0 |
| 335 | put w 73 ctr |
| 336 | fl = ulong 3 |
| 337 | if ne is_root 0 |
| 338 | fl = ulong 11 |
| 339 | end |
| 340 | put w 74 fl |
| 341 | ohtx_b3_compress w blk |
| 342 | ohtx_b3_copy_cv w |
| 343 | ret |
| 344 | end |
| 345 | pos = ulong 0 |
| 346 | while lt pos n bound 16 |
| 347 | blen = sub n pos |
| 348 | if gt blen 64 |
| 349 | blen = ulong 64 |
| 350 | end |
| 351 | ohtx_b3_fill_blk blk msg pos blen |
| 352 | fl = ulong 0 |
| 353 | if eq pos 0 |
| 354 | fl = or fl 1 |
| 355 | end |
| 356 | if eq add pos blen n |
| 357 | fl = or fl 2 |
| 358 | if ne is_root 0 |
| 359 | fl = or fl 8 |
| 360 | end |
| 361 | end |
| 362 | put w 72 blen |
| 363 | put w 73 ctr |
| 364 | put w 74 fl |
| 365 | ohtx_b3_compress w blk |
| 366 | ohtx_b3_copy_cv w |
| 367 | pos = add pos blen |
| 368 | end |
| 369 | end |
| 370 | |
| 371 | fn ohtx_b3_put4 blk edit uchar 64 off ulong v ulong to void |
| 372 | put blk off uchar and v 255 |
| 373 | put blk add off 1 uchar and shr v 8 255 |
| 374 | put blk add off 2 uchar and shr v 16 255 |
| 375 | put blk add off 3 uchar and shr v 24 255 |
| 376 | end |
| 377 | |
| 378 | fn ohtx_b3_parent w edit ulong 128 st view ulong 128 lidx ulong r_off ulong is_root ulong to void |
| 379 | blk = new uchar 64 0 |
| 380 | for i ulong 8 |
| 381 | o = mul i 4 |
| 382 | ohtx_b3_put4 blk o get st add mul lidx 8 i |
| 383 | ohtx_b3_put4 blk add 32 o get w add r_off i |
| 384 | end |
| 385 | ohtx_b3_load_iv w |
| 386 | put w 72 64 |
| 387 | put w 73 0 |
| 388 | fl = ulong 4 |
| 389 | if ne is_root 0 |
| 390 | fl = ulong 12 |
| 391 | end |
| 392 | put w 74 fl |
| 393 | ohtx_b3_compress w blk |
| 394 | ohtx_b3_copy_cv w |
| 395 | end |
| 396 | |
| 397 | fn ohtx_b3_push w view ulong 128 st edit ulong 128 depth ulong to void |
| 398 | base = mul depth 8 |
| 399 | for i ulong 8 |
| 400 | put st add base i get w add 48 i |
| 401 | end |
| 402 | end |
| 403 | |
| 404 | note Full multi-chunk unkeyed BLAKE3 hash (up to 64 KiB) matching blake3::hash byte-for-byte. |
| 405 | fn ohtx_b3_hash msg view uchar * out32 edit uchar 32 to void |
| 406 | w = new ulong 128 0 |
| 407 | st = new ulong 128 0 |
| 408 | ohtx_b3_initw w |
| 409 | n = len msg |
| 410 | if le n 1024 |
| 411 | ohtx_b3_chunk w msg 0 1 |
| 412 | ohtx_b3_outle w out32 |
| 413 | ret |
| 414 | end |
| 415 | depth = ulong 0 |
| 416 | ctr = ulong 0 |
| 417 | off = ulong 0 |
| 418 | while gt sub n off 1024 bound 64 |
| 419 | ohtx_b3_chunk w part msg off 1024 ctr 0 |
| 420 | tot = add ctr 1 |
| 421 | while eq and tot 1 0 bound 8 |
| 422 | depth = sub depth 1 |
| 423 | ohtx_b3_parent w st depth 48 0 |
| 424 | tot = shr tot 1 |
| 425 | end |
| 426 | ohtx_b3_push w st depth |
| 427 | depth = add depth 1 |
| 428 | ctr = add ctr 1 |
| 429 | off = add off 1024 |
| 430 | end |
| 431 | ohtx_b3_chunk w part msg off sub n off ctr 0 |
| 432 | while gt depth 1 bound 8 |
| 433 | depth = sub depth 1 |
| 434 | ohtx_b3_parent w st depth 48 0 |
| 435 | end |
| 436 | ohtx_b3_parent w st 0 48 1 |
| 437 | ohtx_b3_outle w out32 |
| 438 | end |
| 439 | |
| 440 | fn ohtx_b3_load_key w edit ulong 128 secret view uchar 32 to void |
| 441 | for i ulong 8 |
| 442 | o = mul i 4 |
| 443 | b0 = ulong get secret o |
| 444 | b1 = ulong get secret add o 1 |
| 445 | b2 = ulong get secret add o 2 |
| 446 | b3 = ulong get secret add o 3 |
| 447 | kv = or b0 shl b1 8 |
| 448 | kv = or kv shl b2 16 |
| 449 | kv = or kv shl b3 24 |
| 450 | put w add 48 i kv |
| 451 | end |
| 452 | end |
| 453 | |
| 454 | note Keyed BLAKE3 single-chunk (n <= 64 bytes), matching blake3::Hasher::new_keyed(secret). |
| 455 | note Uses key words as input CV and sets KEYED_HASH (16) | CHUNK_START (1) | CHUNK_END (2) | ROOT (8) = 27. |
| 456 | fn ohtx_b3_keyed_64 secret view uchar 32 msg view uchar * out32 edit uchar 32 to void |
| 457 | n = len msg |
| 458 | if gt n 64 |
| 459 | trap 3 |
| 460 | end |
| 461 | w = new ulong 128 0 |
| 462 | blk = new uchar 64 0 |
| 463 | ohtx_b3_initw w |
| 464 | ohtx_b3_load_key w secret |
| 465 | for i ulong n bound 64 |
| 466 | put blk i get msg i |
| 467 | end |
| 468 | put w 72 n |
| 469 | put w 73 0 |
| 470 | put w 74 27 |
| 471 | ohtx_b3_compress w blk |
| 472 | ohtx_b3_outle w out32 |
| 473 | end |
| 474 | |
| 475 | note Exact relay_cookie(secret, addr_ascii, window) -> 16 bytes (crates/beacon-core/src/wire.rs). |
| 476 | fn ohw_relay_cookie secret view uchar 32 addr view uchar * window ulong cookie edit uchar 16 to void |
| 477 | alen = len addr |
| 478 | if gt alen 56 |
| 479 | trap 3 |
| 480 | end |
| 481 | inp = new uchar 64 0 |
| 482 | for i ulong alen bound 56 |
| 483 | put inp i get addr i |
| 484 | end |
| 485 | for i ulong 8 |
| 486 | put inp add alen i uchar and shr window mul i 8 255 |
| 487 | end |
| 488 | h32 = new uchar 32 0 |
| 489 | ohtx_b3_keyed_64 secret part inp 0 add alen 8 h32 |
| 490 | for i ulong 16 |
| 491 | put cookie i get h32 i |
| 492 | end |
| 493 | end |
| 494 | |
| 495 | note True iff cookie matches current window or previous window (120s windows). |
| 496 | fn ohw_cookie_valid secret view uchar 32 addr view uchar * now_secs ulong cookie view uchar 16 to bool |
| 497 | win = div now_secs OHW_COOKIE_WINDOW |
| 498 | c0 = new uchar 16 0 |
| 499 | ohw_relay_cookie secret addr win c0 |
| 500 | ok0 = true |
| 501 | for i ulong 16 |
| 502 | if ne get c0 i get cookie i |
| 503 | ok0 = false |
| 504 | break |
| 505 | end |
| 506 | end |
| 507 | if ok0 |
| 508 | ret true |
| 509 | end |
| 510 | if gt win 0 |
| 511 | ohw_relay_cookie secret addr sub win 1 c0 |
| 512 | ok1 = true |
| 513 | for i ulong 16 |
| 514 | if ne get c0 i get cookie i |
| 515 | ok1 = false |
| 516 | break |
| 517 | end |
| 518 | end |
| 519 | ret ok1 |
| 520 | end |
| 521 | ret false |
| 522 | end |
| 523 | |
| 524 | fn ohtx_is_ws b uchar to bool |
| 525 | if eq b uchar 32 |
| 526 | ret true |
| 527 | end |
| 528 | if eq b uchar 9 |
| 529 | ret true |
| 530 | end |
| 531 | if eq b uchar 10 |
| 532 | ret true |
| 533 | end |
| 534 | ret eq b uchar 13 |
| 535 | end |
| 536 | |
| 537 | note Normalized error fingerprint: scrub user paths to ~/, collapse whitespace, BLAKE3-256. |
| 538 | fn ohw_fingerprint_error err_text view uchar * out32 edit uchar 32 to void |
| 539 | scbuf = new uchar 512 0 |
| 540 | sn = oh_scrub_paths err_text scbuf |
| 541 | norm = new uchar 512 0 |
| 542 | nn = ulong 0 |
| 543 | prev_ws = true |
| 544 | for i ulong sn bound 512 |
| 545 | b = get scbuf i |
| 546 | if ohtx_is_ws b |
| 547 | if not prev_ws |
| 548 | if lt nn 512 |
| 549 | put norm nn uchar 32 |
| 550 | nn = add nn 1 |
| 551 | end |
| 552 | prev_ws = true |
| 553 | end |
| 554 | else |
| 555 | if lt nn 512 |
| 556 | put norm nn b |
| 557 | nn = add nn 1 |
| 558 | end |
| 559 | prev_ws = false |
| 560 | end |
| 561 | end |
| 562 | if gt nn 0 |
| 563 | if eq get norm sub nn 1 uchar 32 |
| 564 | nn = sub nn 1 |
| 565 | end |
| 566 | end |
| 567 | ohtx_b3_hash part norm 0 nn out32 |
| 568 | end |
| 569 | |
| 570 | note ---- Exact-byte Open Humanity UDP Wire Codec (bincode 1.3 LE parity with wire.rs) ---- |
| 571 | |
| 572 | note Encode Challenge frame (19 bytes): [0x4f, 0x51, 0x01] || cookie[16]. |
| 573 | fn ohw_encode_challenge cookie view uchar 16 dst edit uchar 19 to ulong |
| 574 | put dst 0 OHW_MAGIC_0 |
| 575 | put dst 1 OHW_KIND_CHALLENGE |
| 576 | put dst 2 OHW_VERSION |
| 577 | for i ulong 16 |
| 578 | put dst add 3 i get cookie i |
| 579 | end |
| 580 | ret 19 |
| 581 | end |
| 582 | |
| 583 | note Decode Challenge frame. |
| 584 | fn ohw_decode_challenge wire view uchar * cookie edit uchar 16 to ulong |
| 585 | if ne len wire 19 |
| 586 | ret OHTX_ERR_TRUNCATED |
| 587 | end |
| 588 | if or ne get wire 0 OHW_MAGIC_0 ne get wire 1 OHW_KIND_CHALLENGE |
| 589 | ret OHTX_ERR_BAD_MAGIC |
| 590 | end |
| 591 | if ne get wire 2 OHW_VERSION |
| 592 | ret OHTX_ERR_BAD_VERSION |
| 593 | end |
| 594 | for i ulong 16 |
| 595 | put cookie i get wire add 3 i |
| 596 | end |
| 597 | ret OHTX_OK |
| 598 | end |
| 599 | |
| 600 | note Encode Ack frame (8 bytes): [0x4f, 0x41, 0x01] || topics(u8) || ttl_secs(u32 LE). |
| 601 | fn ohw_encode_ack topics uchar ttl_secs ulong dst edit uchar 8 to ulong |
| 602 | put dst 0 OHW_MAGIC_0 |
| 603 | put dst 1 OHW_KIND_ACK |
| 604 | put dst 2 OHW_VERSION |
| 605 | put dst 3 topics |
| 606 | ohtx_put_u32_le dst 4 ttl_secs |
| 607 | ret 8 |
| 608 | end |
| 609 | |
| 610 | note Decode Ack frame into meta[0]=topics, meta[1]=ttl_secs. |
| 611 | fn ohw_decode_ack wire view uchar * meta edit ulong 2 to ulong |
| 612 | if ne len wire 8 |
| 613 | ret OHTX_ERR_TRUNCATED |
| 614 | end |
| 615 | if or ne get wire 0 OHW_MAGIC_0 ne get wire 1 OHW_KIND_ACK |
| 616 | ret OHTX_ERR_BAD_MAGIC |
| 617 | end |
| 618 | if ne get wire 2 OHW_VERSION |
| 619 | ret OHTX_ERR_BAD_VERSION |
| 620 | end |
| 621 | put meta 0 ulong get wire 3 |
| 622 | put meta 1 ohtx_get_u32_le wire 4 |
| 623 | ret OHTX_OK |
| 624 | end |
| 625 | |
| 626 | note Encode and Ed25519-sign Register frame (133 bytes) matching wire.rs::Frame::Register byte-for-byte. |
| 627 | note Layout: [0..3) 4f 52 01, [3..35) pubkey, [35..43) ts_u64_le, [43] topics, [44] replay, |
| 628 | note [45..61) cookie, [61..69) u64_le(64), [69..133) ed25519_sig. |
| 629 | fn ohw_encode_register seed view uchar 32 ts ulong topics uchar replay uchar cookie view uchar 16 dst edit uchar 133 to ulong |
| 630 | pk = new uchar 32 0 |
| 631 | ohtx_ed_pubkey seed pk |
| 632 | put dst 0 OHW_MAGIC_0 |
| 633 | put dst 1 OHW_KIND_REGISTER |
| 634 | put dst 2 OHW_VERSION |
| 635 | for i ulong 32 |
| 636 | put dst add 3 i get pk i |
| 637 | end |
| 638 | oh_put_u64_le dst 35 ts |
| 639 | put dst 43 topics |
| 640 | put dst 44 replay |
| 641 | for i ulong 16 |
| 642 | put dst add 45 i get cookie i |
| 643 | end |
| 644 | oh_put_u64_le dst 61 64 |
| 645 | for i ulong 64 |
| 646 | put dst add 69 i uchar 0 |
| 647 | end |
| 648 | note signable_bytes is dst[3..133) (130 bytes, signature zeroed) |
| 649 | sbuf = new uchar 65536 0 |
| 650 | for i ulong 130 |
| 651 | put sbuf i get dst add 3 i |
| 652 | end |
| 653 | sig = new uchar 64 0 |
| 654 | ohtx_ed_sign seed sbuf 130 sig |
| 655 | for i ulong 64 |
| 656 | put dst add 69 i get sig i |
| 657 | end |
| 658 | ret 133 |
| 659 | end |
| 660 | |
| 661 | note Verify Register frame: magic, version, exact length 133, sig_len==64, timestamp drift <= 300s, Ed25519. |
| 662 | fn ohw_verify_register wire view uchar * now_secs ulong out_pk edit uchar 32 out_meta edit ulong 3 to ulong |
| 663 | if ne len wire 133 |
| 664 | ret OHTX_ERR_TRUNCATED |
| 665 | end |
| 666 | if or ne get wire 0 OHW_MAGIC_0 ne get wire 1 OHW_KIND_REGISTER |
| 667 | ret OHTX_ERR_BAD_MAGIC |
| 668 | end |
| 669 | if ne get wire 2 OHW_VERSION |
| 670 | ret OHTX_ERR_BAD_VERSION |
| 671 | end |
| 672 | if ne oh_get_u64_le wire 61 64 |
| 673 | ret OHTX_ERR_BAD_FIELD |
| 674 | end |
| 675 | ts = oh_get_u64_le wire 35 |
| 676 | diff = ulong 0 |
| 677 | if ge now_secs ts |
| 678 | diff = sub now_secs ts |
| 679 | else |
| 680 | diff = sub ts now_secs |
| 681 | end |
| 682 | if gt diff OHW_MAX_DRIFT_SECS |
| 683 | ret OHTX_ERR_DRIFT |
| 684 | end |
| 685 | pk = new uchar 32 0 |
| 686 | sig = new uchar 64 0 |
| 687 | sbuf = new uchar 65536 0 |
| 688 | for i ulong 32 |
| 689 | b = get wire add 3 i |
| 690 | put pk i b |
| 691 | put out_pk i b |
| 692 | end |
| 693 | for i ulong 64 |
| 694 | put sig i get wire add 69 i |
| 695 | end |
| 696 | for i ulong 66 |
| 697 | put sbuf i get wire add 3 i |
| 698 | end |
| 699 | note sbuf[66..130) stays zero (signable_bytes has zeroed signature) |
| 700 | if ne edverify pk sbuf 130 sig 0 1 |
| 701 | ret OHTX_ERR_BAD_SIG |
| 702 | end |
| 703 | put out_meta 0 ts |
| 704 | put out_meta 1 ulong get wire 43 |
| 705 | put out_meta 2 ulong get wire 44 |
| 706 | ret OHTX_OK |
| 707 | end |
| 708 | |
| 709 | note ---- Exact-byte AITR v1 Trace Segment Helpers (matches seed/trace/segment.om) ---- |
| 710 | |
| 711 | fn ohtx_crc32 buf view uchar * off ulong n ulong to ulong |
| 712 | c = ulong 4294967295 |
| 713 | for i ulong n bound 64 |
| 714 | c = xor c ulong get buf add off i |
| 715 | for k ulong 8 |
| 716 | if eq and c 1 1 |
| 717 | c = xor shr c 1 3988292384 |
| 718 | else |
| 719 | c = shr c 1 |
| 720 | end |
| 721 | end |
| 722 | end |
| 723 | ret xor c 4294967295 |
| 724 | end |
| 725 | |
| 726 | note Write 16-byte AITR v1 header at dst[0..16). |
| 727 | fn ohtx_aitr_write_hdr dst edit uchar * to void |
| 728 | put dst 0 uchar 65 |
| 729 | put dst 1 uchar 73 |
| 730 | put dst 2 uchar 84 |
| 731 | put dst 3 uchar 82 |
| 732 | ohtx_put_u16_le dst 4 1 |
| 733 | ohtx_put_u16_le dst 6 64 |
| 734 | ohtx_put_u32_le dst 8 16 |
| 735 | ohtx_put_u32_le dst 12 0 |
| 736 | end |
| 737 | |
| 738 | note Write one 64-byte AITR v1 record at dst[off..off+64) with IEEE CRC-32. |
| 739 | note rinfo packs rtype (low 16 bits) and seq (bits 16..47): rinfo = or rtype shl seq 16. |
| 740 | fn ohtx_aitr_write_rec dst edit uchar * off ulong rinfo ulong pay view uchar * to ulong |
| 741 | rtype = and rinfo 65535 |
| 742 | seq = and shr rinfo 16 4294967295 |
| 743 | plen = len pay |
| 744 | if gt plen OHTX_AITR_MAX_PAY |
| 745 | ret OHTX_ERR_AITR_PAYLEN |
| 746 | end |
| 747 | for i ulong 64 |
| 748 | put dst add off i uchar 0 |
| 749 | end |
| 750 | ohtx_put_u16_le dst off rtype |
| 751 | ohtx_put_u16_le dst add off 2 plen |
| 752 | ohtx_put_u32_le dst add off 4 seq |
| 753 | pbase = add off 8 |
| 754 | for i ulong plen bound 52 |
| 755 | put dst add pbase i get pay i |
| 756 | end |
| 757 | crc = ohtx_crc32 dst off 60 |
| 758 | ohtx_put_u32_le dst add off 60 crc |
| 759 | ret OHTX_OK |
| 760 | end |
| 761 | |
| 762 | note Validate AITR v1 header and exact record alignment; sets out_rcnt[0] = record count. |
| 763 | fn ohtx_aitr_validate_hdr seg view uchar * out_rcnt edit ulong 1 to ulong |
| 764 | n = len seg |
| 765 | if lt n OHTX_AITR_HDR |
| 766 | ret OHTX_ERR_AITR_HEADER |
| 767 | end |
| 768 | if ne get seg 0 uchar 65 |
| 769 | ret OHTX_ERR_AITR_HEADER |
| 770 | end |
| 771 | if ne get seg 1 uchar 73 |
| 772 | ret OHTX_ERR_AITR_HEADER |
| 773 | end |
| 774 | if ne get seg 2 uchar 84 |
| 775 | ret OHTX_ERR_AITR_HEADER |
| 776 | end |
| 777 | if ne get seg 3 uchar 82 |
| 778 | ret OHTX_ERR_AITR_HEADER |
| 779 | end |
| 780 | if ne ohtx_get_u16_le seg 4 1 |
| 781 | ret OHTX_ERR_AITR_HEADER |
| 782 | end |
| 783 | if ne ohtx_get_u16_le seg 6 64 |
| 784 | ret OHTX_ERR_AITR_HEADER |
| 785 | end |
| 786 | if ne ohtx_get_u32_le seg 8 16 |
| 787 | ret OHTX_ERR_AITR_HEADER |
| 788 | end |
| 789 | if ne ohtx_get_u32_le seg 12 0 |
| 790 | ret OHTX_ERR_AITR_HEADER |
| 791 | end |
| 792 | body = sub n OHTX_AITR_HDR |
| 793 | if ne rem body OHTX_AITR_REC 0 |
| 794 | ret OHTX_ERR_AITR_TORN |
| 795 | end |
| 796 | put out_rcnt 0 div body OHTX_AITR_REC |
| 797 | ret OHTX_OK |
| 798 | end |
| 799 | |
| 800 | note Validate one 64-byte AITR v1 record at index idx (seq, paylen <= 52, zero padding, IEEE CRC-32). |
| 801 | fn ohtx_aitr_validate_rec seg view uchar * idx ulong out_type edit ulong 1 out_plen edit ulong 1 to ulong |
| 802 | off = add OHTX_AITR_HDR mul idx OHTX_AITR_REC |
| 803 | rtype = ohtx_get_u16_le seg off |
| 804 | plen = ohtx_get_u16_le seg add off 2 |
| 805 | seq = ohtx_get_u32_le seg add off 4 |
| 806 | if gt plen OHTX_AITR_MAX_PAY |
| 807 | ret OHTX_ERR_AITR_PAYLEN |
| 808 | end |
| 809 | if ne seq idx |
| 810 | ret OHTX_ERR_AITR_SEQ |
| 811 | end |
| 812 | pbase = add add off 8 plen |
| 813 | zcnt = sub OHTX_AITR_MAX_PAY plen |
| 814 | for j ulong zcnt bound 52 |
| 815 | if ne get seg add pbase j uchar 0 |
| 816 | ret OHTX_ERR_AITR_PAYLEN |
| 817 | end |
| 818 | end |
| 819 | want_crc = ohtx_get_u32_le seg add off 60 |
| 820 | got_crc = ohtx_crc32 seg off 60 |
| 821 | if ne want_crc got_crc |
| 822 | ret OHTX_ERR_AITR_CRC |
| 823 | end |
| 824 | put out_type 0 rtype |
| 825 | put out_plen 0 plen |
| 826 | ret OHTX_OK |
| 827 | end |
| 828 | |
| 829 | note ---- Exact-byte Consent Manifest (OHCN v1, 160 bytes, Ed25519 signed) ---- |
| 830 | |
| 831 | fn ohtx_sign_consent seed view uchar 32 recv_pk view uchar 32 type_mask ulong not_after ulong cap_flags ulong dst edit uchar 160 to void |
| 832 | npk = new uchar 32 0 |
| 833 | ohtx_ed_pubkey seed npk |
| 834 | put dst 0 uchar 79 |
| 835 | put dst 1 uchar 72 |
| 836 | put dst 2 uchar 67 |
| 837 | put dst 3 uchar 78 |
| 838 | put dst 4 uchar 1 |
| 839 | put dst 5 uchar 1 |
| 840 | put dst 6 uchar 0 |
| 841 | put dst 7 uchar 0 |
| 842 | for i ulong 32 |
| 843 | put dst add 8 i get npk i |
| 844 | put dst add 40 i get recv_pk i |
| 845 | end |
| 846 | oh_put_u64_le dst 72 type_mask |
| 847 | oh_put_u64_le dst 80 not_after |
| 848 | oh_put_u64_le dst 88 cap_flags |
| 849 | sbuf = new uchar 65536 0 |
| 850 | dlen = len ohtx_dom_consent |
| 851 | for i ulong dlen bound 64 |
| 852 | put sbuf i get ohtx_dom_consent i |
| 853 | end |
| 854 | for i ulong 96 |
| 855 | put sbuf add dlen i get dst i |
| 856 | end |
| 857 | sig = new uchar 64 0 |
| 858 | ohtx_ed_sign seed sbuf add dlen 96 sig |
| 859 | for i ulong 64 |
| 860 | put dst add 96 i get sig i |
| 861 | end |
| 862 | end |
| 863 | |
| 864 | fn ohtx_verify_consent cbuf view uchar * now_secs ulong expect_recv view uchar 32 out_mask edit ulong 1 to ulong |
| 865 | if ne len cbuf OHTX_CONSENT_LEN |
| 866 | ret OHTX_ERR_TRUNCATED |
| 867 | end |
| 868 | if ne get cbuf 0 uchar 79 |
| 869 | ret OHTX_ERR_BAD_MAGIC |
| 870 | end |
| 871 | if ne get cbuf 1 uchar 72 |
| 872 | ret OHTX_ERR_BAD_MAGIC |
| 873 | end |
| 874 | if ne get cbuf 2 uchar 67 |
| 875 | ret OHTX_ERR_BAD_MAGIC |
| 876 | end |
| 877 | if ne get cbuf 3 uchar 78 |
| 878 | ret OHTX_ERR_BAD_MAGIC |
| 879 | end |
| 880 | if ne get cbuf 4 uchar 1 |
| 881 | ret OHTX_ERR_BAD_VERSION |
| 882 | end |
| 883 | if ne get cbuf 5 uchar 1 |
| 884 | ret OHTX_ERR_NO_CONSENT |
| 885 | end |
| 886 | if or ne get cbuf 6 uchar 0 ne get cbuf 7 uchar 0 |
| 887 | ret OHTX_ERR_BAD_FIELD |
| 888 | end |
| 889 | note Zero-authority boundary: cap_flags MUST be 0 |
| 890 | if ne oh_get_u64_le cbuf 88 0 |
| 891 | ret OHTX_ERR_CONSENT_AUTH |
| 892 | end |
| 893 | not_after = oh_get_u64_le cbuf 80 |
| 894 | if gt now_secs not_after |
| 895 | ret OHTX_ERR_CONSENT_EXPIRED |
| 896 | end |
| 897 | note Receiver binding check (and non-zero receiver pubkey) |
| 898 | nz = false |
| 899 | for i ulong 32 |
| 900 | rb = get cbuf add 40 i |
| 901 | if ne rb uchar 0 |
| 902 | nz = true |
| 903 | end |
| 904 | if ne rb get expect_recv i |
| 905 | ret OHTX_ERR_WRONG_RECEIVER |
| 906 | end |
| 907 | end |
| 908 | if not nz |
| 909 | ret OHTX_ERR_WRONG_RECEIVER |
| 910 | end |
| 911 | npk = new uchar 32 0 |
| 912 | sig = new uchar 64 0 |
| 913 | for i ulong 32 |
| 914 | put npk i get cbuf add 8 i |
| 915 | end |
| 916 | for i ulong 64 |
| 917 | put sig i get cbuf add 96 i |
| 918 | end |
| 919 | sbuf = new uchar 65536 0 |
| 920 | dlen = len ohtx_dom_consent |
| 921 | for i ulong dlen bound 64 |
| 922 | put sbuf i get ohtx_dom_consent i |
| 923 | end |
| 924 | for i ulong 96 |
| 925 | put sbuf add dlen i get cbuf i |
| 926 | end |
| 927 | if ne edverify npk sbuf add dlen 96 sig 0 1 |
| 928 | ret OHTX_ERR_BAD_SIG |
| 929 | end |
| 930 | put out_mask 0 oh_get_u64_le cbuf 72 |
| 931 | ret OHTX_OK |
| 932 | end |
| 933 | |
| 934 | note Check a single trace record payload against Personal Data Firewall + escalation detector. |
| 935 | fn ohtx_check_payload_clean pay view uchar * to ulong |
| 936 | if eq len pay 0 |
| 937 | ret OHTX_OK |
| 938 | end |
| 939 | prc = oh_privacy_check pay |
| 940 | if ne prc OH_OK |
| 941 | ret OHTX_ERR_FIREWALL |
| 942 | end |
| 943 | if oh_scan_escalation_raw pay |
| 944 | ret OHTX_ERR_ESCALATION |
| 945 | end |
| 946 | ret OHTX_OK |
| 947 | end |
| 948 | |
| 949 | note ---- Exact-byte Trace Exporter (AITR v1 + OHCN v1 -> signed OHTX v1 bundle) ---- |
| 950 | |
| 951 | global ohtx_last_export_len ulong 1 |
| 952 | global ohtx_last_recv_len ulong 1 |
| 953 | global ohtx_export_bid uchar 16 |
| 954 | |
| 955 | fn ohtx_set_bundle_id bid view uchar 16 to void |
| 956 | for i ulong 16 |
| 957 | put ohtx_export_bid i get bid i |
| 958 | end |
| 959 | end |
| 960 | |
| 961 | fn ohtx_export_len to ulong |
| 962 | ret get ohtx_last_export_len 0 |
| 963 | end |
| 964 | |
| 965 | fn ohtx_recv_len to ulong |
| 966 | ret get ohtx_last_recv_len 0 |
| 967 | end |
| 968 | |
| 969 | fn ohtx_type_allowed tmask ulong rtype ulong to bool |
| 970 | if ge rtype 64 |
| 971 | ret false |
| 972 | end |
| 973 | ret ne and shr tmask rtype 1 0 |
| 974 | end |
| 975 | |
| 976 | fn ohtx_append_rec out_seg edit uchar 4112 out_cnt edit ulong 1 rtype ulong pay view uchar * to ulong |
| 977 | oc = get out_cnt 0 |
| 978 | rinfo = or rtype shl oc 16 |
| 979 | doff = add OHTX_AITR_HDR mul oc OHTX_AITR_REC |
| 980 | rc = ohtx_aitr_write_rec out_seg doff rinfo pay |
| 981 | if ne rc OHTX_OK |
| 982 | ret rc |
| 983 | end |
| 984 | put out_cnt 0 add oc 1 |
| 985 | ret OHTX_OK |
| 986 | end |
| 987 | |
| 988 | fn ohtx_filter_one in_seg view uchar * idx ulong tmask ulong out_seg edit uchar 4112 out_cnt edit ulong 1 to ulong |
| 989 | rt_box = new ulong 1 0 |
| 990 | pl_box = new ulong 1 0 |
| 991 | rrc = ohtx_aitr_validate_rec in_seg idx rt_box pl_box |
| 992 | if ne rrc OHTX_OK |
| 993 | ret rrc |
| 994 | end |
| 995 | if not ohtx_type_allowed tmask get rt_box 0 |
| 996 | ret OHTX_OK |
| 997 | end |
| 998 | poff = add add OHTX_AITR_HDR mul idx OHTX_AITR_REC 8 |
| 999 | rrc = ohtx_check_payload_clean part in_seg poff get pl_box 0 |
| 1000 | if ne rrc OHTX_OK |
| 1001 | ret rrc |
| 1002 | end |
| 1003 | ret ohtx_append_rec out_seg out_cnt get rt_box 0 part in_seg poff get pl_box 0 |
| 1004 | end |
| 1005 | |
| 1006 | note Helper: filter consented AITR v1 records and check Personal Data Firewall on every byte. |
| 1007 | fn ohtx_filter_aitr in_seg view uchar * tmask ulong out_seg edit uchar 4112 out_seglen edit ulong 1 to ulong |
| 1008 | rcnt_box = new ulong 1 0 |
| 1009 | out_cnt = new ulong 1 0 |
| 1010 | hrc = ohtx_aitr_validate_hdr in_seg rcnt_box |
| 1011 | if ne hrc OHTX_OK |
| 1012 | ret hrc |
| 1013 | end |
| 1014 | rcnt = get rcnt_box 0 |
| 1015 | if gt rcnt 64 |
| 1016 | ret OHTX_ERR_TOO_LARGE |
| 1017 | end |
| 1018 | ohtx_aitr_write_hdr out_seg |
| 1019 | for i ulong rcnt bound 64 |
| 1020 | frc = ohtx_filter_one in_seg i tmask out_seg out_cnt |
| 1021 | if ne frc OHTX_OK |
| 1022 | ret frc |
| 1023 | end |
| 1024 | end |
| 1025 | put out_seglen 0 add OHTX_AITR_HDR mul get out_cnt 0 OHTX_AITR_REC |
| 1026 | ret OHTX_OK |
| 1027 | end |
| 1028 | |
| 1029 | note Helper: sign OHTX v1 header[0..232) with Ed25519 into dst[232..296). |
| 1030 | fn ohtx_sign_bundle_hdr seed view uchar 32 dst edit uchar 4408 to void |
| 1031 | sbuf = new uchar 65536 0 |
| 1032 | dlen = len ohtx_dom_export |
| 1033 | for i ulong dlen bound 64 |
| 1034 | put sbuf i get ohtx_dom_export i |
| 1035 | end |
| 1036 | for i ulong 232 |
| 1037 | put sbuf add dlen i get dst i |
| 1038 | end |
| 1039 | sig = new uchar 64 0 |
| 1040 | ohtx_ed_sign seed sbuf add dlen 232 sig |
| 1041 | for i ulong 64 |
| 1042 | put dst add 232 i get sig i |
| 1043 | end |
| 1044 | end |
| 1045 | |
| 1046 | fn ohtx_verify_consent_for_export seed view uchar 32 consent view uchar 160 now_secs ulong mask_box edit ulong 1 to ulong |
| 1047 | recv_pk = new uchar 32 0 |
| 1048 | for i ulong 32 |
| 1049 | put recv_pk i get consent add 40 i |
| 1050 | end |
| 1051 | crc = ohtx_verify_consent consent now_secs recv_pk mask_box |
| 1052 | if ne crc OHTX_OK |
| 1053 | ret crc |
| 1054 | end |
| 1055 | my_pk = new uchar 32 0 |
| 1056 | ohtx_ed_pubkey seed my_pk |
| 1057 | for i ulong 32 |
| 1058 | if ne get my_pk i get consent add 8 i |
| 1059 | ret OHTX_ERR_BAD_SIG |
| 1060 | end |
| 1061 | end |
| 1062 | ret OHTX_OK |
| 1063 | end |
| 1064 | |
| 1065 | fn ohtx_pack_bundle seed view uchar 32 consent view uchar 160 out_seg view uchar 4112 seg_len ulong now_secs ulong dst edit uchar 4408 to void |
| 1066 | put dst 0 uchar 79 |
| 1067 | put dst 1 uchar 72 |
| 1068 | put dst 2 uchar 84 |
| 1069 | put dst 3 uchar 88 |
| 1070 | put dst 4 uchar 1 |
| 1071 | put dst 5 uchar 0 |
| 1072 | put dst 6 uchar 0 |
| 1073 | put dst 7 uchar 0 |
| 1074 | oh_put_u64_le dst 8 now_secs |
| 1075 | for i ulong 16 |
| 1076 | put dst add 16 i get ohtx_export_bid i |
| 1077 | end |
| 1078 | for i ulong 160 |
| 1079 | put dst add 32 i get consent i |
| 1080 | end |
| 1081 | dig = new uchar 32 0 |
| 1082 | ohtx_b3_hash part out_seg 0 seg_len dig |
| 1083 | for i ulong 32 |
| 1084 | put dst add 192 i get dig i |
| 1085 | end |
| 1086 | oh_put_u64_le dst 224 seg_len |
| 1087 | ohtx_sign_bundle_hdr seed dst |
| 1088 | for i ulong seg_len bound 4112 |
| 1089 | put dst add OHTX_HDR_LEN i get out_seg i |
| 1090 | end |
| 1091 | end |
| 1092 | |
| 1093 | note Export consented, firewall-clean AITR v1 records into signed OHTX v1 bundle dst. |
| 1094 | note Caller sets bundle_id via ohtx_set_bundle_id beforehand (keeps parameter count <= 6 slots). |
| 1095 | note Returns OHTX_OK (0) and sets ohtx_last_export_len, or returns nonzero refusal and writes 0 bytes. |
| 1096 | fn oh_trace_export seed view uchar 32 consent view uchar 160 in_seg view uchar * now_secs ulong dst edit uchar 4408 to ulong |
| 1097 | put ohtx_last_export_len 0 0 |
| 1098 | mask_box = new ulong 1 0 |
| 1099 | rc = ohtx_verify_consent_for_export seed consent now_secs mask_box |
| 1100 | if ne rc OHTX_OK |
| 1101 | ret rc |
| 1102 | end |
| 1103 | out_seg = new uchar 4112 0 |
| 1104 | seglen_box = new ulong 1 0 |
| 1105 | rc = ohtx_filter_aitr in_seg get mask_box 0 out_seg seglen_box |
| 1106 | if ne rc OHTX_OK |
| 1107 | ret rc |
| 1108 | end |
| 1109 | seg_len = get seglen_box 0 |
| 1110 | total_len = add OHTX_HDR_LEN seg_len |
| 1111 | if gt total_len 4408 |
| 1112 | ret OHTX_ERR_TOO_LARGE |
| 1113 | end |
| 1114 | ohtx_pack_bundle seed consent out_seg seg_len now_secs dst |
| 1115 | put ohtx_last_export_len 0 total_len |
| 1116 | ret OHTX_OK |
| 1117 | end |
| 1118 | |
| 1119 | note Replay check/record helper for 16-byte bundle_id. |
| 1120 | fn ohtx_check_replay bid view uchar 16 to bool |
| 1121 | cnt = get ohtx_seen_cnt 0 |
| 1122 | for k ulong cnt bound 64 |
| 1123 | base = mul k 16 |
| 1124 | same = true |
| 1125 | for j ulong 16 |
| 1126 | if ne get ohtx_seen_ids add base j get bid j |
| 1127 | same = false |
| 1128 | break |
| 1129 | end |
| 1130 | end |
| 1131 | if same |
| 1132 | ret false |
| 1133 | end |
| 1134 | end |
| 1135 | ret true |
| 1136 | end |
| 1137 | |
| 1138 | fn ohtx_record_replay bid view uchar 16 to void |
| 1139 | cnt = get ohtx_seen_cnt 0 |
| 1140 | idx = rem cnt OHTX_REPLAY_CAP |
| 1141 | base = mul idx 16 |
| 1142 | for j ulong 16 |
| 1143 | put ohtx_seen_ids add base j get bid j |
| 1144 | end |
| 1145 | if lt cnt OHTX_REPLAY_CAP |
| 1146 | put ohtx_seen_cnt 0 add cnt 1 |
| 1147 | end |
| 1148 | end |
| 1149 | |
| 1150 | note Helper: verify Ed25519 exporter signature on bundle[0..232) and BLAKE3 digest on segment. |
| 1151 | fn ohtx_verify_bundle_crypto bundle view uchar * seg_len ulong to ulong |
| 1152 | npk = new uchar 32 0 |
| 1153 | sig = new uchar 64 0 |
| 1154 | for i ulong 32 |
| 1155 | put npk i get bundle add 40 i |
| 1156 | end |
| 1157 | for i ulong 64 |
| 1158 | put sig i get bundle add 232 i |
| 1159 | end |
| 1160 | sbuf = new uchar 65536 0 |
| 1161 | dlen = len ohtx_dom_export |
| 1162 | for i ulong dlen bound 64 |
| 1163 | put sbuf i get ohtx_dom_export i |
| 1164 | end |
| 1165 | for i ulong 232 |
| 1166 | put sbuf add dlen i get bundle i |
| 1167 | end |
| 1168 | if ne edverify npk sbuf add dlen 232 sig 0 1 |
| 1169 | ret OHTX_ERR_BAD_SIG |
| 1170 | end |
| 1171 | dig = new uchar 32 0 |
| 1172 | ohtx_b3_hash part bundle OHTX_HDR_LEN seg_len dig |
| 1173 | for i ulong 32 |
| 1174 | if ne get dig i get bundle add 192 i |
| 1175 | ret OHTX_ERR_BAD_DIGEST |
| 1176 | end |
| 1177 | end |
| 1178 | ret OHTX_OK |
| 1179 | end |
| 1180 | |
| 1181 | fn ohtx_check_incoming_one seg view uchar * idx ulong tmask ulong to ulong |
| 1182 | rt_box = new ulong 1 0 |
| 1183 | pl_box = new ulong 1 0 |
| 1184 | rrc = ohtx_aitr_validate_rec seg idx rt_box pl_box |
| 1185 | if ne rrc OHTX_OK |
| 1186 | ret rrc |
| 1187 | end |
| 1188 | if not ohtx_type_allowed tmask get rt_box 0 |
| 1189 | ret OHTX_ERR_TYPE_NOT_CONSENTED |
| 1190 | end |
| 1191 | poff = add add OHTX_AITR_HDR mul idx OHTX_AITR_REC 8 |
| 1192 | ret ohtx_check_payload_clean part seg poff get pl_box 0 |
| 1193 | end |
| 1194 | |
| 1195 | note Helper: validate incoming AITR v1 segment against consented mask and Personal Data Firewall. |
| 1196 | fn ohtx_validate_incoming_seg seg view uchar * tmask ulong to ulong |
| 1197 | rcnt_box = new ulong 1 0 |
| 1198 | hrc = ohtx_aitr_validate_hdr seg rcnt_box |
| 1199 | if ne hrc OHTX_OK |
| 1200 | ret hrc |
| 1201 | end |
| 1202 | rcnt = get rcnt_box 0 |
| 1203 | if gt rcnt 64 |
| 1204 | ret OHTX_ERR_TOO_LARGE |
| 1205 | end |
| 1206 | for i ulong rcnt bound 64 |
| 1207 | rrc = ohtx_check_incoming_one seg i tmask |
| 1208 | if ne rrc OHTX_OK |
| 1209 | ret rrc |
| 1210 | end |
| 1211 | end |
| 1212 | ret OHTX_OK |
| 1213 | end |
| 1214 | |
| 1215 | fn ohtx_check_bundle_hdr bundle view uchar * now_secs ulong bid edit uchar 16 to ulong |
| 1216 | blen = len bundle |
| 1217 | if lt blen add OHTX_HDR_LEN OHTX_AITR_HDR |
| 1218 | ret OHTX_ERR_TRUNCATED |
| 1219 | end |
| 1220 | if ne get bundle 0 uchar 79 |
| 1221 | ret OHTX_ERR_BAD_MAGIC |
| 1222 | end |
| 1223 | if ne get bundle 1 uchar 72 |
| 1224 | ret OHTX_ERR_BAD_MAGIC |
| 1225 | end |
| 1226 | if ne get bundle 2 uchar 84 |
| 1227 | ret OHTX_ERR_BAD_MAGIC |
| 1228 | end |
| 1229 | if ne get bundle 3 uchar 88 |
| 1230 | ret OHTX_ERR_BAD_MAGIC |
| 1231 | end |
| 1232 | if ne get bundle 4 uchar 1 |
| 1233 | ret OHTX_ERR_BAD_VERSION |
| 1234 | end |
| 1235 | if ne get bundle 5 uchar 0 |
| 1236 | ret OHTX_ERR_BAD_FIELD |
| 1237 | end |
| 1238 | if ne get bundle 6 uchar 0 |
| 1239 | ret OHTX_ERR_BAD_FIELD |
| 1240 | end |
| 1241 | if ne get bundle 7 uchar 0 |
| 1242 | ret OHTX_ERR_BAD_FIELD |
| 1243 | end |
| 1244 | ts = oh_get_u64_le bundle 8 |
| 1245 | diff = ulong 0 |
| 1246 | if ge now_secs ts |
| 1247 | diff = sub now_secs ts |
| 1248 | else |
| 1249 | diff = sub ts now_secs |
| 1250 | end |
| 1251 | if gt diff OHW_MAX_DRIFT_SECS |
| 1252 | ret OHTX_ERR_DRIFT |
| 1253 | end |
| 1254 | for i ulong 16 |
| 1255 | put bid i get bundle add 16 i |
| 1256 | end |
| 1257 | if not ohtx_check_replay bid |
| 1258 | ret OHTX_ERR_REPLAY |
| 1259 | end |
| 1260 | ret OHTX_OK |
| 1261 | end |
| 1262 | |
| 1263 | note ---- Exact-byte Trace Receiver (OHTX v1 -> verified AITR v1 segment, fail-closed) ---- |
| 1264 | |
| 1265 | fn oh_trace_receive bundle view uchar * recv_pk view uchar 32 now_secs ulong dst_seg edit uchar * to ulong |
| 1266 | put ohtx_last_recv_len 0 0 |
| 1267 | bid = new uchar 16 0 |
| 1268 | rc = ohtx_check_bundle_hdr bundle now_secs bid |
| 1269 | if ne rc OHTX_OK |
| 1270 | ret rc |
| 1271 | end |
| 1272 | mask_box = new ulong 1 0 |
| 1273 | rc = ohtx_verify_consent part bundle 32 OHTX_CONSENT_LEN now_secs recv_pk mask_box |
| 1274 | if ne rc OHTX_OK |
| 1275 | ret rc |
| 1276 | end |
| 1277 | seg_len = oh_get_u64_le bundle 224 |
| 1278 | if ne add OHTX_HDR_LEN seg_len len bundle |
| 1279 | ret OHTX_ERR_TRUNCATED |
| 1280 | end |
| 1281 | if gt seg_len len dst_seg |
| 1282 | ret OHTX_ERR_TOO_LARGE |
| 1283 | end |
| 1284 | rc = ohtx_verify_bundle_crypto bundle seg_len |
| 1285 | if ne rc OHTX_OK |
| 1286 | ret rc |
| 1287 | end |
| 1288 | rc = ohtx_validate_incoming_seg part bundle OHTX_HDR_LEN seg_len get mask_box 0 |
| 1289 | if ne rc OHTX_OK |
| 1290 | ret rc |
| 1291 | end |
| 1292 | ohtx_record_replay bid |
| 1293 | for i ulong seg_len bound 4112 |
| 1294 | put dst_seg i get bundle add OHTX_HDR_LEN i |
| 1295 | end |
| 1296 | put ohtx_last_recv_len 0 seg_len |
| 1297 | ret OHTX_OK |
| 1298 | end |