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 / aesgcm / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: program-mode test driver for om/tls/aesgcm.om (concatenated after `om2` and the |
| 3 | note library). Reads records from stdin (at most 16 MiB), one per line, fields separated by one space, |
| 4 | note hex fields in lowercase or uppercase, "-" for an empty field: |
| 5 | note B KEY PT CT FIPS 197 single block: AES-KEY(PT) = CT (16-byte blocks) |
| 6 | note E KEY IV AAD PT CT TAG CAVP encrypt vector: seal(PT) = CT || TAG, open(CT || TAG) |
| 7 | note = PT, and open with the last tag byte flipped returns 1 and |
| 8 | note zeroes the data area (tamper control) |
| 9 | note D KEY IV AAD CT TAG PT CAVP decrypt vector: open(CT || TAG) = PT; PT is FAIL when |
| 10 | note open must return 1 and zero the data area |
| 11 | note W TCID V|I KEY IV AAD MSG CT TAG Wycheproof: V as E; I: open returns 1 and zeroes the data |
| 12 | note area, or, for an empty IV, seal and open both return 2 |
| 13 | note X CTL KIALEN BUFLEN WANT seal and open with ctl on zero arrays both return WANT |
| 14 | note Y KNLEN AADLEN BUFLEN WANT aead_aes128gcm_seal/open on zero arrays both return WANT |
| 15 | note T N seal then open N zero bytes (N <= 1048576, key/IV fixed); |
| 16 | note prints "aesgcm speed bytes=N seal_ns=S open_ns=O seal_MBps=A |
| 17 | note open_MBps=B" (MB = 10^6 bytes) |
| 18 | note Every E, D and W vector whose shape is AES-128, 12-byte IV, 16-byte tag also runs through |
| 19 | note aead_aes128gcm_seal/open (same expectations, tamper control included). |
| 20 | note Lines that are empty or start with # are skipped; a trailing carriage return is allowed. |
| 21 | note Prints "MISMATCH line L" (wrong result) or "BAD line L" (malformed record) per failure, then |
| 22 | note "aesgcm driver: C checks, F failed". Exit 0 only when F is 0 and C is above 0; exit 1 otherwise; |
| 23 | note exit 2 when the input cannot be read or exceeds 16 MiB. |
| 24 | |
| 25 | global inp uchar 16777216 |
| 26 | global st ulong 20 |
| 27 | global ob uchar 32 |
| 28 | global one uchar 1 |
| 29 | text tfail "MISMATCH line " |
| 30 | text tbad "BAD line " |
| 31 | text tsum "aesgcm driver: " |
| 32 | text tchk " checks, " |
| 33 | text tfld " failed\n" |
| 34 | text tnl "\n" |
| 35 | text tio "aesgcm driver: cannot read input or input over 16 MiB\n" |
| 36 | text tspd "aesgcm speed bytes=" |
| 37 | text tsns " seal_ns=" |
| 38 | text tons " open_ns=" |
| 39 | text tsmb " seal_MBps=" |
| 40 | text tomb " open_MBps=" |
| 41 | |
| 42 | note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start, |
| 43 | note 5 key bytes, 6 iv bytes, 7 aad bytes, 8 plaintext bytes, 9 ciphertext bytes, 10 tag bytes, |
| 44 | note 11 expected-failure flag (D FAIL, W I), 12 checks, 13 failures, 14 key||nonce bytes for dacall |
| 45 | |
| 46 | fn dslurp to int |
| 47 | t = ulong 0 |
| 48 | while true bound 999999 |
| 49 | room = sub len inp t |
| 50 | if eq room 0 |
| 51 | r = read 0 one 0 1 |
| 52 | if ne r 0 |
| 53 | ret 2 |
| 54 | end |
| 55 | break |
| 56 | end |
| 57 | r = read 0 inp t room |
| 58 | if lt r 0 |
| 59 | ret 2 |
| 60 | end |
| 61 | if eq r 0 |
| 62 | break |
| 63 | end |
| 64 | t = add t ulong r |
| 65 | end |
| 66 | put st 0 t |
| 67 | ret 0 |
| 68 | end |
| 69 | |
| 70 | fn dpeek to ulong |
| 71 | c = get st 1 |
| 72 | if ge c get st 0 |
| 73 | ret 256 |
| 74 | end |
| 75 | ret ulong get inp c |
| 76 | end |
| 77 | |
| 78 | fn dadv to void |
| 79 | put st 1 add get st 1 1 |
| 80 | end |
| 81 | |
| 82 | fn dbad to void |
| 83 | put st 3 1 |
| 84 | end |
| 85 | |
| 86 | fn dhexv c ulong to ulong |
| 87 | if ge c 48 |
| 88 | if le c 57 |
| 89 | ret sub c 48 |
| 90 | end |
| 91 | end |
| 92 | if ge c 97 |
| 93 | if le c 102 |
| 94 | ret sub c 87 |
| 95 | end |
| 96 | end |
| 97 | if ge c 65 |
| 98 | if le c 70 |
| 99 | ret sub c 55 |
| 100 | end |
| 101 | end |
| 102 | ret 16 |
| 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 a decimal number at the cursor (1 to 9 digits) |
| 114 | fn dnum to ulong |
| 115 | v = ulong 0 |
| 116 | k = ulong 0 |
| 117 | while true bound 10 |
| 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 9 |
| 133 | dbad |
| 134 | end |
| 135 | ret v |
| 136 | end |
| 137 | |
| 138 | note a hex field at the cursor into a[o ..); the byte count ("-" is 0 bytes) |
| 139 | fn dhexf a edit uchar 65536 o ulong to ulong |
| 140 | if eq dpeek 45 |
| 141 | dadv |
| 142 | ret 0 |
| 143 | end |
| 144 | n = ulong 0 |
| 145 | while true bound 65537 |
| 146 | hi = dhexv dpeek |
| 147 | if gt hi 15 |
| 148 | break |
| 149 | end |
| 150 | dadv |
| 151 | lo = dhexv dpeek |
| 152 | if gt lo 15 |
| 153 | dbad |
| 154 | break |
| 155 | end |
| 156 | dadv |
| 157 | if ge add o n 65536 |
| 158 | dbad |
| 159 | break |
| 160 | end |
| 161 | put a add o n uchar or shl hi 4 lo |
| 162 | n = add n 1 |
| 163 | end |
| 164 | if eq n 0 |
| 165 | dbad |
| 166 | end |
| 167 | ret n |
| 168 | end |
| 169 | |
| 170 | fn deor to void |
| 171 | if eq dpeek 13 |
| 172 | dadv |
| 173 | end |
| 174 | c = dpeek |
| 175 | if eq c 10 |
| 176 | dadv |
| 177 | ret |
| 178 | end |
| 179 | if ne c 256 |
| 180 | dbad |
| 181 | end |
| 182 | end |
| 183 | |
| 184 | fn dskip to void |
| 185 | while true bound 999999 |
| 186 | c = dpeek |
| 187 | if eq c 256 |
| 188 | ret |
| 189 | end |
| 190 | dadv |
| 191 | if eq c 10 |
| 192 | ret |
| 193 | end |
| 194 | end |
| 195 | end |
| 196 | |
| 197 | note 0 when a[0..n) = b[0..n) |
| 198 | fn dcmp a view uchar 65536 b view uchar 65536 n ulong to ulong |
| 199 | d = ulong 0 |
| 200 | for i ulong n |
| 201 | if ne get a i get b i |
| 202 | d = 1 |
| 203 | end |
| 204 | end |
| 205 | ret d |
| 206 | end |
| 207 | |
| 208 | note 0 when a[0..n) is all zero |
| 209 | fn dzero a view uchar 65536 n ulong to ulong |
| 210 | d = ulong 0 |
| 211 | for i ulong n |
| 212 | if ne get a i 0 |
| 213 | d = 1 |
| 214 | end |
| 215 | end |
| 216 | ret d |
| 217 | end |
| 218 | |
| 219 | fn dcopy a edit uchar 65536 b view uchar 65536 n ulong to void |
| 220 | for i ulong n |
| 221 | put a i get b i |
| 222 | end |
| 223 | end |
| 224 | |
| 225 | fn dprn v ulong to void |
| 226 | x = v |
| 227 | k = ulong 0 |
| 228 | while true bound 21 |
| 229 | put ob sub 31 k uchar add 48 rem x 10 |
| 230 | k = add k 1 |
| 231 | x = div x 10 |
| 232 | if eq x 0 |
| 233 | break |
| 234 | end |
| 235 | end |
| 236 | r = write 1 ob sub 32 k k |
| 237 | end |
| 238 | |
| 239 | fn dmsg bad ulong ln ulong to void |
| 240 | if eq bad 1 |
| 241 | r = write 1 tbad 0 len tbad |
| 242 | else |
| 243 | r = write 1 tfail 0 len tfail |
| 244 | end |
| 245 | dprn ln |
| 246 | r = write 1 tnl 0 1 |
| 247 | end |
| 248 | |
| 249 | note ctl of the current record: klen + 256 tlen + 65536 ivlen |
| 250 | fn dctl to ulong |
| 251 | ret or or get st 5 shl get st 10 8 shl get st 6 16 |
| 252 | end |
| 253 | |
| 254 | note key || iv || aad length |
| 255 | fn dka to ulong |
| 256 | ret add add get st 5 get st 6 get st 7 |
| 257 | end |
| 258 | |
| 259 | note data || tag length |
| 260 | fn dbn to ulong |
| 261 | ret add get st 8 get st 10 |
| 262 | end |
| 263 | |
| 264 | note 1 when the record has the TLS shape (AES-128, 12-byte IV, 16-byte tag) |
| 265 | fn dtls to ulong |
| 266 | if ne get st 5 16 |
| 267 | ret 0 |
| 268 | end |
| 269 | if ne get st 6 12 |
| 270 | ret 0 |
| 271 | end |
| 272 | if ne get st 10 16 |
| 273 | ret 0 |
| 274 | end |
| 275 | ret 1 |
| 276 | end |
| 277 | |
| 278 | note seal pt into buf; 0 when it returns 0 and buf = ex (data || tag) |
| 279 | fn dseal kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 to ulong |
| 280 | n = dbn |
| 281 | dcopy buf pt get st 8 |
| 282 | c = dctl |
| 283 | a = dka |
| 284 | r = aes_gcm_seal c part kia 0 a part buf 0 n |
| 285 | if ne r 0 |
| 286 | ret 1 |
| 287 | end |
| 288 | ret dcmp buf ex n |
| 289 | end |
| 290 | |
| 291 | note open buf (data || tag); 0 when it returns 0 and buf[0..pl) = pt |
| 292 | fn dopen kia view uchar 65536 pt view uchar 65536 buf edit uchar 65536 to ulong |
| 293 | c = dctl |
| 294 | a = dka |
| 295 | n = dbn |
| 296 | r = aes_gcm_open c part kia 0 a part buf 0 n |
| 297 | if ne r 0 |
| 298 | ret 1 |
| 299 | end |
| 300 | ret dcmp buf pt get st 8 |
| 301 | end |
| 302 | |
| 303 | note open buf; 0 when it returns want and, for want 1, the data area is zero |
| 304 | fn dfail kia view uchar 65536 buf edit uchar 65536 want ulong to ulong |
| 305 | c = dctl |
| 306 | a = dka |
| 307 | n = dbn |
| 308 | r = aes_gcm_open c part kia 0 a part buf 0 n |
| 309 | if ne r want |
| 310 | ret 1 |
| 311 | end |
| 312 | if eq want 1 |
| 313 | ret dzero buf get st 8 |
| 314 | end |
| 315 | ret 0 |
| 316 | end |
| 317 | |
| 318 | note tamper control: ex with its last byte flipped must be refused (1, data area zero) |
| 319 | fn dtamp kia view uchar 65536 ex view uchar 65536 buf edit uchar 65536 to ulong |
| 320 | n = dbn |
| 321 | dcopy buf ex n |
| 322 | put buf sub n 1 xor get buf sub n 1 1 |
| 323 | ret dfail kia buf 1 |
| 324 | end |
| 325 | |
| 326 | note aad of the record (kia after key and iv) into aa |
| 327 | fn daad kia view uchar 65536 aa edit uchar 65536 to void |
| 328 | o = add get st 5 get st 6 |
| 329 | for i ulong get st 7 |
| 330 | put aa i get kia add o i |
| 331 | end |
| 332 | end |
| 333 | |
| 334 | note aead_aes128gcm_open (o 1) or _seal (o 0) on buf[0 .. dbn) with kn = kia[0 .. st 14) and aad |
| 335 | note aa[0 .. st 7) |
| 336 | fn dacall kia view uchar 65536 aa view uchar 65536 buf edit uchar 65536 o ulong to ulong |
| 337 | kn = get st 14 |
| 338 | al = get st 7 |
| 339 | bl = dbn |
| 340 | if eq o 0 |
| 341 | ret aead_aes128gcm_seal part kia 0 kn part aa 0 al part buf 0 bl |
| 342 | end |
| 343 | ret aead_aes128gcm_open part kia 0 kn part aa 0 al part buf 0 bl |
| 344 | end |
| 345 | |
| 346 | note aead_aes128gcm_open on buf; 0 when it returns want and, for 0, buf = pt, for 1, zero data |
| 347 | fn daopen kia view uchar 65536 pt view uchar 65536 buf edit uchar 65536 aa view uchar 65536 want ulong to ulong |
| 348 | r = dacall kia aa buf 1 |
| 349 | if ne r want |
| 350 | ret 1 |
| 351 | end |
| 352 | if eq want 0 |
| 353 | ret dcmp buf pt get st 8 |
| 354 | end |
| 355 | ret dzero buf get st 8 |
| 356 | end |
| 357 | |
| 358 | note the TLS-shaped encrypt checks through aead_aes128gcm_*: seal, open, tamper |
| 359 | fn daseal kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 360 | daad kia aa |
| 361 | n = dbn |
| 362 | dcopy buf pt get st 8 |
| 363 | f = ulong 0 |
| 364 | if ne dacall kia aa buf 0 0 |
| 365 | f = 1 |
| 366 | end |
| 367 | f = or f dcmp buf ex n |
| 368 | f = or f daopen kia pt buf aa 0 |
| 369 | dcopy buf ex n |
| 370 | put buf sub n 1 xor get buf sub n 1 1 |
| 371 | f = or f daopen kia pt buf aa 1 |
| 372 | ret f |
| 373 | end |
| 374 | |
| 375 | note an encrypt-direction vector (E, W V): seal, open, tamper, and the TLS path |
| 376 | fn dvalid kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 377 | if ne get st 8 get st 9 |
| 378 | dbad |
| 379 | ret 2 |
| 380 | end |
| 381 | f = dseal kia pt ex buf |
| 382 | dcopy buf ex dbn |
| 383 | f = or f dopen kia pt buf |
| 384 | f = or f dtamp kia ex buf |
| 385 | if eq dtls 1 |
| 386 | f = or f daseal kia pt ex buf aa |
| 387 | end |
| 388 | ret f |
| 389 | end |
| 390 | |
| 391 | note a vector that must be refused (D FAIL, W I) |
| 392 | fn dinval kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 393 | n = dbn |
| 394 | dcopy buf ex n |
| 395 | if eq get st 6 0 |
| 396 | f = dfail kia buf 2 |
| 397 | dcopy buf ex n |
| 398 | c = dctl |
| 399 | a = dka |
| 400 | if ne aes_gcm_seal c part kia 0 a part buf 0 n 2 |
| 401 | f = 1 |
| 402 | end |
| 403 | ret f |
| 404 | end |
| 405 | f = dfail kia buf 1 |
| 406 | if eq dtls 1 |
| 407 | daad kia aa |
| 408 | dcopy buf ex n |
| 409 | f = or f daopen kia pt buf aa 1 |
| 410 | end |
| 411 | ret f |
| 412 | end |
| 413 | |
| 414 | note a decrypt vector with a plaintext (D) |
| 415 | fn ddec kia view uchar 65536 pt view uchar 65536 ex view uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 416 | n = dbn |
| 417 | dcopy buf ex n |
| 418 | f = dopen kia pt buf |
| 419 | if eq dtls 1 |
| 420 | daad kia aa |
| 421 | dcopy buf ex n |
| 422 | f = or f daopen kia pt buf aa 0 |
| 423 | end |
| 424 | ret f |
| 425 | end |
| 426 | |
| 427 | note key, iv and aad fields into kia |
| 428 | fn dkia kia edit uchar 65536 to void |
| 429 | put st 5 dhexf kia 0 |
| 430 | dspace |
| 431 | put st 6 dhexf kia get st 5 |
| 432 | dspace |
| 433 | put st 7 dhexf kia add get st 5 get st 6 |
| 434 | end |
| 435 | |
| 436 | note ct then tag fields into ex |
| 437 | fn dcttag ex edit uchar 65536 to void |
| 438 | put st 9 dhexf ex 0 |
| 439 | dspace |
| 440 | put st 10 dhexf ex get st 9 |
| 441 | end |
| 442 | |
| 443 | note E record after the tag |
| 444 | fn drece kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 445 | dspace |
| 446 | dkia kia |
| 447 | dspace |
| 448 | put st 8 dhexf pt 0 |
| 449 | dspace |
| 450 | dcttag ex |
| 451 | deor |
| 452 | if eq get st 3 1 |
| 453 | ret 2 |
| 454 | end |
| 455 | ret dvalid kia pt ex buf aa |
| 456 | end |
| 457 | |
| 458 | note D record after the tag |
| 459 | fn drecd kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 460 | dspace |
| 461 | dkia kia |
| 462 | dspace |
| 463 | dcttag ex |
| 464 | dspace |
| 465 | put st 8 get st 9 |
| 466 | put st 11 0 |
| 467 | if eq dpeek 70 |
| 468 | put st 11 1 |
| 469 | for i ulong 4 |
| 470 | dadv |
| 471 | end |
| 472 | else |
| 473 | if ne dhexf pt 0 get st 9 |
| 474 | dbad |
| 475 | end |
| 476 | end |
| 477 | deor |
| 478 | if eq get st 3 1 |
| 479 | ret 2 |
| 480 | end |
| 481 | if eq get st 11 1 |
| 482 | ret dinval kia pt ex buf aa |
| 483 | end |
| 484 | ret ddec kia pt ex buf aa |
| 485 | end |
| 486 | |
| 487 | note W record after the tag |
| 488 | fn drecw kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to ulong |
| 489 | dspace |
| 490 | t = dnum |
| 491 | dspace |
| 492 | c = dpeek |
| 493 | dadv |
| 494 | put st 11 1 |
| 495 | if eq c 86 |
| 496 | put st 11 0 |
| 497 | else |
| 498 | if ne c 73 |
| 499 | dbad |
| 500 | end |
| 501 | end |
| 502 | dspace |
| 503 | dkia kia |
| 504 | dspace |
| 505 | put st 8 dhexf pt 0 |
| 506 | dspace |
| 507 | dcttag ex |
| 508 | deor |
| 509 | if eq get st 3 1 |
| 510 | ret 2 |
| 511 | end |
| 512 | if eq get st 11 1 |
| 513 | ret dinval kia pt ex buf aa |
| 514 | end |
| 515 | ret dvalid kia pt ex buf aa |
| 516 | end |
| 517 | |
| 518 | note one block pt through aes_setkey (key kia[0..kl)) and aes_enc4 (lane 0); 0 when it equals ex |
| 519 | fn dblk kia view uchar 65536 pt edit uchar 65536 ex view uchar 65536 kl ulong to ulong |
| 520 | k = new ulong AGKN 0 |
| 521 | s = new ulong AGSN 0 |
| 522 | aes_setkey k s part kia 0 kl |
| 523 | h = ulong 0 |
| 524 | l = ulong 0 |
| 525 | for i ulong 8 |
| 526 | h = or shl h 8 ulong get pt i |
| 527 | l = or shl l 8 ulong get pt add i 8 |
| 528 | end |
| 529 | put s 150 h |
| 530 | put s 151 l |
| 531 | aes_enc4 k s |
| 532 | h = get s 150 |
| 533 | l = get s 151 |
| 534 | for i ulong 8 |
| 535 | put pt sub 7 i uchar and h 0xff |
| 536 | put pt sub 15 i uchar and l 0xff |
| 537 | h = shr h 8 |
| 538 | l = shr l 8 |
| 539 | end |
| 540 | ret dcmp pt ex 16 |
| 541 | end |
| 542 | |
| 543 | note B record after the tag |
| 544 | fn drecb kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 to ulong |
| 545 | dspace |
| 546 | kl = dhexf kia 0 |
| 547 | dspace |
| 548 | pl = dhexf pt 0 |
| 549 | dspace |
| 550 | cl = dhexf ex 0 |
| 551 | deor |
| 552 | if eq get st 3 1 |
| 553 | ret 2 |
| 554 | end |
| 555 | if ne pl 16 |
| 556 | dbad |
| 557 | ret 2 |
| 558 | end |
| 559 | if ne cl 16 |
| 560 | dbad |
| 561 | ret 2 |
| 562 | end |
| 563 | if ne gcm_chk or kl 0x11000 add kl 1 16 0 |
| 564 | dbad |
| 565 | ret 2 |
| 566 | end |
| 567 | ret dblk kia pt ex kl |
| 568 | end |
| 569 | |
| 570 | note X record: seal and open with ctl on zero arrays of the given lengths must both return want |
| 571 | fn drecx kia edit uchar 65536 buf edit uchar 65536 to ulong |
| 572 | dspace |
| 573 | ctl = dnum |
| 574 | dspace |
| 575 | ka = dnum |
| 576 | dspace |
| 577 | bl = dnum |
| 578 | dspace |
| 579 | want = dnum |
| 580 | deor |
| 581 | if eq get st 3 1 |
| 582 | ret 2 |
| 583 | end |
| 584 | if gt ka 65536 |
| 585 | dbad |
| 586 | ret 2 |
| 587 | end |
| 588 | if gt bl 65536 |
| 589 | dbad |
| 590 | ret 2 |
| 591 | end |
| 592 | for i ulong 65536 |
| 593 | put kia i 0 |
| 594 | put buf i 0 |
| 595 | end |
| 596 | f = ulong 0 |
| 597 | if ne aes_gcm_seal ctl part kia 0 ka part buf 0 bl want |
| 598 | f = 1 |
| 599 | end |
| 600 | if ne aes_gcm_open ctl part kia 0 ka part buf 0 bl want |
| 601 | f = 1 |
| 602 | end |
| 603 | ret f |
| 604 | end |
| 605 | |
| 606 | note Y record: aead_aes128gcm_seal and _open on zero arrays must both return want |
| 607 | fn drecy kia edit uchar 65536 aa edit uchar 65536 buf edit uchar 65536 to ulong |
| 608 | dspace |
| 609 | kn = dnum |
| 610 | dspace |
| 611 | al = dnum |
| 612 | dspace |
| 613 | bl = dnum |
| 614 | dspace |
| 615 | want = dnum |
| 616 | deor |
| 617 | if eq get st 3 1 |
| 618 | ret 2 |
| 619 | end |
| 620 | if gt kn 65536 |
| 621 | dbad |
| 622 | ret 2 |
| 623 | end |
| 624 | if gt al 65536 |
| 625 | dbad |
| 626 | ret 2 |
| 627 | end |
| 628 | if gt bl 65536 |
| 629 | dbad |
| 630 | ret 2 |
| 631 | end |
| 632 | for i ulong 65536 |
| 633 | put kia i 0 |
| 634 | put aa i 0 |
| 635 | put buf i 0 |
| 636 | end |
| 637 | put st 14 kn |
| 638 | put st 7 al |
| 639 | put st 8 bl |
| 640 | put st 10 0 |
| 641 | f = ulong 0 |
| 642 | if ne dacall kia aa buf 0 want |
| 643 | f = 1 |
| 644 | end |
| 645 | if ne dacall kia aa buf 1 want |
| 646 | f = 1 |
| 647 | end |
| 648 | put st 14 28 |
| 649 | ret f |
| 650 | end |
| 651 | |
| 652 | note the speed line: n bytes, seal and open nanoseconds, and n * 1000 / ns (decimal MB/s, 10^6 bytes) |
| 653 | fn dspd n ulong a ulong b ulong to void |
| 654 | w = write 1 tspd 0 len tspd |
| 655 | dprn n |
| 656 | w = write 1 tsns 0 len tsns |
| 657 | dprn a |
| 658 | w = write 1 tons 0 len tons |
| 659 | dprn b |
| 660 | w = write 1 tsmb 0 len tsmb |
| 661 | dprn div mul n 1000 or a 1 |
| 662 | w = write 1 tomb 0 len tomb |
| 663 | dprn div mul n 1000 or b 1 |
| 664 | w = write 1 tnl 0 1 |
| 665 | end |
| 666 | |
| 667 | note T record: seal then open n bytes in place with a fixed key and 12-byte IV, timed |
| 668 | fn drect kia edit uchar 65536 to ulong |
| 669 | mbuf = new uchar 1048592 0 |
| 670 | dspace |
| 671 | n = dnum |
| 672 | deor |
| 673 | if eq get st 3 1 |
| 674 | ret 2 |
| 675 | end |
| 676 | if gt n 1048576 |
| 677 | dbad |
| 678 | ret 2 |
| 679 | end |
| 680 | for i ulong 28 |
| 681 | put kia i uchar i |
| 682 | end |
| 683 | ctl = ulong 0xc1010 |
| 684 | t0 = monons |
| 685 | m = add n 16 |
| 686 | r = aes_gcm_seal ctl part kia 0 28 part mbuf 0 m |
| 687 | t1 = monons |
| 688 | r = or r aes_gcm_open ctl part kia 0 28 part mbuf 0 m |
| 689 | t2 = monons |
| 690 | dspd n ulong sub t1 t0 ulong sub t2 t1 |
| 691 | if ne r 0 |
| 692 | ret 1 |
| 693 | end |
| 694 | ret dzbig part mbuf 0 n |
| 695 | end |
| 696 | |
| 697 | note 0 when a is all zero (two loop levels: one rolled loop traps past 1000000 passes) |
| 698 | fn dzbig a view uchar * to ulong |
| 699 | n = len a |
| 700 | d = ulong 0 |
| 701 | o = ulong 0 |
| 702 | for h ulong add shr n 16 1 |
| 703 | for l ulong 65536 |
| 704 | if lt o n |
| 705 | d = or d ulong get a o |
| 706 | end |
| 707 | o = add o 1 |
| 708 | end |
| 709 | end |
| 710 | ret d |
| 711 | end |
| 712 | |
| 713 | note one record at the cursor; adds to st 12 (checks) and st 13 (failures) |
| 714 | fn drec kia edit uchar 65536 pt edit uchar 65536 ex edit uchar 65536 buf edit uchar 65536 aa edit uchar 65536 to void |
| 715 | c = dpeek |
| 716 | res = ulong 0 |
| 717 | if eq c 35 |
| 718 | dskip |
| 719 | ret |
| 720 | end |
| 721 | if eq c 10 |
| 722 | dadv |
| 723 | ret |
| 724 | end |
| 725 | if eq c 13 |
| 726 | dskip |
| 727 | ret |
| 728 | end |
| 729 | dadv |
| 730 | if eq c 66 |
| 731 | res = drecb kia pt ex |
| 732 | else |
| 733 | if eq c 69 |
| 734 | res = drece kia pt ex buf aa |
| 735 | else |
| 736 | if eq c 68 |
| 737 | res = drecd kia pt ex buf aa |
| 738 | else |
| 739 | if eq c 87 |
| 740 | res = drecw kia pt ex buf aa |
| 741 | else |
| 742 | if eq c 88 |
| 743 | res = drecx kia buf |
| 744 | else |
| 745 | if eq c 89 |
| 746 | res = drecy kia aa buf |
| 747 | else |
| 748 | if eq c 84 |
| 749 | res = drect kia |
| 750 | else |
| 751 | dbad |
| 752 | end |
| 753 | end |
| 754 | end |
| 755 | end |
| 756 | end |
| 757 | end |
| 758 | end |
| 759 | put st 12 add get st 12 1 |
| 760 | if ne res 0 |
| 761 | put st 13 add get st 13 1 |
| 762 | if eq get st 3 1 |
| 763 | dmsg 1 get st 2 |
| 764 | else |
| 765 | dmsg 0 get st 2 |
| 766 | end |
| 767 | end |
| 768 | end |
| 769 | |
| 770 | fn main to int |
| 771 | kia = new uchar 65536 0 |
| 772 | pt = new uchar 65536 0 |
| 773 | ex = new uchar 65536 0 |
| 774 | buf = new uchar 65536 0 |
| 775 | aa = new uchar 65536 0 |
| 776 | if ne dslurp 0 |
| 777 | r = write 2 tio 0 len tio |
| 778 | ret 2 |
| 779 | end |
| 780 | put st 1 0 |
| 781 | put st 2 0 |
| 782 | put st 12 0 |
| 783 | put st 13 0 |
| 784 | put st 14 28 |
| 785 | while lt get st 1 get st 0 bound 999999 |
| 786 | put st 2 add get st 2 1 |
| 787 | put st 3 0 |
| 788 | put st 4 get st 1 |
| 789 | drec kia pt ex buf aa |
| 790 | if eq get st 3 1 |
| 791 | put st 1 get st 4 |
| 792 | dskip |
| 793 | end |
| 794 | end |
| 795 | r = write 1 tsum 0 len tsum |
| 796 | dprn get st 12 |
| 797 | r = write 1 tchk 0 len tchk |
| 798 | dprn get st 13 |
| 799 | r = write 1 tfld 0 len tfld |
| 800 | if ne get st 13 0 |
| 801 | ret 1 |
| 802 | end |
| 803 | if eq get st 12 0 |
| 804 | ret 1 |
| 805 | end |
| 806 | ret 0 |
| 807 | end |