interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / codec / README.md

283 lines · 19847 bytes Raw Blame History Rendered
1# om/codec: base64, base32 and hex for the Tailnet Lock wire format
2
3The text encodings that Tailnet Lock (TKA) JSON uses, written in Omega (`.om`) and compiled by the
4pinned seed-born `omc`. Each one gives the same output as the Go code that the Tailscale client and
5`tailscale.com` v1.102.0 use. That includes accepting and rejecting the same malformed inputs.
6
7| Wire field | Go code | Functions |
8|---|---|---|
9| `[]byte` fields (`GenesisAUM`, `MissingAUMs`, signatures, `DisablementSecret`, ...) | `encoding/json` marshals with `base64.StdEncoding` and unmarshals with `base64.StdEncoding.Decode` | `cdb64enc`, `cdb64dec` |
10| AUM hashes (`Head`, `Ancestors`) | `tka.AUMHash` `MarshalText` / `UnmarshalText`: `base32.StdEncoding.WithPadding(base32.NoPadding)` | `cdaumenc`, `cdaumdec`, plus the general `cdb32enc`, `cdb32dec` |
11| Node keys (`NodeKey`) | `key.NodePublic` `MarshalText` / `UnmarshalText`: `nodekey:` + hex | `cdnkenc`, `cdnkdec`, plus the general `cdhexenc`, `cdhexdec` |
12
13`codec.om` is a library unit: functions only, no `fn main`, no globals. Every name starts with
14`cd`. It needs no other unit. Go appears only in the tests, as the oracle.
15
16## API
17
18```
19const cdbad ulong 65537
20fn cdb64enc s view uchar 65536 n ulong o edit uchar 65536 to ulong
21fn cdb64dec s view uchar 65536 n ulong o edit uchar 65536 to ulong
22fn cdb32enc s view uchar 65536 n ulong o edit uchar 65536 to ulong
23fn cdb32dec s view uchar 65536 n ulong o edit uchar 65536 to ulong
24fn cdhexenc s view uchar 65536 n ulong o edit uchar 65536 to ulong
25fn cdhexdec s view uchar 65536 n ulong o edit uchar 65536 to ulong
26fn cdaumenc h view uchar 32 o edit uchar 65536 to ulong
27fn cdaumdec s view uchar 65536 n ulong h edit uchar 32 to ulong
28fn cdnkenc k view uchar 32 o edit uchar 65536 to ulong
29fn cdnkdec s view uchar 65536 n ulong k edit uchar 32 to ulong
30```
31
32| Function | Input | Result |
33|---|---|---|
34| `cdb64enc` | bytes `s[0..n)`, `n` 0..49152 | Padded standard base64 in `o`. Returns the length, `4*ceil(n/3)`, at most 65536. |
35| `cdb64dec` | text `s[0..n)`, `n` 0..65536 | Returns the decoded byte count, with the bytes in `o`, or `cdbad`. |
36| `cdb32enc` | bytes, `n` 0..40960 | Unpadded standard base32. Returns `n/5*8 + (n%5*8+4)/5`, at most 65536. |
37| `cdb32dec` | text, `n` 0..65536 | Byte count or `cdbad`. |
38| `cdhexenc` | bytes, `n` 0..32768 | Lowercase hex. Returns `2n`. |
39| `cdhexdec` | text, `n` 0..65536 | `n/2` or `cdbad`. |
40| `cdaumenc` | 32-byte hash | 52 base32 characters. Returns 52. |
41| `cdaumdec` | text, `n` 0..65536 | 1 means accepted, with the hash in `h`. 0 means rejected, and `h` is all zero. |
42| `cdnkenc` | 32-byte key | `nodekey:` and 64 lowercase hex digits. Returns 72. |
43| `cdnkdec` | text, `n` 0..65536 | 1 means accepted, with the key in `k`. 0 means rejected, and `k` is all zero. |
44
45- A decoder never traps on its input text: any malformed text returns `cdbad` or 0. Evidence: in
46 every run listed under Evidence, each case ran with the driver exiting 0. 11 runs × 40,102
47 cases make 441,122 driver cases. A trap exits 100 + its code, and `run.sh` then fails the check.
48- An `n` above the stated maximum breaks the caller's contract. The function traps 3 (program exit
49 103) before it writes anything. The six `driver N` calls check this, one past each capacity.
50- When a decoder returns `cdbad`, the bytes it already wrote to `o` stay there. The caller discards `o`.
51- The decoders take the bytes after JSON unquoting. JSON string escapes (`\n`, `\u0041`, ...) belong
52 to the JSON layer. So does the `null` / `""` distinction for a nil or empty `[]byte`: Go's
53 `encodeByteSlice` writes a nil slice as `null` (encode.go 824-828).
54- `cdaumdec` matches `UnmarshalText` called on a zero `AUMHash`, which is how the client uses it
55 (`var remoteHead tka.AUMHash` at `ipn/ipnlocal/tailnet-lock.go` 494-495). When the input has fewer
56 symbols than 52, `Decode` writes only the leading bytes, and the rest stay zero.
57
58The other functions are internal: character maps (`cdb64v`, `cdb64c`, `cdb32v`, `cdb32c`,
59`cdhexv`, `cdhexc`), byte packing (`cdgetbe`, `cdputbe`), and the base32 decoder pieces
60(`cdstrip`, `cdb32q`, `cdb32pad`, `cdb32nb`, `cdb32out`, `cdb32raw`).
61
62## Rules, pinned to source
63
64Sources: Go go1.26.0 `src/encoding/{json,base64,base32,hex}` and `tailscale.com` v1.102.0 (commit
65`35283c95445fb198af1ce708dac05714bdfaa037`). Both were fetched with the Go module proxy on
662026-10-25. The line numbers below are from those files.
67
68### base64 (`[]byte` in JSON)
69
70- **Encoding.** `encodeByteSlice` (json/encode.go 824-834) calls `base64.StdEncoding.AppendEncode`.
71 That is RFC 4648 section 4 with the alphabet `A-Z a-z 0-9 + /` and `=` padding.
72- **Decoding.** `literalStore` (json/decode.go 945-956) calls
73 `base64.StdEncoding.Decode(b, s)` into `DecodedLen(len(s))` bytes. Any error rejects the field.
74 This applies to the default build, where `decode.go` has `//go:build !goexperiment.jsonv2`.
75 There is **no** `RawStdEncoding` fallback and no `Strict()`. The v2 code under `GOEXPERIMENT=jsonv2`
76 was not traced end to end.
77- **Accepted input** (`decodeQuantum` base64.go 312-407, `Decode` 518-584):
78 - CR and LF are skipped everywhere, including between and after the `=` characters
79 (base64.go 340, 356-359, 372-375).
80 - Without CR and LF, the text must be whole groups of 4 characters. Only the last group may end in
81 `xx==` or `xxx=`. A `=` anywhere else, missing padding (`Zg`, `Zg=`), or anything other than CR
82 or LF after the padding (`Zg==A`, `Zm8==`) is an error.
83 - Non-zero trailing bits are accepted (`Zh==` decodes to `66`), because the strict checks at
84 base64.go 394 and 401 run only for `Strict()` encodings.
85 - Empty text, or text that is only CR and LF, decodes to zero bytes.
86 - Any byte outside the alphabet, including space, tab, NUL and bytes 128..255, is an error.
87- `cdb64dec` works in one pass, with the same rules as an explicit state machine. The variable `pd`
88 holds the padding state.
89
90### base32 (AUM hashes)
91
92- **The encoding.** `base32StdNoPad = base32.StdEncoding.WithPadding(base32.NoPadding)` (tka/aum.go
93 24). That is RFC 4648 section 6 with the alphabet `A-Z 2-7` and no padding.
94- **Encoding** (base32.go 121-187). A final 1, 2, 3 or 4 bytes become 2, 4, 5 or 7 characters
95 (`EncodedLen` 284-289).
96- **Decoding.** `DecodeString` (base32.go 421-426) first strips CR and LF everywhere
97 (`stripNewlines` 545-555). Then `decode` (305-386) runs. Because the padding is `NoPadding`, Go
98 behaves like this, and `cdb32dec` copies it:
99 - Lowercase letters, `0`, `1`, `8`, `9`, `=` and every other byte outside `A-Z 2-7` are errors.
100 `=` is not special without padding.
101 - A final group of 1, 3 or 6 symbols decodes to **no bytes and no error** (switch at 362-382 has
102 no case for them; the end-of-input branch at 320-326 never checks). `M` and `MZX` decode to the
103 empty string. `MZXW6YTBO` decodes to `fooba`, and the final `O` is dropped.
104 - Non-zero trailing bits are accepted (`MZ` decodes to `66`).
105 - The padding test at line 330 compares the input byte with `byte(enc.padChar)`. For `NoPadding`
106 (rune -1) that is **byte 255**. A 255 byte after at least 2 symbols of a group, with fewer than
107 8 bytes left, is handled as padding:
108 - the next `7 - j` bytes must also be 255, and fewer than that is an error (lines 331-341);
109 - a group of 3 or 6 symbols before it is an error (line 348);
110 - bytes after the checked ones are not read, so `MY` + six 255 bytes + `MY` decodes to `66`.
111
112 `cdb32pad` copies this exactly. Through JSON, byte 255 cannot reach `UnmarshalText`: Go's
113 JSON unquoting replaces invalid UTF-8 with U+FFFD. The decoder accepts it anyway, because it
114 matches Go's `Decode` and not a guess about where its input comes from.
115- **`tka.AUMHash.UnmarshalText`** (tka/aum.go 33-41). It first requires
116 `DecodedLen(len(text)) == 32`, computed on the raw length before CR and LF are stripped. The
117 `NoPadding` `decodedLen` (base32.go 582-587) is `n/8*5 + n%8*5/8`. That equals 32 only for
118 n = 52 (51 gives 31 and 53 gives 33). Then it decodes into the 32 bytes. Consequences, all present
119 in the corpus:
120 - 51 symbols plus one LF is accepted: the last group of 3 symbols is dropped, so 30 bytes come
121 from the text and 2 stay zero.
122 - 52 symbols plus an LF (53 bytes) is rejected.
123 - Lowercase is rejected.
124- **`tka.AUMHash.MarshalText`** (tka/aum.go 44-51): 52 characters.
125
126### hex and node keys
127
128- `hex.EncodeToString` (hex.go 45-53) is lowercase. `hex.DecodeString` (hex.go 87-113, 138-142)
129 accepts either case and rejects an odd length or any byte outside `0-9 a-f A-F`.
130- **`key.NodePublic.MarshalText`** (types/key/node.go 324-332, util.go `appendHexKey` 56-61)
131 writes `nodekey:` (node.go 36) and 64 lowercase hex digits.
132- **`key.NodePublic.UnmarshalText`** (node.go 336-338, util.go `parseHex` 70-87, `fromHexChar`
133 90-101):
134 - the prefix is exactly `nodekey:`, case-sensitive;
135 - then exactly 64 characters follow, each a hex digit of either case;
136 - there is no whitespace or CR/LF tolerance, so mixed-case digits are accepted and `NODEKEY:` is
137 rejected.
138
139 `cdnkdec` checks the 8 prefix bytes against the constant `cdnkpfx` (the big-endian value of
140 `nodekey:`).
141
142## Test driver protocol
143
144The driver (`test/driver.om`) reads one case per line on stdin: `OP ARG`.
145
146- `OP` is one of `b64e b64d b32e b32d hexe hexd aume aumd nkye nkyd`.
147- `ARG` is the input bytes in hex, or `-` for empty. For a decoder these are the bytes of the text,
148 so CR, LF, 255 and any other byte can appear.
149- For each case it writes one line: `ok TEXT` for an encoder, and `ok HEX` or `err` for a decoder.
150 `-` stands for an empty result. For `aumd`/`nkyd` the driver also checks that a rejected call left
151 the 32-byte output all zero.
152- It exits 0 at the end of input and 65 on a malformed line.
153- `driver N` (N = 1..6) calls one coder with a length one past its capacity, and must exit 103.
154
155`run.sh OMC` does the following:
156
1571. Builds `build/test/codec/driver`.
1582. Turns the RFC 4648 section 10 lines into 47 cases:
159 - base64: encode and decode;
160 - base32: encode to the RFC value with `=` removed, decode that value, and check that the
161 padded RFC form is rejected, as Go `NoPadding` rejects `=`;
162 - base16: encode to lowercase, and decode the RFC's uppercase.
1633. Runs the 249 committed malformed-input cases in `vectors/malformed.txt` against Go's results.
1644. Runs the differential corpus, when a `go` command (or `$GO`) can build `gen/oracle` with
165 `GOTOOLCHAIN=go1.26.0`. Otherwise it prints `NOTE ... NOT_RUN` and still exits on the static checks.
1665. Runs the six over-capacity trap calls.
1676. Runs the mutants against the 296 static cases. A mutant counts as detected only when the driver
168 runs every case to completion and at least one result differs from the expected one. Running
169 to completion means every chunk exits 0 (no trap, no signal, no launch failure) and writes one
170 result line per case. Any other outcome fails the suite: a run that does not complete, a short
171 output, or identical output. The mutants:
172 - expectation mutant: the first expected result changed to `err`;
173 - `b64-lower-value`: `a-z` decodes one too high;
174 - `b64-no-crlf-skip`: LF is no longer skipped in base64;
175 - `b64-pad-after-1`: a single `=` is allowed after 1 symbol instead of 3;
176 - `b32-digit-char`: the base32 digit characters are off by one;
177 - `b32-pad-byte-eq`: base32 treats `=` as padding, as padded `StdEncoding` does;
178 - `b32-pad-dlen6`: a 255-padding after 6 symbols is accepted;
179 - `hex-upper-value`: `A-F` decodes one too low;
180 - `aum-length`: AUMHash accepts lengths below 52;
181 - `nodekey-prefix`: the prefix constant is off by one.
182
183`CODEC_SEED` sets the differential seed (default 4648). The driver holds 16 MiB of input, so
184`run.sh` feeds it 1000 lines at a time.
185
186## Vector sources
187
188| Source | Origin |
189|---|---|
190| `vectors/rfc4648.txt` | The 21 BASE64/BASE32/BASE16 lines of RFC 4648 section 10, copied verbatim. Source: https://www.rfc-editor.org/rfc/rfc4648.txt, fetched 2026-10-25, sha256 `84e14418f795d503be5f34bf23ce4ebaa119e9ec7c9f667d8caeb111385b178f`, lines 652-713. See [Third-party material and notices](#third-party-material-and-notices). |
191| `vectors/malformed.txt` | 249 hand-made cases, in 18 named groups (`gen/oracle corpus`). They cover empty input, bad characters, wrong padding, non-zero trailing bits, wrong lengths, mixed case, embedded CR/LF, the base32 255-padding byte, AUMHash length versus CR/LF, and node-key prefix, case and length. Results come from Go go1.26.0 (`gen/vectors.sh`). |
192| Differential (`gen/oracle rand SEED`, at test time) | 39,806 cases per seed: (1) 300 rounds of random bytes, 0..4096 long, given to every decoder (1,500 cases); (2) every length 0..2048 for `b64e`, `b32e` and `hexe`, plus decoding each result (12,294); (3) 4,000 mutated encodings for each of `b64d`, `b32d` and `hexd`, with 1..3 edits each: replace, insert, delete, truncate, append, case flip, CR/LF insert, drawing from the alphabet and from `= CR LF space tab NUL 0x80 0xff - _ " \ ! . : g G z Z 0 1 8 9 + / a A` (12,000); (4) 2,000 random hashes and keys, each with `aume`, `nkye`, valid `aumd`/`nkyd`, a mutated `aumd`/`nkyd`, and a 52-byte `aumd` with 1..8 symbols replaced by CR, LF or 255 (14,000); (5) the capacity edges `b64e` 49152, `b32e` 40960 and `hexe` 32768 bytes, plus 65536-character `b64d`/`b32d`/`hexd` inputs that are valid, that end in `!`, or are random bytes (12). |
193
194`gen/oracle/main.go` uses only the standard library and is built with go1.26.0. It contains code
195derived from tailscale.com v1.102.0, listed under
196[Third-party material and notices](#third-party-material-and-notices). The derived code is there
197because that module requires go >= 1.26.5. `gen/tsoracle/` calls the real
198`tailscale.com/tka` and `tailscale.com/types/key` at v1.102.0 instead. `gen/check-tailscale.sh`
199compares the two referees. It is not part of `run.sh`, because it needs network access and a newer
200Go.
201
202### Third-party material and notices
203
204| Copied material | Owner and license | Notice file |
205|---|---|---|
206| In `gen/oracle/main.go`: `base32StdNoPad`, `aumUnmarshalText`, `parseHex` and `fromHexChar`. They are derived from tailscale/tailscale tag v1.102.0 (commit `35283c95445fb198af1ce708dac05714bdfaa037`), `tka/aum.go` 24 and 33-41 and `types/key/util.go` 70-101. The code is modified: `*[32]byte` instead of `AUMHash`, strings instead of `mem.RO`, and shorter `parseHex` error messages. Each block is marked in the source, from "Derived from tailscale" to "End of tailscale-derived code". The file is `SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause`. | Copyright (c) 2020 Tailscale Inc & contributors; BSD 3-Clause License | `gen/oracle/TAILSCALE.LICENSE`: the upstream LICENSE at v1.102.0, verbatim (sha256 `a7ca6186…a45b`, identical to the LICENSE in the v1.102.0 module zip) |
207| The 21 BASE64/BASE32/BASE16 lines in `vectors/rfc4648.txt`, from RFC 4648 section 10 | Copyright (C) The Internet Society (2006), under BCP 78. Section 15, "Copying Conditions" (Copyright (c) 2000-2006 Simon Josefsson), grants permission to use, modify and distribute sections 1, 3, 8, 10, 12, 13 and 14 | `vectors/rfc4648.NOTICE`: the RFC's Copyright Notice, section 15 and Full Copyright Statement, verbatim |
208
209The header of `vectors/rfc4648.txt` names its notice file.
210
211Nothing else in `om/codec` is copied from a third party:
212
213- `codec.om`, `test/driver.om`, the shell scripts and `gen/tsoracle/main.go` are omnet's own.
214 `tsoracle` imports Tailscale packages but copies no code from them.
215- `codec.om` reproduces the behaviour of Go's `encoding/base64`, `encoding/base32` and
216 `encoding/hex` (BSD-3-Clause, The Go Authors). Its `note` lines cite Go source line numbers, and
217 `cdb32q`/`cdb32pad` follow the same order of checks as base32.go `decode` so that the error
218 cases come out identical. No Go source text is copied into it.
219- `vectors/malformed.txt` is generated data, and `gen/tsoracle/go.sum` lists module hashes.
220
221## Evidence (2026-10-25, linux arm64 host)
222
223```
224$ GO=go sh om/codec/test/run.sh "$(tools/omc.sh)" # go 1.26.8 on PATH, runs go1.26.0
225codec: omc sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61
226codec: codec.om sha256 b3a2de16db05eaac663df2c1cca63c033309eb4705382634a9ed793e6b201b55
227codec: driver sha256 2ebf7d063f0a7a90ad4b6e1cc267e0148887a3310b32b73c1cd46e63f2eddcc5
228codec: rfc4648 47/47 PASS (42 ok, 5 err)
229codec: malformed 249/249 PASS (112 ok, 137 err)
230codec: oracle go version go1.26.0 linux/arm64, seed 4648
231codec: differential 39806/39806 PASS (25209 ok, 14597 err)
232codec: over-capacity call 1 traps 3 PASS
233 ... calls 2..6 likewise PASS
234codec: mutant expected-result detected PASS (1 of 296 results differ)
235codec: mutant b64-lower-value detected PASS (27 of 296 results differ)
236codec: mutant b64-no-crlf-skip detected PASS (18 of 296 results differ)
237codec: mutant b64-pad-after-1 detected PASS (11 of 296 results differ)
238codec: mutant b32-digit-char detected PASS (4 of 296 results differ)
239codec: mutant b32-pad-byte-eq detected PASS (15 of 296 results differ)
240codec: mutant b32-pad-dlen6 detected PASS (1 of 296 results differ)
241codec: mutant hex-upper-value detected PASS (11 of 296 results differ)
242codec: mutant aum-length detected PASS (3 of 296 results differ)
243codec: mutant nodekey-prefix detected PASS (7 of 296 results differ)
244```
245
246The run exited with code 0 and took about 1.4 s.
247
248Other runs:
249
250| Run | Result |
251|---|---|
252| `CODEC_SEED` = 1..10 | 39806/39806 each. Together with seed 4648 that is 437,866 differential cases (11 × 39,806). |
253| `PATH=/usr/bin:/bin` (no Go) | RFC, corpus, traps and mutants PASS. Differential NOT_RUN. Exit 0. |
254| `gen/check-tailscale.sh` (oracle go1.26.0, tsoracle go1.26.8 with tailscale.com v1.102.0) | `aume`/`aumd`/`nkye`/`nkyd` lines: corpus 58/58, rand seeds 1, 2 and 3 14600/14600 each. The two referees are identical. |
255| Throwaway mutant `ret 63` → `trap 9` in `cdb64v`, appended to `run.sh` once and then removed | `codec: mutant zz-throwaway-trap did not run to completion (exit 109) FAIL`, `run.sh` exit 1. A trapping mutant fails the suite; it is not counted as detected. |
256
257Hashes:
258
259| Item | sha256 |
260|---|---|
261| omega-mv pin | `acb3f15cdc691b1b046b48b0dad0f9255a12ba4b` (commit) |
262| omc | `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61` |
263| `codec.om` | `b3a2de16db05eaac663df2c1cca63c033309eb4705382634a9ed793e6b201b55` |
264| `test/driver.om` | `c7fdb4e633b13d358582d5218c7a755a6bea4bc6b6a68f272b47fcc943aeed04` |
265| driver binary | `2ebf7d063f0a7a90ad4b6e1cc267e0148887a3310b32b73c1cd46e63f2eddcc5` |
266| `vectors/rfc4648.txt` | `c022f1ba1aa15451c29188086461ceb1ee5b7b60f4deff1eb47ed149a23b37e5` |
267| `vectors/rfc4648.NOTICE` | `25fb7ac673a9552abf668e7a88e029299f44f6e9b45ff43160e50941ef79c7b3` |
268| `gen/oracle/TAILSCALE.LICENSE` | `a7ca6186a7963a0a60740f6047760eecd7a0234e8c38bd7e1e0bbcb324bda45b` |
269| `vectors/malformed.txt` | `8abed84be2f8bd56b0b29b092ff6cb01e2a1397172df8177ef6704044e18285f` |
270
271These are host runs only. They are not hardware qualification.
272
273## Limits
274
275- **Not constant-time-audited.** The decoders branch on every input byte. Omega gives no timing
276 guarantee, and no timing was measured. Base64 and hex of secrets (`DisablementSecret`) go through
277 these branches.
278- Capacities: decoder text 65536 bytes, base64 encoder input 49152, base32 40960, hex 32768. The
279 TKA design caps request bodies at 64 KiB, so one field always fits.
280- No streaming API and no offset parameters: input starts at `s[0]` and output at `o[0]`.
281- The JSON layer (unquoting, `null`, field names) is not here.
282- The jsonv2 experiment path (`GOEXPERIMENT=jsonv2`) and Tailscale's own Go toolchain fork were
283 not examined. The rules follow the standard go1.26.0 default build.