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 / seed / qualification / om2 / oracle / sha / sha_qual.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * ORACLE (outside the delivered closure). OM-2 qualification, L836 (#748), task phaseB/sha-f5d14cb2. |
| 3 | * Independent C driver for the OM-2 SHA unit om/sha256/sha256-om2.om bound through the omega-bridge |
| 4 | * ABI v2 generator. It never computes SHA-256 itself: every expected digest comes from |
| 5 | * /usr/bin/sha256sum (child process fed through a pipe), from the Phase A fixture file (external |
| 6 | * oracles), or is a self-consistency relation (copy == original, hash == stream). |
| 7 | * Calls go through the raw omega2_* symbols so a nonzero status is observed (TRAP line), never an |
| 8 | * abort; the bridge-generated omega_api.h supplies sha256_ctx only. |
| 9 | * |
| 10 | * Build (see PROVENANCE.txt): |
| 11 | * gcc -std=gnu11 -O2 -Wall -Wextra -Werror -I<bind> sha_qual.c -L<bind> -lomega_native \ |
| 12 | * -Wl,-rpath,<bind> -o sha_qual |
| 13 | * Modes (each prints PASS/FAIL/TRAP/NOT_RUN lines, then a TOTAL line; exit 0 iff no FAIL/TRAP): |
| 14 | * vec FILE ROOT fixtures/sha256-vectors.txt rows, three call paths each |
| 15 | * rcpt section 7 / E3 receipts, empty updates, refusals |
| 16 | * ident EXPECTED omega2_ident_sha256_om2() against the harness-computed string |
| 17 | * rand SEED COUNT seeded random messages, random chunkings, vs sha256sum; prints RAND lines |
| 18 | * part messages around the 2^25-byte run boundaries of blocks() |
| 19 | * big >= 2^32-byte single update, hash, > 2^32 total via many updates |
| 20 | * bigzero 2^32+3 zero bytes from a NORESERVE mapping (no resident memory) |
| 21 | * Line format of a failure: FAIL <id> first-diff-byte=<i> got=<hex> want=<hex> [detail] */ |
| 22 | #define _GNU_SOURCE |
| 23 | #include <dirent.h> |
| 24 | #include <errno.h> |
| 25 | #include <stdint.h> |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <string.h> |
| 29 | #include <sys/mman.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <unistd.h> |
| 32 | |
| 33 | #include "omega_api.h" |
| 34 | |
| 35 | static long npass, nfail, ntrap; |
| 36 | static uint32_t sq; /* sequenced status accumulator: (sq = a, sq |= b, ..., sq) */ |
| 37 | static const char *SHA256SUM = "/usr/bin/sha256sum"; |
| 38 | |
| 39 | static void hex(const uint8_t *d, char *h) |
| 40 | { |
| 41 | static const char x[] = "0123456789abcdef"; |
| 42 | for (int i = 0; i < 32; i++) { |
| 43 | h[2 * i] = x[d[i] >> 4]; |
| 44 | h[2 * i + 1] = x[d[i] & 15]; |
| 45 | } |
| 46 | h[64] = 0; |
| 47 | } |
| 48 | |
| 49 | static int unhex(const char *h, uint8_t *d) |
| 50 | { |
| 51 | for (int i = 0; i < 32; i++) { |
| 52 | unsigned v; |
| 53 | if (sscanf(h + 2 * i, "%2x", &v) != 1) |
| 54 | return -1; |
| 55 | d[i] = (uint8_t)v; |
| 56 | } |
| 57 | return 0; |
| 58 | } |
| 59 | |
| 60 | static void pass(const char *id, const char *what) |
| 61 | { |
| 62 | npass++; |
| 63 | printf("PASS %s %s\n", id, what); |
| 64 | } |
| 65 | |
| 66 | /* compare got against want (both binary); report the first differing byte */ |
| 67 | static int cmpd(const char *id, const uint8_t *got, const uint8_t *want, const char *what) |
| 68 | { |
| 69 | char hg[65], hw[65]; |
| 70 | for (int i = 0; i < 32; i++) |
| 71 | if (got[i] != want[i]) { |
| 72 | hex(got, hg); |
| 73 | hex(want, hw); |
| 74 | nfail++; |
| 75 | printf("FAIL %s first-diff-byte=%d got=%s want=%s %s\n", id, i, hg, hw, what); |
| 76 | return 0; |
| 77 | } |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static int trapped(const char *id, uint32_t s, const char *what) |
| 82 | { |
| 83 | if (s == 0) |
| 84 | return 0; |
| 85 | ntrap++; |
| 86 | printf("TRAP %s status=%u %s\n", id, s, what); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | /* raw calls; return status */ |
| 91 | static uint32_t I(sha256_ctx *c) |
| 92 | { |
| 93 | uint64_t r; |
| 94 | return omega2_sha256_init(&r, (uint8_t *)c); |
| 95 | } |
| 96 | static uint32_t U(sha256_ctx *c, const void *p, uint64_t n) |
| 97 | { |
| 98 | uint64_t r; |
| 99 | return omega2_sha256_update(&r, (uint8_t *)c, p, n); |
| 100 | } |
| 101 | static uint32_t F(sha256_ctx *c, uint8_t *o) |
| 102 | { |
| 103 | uint64_t r; |
| 104 | return omega2_sha256_final(&r, (uint8_t *)c, o); |
| 105 | } |
| 106 | static uint32_t H(const void *p, uint64_t n, uint8_t *o) |
| 107 | { |
| 108 | uint64_t r; |
| 109 | return omega2_sha256_hash(&r, p, n, o); |
| 110 | } |
| 111 | |
| 112 | /* /usr/bin/sha256sum of n bytes at p, via a pipe; exits 3 if the oracle cannot run */ |
| 113 | static void ref(const void *p, size_t n, uint8_t *d) |
| 114 | { |
| 115 | int in[2], out[2]; |
| 116 | if (pipe(in) || pipe(out)) { |
| 117 | perror("pipe"); |
| 118 | exit(3); |
| 119 | } |
| 120 | pid_t pid = fork(); |
| 121 | if (pid < 0) { |
| 122 | perror("fork"); |
| 123 | exit(3); |
| 124 | } |
| 125 | if (pid == 0) { |
| 126 | dup2(in[0], 0); |
| 127 | dup2(out[1], 1); |
| 128 | close(in[0]); |
| 129 | close(in[1]); |
| 130 | close(out[0]); |
| 131 | close(out[1]); |
| 132 | execl(SHA256SUM, SHA256SUM, "-b", (char *)NULL); |
| 133 | _exit(127); |
| 134 | } |
| 135 | close(in[0]); |
| 136 | close(out[1]); |
| 137 | const uint8_t *q = p; |
| 138 | while (n > 0) { |
| 139 | size_t k = n > (1u << 24) ? (1u << 24) : n; |
| 140 | ssize_t w = write(in[1], q, k); |
| 141 | if (w < 0) { |
| 142 | if (errno == EINTR) |
| 143 | continue; |
| 144 | perror("write sha256sum"); |
| 145 | exit(3); |
| 146 | } |
| 147 | q += w; |
| 148 | n -= (size_t)w; |
| 149 | } |
| 150 | close(in[1]); |
| 151 | char buf[200]; |
| 152 | size_t got = 0; |
| 153 | ssize_t r; |
| 154 | while ((r = read(out[0], buf + got, sizeof buf - 1 - got)) > 0) |
| 155 | got += (size_t)r; |
| 156 | close(out[0]); |
| 157 | int st; |
| 158 | waitpid(pid, &st, 0); |
| 159 | buf[got] = 0; |
| 160 | if (!WIFEXITED(st) || WEXITSTATUS(st) != 0 || got < 64 || unhex(buf, d)) { |
| 161 | fprintf(stderr, "sha256sum oracle failed\n"); |
| 162 | exit(3); |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* splitmix64, the generator of fixtures/PROVENANCE.txt */ |
| 167 | static uint64_t sm_s; |
| 168 | static uint64_t sm(void) |
| 169 | { |
| 170 | uint64_t z = (sm_s += 0x9E3779B97F4A7C15ull); |
| 171 | z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; |
| 172 | z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; |
| 173 | return z ^ (z >> 31); |
| 174 | } |
| 175 | static void rndfill(uint8_t *b, size_t n, uint64_t seed) |
| 176 | { |
| 177 | sm_s = seed; |
| 178 | size_t i = 0; |
| 179 | while (i < n) { |
| 180 | uint64_t z = sm(); |
| 181 | for (int k = 0; k < 8 && i < n; k++, i++) |
| 182 | b[i] = (uint8_t)(z >> (8 * k)); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /* stream with the given chunk plan; returns status */ |
| 187 | static uint32_t stream(const uint8_t *m, size_t n, const size_t *plan, size_t np, uint8_t *d) |
| 188 | { |
| 189 | sha256_ctx c; |
| 190 | uint32_t s = I(&c); |
| 191 | size_t off = 0, i = 0; |
| 192 | while (!s && off < n) { |
| 193 | size_t k = np ? plan[i++ % np] : n; |
| 194 | if (k > n - off) |
| 195 | k = n - off; |
| 196 | s = U(&c, m + off, k); |
| 197 | off += k; |
| 198 | } |
| 199 | if (!s) |
| 200 | s = F(&c, d); |
| 201 | return s; |
| 202 | } |
| 203 | |
| 204 | /* A file: fixture name must be one plain component: letters, digits, dot, underscore, dash. It is |
| 205 | * joined to ROOT below, so anything that could leave ROOT is refused (no slash, no leading dot, |
| 206 | * therefore no ".."). */ |
| 207 | static int safe_name(const char *s) |
| 208 | { |
| 209 | size_t i, n = strlen(s); |
| 210 | if (n == 0 || n > 200) |
| 211 | return 0; |
| 212 | if (s[0] == '.') |
| 213 | return 0; |
| 214 | for (i = 0; i < n; i++) { |
| 215 | char ch = s[i]; |
| 216 | if (!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') |
| 217 | || ch == '.' || ch == '_' || ch == '-')) |
| 218 | return 0; |
| 219 | } |
| 220 | return 1; |
| 221 | } |
| 222 | |
| 223 | /* ---- vec ---------------------------------------------------------------------------------------- */ |
| 224 | static uint8_t *genmsg(const char *spec, const char *root, size_t n) |
| 225 | { |
| 226 | uint8_t *m = malloc(n ? n : 1); |
| 227 | if (!m) |
| 228 | exit(3); |
| 229 | if (strcmp(spec, "-") == 0) |
| 230 | return m; |
| 231 | if (strncmp(spec, "seq:", 4) == 0) { |
| 232 | for (size_t i = 0; i < n; i++) |
| 233 | m[i] = (uint8_t)(i % 251); |
| 234 | } else if (strncmp(spec, "rep:", 4) == 0) { |
| 235 | unsigned b = (unsigned)strtoul(spec + 4, NULL, 10); |
| 236 | memset(m, (int)b, n); |
| 237 | } else if (strncmp(spec, "rnd:", 4) == 0) { |
| 238 | rndfill(m, n, strtoull(spec + 4, NULL, 10)); |
| 239 | } else if (strncmp(spec, "file:", 5) == 0) { |
| 240 | char path[4096]; |
| 241 | const char *want = spec + 5; |
| 242 | struct dirent *ent; |
| 243 | DIR *d; |
| 244 | int len, found = 0; |
| 245 | if (!safe_name(want)) { |
| 246 | fprintf(stderr, "refused file name %s\n", want); |
| 247 | exit(3); |
| 248 | } |
| 249 | /* Resolve the name against the directory itself: only a name present in |
| 250 | * root can be opened, and the opened path is built from the directory |
| 251 | * entry, never from the vector text. */ |
| 252 | d = opendir(root); |
| 253 | if (!d) { |
| 254 | fprintf(stderr, "cannot open directory %s\n", root); |
| 255 | exit(3); |
| 256 | } |
| 257 | while ((ent = readdir(d)) != NULL) { |
| 258 | if (strcmp(ent->d_name, want) == 0) { |
| 259 | found = 1; |
| 260 | break; |
| 261 | } |
| 262 | } |
| 263 | if (!found) { |
| 264 | closedir(d); |
| 265 | fprintf(stderr, "no fixture named %s\n", want); |
| 266 | exit(3); |
| 267 | } |
| 268 | len = snprintf(path, sizeof path, "%s/%s", root, ent->d_name); |
| 269 | closedir(d); |
| 270 | if (len < 0 || (size_t)len >= sizeof path) { |
| 271 | fprintf(stderr, "path too long for %s\n", want); |
| 272 | exit(3); |
| 273 | } |
| 274 | { |
| 275 | FILE *f = fopen(path, "rb"); |
| 276 | if (!f || fread(m, 1, n, f) != n) { |
| 277 | fprintf(stderr, "cannot read %s\n", path); |
| 278 | exit(3); |
| 279 | } |
| 280 | fclose(f); |
| 281 | } |
| 282 | } else { |
| 283 | fprintf(stderr, "bad spec %s\n", spec); |
| 284 | exit(3); |
| 285 | } |
| 286 | return m; |
| 287 | } |
| 288 | |
| 289 | static const size_t plan_a[] = {1, 63, 64, 65, 0, 127, 3, 4096, 55}; |
| 290 | static const size_t plan_b[] = {7}; |
| 291 | |
| 292 | static int mode_vec(const char *file, const char *root) |
| 293 | { |
| 294 | FILE *f = fopen(file, "r"); |
| 295 | if (!f) |
| 296 | return 3; |
| 297 | char name[128], spec[512], dig[80]; |
| 298 | unsigned long long n; |
| 299 | long rows = 0; |
| 300 | while (fscanf(f, "%127s %511s %llu %79s", name, spec, &n, dig) == 4) { |
| 301 | uint8_t want[32], d[32]; |
| 302 | char id[200]; |
| 303 | rows++; |
| 304 | unhex(dig, want); |
| 305 | uint8_t *m = genmsg(spec, root, (size_t)n); |
| 306 | snprintf(id, sizeof id, "vec-%s-hash", name); |
| 307 | if (!trapped(id, H(m, n, d), "") && cmpd(id, d, want, "sha256_hash")) |
| 308 | npass++; |
| 309 | snprintf(id, sizeof id, "vec-%s-one", name); |
| 310 | if (!trapped(id, stream(m, n, NULL, 0, d), "") && cmpd(id, d, want, "init/update(n)/final")) |
| 311 | npass++; |
| 312 | snprintf(id, sizeof id, "vec-%s-chunkA", name); |
| 313 | if (!trapped(id, stream(m, n, plan_a, 9, d), "") && cmpd(id, d, want, "chunks 1,63,64,65,0,127,3,4096,55")) |
| 314 | npass++; |
| 315 | snprintf(id, sizeof id, "vec-%s-chunk7", name); |
| 316 | if (n <= 200000 && !trapped(id, stream(m, n, plan_b, 1, d), "") && cmpd(id, d, want, "chunks of 7")) |
| 317 | npass++; |
| 318 | free(m); |
| 319 | } |
| 320 | fclose(f); |
| 321 | printf("INFO vec rows=%ld\n", rows); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* ---- rcpt --------------------------------------------------------------------------------------- */ |
| 326 | static uint8_t msg[2000]; |
| 327 | |
| 328 | static void mode_rcpt(void) |
| 329 | { |
| 330 | sha256_ctx c, c2; |
| 331 | uint8_t d[32], d2[32], w[32]; |
| 332 | char id[160]; |
| 333 | int ok; |
| 334 | for (size_t i = 0; i < sizeof msg; i++) |
| 335 | msg[i] = (uint8_t)(i % 251); |
| 336 | |
| 337 | /* hash(n) == init/update(n)/final == sha256sum, n = 0..300 and 1000, 2000 */ |
| 338 | ok = 1; |
| 339 | for (size_t n = 0; n <= 2000 && ok; n = (n < 300 ? n + 1 : (n == 300 ? 1000 : n + 1000))) { |
| 340 | ref(msg, n, w); |
| 341 | snprintf(id, sizeof id, "rcpt-hash-eq-stream-n%zu", n); |
| 342 | if (trapped(id, H(msg, n, d), "hash") || trapped(id, stream(msg, n, NULL, 0, d2), "stream")) { |
| 343 | ok = 0; |
| 344 | break; |
| 345 | } |
| 346 | ok = cmpd(id, d, w, "hash vs sha256sum") && cmpd(id, d2, d, "init/update/final vs hash"); |
| 347 | } |
| 348 | if (ok) |
| 349 | pass("rcpt-hash-eq-stream", "hash(n) == init/update(n)/final == sha256sum, n = 0..300, 1000, 2000"); |
| 350 | |
| 351 | /* context byte copy after k updates (E3): continue both, equal digests, == sha256sum */ |
| 352 | ref(msg, 2000, w); |
| 353 | ok = 1; |
| 354 | for (size_t k = 0; k <= 2000 && ok; k += (k < 200 ? 1 : 61)) { |
| 355 | static uint8_t raw[104 + 16]; |
| 356 | sha256_ctx *c3 = (sha256_ctx *)(void *)(raw + 1 + (k % 7)); /* misaligned copy: layout alignment 1 */ |
| 357 | snprintf(id, sizeof id, "rcpt-ctx-copy-k%zu", k); |
| 358 | size_t a = k / 3, b = k - a; |
| 359 | if (trapped(id, (sq = I(&c), sq |= U(&c, msg, a), sq |= U(&c, msg + a, b), sq), "prefix")) |
| 360 | break; |
| 361 | memcpy(&c2, &c, sizeof c); |
| 362 | memcpy(c3, &c, sizeof c); |
| 363 | uint32_t s = U(&c, msg + k, 2000 - k); |
| 364 | s |= U(&c2, msg + k, (2000 - k) / 2); |
| 365 | s |= U(c3, msg + k, 2000 - k); |
| 366 | s |= U(&c2, msg + k + (2000 - k) / 2, 2000 - k - (2000 - k) / 2); |
| 367 | uint8_t d3[32]; |
| 368 | s |= F(&c, d); |
| 369 | s |= F(&c2, d2); |
| 370 | s |= F(c3, d3); |
| 371 | if (trapped(id, s, "continue")) |
| 372 | break; |
| 373 | ok = cmpd(id, d, w, "original") && cmpd(id, d2, w, "aligned copy") && cmpd(id, d3, w, "misaligned copy"); |
| 374 | } |
| 375 | if (ok) |
| 376 | pass("rcpt-ctx-copy", "copy after k updates (k = 0..200, then every 61 to 2000), both copies continued, digests equal == sha256sum"); |
| 377 | |
| 378 | /* hash(d, n, d + j): output overlapping input, j = -31..31, n in {32, 64, 100} */ |
| 379 | ok = 1; |
| 380 | for (int ni = 0; ni < 3 && ok; ni++) { |
| 381 | size_t n = ni == 0 ? 32 : ni == 1 ? 64 : 100; |
| 382 | ref(msg, n, w); |
| 383 | for (int j = -31; j <= 31 && ok; j++) { |
| 384 | static uint8_t ov[400]; |
| 385 | memset(ov, 0xa5, sizeof ov); |
| 386 | memcpy(ov + 100, msg, n); |
| 387 | snprintf(id, sizeof id, "rcpt-hash-overlap-n%zu-j%d", n, j); |
| 388 | if (trapped(id, H(ov + 100, n, ov + 100 + j), "")) { |
| 389 | ok = 0; |
| 390 | break; |
| 391 | } |
| 392 | ok = cmpd(id, ov + 100 + j, w, "digest written over input"); |
| 393 | } |
| 394 | } |
| 395 | if (ok) |
| 396 | pass("rcpt-hash-overlap", "hash(d, n, d+j) == sha256sum(original d) for n in {32,64,100}, j = -31..31 (incl. hash(d,32,d))"); |
| 397 | |
| 398 | /* reuse after final */ |
| 399 | ok = 1; |
| 400 | for (size_t n = 0; n <= 130 && ok; n++) { |
| 401 | ref(msg, n, w); |
| 402 | snprintf(id, sizeof id, "rcpt-post-final-n%zu", n); |
| 403 | uint32_t s = (sq = I(&c), sq |= U(&c, msg + 7, 77), sq |= F(&c, d2), sq); |
| 404 | s |= (sq = I(&c), sq |= U(&c, msg, n), sq |= F(&c, d), sq); |
| 405 | if (trapped(id, s, "")) |
| 406 | break; |
| 407 | ok = cmpd(id, d, w, "init/update/final after a final"); |
| 408 | /* no init after a final: memory safety only (digest unspecified) */ |
| 409 | s = (sq = U(&c, msg, n), sq |= F(&c, d2), sq |= F(&c, d2), sq); |
| 410 | if (ok && trapped(id, s, "update/final without init")) |
| 411 | ok = 0; |
| 412 | } |
| 413 | if (ok) |
| 414 | pass("rcpt-post-final", "init/update(n)/final after a final == sha256sum, n = 0..130; update/final without init returned status 0"); |
| 415 | |
| 416 | /* empty updates (NULL and non-NULL, including a bogus pointer) at every position of seq:200 */ |
| 417 | ref(msg, 200, w); |
| 418 | ok = 1; |
| 419 | static const void *const ep[3] = {NULL, msg, (const void *)(uintptr_t)0x10}; |
| 420 | for (size_t p = 0; p <= 200 && ok; p++) { |
| 421 | for (int e = 0; e < 3 && ok; e++) { |
| 422 | snprintf(id, sizeof id, "rcpt-empty-p%zu-%s", p, e == 0 ? "null" : e == 1 ? "ptr" : "bogus"); |
| 423 | uint32_t s = (sq = I(&c), sq |= U(&c, ep[e], 0), sq |= U(&c, msg, p), sq |= U(&c, ep[e], 0), |
| 424 | sq |= U(&c, ep[(e + 1) % 3], 0), sq |= U(&c, msg + p, 200 - p), sq |= U(&c, ep[e], 0), |
| 425 | sq |= F(&c, d), sq); |
| 426 | if (trapped(id, s, "")) |
| 427 | ok = 0; |
| 428 | else |
| 429 | ok = cmpd(id, d, w, "empty updates around a split"); |
| 430 | } |
| 431 | } |
| 432 | if (ok && !trapped("rcpt-empty-only", (sq = I(&c), sq |= U(&c, NULL, 0), sq |= U(&c, msg, 0), sq |= F(&c, d), sq), "")) { |
| 433 | ref(msg, 0, w); |
| 434 | ok = cmpd("rcpt-empty-only", d, w, "init, update(NULL,0), update(p,0), final"); |
| 435 | if (ok && !trapped("rcpt-empty-hash", (sq = H(NULL, 0, d), sq |= H((const void *)(uintptr_t)0x10, 0, d2), sq), "")) |
| 436 | ok = cmpd("rcpt-empty-hash", d, w, "hash(NULL,0)") && cmpd("rcpt-empty-hash", d2, w, "hash(bogus,0)"); |
| 437 | } |
| 438 | if (ok) |
| 439 | pass("rcpt-empty", "len-0 updates (NULL, valid, bogus 0x10 pointer) at every split 0..200, before first data, before final; hash(NULL,0)"); |
| 440 | |
| 441 | /* status-32 refusals through the real API; nothing written */ |
| 442 | { |
| 443 | uint8_t o[32], o0[32]; |
| 444 | sha256_ctx s0; |
| 445 | uint64_t r = 7; |
| 446 | const uint8_t *wrap = (const uint8_t *)(UINTPTR_MAX - 15); |
| 447 | int rok = 1; |
| 448 | I(&c); |
| 449 | U(&c, msg, 10); |
| 450 | memcpy(&s0, &c, sizeof c); |
| 451 | memset(o, 0x5a, 32); |
| 452 | memcpy(o0, o, 32); |
| 453 | struct { |
| 454 | const char *n; |
| 455 | uint32_t s; |
| 456 | } t[] = { |
| 457 | {"update-null-len1", omega2_sha256_update(&r, (uint8_t *)&c, NULL, 1)}, |
| 458 | {"update-null-len2^40", omega2_sha256_update(&r, (uint8_t *)&c, NULL, 1ull << 40)}, |
| 459 | {"update-wrap", omega2_sha256_update(&r, (uint8_t *)&c, wrap, 32)}, |
| 460 | {"update-wrap-end2^64", omega2_sha256_update(&r, (uint8_t *)&c, wrap, 16)}, |
| 461 | {"update-len-max", omega2_sha256_update(&r, (uint8_t *)&c, msg, UINT64_MAX)}, |
| 462 | {"update-ctx-null", omega2_sha256_update(&r, NULL, msg, 3)}, |
| 463 | {"update-ctx-wrap", omega2_sha256_update(&r, (uint8_t *)(UINTPTR_MAX - 50), msg, 3)}, |
| 464 | {"update-view-overlaps-ctx", omega2_sha256_update(&r, (uint8_t *)&c, (uint8_t *)&c + 100, 8)}, |
| 465 | {"update-result-in-ctx", omega2_sha256_update((uint64_t *)(void *)&c, (uint8_t *)&c, msg, 3)}, |
| 466 | {"update-result-null", omega2_sha256_update(NULL, (uint8_t *)&c, msg, 3)}, |
| 467 | {"init-null", omega2_sha256_init(&r, NULL)}, |
| 468 | {"final-out-null", omega2_sha256_final(&r, (uint8_t *)&c, NULL)}, |
| 469 | {"final-ctx-null", omega2_sha256_final(&r, NULL, o)}, |
| 470 | {"final-out-overlaps-ctx", omega2_sha256_final(&r, (uint8_t *)&c, (uint8_t *)&c + 90)}, |
| 471 | {"final-out-wrap", omega2_sha256_final(&r, (uint8_t *)&c, (uint8_t *)(UINTPTR_MAX - 20))}, |
| 472 | {"hash-null-len5", omega2_sha256_hash(&r, NULL, 5, o)}, |
| 473 | {"hash-wrap", omega2_sha256_hash(&r, wrap, 64, o)}, |
| 474 | {"hash-out-null", omega2_sha256_hash(&r, msg, 5, NULL)}, |
| 475 | {"hash-result-overlaps-out", omega2_sha256_hash((uint64_t *)(void *)o, msg, 5, o)}, |
| 476 | }; |
| 477 | for (size_t i = 0; i < sizeof t / sizeof t[0]; i++) { |
| 478 | snprintf(id, sizeof id, "rcpt-refuse-%s", t[i].n); |
| 479 | if (t[i].s != 32) { |
| 480 | nfail++; |
| 481 | rok = 0; |
| 482 | printf("FAIL %s status=%u want=32\n", id, t[i].s); |
| 483 | } else |
| 484 | printf("INFO %s status=32\n", id); |
| 485 | } |
| 486 | if (memcmp(&s0, &c, sizeof c) || memcmp(o, o0, 32) || r != 7) { |
| 487 | nfail++; |
| 488 | rok = 0; |
| 489 | printf("FAIL rcpt-refuse-nowrite context, out or result changed by a refused call\n"); |
| 490 | } |
| 491 | if (rok) |
| 492 | pass("rcpt-refuse", "19 refusals returned status 32; context, out and result untouched"); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* ---- ident -------------------------------------------------------------------------------------- */ |
| 497 | static void mode_ident(const char *want) |
| 498 | { |
| 499 | const char *got = omega2_ident_sha256_om2(); |
| 500 | if (strcmp(got, want) == 0 && strcmp(got, OMEGA2_IDENT_sha256_om2) == 0) |
| 501 | pass("ident", got); |
| 502 | else { |
| 503 | nfail++; |
| 504 | printf("FAIL ident got=\"%s\" want=\"%s\" header=\"%s\"\n", got, want, OMEGA2_IDENT_sha256_om2); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | /* ---- rand --------------------------------------------------------------------------------------- */ |
| 509 | static void mode_rand(uint64_t seed, long count) |
| 510 | { |
| 511 | uint8_t d[32], d2[32], w[32]; |
| 512 | char id[160], h[65]; |
| 513 | long ok = 0; |
| 514 | uint64_t tot = 0, nupd = 0, nempty = 0; |
| 515 | for (long i = 0; i < count; i++) { |
| 516 | sm_s = seed * 1000003ull + (uint64_t)i; |
| 517 | uint64_t r = sm(), cls = r % 16, len; |
| 518 | if (cls < 7) |
| 519 | len = sm() % 300; |
| 520 | else if (cls < 11) |
| 521 | len = sm() % 5000; |
| 522 | else if (cls < 14) |
| 523 | len = sm() % 70000; |
| 524 | else if (cls < 15) |
| 525 | { |
| 526 | uint64_t kb = 1 + sm() % 64; |
| 527 | len = 64 * kb + sm() % 3 - 1; /* block multiples and neighbours */ |
| 528 | } |
| 529 | else |
| 530 | len = sm() % 1100000; |
| 531 | uint64_t plan_seed = sm(); |
| 532 | uint8_t *m = malloc(len ? len : 1); |
| 533 | rndfill(m, len, seed ^ ((uint64_t)i << 20) ^ 0x5eedull); |
| 534 | /* random chunking with empty updates (NULL and non-NULL) */ |
| 535 | sha256_ctx c; |
| 536 | snprintf(id, sizeof id, "rand-%llu-%ld-len%llu", (unsigned long long)seed, i, (unsigned long long)len); |
| 537 | sm_s = plan_seed; |
| 538 | uint32_t s = I(&c); |
| 539 | uint64_t off = 0; |
| 540 | while (!s && off < len) { |
| 541 | uint64_t q = sm(), k; |
| 542 | switch (q % 6) { |
| 543 | case 0: |
| 544 | k = 0; |
| 545 | break; |
| 546 | case 1: |
| 547 | k = 1 + (q >> 8) % 64; |
| 548 | break; |
| 549 | case 2: |
| 550 | k = 1 + (q >> 8) % 300; |
| 551 | break; |
| 552 | case 3: |
| 553 | k = 64 - (off % 64); |
| 554 | break; |
| 555 | case 4: |
| 556 | k = 1 + (q >> 8) % 70000; |
| 557 | break; |
| 558 | default: |
| 559 | k = len - off; |
| 560 | break; |
| 561 | } |
| 562 | if (k > len - off) |
| 563 | k = len - off; |
| 564 | nupd++; |
| 565 | if (k == 0) { |
| 566 | nempty++; |
| 567 | s = U(&c, (q >> 3) & 1 ? NULL : m, 0); |
| 568 | } else |
| 569 | s = U(&c, m + off, k); |
| 570 | off += k; |
| 571 | } |
| 572 | if (!s) |
| 573 | s = (sq = U(&c, NULL, 0), sq |= F(&c, d), sq); |
| 574 | if (trapped(id, s, "stream") || trapped(id, H(m, len, d2), "hash")) { |
| 575 | free(m); |
| 576 | continue; |
| 577 | } |
| 578 | ref(m, len, w); |
| 579 | tot += len; |
| 580 | if (cmpd(id, d, w, "random chunking vs sha256sum") && cmpd(id, d2, w, "hash vs sha256sum")) |
| 581 | ok++; |
| 582 | hex(d, h); |
| 583 | printf("RAND %llu %ld %llu %s\n", (unsigned long long)seed, i, (unsigned long long)len, h); |
| 584 | free(m); |
| 585 | } |
| 586 | printf("INFO rand bytes=%llu updates=%llu empty-updates=%llu\n", (unsigned long long)tot, |
| 587 | (unsigned long long)nupd, (unsigned long long)nempty); |
| 588 | if (ok == count) { |
| 589 | snprintf(id, sizeof id, "%ld seeded messages (seed %llu): random chunkings incl. empty updates and hash == sha256sum", |
| 590 | count, (unsigned long long)seed); |
| 591 | pass("rand", id); |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | /* ---- part: 2^25-byte run boundaries of blocks() ------------------------------------------------- */ |
| 596 | static void mode_part(void) |
| 597 | { |
| 598 | const uint64_t R = 1ull << 25; |
| 599 | const uint64_t lens[] = {R - 64, R - 1, R, R + 1, R + 64, 2 * R, 2 * R + 77, 3 * R + 4095}; |
| 600 | uint64_t max = 3 * R + 4095; |
| 601 | uint8_t *m = malloc(max); |
| 602 | uint8_t d[32], d2[32], d3[32], w[32]; |
| 603 | char id[160]; |
| 604 | rndfill(m, max, 0x25); |
| 605 | int ok = 1; |
| 606 | for (size_t i = 0; i < sizeof lens / sizeof lens[0]; i++) { |
| 607 | uint64_t n = lens[i]; |
| 608 | static const size_t plan[] = {33, (1u << 25) + 5, 1u << 24}; |
| 609 | snprintf(id, sizeof id, "part-len%llu", (unsigned long long)n); |
| 610 | ref(m, n, w); |
| 611 | if (trapped(id, H(m, n, d), "hash") || trapped(id, stream(m, n, NULL, 0, d2), "one") || |
| 612 | trapped(id, stream(m, n, plan, 3, d3), "chunks")) { |
| 613 | ok = 0; |
| 614 | continue; |
| 615 | } |
| 616 | if (!(cmpd(id, d, w, "hash") && cmpd(id, d2, w, "one update") && cmpd(id, d3, w, "chunks 33,2^25+5,2^24"))) |
| 617 | ok = 0; |
| 618 | } |
| 619 | free(m); |
| 620 | if (ok) |
| 621 | pass("part", "lengths 2^25-64 .. 3*2^25+4095 (one update, hash, chunked) == sha256sum"); |
| 622 | } |
| 623 | |
| 624 | /* ---- big ---------------------------------------------------------------------------------------- */ |
| 625 | static void mode_big(void) |
| 626 | { |
| 627 | const uint64_t L = (1ull << 32) + 1000003ull; |
| 628 | uint8_t *m = malloc(L); |
| 629 | uint8_t d[32], w[32], wl[32]; |
| 630 | sha256_ctx c; |
| 631 | if (!m) { |
| 632 | printf("NOT_RUN big malloc(%llu) failed\n", (unsigned long long)L); |
| 633 | return; |
| 634 | } |
| 635 | rndfill(m, L, 0xb16); |
| 636 | ref(m, L, w); |
| 637 | ref(m, (1ull << 32) - 1, wl); |
| 638 | printf("INFO big L=%llu sha256sum=", (unsigned long long)L); |
| 639 | for (int i = 0; i < 32; i++) |
| 640 | printf("%02x", w[i]); |
| 641 | printf("\n"); |
| 642 | if (!trapped("big-1", (sq = I(&c), sq |= U(&c, m, L), sq |= F(&c, d), sq), "") && cmpd("big-1", d, w, "one update of 2^32+1000003 bytes")) |
| 643 | pass("big-1", "one update of 2^32+1000003 random bytes == sha256sum"); |
| 644 | if (!trapped("big-hash", H(m, L, d), "") && cmpd("big-hash", d, w, "hash")) |
| 645 | pass("big-hash", "sha256_hash of 2^32+1000003 bytes == sha256sum"); |
| 646 | if (!trapped("big-split32", (sq = I(&c), sq |= U(&c, m, (1ull << 32) - 1), sq |= U(&c, m + (1ull << 32) - 1, L - ((1ull << 32) - 1)), sq |= F(&c, d), sq), "") && |
| 647 | cmpd("big-split32", d, w, "updates of 2^32-1 and the rest")) |
| 648 | pass("big-split32", "updates of 2^32-1 and 1000004 bytes == sha256sum"); |
| 649 | if (!trapped("big-below", H(m, (1ull << 32) - 1, d), "") && cmpd("big-below", d, wl, "hash of 2^32-1 bytes")) |
| 650 | pass("big-below", "hash of 2^32-1 bytes == sha256sum"); |
| 651 | /* total > 2^32 through many updates of random sizes (count crosses 2^32 inside an update) */ |
| 652 | sm_s = 0x3a7; |
| 653 | uint64_t off = 0, n = 0; |
| 654 | uint32_t s = I(&c); |
| 655 | while (!s && off < L) { |
| 656 | uint64_t k = sm() % (1ull << 24) + 1; |
| 657 | if (n % 97 == 5) |
| 658 | k = sm() % 64; |
| 659 | if (k > L - off) |
| 660 | k = L - off; |
| 661 | s = U(&c, k ? m + off : NULL, k); |
| 662 | off += k; |
| 663 | n++; |
| 664 | } |
| 665 | if (!s) |
| 666 | s = F(&c, d); |
| 667 | char what[120]; |
| 668 | snprintf(what, sizeof what, "%llu updates totalling 2^32+1000003 bytes == sha256sum", (unsigned long long)n); |
| 669 | if (!trapped("big-many", s, "") && cmpd("big-many", d, w, what)) |
| 670 | pass("big-many", what); |
| 671 | free(m); |
| 672 | } |
| 673 | |
| 674 | static void mode_bigzero(void) |
| 675 | { |
| 676 | const uint64_t L = (1ull << 32) + 3; |
| 677 | uint8_t *z = mmap(NULL, L, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); |
| 678 | uint8_t d[32], w[32]; |
| 679 | sha256_ctx c; |
| 680 | if (z == MAP_FAILED) { |
| 681 | printf("NOT_RUN bigzero mmap failed\n"); |
| 682 | return; |
| 683 | } |
| 684 | ref(z, L, w); |
| 685 | if (!trapped("bigzero-1", (sq = I(&c), sq |= U(&c, z, L), sq |= F(&c, d), sq), "") && cmpd("bigzero-1", d, w, "")) |
| 686 | pass("bigzero-1", "one update of 2^32+3 zero bytes == sha256sum"); |
| 687 | if (!trapped("bigzero-hash", H(z, L, d), "") && cmpd("bigzero-hash", d, w, "")) |
| 688 | pass("bigzero-hash", "hash of 2^32+3 zero bytes == sha256sum"); |
| 689 | munmap(z, L); |
| 690 | } |
| 691 | |
| 692 | int main(int argc, char **argv) |
| 693 | { |
| 694 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 695 | if (argc < 2) |
| 696 | return 90; |
| 697 | const char *m = argv[1]; |
| 698 | if (!strcmp(m, "vec") && argc == 4) |
| 699 | mode_vec(argv[2], argv[3]); |
| 700 | else if (!strcmp(m, "rcpt")) |
| 701 | mode_rcpt(); |
| 702 | else if (!strcmp(m, "ident") && argc == 3) |
| 703 | mode_ident(argv[2]); |
| 704 | else if (!strcmp(m, "rand") && argc == 4) |
| 705 | mode_rand(strtoull(argv[2], NULL, 10), strtol(argv[3], NULL, 10)); |
| 706 | else if (!strcmp(m, "part")) |
| 707 | mode_part(); |
| 708 | else if (!strcmp(m, "big")) |
| 709 | mode_big(); |
| 710 | else if (!strcmp(m, "bigzero")) |
| 711 | mode_bigzero(); |
| 712 | else |
| 713 | return 90; |
| 714 | printf("TOTAL %s pass=%ld fail=%ld trap=%ld\n", m, npass, nfail, ntrap); |
| 715 | return (nfail || ntrap) ? 1 : 0; |
| 716 | } |