interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / qualification / om2 / oracle / s5s7 / sha / sha_driver.c

165 lines · 6798 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * OM-2 qualification, lane s5s7 (#748, ledger L836): SHA consumer-semantics ORACLE driver for
3 * contract section 7 (spec/omegese-om2.md lines 212-223) and erratum E3. Outside the delivered
4 * closure. Expected digests: expected.h, generated by ../oracle.py with python3 hashlib.
5 *
6 * Built against the bridge-GENERATED declaration header (contract 216-217, E3: it maps sha256_* to
7 * omega2_sha256_*); its file name is not fixed by the contract, so it is passed as GENHDR:
8 * gcc -std=gnu11 -O2 -Wall -Wextra -Werror -DGENHDR='"<generated header>"' -I<dir> \
9 * sha_driver.c <generated binding/library> -o sha_driver
10 * Self-check of this driver (NOT OM-2 evidence): the same file built against the CURRENT
11 * src/sha256.c/.h of main acb3f15 (-DGENHDR='"sha256.h"' -I/tmp/zl/main/src /tmp/zl/main/src/sha256.c).
12 * Usage: sha_driver [big] ("big" adds the >= 2^32-byte update cases; needs 4 GiB of address
13 * space, read from the shared zero page, no resident memory).
14 * Output: one PASS/FAIL line per case id (ids match candidate.cases sha-* rows); exit 0 iff no FAIL. */
15#include <stdint.h>
16#include <stdio.h>
17#include <string.h>
18#include <sys/mman.h>
19
20#ifndef GENHDR
21#define GENHDR "sha256.h"
22#endif
23#include GENHDR
24#include "expected.h"
25
26static int fails;
27static void check(const char *id, int ok, const char *what)
28{
29 printf("%s %s %s\n", ok ? "PASS" : "FAIL", id, what);
30 if (!ok)
31 fails++;
32}
33
34static int eqhex(const uint8_t *d, const char *hex)
35{
36 static const char x[] = "0123456789abcdef";
37 int i;
38 for (i = 0; i < 32; i++)
39 if (hex[2 * i] != x[d[i] >> 4] || hex[2 * i + 1] != x[d[i] & 15])
40 return 0;
41 return hex[64] == 0;
42}
43
44static uint8_t msg[1000];
45
46int main(int argc, char **argv)
47{
48 sha256_ctx c, c2;
49 uint8_t d[32], d2[32];
50 static uint8_t ov[96];
51 size_t i, k;
52 int ok;
53
54 for (i = 0; i < sizeof msg; i++)
55 msg[i] = (uint8_t)(i % 251);
56 sha256_init(&c);
57 sha256_final(&c, d);
58 check("sha-empty", eqhex(d, EXP_EMPTY), "init, final = sha256(\"\")");
59 sha256_init(&c);
60 sha256_update(&c, (const uint8_t *)"abc", 3);
61 sha256_final(&c, d);
62 check("sha-abc", eqhex(d, EXP_ABC), "abc");
63 /* any split point (contract 214-215) */
64 ok = 1;
65 for (k = 0; k <= sizeof msg; k++) {
66 sha256_init(&c);
67 sha256_update(&c, msg, k);
68 sha256_update(&c, msg + k, sizeof msg - k);
69 sha256_final(&c, d);
70 ok &= eqhex(d, EXP_SEQ1000);
71 }
72 check("sha-split2", ok, "every two-way split k = 0..1000 of seq:1000");
73 ok = 1;
74 for (k = 0; k <= sizeof msg; k += 7) {
75 size_t j = (k * 13) % (sizeof msg - k + 1);
76 sha256_init(&c);
77 sha256_update(&c, msg, 0);
78 sha256_update(&c, msg, k);
79 sha256_update(&c, msg + k, 0);
80 sha256_update(&c, msg + k, j);
81 sha256_update(&c, msg + k + j, sizeof msg - k - j);
82 sha256_update(&c, msg, 0);
83 sha256_final(&c, d);
84 ok &= eqhex(d, EXP_SEQ1000);
85 }
86 check("sha-split3-zero", ok, "three-way splits with length-0 updates between them");
87 sha256_init(&c);
88 for (i = 0; i < sizeof msg; i++)
89 sha256_update(&c, msg + i, 1);
90 sha256_final(&c, d);
91 check("sha-bytewise", eqhex(d, EXP_SEQ1000), "1000 one-byte updates");
92 sha256_init(&c);
93 sha256_update(&c, NULL, 0);
94 sha256_final(&c, d);
95 check("sha-update-null0", eqhex(d, EXP_EMPTY), "update(NULL, 0) is an empty update (ABI: len 0 any pointer)");
96 /* digest overlapping data (contract 214-215, E3) */
97 memcpy(ov, msg, 64);
98 sha256_hash(ov, 64, ov);
99 check("sha-hash-overlap0", eqhex(ov, EXP_SEQ64), "sha256_hash(buf, 64, buf)");
100 memcpy(ov, msg, 64);
101 sha256_hash(ov, 64, ov + 16);
102 check("sha-hash-overlap16", eqhex(ov + 16, EXP_SEQ64), "sha256_hash(buf, 64, buf + 16)");
103 memcpy(ov + 32, msg, 64);
104 sha256_hash(ov + 32, 64, ov + 8);
105 check("sha-hash-overlap-before", eqhex(ov + 8, EXP_SEQ64), "digest [8,40) overlapping data [32,96)");
106 /* post-final reuse (contract 219-222) */
107 sha256_init(&c);
108 sha256_update(&c, (const uint8_t *)"abc", 3);
109 sha256_final(&c, d);
110 sha256_init(&c);
111 sha256_update(&c, msg, sizeof msg);
112 sha256_final(&c, d);
113 check("sha-post-final", eqhex(d, EXP_SEQ1000), "init, update, final after a final == fresh context");
114 sha256_update(&c, msg, 10);
115 sha256_final(&c, d2);
116 check("sha-no-init-safe", 1, "update, final without init returned (digest unspecified; memory safety only)");
117 /* hash(n) == init, update(n), final (contract 223) */
118 ok = 1;
119 for (i = 0; i < sizeof exp_hash_n / sizeof exp_hash_n[0]; i++) {
120 size_t n = exp_hash_n[i];
121 sha256_hash(msg, n, d);
122 sha256_init(&c);
123 sha256_update(&c, msg, n);
124 sha256_final(&c, d2);
125 ok &= eqhex(d, exp_hash_d[i]) && memcmp(d, d2, 32) == 0;
126 }
127 check("sha-hash-eq-stream", ok, "hash(n) == init, update(n), final == python, n in {0,1,55,56,63,64,65,119,120,1000}");
128 /* context byte copy is equivalent (erratum E3) */
129 ok = 1;
130 for (k = 0; k <= 200; k += 25) {
131 sha256_init(&c);
132 sha256_update(&c, msg, k);
133 memcpy(&c2, &c, sizeof c);
134 sha256_update(&c, msg + k, sizeof msg - k);
135 sha256_update(&c2, msg + k, sizeof msg - k);
136 sha256_final(&c, d);
137 sha256_final(&c2, d2);
138 ok &= eqhex(d, EXP_SEQ1000) && eqhex(d2, EXP_SEQ1000);
139 }
140 check("sha-ctx-copy", ok, "byte copy after k updates, both continued: equal digests (E3)");
141 /* updates of at least 2^32 bytes (contract 214) */
142 if (argc > 1 && strcmp(argv[1], "big") == 0) {
143 uint8_t *z = mmap(NULL, (size_t)EXP_BIG_LEN, PROT_READ, MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
144 if (z == MAP_FAILED) {
145 printf("NOT_RUN sha-big-1 mmap failed\nNOT_RUN sha-big-split mmap failed\n");
146 } else {
147 sha256_init(&c);
148 sha256_update(&c, z, (size_t)EXP_BIG_LEN);
149 sha256_final(&c, d);
150 check("sha-big-1", eqhex(d, EXP_BIG_ZERO), "one update of 2^32+3 zero bytes");
151 sha256_init(&c);
152 sha256_update(&c, z, (size_t)0xffffffffu);
153 sha256_update(&c, z, 4);
154 sha256_final(&c, d);
155 check("sha-big-split", eqhex(d, EXP_BIG_ZERO), "updates of 2^32-1 and 4 zero bytes");
156 sha256_hash(z, (size_t)EXP_BIG_LEN, d);
157 check("sha-big-hash", eqhex(d, EXP_BIG_ZERO), "hash of 2^32+3 zero bytes");
158 munmap(z, (size_t)EXP_BIG_LEN);
159 }
160 } else {
161 printf("NOT_RUN sha-big-1 sha-big-split sha-big-hash (run with argument big)\n");
162 }
163 printf("%s SHA driver: %d failure(s)\n", fails ? "FAIL" : "PASS", fails);
164 return fails ? 1 : 0;
165}