omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / blake2s / test / driver.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note driver.om: program-mode test driver for om/blake2s/blake2s.om (concatenated after it). |
| 3 | note |
| 4 | note driver reads case lines from stdin, one digest line per case on stdout: |
| 5 | note OUTN KEY MSG\n |
| 6 | note OUTN decimal 1..32; KEY and MSG are hex (either case, even digit count) or |
| 7 | note "-" for empty; KEY at most 32 bytes, MSG at most 65536 bytes. Output is |
| 8 | note 2*OUTN lowercase hex digits and a newline. Exit 0 at end of input, |
| 9 | note 2 on a malformed line (the digests already written stay on stdout). |
| 10 | note driver selftest runs the RFC 7693 Appendix E BLAKE2s self-test and prints its grand hash |
| 11 | note (64 hex digits); exit 0 when it equals the RFC value, 1 otherwise. |
| 12 | note The self-test is translated from RFC 7693 Appendix E; notice: om/blake2s/test/vectors/rfc7693.NOTICE. |
| 13 | |
| 14 | text tself "selftest" |
| 15 | |
| 16 | global ib uchar 65536 |
| 17 | global ist ulong 2 |
| 18 | |
| 19 | note next input byte, -1 at end of input |
| 20 | fn getc to int |
| 21 | if eq get ist 0 get ist 1 |
| 22 | r = read 0 ib 0 65536 |
| 23 | if le r 0 |
| 24 | ret -1 |
| 25 | end |
| 26 | put ist 0 0 |
| 27 | put ist 1 ulong r |
| 28 | end |
| 29 | p = get ist 0 |
| 30 | put ist 0 add p 1 |
| 31 | ret int get ib p |
| 32 | end |
| 33 | |
| 34 | fn hexv c int to int |
| 35 | if and ge c 48 le c 57 |
| 36 | ret sub c 48 |
| 37 | end |
| 38 | if and ge c 97 le c 102 |
| 39 | ret sub c 87 |
| 40 | end |
| 41 | if and ge c 65 le c 70 |
| 42 | ret sub c 55 |
| 43 | end |
| 44 | ret -1 |
| 45 | end |
| 46 | |
| 47 | fn hexd v ulong to uchar |
| 48 | if lt v 10 |
| 49 | ret uchar add v 48 |
| 50 | end |
| 51 | ret uchar add v 87 |
| 52 | end |
| 53 | |
| 54 | note one byte from two hex digits, the first already read as c; -1 when not hex |
| 55 | fn hexbyte c int to int |
| 56 | hi = hexv c |
| 57 | lo = hexv getc |
| 58 | if or lt hi 0 lt lo 0 |
| 59 | ret -1 |
| 60 | end |
| 61 | ret add mul hi 16 lo |
| 62 | end |
| 63 | |
| 64 | note a hex field into a (at most cap bytes) ended by the byte sep; the length, or -1 |
| 65 | fn rdmsg a edit uchar 65536 cap ulong sep int to long |
| 66 | c = getc |
| 67 | if eq c 45 |
| 68 | if eq getc sep |
| 69 | ret 0 |
| 70 | end |
| 71 | ret -1 |
| 72 | end |
| 73 | t = ulong 0 |
| 74 | while ne c sep bound 999999 |
| 75 | if ge t cap |
| 76 | ret -1 |
| 77 | end |
| 78 | v = hexbyte c |
| 79 | if lt v 0 |
| 80 | ret -1 |
| 81 | end |
| 82 | put a t uchar v |
| 83 | t = add t 1 |
| 84 | c = getc |
| 85 | end |
| 86 | if eq t 0 |
| 87 | ret -1 |
| 88 | end |
| 89 | ret long t |
| 90 | end |
| 91 | |
| 92 | fn rdkey a edit uchar 32 sep int to long |
| 93 | c = getc |
| 94 | if eq c 45 |
| 95 | if eq getc sep |
| 96 | ret 0 |
| 97 | end |
| 98 | ret -1 |
| 99 | end |
| 100 | t = ulong 0 |
| 101 | while ne c sep bound 999999 |
| 102 | if ge t 32 |
| 103 | ret -1 |
| 104 | end |
| 105 | v = hexbyte c |
| 106 | if lt v 0 |
| 107 | ret -1 |
| 108 | end |
| 109 | put a t uchar v |
| 110 | t = add t 1 |
| 111 | c = getc |
| 112 | end |
| 113 | if eq t 0 |
| 114 | ret -1 |
| 115 | end |
| 116 | ret long t |
| 117 | end |
| 118 | |
| 119 | note decimal OUTN ended by a space; -2 at end of input before any byte, -1 when malformed |
| 120 | fn rdoutn to long |
| 121 | c = getc |
| 122 | if eq c -1 |
| 123 | ret -2 |
| 124 | end |
| 125 | t = long 0 |
| 126 | k = ulong 0 |
| 127 | while ne c 32 bound 16 |
| 128 | d = sub c 48 |
| 129 | if or lt d 0 gt d 9 |
| 130 | ret -1 |
| 131 | end |
| 132 | if ge k 2 |
| 133 | ret -1 |
| 134 | end |
| 135 | t = add mul t 10 long d |
| 136 | k = add k 1 |
| 137 | c = getc |
| 138 | end |
| 139 | if or lt t 1 gt t 32 |
| 140 | ret -1 |
| 141 | end |
| 142 | ret t |
| 143 | end |
| 144 | |
| 145 | note o[0..outn) as lowercase hex and a newline |
| 146 | fn emit o view uchar 32 outn ulong to void |
| 147 | h = new uchar 65 0 |
| 148 | for i ulong outn |
| 149 | v = ulong get o i |
| 150 | put h mul i 2 hexd shr v 4 |
| 151 | put h add mul i 2 1 hexd and v 15 |
| 152 | end |
| 153 | put h mul outn 2 10 |
| 154 | r = write 1 h 0 add mul outn 2 1 |
| 155 | if ne r long add mul outn 2 1 |
| 156 | trap 1 |
| 157 | end |
| 158 | end |
| 159 | |
| 160 | fn cases to int |
| 161 | m = new uchar 65536 0 |
| 162 | k = new uchar 32 0 |
| 163 | o = new uchar 32 0 |
| 164 | while true bound 999999 |
| 165 | on = rdoutn |
| 166 | if eq on -2 |
| 167 | ret 0 |
| 168 | end |
| 169 | if lt on 0 |
| 170 | ret 2 |
| 171 | end |
| 172 | kn = rdkey edit k 32 |
| 173 | if lt kn 0 |
| 174 | ret 2 |
| 175 | end |
| 176 | n = rdmsg edit m 65536 10 |
| 177 | if lt n 0 |
| 178 | ret 2 |
| 179 | end |
| 180 | b2shash view m ulong n view k ulong kn ulong on edit o |
| 181 | emit view o ulong on |
| 182 | end |
| 183 | ret 2 |
| 184 | end |
| 185 | |
| 186 | note RFC 7693 Appendix E selftest_seq: a = 0xDEAD4BAD * seed, b = 1, out[i] = (a+b) >> 24 (Fibonacci) |
| 187 | fn seqm a edit uchar 65536 n ulong seed ulong to void |
| 188 | x = and mul 3735899053 seed 4294967295 |
| 189 | y = ulong 1 |
| 190 | for i ulong n |
| 191 | t = and add x y 4294967295 |
| 192 | x = y |
| 193 | y = t |
| 194 | put a i uchar shr t 24 |
| 195 | end |
| 196 | end |
| 197 | |
| 198 | fn seqk a edit uchar 32 n ulong seed ulong to void |
| 199 | x = and mul 3735899053 seed 4294967295 |
| 200 | y = ulong 1 |
| 201 | for i ulong n |
| 202 | t = and add x y 4294967295 |
| 203 | x = y |
| 204 | y = t |
| 205 | put a i uchar shr t 24 |
| 206 | end |
| 207 | end |
| 208 | |
| 209 | note RFC 7693 Appendix E md_len and in_len tables |
| 210 | fn mdlen i ulong to ulong |
| 211 | if eq i 0 |
| 212 | ret 16 |
| 213 | end |
| 214 | if eq i 1 |
| 215 | ret 20 |
| 216 | end |
| 217 | if eq i 2 |
| 218 | ret 28 |
| 219 | end |
| 220 | ret 32 |
| 221 | end |
| 222 | |
| 223 | fn inlen j ulong to ulong |
| 224 | if eq j 0 |
| 225 | ret 0 |
| 226 | end |
| 227 | if eq j 1 |
| 228 | ret 3 |
| 229 | end |
| 230 | if eq j 2 |
| 231 | ret 64 |
| 232 | end |
| 233 | if eq j 3 |
| 234 | ret 65 |
| 235 | end |
| 236 | if eq j 4 |
| 237 | ret 255 |
| 238 | end |
| 239 | ret 1024 |
| 240 | end |
| 241 | |
| 242 | note append md[0..n) to g at offset at |
| 243 | fn append g edit uchar 65536 at ulong md view uchar 32 n ulong to void |
| 244 | for i ulong n |
| 245 | put g add at i get md i |
| 246 | end |
| 247 | end |
| 248 | |
| 249 | note RFC 7693 Appendix E grand-hash bytes 6A 41 1F 08 CE 25 AD CD FB 02 AB A6 41 45 1C EC |
| 250 | note 53 C5 98 B2 4F 4F C7 87 FB DC 88 79 7F 4C 1D FE |
| 251 | fn wantres r edit uchar 32 to void |
| 252 | put r 0 106 |
| 253 | put r 1 65 |
| 254 | put r 2 31 |
| 255 | put r 3 8 |
| 256 | put r 4 206 |
| 257 | put r 5 37 |
| 258 | put r 6 173 |
| 259 | put r 7 205 |
| 260 | put r 8 251 |
| 261 | put r 9 2 |
| 262 | put r 10 171 |
| 263 | put r 11 166 |
| 264 | put r 12 65 |
| 265 | put r 13 69 |
| 266 | put r 14 28 |
| 267 | put r 15 236 |
| 268 | put r 16 83 |
| 269 | put r 17 197 |
| 270 | put r 18 152 |
| 271 | put r 19 178 |
| 272 | put r 20 79 |
| 273 | put r 21 79 |
| 274 | put r 22 199 |
| 275 | put r 23 135 |
| 276 | put r 24 251 |
| 277 | put r 25 220 |
| 278 | put r 26 136 |
| 279 | put r 27 121 |
| 280 | put r 28 127 |
| 281 | put r 29 76 |
| 282 | put r 30 29 |
| 283 | put r 31 254 |
| 284 | end |
| 285 | |
| 286 | note the grand hash is BLAKE2s-256 of all 48 digests in order; feeding them one update at a time |
| 287 | note (as Appendix E does) and hashing their concatenation once give the same digest |
| 288 | fn selftst to int |
| 289 | m = new uchar 65536 0 |
| 290 | g = new uchar 65536 0 |
| 291 | k = new uchar 32 0 |
| 292 | md = new uchar 32 0 |
| 293 | r = new uchar 32 0 |
| 294 | at = ulong 0 |
| 295 | for i ulong 4 |
| 296 | ol = mdlen i |
| 297 | for j ulong 6 |
| 298 | il = inlen j |
| 299 | seqm edit m il il |
| 300 | b2shash view m il view k 0 ol edit md |
| 301 | append edit g at view md ol |
| 302 | at = add at ol |
| 303 | seqk edit k ol ol |
| 304 | b2shash view m il view k ol ol edit md |
| 305 | append edit g at view md ol |
| 306 | at = add at ol |
| 307 | end |
| 308 | end |
| 309 | b2shash view g at view k 0 32 edit md |
| 310 | emit view md 32 |
| 311 | wantres edit r |
| 312 | bad = ulong 0 |
| 313 | for i ulong 32 |
| 314 | if ne get md i get r i |
| 315 | bad = 1 |
| 316 | end |
| 317 | end |
| 318 | ret int bad |
| 319 | end |
| 320 | |
| 321 | fn main to int |
| 322 | if eq argcnt 1 |
| 323 | ret cases |
| 324 | end |
| 325 | a = new uchar 16 0 |
| 326 | l = argread 1 a 0 16 |
| 327 | if ne l 8 |
| 328 | ret 2 |
| 329 | end |
| 330 | for i ulong 8 |
| 331 | if ne get a i get tself i |
| 332 | ret 2 |
| 333 | end |
| 334 | end |
| 335 | ret selftst |
| 336 | end |