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 / rsa / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: test driver for om/tls/rsa.om (program mode). Built as: om2 line, sha2.om, rsa.om, |
| 3 | note this file. Reads a vector file on stdin, one case per line (see gen_vectors.py): |
| 4 | note EXP MODE CTL KEY MSG DIG SIG ID |
| 5 | note EXP 0..5 exact return code or n (any nonzero); MODE p (pkcs1, CTL = hash) or s (pss, CTL = ctl); |
| 6 | note KEY / MSG / DIG / SIG hex ('-' empty; MSG '*' = use DIG verbatim, no hashing). |
| 7 | note For every MSG the driver computes sha2_digest with the CTL hash and requires it to equal DIG |
| 8 | note (the converter's hashlib digest); hs = that digest || SIG goes to the verify call. |
| 9 | note Negative control: every case expected 0 that verifies is run again with one signature byte |
| 10 | note flipped (bit 0 of the middle byte) and must be refused. |
| 11 | note Optional argument: repeat count R (each verify runs R times; for timing). |
| 12 | note Output: one MISMATCH line per wrong result, then |
| 13 | note cases C verifies V mismatches X controls K ns T |
| 14 | note Exit 0 only when at least one verify ran and nothing mismatched; 1 otherwise; 65 bad input. |
| 15 | |
| 16 | text rvtmis "MISMATCH line " |
| 17 | text rvtgot " got " |
| 18 | text rvtexp " want " |
| 19 | text rvtdig " digest" |
| 20 | text rvtctl " control" |
| 21 | text rvtvfy " verify" |
| 22 | text rvtcase "cases " |
| 23 | text rvtver " verifies " |
| 24 | text rvtmm " mismatches " |
| 25 | text rvtcon " controls " |
| 26 | text rvtns " ns " |
| 27 | text rvtnl "\n" |
| 28 | text rvtbad "driver: bad input\n" |
| 29 | |
| 30 | global rvin uchar 16777216 |
| 31 | global rvout uchar 32 |
| 32 | global rvarg uchar 32 |
| 33 | global rvcnt ulong 16 |
| 34 | global rvf ulong 16 |
| 35 | |
| 36 | fn rv_bad to void |
| 37 | r = write 2 rvtbad 0 len rvtbad |
| 38 | exit 65 |
| 39 | end |
| 40 | |
| 41 | fn rv_num v ulong to void |
| 42 | k = ulong 0 |
| 43 | x = v |
| 44 | while true bound 30 |
| 45 | put rvout 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 rvout sub 32 k k |
| 53 | end |
| 54 | |
| 55 | fn rv_nib c ulong to ulong |
| 56 | if and ge c 48 le c 57 |
| 57 | ret sub c 48 |
| 58 | end |
| 59 | if and ge c 97 le c 102 |
| 60 | ret sub c 87 |
| 61 | end |
| 62 | rv_bad |
| 63 | ret 0 |
| 64 | end |
| 65 | |
| 66 | note end of the token starting at p (the next space or newline) |
| 67 | fn rv_tok p ulong to ulong |
| 68 | e = p |
| 69 | while and ne get rvin e 32 ne get rvin e 10 bound 999999 |
| 70 | e = add e 1 |
| 71 | end |
| 72 | ret e |
| 73 | end |
| 74 | |
| 75 | note hex rvin[p..q) into d; '-' is empty; returns the byte count |
| 76 | fn rv_hex d edit uchar * p ulong q ulong to ulong |
| 77 | if and eq sub q p 1 eq get rvin p 45 |
| 78 | ret 0 |
| 79 | end |
| 80 | h = sub q p |
| 81 | if ne and h 1 0 |
| 82 | rv_bad |
| 83 | end |
| 84 | c = div h 2 |
| 85 | if gt c len d |
| 86 | rv_bad |
| 87 | end |
| 88 | for i ulong c |
| 89 | o = add p mul i 2 |
| 90 | hi = rv_nib ulong get rvin o |
| 91 | lo = rv_nib ulong get rvin add o 1 |
| 92 | put d i uchar or shl hi 4 lo |
| 93 | end |
| 94 | ret c |
| 95 | end |
| 96 | |
| 97 | fn rv_dec p ulong q ulong to ulong |
| 98 | v = ulong 0 |
| 99 | for i ulong sub q p |
| 100 | c = ulong get rvin add p i |
| 101 | if or lt c 48 gt c 57 |
| 102 | rv_bad |
| 103 | end |
| 104 | v = add mul v 10 sub c 48 |
| 105 | end |
| 106 | ret v |
| 107 | end |
| 108 | |
| 109 | fn rv_slurp to void |
| 110 | t = ulong 0 |
| 111 | while true bound 999999 |
| 112 | room = sub len rvin t |
| 113 | if eq room 0 |
| 114 | rv_bad |
| 115 | end |
| 116 | r = read 0 rvin t room |
| 117 | if lt r 0 |
| 118 | rv_bad |
| 119 | end |
| 120 | if eq r 0 |
| 121 | break |
| 122 | end |
| 123 | t = add t ulong r |
| 124 | end |
| 125 | put rvcnt 6 t |
| 126 | end |
| 127 | |
| 128 | fn rv_reps to ulong |
| 129 | if lt argcnt 2 |
| 130 | ret 1 |
| 131 | end |
| 132 | l = argread 1 rvarg 0 20 |
| 133 | v = ulong 0 |
| 134 | for i ulong ulong l |
| 135 | v = add mul v 10 sub ulong get rvarg i 48 |
| 136 | end |
| 137 | ret v |
| 138 | end |
| 139 | |
| 140 | note counters in rvcnt: 0 line, 1 cases, 2 verifies, 3 mismatches, 4 ns, 5 repeats, 6 input length, |
| 141 | note 7 controls |
| 142 | fn rv_bump i ulong v ulong to void |
| 143 | put rvcnt i add get rvcnt i v |
| 144 | end |
| 145 | |
| 146 | fn rv_mis tag view uchar * got ulong want ulong to void |
| 147 | rv_bump 3 1 |
| 148 | r = write 1 rvtmis 0 len rvtmis |
| 149 | rv_num get rvcnt 0 |
| 150 | r = write 1 tag 0 len tag |
| 151 | r = write 1 rvtgot 0 len rvtgot |
| 152 | rv_num got |
| 153 | r = write 1 rvtexp 0 len rvtexp |
| 154 | rv_num want |
| 155 | r = write 1 rvtnl 0 1 |
| 156 | end |
| 157 | |
| 158 | note one verify call: mode (rvcnt cell 8) 112 'p' or 115 's' |
| 159 | fn rv_call c ulong key view uchar * hs view uchar * hl ulong to ulong |
| 160 | if eq get rvcnt 8 112 |
| 161 | ret rsa_verify_pkcs1 c key hs hl |
| 162 | end |
| 163 | ret rsa_verify_pss c key hs hl |
| 164 | end |
| 165 | |
| 166 | note run the verify R times; returns the last result |
| 167 | fn rv_run c ulong key view uchar * hs view uchar * hl ulong to ulong |
| 168 | got = ulong 0 |
| 169 | for k ulong get rvcnt 5 |
| 170 | t0 = monons |
| 171 | got = rv_call c key hs hl |
| 172 | rv_bump 4 ulong sub monons t0 |
| 173 | rv_bump 2 1 |
| 174 | end |
| 175 | ret got |
| 176 | end |
| 177 | |
| 178 | note judge result got against expectation x (99 = any nonzero); 0 ok, 1 mismatch |
| 179 | fn rv_judge got ulong x ulong to ulong |
| 180 | if eq x 99 |
| 181 | if eq got 0 |
| 182 | ret 1 |
| 183 | end |
| 184 | ret 0 |
| 185 | end |
| 186 | if ne got x |
| 187 | ret 1 |
| 188 | end |
| 189 | ret 0 |
| 190 | end |
| 191 | |
| 192 | note digest of msg with alg into dc; must equal the converter digest dg (length dl); returns the |
| 193 | note digest length to use (dl when alg is not a sha2 hash: dg is used as given) |
| 194 | fn rv_digest alg ulong m view uchar * dg edit uchar 128 dl ulong to ulong |
| 195 | dc = new uchar 64 0 |
| 196 | n = sha2_digest alg m dc |
| 197 | if eq n 0 |
| 198 | ret dl |
| 199 | end |
| 200 | d = ulong 0 |
| 201 | if ne n dl |
| 202 | d = 1 |
| 203 | end |
| 204 | if eq n dl |
| 205 | for i ulong n |
| 206 | d = or d ulong xor get dc i get dg i |
| 207 | end |
| 208 | end |
| 209 | if ne d 0 |
| 210 | rv_mis rvtdig n dl |
| 211 | end |
| 212 | ret dl |
| 213 | end |
| 214 | |
| 215 | note token bounds of the 8 fields of the line at p into rvf (start, end pairs) |
| 216 | fn rv_split p ulong to void |
| 217 | for t ulong 8 |
| 218 | q = rv_tok p |
| 219 | put rvf mul t 2 p |
| 220 | put rvf add mul t 2 1 q |
| 221 | p = add q 1 |
| 222 | end |
| 223 | end |
| 224 | |
| 225 | note MSG field (unless '*'): hash it, cross-check DIG; returns the digest length for hs |
| 226 | fn rv_msgdig msg edit uchar 4096 dg edit uchar 128 dl ulong to ulong |
| 227 | p = get rvf 8 |
| 228 | q = get rvf 9 |
| 229 | if and eq sub q p 1 eq get rvin p 42 |
| 230 | ret dl |
| 231 | end |
| 232 | ml = rv_hex part msg 0 4096 p q |
| 233 | alg = get rvcnt 10 |
| 234 | if eq get rvcnt 8 115 |
| 235 | alg = and alg 0xffff |
| 236 | end |
| 237 | ret rv_digest alg part msg 0 ml dg dl |
| 238 | end |
| 239 | |
| 240 | note parse the split line: rvcnt 8 mode, 9 expectation, 10 ctl, 11 key length, 12 digest |
| 241 | note length, 13 signature length; hs = digest || signature |
| 242 | fn rv_parse key edit uchar 2048 msg edit uchar 4096 dg edit uchar 128 hs edit uchar 2200 to void |
| 243 | x = ulong 99 |
| 244 | if ne get rvin get rvf 0 110 |
| 245 | x = rv_dec get rvf 0 get rvf 1 |
| 246 | end |
| 247 | put rvcnt 9 x |
| 248 | put rvcnt 8 ulong get rvin get rvf 2 |
| 249 | put rvcnt 10 rv_dec get rvf 4 get rvf 5 |
| 250 | put rvcnt 11 rv_hex part key 0 2048 get rvf 6 get rvf 7 |
| 251 | dl = rv_hex part dg 0 128 get rvf 10 get rvf 11 |
| 252 | dl = rv_msgdig msg dg dl |
| 253 | for i ulong dl |
| 254 | put hs i get dg i |
| 255 | end |
| 256 | put rvcnt 12 dl |
| 257 | put rvcnt 13 rv_hex part hs dl sub 2200 dl get rvf 12 get rvf 13 |
| 258 | end |
| 259 | |
| 260 | note flipped-byte control: bit 0 of the middle signature byte flipped must be refused |
| 261 | fn rv_ctrl key view uchar 2048 hs edit uchar 2200 to void |
| 262 | dl = get rvcnt 12 |
| 263 | n = add dl get rvcnt 13 |
| 264 | fx = add dl div get rvcnt 13 2 |
| 265 | put hs fx xor get hs fx 1 |
| 266 | rv_bump 7 1 |
| 267 | cg = rv_call get rvcnt 10 part key 0 get rvcnt 11 part hs 0 n dl |
| 268 | if eq cg 0 |
| 269 | rv_mis rvtctl cg 99 |
| 270 | end |
| 271 | end |
| 272 | |
| 273 | note run the parsed case and judge it; cases expected valid that verify get the control |
| 274 | fn rv_check key view uchar 2048 hs edit uchar 2200 to void |
| 275 | dl = get rvcnt 12 |
| 276 | n = add dl get rvcnt 13 |
| 277 | x = get rvcnt 9 |
| 278 | got = rv_run get rvcnt 10 part key 0 get rvcnt 11 part hs 0 n dl |
| 279 | if ne rv_judge got x 0 |
| 280 | rv_mis rvtvfy got x |
| 281 | end |
| 282 | if and eq x 0 and eq got 0 gt get rvcnt 13 0 |
| 283 | rv_ctrl key hs |
| 284 | end |
| 285 | end |
| 286 | |
| 287 | fn rv_line key edit uchar 2048 msg edit uchar 4096 dg edit uchar 128 hs edit uchar 2200 p ulong to void |
| 288 | rv_bump 1 1 |
| 289 | rv_split p |
| 290 | rv_parse key msg dg hs |
| 291 | rv_check key hs |
| 292 | end |
| 293 | |
| 294 | fn main to int |
| 295 | key = new uchar 2048 0 |
| 296 | msg = new uchar 4096 0 |
| 297 | dg = new uchar 128 0 |
| 298 | hs = new uchar 2200 0 |
| 299 | rv_slurp |
| 300 | put rvcnt 5 rv_reps |
| 301 | tot = get rvcnt 6 |
| 302 | p = ulong 0 |
| 303 | while lt p tot bound 999999 |
| 304 | rv_bump 0 1 |
| 305 | e = p |
| 306 | while ne get rvin e 10 bound 999999 |
| 307 | e = add e 1 |
| 308 | end |
| 309 | if ne get rvin p 35 |
| 310 | rv_line key msg dg hs p |
| 311 | end |
| 312 | p = add e 1 |
| 313 | end |
| 314 | r = write 1 rvtcase 0 len rvtcase |
| 315 | rv_num get rvcnt 1 |
| 316 | r = write 1 rvtver 0 len rvtver |
| 317 | rv_num get rvcnt 2 |
| 318 | r = write 1 rvtmm 0 len rvtmm |
| 319 | rv_num get rvcnt 3 |
| 320 | r = write 1 rvtcon 0 len rvtcon |
| 321 | rv_num get rvcnt 7 |
| 322 | r = write 1 rvtns 0 len rvtns |
| 323 | rv_num get rvcnt 4 |
| 324 | r = write 1 rvtnl 0 1 |
| 325 | if or gt get rvcnt 3 0 eq get rvcnt 2 0 |
| 326 | ret 1 |
| 327 | end |
| 328 | ret 0 |
| 329 | end |