Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/om/tls/test/chacha/driver.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / om / tls / test / chacha / driver.om` 527 lines ยท 8525 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note driver.om: program-mode test driver for om/tls/chacha.om, built as `om2`, chacha.om, driver.om. 3 note Reads vector records from stdin (at most 16 MiB), one per line, fields separated by one space; 4 note hex fields are lowercase or uppercase hex, "-" for empty: 5 note Q IA IB IC ID STATE WANT chacha20_qr on 16 words (each 8 hex digits, big-endian as RFC prints) 6 note C KN CTR IN OUT CODE chacha20_xor KN CTR on a copy of IN; CODE 0: result == OUT; 7 note CODE 2: refused with 2 and the buffer untouched 8 note P KEY MSG TAG CODE poly1305_mac; CODE 0: tag == TAG; CODE 2: refused with 2 9 note A KN AAD PT CTTAG CODE CODE 0: seal(PT) == CTTAG and open(CTTAG) == 0 with PT; 10 note CODE 1: open(CTTAG) == 1 and the ciphertext part zeroed; 11 note CODE 2: seal and open both refuse with 2 and leave the buffer alone 12 note Lines that are empty or start with # are skipped. Prints "MISMATCH line L" (wrong result) or 13 note "BAD line L" (malformed record) per failure, then "chacha driver: C checks, F failed". 14 note Exit 0 only when F is 0 and C is above 0; exit 1 otherwise; 2 when stdin cannot be read. 15 16 global inp uchar `16777216` 17 global st ulong 12 18 global ob uchar 32 19 global one uchar 1 20 text tfail "MISMATCH line " 21 text tbad "BAD line " 22 text tsum "chacha driver: " 23 text tchk " checks, " 24 text tfld " failed\n" 25 text tnl "\n" 26 text tio "chacha driver: cannot read input or input over 16 MiB\n" 27 28 note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start 29 30 fn dslurp to int 31 t = ulong 0 32 while true bound 999999 33 room = sub len inp t 34 if eq room 0 35 r = read 0 one 0 1 36 if ne r 0 37 ret 2 38 end 39 break 40 end 41 r = read 0 inp t room 42 if lt r 0 43 ret 2 44 end 45 if eq r 0 46 break 47 end 48 t = add t ulong r 49 end 50 put st 0 t 51 ret 0 52 end 53 54 fn dpeek to ulong 55 c = get st 1 56 if ge c get st 0 57 ret 256 58 end 59 ret ulong get inp c 60 end 61 62 fn dadv to void 63 put st 1 add get st 1 1 64 end 65 66 fn dbad to void 67 put st 3 1 68 end 69 70 fn dhexv c ulong to ulong 71 if ge c 48 72 if le c 57 73 ret sub c 48 74 end 75 end 76 if ge c 97 77 if le c 102 78 ret sub c 87 79 end 80 end 81 if ge c 65 82 if le c 70 83 ret sub c 55 84 end 85 end 86 ret 16 87 end 88 89 fn dbyte to uchar 90 hi = dhexv dpeek 91 dadv 92 lo = dhexv dpeek 93 dadv 94 if gt hi 15 95 dbad 96 ret 0 97 end 98 if gt lo 15 99 dbad 100 ret 0 101 end 102 ret uchar or shl hi 4 lo 103 end 104 105 fn dspace to void 106 if ne dpeek 32 107 dbad 108 ret 109 end 110 dadv 111 end 112 113 note decimal number at the cursor (1 to 19 digits) 114 fn dnum to ulong 115 v = ulong 0 116 k = ulong 0 117 while true bound 21 118 c = dpeek 119 if lt c 48 120 break 121 end 122 if gt c 57 123 break 124 end 125 v = add mul v 10 sub c 48 126 k = add k 1 127 dadv 128 end 129 if eq k 0 130 dbad 131 end 132 if gt k 19 133 dbad 134 end 135 ret v 136 end 137 138 note hex field into a; returns the byte count ("-" is 0 bytes) 139 fn dhexf a edit uchar * to ulong 140 if eq dpeek 45 141 dadv 142 ret 0 143 end 144 n = ulong 0 145 while true bound 999999 146 c = dpeek 147 if eq c 32 148 break 149 end 150 if eq c 10 151 break 152 end 153 if eq c 13 154 break 155 end 156 if eq c 256 157 break 158 end 159 if ge n len a 160 dbad 161 ret n 162 end 163 put a n dbyte 164 n = add n 1 165 if eq get st 3 1 166 ret n 167 end 168 end 169 ret n 170 end 171 172 fn deor to void 173 if eq dpeek 13 174 dadv 175 end 176 c = dpeek 177 if eq c 10 178 dadv 179 ret 180 end 181 if ne c 256 182 dbad 183 end 184 end 185 186 fn dskip to void 187 while true bound 999999 188 c = dpeek 189 if eq c 256 190 ret 191 end 192 dadv 193 if eq c 10 194 ret 195 end 196 end 197 end 198 199 note 0 when a and b have the same length and bytes 200 fn dsame a view uchar * b view uchar * to ulong 201 if ne len a len b 202 ret 1 203 end 204 d = ulong 0 205 for i ulong len a 206 if ne get a i get b i 207 d = 1 208 end 209 end 210 ret d 211 end 212 213 note 0 when every byte of a is 0 214 fn dzero a view uchar * to ulong 215 d = ulong 0 216 for i ulong len a 217 if ne get a i 0 218 d = 1 219 end 220 end 221 ret d 222 end 223 224 fn dcopy d edit uchar * s view uchar * to void 225 for i ulong len s 226 put d i get s i 227 end 228 end 229 230 fn dprn v ulong to void 231 x = v 232 k = ulong 0 233 while true bound 21 234 put ob sub 31 k uchar add 48 rem x 10 235 k = add k 1 236 x = div x 10 237 if eq x 0 238 break 239 end 240 end 241 r = write 1 ob sub 32 k k 242 end 243 244 fn dmsg bad ulong ln ulong to void 245 if eq bad 1 246 r = write 1 tbad 0 len tbad 247 else 248 r = write 1 tfail 0 len tfail 249 end 250 dprn ln 251 r = write 1 tnl 0 1 252 end 253 254 note 16 big-endian words from 64 bytes 255 fn dwords w edit uint 16 b view uchar * to void 256 for i ulong 16 257 q = mul i 4 258 x = uint get b q 259 x = or shl x 8 uint get b add q 1 260 x = or shl x 8 uint get b add q 2 261 put w i or shl x 8 uint get b add q 3 262 end 263 end 264 265 note the library quarter round, called (not inlined) so drecq keeps its names in registers 266 fn dqr x edit uint 16 q ulong to void 267 chacha20_qr x q 268 end 269 270 note Q record; 0 pass, 1 mismatch, 2 malformed 271 fn drecq b edit uchar 65552 e edit uchar 65552 to ulong 272 x = new uint 16 0 273 y = new uint 16 0 274 dspace 275 ia = dnum 276 dspace 277 ib = dnum 278 dspace 279 ic = dnum 280 dspace 281 id = dnum 282 dspace 283 nb = dhexf b 284 dspace 285 nx = dhexf e 286 deor 287 if eq get st 3 1 288 ret 2 289 end 290 if ne nb 64 291 ret 2 292 end 293 if ne nx 64 294 ret 2 295 end 296 if gt or or or ia ib ic id 15 297 ret 2 298 end 299 dwords x b 300 dwords y e 301 dqr x or or or ia shl ib 8 shl ic 16 shl id 24 302 d = ulong 0 303 for i ulong 16 304 if ne get x i get y i 305 d = 1 306 end 307 end 308 ret d 309 end 310 311 note C record 312 fn drecc kn edit uchar 64 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong 313 dspace 314 put st 5 dhexf kn 315 dspace 316 ctr = dnum 317 dspace 318 nb = dhexf b 319 dspace 320 put st 8 dhexf e 321 dspace 322 put st 9 dnum 323 deor 324 if eq get st 3 1 325 ret 2 326 end 327 dcopy w part b 0 nb 328 r = chacha20_xor part kn 0 get st 5 ctr part w 0 nb 329 if ne r get st 9 330 ret 1 331 end 332 if eq r 0 333 ret dsame part w 0 nb part e 0 get st 8 334 end 335 ret dsame part w 0 nb part b 0 nb 336 end 337 338 note poly1305_mac on the parsed key (st 5 bytes) and message (st 7 bytes) into w[0..16) 339 fn dpmac kn edit uchar 64 b edit uchar 65552 w edit uchar 65552 to ulong 340 nk = get st 5 341 nb = get st 7 342 ret poly1305_mac part kn 0 nk part b 0 nb part w 0 16 343 end 344 345 note P record 346 fn drecp kn edit uchar 64 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong 347 dspace 348 put st 5 dhexf kn 349 dspace 350 put st 7 dhexf b 351 dspace 352 put st 8 dhexf e 353 dspace 354 put st 9 dnum 355 deor 356 if eq get st 3 1 357 ret 2 358 end 359 r = dpmac kn b w 360 if ne r get st 9 361 ret 1 362 end 363 if eq r 0 364 ret dsame part w 0 16 part e 0 get st 8 365 end 366 ret 0 367 end 368 369 note aead_chacha_seal / open on the parsed key (st 5 bytes), aad (st 6 bytes) and w[0..n) 370 fn dseal kn edit uchar 64 a edit uchar 65552 w edit uchar 65552 n ulong to ulong 371 nk = get st 5 372 na = get st 6 373 ret aead_chacha_seal part kn 0 nk part a 0 na part w 0 n 374 end 375 376 fn dopen kn edit uchar 64 a edit uchar 65552 w edit uchar 65552 n ulong to ulong 377 nk = get st 5 378 na = get st 6 379 ret aead_chacha_open part kn 0 nk part a 0 na part w 0 n 380 end 381 382 note A record, CODE 0; lengths in st 5 key, 6 aad, 7 plaintext, 8 ciphertext with tag 383 fn drecav kn edit uchar 64 a edit uchar 65552 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong 384 np = get st 7 385 nc = get st 8 386 if ne nc add np 16 387 ret 1 388 end 389 dcopy w part b 0 np 390 r = dseal kn a w nc 391 if ne r 0 392 ret 1 393 end 394 if ne dsame part w 0 nc part e 0 nc 0 395 ret 1 396 end 397 dcopy w part e 0 nc 398 r = dopen kn a w nc 399 if ne r 0 400 ret 1 401 end 402 ret dsame part w 0 np part b 0 np 403 end 404 405 note A record, CODE 1 or 2 (st 9) 406 fn drecai kn edit uchar 64 a edit uchar 65552 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong 407 code = get st 9 408 nc = get st 8 409 dcopy w part e 0 nc 410 r = dopen kn a w nc 411 if ne r code 412 ret 1 413 end 414 if eq code 1 415 ret dzero part w 0 sub nc 16 416 end 417 if ne dsame part w 0 nc part e 0 nc 0 418 ret 1 419 end 420 np = get st 7 421 dcopy w part b 0 np 422 r = dseal kn a w add np 16 423 if ne r 2 424 ret 1 425 end 426 ret dsame part w 0 np part b 0 np 427 end 428 429 note A record 430 fn dreca kn edit uchar 64 a edit uchar 65552 b edit uchar 65552 e edit uchar 65552 w edit uchar 65552 to ulong 431 dspace 432 put st 5 dhexf kn 433 dspace 434 put st 6 dhexf a 435 dspace 436 put st 7 dhexf b 437 dspace 438 put st 8 dhexf e 439 dspace 440 put st 9 dnum 441 deor 442 if eq get st 3 1 443 ret 2 444 end 445 if gt get st 9 2 446 ret 2 447 end 448 if gt get st 7 65536 449 ret 2 450 end 451 if eq get st 9 0 452 ret drecav kn a b e w 453 end 454 if and eq get st 9 1 lt get st 8 16 455 ret 2 456 end 457 ret drecai kn a b e w 458 end 459 460 fn main to int 461 kn = new uchar 64 0 462 a = new uchar 65552 0 463 b = new uchar 65552 0 464 e = new uchar 65552 0 465 w = new uchar 65552 0 466 if ne dslurp 0 467 r = write 2 tio 0 len tio 468 ret 2 469 end 470 put st 1 0 471 put st 2 0 472 chk = ulong 0 473 fl = ulong 0 474 res = ulong 0 475 while lt get st 1 get st 0 bound 999999 476 put st 2 add get st 2 1 477 ln = get st 2 478 put st 3 0 479 put st 4 get st 1 480 c = dpeek 481 if eq c 35 482 dskip 483 continue 484 end 485 if eq c 10 486 dadv 487 continue 488 end 489 dadv 490 res = 2 491 if eq c 81 492 res = drecq b e 493 end 494 if eq c 67 495 res = drecc kn b e w 496 end 497 if eq c 80 498 res = drecp kn b e w 499 end 500 if eq c 65 501 res = dreca kn a b e w 502 end 503 chk = add chk 1 504 if ne res 0 505 fl = add fl 1 506 if eq res 2 507 dmsg 1 ln 508 put st 1 get st 4 509 dskip 510 else 511 dmsg 0 ln 512 end 513 end 514 end 515 r = write 1 tsum 0 len tsum 516 dprn chk 517 r = write 1 tchk 0 len tchk 518 dprn fl 519 r = write 1 tfld 0 len tfld 520 if ne fl 0 521 ret 1 522 end 523 if eq chk 0 524 ret 1 525 end 526 ret 0 527 end