interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / searchtrace / st_holdout.om

1940 lines · 41755 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note src/searchtrace/st_holdout.om: Pure seeded OM-2 G3 sealed-holdout commitment
4note format v1 (commit, parse, reveal, receipt, Ed25519 sign, strict verify, CLI).
5note Compose after:
6note src/searchtrace/st_om2_hdr.om
7note seed/ng/sha256.om
8note protocol/omnet/om/sha512/sha512.om
9note protocol/omnet/om/ed25519/ed25519.om
10note protocol/uor-gpu/ed_sign.om
11
12const STH_OK long 0
13const STH_EARG long -1
14const STH_ELABEL long -2
15const STH_ENOTASKS long -3
16const STH_ECAP long -4
17const STH_EFORMAT long -5
18const STH_EDIGEST long -6
19const STH_ESALT long -7
20const STH_ELEN long -8
21const STH_ECOUNT long -9
22const STH_ECOMMIT long -10
23const STH_ECOMMITID long -11
24const STH_EUNSIGNED long -12
25const STH_EBIND long -13
26const STH_EKEY long -14
27const STH_ESIG long -15
28
29const STH_REC_MAX ulong 1024
30
31text sth_t_magic "OMEGA-G3-HOLDOUT-COMMIT v1"
32text sth_t_rmagic "OMEGA-G3-HOLDOUT-REVEAL v1"
33text sth_t_smagic "OMEGA-G3-HOLDOUT-SIG v1"
34text sth_t_dom "omega.g3.holdout.commit.v1"
35text sth_t_sdom "omega.g3.holdout.salt.v1"
36text sth_t_sigdom "omega.g3.holdout.sig.v1"
37
38text sth_k_domain "domain"
39text sth_k_hid "holdout_id"
40text sth_k_tcnt "task_count"
41text sth_k_tlen "taskset_len"
42text sth_k_sdig "salt_digest"
43text sth_k_comm "commitment"
44text sth_k_rdig "record_digest"
45text sth_k_kid "key_id"
46text sth_k_sig "signature"
47text sth_k_rcom "repo_commit"
48text sth_k_verd "verdict PASS\n"
49text sth_k_end "end"
50text sth_t_endsp "end "
51text sth_t_nl "\n"
52text sth_t_sp " "
53text sth_t_slash "/"
54text sth_t_dot "."
55text sth_t_p_commit "/g3-commit-"
56text sth_t_p_reveal "/g3-reveal-"
57text sth_t_p_sig "/g3-sig-"
58text sth_t_txt ".txt"
59text sth_t_tmp ".tmp."
60text sth_t_envk "OMEGA_REPO_COMMIT="
61
62text sth_t_cmd_commit "commit"
63text sth_t_cmd_reveal "reveal"
64text sth_t_cmd_verify "verify"
65text sth_t_cmd_sign "sign"
66text sth_t_opt_strict "--strict"
67
68text sth_m_ok "OK holdout_id "
69text sth_m_ok_tc " task_count "
70text sth_m_ok_rd " record_digest "
71text sth_m_oks "OK SIGNED holdout_id "
72text sth_m_oks_kid " key_id "
73
74text sth_w_null "null argument"
75text sth_w_long "record too long"
76text sth_w_nul "NUL byte in record"
77text sth_w_cr "CR character in record (LF only)"
78text sth_w_magic "wrong magic or version"
79text sth_w_dom "wrong domain"
80text sth_w_label "bad holdout_id label"
81text sth_w_ztask "zero tasks refused"
82text sth_w_tsmall "taskset_len too small for task_count"
83text sth_w_trail "trailing bytes after end line"
84text sth_w_edig "end digest mismatch"
85text sth_w_trunc "truncated record (missing field or end line)"
86text sth_w_nolf "truncated record (line without LF)"
87text sth_w_expf "expected field '"
88text sth_w_expf2 "' (missing/extra/reordered)"
89text sth_w_hex64 "hex field is not 64 characters"
90text sth_w_hexup "uppercase hex refused"
91text sth_w_hexbad "bad hex character"
92text sth_w_dec1 "bad decimal (empty, too long or leading zero)"
93text sth_w_dec2 "bad decimal digit"
94text sth_w_dec3 "decimal out of range"
95text sth_w_esalt "salt does not match salt_digest"
96text sth_w_elen "taskset_len mismatch"
97text sth_w_ecnt "task_count mismatch"
98text sth_w_ecomm "commitment mismatch"
99text sth_w_sig128 "signature is not 128 hex characters"
100text sth_w_unsig "unsigned record (strict mode needs a signature)"
101text sth_w_slong "signature record too long"
102text sth_w_snulcr "NUL or CR byte in signature record"
103text sth_w_smagic "signature record: wrong magic or version"
104text sth_w_sdom "signature record: wrong domain"
105text sth_w_slabel "signature record: bad holdout_id label"
106text sth_w_strail "signature record: trailing bytes after end line"
107text sth_w_sedig "signature record: end digest mismatch"
108text sth_w_ebind "signature names a different commitment record"
109text sth_w_ekey "signed by a different key (key_id mismatch)"
110text sth_w_esig "Ed25519 signature does not verify"
111text sth_w_sself "signature self-check failed"
112text sth_w_cap "output buffer too small"
113text sth_w_usage "usage: commit <holdout_id> <taskset_file> <salt_file> <out_dir>\n reveal <record_file> <taskset_file> <salt_file> <out_dir>\n verify <record_file>\n verify --strict <public_key_file> <record_file> [<sig_file>]\n sign <record_file> <secret_key_file> <out_dir>\n"
114text sth_w_errpfx "st_holdout: "
115
116note Parsed commitment state (globals avoid 6-slot parameter overflow)
117global sth_c_id uchar 65
118global sth_c_idlen ulong 1
119global sth_c_tcnt ulong 1
120global sth_c_tlen ulong 1
121global sth_c_sd uchar 32
122global sth_c_cm uchar 32
123global sth_c_ed uchar 32
124global sth_c_rd uchar 32
125
126note Last refusal message
127global sth_why uchar 128
128global sth_whylen ulong 1
129
130note Scratch buffers for commit/parse/sign/verify/CLI
131global sth_rec uchar 2048
132global sth_reclen ulong 1
133global sth_sig uchar 2048
134global sth_siglen ulong 1
135global sth_rcpt uchar 2048
136global sth_rcptlen ulong 1
137global sth_fbuf uchar 1048576
138global sth_flen ulong 1
139global sth_fbuf2 uchar 1048576
140global sth_flen2 ulong 1
141
142note Cursor state for parsing
143global sth_cur_pos ulong 1
144global sth_val_off ulong 1
145global sth_val_len ulong 1
146
147note CLI argv storage (up to 8 args, 512 bytes each)
148global sth_argc ulong 1
149global sth_argv uchar 4096
150global sth_argl ulong 8
151
152note Optional env override for testing OMEGA_REPO_COMMIT
153note mode: 0 = read process env, 1 = unset, 2 = use sth_env_val
154global sth_env_mode ulong 1
155global sth_env_val uchar 128
156global sth_env_len ulong 1
157
158note PKCS#8 and SPKI DER prefixes for Ed25519
159global sth_pkcs8 uchar 16
160global sth_spki uchar 12
161global sth_der_init ulong 1
162
163fn sth_init_der to void
164 if ne get sth_der_init 0 0
165 ret
166 end
167 put sth_der_init 0 1
168 put sth_pkcs8 0 0x30
169 put sth_pkcs8 1 0x2e
170 put sth_pkcs8 2 0x02
171 put sth_pkcs8 3 0x01
172 put sth_pkcs8 4 0x00
173 put sth_pkcs8 5 0x30
174 put sth_pkcs8 6 0x05
175 put sth_pkcs8 7 0x06
176 put sth_pkcs8 8 0x03
177 put sth_pkcs8 9 0x2b
178 put sth_pkcs8 10 0x65
179 put sth_pkcs8 11 0x70
180 put sth_pkcs8 12 0x04
181 put sth_pkcs8 13 0x22
182 put sth_pkcs8 14 0x04
183 put sth_pkcs8 15 0x20
184 put sth_spki 0 0x30
185 put sth_spki 1 0x2a
186 put sth_spki 2 0x30
187 put sth_spki 3 0x05
188 put sth_spki 4 0x06
189 put sth_spki 5 0x03
190 put sth_spki 6 0x2b
191 put sth_spki 7 0x65
192 put sth_spki 8 0x70
193 put sth_spki 9 0x03
194 put sth_spki 10 0x21
195 put sth_spki 11 0x00
196end
197
198fn sth_set_why msg view uchar * to void
199 n = len msg
200 if gt n 127
201 n = 127
202 end
203 for i ulong n bound 128
204 put sth_why i get msg i
205 end
206 put sth_whylen 0 n
207end
208
209fn sth_set_why_exp key view uchar * to void
210 p = ulong 0
211 n1 = len sth_w_expf
212 for i ulong n1 bound 32
213 put sth_why p get sth_w_expf i
214 p = add p 1
215 end
216 nk = len key
217 for i ulong nk bound 32
218 if lt p 127
219 put sth_why p get key i
220 p = add p 1
221 end
222 end
223 n2 = len sth_w_expf2
224 for i ulong n2 bound 64
225 if lt p 127
226 put sth_why p get sth_w_expf2 i
227 p = add p 1
228 end
229 end
230 put sth_whylen 0 p
231end
232
233fn sth_wr fd int s view uchar * to void
234 r = write fd s 0 len s
235end
236
237fn sth_wr_why fd int to void
238 n = get sth_whylen 0
239 if gt n 0
240 r = write fd sth_why 0 n
241 end
242end
243
244fn sth_hex_nib v ulong to uchar
245 if lt v 10
246 ret uchar add 48 v
247 end
248 ret uchar add 87 v
249end
250
251fn sth_ct_eq32 a view uchar 32 b view uchar 32 to bool
252 acc = ulong 0
253 for i ulong 32
254 acc = or acc xor ulong get a i ulong get b i
255 end
256 ret eq acc 0
257end
258
259fn sth_label_ok s view uchar * n ulong to bool
260 if or lt n 1 gt n 64
261 ret false
262 end
263 for i ulong n bound 65
264 c = ulong get s i
265 ok = ulong 0
266 if and ge c 97 le c 122
267 ok = 1
268 end
269 if and ge c 65 le c 90
270 ok = 1
271 end
272 if and ge c 48 le c 57
273 ok = 1
274 end
275 if or eq c 46 or eq c 95 eq c 45
276 ok = 1
277 end
278 if eq ok 0
279 ret false
280 end
281 end
282 ret true
283end
284
285fn sth_is_task_at buf view uchar * slen ulong pos ulong to bool
286 if lt sub slen pos 5
287 ret false
288 end
289 if ne ulong get buf pos 116
290 ret false
291 end
292 if ne ulong get buf add pos 1 97
293 ret false
294 end
295 if ne ulong get buf add pos 2 115
296 ret false
297 end
298 if ne ulong get buf add pos 3 107
299 ret false
300 end
301 if ne ulong get buf add pos 4 32
302 ret false
303 end
304 ret true
305end
306
307fn sth_count_tasks buf view uchar * slen ulong to ulong
308 n = ulong 0
309 i = ulong 0
310 while lt i slen bound 9223372036854775807
311 if sth_is_task_at buf slen i
312 if lt n 4294967295
313 n = add n 1
314 end
315 end
316 while lt i slen bound 9223372036854775807
317 c = ulong get buf i
318 i = add i 1
319 if eq c 10
320 break
321 end
322 end
323 end
324 ret n
325end
326
327fn sth_sha256_copy dst edit uchar 32 to void
328 for i ulong 32
329 put dst i get sh_dig i
330 end
331end
332
333fn sth_sha256_buf buf view uchar * n ulong dst edit uchar 32 to void
334 for i ulong n bound 9223372036854775807
335 put sh_dat i get buf i
336 end
337 sh_all n
338 sth_sha256_copy dst
339end
340
341fn sth_salt_digest salt view uchar 32 dst edit uchar 32 to void
342 dl = len sth_t_sdom
343 for i ulong dl bound 64
344 put sh_dat i get sth_t_sdom i
345 end
346 put sh_dat dl 0
347 p = add dl 1
348 for i ulong 32
349 put sh_dat add p i get salt i
350 end
351 sh_all add p 32
352 sth_sha256_copy dst
353end
354
355fn sth_commitment salt view uchar 32 ts view uchar * tlen ulong cnt ulong to void
356 p = len sth_t_dom
357 for i ulong p bound 64
358 put sh_dat i get sth_t_dom i
359 end
360 put sh_dat p 0
361 p = add p 1
362 for i ulong 32
363 put sh_dat add p i get salt i
364 end
365 p = add p 32
366 for i ulong 8
367 sh = sub 56 mul i 8
368 bv = and shr tlen sh 255
369 put sh_dat add p i uchar bv
370 end
371 p = add p 8
372 for i ulong 4
373 sh = sub 24 mul i 8
374 bv = and shr cnt sh 255
375 put sh_dat add p i uchar bv
376 end
377 p = add p 4
378 for i ulong tlen bound 9223372036854775807
379 put sh_dat add p i get ts i
380 end
381 sh_all add p tlen
382end
383
384note Append helpers into sth_rec at pos
385fn sth_rec_put_txt pos ulong s view uchar * to ulong
386 n = len s
387 for i ulong n bound 256
388 put sth_rec add pos i get s i
389 end
390 ret add pos n
391end
392
393fn sth_rec_put_slice pos ulong s view uchar * n ulong to ulong
394 for i ulong n bound 1024
395 put sth_rec add pos i get s i
396 end
397 ret add pos n
398end
399
400fn sth_rec_put_u64 pos ulong v ulong to ulong
401 tmp = new uchar 24 0
402 k = ulong 24
403 x = v
404 while true bound 24
405 k = sub k 1
406 put tmp k uchar add 48 rem x 10
407 x = div x 10
408 if eq x 0
409 break
410 end
411 end
412 n = sub 24 k
413 for i ulong n bound 24
414 put sth_rec add pos i get tmp add k i
415 end
416 ret add pos n
417end
418
419fn sth_rec_put_hex pos ulong d view uchar * n ulong to ulong
420 for i ulong n bound 64
421 b = ulong get d i
422 put sth_rec add pos mul i 2 sth_hex_nib shr b 4
423 put sth_rec add pos add mul i 2 1 sth_hex_nib and b 15
424 end
425 ret add pos mul n 2
426end
427
428fn sth_rec_seal pos ulong cap ulong to long
429 for i ulong pos bound 1024
430 put sh_dat i get sth_rec i
431 end
432 sh_all pos
433 ed = new uchar 32 0
434 sth_sha256_copy ed
435 tot = add pos 69
436 if lt cap tot
437 sth_set_why sth_w_cap
438 ret STH_ECAP
439 end
440 p = sth_rec_put_txt pos sth_t_endsp
441 p = sth_rec_put_hex p ed 32
442 put sth_rec p 10
443 put sth_reclen 0 add p 1
444 ret STH_OK
445end
446
447note Build canonical commitment record into sth_rec[0..sth_reclen).
448fn sth_commit hid view uchar * ts view uchar * salt view uchar 32 cap ulong to long
449 hlen = len hid
450 if not sth_label_ok hid hlen
451 ret STH_ELABEL
452 end
453 tlen = len ts
454 cnt = sth_count_tasks ts tlen
455 if eq cnt 0
456 ret STH_ENOTASKS
457 end
458 sd = new uchar 32 0
459 cm = new uchar 32 0
460 sth_salt_digest salt sd
461 sth_commitment salt ts tlen cnt
462 sth_sha256_copy cm
463 p = ulong 0
464 p = sth_rec_put_txt p sth_t_magic
465 put sth_rec p 10
466 p = add p 1
467 p = sth_rec_put_txt p sth_k_domain
468 put sth_rec p 32
469 p = add p 1
470 p = sth_rec_put_txt p sth_t_dom
471 put sth_rec p 10
472 p = add p 1
473 p = sth_rec_put_txt p sth_k_hid
474 put sth_rec p 32
475 p = add p 1
476 p = sth_rec_put_slice p hid hlen
477 put sth_rec p 10
478 p = add p 1
479 p = sth_rec_put_txt p sth_k_tcnt
480 put sth_rec p 32
481 p = add p 1
482 p = sth_rec_put_u64 p cnt
483 put sth_rec p 10
484 p = add p 1
485 p = sth_rec_put_txt p sth_k_tlen
486 put sth_rec p 32
487 p = add p 1
488 p = sth_rec_put_u64 p tlen
489 put sth_rec p 10
490 p = add p 1
491 p = sth_rec_put_txt p sth_k_sdig
492 put sth_rec p 32
493 p = add p 1
494 p = sth_rec_put_hex p sd 32
495 put sth_rec p 10
496 p = add p 1
497 p = sth_rec_put_txt p sth_k_comm
498 put sth_rec p 32
499 p = add p 1
500 p = sth_rec_put_hex p cm 32
501 put sth_rec p 10
502 p = add p 1
503 ret sth_rec_seal p cap
504end
505
506note ---- strict parser helpers ----
507
508fn sth_check_rec_bytes rec view uchar * rlen ulong is_sig ulong to long
509 if gt rlen STH_REC_MAX
510 if ne is_sig 0
511 sth_set_why sth_w_slong
512 else
513 sth_set_why sth_w_long
514 end
515 ret STH_EFORMAT
516 end
517 for i ulong rlen bound 1025
518 b = ulong get rec i
519 if eq b 0
520 if ne is_sig 0
521 sth_set_why sth_w_snulcr
522 else
523 sth_set_why sth_w_nul
524 end
525 ret STH_EFORMAT
526 end
527 if eq b 13
528 if ne is_sig 0
529 sth_set_why sth_w_snulcr
530 else
531 sth_set_why sth_w_cr
532 end
533 ret STH_EFORMAT
534 end
535 end
536 ret STH_OK
537end
538
539fn sth_slice_eq a view uchar * off ulong alen ulong b view uchar * to bool
540 if ne alen len b
541 ret false
542 end
543 for i ulong alen bound 1024
544 if ne get a add off i get b i
545 ret false
546 end
547 end
548 ret true
549end
550
551fn sth_next_field rec view uchar * rlen ulong key view uchar * to long
552 pos = get sth_cur_pos 0
553 if ge pos rlen
554 sth_set_why sth_w_trunc
555 ret STH_EFORMAT
556 end
557 nl = pos
558 found = ulong 0
559 while lt nl rlen bound 1025
560 if eq ulong get rec nl 10
561 found = 1
562 break
563 end
564 nl = add nl 1
565 end
566 if eq found 0
567 sth_set_why sth_w_nolf
568 ret STH_EFORMAT
569 end
570 ll = sub nl pos
571 kl = len key
572 if lt ll add kl 1
573 sth_set_why_exp key
574 ret STH_EFORMAT
575 end
576 for i ulong kl bound 64
577 if ne get rec add pos i get key i
578 sth_set_why_exp key
579 ret STH_EFORMAT
580 end
581 end
582 if ne ulong get rec add pos kl 32
583 sth_set_why_exp key
584 ret STH_EFORMAT
585 end
586 put sth_val_off 0 add pos add kl 1
587 put sth_val_len 0 sub sub ll kl 1
588 put sth_cur_pos 0 add nl 1
589 ret STH_OK
590end
591
592fn sth_parse_hex32 rec view uchar * off ulong n ulong dst edit uchar 32 to long
593 if ne n 64
594 sth_set_why sth_w_hex64
595 ret STH_EFORMAT
596 end
597 for i ulong 64
598 ch = ulong get rec add off i
599 x = ulong 99
600 if and ge ch 48 le ch 57
601 x = sub ch 48
602 else
603 if and ge ch 97 le ch 102
604 x = add sub ch 97 10
605 else
606 if and ge ch 65 le ch 70
607 sth_set_why sth_w_hexup
608 ret STH_EFORMAT
609 end
610 sth_set_why sth_w_hexbad
611 ret STH_EFORMAT
612 end
613 end
614 idx = div i 2
615 if eq and i 1 1
616 put dst idx uchar or ulong get dst idx x
617 else
618 put dst idx uchar shl x 4
619 end
620 end
621 ret STH_OK
622end
623
624fn sth_parse_hex64 rec view uchar * off ulong n ulong dst edit uchar 64 to long
625 if ne n 128
626 sth_set_why sth_w_sig128
627 ret STH_EFORMAT
628 end
629 h1 = new uchar 32 0
630 h2 = new uchar 32 0
631 r1 = sth_parse_hex32 rec off 64 h1
632 if ne r1 0
633 ret r1
634 end
635 r2 = sth_parse_hex32 rec add off 64 64 h2
636 if ne r2 0
637 ret r2
638 end
639 for i ulong 32
640 put dst i get h1 i
641 put dst add i 32 get h2 i
642 end
643 ret STH_OK
644end
645
646fn sth_parse_dec rec view uchar * off ulong n ulong maxv ulong dst edit ulong 1 to long
647 if or eq n 0 gt n 20
648 sth_set_why sth_w_dec1
649 ret STH_EFORMAT
650 end
651 if gt n 1
652 if eq ulong get rec off 48
653 sth_set_why sth_w_dec1
654 ret STH_EFORMAT
655 end
656 end
657 x = ulong 0
658 for i ulong n bound 24
659 c = ulong get rec add off i
660 if or lt c 48 gt c 57
661 sth_set_why sth_w_dec2
662 ret STH_EFORMAT
663 end
664 d = sub c 48
665 if gt x div sub maxv d 10
666 sth_set_why sth_w_dec3
667 ret STH_EFORMAT
668 end
669 x = add mul x 10 d
670 end
671 put dst 0 x
672 ret STH_OK
673end
674
675fn sth_parse_hdr rec view uchar * rlen ulong to long
676 ml = len sth_t_magic
677 if lt rlen add ml 1
678 sth_set_why sth_w_magic
679 ret STH_EFORMAT
680 end
681 if not sth_slice_eq rec 0 ml sth_t_magic
682 sth_set_why sth_w_magic
683 ret STH_EFORMAT
684 end
685 if ne ulong get rec ml 10
686 sth_set_why sth_w_magic
687 ret STH_EFORMAT
688 end
689 put sth_cur_pos 0 add ml 1
690 r = sth_next_field rec rlen sth_k_domain
691 if ne r 0
692 ret r
693 end
694 if not sth_slice_eq rec get sth_val_off 0 get sth_val_len 0 sth_t_dom
695 sth_set_why sth_w_dom
696 ret STH_EFORMAT
697 end
698 r = sth_next_field rec rlen sth_k_hid
699 if ne r 0
700 ret r
701 end
702 vo = get sth_val_off 0
703 vl = get sth_val_len 0
704 tmp = new uchar 65 0
705 if or lt vl 1 gt vl 64
706 sth_set_why sth_w_label
707 ret STH_EFORMAT
708 end
709 for i ulong vl bound 65
710 put tmp i get rec add vo i
711 end
712 if not sth_label_ok tmp vl
713 sth_set_why sth_w_label
714 ret STH_EFORMAT
715 end
716 for i ulong vl bound 65
717 put sth_c_id i get tmp i
718 end
719 put sth_c_idlen 0 vl
720 ret STH_OK
721end
722
723fn sth_parse_nums rec view uchar * rlen ulong to long
724 num = new ulong 1 0
725 r = sth_next_field rec rlen sth_k_tcnt
726 if ne r 0
727 ret r
728 end
729 r = sth_parse_dec rec get sth_val_off 0 get sth_val_len 0 4294967295 num
730 if ne r 0
731 ret r
732 end
733 tc = get num 0
734 if eq tc 0
735 sth_set_why sth_w_ztask
736 ret STH_EFORMAT
737 end
738 put sth_c_tcnt 0 tc
739 r = sth_next_field rec rlen sth_k_tlen
740 if ne r 0
741 ret r
742 end
743 r = sth_parse_dec rec get sth_val_off 0 get sth_val_len 0 18446744073709551615 num
744 if ne r 0
745 ret r
746 end
747 tl = get num 0
748 if lt div tl 5 tc
749 sth_set_why sth_w_tsmall
750 ret STH_EFORMAT
751 end
752 put sth_c_tlen 0 tl
753 ret STH_OK
754end
755
756fn sth_parse rec view uchar * rlen ulong to long
757 r = sth_check_rec_bytes rec rlen 0
758 if ne r 0
759 ret r
760 end
761 r = sth_parse_hdr rec rlen
762 if ne r 0
763 ret r
764 end
765 r = sth_parse_nums rec rlen
766 if ne r 0
767 ret r
768 end
769 sd = new uchar 32 0
770 cm = new uchar 32 0
771 ed = new uchar 32 0
772 r = sth_next_field rec rlen sth_k_sdig
773 if ne r 0
774 ret r
775 end
776 r = sth_parse_hex32 rec get sth_val_off 0 get sth_val_len 0 sd
777 if ne r 0
778 ret r
779 end
780 r = sth_next_field rec rlen sth_k_comm
781 if ne r 0
782 ret r
783 end
784 r = sth_parse_hex32 rec get sth_val_off 0 get sth_val_len 0 cm
785 if ne r 0
786 ret r
787 end
788 blen = get sth_cur_pos 0
789 r = sth_next_field rec rlen sth_k_end
790 if ne r 0
791 ret r
792 end
793 r = sth_parse_hex32 rec get sth_val_off 0 get sth_val_len 0 ed
794 if ne r 0
795 ret r
796 end
797 if ne get sth_cur_pos 0 rlen
798 sth_set_why sth_w_trail
799 ret STH_EFORMAT
800 end
801 d = new uchar 32 0
802 sth_sha256_buf rec blen d
803 if not sth_ct_eq32 d ed
804 sth_set_why sth_w_edig
805 ret STH_EDIGEST
806 end
807 rd = new uchar 32 0
808 sth_sha256_buf rec rlen rd
809 for i ulong 32
810 put sth_c_sd i get sd i
811 put sth_c_cm i get cm i
812 put sth_c_ed i get ed i
813 put sth_c_rd i get rd i
814 end
815 ret STH_OK
816end
817
818fn sth_reveal ts view uchar * tlen ulong salt view uchar 32 to long
819 d = new uchar 32 0
820 want = new uchar 32 0
821 sth_salt_digest salt d
822 for i ulong 32
823 put want i get sth_c_sd i
824 end
825 if not sth_ct_eq32 d want
826 sth_set_why sth_w_esalt
827 ret STH_ESALT
828 end
829 if ne tlen get sth_c_tlen 0
830 sth_set_why sth_w_elen
831 ret STH_ELEN
832 end
833 cnt = sth_count_tasks ts tlen
834 if ne cnt get sth_c_tcnt 0
835 sth_set_why sth_w_ecnt
836 ret STH_ECOUNT
837 end
838 sth_commitment salt ts tlen cnt
839 sth_sha256_copy d
840 for i ulong 32
841 put want i get sth_c_cm i
842 end
843 if not sth_ct_eq32 d want
844 sth_set_why sth_w_ecomm
845 ret STH_ECOMMIT
846 end
847 ret STH_OK
848end
849
850fn sth_commit40_ok s view uchar * n ulong to bool
851 if ne n 40
852 ret false
853 end
854 for i ulong 40
855 ch = ulong get s i
856 ok = ulong 0
857 if and ge ch 48 le ch 57
858 ok = 1
859 end
860 if and ge ch 97 le ch 102
861 ok = 1
862 end
863 if eq ok 0
864 ret false
865 end
866 end
867 ret true
868end
869
870fn sth_reveal_receipt rcom view uchar * rlen ulong cap ulong to long
871 if not sth_commit40_ok rcom rlen
872 ret STH_ECOMMITID
873 end
874 idlen = get sth_c_idlen 0
875 idb = new uchar 64 0
876 if or lt idlen 1 gt idlen 64
877 ret STH_ELABEL
878 end
879 for i ulong idlen bound 64
880 put idb i get sth_c_id i
881 end
882 if not sth_label_ok idb idlen
883 ret STH_ELABEL
884 end
885 cm = new uchar 32 0
886 rd = new uchar 32 0
887 for i ulong 32
888 put cm i get sth_c_cm i
889 put rd i get sth_c_rd i
890 end
891 p = ulong 0
892 p = sth_rec_put_txt p sth_t_rmagic
893 put sth_rec p 10
894 p = add p 1
895 p = sth_rec_put_txt p sth_k_domain
896 put sth_rec p 32
897 p = add p 1
898 p = sth_rec_put_txt p sth_t_dom
899 put sth_rec p 10
900 p = add p 1
901 p = sth_rec_put_txt p sth_k_hid
902 put sth_rec p 32
903 p = add p 1
904 p = sth_rec_put_slice p idb idlen
905 put sth_rec p 10
906 p = add p 1
907 p = sth_rec_put_txt p sth_k_comm
908 put sth_rec p 32
909 p = add p 1
910 p = sth_rec_put_hex p cm 32
911 put sth_rec p 10
912 p = add p 1
913 p = sth_rec_put_txt p sth_k_rdig
914 put sth_rec p 32
915 p = add p 1
916 p = sth_rec_put_hex p rd 32
917 put sth_rec p 10
918 p = add p 1
919 p = sth_rec_put_txt p sth_k_tcnt
920 put sth_rec p 32
921 p = add p 1
922 p = sth_rec_put_u64 p get sth_c_tcnt 0
923 put sth_rec p 10
924 p = add p 1
925 p = sth_rec_put_txt p sth_k_tlen
926 put sth_rec p 32
927 p = add p 1
928 p = sth_rec_put_u64 p get sth_c_tlen 0
929 put sth_rec p 10
930 p = add p 1
931 p = sth_rec_put_txt p sth_k_rcom
932 put sth_rec p 32
933 p = add p 1
934 p = sth_rec_put_slice p rcom 40
935 put sth_rec p 10
936 p = add p 1
937 p = sth_rec_put_txt p sth_k_verd
938 r = sth_rec_seal p cap
939 if ne r 0
940 ret r
941 end
942 tot = get sth_reclen 0
943 for i ulong tot bound 1024
944 put sth_rcpt i get sth_rec i
945 end
946 put sth_rcptlen 0 tot
947 ret STH_OK
948end
949
950note ---- Ed25519 strict verify (rejects small-order A and R, matching aienos_sig) ----
951
952fn sth_pt_small_order b view uchar 32 to bool
953 w = new ulong 1024 0
954 edinit w
955 if eq edpdec w edq b 0
956 ret true
957 end
958 edpdbl w edq
959 edpdbl w edq
960 edpdbl w edq
961 ret eq edpid w edq 1
962end
963
964fn sth_edverify_strict pk view uchar 32 msg view uchar 65536 mlen ulong sg view uchar 64 to bool
965 if sth_pt_small_order pk
966 ret false
967 end
968 rb = new uchar 32 0
969 for i ulong 32
970 put rb i get sg i
971 end
972 if sth_pt_small_order rb
973 ret false
974 end
975 ret eq edverify pk msg mlen sg 0 1
976end
977
978fn sth_build_sig_msg rec view uchar * rlen ulong msg edit uchar 65536 to ulong
979 dl = len sth_t_sigdom
980 for i ulong dl bound 64
981 put msg i get sth_t_sigdom i
982 end
983 put msg dl 0
984 p = add dl 1
985 for i ulong rlen bound 1025
986 put msg add p i get rec i
987 end
988 ret add p rlen
989end
990
991fn sth_emit_sig_rec kid view uchar 32 sg view uchar 64 cap ulong to long
992 idlen = get sth_c_idlen 0
993 idb = new uchar 64 0
994 rd = new uchar 32 0
995 for i ulong idlen bound 64
996 put idb i get sth_c_id i
997 end
998 for i ulong 32
999 put rd i get sth_c_rd i
1000 end
1001 p = ulong 0
1002 p = sth_rec_put_txt p sth_t_smagic
1003 put sth_rec p 10
1004 p = add p 1
1005 p = sth_rec_put_txt p sth_k_domain
1006 put sth_rec p 32
1007 p = add p 1
1008 p = sth_rec_put_txt p sth_t_sigdom
1009 put sth_rec p 10
1010 p = add p 1
1011 p = sth_rec_put_txt p sth_k_hid
1012 put sth_rec p 32
1013 p = add p 1
1014 p = sth_rec_put_slice p idb idlen
1015 put sth_rec p 10
1016 p = add p 1
1017 p = sth_rec_put_txt p sth_k_rdig
1018 put sth_rec p 32
1019 p = add p 1
1020 p = sth_rec_put_hex p rd 32
1021 put sth_rec p 10
1022 p = add p 1
1023 p = sth_rec_put_txt p sth_k_kid
1024 put sth_rec p 32
1025 p = add p 1
1026 p = sth_rec_put_hex p kid 32
1027 put sth_rec p 10
1028 p = add p 1
1029 p = sth_rec_put_txt p sth_k_sig
1030 put sth_rec p 32
1031 p = add p 1
1032 p = sth_rec_put_hex p sg 64
1033 put sth_rec p 10
1034 p = add p 1
1035 r = sth_rec_seal p cap
1036 if ne r 0
1037 ret r
1038 end
1039 tot = get sth_reclen 0
1040 for i ulong tot bound 1024
1041 put sth_sig i get sth_rec i
1042 end
1043 put sth_siglen 0 tot
1044 ret STH_OK
1045end
1046
1047fn sth_sign rec view uchar * rlen ulong sk view uchar 32 cap ulong to long
1048 r = sth_parse rec rlen
1049 if ne r 0
1050 ret r
1051 end
1052 pk = new uchar 32 0
1053 kid = new uchar 32 0
1054 sg = new uchar 64 0
1055 msg = new uchar 65536 0
1056 ues_pubkey sk pk
1057 sth_sha256_buf pk 32 kid
1058 ml = sth_build_sig_msg rec rlen msg
1059 ues_sign sk msg ml sg
1060 if not sth_edverify_strict pk msg ml sg
1061 sth_set_why sth_w_sself
1062 ret STH_ESIG
1063 end
1064 ret sth_emit_sig_rec kid sg cap
1065end
1066
1067fn sth_vsig_hdr srec view uchar * slen ulong id_ok edit ulong 1 to long
1068 r = sth_check_rec_bytes srec slen 1
1069 if ne r 0
1070 ret r
1071 end
1072 ml = len sth_t_smagic
1073 if lt slen add ml 1
1074 sth_set_why sth_w_smagic
1075 ret STH_EFORMAT
1076 end
1077 if not sth_slice_eq srec 0 ml sth_t_smagic
1078 sth_set_why sth_w_smagic
1079 ret STH_EFORMAT
1080 end
1081 if ne ulong get srec ml 10
1082 sth_set_why sth_w_smagic
1083 ret STH_EFORMAT
1084 end
1085 put sth_cur_pos 0 add ml 1
1086 r = sth_next_field srec slen sth_k_domain
1087 if ne r 0
1088 ret r
1089 end
1090 if not sth_slice_eq srec get sth_val_off 0 get sth_val_len 0 sth_t_sigdom
1091 sth_set_why sth_w_sdom
1092 ret STH_EFORMAT
1093 end
1094 r = sth_next_field srec slen sth_k_hid
1095 if ne r 0
1096 ret r
1097 end
1098 vo = get sth_val_off 0
1099 vl = get sth_val_len 0
1100 tmp = new uchar 65 0
1101 if or lt vl 1 gt vl 64
1102 sth_set_why sth_w_slabel
1103 ret STH_EFORMAT
1104 end
1105 for i ulong vl bound 65
1106 put tmp i get srec add vo i
1107 end
1108 if not sth_label_ok tmp vl
1109 sth_set_why sth_w_slabel
1110 ret STH_EFORMAT
1111 end
1112 ok = ulong 0
1113 if eq vl get sth_c_idlen 0
1114 ok = 1
1115 for i ulong vl bound 65
1116 if ne get tmp i get sth_c_id i
1117 ok = 0
1118 end
1119 end
1120 end
1121 put id_ok 0 ok
1122 ret STH_OK
1123end
1124
1125fn sth_vsig_crypto rec view uchar 1024 rlen ulong pk view uchar 32 sg view uchar 64 to long
1126 msg = new uchar 65536 0
1127 ml = sth_build_sig_msg rec rlen msg
1128 if not sth_edverify_strict pk msg ml sg
1129 sth_set_why sth_w_esig
1130 ret STH_ESIG
1131 end
1132 ret STH_OK
1133end
1134
1135fn sth_vsig_check_bind idok ulong rd view uchar 32 kid view uchar 32 pk view uchar 32 to long
1136 crd = new uchar 32 0
1137 for i ulong 32
1138 put crd i get sth_c_rd i
1139 end
1140 if or eq idok 0 not sth_ct_eq32 rd crd
1141 sth_set_why sth_w_ebind
1142 ret STH_EBIND
1143 end
1144 wkid = new uchar 32 0
1145 sth_sha256_buf pk 32 wkid
1146 if not sth_ct_eq32 kid wkid
1147 sth_set_why sth_w_ekey
1148 ret STH_EKEY
1149 end
1150 ret STH_OK
1151end
1152
1153fn sth_vsig_check_end srec view uchar 1024 blen ulong ed view uchar 32 to long
1154 for i ulong blen bound 1025
1155 put sh_dat i get srec i
1156 end
1157 sh_all blen
1158 d_ok = ulong 0
1159 for i ulong 32
1160 d_ok = or d_ok xor ulong get sh_dig i ulong get ed i
1161 end
1162 if ne d_ok 0
1163 sth_set_why sth_w_sedig
1164 ret STH_EDIGEST
1165 end
1166 ret STH_OK
1167end
1168
1169fn sth_verify_signed rec view uchar 1024 rlen ulong srec view uchar 1024 slen ulong pk view uchar 32 to long
1170 r = sth_parse rec rlen
1171 if ne r 0
1172 ret r
1173 end
1174 if eq slen 0
1175 sth_set_why sth_w_unsig
1176 ret STH_EUNSIGNED
1177 end
1178 id_ok = new ulong 1 0
1179 r = sth_vsig_hdr srec slen id_ok
1180 if ne r 0
1181 ret r
1182 end
1183 rd = new uchar 32 0
1184 kid = new uchar 32 0
1185 sg = new uchar 64 0
1186 ed = new uchar 32 0
1187 r = sth_next_field srec slen sth_k_rdig
1188 if ne r 0
1189 ret r
1190 end
1191 r = sth_parse_hex32 srec get sth_val_off 0 get sth_val_len 0 rd
1192 if ne r 0
1193 ret r
1194 end
1195 r = sth_next_field srec slen sth_k_kid
1196 if ne r 0
1197 ret r
1198 end
1199 r = sth_parse_hex32 srec get sth_val_off 0 get sth_val_len 0 kid
1200 if ne r 0
1201 ret r
1202 end
1203 r = sth_next_field srec slen sth_k_sig
1204 if ne r 0
1205 ret r
1206 end
1207 r = sth_parse_hex64 srec get sth_val_off 0 get sth_val_len 0 sg
1208 if ne r 0
1209 ret r
1210 end
1211 blen = get sth_cur_pos 0
1212 r = sth_next_field srec slen sth_k_end
1213 if ne r 0
1214 ret r
1215 end
1216 r = sth_parse_hex32 srec get sth_val_off 0 get sth_val_len 0 ed
1217 if ne r 0
1218 ret r
1219 end
1220 if ne get sth_cur_pos 0 slen
1221 sth_set_why sth_w_strail
1222 ret STH_EFORMAT
1223 end
1224 r = sth_vsig_check_end srec blen ed
1225 if ne r 0
1226 ret r
1227 end
1228 r = sth_vsig_check_bind get id_ok 0 rd kid pk
1229 if ne r 0
1230 ret r
1231 end
1232 ret sth_vsig_crypto rec rlen pk sg
1233end
1234
1235note ---- CLI file I/O and subcommands ----
1236
1237fn sth_arg_get idx ulong dst edit uchar 512 to ulong
1238 n = get sth_argl idx
1239 base = mul idx 512
1240 for i ulong n bound 512
1241 put dst i get sth_argv add base i
1242 end
1243 ret n
1244end
1245
1246fn sth_arg_eq idx ulong s view uchar * to bool
1247 n = get sth_argl idx
1248 if ne n len s
1249 ret false
1250 end
1251 base = mul idx 512
1252 for i ulong n bound 512
1253 if ne get sth_argv add base i get s i
1254 ret false
1255 end
1256 end
1257 ret true
1258end
1259
1260fn sth_arg_set idx ulong s view uchar * n ulong to void
1261 base = mul idx 512
1262 for i ulong n bound 512
1263 put sth_argv add base i get s i
1264 end
1265 put sth_argl idx n
1266end
1267
1268note Read file into sth_fbuf (slot 0) or sth_fbuf2 (slot 1). Returns length or -errno.
1269fn sth_read_file pth view uchar 512 plen ulong slot ulong to long
1270 fd = fopen pth 0 plen 0 0
1271 if lt fd 0
1272 ret fd
1273 end
1274 tot = ulong 0
1275 cap = ulong 1048576
1276 while lt tot cap bound 1048576
1277 r = long 0
1278 if eq slot 0
1279 r = read int fd sth_fbuf tot sub cap tot
1280 else
1281 r = read int fd sth_fbuf2 tot sub cap tot
1282 end
1283 if lt r 0
1284 if eq r -4
1285 continue
1286 end
1287 rc = fclose int fd
1288 ret r
1289 end
1290 if eq r 0
1291 break
1292 end
1293 tot = add tot ulong r
1294 end
1295 rc = fclose int fd
1296 if eq slot 0
1297 put sth_flen 0 tot
1298 else
1299 put sth_flen2 0 tot
1300 end
1301 ret long tot
1302end
1303
1304fn sth_pack_two p1 view uchar 512 l1 ulong p2 view uchar 512 l2 ulong dst edit uchar 1040 to ulong
1305 put dst 0 2
1306 put dst 1 0
1307 put dst 2 uchar and l1 255
1308 put dst 3 uchar and shr l1 8 255
1309 p = ulong 4
1310 for i ulong l1 bound 512
1311 put dst add p i get p1 i
1312 end
1313 p = add p l1
1314 put dst p uchar and l2 255
1315 put dst add p 1 uchar and shr l2 8 255
1316 p = add p 2
1317 for i ulong l2 bound 512
1318 put dst add p i get p2 i
1319 end
1320 ret add p l2
1321end
1322
1323fn sth_append_u64_buf dst edit uchar 512 pos ulong v ulong to ulong
1324 tmp = new uchar 24 0
1325 k = ulong 24
1326 x = v
1327 while true bound 24
1328 k = sub k 1
1329 put tmp k uchar add 48 rem x 10
1330 x = div x 10
1331 if eq x 0
1332 break
1333 end
1334 end
1335 n = sub 24 k
1336 for i ulong n bound 24
1337 put dst add pos i get tmp add k i
1338 end
1339 ret add pos n
1340end
1341
1342fn sth_make_tmp_path pth view uchar 512 plen ulong tmp edit uchar 512 to ulong
1343 for i ulong plen bound 480
1344 put tmp i get pth i
1345 end
1346 tp = plen
1347 tl = len sth_t_tmp
1348 for i ulong tl bound 16
1349 put tmp add tp i get sth_t_tmp i
1350 end
1351 tp = add tp tl
1352 ret sth_append_u64_buf tmp tp ulong getpid
1353end
1354
1355fn sth_write_addressed pth view uchar 512 plen ulong dat view uchar * dlen ulong to long
1356 r = sth_read_file pth plen 1
1357 if ge r 0
1358 if ne ulong r dlen
1359 ret -2
1360 end
1361 for i ulong dlen bound 1048576
1362 if ne get sth_fbuf2 i get dat i
1363 ret -2
1364 end
1365 end
1366 ret 0
1367 end
1368 if ne r -2
1369 ret -1
1370 end
1371 tmp = new uchar 512 0
1372 pk = new uchar 1040 0
1373 tp = sth_make_tmp_path pth plen tmp
1374 note O_WRONLY|O_CREAT|O_EXCL = 1|64|128 = 193, mode 0666 = 438
1375 fd = fopen tmp 0 tp 193 438
1376 if lt fd 0
1377 ret -1
1378 end
1379 ok = eq write int fd dat 0 dlen long dlen
1380 if ne fsync int fd 0
1381 ok = false
1382 end
1383 if ne fclose int fd 0
1384 ok = false
1385 end
1386 if not ok
1387 r = unlink tmp 0 tp
1388 ret -1
1389 end
1390 pkl = sth_pack_two tmp tp pth plen pk
1391 lr = link pk 0 pkl
1392 r = unlink tmp 0 tp
1393 if ne lr 0
1394 if eq lr -17
1395 ret -2
1396 end
1397 ret -1
1398 end
1399 ret 0
1400end
1401
1402fn sth_read_salt pth view uchar 512 plen ulong salt edit uchar 32 to long
1403 r = sth_read_file pth plen 0
1404 if lt r 0
1405 ret -1
1406 end
1407 if ne r 32
1408 ret -1
1409 end
1410 for i ulong 32
1411 put salt i get sth_fbuf i
1412 put sth_fbuf i 0
1413 end
1414 ret 0
1415end
1416
1417fn sth_check_key_stat fd int secret ulong to long
1418 st = new uchar 128 0
1419 if ne fstat fd st 0 128 0
1420 ret -1
1421 end
1422 mode = ulong get st 16
1423 mode = or mode shl ulong get st 17 8
1424 mode = or mode shl ulong get st 18 16
1425 mode = or mode shl ulong get st 19 24
1426 if ne and mode 0xf000 0x8000
1427 ret -1
1428 end
1429 if ne secret 0
1430 if ne and mode 63 0
1431 ret -3
1432 end
1433 end
1434 ret 0
1435end
1436
1437note Read key file safely: O_RDONLY|O_NONBLOCK|O_CLOEXEC (2048+524288=526336), fstat S_ISREG, mode & 077.
1438fn sth_read_key_file pth view uchar 512 plen ulong buf edit uchar 80 secret ulong to long
1439 ex = new uchar 1 0
1440 fd = fopen pth 0 plen 526336 0
1441 if lt fd 0
1442 ret -1
1443 end
1444 sr = sth_check_key_stat int fd secret
1445 if ne sr 0
1446 rc = fclose int fd
1447 ret sr
1448 end
1449 n = ulong 0
1450 while lt n 80 bound 128
1451 r = read int fd buf n sub 80 n
1452 if lt r 0
1453 if eq r -4
1454 continue
1455 end
1456 rc = fclose int fd
1457 ret -1
1458 end
1459 if eq r 0
1460 break
1461 end
1462 n = add n ulong r
1463 end
1464 if eq n 80
1465 er = read int fd ex 0 1
1466 if ne er 0
1467 rc = fclose int fd
1468 ret -2
1469 end
1470 end
1471 rc = fclose int fd
1472 ret long n
1473end
1474
1475fn sth_load_key pth view uchar 512 plen ulong secret ulong key edit uchar 32 to long
1476 sth_init_der
1477 buf = new uchar 80 0
1478 n = sth_read_key_file pth plen buf secret
1479 if lt n 0
1480 for i ulong 80
1481 put buf i 0
1482 end
1483 ret -1
1484 end
1485 un = ulong n
1486 ok = ulong 0
1487 if eq un 32
1488 for i ulong 32
1489 put key i get buf i
1490 end
1491 ok = 1
1492 else
1493 is_hex = ulong 0
1494 if eq un 64
1495 is_hex = 1
1496 else
1497 if eq un 65
1498 if eq ulong get buf 64 10
1499 is_hex = 1
1500 end
1501 end
1502 end
1503 if eq is_hex 1
1504 for i ulong 64
1505 c = ulong get buf i
1506 if and ge c 65 le c 70
1507 put buf i uchar add sub c 65 97
1508 end
1509 end
1510 if eq sth_parse_hex32 buf 0 64 key 0
1511 ok = 1
1512 end
1513 else
1514 if ne secret 0
1515 if eq un 48
1516 m = ulong 1
1517 for i ulong 16
1518 if ne get buf i get sth_pkcs8 i
1519 m = 0
1520 end
1521 end
1522 if eq m 1
1523 for i ulong 32
1524 put key i get buf add 16 i
1525 end
1526 ok = 1
1527 end
1528 end
1529 else
1530 if eq un 44
1531 m = ulong 1
1532 for i ulong 12
1533 if ne get buf i get sth_spki i
1534 m = 0
1535 end
1536 end
1537 if eq m 1
1538 for i ulong 32
1539 put key i get buf add 12 i
1540 end
1541 ok = 1
1542 end
1543 end
1544 end
1545 end
1546 end
1547 for i ulong 80
1548 put buf i 0
1549 end
1550 if eq ok 0
1551 for i ulong 32
1552 put key i 0
1553 end
1554 ret -1
1555 end
1556 ret 0
1557end
1558
1559fn sth_make_out_path dir view uchar 512 dlen ulong pfx view uchar * dig view uchar 32 dst edit uchar 512 to ulong
1560 p = ulong 0
1561 for i ulong dlen bound 400
1562 put dst p get dir i
1563 p = add p 1
1564 end
1565 pl = len pfx
1566 for i ulong pl bound 32
1567 put dst p get pfx i
1568 p = add p 1
1569 end
1570 for i ulong 32
1571 b = ulong get dig i
1572 put dst p sth_hex_nib shr b 4
1573 put dst add p 1 sth_hex_nib and b 15
1574 p = add p 2
1575 end
1576 tl = len sth_t_txt
1577 for i ulong tl bound 8
1578 put dst p get sth_t_txt i
1579 p = add p 1
1580 end
1581 ret p
1582end
1583
1584fn sth_get_repo_commit dst edit uchar 64 to long
1585 m = get sth_env_mode 0
1586 if eq m 1
1587 ret -1
1588 end
1589 if eq m 2
1590 n = get sth_env_len 0
1591 if gt n 64
1592 n = 64
1593 end
1594 for i ulong n bound 64
1595 put dst i get sth_env_val i
1596 end
1597 ret long n
1598 end
1599 ec = envcnt
1600 eb = new uchar 256 0
1601 kl = len sth_t_envk
1602 for i ulong ulong ec bound 4096
1603 el = envread i eb 0 256
1604 if gt el long kl
1605 match = ulong 1
1606 for j ulong kl bound 32
1607 if ne get eb j get sth_t_envk j
1608 match = 0
1609 end
1610 end
1611 if eq match 1
1612 vl = sub ulong el kl
1613 if gt vl 64
1614 vl = 64
1615 end
1616 for j ulong vl bound 64
1617 put dst j get eb add kl j
1618 end
1619 ret long vl
1620 end
1621 end
1622 end
1623 ret -1
1624end
1625
1626fn sth_cli_write_out dir_idx ulong pfx view uchar * dat view uchar * dlen ulong to int
1627 rd = new uchar 32 0
1628 dp = new uchar 512 0
1629 op = new uchar 512 0
1630 sth_sha256_buf dat dlen rd
1631 dpl = sth_arg_get dir_idx dp
1632 opl = sth_make_out_path dp dpl pfx rd op
1633 if ne sth_write_addressed op opl dat dlen 0
1634 ret 2
1635 end
1636 w1 = write 1 op 0 opl
1637 sth_wr 1 sth_t_nl
1638 ret 0
1639end
1640
1641fn sth_cli_commit to int
1642 pth = new uchar 512 0
1643 salt = new uchar 32 0
1644 pl = sth_arg_get 3 pth
1645 if ne sth_read_salt pth pl salt 0
1646 ret 2
1647 end
1648 pl = sth_arg_get 2 pth
1649 tl = sth_read_file pth pl 0
1650 if or lt tl 0 gt tl 65536
1651 ret 2
1652 end
1653 ts = new uchar 65536 0
1654 for i ulong ulong tl bound 65536
1655 put ts i get sth_fbuf i
1656 end
1657 pl = sth_arg_get 1 pth
1658 r = sth_commit part pth 0 pl part ts 0 ulong tl salt STH_REC_MAX
1659 for i ulong 32
1660 put salt i 0
1661 end
1662 if ne r 0
1663 ret 2
1664 end
1665 rl = get sth_reclen 0
1666 rb = new uchar 1024 0
1667 for i ulong rl bound 1024
1668 put rb i get sth_rec i
1669 end
1670 if ne sth_parse rb rl 0
1671 ret 2
1672 end
1673 ret sth_cli_write_out 4 sth_t_p_commit rb rl
1674end
1675
1676fn sth_cli_reveal to int
1677 rcom = new uchar 64 0
1678 rcl = sth_get_repo_commit rcom
1679 if le rcl 0
1680 ret 2
1681 end
1682 pth = new uchar 512 0
1683 pl = sth_arg_get 1 pth
1684 rbl = sth_read_file pth pl 0
1685 if or lt rbl 0 gt rbl 1024
1686 ret 2
1687 end
1688 rb = new uchar 1024 0
1689 for i ulong ulong rbl bound 1024
1690 put rb i get sth_fbuf i
1691 end
1692 if ne sth_parse rb ulong rbl 0
1693 ret 2
1694 end
1695 salt = new uchar 32 0
1696 pl = sth_arg_get 3 pth
1697 if ne sth_read_salt pth pl salt 0
1698 ret 2
1699 end
1700 pl = sth_arg_get 2 pth
1701 tl = sth_read_file pth pl 0
1702 if or lt tl 0 gt tl 65536
1703 ret 2
1704 end
1705 ts = new uchar 65536 0
1706 for i ulong ulong tl bound 65536
1707 put ts i get sth_fbuf i
1708 end
1709 rv = sth_reveal ts ulong tl salt
1710 for i ulong 32
1711 put salt i 0
1712 end
1713 if ne rv 0
1714 ret 2
1715 end
1716 if ne sth_reveal_receipt rcom ulong rcl STH_REC_MAX 0
1717 ret 2
1718 end
1719 ol = get sth_rcptlen 0
1720 for i ulong ol bound 1024
1721 put rb i get sth_rcpt i
1722 end
1723 ret sth_cli_write_out 4 sth_t_p_reveal rb ol
1724end
1725
1726fn sth_cli_verify to int
1727 rp = new uchar 512 0
1728 rpl = sth_arg_get 1 rp
1729 rbl = sth_read_file rp rpl 0
1730 if or lt rbl 0 gt rbl 1024
1731 ret 2
1732 end
1733 rb = new uchar 1024 0
1734 for i ulong ulong rbl bound 1024
1735 put rb i get sth_fbuf i
1736 end
1737 if ne sth_parse rb ulong rbl 0
1738 ret 2
1739 end
1740 sth_wr 1 sth_m_ok
1741 w1 = write 1 sth_c_id 0 get sth_c_idlen 0
1742 sth_wr 1 sth_m_ok_tc
1743 nb = new uchar 512 0
1744 nl = sth_append_u64_buf nb 0 get sth_c_tcnt 0
1745 w2 = write 1 nb 0 nl
1746 sth_wr 1 sth_m_ok_rd
1747 hx = new uchar 64 0
1748 for i ulong 32
1749 b = ulong get sth_c_rd i
1750 put hx mul i 2 sth_hex_nib shr b 4
1751 put hx add mul i 2 1 sth_hex_nib and b 15
1752 end
1753 w3 = write 1 hx 0 64
1754 sth_wr 1 sth_t_nl
1755 ret 0
1756end
1757
1758fn sth_cli_sign_write dir_idx ulong sb view uchar 1024 sl ulong to int
1759 rd = new uchar 32 0
1760 dp = new uchar 512 0
1761 op = new uchar 512 0
1762 for i ulong 32
1763 put rd i get sth_c_rd i
1764 end
1765 dpl = sth_arg_get dir_idx dp
1766 opl = sth_make_out_path dp dpl sth_t_p_sig rd op
1767 if ne sth_write_addressed op opl sb sl 0
1768 ret 2
1769 end
1770 w1 = write 1 op 0 opl
1771 sth_wr 1 sth_t_nl
1772 ret 0
1773end
1774
1775fn sth_cli_sign to int
1776 pth = new uchar 512 0
1777 rb = new uchar 1024 0
1778 sk = new uchar 32 0
1779 pl = sth_arg_get 1 pth
1780 rbl = sth_read_file pth pl 0
1781 if or lt rbl 0 gt rbl 1024
1782 ret 2
1783 end
1784 for i ulong ulong rbl bound 1024
1785 put rb i get sth_fbuf i
1786 end
1787 if ne sth_parse rb ulong rbl 0
1788 ret 2
1789 end
1790 pl = sth_arg_get 2 pth
1791 if ne sth_load_key pth pl 1 sk 0
1792 ret 2
1793 end
1794 sr = sth_sign rb ulong rbl sk STH_REC_MAX
1795 for i ulong 32
1796 put sk i 0
1797 end
1798 if ne sr 0
1799 ret 2
1800 end
1801 sl = get sth_siglen 0
1802 for i ulong sl bound 1024
1803 put rb i get sth_sig i
1804 end
1805 ret sth_cli_sign_write 3 rb sl
1806end
1807
1808fn sth_print_ok_signed pk view uchar 32 to void
1809 sth_wr 1 sth_m_oks
1810 w1 = write 1 sth_c_id 0 get sth_c_idlen 0
1811 sth_wr 1 sth_m_ok_tc
1812 nb = new uchar 512 0
1813 nl = sth_append_u64_buf nb 0 get sth_c_tcnt 0
1814 w2 = write 1 nb 0 nl
1815 sth_wr 1 sth_m_ok_rd
1816 hx = new uchar 64 0
1817 for i ulong 32
1818 b = ulong get sth_c_rd i
1819 put hx mul i 2 sth_hex_nib shr b 4
1820 put hx add mul i 2 1 sth_hex_nib and b 15
1821 end
1822 w3 = write 1 hx 0 64
1823 sth_wr 1 sth_m_oks_kid
1824 kd = new uchar 32 0
1825 sth_sha256_buf pk 32 kd
1826 for i ulong 32
1827 b = ulong get kd i
1828 put hx mul i 2 sth_hex_nib shr b 4
1829 put hx add mul i 2 1 sth_hex_nib and b 15
1830 end
1831 w4 = write 1 hx 0 64
1832 sth_wr 1 sth_t_nl
1833end
1834
1835fn sth_derive_sig_path rp view uchar 512 rpl ulong rb view uchar 1024 rbl ulong sp edit uchar 512 to ulong
1836 d = new uchar 32 0
1837 dir = new uchar 512 0
1838 sth_sha256_buf rb rbl d
1839 slash = long -1
1840 for i ulong rpl bound 512
1841 if eq ulong get rp i 47
1842 slash = long i
1843 end
1844 end
1845 dl = ulong 0
1846 if ge slash 0
1847 dl = ulong slash
1848 for i ulong dl bound 512
1849 put dir i get rp i
1850 end
1851 else
1852 put dir 0 46
1853 dl = 1
1854 end
1855 ret sth_make_out_path dir dl sth_t_p_sig d sp
1856end
1857
1858fn sth_cli_load_sig rb view uchar 1024 rbl ulong sb edit uchar 1024 to long
1859 rp = new uchar 512 0
1860 sp = new uchar 512 0
1861 ac = get sth_argc 0
1862 spl = ulong 0
1863 if eq ac 5
1864 spl = sth_arg_get 4 sp
1865 else
1866 rpl = sth_arg_get 3 rp
1867 spl = sth_derive_sig_path rp rpl rb rbl sp
1868 end
1869 sbl = sth_read_file sp spl 0
1870 if lt sbl 0
1871 if or ne sbl -2 eq ac 5
1872 ret -1
1873 end
1874 ret 0
1875 end
1876 if gt sbl 1024
1877 ret -1
1878 end
1879 for i ulong ulong sbl bound 1024
1880 put sb i get sth_fbuf i
1881 end
1882 ret sbl
1883end
1884
1885fn sth_cli_verify_strict to int
1886 pth = new uchar 512 0
1887 pk = new uchar 32 0
1888 rb = new uchar 1024 0
1889 sb = new uchar 1024 0
1890 pl = sth_arg_get 2 pth
1891 if ne sth_load_key pth pl 0 pk 0
1892 ret 2
1893 end
1894 pl = sth_arg_get 3 pth
1895 rbl = sth_read_file pth pl 0
1896 if or lt rbl 0 gt rbl 1024
1897 ret 2
1898 end
1899 for i ulong ulong rbl bound 1024
1900 put rb i get sth_fbuf i
1901 end
1902 sbl = sth_cli_load_sig rb ulong rbl sb
1903 if lt sbl 0
1904 ret 2
1905 end
1906 if ne sth_verify_signed rb ulong rbl sb ulong sbl pk 0
1907 ret 2
1908 end
1909 sth_print_ok_signed pk
1910 ret 0
1911end
1912
1913fn sth_cli_run to int
1914 ac = get sth_argc 0
1915 if lt ac 1
1916 sth_wr 2 sth_w_usage
1917 ret 1
1918 end
1919 if and sth_arg_eq 0 sth_t_cmd_commit eq ac 5
1920 ret sth_cli_commit
1921 end
1922 if and sth_arg_eq 0 sth_t_cmd_reveal eq ac 5
1923 ret sth_cli_reveal
1924 end
1925 if sth_arg_eq 0 sth_t_cmd_verify
1926 if or eq ac 4 eq ac 5
1927 if sth_arg_eq 1 sth_t_opt_strict
1928 ret sth_cli_verify_strict
1929 end
1930 end
1931 if eq ac 2
1932 ret sth_cli_verify
1933 end
1934 end
1935 if and sth_arg_eq 0 sth_t_cmd_sign eq ac 4
1936 ret sth_cli_sign
1937 end
1938 sth_wr 2 sth_w_usage
1939 ret 1
1940end