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 / tests / test_sha256_freestanding_parity.c

59 lines · 2625 bytes Raw Blame History
1/* SHA-256 through the generated C API (src/sha256.h -> src/omega_api_decl.h), one source built twice by
2 * mk/omega-gpu-engine.mk: against the hosted binding src/omega_om_units.c and against the freestanding
3 * binding src/omega_om_units_fs.c compiled with GPU_FS_CFLAGS. Both print the same lines; the make
4 * target compares them byte for byte and checks the FIPS 180-2 digests.
5 * fips <i> <digest> empty, "abc", the 448-bit message, one million 'a' (in uneven update calls)
6 * len <n> <digest> n = 0..1100 (every padding boundary 55/56/63/64/65 and its block multiples), 4096,
7 * 65536 and 1048583 bytes from a fixed-seed xorshift64 stream, one-shot; the same
8 * bytes through init/update/final split at a seeded point must give the same digest
9 * (else "split-mismatch <n>" and exit 1). */
10#include "sha256.h"
11#include <stdio.h>
12#include <string.h>
13
14static void put(const char *tag, unsigned i, const uint8_t d[SHA256_DIGEST_SIZE])
15{
16 printf("%s %u ", tag, i);
17 for (int k = 0; k < SHA256_DIGEST_SIZE; k++) printf("%02x", d[k]);
18 printf("\n");
19}
20
21static uint64_t rng = 0x9e3779b97f4a7c15ull;
22static uint64_t next(void)
23{
24 rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17;
25 return rng;
26}
27
28int main(void)
29{
30 static const char *const fips[] = { "", "abc", "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" };
31 enum { MILLION = 1000000, LEN_MAX = 1048583 };
32 static const unsigned big[] = { 4096, 65536, LEN_MAX };
33 static uint8_t buf[LEN_MAX];
34 uint8_t d[SHA256_DIGEST_SIZE], e[SHA256_DIGEST_SIZE];
35 sha256_ctx c;
36 unsigned i;
37 for (i = 0; i < 3; i++) { sha256_hash((const uint8_t *)fips[i], strlen(fips[i]), d); put("fips", i, d); }
38 memset(buf, 'a', MILLION);
39 sha256_init(&c);
40 for (size_t off = 0, step = 1; off < MILLION; off += step, step = step * 3 + 7) {
41 size_t n = MILLION - off < step ? MILLION - off : step;
42 sha256_update(&c, buf + off, n);
43 }
44 sha256_final(&c, d);
45 put("fips", 3, d);
46 for (unsigned t = 0; t < 1101 + sizeof big / sizeof big[0]; t++) {
47 const unsigned n = t <= 1100 ? t : big[t - 1101];
48 for (unsigned k = 0; k < n; k++) buf[k] = (uint8_t)next();
49 sha256_hash(buf, n, d);
50 const unsigned cut = n ? (unsigned)(next() % (n + 1)) : 0;
51 sha256_init(&c);
52 sha256_update(&c, buf, cut);
53 sha256_update(&c, buf + cut, n - cut);
54 sha256_final(&c, e);
55 if (memcmp(d, e, sizeof d)) { printf("split-mismatch %u\n", n); return 1; }
56 put("len", n, d);
57 }
58 return 0;
59}