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 / ecdh / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: test driver for om/tls/ecdh.om (program mode), concatenated after ec.om and ecdh.om. |
| 3 | note Reads a vector file on stdin (see gen_vectors.py), one case per line: |
| 4 | note ID OP RC DHEX PHEX XHEX ('-' = empty hex field, '#' comment) |
| 5 | note OP S: ecdh_shared(d, p, z) with len z = len x; must return exactly RC and write x |
| 6 | note OP P: ecdh_pub(d, q) with len q = len x; must return exactly RC and write x (zeros when RC != 0) |
| 7 | note OP C: ecdh_pct(d, p); must return exactly RC |
| 8 | note Controls for every RC 0 case. S: the last byte of p flipped must give exactly 3 (off-curve), |
| 9 | note d with its last byte xor 1 must not give x, a 1-byte-shorter d must give 5 and a 1-byte-longer |
| 10 | note z must give 2. P: ecdh_pct(d, x) must give 0 and with the last byte of x flipped exactly 6. |
| 11 | note Optional argument: repeat count R (timing; each call runs R times). Prints one MISMATCH line |
| 12 | note per wrong result and the summary line: |
| 13 | note cases C calls V controls K mismatches X ns T codes c0 c1 c2 c3 c4 c5 c6 |
| 14 | note Exit 0 only when at least one call ran and none mismatched; 1 otherwise; 65 bad input. |
| 15 | |
| 16 | text txmis "MISMATCH line " |
| 17 | text txgot " got " |
| 18 | text txwhy " " |
| 19 | text txctl "control" |
| 20 | text txver "call" |
| 21 | text txcase "cases " |
| 22 | text txvers " calls " |
| 23 | text txctls " controls " |
| 24 | text txmm " mismatches " |
| 25 | text txns " ns " |
| 26 | text txcod " codes" |
| 27 | text txpct "pct" |
| 28 | text txz "bytes" |
| 29 | text txsp " " |
| 30 | text txnl "\n" |
| 31 | text txbad "driver: bad input\n" |
| 32 | |
| 33 | global etin uchar 16777216 |
| 34 | global etout uchar 32 |
| 35 | global etarg uchar 32 |
| 36 | note etc: 0 line, 1 cases, 2 calls, 3 mismatches, 4 ns, 5 repeats, 6 pos, 7 controls, |
| 37 | note 8..14 code counts 0..6, 17 op (83 S, 80 P, 67 C), 18 expected code, 20 d len, 21 p len, |
| 38 | note 22 x len, 24 id start, 25 id end, 26 input length |
| 39 | global etc ulong 32 |
| 40 | |
| 41 | fn etnum v ulong to void |
| 42 | k = ulong 0 |
| 43 | x = v |
| 44 | while true bound 30 |
| 45 | put etout sub 31 k uchar add rem x 10 48 |
| 46 | k = add k 1 |
| 47 | x = div x 10 |
| 48 | if eq x 0 |
| 49 | break |
| 50 | end |
| 51 | end |
| 52 | r = write 1 etout sub 32 k k |
| 53 | end |
| 54 | |
| 55 | fn etdie to void |
| 56 | r = write 2 txbad 0 len txbad |
| 57 | exit 65 |
| 58 | end |
| 59 | |
| 60 | fn etnib c ulong to ulong |
| 61 | if and ge c 48 le c 57 |
| 62 | ret sub c 48 |
| 63 | end |
| 64 | if and ge c 97 le c 102 |
| 65 | ret sub c 87 |
| 66 | end |
| 67 | etdie |
| 68 | ret 0 |
| 69 | end |
| 70 | |
| 71 | fn etslurp to void |
| 72 | t = ulong 0 |
| 73 | while true bound 999999 |
| 74 | room = sub len etin t |
| 75 | if eq room 0 |
| 76 | etdie |
| 77 | end |
| 78 | r = read 0 etin t room |
| 79 | if lt r 0 |
| 80 | etdie |
| 81 | end |
| 82 | if eq r 0 |
| 83 | break |
| 84 | end |
| 85 | t = add t ulong r |
| 86 | end |
| 87 | put etc 26 t |
| 88 | end |
| 89 | |
| 90 | fn etreps to ulong |
| 91 | if lt argcnt 2 |
| 92 | ret 1 |
| 93 | end |
| 94 | l = argread 1 etarg 0 20 |
| 95 | v = ulong 0 |
| 96 | for i ulong ulong l |
| 97 | v = add mul v 10 sub ulong get etarg i 48 |
| 98 | end |
| 99 | ret v |
| 100 | end |
| 101 | |
| 102 | note end of the token at the current position (stops at a space or newline); the token must be |
| 103 | note non-empty |
| 104 | fn etend to ulong |
| 105 | p = get etc 6 |
| 106 | e = p |
| 107 | while lt e get etc 26 bound 999999 |
| 108 | c = get etin e |
| 109 | if or eq c 32 eq c 10 |
| 110 | break |
| 111 | end |
| 112 | e = add e 1 |
| 113 | end |
| 114 | if eq e p |
| 115 | etdie |
| 116 | end |
| 117 | ret e |
| 118 | end |
| 119 | |
| 120 | note step past the token ending at e and one following space (a newline is left in place) |
| 121 | fn etnext e ulong to void |
| 122 | if lt e get etc 26 |
| 123 | if eq get etin e 32 |
| 124 | e = add e 1 |
| 125 | end |
| 126 | end |
| 127 | put etc 6 e |
| 128 | end |
| 129 | |
| 130 | fn etdec to ulong |
| 131 | e = etend |
| 132 | v = ulong 0 |
| 133 | for i ulong sub e get etc 6 |
| 134 | c = ulong get etin add get etc 6 i |
| 135 | if or lt c 48 gt c 57 |
| 136 | etdie |
| 137 | end |
| 138 | v = add mul v 10 sub c 48 |
| 139 | end |
| 140 | etnext e |
| 141 | ret v |
| 142 | end |
| 143 | |
| 144 | fn etbyte p ulong to uchar |
| 145 | hi = etnib ulong get etin p |
| 146 | lo = etnib ulong get etin add p 1 |
| 147 | ret uchar or shl hi 4 lo |
| 148 | end |
| 149 | |
| 150 | note hex token into a[0..); returns the byte count ('-' is empty) |
| 151 | fn ethex a edit uchar * to ulong |
| 152 | e = etend |
| 153 | p = get etc 6 |
| 154 | if and eq sub e p 1 eq get etin p 45 |
| 155 | etnext e |
| 156 | ret 0 |
| 157 | end |
| 158 | h = sub e p |
| 159 | if ne and h 1 0 |
| 160 | etdie |
| 161 | end |
| 162 | c = div h 2 |
| 163 | if gt c len a |
| 164 | etdie |
| 165 | end |
| 166 | for i ulong c |
| 167 | put a i etbyte add p mul i 2 |
| 168 | end |
| 169 | etnext e |
| 170 | ret c |
| 171 | end |
| 172 | |
| 173 | fn etbump i ulong v ulong to void |
| 174 | put etc i add get etc i v |
| 175 | end |
| 176 | |
| 177 | fn etmis why view uchar * got ulong to void |
| 178 | r = write 1 txmis 0 len txmis |
| 179 | etnum get etc 0 |
| 180 | r = write 1 txsp 0 1 |
| 181 | r = write 1 etin get etc 24 sub get etc 25 get etc 24 |
| 182 | r = write 1 txwhy 0 1 |
| 183 | r = write 1 why 0 len why |
| 184 | r = write 1 txgot 0 len txgot |
| 185 | etnum got |
| 186 | r = write 1 txnl 0 1 |
| 187 | etbump 3 1 |
| 188 | end |
| 189 | |
| 190 | |
| 191 | note one library call for the case's OP (etc 17); the output is z[0..etc 22) |
| 192 | fn etcall d view uchar * p view uchar * z edit uchar * to ulong |
| 193 | op = get etc 17 |
| 194 | if eq op 83 |
| 195 | ret ecdh_shared d p part z 0 get etc 22 |
| 196 | end |
| 197 | if eq op 80 |
| 198 | ret ecdh_pub d part z 0 get etc 22 |
| 199 | end |
| 200 | ret ecdh_pct d p |
| 201 | end |
| 202 | |
| 203 | note parse one case line into d, p and the expected bytes x |
| 204 | fn etparse d edit uchar * p edit uchar * x edit uchar * to void |
| 205 | put etc 24 get etc 6 |
| 206 | e = etend |
| 207 | put etc 25 e |
| 208 | etnext e |
| 209 | e = etend |
| 210 | if ne sub e get etc 6 1 |
| 211 | etdie |
| 212 | end |
| 213 | c = ulong get etin get etc 6 |
| 214 | if and and ne c 83 ne c 80 ne c 67 |
| 215 | etdie |
| 216 | end |
| 217 | put etc 17 c |
| 218 | etnext e |
| 219 | put etc 18 etdec |
| 220 | put etc 20 ethex d |
| 221 | put etc 21 ethex p |
| 222 | put etc 22 ethex x |
| 223 | if ne get etin get etc 6 10 |
| 224 | etdie |
| 225 | end |
| 226 | end |
| 227 | |
| 228 | fn etzero o edit uchar * to void |
| 229 | for i ulong len o |
| 230 | put o i 0 |
| 231 | end |
| 232 | end |
| 233 | |
| 234 | note bytes o[0..n) against x[0..n): 0 equal, else 1 |
| 235 | fn etdiff o view uchar * x view uchar * to ulong |
| 236 | k = ulong 0 |
| 237 | for i ulong len x |
| 238 | k = or k ulong xor get o i get x i |
| 239 | end |
| 240 | if eq k 0 |
| 241 | ret 0 |
| 242 | end |
| 243 | ret 1 |
| 244 | end |
| 245 | |
| 246 | fn etctl got ulong want ulong to void |
| 247 | etbump 7 1 |
| 248 | if ne got want |
| 249 | etmis txctl got |
| 250 | end |
| 251 | end |
| 252 | |
| 253 | note S controls (expected code 0), on views of exact length: peer flipped must give 3, scalar |
| 254 | note changed must not give x, a short d must give 5, a long z must give 2 |
| 255 | fn etcs1 d view uchar * p edit uchar * to void |
| 256 | o = new uchar 64 0 |
| 257 | np = len p |
| 258 | put p sub np 1 xor get p sub np 1 1 |
| 259 | got = ecdh_shared d p part o 0 len d |
| 260 | put p sub np 1 xor get p sub np 1 1 |
| 261 | etctl got 3 |
| 262 | end |
| 263 | |
| 264 | fn etcs2 d edit uchar * p view uchar * x view uchar * to void |
| 265 | o = new uchar 64 0 |
| 266 | nd = len d |
| 267 | put d sub nd 1 xor get d sub nd 1 1 |
| 268 | got = ecdh_shared d p part o 0 nd |
| 269 | put d sub nd 1 xor get d sub nd 1 1 |
| 270 | etbump 7 1 |
| 271 | if eq got 0 |
| 272 | if eq etdiff part o 0 nd x 0 |
| 273 | etmis txctl 0 |
| 274 | end |
| 275 | end |
| 276 | end |
| 277 | |
| 278 | fn etcs3 d view uchar * p view uchar * to void |
| 279 | o = new uchar 64 0 |
| 280 | nd = len d |
| 281 | got = ecdh_shared part d 0 sub nd 1 p part o 0 nd |
| 282 | etctl got 5 |
| 283 | got = ecdh_shared d p part o 0 add nd 1 |
| 284 | etctl got 2 |
| 285 | end |
| 286 | |
| 287 | note P controls (expected code 0): pct of the expected key passes, a flipped key fails with 6 |
| 288 | fn etcp d view uchar * x edit uchar * to void |
| 289 | nx = len x |
| 290 | got = ecdh_pct d x |
| 291 | etctl got 0 |
| 292 | put x sub nx 1 xor get x sub nx 1 1 |
| 293 | got = ecdh_pct d x |
| 294 | put x sub nx 1 xor get x sub nx 1 1 |
| 295 | etctl got 6 |
| 296 | end |
| 297 | |
| 298 | note run the case R times (etc 5), keeping the last code in etc 19 |
| 299 | fn etrun d view uchar * p view uchar * o edit uchar * to void |
| 300 | for k ulong get etc 5 |
| 301 | etzero o |
| 302 | t0 = monons |
| 303 | put etc 19 etcall d p o |
| 304 | etbump 4 ulong sub monons t0 |
| 305 | etbump 2 1 |
| 306 | end |
| 307 | end |
| 308 | |
| 309 | fn etchk o view uchar * x view uchar * to void |
| 310 | got = get etc 19 |
| 311 | if le got 6 |
| 312 | etbump add 8 got 1 |
| 313 | end |
| 314 | if ne got get etc 18 |
| 315 | etmis txver got |
| 316 | end |
| 317 | if eq etdiff o x 1 |
| 318 | etmis txz got |
| 319 | end |
| 320 | end |
| 321 | |
| 322 | fn etcase d edit uchar * p edit uchar * x edit uchar * to void |
| 323 | o = new uchar 128 0 |
| 324 | etbump 1 1 |
| 325 | etparse d p x |
| 326 | etrun part d 0 get etc 20 part p 0 get etc 21 o |
| 327 | etchk part o 0 get etc 22 part x 0 get etc 22 |
| 328 | if eq get etc 18 0 |
| 329 | nd = get etc 20 |
| 330 | np = get etc 21 |
| 331 | nx = get etc 22 |
| 332 | if eq get etc 17 83 |
| 333 | etcs1 part d 0 nd part p 0 np |
| 334 | etcs2 part d 0 nd part p 0 np part x 0 nx |
| 335 | etcs3 part d 0 nd part p 0 np |
| 336 | end |
| 337 | if eq get etc 17 80 |
| 338 | etcp part d 0 nd part x 0 nx |
| 339 | end |
| 340 | end |
| 341 | end |
| 342 | |
| 343 | fn main to int |
| 344 | d = new uchar 64 0 |
| 345 | p = new uchar 256 0 |
| 346 | x = new uchar 128 0 |
| 347 | etslurp |
| 348 | put etc 5 etreps |
| 349 | tot = get etc 26 |
| 350 | while lt get etc 6 tot bound 999999 |
| 351 | etbump 0 1 |
| 352 | q = get etc 6 |
| 353 | if ne get etin q 35 |
| 354 | etcase d p x |
| 355 | end |
| 356 | e = get etc 6 |
| 357 | while and lt e tot ne get etin e 10 bound 999999 |
| 358 | e = add e 1 |
| 359 | end |
| 360 | put etc 6 add e 1 |
| 361 | end |
| 362 | r = write 1 txcase 0 len txcase |
| 363 | etnum get etc 1 |
| 364 | r = write 1 txvers 0 len txvers |
| 365 | etnum get etc 2 |
| 366 | r = write 1 txctls 0 len txctls |
| 367 | etnum get etc 7 |
| 368 | r = write 1 txmm 0 len txmm |
| 369 | etnum get etc 3 |
| 370 | r = write 1 txns 0 len txns |
| 371 | etnum get etc 4 |
| 372 | r = write 1 txcod 0 len txcod |
| 373 | for i ulong 7 |
| 374 | r = write 1 txsp 0 1 |
| 375 | etnum get etc add 8 i |
| 376 | end |
| 377 | r = write 1 txnl 0 1 |
| 378 | if or gt get etc 3 0 eq get etc 2 0 |
| 379 | ret 1 |
| 380 | end |
| 381 | ret 0 |
| 382 | end |