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
omega-mv / om / tls / test / fuzz / fz_flight.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note fz_flight.om: fuzz target for the client's whole server-flight path in om/tls/tls.om: the record |
| 3 | note layer (tls_rdrec: framing, CCS, AEAD open, inner type and padding), handshake reassembly |
| 4 | note (tls_hsmsg), ServerHello (tls_rdsh) and the encrypted flight (tls_rdenc: EncryptedExtensions, |
| 5 | note CertificateRequest, Certificate + chain verification, CertificateVerify, Finished), exactly as |
| 6 | note tls_connect runs them after a real ClientHello (tls_mkch). |
| 7 | note Input: R1 R0 raw[R] chunks. raw is what the server sends before its keys exist (plaintext |
| 8 | note ServerHello, CCS, alerts, any forged records). The chunks (common.om fz_feed) follow, sealed |
| 9 | note under the server handshake key that the input's ServerHello gives this ClientHello. |
| 10 | note To seal the chunks before the client reads anything, a byte copy m of the client state (same |
| 11 | note ClientHello, same transcript) first reads raw from its own socket pair, which is shut so it sees |
| 12 | note the end instead of waiting; when its tls_rdsh succeeds, its read key becomes the sealing key. |
| 13 | note All the client's input is then in its socket and the write side is shut: no read can block. |
| 14 | note Arguments: 1 = PEM trust store (om/tls/test/x509/vectors/pki/storeAll.pem). |
| 15 | |
| 16 | note fz_fdset: connection fd and a 2 s deadline into a state (no wait can reach it: see above) |
| 17 | fn fz_fdset c edit uchar * fd ulong to void |
| 18 | tls_pn c TLFD fd 4 |
| 19 | tls_pn c TLDL add ulong monons 2000000000 8 |
| 20 | end |
| 21 | |
| 22 | note fz_mirror: m (after its tls_rdsh) turns into the server's sealing side, writing to fd |
| 23 | fn fz_mirror m edit uchar * fd ulong to void |
| 24 | for i ulong 32 |
| 25 | put m add TLWKEY i get m add TLRKEY i |
| 26 | end |
| 27 | for i ulong 12 |
| 28 | put m add TLWIV i get m add TLRIV i |
| 29 | end |
| 30 | tls_pn m TLWS 0 8 |
| 31 | put m TLWK 1 |
| 32 | tls_pn m TLFD fd 4 |
| 33 | end |
| 34 | |
| 35 | note fz_pre: m reads raw from pair a (shut at once); on success, m seals the chunks into the client's |
| 36 | note pair b. Returns the client's end of b plus 1, or 0 when no socket pair could be made. |
| 37 | fn fz_pre m edit uchar * th edit ulong 40 x view uchar * r ulong to ulong |
| 38 | a = new ulong 2 0 |
| 39 | b = new ulong 2 0 |
| 40 | if ne fz_pair a 0 |
| 41 | ret 0 |
| 42 | end |
| 43 | if ne fz_pair b 0 |
| 44 | ret 0 |
| 45 | end |
| 46 | fz_fdset m get a 0 |
| 47 | s = fz_send int get a 1 x 2 r |
| 48 | s = fz_send int get b 1 x 2 r |
| 49 | z = shutdown int get a 1 1 |
| 50 | if eq tls_rdsh m th 0 |
| 51 | fz_mirror m get b 1 |
| 52 | s = fz_feed m x add 2 r |
| 53 | end |
| 54 | z = shutdown int get b 1 1 |
| 55 | ret add get b 0 1 |
| 56 | end |
| 57 | |
| 58 | note fz_detch: make the ClientHello tls_mkch wrote deterministic, so a seed's CertificateVerify and |
| 59 | note Finished (which sign or MAC the transcript) stay valid: random = 64..95, legacy_session_id = |
| 60 | note 96..127, x25519 private key = 1..32 (its public key replaces the old one in key_share, both in |
| 61 | note the x25519 entry and as the tail of the X25519MLKEM768 entry), and the hybrid's ML-KEM-768 key |
| 62 | note pair (fz_detkem). Every other ClientHello byte is tls_mkch's own. |
| 63 | fn fz_detch c edit uchar * to ulong |
| 64 | k = new uchar 32 0 |
| 65 | o = new uchar 32 0 |
| 66 | fz_fill k 0 32 1 |
| 67 | if ne x25519_base o k 0 |
| 68 | ret 1 |
| 69 | end |
| 70 | b = add TLWB 5 |
| 71 | n = add tls_gn c add b 1 3 4 |
| 72 | p = fz_find c b n |
| 73 | if eq p 0 |
| 74 | ret 1 |
| 75 | end |
| 76 | r = sub add b n add p 32 |
| 77 | q = ulong 0 |
| 78 | if ge r 32 |
| 79 | q = fz_find c add p 32 r |
| 80 | end |
| 81 | if and ne get c TLKGEN 0 eq q 0 |
| 82 | ret 1 |
| 83 | end |
| 84 | if ne fz_detkem c b p 0 |
| 85 | ret 1 |
| 86 | end |
| 87 | for i ulong 32 |
| 88 | put c add TLEPK i get k i |
| 89 | put c add TLEPU i get o i |
| 90 | put c add p i get o i |
| 91 | if ne q 0 |
| 92 | put c add q i get o i |
| 93 | end |
| 94 | end |
| 95 | fz_fill c add b 6 32 64 |
| 96 | fz_fill c add b 39 32 96 |
| 97 | tls_mov c TLSID add b 39 32 |
| 98 | ret 0 |
| 99 | end |
| 100 | |
| 101 | note fz_detkem: while tls_kemset holds an ML-KEM-768 key pair (c[TLKGEN]), the hybrid entry is ek || |
| 102 | note x25519 public key, so its ek is the 1184 bytes before p (the first copy of that public key in |
| 103 | note the ClientHello at c[b..]). The pair is made again from the seed 128..191: dk where tls_kemset |
| 104 | note keeps it (c[TLKDK], 2400 bytes), the new ek over the old one. 0, or 1 when the bytes before p |
| 105 | note are not the held ek or the key pair fails. |
| 106 | fn fz_detkem c edit uchar * b ulong p ulong to ulong |
| 107 | if eq get c TLKGEN 0 |
| 108 | ret 0 |
| 109 | end |
| 110 | if lt p add b 1184 |
| 111 | ret 1 |
| 112 | end |
| 113 | e = sub p 1184 |
| 114 | for i ulong 1184 |
| 115 | if ne get c add e i get c add TLKDK add 1152 i |
| 116 | ret 1 |
| 117 | end |
| 118 | end |
| 119 | s = new uchar 64 0 |
| 120 | fz_fill s 0 64 128 |
| 121 | ek = new uchar 1184 0 |
| 122 | if ne mlkem_keygen_seed s ek part c TLKDK 2400 0 |
| 123 | ret 1 |
| 124 | end |
| 125 | for i ulong 1184 |
| 126 | put c add e i get ek i |
| 127 | end |
| 128 | ret 0 |
| 129 | end |
| 130 | |
| 131 | note fz_find: the offset in c[b, b+n) of the 32-byte public key c[TLEPU..]; 0 when absent |
| 132 | fn fz_find c view uchar * b ulong n ulong to ulong |
| 133 | for j ulong sub n 31 |
| 134 | d = ulong 0 |
| 135 | for i ulong 32 |
| 136 | d = or d xor ulong get c add add b j i ulong get c add TLEPU i |
| 137 | end |
| 138 | if eq d 0 |
| 139 | ret add b j |
| 140 | end |
| 141 | end |
| 142 | ret 0 |
| 143 | end |
| 144 | |
| 145 | note fz_dump: with argument 2, the ClientHello message is written to that file (gen_seeds.py reads it) |
| 146 | fn fz_dump c view uchar * n ulong to void |
| 147 | p = new uchar 4096 0 |
| 148 | k = fz_arg 2 p |
| 149 | if eq k 0 |
| 150 | ret |
| 151 | end |
| 152 | f = fopen p 0 k 524865 420 |
| 153 | if lt f 0 |
| 154 | ret |
| 155 | end |
| 156 | w = write int f c add TLWB 5 n |
| 157 | x = fclose int f |
| 158 | end |
| 159 | |
| 160 | fn fz_flrun c edit uchar * m edit uchar * x view uchar * to ulong |
| 161 | th = new ulong 40 0 |
| 162 | t2 = new ulong 40 0 |
| 163 | st = new uchar TLSTSZ 0 |
| 164 | s = fz_store st |
| 165 | if ne tls_init c FZHOST 1 0 |
| 166 | ret 0 |
| 167 | end |
| 168 | sha256_init th |
| 169 | if ne tls_mkch c 0 |
| 170 | ret 0 |
| 171 | end |
| 172 | if ne fz_detch c 0 |
| 173 | ret 0 |
| 174 | end |
| 175 | n = add tls_gn c add TLWB 6 3 4 |
| 176 | fz_dump c n |
| 177 | sha256_update th part c add TLWB 5 n |
| 178 | for i ulong TLCSZ |
| 179 | put m i get c i |
| 180 | end |
| 181 | for i ulong 40 |
| 182 | put t2 i get th i |
| 183 | end |
| 184 | r = fz_be2 x 0 |
| 185 | if gt r sub len x 2 |
| 186 | r = sub len x 2 |
| 187 | end |
| 188 | f = fz_pre m t2 x r |
| 189 | if eq f 0 |
| 190 | ret 0 |
| 191 | end |
| 192 | fz_fdset c sub f 1 |
| 193 | e = tls_rdsh c th |
| 194 | if eq e 0 |
| 195 | e = tls_rdenc c st th |
| 196 | end |
| 197 | ret e |
| 198 | end |
| 199 | |
| 200 | fn main to int |
| 201 | c = new uchar TLCSZ 0 |
| 202 | m = new uchar TLCSZ 0 |
| 203 | in = new uchar FZCAP 0 |
| 204 | n = fz_read in |
| 205 | if lt n 2 |
| 206 | ret 0 |
| 207 | end |
| 208 | r = fz_flrun c m part in 0 n |
| 209 | fz_say r |
| 210 | ret 0 |
| 211 | end |