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/drbg/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 / drbg / driver.om` 510 lines ยท 9695 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note driver.om: program-mode test driver for om/tls/drbg.om (run.sh writes om2, then sha2.om, hkdf.om, 3 note drbg.om, this file). Reads records from stdin (at most 8 MiB), one per line, fields separated by 4 note one space, byte strings in hex ("-" = empty): 5 note D NB EI NONCE PS OP .. OP = RB 6 note drbg_inst(EI || NONCE || PS), then each OP in order, then compare the output of the last 7 note generate with RB (NB bytes). OP is one of 8 note G AI drbg_gen of NB bytes with additional input AI 9 note R AI EI drbg_reseed(EI || AI) (CAVP pr_false, ACVP reSeed) 10 note P AI EI prediction resistance: drbg_reseed(EI || AI), then drbg_gen of NB bytes with 11 note Null additional input (SP 800-90A 9.3.1 step 7; CAVP pr_true, ACVP PR) 12 note Checks: RB matches (1), and the negative control: RB with one byte flipped must not match (1). 13 note S the API checks below (one check each) 14 note Lines that are empty or start with # are skipped. Prints "MISMATCH line L" (wrong result), 15 note "STUCK line L" (a call returned 4, the continuous-test error state) or "BAD line L" 16 note (malformed record), then "drbg driver: C checks, F failed". Exit 0 only when F is 0 and C > 0; 17 note 1 otherwise; 2 when the input cannot be read or exceeds 8 MiB. 18 19 global inp uchar `8388608` 20 global st ulong 8 21 global ob uchar 32 22 global one uchar 1 23 text tfail "MISMATCH line " 24 text tstuck "STUCK line " 25 text tbad "BAD line " 26 text tsum "drbg driver: " 27 text tchk " checks, " 28 text tfld " failed\n" 29 text tnl "\n" 30 text tio "drbg driver: cannot read input or input over 8 MiB\n" 31 32 note st: 0 input length, 1 cursor, 2 line number, 3 malformed flag, 4 line start, 5 checks, 6 failed 33 34 fn drslurp to int 35 t = ulong 0 36 while true bound 999999 37 room = sub len inp t 38 if eq room 0 39 r = read 0 one 0 1 40 if ne r 0 41 ret 2 42 end 43 break 44 end 45 r = read 0 inp t room 46 if lt r 0 47 ret 2 48 end 49 if eq r 0 50 break 51 end 52 t = add t ulong r 53 end 54 put st 0 t 55 ret 0 56 end 57 58 fn drpeek to ulong 59 c = get st 1 60 if ge c get st 0 61 ret 256 62 end 63 ret ulong get inp c 64 end 65 66 fn dradv to void 67 put st 1 add get st 1 1 68 end 69 70 fn drbad to void 71 put st 3 1 72 end 73 74 fn drhexv c ulong to ulong 75 if ge c 48 76 if le c 57 77 ret sub c 48 78 end 79 end 80 if ge c 97 81 if le c 102 82 ret sub c 87 83 end 84 end 85 if ge c 65 86 if le c 70 87 ret sub c 55 88 end 89 end 90 ret 16 91 end 92 93 fn drbyte to uchar 94 hi = drhexv drpeek 95 dradv 96 lo = drhexv drpeek 97 dradv 98 if gt hi 15 99 drbad 100 ret 0 101 end 102 if gt lo 15 103 drbad 104 ret 0 105 end 106 ret uchar or shl hi 4 lo 107 end 108 109 fn drsp to void 110 if ne drpeek 32 111 drbad 112 ret 113 end 114 dradv 115 end 116 117 note a decimal number (at most 9 digits) after one space 118 fn drnum to ulong 119 drsp 120 v = ulong 0 121 k = ulong 0 122 while true bound 10 123 c = drpeek 124 if lt c 48 125 break 126 end 127 if gt c 57 128 break 129 end 130 v = add mul v 10 sub c 48 131 k = add k 1 132 dradv 133 end 134 if eq k 0 135 drbad 136 end 137 if gt k 9 138 drbad 139 end 140 ret v 141 end 142 143 note one space, then hex bytes into a[0..n) (or "-"); returns n 144 fn drhex a edit uchar * to ulong 145 drsp 146 if eq drpeek 45 147 dradv 148 ret 0 149 end 150 n = ulong 0 151 while true bound 999999 152 if gt drhexv drpeek 15 153 break 154 end 155 if ge n len a 156 drbad 157 ret n 158 end 159 put a n drbyte 160 n = add n 1 161 end 162 if eq n 0 163 drbad 164 end 165 ret n 166 end 167 168 fn dreor to void 169 if eq drpeek 13 170 dradv 171 end 172 c = drpeek 173 if eq c 10 174 dradv 175 ret 176 end 177 if ne c 256 178 drbad 179 end 180 end 181 182 fn drskip to void 183 while true bound 999999 184 c = drpeek 185 if eq c 256 186 ret 187 end 188 dradv 189 if eq c 10 190 ret 191 end 192 end 193 end 194 195 fn drprn v ulong to void 196 x = v 197 k = ulong 0 198 while true bound 21 199 put ob sub 31 k uchar add 48 rem x 10 200 k = add k 1 201 x = div x 10 202 if eq x 0 203 break 204 end 205 end 206 r = write 1 ob sub 32 k k 207 end 208 209 note kind 0 MISMATCH, 1 BAD, 2 STUCK 210 fn drmsg kind ulong to void 211 if eq kind 1 212 r = write 1 tbad 0 len tbad 213 else 214 if eq kind 2 215 r = write 1 tstuck 0 len tstuck 216 else 217 r = write 1 tfail 0 len tfail 218 end 219 end 220 drprn get st 2 221 r = write 1 tnl 0 1 222 end 223 224 note one check: ok 1 passes, anything else fails (and is reported) 225 fn drchk ok ulong to void 226 put st 5 add get st 5 1 227 if ne ok 1 228 put st 6 add get st 6 1 229 drmsg 0 230 end 231 end 232 233 note 1 when a[0..n) equals b[0..n) (n <= both lengths), else 0 234 fn dreq a view uchar * b view uchar * n ulong to ulong 235 if gt n len a 236 ret 0 237 end 238 if gt n len b 239 ret 0 240 end 241 d = uchar 0 242 for i ulong n 243 d = or d xor get a i get b i 244 end 245 if eq d 0 246 ret 1 247 end 248 ret 0 249 end 250 251 note sm[n..) gets x; returns n + len x (0 and the record is malformed when it does not fit) 252 fn drapp sm edit uchar * n ulong x view uchar * to ulong 253 if gt add n len x len sm 254 drbad 255 ret 0 256 end 257 for i ulong len x 258 put sm add n i get x i 259 end 260 ret add n len x 261 end 262 263 note seed material EI || NONCE || PS from the record into sm, then drbg_inst; returns its code 264 fn drinit a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 s edit uchar 128 to ulong 265 na = drhex a 266 nn = drhex b 267 n = drapp sm 0 part a 0 na 268 n = drapp sm n part b 0 nn 269 na = drhex a 270 n = drapp sm n part a 0 na 271 ret drbg_inst s part sm 0 n 272 end 273 274 note reseed OP fields "AI EI": drbg_reseed(EI || AI); returns its code 275 fn drres a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 s edit uchar 128 to ulong 276 na = drhex a 277 nn = drhex b 278 n = drapp sm 0 part b 0 nn 279 n = drapp sm n part a 0 na 280 ret drbg_reseed s part sm 0 n 281 end 282 283 note generate nb bytes into o with additional input a[0..na) 284 fn drgen a edit uchar 4096 na ulong o edit uchar 65537 s edit uchar 128 nb ulong to ulong 285 ret drbg_gen s part o 0 nb part a 0 na 286 end 287 288 note one OP (after its space): returns the call's code, plus 256 when it generated 289 fn drop1 a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 s edit uchar 128 nb ulong to ulong 290 c = drpeek 291 dradv 292 if eq c 71 293 ret add 256 drgen a drhex a o s nb 294 end 295 if eq c 82 296 ret drres a b sm s 297 end 298 if eq c 80 299 rc = drres a b sm s 300 if eq rc 0 301 rc = drgen a 0 o s nb 302 end 303 ret add 256 rc 304 end 305 drbad 306 ret 0 307 end 308 309 note the OPs up to and including "="; returns bit 0 a call failed, bit 1 a call returned 4 (the 310 note continuous-test error state), bit 2 at least one generate ran 311 fn drops a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 s edit uchar 128 nb ulong to ulong 312 f = ulong 0 313 while true bound 64 314 drsp 315 if eq drpeek 61 316 dradv 317 break 318 end 319 r = drop1 a b sm o s nb 320 if ge r 256 321 f = or f 4 322 end 323 r = and r 255 324 if eq r 4 325 f = or f 2 326 end 327 if ne r 0 328 f = or f 1 329 end 330 if eq get st 3 1 331 ret f 332 end 333 end 334 ret f 335 end 336 337 note a D record (after the D); a, b scratch, sm seed material, o output, ex expected, s the DRBG state 338 fn drrecd a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 ex edit uchar 65536 s edit uchar 128 to void 339 nb = drnum 340 if gt nb len o 341 drbad 342 ret 343 end 344 if eq nb 0 345 drbad 346 ret 347 end 348 f = ulong 0 349 if ne drinit a b sm s 0 350 f = 1 351 end 352 f = or f drops a b sm o s nb 353 nx = drhex ex 354 dreor 355 if eq get st 3 1 356 ret 357 end 358 if ne nx nb 359 drbad 360 ret 361 end 362 if eq and f 4 0 363 drbad 364 ret 365 end 366 if ne and f 2 0 367 drmsg 2 368 end 369 ok = dreq part o 0 nb ex nb 370 if ne and f 1 0 371 ok = 0 372 end 373 drchk ok 374 put ex 0 xor get ex 0 1 375 drchk xor dreq part o 0 nb ex nb 1 376 drbg_wipe s 377 end 378 379 note one check that got equals want 380 fn drwant got ulong want ulong to void 381 ok = ulong 0 382 if eq got want 383 ok = 1 384 end 385 drchk ok 386 end 387 388 note fixed seed material of n bytes (byte i = i + 1) for the API checks 389 fn drseed sm edit uchar * n ulong to void 390 for i ulong n 391 put sm i uchar add i 1 392 end 393 end 394 395 note S record: API checks (each is one check) 396 fn drrecs sm edit uchar 8192 o edit uchar 65537 s edit uchar 128 to void 397 dreor 398 drbg_wipe s 399 note generate and reseed before instantiate: 2 400 drwant drbg_gen s part o 0 32 part sm 0 0 2 401 drseed sm 64 402 drwant drbg_reseed s part sm 0 32 2 403 note seed material under 48 bytes is refused (3); 48 is accepted 404 drwant drbg_inst s part sm 0 47 3 405 drwant drbg_inst s part sm 0 48 0 406 note reseed material under 32 bytes is refused (3) 407 drwant drbg_reseed s part sm 0 31 3 408 note request over 65536 bytes is refused (3); 65536 is accepted 409 drwant drbg_gen s part o 0 65537 part sm 0 0 3 410 drwant drbg_gen s part o 0 65536 part sm 0 0 0 411 note reseed interval: limit 2 allows generates with counter 1 and 2, then 1 (reseed required) and 412 note no output written; after a reseed generates work again 413 drwant drbg_limit s 0 3 414 drwant drbg_limit s add DRRSMX 1 3 415 drwant drbg_inst s part sm 0 48 0 416 drwant drbg_limit s 2 0 417 drwant drbg_gen s part o 0 16 part sm 0 0 0 418 drwant drbg_gen s part o 0 16 part sm 0 0 0 419 for i ulong 16 420 put o i 170 421 end 422 drwant drbg_gen s part o 0 16 part sm 0 0 1 423 d = ulong 0 424 for i ulong 16 425 d = or d xor ulong get o i 170 426 end 427 drwant d 0 428 drwant drbg_reseed s part sm 0 32 0 429 drwant drbg_gen s part o 0 16 part sm 0 0 0 430 note two generates give different output; the state after wipe is all zero and refuses 431 drwant drbg_limit s DRRSMX 0 432 drwant drbg_gen s part o 0 32 part sm 0 0 0 433 drwant drbg_gen s part o 32 32 part sm 0 0 0 434 for i ulong 32 435 put sm add 4096 i get o i 436 end 437 drchk xor dreq part o 32 32 part sm 4096 32 32 1 438 drbg_wipe s 439 d = ulong 0 440 for i ulong len s 441 d = or d ulong get s i 442 end 443 drwant d 0 444 drwant drbg_gen s part o 0 16 part sm 0 0 2 445 end 446 447 fn drrec a edit uchar 4096 b edit uchar 4096 sm edit uchar 8192 o edit uchar 65537 ex edit uchar 65536 s edit uchar 128 to void 448 c = drpeek 449 dradv 450 if eq c 68 451 drrecd a b sm o ex s 452 ret 453 end 454 if eq c 83 455 drrecs sm o s 456 ret 457 end 458 drbad 459 end 460 461 fn main to int 462 a = new uchar 4096 0 463 b = new uchar 4096 0 464 sm = new uchar 8192 0 465 o = new uchar 65537 0 466 ex = new uchar 65536 0 467 s = new uchar 128 0 468 if ne drslurp 0 469 r = write 2 tio 0 len tio 470 ret 2 471 end 472 put st 1 0 473 put st 2 0 474 put st 5 0 475 put st 6 0 476 while lt get st 1 get st 0 bound 999999 477 put st 2 add get st 2 1 478 put st 3 0 479 put st 4 get st 1 480 c = drpeek 481 if eq c 35 482 drskip 483 else 484 if eq c 10 485 dradv 486 else 487 drrec a b sm o ex s 488 if eq get st 3 1 489 put st 5 add get st 5 1 490 put st 6 add get st 6 1 491 drmsg 1 492 put st 1 get st 4 493 drskip 494 end 495 end 496 end 497 end 498 r = write 1 tsum 0 len tsum 499 drprn get st 5 500 r = write 1 tchk 0 len tchk 501 drprn get st 6 502 r = write 1 tfld 0 len tfld 503 if ne get st 6 0 504 ret 1 505 end 506 if eq get st 5 0 507 ret 1 508 end 509 ret 0 510 end