omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / sha512 / README.md
| 1 | # om/sha512: SHA-512 in Omega |
| 2 | |
| 3 | SHA-512 as defined in FIPS 180-4, section 6.4, written in Omega (`.om`). The library takes one |
| 4 | message of up to 65536 bytes and writes its 64-byte digest. It exists for Ed25519, which needs |
| 5 | SHA-512(R || A || M) (RFC 8032, section 5.1.7). |
| 6 | |
| 7 | `sha512.om` is a library unit: it holds only functions, with no `fn main` and no globals. Every |
| 8 | function name starts with `s512`. To use it, concatenate it before the program that calls it. |
| 9 | |
| 10 | ## API |
| 11 | |
| 12 | ``` |
| 13 | fn s512hash m view uchar 65536 n ulong o edit uchar 64 to void |
| 14 | fn s512pre p view uchar 64 m view uchar 65536 n ulong o edit uchar 64 to void |
| 15 | ``` |
| 16 | |
| 17 | - `s512hash` writes SHA-512(m[0..n)) to o[0..64). |
| 18 | - `s512pre` writes SHA-512(p[0..64) || m[0..n)) to o[0..64). For Ed25519, p holds R || A, so the |
| 19 | caller does not have to copy M. |
| 20 | - If n > 65536, both functions trap 3 (in program mode the process exits 103). Bytes of m past n |
| 21 | are never read. |
| 22 | - The digest is the usual big-endian byte string, H0 first (FIPS 180-4, section 6.4.2). |
| 23 | - Omega refuses to pass the same array as two arguments of one call (error 19), so `m` and `o` are |
| 24 | always separate storage. |
| 25 | - Globals cannot be passed to functions (error 19). A program-mode caller copies its message into a |
| 26 | local array first, such as `buf = new uchar 65536 0` in the function's outermost block. The pinned |
| 27 | omc accepts that 65536-element local even though OMEGESE.md section 3 lists `new` lengths as |
| 28 | 1..64. omc's own limit is 4 MiB of local arrays (OMEGESE.md section 9.4). |
| 29 | |
| 30 | The other functions are internal: compression, padding, and big-endian loads. The constants |
| 31 | come straight from FIPS 180-4: K (section 4.2.3), H(0) (section 5.3.5), Ch, Maj, Σ0, Σ1, σ0 |
| 32 | and σ1 (section 4.1.3, equations 4.8-4.13), and padding (section 5.1.2). Words are `ulong`, and |
| 33 | unsigned arithmetic wraps mod 2^64. Omega has no rotate word, so ROTR^r(x) is written |
| 34 | `(x >> r) | (x << (64 - r))` (section 3.2). |
| 35 | |
| 36 | In the source, K and H(0) are decimal literals, because Omega has no hex literals. They were |
| 37 | checked against the hex tables in FIPS 180-4 sections 4.2.3 and 5.3.5: all 80 K words matched the |
| 38 | extracted table, and so did the cube-root and square-root derivation. Changing one bit of K[0] |
| 39 | makes every NIST example fail (see "mutant-constant" below). |
| 40 | |
| 41 | ## Tests |
| 42 | |
| 43 | ``` |
| 44 | sh om/sha512/test/run.sh "$(tools/omc.sh)" |
| 45 | ``` |
| 46 | |
| 47 | `run.sh` concatenates `sha512.om` with `test/driver.om`, builds `build/test/sha512/driver`, and |
| 48 | feeds it vector records on stdin. The driver's record format is described in its header. The |
| 49 | script exits 0 only if every vector passes and every negative check is rejected. |
| 50 | |
| 51 | | Check | Records | Source | |
| 52 | |---|---|---| |
| 53 | | nist-examples | 10 | `vectors/nist-examples.txt`: 'abc' and the 896-bit two-block message from NIST `SHA512.pdf`, and SHA-512 items #1-#8 from NIST `SHA2_Additional.pdf` (0 bytes; 111, 112, 113 and 122 zero bytes; 1000 zero bytes; 1000 'A'; 1005 'U'). Items #9-#12 are 10^6 bytes or more, beyond the capacity. | |
| 54 | | cavp-short | 129 | `vectors/SHA512ShortMsg.rsp`, unmodified (0 to 1024 bits) | |
| 55 | | cavp-long | 128 | `vectors/SHA512LongMsg.rsp`, unmodified (1816 to 102400 bits, all within the capacity) | |
| 56 | | cavp-monte | 100 | `vectors/SHA512Monte.rsp`, unmodified: the SHAVS Monte Carlo chain, 100 checkpoints of 1000 hashes each | |
| 57 | | oracle-static | 52 | `vectors/oracle.txt`, generated by `gen_vectors.py static` with python3 `hashlib.sha512` as the oracle. It covers the 448-bit "abcdbcdec…nopq" message, lengths 0, 1, 55, 56, 63, 64, 110-113, 119, 120, 126-129, 239-241, 255-257 and 1023-1025, all-0x00 and all-0xff messages of 111/112/127/128 bytes, n = 65536, and 17 `s512pre` records | |
| 58 | | hashlib-diff | 1125 | Generated at test time: every length 0..1100, plus 65536, 65535, 65519, 65408, 65407, 65520, 32768, 32767 and 16 random lengths up to 65536. Random bytes from `random.Random(seed)`, oracle python3 `hashlib.sha512` | |
| 59 | | hashlib-pre | 313 | Generated at test time: `s512pre` with every length 0..300, plus 65536, 65535, 65472, 65471 and 8 random lengths | |
| 60 | |
| 61 | The seed defaults to 1 and can be changed with `S512_SEED`. Without python3 the two hashlib checks |
| 62 | are skipped and a `NOTE` line is printed. The committed vectors still run. |
| 63 | |
| 64 | Negative checks (each must be detected): |
| 65 | |
| 66 | - mutant-expected: one hex digit of the expected 'abc' digest is changed. Must produce exactly 1 failure. |
| 67 | - mutant-constant: K[0] in a copy of the library is changed by one. Must produce failures on all 10 NIST examples. |
| 68 | - malformed-record: a record with an odd number of hex digits. Must be refused as BAD and not hashed. |
| 69 | - over-capacity: `test/trap.om` calls `s512hash` with n = 65537. Must exit 103 (trap 3). |
| 70 | |
| 71 | The first three count as detected only when the driver finished normally and reported the |
| 72 | difference. That means exit status exactly 1, which is the driver's own "a check failed" return, |
| 73 | and a complete last line `sha512 driver: C checks, F failed` with the expected C (all records) and |
| 74 | F > 0. A trap (exit 100+N), a signal (128+N), a launch failure (126/127), exit 0 or a truncated |
| 75 | summary all fail the run. A passing check likewise needs exit 0 and a complete `... 0 failed` |
| 76 | line. Exit statuses are taken directly from the driver, never through a pipeline. This was |
| 77 | checked on 2026-10-25 by temporarily replacing the K[0] mutant with one that traps 3 on every |
| 78 | call (`put k 0 get k add 80 get k 1`). `run.sh` reported |
| 79 | `FAIL mutant-constant: expected exit 1 and "sha512 driver: 10 checks, 10 failed", got exit 103`, |
| 80 | ended with `sha512: FAIL` and exited 1. The real mutant was then restored. |
| 81 | |
| 82 | ## Evidence (2026-10-25) |
| 83 | |
| 84 | - omc: seed-born from omega-mv `acb3f15c`, sha256 |
| 85 | `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61` |
| 86 | - `sha512.om` sha256 `069a54b75484221bc7dbacfbea744ca9002c295f4f34f2c3905b0776450e3ce2` (332 lines) |
| 87 | - driver binary sha256 `a45c2e051c1cd106490c744bd42ec7884d3c01b131f0c0dbbbbe5349d56a2670` |
| 88 | - `sh om/sha512/test/run.sh "$(tools/omc.sh)"`: every check above PASS, ending with |
| 89 | `sha512: PASS (1857 vector checks)`, exit 0. All four negative checks were detected |
| 90 | (mutant-constant: 10 of 10 failed). The run takes about 0.6 s on an arm64 Linux host. |
| 91 | - `S512_SEED` = 2, 3, 4 and 5 each passed 1125 + 313 differential records. |
| 92 | - With python3 removed from PATH: 419 vector checks PASS, the NOTE was printed, and all negative checks were rejected. |
| 93 | |
| 94 | ## Vector sources (fetched 2026-10-25) |
| 95 | |
| 96 | - NIST CAVP SHA byte-oriented test vectors, |
| 97 | <https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Algorithm-Validation-Program/documents/shs/shabytetestvectors.zip> |
| 98 | (zip sha256 `929ef80b7b3418aca026643f6f248815913b60e01741a44bba9e118067f4c9b8`; project page |
| 99 | <https://csrc.nist.gov/projects/cryptographic-algorithm-validation-program/secure-hashing>). |
| 100 | Files copied without changes (CRLF line ends, kept byte-exact by `vectors/.gitattributes`): |
| 101 | - `SHA512ShortMsg.rsp` sha256 `e53a36c03609e5a3e3cc4b6e117a499db7864c23ec825c6cec99503a45f40764` |
| 102 | - `SHA512LongMsg.rsp` sha256 `b1f3f05d5c209777954d49521d7ea1349447c36a0c52849e044bc397a27dd410` |
| 103 | - `SHA512Monte.rsp` sha256 `8ca78659286c2f01667a98fc7accd32fc171ae7b24ac00f1a8ce6b77770247fa` |
| 104 | - NIST example values, |
| 105 | <https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA512.pdf> |
| 106 | (sha256 `84ce867c99bad5a7b05abc74bb1dfc9de8486f49a160653cc8b15272cc532115`) and |
| 107 | <https://csrc.nist.gov/CSRC/media/Projects/Cryptographic-Standards-and-Guidelines/documents/examples/SHA2_Additional.pdf> |
| 108 | (sha256 `95f25cb0c8a0636c7cd5773469883d3cf802d8e8265dcbb13cca1dad3be3c3b3`). |
| 109 | The digests were extracted from the PDF text into `vectors/nist-examples.txt`. |
| 110 | - FIPS 180-4, <https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf> |
| 111 | (sha256 `0455b406d89648d20cbde375561e19c245b9815e894164c2670772e3d54deb82`). |
| 112 | - NIST publishes no SHA-512 digest for the 448-bit message. Its expected value in |
| 113 | `vectors/oracle.txt` comes from hashlib only. |
| 114 | |
| 115 | ## Third-party material and notices |
| 116 | |
| 117 | | copied material | owner and terms | notice file | |
| 118 | |---|---|---| |
| 119 | | `vectors/SHA512ShortMsg.rsp`, `SHA512LongMsg.rsp`, `SHA512Monte.rsp`, byte-for-byte copies from the CAVP zip above | NIST, a US Government agency. Under 17 U.S.C. §105, works of NIST employees are not subject to copyright protection in the United States and may be subject to foreign copyright. NIST grants a worldwide royalty-free right to copy, modify and distribute, asks that NIST be acknowledged, and provides the data "AS IS" (<https://www.nist.gov/open/license>, "Fair Use of Other NIST Data/Works") | `vectors/NIST.NOTICE` | |
| 120 | | digests and message descriptions in `vectors/nist-examples.txt`, transcribed from NIST `SHA512.pdf` and `SHA2_Additional.pdf` (modified on 2026-10-25: values copied into the record format, the PDF layout and intermediate values dropped) | NIST, same terms | `vectors/NIST.NOTICE`, which also records the change, as NIST's terms ask | |
| 121 | |
| 122 | `vectors/NIST.NOTICE` quotes NIST's recommended statement for non-SRD data/works verbatim from |
| 123 | <https://www.nist.gov/open/license> (fetched 2026-10-25), names the source URLs, and states what was |
| 124 | changed. The header of `nist-examples.txt` points to it. The `.rsp` files carry no notice of their |
| 125 | own and are kept unmodified. |
| 126 | |
| 127 | FIPS 180-4 (also a NIST work) is cited by section only. Its K and H(0) values appear in |
| 128 | `sha512.om` as decimal constants, and the standard defines them as cube- and square-root |
| 129 | fractions. The 448-bit message in `oracle.txt` is the standard's sample text. No other |
| 130 | third-party material is included: `oracle.txt` and the differential corpus are this unit's own |
| 131 | output, with python3 `hashlib` used only as the reference. |
| 132 | |
| 133 | ## Limits |
| 134 | |
| 135 | - Messages are at most 65536 bytes (CAP). For `s512pre` that is 64 + 65536 bytes. There is no |
| 136 | streaming (init/update/final) interface. |
| 137 | - The code is NOT constant-time audited. Omega gives no timing guarantee, and nothing here was |
| 138 | measured for timing. SHA-512 has no secret-dependent branches on its input by design, but this |
| 139 | implementation's machine code was not inspected for that property. |
| 140 | - Each call recomputes the K table (80 stores) and uses about 1.7 KiB of stack for local arrays. |
| 141 | - Only tested on the host CPU (arm64 Linux, program mode). Function-mode / IR-mode use was not |
| 142 | exercised. |