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 / reuse / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: OMEGA-CRYPTO-REUSE-1 gates 1 and 2 (issue #639), keystream reuse on synthetic inputs. |
| 3 | note Built as `om2`, om/tls/chacha.om, om/tls/aesgcm.om, om/tls/gcmsiv.om, driver.om (om/tls/test/reuse/run.sh). |
| 4 | note Stdin: lines; empty lines and lines starting with # are skipped; exactly one line |
| 5 | note X HEX HEX = 160 hex digits, the 80 bytes of P1 XOR P2, computed outside this program |
| 6 | note All inputs are synthetic (no real key, no real payload), 80 bytes so that ChaCha20 crosses a |
| 7 | note 64-byte block and AES-GCM uses five counter blocks: |
| 8 | note P1[i] = (7 i + 3) mod 256 P2[i] = (13 i + 101) mod 256 i = 0..79 |
| 9 | note K1[i] = (29 i + 17) mod 256 K2[i] = (31 i + 5) mod 256 synthetic keystreams |
| 10 | note ChaCha20-Poly1305 kn = key || nonce, byte i = i (44 bytes); the distinct-nonce kn flips bit 0 |
| 11 | note of the last nonce byte. AES-128-GCM and AES-128-GCM-SIV kn byte i = (3 i + 1) mod 256 |
| 12 | note (28 bytes), same flip. Mode 2 derives its keystream from the tag, so a repeated nonce |
| 13 | note does not disclose P1 XOR P2 (RFC 8452). |
| 14 | note aad = "abcd". Tags (16 bytes) are never part of the reuse comparison. |
| 15 | note The detector kreuse says "reuse" when C1 XOR C2 equals P1 XOR P2 over the ciphertext bytes: |
| 16 | note with both plaintexts known, equal XORs mean the two keystreams were equal there. A primitive |
| 17 | note API accepts a repeated nonce (RFC 8439 4, SP 800-38D 8): nothing here expects it to refuse. |
| 18 | note Checks (numbered as printed): |
| 19 | note 1 synthetic: (P1^K1) ^ (P2^K1) == X (independent expected bytes) |
| 20 | note 2 synthetic: detector flags reuse for one keystream K1 |
| 21 | note 3 synthetic: detector does not flag K1 against K2 (distinct-keystream control) |
| 22 | note then for m = 0 (ChaCha20-Poly1305, checks 4..11) and m = 1 (AES-128-GCM, checks 12..19): |
| 23 | note +0 seal P1 and P2 under one kn, and P2 under the distinct-nonce kn: all three return 0 |
| 24 | note +1 ct1 ^ ct2 == X over the 80 ciphertext bytes (payload XOR disclosed) |
| 25 | note +2 detector flags reuse for the repeated nonce |
| 26 | note +3 detector does not flag the distinct nonce (negative control) |
| 27 | note +4 the two tags under the repeated nonce differ (why tags are excluded) |
| 28 | note +5 open(ct1) under the repeated nonce returns 0 and P1 (authentication still holds) |
| 29 | note +6 ct2 with one ciphertext bit flipped: open returns 1 and zeroes the data part |
| 30 | note +7 the distinct-nonce sealed P2 opened with the first kn returns 1 (wrong nonce refused) |
| 31 | note then mode 2 (AES-128-GCM-SIV, checks 20..27): |
| 32 | note 20 the three seals return 0 |
| 33 | note 21 mode-2 ciphertext of P1 differs from the mode-1 ciphertext of the same kn and P1 |
| 34 | note 22 ct1 ^ ct2 != X over the 80 ciphertext bytes (nonce misuse does not disclose XOR) |
| 35 | note 23 detector does not flag the repeated nonce |
| 36 | note 24 the two tags under the repeated nonce differ |
| 37 | note 25 open(ct1) returns 0 and restores P1 |
| 38 | note 26 ct2 with one other ciphertext bit flipped: open returns 1 and zeroes the data |
| 39 | note 27 the distinct-nonce sealed P2 opened with the first kn returns 1 |
| 40 | note Prints "MISMATCH check N" per failed check, "BAD input" for a malformed X line, then |
| 41 | note "reuse driver: C checks, F failed". Exit 0 only when F is 0 and C is 27; 1 otherwise; 2 when |
| 42 | note stdin cannot be read or is over 4096 bytes. |
| 43 | |
| 44 | const RLEN ulong 80 |
| 45 | const SLEN ulong 96 |
| 46 | note offsets in the work buffer w (one buffer keeps main within 16 live values) |
| 47 | const OP1 ulong 0 |
| 48 | const OP2 ulong 96 |
| 49 | const OK1 ulong 192 |
| 50 | const OK2 ulong 288 |
| 51 | const OC1 ulong 384 |
| 52 | const OC2 ulong 480 |
| 53 | const OC3 ulong 576 |
| 54 | const OT ulong 672 |
| 55 | const OX ulong 768 |
| 56 | const ON1 ulong 864 |
| 57 | const ON2 ulong 928 |
| 58 | const OAD ulong 992 |
| 59 | const WLEN ulong 1024 |
| 60 | |
| 61 | global inp uchar 4096 |
| 62 | global st ulong 4 |
| 63 | global ob uchar 32 |
| 64 | global one uchar 1 |
| 65 | text tmis "MISMATCH check " |
| 66 | text tbad "BAD input\n" |
| 67 | text tsum "reuse driver: " |
| 68 | text tchk " checks, " |
| 69 | text tfld " failed\n" |
| 70 | text tnl "\n" |
| 71 | text tio "reuse driver: cannot read input or input over 4096 bytes\n" |
| 72 | |
| 73 | note st: 0 input length, 1 checks, 2 failed |
| 74 | |
| 75 | fn dslurp to int |
| 76 | t = ulong 0 |
| 77 | while true bound 4097 |
| 78 | room = sub len inp t |
| 79 | if eq room 0 |
| 80 | r = read 0 one 0 1 |
| 81 | if ne r 0 |
| 82 | ret 2 |
| 83 | end |
| 84 | break |
| 85 | end |
| 86 | r = read 0 inp t room |
| 87 | if lt r 0 |
| 88 | ret 2 |
| 89 | end |
| 90 | if eq r 0 |
| 91 | break |
| 92 | end |
| 93 | t = add t ulong r |
| 94 | end |
| 95 | put st 0 t |
| 96 | ret 0 |
| 97 | end |
| 98 | |
| 99 | fn dhexv c ulong to ulong |
| 100 | if ge c 48 |
| 101 | if le c 57 |
| 102 | ret sub c 48 |
| 103 | end |
| 104 | end |
| 105 | if ge c 97 |
| 106 | if le c 102 |
| 107 | ret sub c 87 |
| 108 | end |
| 109 | end |
| 110 | if ge c 65 |
| 111 | if le c 70 |
| 112 | ret sub c 55 |
| 113 | end |
| 114 | end |
| 115 | ret 16 |
| 116 | end |
| 117 | |
| 118 | note find the one "X " line and decode its 160 hex digits into x; 0 ok, 1 malformed or not one line |
| 119 | fn dparse x edit uchar * to ulong |
| 120 | n = get st 0 |
| 121 | p = ulong 0 |
| 122 | seen = ulong 0 |
| 123 | bad = ulong 0 |
| 124 | while lt p n bound 4097 |
| 125 | c = ulong get inp p |
| 126 | q = p |
| 127 | while lt q n bound 4097 |
| 128 | if eq get inp q 10 |
| 129 | break |
| 130 | end |
| 131 | q = add q 1 |
| 132 | end |
| 133 | if eq c 88 |
| 134 | seen = add seen 1 |
| 135 | bad = or bad dxline x p q |
| 136 | else |
| 137 | if and ne c 35 ne q p |
| 138 | bad = 1 |
| 139 | end |
| 140 | end |
| 141 | p = add q 1 |
| 142 | end |
| 143 | if ne seen 1 |
| 144 | bad = 1 |
| 145 | end |
| 146 | ret bad |
| 147 | end |
| 148 | |
| 149 | note decode the X line inp[p .. q) into x: 0 ok, 1 malformed |
| 150 | fn dxline x edit uchar * p ulong q ulong to ulong |
| 151 | if ne sub q p 162 |
| 152 | ret 1 |
| 153 | end |
| 154 | if ne get inp add p 1 32 |
| 155 | ret 1 |
| 156 | end |
| 157 | bad = ulong 0 |
| 158 | for i ulong RLEN |
| 159 | o = add p mul i 2 |
| 160 | hi = dhexv ulong get inp add o 2 |
| 161 | lo = dhexv ulong get inp add o 3 |
| 162 | if or gt hi 15 gt lo 15 |
| 163 | bad = 1 |
| 164 | else |
| 165 | put x i uchar or shl hi 4 lo |
| 166 | end |
| 167 | end |
| 168 | ret bad |
| 169 | end |
| 170 | |
| 171 | fn dprn v ulong to void |
| 172 | x = v |
| 173 | k = ulong 0 |
| 174 | while true bound 21 |
| 175 | put ob sub 31 k uchar add 48 rem x 10 |
| 176 | k = add k 1 |
| 177 | x = div x 10 |
| 178 | if eq x 0 |
| 179 | break |
| 180 | end |
| 181 | end |
| 182 | r = write 1 ob sub 32 k k |
| 183 | end |
| 184 | |
| 185 | fn dchk ok bool id ulong to void |
| 186 | put st 1 add get st 1 1 |
| 187 | if not ok |
| 188 | put st 2 add get st 2 1 |
| 189 | r = write 1 tmis 0 len tmis |
| 190 | dprn id |
| 191 | r = write 1 tnl 0 1 |
| 192 | end |
| 193 | end |
| 194 | |
| 195 | note the reuse detector over the work buffer: true when w[a..] ^ w[b..] == w[p..] ^ w[q..] over the |
| 196 | note first n bytes (a, b ciphertexts; p, q the known plaintexts) |
| 197 | fn kreuse w view uchar WLEN a ulong b ulong p ulong q ulong n ulong to bool |
| 198 | acc = uchar 0 |
| 199 | for i ulong n |
| 200 | c = xor get w add a i get w add b i |
| 201 | acc = or acc xor c xor get w add p i get w add q i |
| 202 | end |
| 203 | ret not ne acc 0 |
| 204 | end |
| 205 | |
| 206 | note true when w[a..] ^ w[b..] == w[x..] over the first n bytes |
| 207 | fn dxeq w view uchar WLEN a ulong b ulong x ulong n ulong to bool |
| 208 | acc = uchar 0 |
| 209 | for i ulong n |
| 210 | acc = or acc xor xor get w add a i get w add b i get w add x i |
| 211 | end |
| 212 | ret eq acc 0 |
| 213 | end |
| 214 | |
| 215 | note true when w[a..] and w[b..] differ somewhere in their first n bytes |
| 216 | fn ddiff w view uchar WLEN a ulong b ulong n ulong to bool |
| 217 | acc = uchar 0 |
| 218 | for i ulong n |
| 219 | acc = or acc xor get w add a i get w add b i |
| 220 | end |
| 221 | ret ne acc 0 |
| 222 | end |
| 223 | |
| 224 | note copy w[s .. s + n) to w[d .. d + n) |
| 225 | fn dmove w edit uchar WLEN d ulong s ulong n ulong to void |
| 226 | for i ulong n |
| 227 | put w add d i get w add s i |
| 228 | end |
| 229 | end |
| 230 | |
| 231 | note true when the first n bytes of a are 0 |
| 232 | fn dzero a view uchar * n ulong to bool |
| 233 | acc = uchar 0 |
| 234 | for i ulong n |
| 235 | acc = or acc get a i |
| 236 | end |
| 237 | ret eq acc 0 |
| 238 | end |
| 239 | |
| 240 | fn dcopy d edit uchar * s view uchar * to void |
| 241 | for i ulong len s |
| 242 | put d i get s i |
| 243 | end |
| 244 | end |
| 245 | |
| 246 | note synthetic plaintexts, keystreams, aad and the kn pair of mode m (0 ChaCha, 1 AES-GCM, 2 AES-GCM-SIV) |
| 247 | fn dgen w edit uchar WLEN m ulong to ulong |
| 248 | for i ulong SLEN |
| 249 | put w add OP1 i 0 |
| 250 | put w add OP2 i 0 |
| 251 | end |
| 252 | for i ulong RLEN |
| 253 | put w add OP1 i uchar rem add mul 7 i 3 256 |
| 254 | put w add OP2 i uchar rem add mul 13 i 101 256 |
| 255 | put w add OK1 i uchar rem add mul 29 i 17 256 |
| 256 | put w add OK2 i uchar rem add mul 31 i 5 256 |
| 257 | end |
| 258 | put w add OAD 0 97 |
| 259 | put w add OAD 1 98 |
| 260 | put w add OAD 2 99 |
| 261 | put w add OAD 3 100 |
| 262 | nl = ulong 44 |
| 263 | if eq m 1 |
| 264 | nl = 28 |
| 265 | end |
| 266 | if eq m 2 |
| 267 | nl = 28 |
| 268 | end |
| 269 | for i ulong nl |
| 270 | v = uchar i |
| 271 | if eq m 1 |
| 272 | v = uchar rem add mul 3 i 1 256 |
| 273 | end |
| 274 | if eq m 2 |
| 275 | v = uchar rem add mul 3 i 1 256 |
| 276 | end |
| 277 | put w add ON1 i v |
| 278 | put w add ON2 i v |
| 279 | end |
| 280 | last = add ON2 sub nl 1 |
| 281 | put w last xor get w last 1 |
| 282 | ret nl |
| 283 | end |
| 284 | |
| 285 | note seal / open w[o .. o + SLEN) in place under kn = w[k .. k + nl) and aad = w[OAD .. OAD + 4); |
| 286 | note kn and aad are copied to local arrays first (one array may not be passed twice in a call) |
| 287 | fn dseal m ulong w edit uchar WLEN k ulong nl ulong o ulong to ulong |
| 288 | kb = new uchar 44 0 |
| 289 | ad = new uchar 4 0 |
| 290 | dcopy part kb 0 nl part w k nl |
| 291 | dcopy ad part w OAD 4 |
| 292 | if eq m 2 |
| 293 | ret aes_gcmsiv_seal part kb 0 nl ad part w o SLEN |
| 294 | end |
| 295 | if eq m 1 |
| 296 | ret aead_aes128gcm_seal part kb 0 nl ad part w o SLEN |
| 297 | end |
| 298 | ret aead_chacha_seal part kb 0 nl ad part w o SLEN |
| 299 | end |
| 300 | |
| 301 | fn dopen m ulong w edit uchar WLEN k ulong nl ulong o ulong to ulong |
| 302 | kb = new uchar 44 0 |
| 303 | ad = new uchar 4 0 |
| 304 | dcopy part kb 0 nl part w k nl |
| 305 | dcopy ad part w OAD 4 |
| 306 | if eq m 2 |
| 307 | ret aes_gcmsiv_open part kb 0 nl ad part w o SLEN |
| 308 | end |
| 309 | if eq m 1 |
| 310 | ret aead_aes128gcm_open part kb 0 nl ad part w o SLEN |
| 311 | end |
| 312 | ret aead_chacha_open part kb 0 nl ad part w o SLEN |
| 313 | end |
| 314 | |
| 315 | note gate 1: one synthetic keystream reused, then two distinct ones |
| 316 | fn dsynth w edit uchar WLEN to void |
| 317 | for i ulong RLEN |
| 318 | put w add OC1 i xor get w add OP1 i get w add OK1 i |
| 319 | put w add OC2 i xor get w add OP2 i get w add OK1 i |
| 320 | put w add OC3 i xor get w add OP2 i get w add OK2 i |
| 321 | end |
| 322 | dchk dxeq w OC1 OC2 OX RLEN 1 |
| 323 | dchk kreuse w OC1 OC2 OP1 OP2 RLEN 2 |
| 324 | dchk not kreuse w OC1 OC3 OP1 OP2 RLEN 3 |
| 325 | end |
| 326 | |
| 327 | note gate 2 for mode m, checks b .. b + 7 |
| 328 | fn daead w edit uchar WLEN m ulong b ulong to void |
| 329 | nl = dgen w m |
| 330 | dmove w OC1 OP1 SLEN |
| 331 | dmove w OC2 OP2 SLEN |
| 332 | dmove w OC3 OP2 SLEN |
| 333 | r1 = dseal m w ON1 nl OC1 |
| 334 | r2 = dseal m w ON1 nl OC2 |
| 335 | r3 = dseal m w ON2 nl OC3 |
| 336 | dchk eq or or r1 r2 r3 0 b |
| 337 | dchk dxeq w OC1 OC2 OX RLEN add b 1 |
| 338 | dchk kreuse w OC1 OC2 OP1 OP2 RLEN add b 2 |
| 339 | dchk not kreuse w OC1 OC3 OP1 OP2 RLEN add b 3 |
| 340 | dchk ddiff w add OC1 RLEN add OC2 RLEN 16 add b 4 |
| 341 | dmove w OT OC1 SLEN |
| 342 | r4 = dopen m w ON1 nl OT |
| 343 | dchk and eq r4 0 not ddiff w OT OP1 RLEN add b 5 |
| 344 | dmove w OT OC2 SLEN |
| 345 | put w add OT 5 xor get w add OT 5 1 |
| 346 | r5 = dopen m w ON1 nl OT |
| 347 | dchk and eq r5 1 dzero part w OT RLEN RLEN add b 6 |
| 348 | dmove w OT OC3 SLEN |
| 349 | r6 = dopen m w ON1 nl OT |
| 350 | dchk eq r6 1 add b 7 |
| 351 | end |
| 352 | |
| 353 | note gate 3: AES-128-GCM-SIV. Check 21 is the siv-as-other-mode site: only the first seal is mode 2. |
| 354 | fn dsiv_seals w edit uchar WLEN nl ulong to void |
| 355 | dmove w OC1 OP1 SLEN |
| 356 | dmove w OC2 OP2 SLEN |
| 357 | dmove w OC3 OP2 SLEN |
| 358 | r1 = dseal 2 w ON1 nl OC1 |
| 359 | r2 = dseal 2 w ON1 nl OC2 |
| 360 | r3 = dseal 2 w ON2 nl OC3 |
| 361 | dchk eq or or r1 r2 r3 0 20 |
| 362 | end |
| 363 | |
| 364 | fn dsiv_notgcm w edit uchar WLEN nl ulong to void |
| 365 | dmove w OT OC1 SLEN |
| 366 | dmove w OC1 OP1 SLEN |
| 367 | r1 = dseal 1 w ON1 nl OC1 |
| 368 | dchk ddiff w OT OC1 RLEN 21 |
| 369 | dmove w OC1 OP1 SLEN |
| 370 | r2 = dseal 2 w ON1 nl OC1 |
| 371 | end |
| 372 | |
| 373 | fn dsiv_misuse w edit uchar WLEN to void |
| 374 | dchk not dxeq w OC1 OC2 OX RLEN 22 |
| 375 | dchk not kreuse w OC1 OC2 OP1 OP2 RLEN 23 |
| 376 | dchk ddiff w add OC1 RLEN add OC2 RLEN 16 24 |
| 377 | end |
| 378 | |
| 379 | fn dsiv_auth w edit uchar WLEN nl ulong to void |
| 380 | dmove w OT OC1 SLEN |
| 381 | r4 = dopen 2 w ON1 nl OT |
| 382 | dchk and eq r4 0 not ddiff w OT OP1 RLEN 25 |
| 383 | dmove w OT OC2 SLEN |
| 384 | put w add OT 9 xor get w add OT 9 1 |
| 385 | r5 = dopen 2 w ON1 nl OT |
| 386 | dchk and eq r5 1 dzero part w OT RLEN RLEN 26 |
| 387 | dmove w OT OC3 SLEN |
| 388 | r6 = dopen 2 w ON1 nl OT |
| 389 | dchk eq r6 1 27 |
| 390 | end |
| 391 | |
| 392 | fn dsiv w edit uchar WLEN to void |
| 393 | nl = dgen w 2 |
| 394 | dsiv_seals w nl |
| 395 | dsiv_notgcm w nl |
| 396 | dsiv_misuse w |
| 397 | dsiv_auth w nl |
| 398 | end |
| 399 | |
| 400 | fn main to int |
| 401 | w = new uchar WLEN 0 |
| 402 | if ne dslurp 0 |
| 403 | r = write 2 tio 0 len tio |
| 404 | ret 2 |
| 405 | end |
| 406 | put st 1 0 |
| 407 | put st 2 0 |
| 408 | if ne dparse part w OX RLEN 0 |
| 409 | r = write 1 tbad 0 len tbad |
| 410 | put st 2 add get st 2 1 |
| 411 | end |
| 412 | nl = dgen w 0 |
| 413 | dsynth w |
| 414 | daead w 0 4 |
| 415 | daead w 1 12 |
| 416 | dsiv w |
| 417 | r = write 1 tsum 0 len tsum |
| 418 | dprn get st 1 |
| 419 | r = write 1 tchk 0 len tchk |
| 420 | dprn get st 2 |
| 421 | r = write 1 tfld 0 len tfld |
| 422 | if ne get st 2 0 |
| 423 | ret 1 |
| 424 | end |
| 425 | if ne get st 1 27 |
| 426 | ret 1 |
| 427 | end |
| 428 | ret 0 |
| 429 | end |