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 / protocol / omnet / om / argon2 / test / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: program-mode test driver for om/argon2 (concatenated after om/blake2b/blake2b.om, |
| 3 | note om/argon2/argon2.om and om/argon2/argon2pool.om). |
| 4 | note |
| 5 | note Reads one case per line from stdin and writes one line per case on stdout: |
| 6 | note T M P TL PW SALT KEY AD\n Argon2i (a2hash): decimal t, m KiB, p, tag length, then hex |
| 7 | note password, salt, secret K and associated data X ("-" = empty); |
| 8 | note output: 2*TL lowercase hex digits, or "e1" when a2hash returns 1 |
| 9 | note tka SEC\n TKA disablement value (a2tka) of the hex secret SEC ("-" = empty) |
| 10 | note output: 64 lowercase hex digits |
| 11 | note Decimals have at most 7 digits; a hex field is at most 65536 bytes (SEC at most 1024) and all |
| 12 | note four together must fit the 65496-byte input. Exit 0 at end of input, 2 on a malformed line. |
| 13 | |
| 14 | global ib uchar 65536 |
| 15 | global ist ulong 2 |
| 16 | |
| 17 | note next input byte, -1 at end of input |
| 18 | fn getc to int |
| 19 | if eq get ist 0 get ist 1 |
| 20 | r = read 0 ib 0 65536 |
| 21 | if le r 0 |
| 22 | ret -1 |
| 23 | end |
| 24 | put ist 0 0 |
| 25 | put ist 1 ulong r |
| 26 | end |
| 27 | p = get ist 0 |
| 28 | put ist 0 add p 1 |
| 29 | ret int get ib p |
| 30 | end |
| 31 | |
| 32 | fn hexv c int to int |
| 33 | if and ge c 48 le c 57 |
| 34 | ret sub c 48 |
| 35 | end |
| 36 | if and ge c 97 le c 102 |
| 37 | ret sub c 87 |
| 38 | end |
| 39 | if and ge c 65 le c 70 |
| 40 | ret sub c 55 |
| 41 | end |
| 42 | ret -1 |
| 43 | end |
| 44 | |
| 45 | fn hexd v ulong to uchar |
| 46 | if lt v 10 |
| 47 | ret uchar add v 48 |
| 48 | end |
| 49 | ret uchar add v 87 |
| 50 | end |
| 51 | |
| 52 | note a hex field into a[at ..) of at most cap bytes, ended by the byte sep; its length, or -1 |
| 53 | fn rdhex a edit uchar 65536 at ulong cap ulong sep int to long |
| 54 | c = getc |
| 55 | if eq c 45 |
| 56 | if eq getc sep |
| 57 | ret 0 |
| 58 | end |
| 59 | ret -1 |
| 60 | end |
| 61 | t = ulong 0 |
| 62 | while ne c sep bound 999999 |
| 63 | if ge t cap |
| 64 | ret -1 |
| 65 | end |
| 66 | hi = hexv c |
| 67 | lo = hexv getc |
| 68 | if or lt hi 0 lt lo 0 |
| 69 | ret -1 |
| 70 | end |
| 71 | put a add at t uchar add mul hi 16 lo |
| 72 | t = add t 1 |
| 73 | c = getc |
| 74 | end |
| 75 | if eq t 0 |
| 76 | ret -1 |
| 77 | end |
| 78 | ret long t |
| 79 | end |
| 80 | |
| 81 | note decimal ended by a space, first byte c already read; -1 when malformed |
| 82 | fn rddec c int to long |
| 83 | t = long 0 |
| 84 | k = ulong 0 |
| 85 | d = c |
| 86 | while ne d 32 bound 16 |
| 87 | if or lt d 48 gt d 57 |
| 88 | ret -1 |
| 89 | end |
| 90 | if ge k 7 |
| 91 | ret -1 |
| 92 | end |
| 93 | t = add mul t 10 long sub d 48 |
| 94 | k = add k 1 |
| 95 | d = getc |
| 96 | end |
| 97 | if eq k 0 |
| 98 | ret -1 |
| 99 | end |
| 100 | ret t |
| 101 | end |
| 102 | |
| 103 | note o[0..n) as lowercase hex and a newline |
| 104 | fn emit o view uchar 1024 n ulong to void |
| 105 | h = new uchar 2049 0 |
| 106 | for i ulong n |
| 107 | v = ulong get o i |
| 108 | put h mul i 2 hexd shr v 4 |
| 109 | put h add mul i 2 1 hexd and v 15 |
| 110 | end |
| 111 | put h mul n 2 10 |
| 112 | r = write 1 h 0 add mul n 2 1 |
| 113 | if ne r long add mul n 2 1 |
| 114 | trap 1 |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | note "tka SEC" after the t: 0 done, 2 malformed |
| 119 | fn tkaline to int |
| 120 | if ne getc 107 |
| 121 | ret 2 |
| 122 | end |
| 123 | if ne getc 97 |
| 124 | ret 2 |
| 125 | end |
| 126 | if ne getc 32 |
| 127 | ret 2 |
| 128 | end |
| 129 | b = new uchar 65536 0 |
| 130 | n = rdhex b 0 1024 10 |
| 131 | if lt n 0 |
| 132 | ret 2 |
| 133 | end |
| 134 | s = new uchar 1024 0 |
| 135 | for i ulong ulong n |
| 136 | put s i get b i |
| 137 | end |
| 138 | o = new uchar 32 0 |
| 139 | a2tka s ulong n o |
| 140 | e = new uchar 1024 0 |
| 141 | for i ulong 32 |
| 142 | put e i get o i |
| 143 | end |
| 144 | emit e 32 |
| 145 | ret 0 |
| 146 | end |
| 147 | |
| 148 | note "T M P TL PW SALT KEY AD" with the first byte c read: 0 done, 2 malformed |
| 149 | fn hashline c int to int |
| 150 | prm = new ulong 8 0 |
| 151 | v = rddec c |
| 152 | for i ulong 4 |
| 153 | if lt v 0 |
| 154 | ret 2 |
| 155 | end |
| 156 | put prm i ulong v |
| 157 | if lt i 3 |
| 158 | v = rddec getc |
| 159 | end |
| 160 | end |
| 161 | inp = new uchar 65536 0 |
| 162 | at = ulong 0 |
| 163 | for f ulong 4 |
| 164 | sep = int 32 |
| 165 | if eq f 3 |
| 166 | sep = 10 |
| 167 | end |
| 168 | n = rdhex inp at sub 65496 at sep |
| 169 | if lt n 0 |
| 170 | ret 2 |
| 171 | end |
| 172 | put prm add 4 f ulong n |
| 173 | at = add at ulong n |
| 174 | end |
| 175 | out = new uchar 1024 0 |
| 176 | e = a2hash prm inp out |
| 177 | if ne e 0 |
| 178 | put out 0 uchar add e 224 |
| 179 | emit out 1 |
| 180 | ret 0 |
| 181 | end |
| 182 | emit out get prm 3 |
| 183 | ret 0 |
| 184 | end |
| 185 | |
| 186 | fn main to int |
| 187 | while true bound 999999 |
| 188 | c = getc |
| 189 | if eq c -1 |
| 190 | ret 0 |
| 191 | end |
| 192 | r = int 0 |
| 193 | if eq c 116 |
| 194 | r = tkaline |
| 195 | else |
| 196 | r = hashline c |
| 197 | end |
| 198 | if ne r 0 |
| 199 | ret r |
| 200 | end |
| 201 | end |
| 202 | ret 2 |
| 203 | end |