omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / sha512 / test / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: program-mode test driver for om/sha512, concatenated after sha512.om. |
| 3 | note Reads vector records from stdin (at most 16 MiB), one per line, fields separated by one space: |
| 4 | note H n MSG MD MD = SHA-512(MSG), MSG n bytes in hex ("-" when n is 0) |
| 5 | note P n PRE MSG MD MD = SHA-512(PRE || MSG), PRE 64 bytes in hex, checks s512pre |
| 6 | note M SEED MD0 .. MD99 NIST CAVP SHA-2 Monte Carlo chain (SHAVS section 6.4), 100 checkpoints |
| 7 | note Lines that are empty or start with # are skipped; a trailing carriage return is allowed. |
| 8 | note Prints "FAIL line L" (wrong digest) or "BAD line L" (malformed record) per failure, then |
| 9 | note "sha512 driver: C checks, F failed". Exit 0 only when F is 0 and C is above 0; exit 1 otherwise; |
| 10 | note exit 2 when the input cannot be read or exceeds 16 MiB. |
| 11 | |
| 12 | global inp uchar 16777216 |
| 13 | global st ulong 8 |
| 14 | global ob uchar 32 |
| 15 | global one uchar 1 |
| 16 | text tfail "FAIL line " |
| 17 | text tbad "BAD line " |
| 18 | text tsum "sha512 driver: " |
| 19 | text tchk " checks, " |
| 20 | text tfld " failed\n" |
| 21 | text tnl "\n" |
| 22 | text tio "sha512 driver: cannot read input or input over 16 MiB\n" |
| 23 | |
| 24 | note st: 0 input length, 1 cursor, 2 line number (1-based), 3 malformed flag |
| 25 | |
| 26 | note read all of stdin into inp; 0 ok, 2 read error or over capacity |
| 27 | fn dslurp to int |
| 28 | t = ulong 0 |
| 29 | while true bound 999999 |
| 30 | room = sub len inp t |
| 31 | if eq room 0 |
| 32 | r = read 0 one 0 1 |
| 33 | if ne r 0 |
| 34 | ret 2 |
| 35 | end |
| 36 | break |
| 37 | end |
| 38 | r = read 0 inp t room |
| 39 | if lt r 0 |
| 40 | ret 2 |
| 41 | end |
| 42 | if eq r 0 |
| 43 | break |
| 44 | end |
| 45 | t = add t ulong r |
| 46 | end |
| 47 | put st 0 t |
| 48 | ret 0 |
| 49 | end |
| 50 | |
| 51 | note byte at the cursor, or 256 at the end of input |
| 52 | fn dpeek to ulong |
| 53 | c = get st 1 |
| 54 | if ge c get st 0 |
| 55 | ret 256 |
| 56 | end |
| 57 | ret ulong get inp c |
| 58 | end |
| 59 | |
| 60 | fn dadv to void |
| 61 | put st 1 add get st 1 1 |
| 62 | end |
| 63 | |
| 64 | fn dbad to void |
| 65 | put st 3 1 |
| 66 | end |
| 67 | |
| 68 | note the value of hex digit c, or 16 when c is not one |
| 69 | fn dhexv c ulong to ulong |
| 70 | if ge c 48 |
| 71 | if le c 57 |
| 72 | ret sub c 48 |
| 73 | end |
| 74 | end |
| 75 | if ge c 97 |
| 76 | if le c 102 |
| 77 | ret sub c 87 |
| 78 | end |
| 79 | end |
| 80 | if ge c 65 |
| 81 | if le c 70 |
| 82 | ret sub c 55 |
| 83 | end |
| 84 | end |
| 85 | ret 16 |
| 86 | end |
| 87 | |
| 88 | note one byte from two hex digits at the cursor (malformed flag on a bad digit) |
| 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 a decimal number at the cursor (at most 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 n bytes of hex into a[0..n); n 0 is written "-" |
| 139 | fn dhexm a edit uchar 65536 n ulong to void |
| 140 | if eq n 0 |
| 141 | if ne dpeek 45 |
| 142 | dbad |
| 143 | ret |
| 144 | end |
| 145 | dadv |
| 146 | ret |
| 147 | end |
| 148 | for i ulong n |
| 149 | put a i dbyte |
| 150 | end |
| 151 | end |
| 152 | |
| 153 | fn dhex64 a edit uchar 64 to void |
| 154 | for i ulong 64 |
| 155 | put a i dbyte |
| 156 | end |
| 157 | end |
| 158 | |
| 159 | note end of record: an optional carriage return, then a newline or the end of input |
| 160 | fn deor to void |
| 161 | if eq dpeek 13 |
| 162 | dadv |
| 163 | end |
| 164 | c = dpeek |
| 165 | if eq c 10 |
| 166 | dadv |
| 167 | ret |
| 168 | end |
| 169 | if ne c 256 |
| 170 | dbad |
| 171 | end |
| 172 | end |
| 173 | |
| 174 | note skip the rest of the line, newline included |
| 175 | fn dskip to void |
| 176 | while true bound 999999 |
| 177 | c = dpeek |
| 178 | if eq c 256 |
| 179 | ret |
| 180 | end |
| 181 | dadv |
| 182 | if eq c 10 |
| 183 | ret |
| 184 | end |
| 185 | end |
| 186 | end |
| 187 | |
| 188 | note 0 when a and b hold the same 64 bytes |
| 189 | fn dcmp a view uchar 64 b view uchar 64 to ulong |
| 190 | d = ulong 0 |
| 191 | for i ulong 64 |
| 192 | if ne get a i get b i |
| 193 | d = 1 |
| 194 | end |
| 195 | end |
| 196 | ret d |
| 197 | end |
| 198 | |
| 199 | note write v in decimal to stdout |
| 200 | fn dprn v ulong to void |
| 201 | x = v |
| 202 | k = ulong 0 |
| 203 | while true bound 21 |
| 204 | put ob sub 31 k uchar add 48 rem x 10 |
| 205 | k = add k 1 |
| 206 | x = div x 10 |
| 207 | if eq x 0 |
| 208 | break |
| 209 | end |
| 210 | end |
| 211 | r = write 1 ob sub 32 k k |
| 212 | end |
| 213 | |
| 214 | fn dmsg bad ulong ln ulong to void |
| 215 | if eq bad 1 |
| 216 | r = write 1 tbad 0 len tbad |
| 217 | else |
| 218 | r = write 1 tfail 0 len tfail |
| 219 | end |
| 220 | dprn ln |
| 221 | r = write 1 tnl 0 1 |
| 222 | end |
| 223 | |
| 224 | note H record after the tag; 0 pass, 1 wrong digest, 2 malformed |
| 225 | fn drech msg edit uchar 65536 dig edit uchar 64 ex edit uchar 64 to ulong |
| 226 | dspace |
| 227 | n = dnum |
| 228 | if gt n 65536 |
| 229 | dbad |
| 230 | ret 2 |
| 231 | end |
| 232 | dspace |
| 233 | dhexm msg n |
| 234 | dspace |
| 235 | dhex64 ex |
| 236 | deor |
| 237 | if eq get st 3 1 |
| 238 | ret 2 |
| 239 | end |
| 240 | s512hash msg n dig |
| 241 | ret dcmp dig ex |
| 242 | end |
| 243 | |
| 244 | note P record after the tag |
| 245 | fn drecp msg edit uchar 65536 dig edit uchar 64 ex edit uchar 64 pre edit uchar 64 to ulong |
| 246 | dspace |
| 247 | n = dnum |
| 248 | if gt n 65536 |
| 249 | dbad |
| 250 | ret 2 |
| 251 | end |
| 252 | dspace |
| 253 | dhex64 pre |
| 254 | dspace |
| 255 | dhexm msg n |
| 256 | dspace |
| 257 | dhex64 ex |
| 258 | deor |
| 259 | if eq get st 3 1 |
| 260 | ret 2 |
| 261 | end |
| 262 | s512pre pre msg n dig |
| 263 | ret dcmp dig ex |
| 264 | end |
| 265 | |
| 266 | note M record after the tag: SHAVS Monte Carlo. For j = 0..99: MD0 = MD1 = MD2 = Seed; for i = 3..1002 |
| 267 | note MDi = SHA-512(MD(i-3) || MD(i-2) || MD(i-1)); checkpoint j is MD1002, which is also the next Seed. |
| 268 | note msg[0..192) holds the three previous digests. Returns the number of failed checkpoints; the |
| 269 | note malformed flag is set on a parse error. |
| 270 | fn drecm msg edit uchar 65536 dig edit uchar 64 ex edit uchar 64 sd edit uchar 64 to ulong |
| 271 | fl = ulong 0 |
| 272 | dspace |
| 273 | dhex64 sd |
| 274 | for j ulong 100 |
| 275 | for i ulong 64 |
| 276 | put msg i get sd i |
| 277 | put msg add i 64 get sd i |
| 278 | put msg add i 128 get sd i |
| 279 | end |
| 280 | for c ulong 1000 |
| 281 | s512hash msg 192 dig |
| 282 | for i ulong 128 |
| 283 | put msg i get msg add i 64 |
| 284 | end |
| 285 | for i ulong 64 |
| 286 | put msg add i 128 get dig i |
| 287 | end |
| 288 | end |
| 289 | dspace |
| 290 | dhex64 ex |
| 291 | if ne dcmp dig ex 0 |
| 292 | fl = add fl 1 |
| 293 | end |
| 294 | for i ulong 64 |
| 295 | put sd i get dig i |
| 296 | end |
| 297 | end |
| 298 | deor |
| 299 | ret fl |
| 300 | end |
| 301 | |
| 302 | fn main to int |
| 303 | msg = new uchar 65536 0 |
| 304 | dig = new uchar 64 0 |
| 305 | ex = new uchar 64 0 |
| 306 | pre = new uchar 64 0 |
| 307 | if ne dslurp 0 |
| 308 | r = write 2 tio 0 len tio |
| 309 | ret 2 |
| 310 | end |
| 311 | put st 1 0 |
| 312 | put st 2 0 |
| 313 | chk = ulong 0 |
| 314 | fl = ulong 0 |
| 315 | while lt get st 1 get st 0 bound 999999 |
| 316 | put st 2 add get st 2 1 |
| 317 | ln = get st 2 |
| 318 | put st 3 0 |
| 319 | put st 4 get st 1 |
| 320 | c = dpeek |
| 321 | if eq c 72 |
| 322 | dadv |
| 323 | res = drech msg dig ex |
| 324 | chk = add chk 1 |
| 325 | if ne res 0 |
| 326 | fl = add fl 1 |
| 327 | dmsg get st 3 ln |
| 328 | end |
| 329 | else |
| 330 | if eq c 80 |
| 331 | dadv |
| 332 | res = drecp msg dig ex pre |
| 333 | chk = add chk 1 |
| 334 | if ne res 0 |
| 335 | fl = add fl 1 |
| 336 | dmsg get st 3 ln |
| 337 | end |
| 338 | else |
| 339 | if eq c 77 |
| 340 | dadv |
| 341 | res = drecm msg dig ex pre |
| 342 | chk = add chk 100 |
| 343 | fl = add fl res |
| 344 | if eq get st 3 1 |
| 345 | fl = add fl 1 |
| 346 | dmsg 1 ln |
| 347 | else |
| 348 | if ne res 0 |
| 349 | dmsg 0 ln |
| 350 | end |
| 351 | end |
| 352 | else |
| 353 | if eq c 35 |
| 354 | dskip |
| 355 | else |
| 356 | if eq c 10 |
| 357 | dadv |
| 358 | else |
| 359 | if eq c 13 |
| 360 | dskip |
| 361 | else |
| 362 | chk = add chk 1 |
| 363 | fl = add fl 1 |
| 364 | dmsg 1 ln |
| 365 | dskip |
| 366 | end |
| 367 | end |
| 368 | end |
| 369 | end |
| 370 | end |
| 371 | end |
| 372 | if eq get st 3 1 |
| 373 | put st 1 get st 4 |
| 374 | dskip |
| 375 | end |
| 376 | end |
| 377 | r = write 1 tsum 0 len tsum |
| 378 | dprn chk |
| 379 | r = write 1 tchk 0 len tchk |
| 380 | dprn fl |
| 381 | r = write 1 tfld 0 len tfld |
| 382 | if ne fl 0 |
| 383 | ret 1 |
| 384 | end |
| 385 | if eq chk 0 |
| 386 | ret 1 |
| 387 | end |
| 388 | ret 0 |
| 389 | end |