omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / blake2s / README.md
| 1 | # om/blake2s: BLAKE2s in Omega |
| 2 | |
| 3 | BLAKE2s ([RFC 7693](https://www.rfc-editor.org/rfc/rfc7693)), written in Omega (`.om`) and compiled |
| 4 | by the pinned seed-born `omc`. It supports unkeyed and keyed hashing, keys of 0..32 bytes, and digest |
| 5 | lengths of 1..32 bytes. BLAKE2s-256 is the case with no key and a 32-byte digest. Python appears only |
| 6 | in the tests, as an oracle. No other language is on the implementation path. |
| 7 | |
| 8 | ## Files |
| 9 | |
| 10 | | Path | Role | |
| 11 | |---|---| |
| 12 | | `blake2s.om` | Library unit. It has functions only, no `main` and no globals, and every name starts with `b2s`. | |
| 13 | | `test/driver.om` | Program-mode test driver. It is concatenated after the library. | |
| 14 | | `test/run.sh` | Test suite. Usage: `sh om/blake2s/test/run.sh OMC`. | |
| 15 | | `test/oracle.py` | Vector generator built on python3 `hashlib.blake2s`. It is used only for tests. | |
| 16 | | `test/vectors/rfc7693.txt` | The RFC 7693 Appendix B digest of "abc" and the Appendix E grand hash. | |
| 17 | | `test/vectors/blake2s-kat.txt` | The official BLAKE2 KAT file, copied verbatim. | |
| 18 | | `test/vectors/boundary.txt` | 60 block-edge and length cases, with digests from `oracle.py boundary`. | |
| 19 | |
| 20 | ## API |
| 21 | |
| 22 | ``` |
| 23 | fn b2shash m view uchar 65536 n ulong k view uchar 32 kn ulong outn ulong o edit uchar 32 to void |
| 24 | ``` |
| 25 | |
| 26 | | Parameter | Meaning | |
| 27 | |---|---| |
| 28 | | `m`, `n` | The message. Only `m[0..n)` is read. `n` is 0..65536. | |
| 29 | | `k`, `kn` | The key. Only `k[0..kn)` is read. `kn` is 0..32, and 0 means unkeyed. | |
| 30 | | `outn` | Digest length in bytes, 1..32. | |
| 31 | | `o` | Receives the digest in `o[0..outn)`. `o[outn..32)` is set to 0. | |
| 32 | |
| 33 | If `n`, `kn` or `outn` is out of range, the call traps 3 (program exit 103) before it writes `o`. |
| 34 | This was checked with an ad hoc program for `n=65537`, `kn=33`, `outn=0` and `outn=33`. |
| 35 | |
| 36 | The message capacity is 65536 bytes, which is the contract value. omc accepts it both as a |
| 37 | `view uchar 65536` parameter and as a `new uchar 65536 0` local array in the caller. |
| 38 | |
| 39 | The other functions are internal helpers. All of them take the 64-word working array `w`: |
| 40 | |
| 41 | | Function | Purpose | |
| 42 | |---|---| |
| 43 | | `b2sinit` | Sets up the IV and SIGMA. | |
| 44 | | `b2sstart` | Sets h and the parameter block. | |
| 45 | | `b2smw` | Selects the message word. | |
| 46 | | `b2sg` | The G function. | |
| 47 | | `b2sround` | One round. | |
| 48 | | `b2scomp` | The compression function F. | |
| 49 | | `b2sbyte` | Returns one byte of the key-block‖message stream. | |
| 50 | | `b2sload` | Loads a block. | |
| 51 | | `b2sout` | Writes the digest bytes. | |
| 52 | |
| 53 | ## Standards followed |
| 54 | |
| 55 | These are the sections of RFC 7693: |
| 56 | |
| 57 | | Section | What it defines | How the code uses it | |
| 58 | |---|---|---| |
| 59 | | 2.1 | BLAKE2s parameters | w=32, r=10, R1..R4 = 16, 12, 8, 7. | |
| 60 | | 2.6 | IV | | |
| 61 | | 2.7 | SIGMA | Each row is packed as sixteen 4-bit indexes. Every row is quoted in a `note` beside its packed value. | |
| 62 | | 3.1 | G | | |
| 63 | | 3.2 | F | | |
| 64 | | 3.3 | Parameter block and padding | `h[0] ^= 0x01010000 ^ (kk << 8) ^ nn`. The key, zero-padded to 64 bytes, is the first block. The last block is zero-padded and carries `t` = total stream bytes and `f0 = 0xFFFFFFFF`. An empty unkeyed message is one zero block with `t = 0`. | |
| 65 | |
| 66 | 32-bit words are kept in `ulong` cells and masked to 32 bits after every add and rotate. |
| 67 | |
| 68 | ## Test driver protocol |
| 69 | |
| 70 | - `driver` reads one case per line from stdin, in the form `OUTN KEY MSG`. `OUTN` is decimal 1..32. |
| 71 | `KEY` and `MSG` are hex (either case, even digit count), or `-` for empty. `KEY` holds at most 32 |
| 72 | bytes and `MSG` at most 65536 bytes. For each case the driver writes `2*OUTN` lowercase hex digits |
| 73 | and a newline. It exits 0 at end of input and 2 on a malformed line. |
| 74 | - `driver selftest` runs the RFC 7693 Appendix E BLAKE2s self-test. That test covers digest lengths |
| 75 | {16, 20, 28, 32} × input lengths {0, 3, 64, 65, 255, 1024}, unkeyed and keyed, with inputs and keys |
| 76 | from `selftest_seq`. The driver prints the grand hash and exits 0 only if it equals the RFC's |
| 77 | `blake2s_res`. The grand hash is the hash of all 48 digests concatenated. That is the same byte |
| 78 | stream Appendix E feeds through `blake2s_update`. |
| 79 | |
| 80 | `run.sh` does the following: |
| 81 | |
| 82 | 1. Builds `build/test/blake2s/driver`. |
| 83 | 2. Converts each vector source to case lines with awk and compares the driver output with `cmp`. |
| 84 | 3. Runs the self-test. |
| 85 | 4. If python3 is present, generates the differential corpus at test time. If it is absent, the suite |
| 86 | prints `NOTE ... NOT_RUN` and still checks the static vectors. |
| 87 | 5. Runs three mutants: |
| 88 | - one flipped expected digest digit; |
| 89 | - IV[0] changed by 1; |
| 90 | - the final-block flag removed (`b2scomp w 1` → `b2scomp w 0`). |
| 91 | |
| 92 | Each source mutant is rebuilt and run on the RFC 7693 Appendix B case and the Appendix E |
| 93 | self-test. |
| 94 | |
| 95 | A mutant counts as detected only when two things hold. First, every run completed normally: |
| 96 | exit 0, or exit 1 for `selftest` (its documented mismatch status), and complete output (one line |
| 97 | per case, each of the expected length). A trap exit (100+N), a signal or a launch failure is |
| 98 | FAIL, never detection. Second, at least one result differs from the expected one. Output goes to |
| 99 | files rather than through pipes, so no exit status is lost. |
| 100 | |
| 101 | `B2S_SEED` sets the differential seed. The default is 7693. |
| 102 | |
| 103 | ## Vector sources |
| 104 | |
| 105 | | Source | Where it comes from | |
| 106 | |---|---| |
| 107 | | RFC 7693 Appendix B (`"abc"`) and Appendix E (`blake2s_res`) | https://www.rfc-editor.org/rfc/rfc7693.txt, fetched 2026-10-25. The text file's sha256 is `c943754888364fe29bbd0bb3c71c6658ef392371497eaa4a9d9dde015b0721e5`. See "Third-party material and notices" for the notice. | |
| 108 | | BLAKE2 KAT, 256 keyed cases | https://github.com/BLAKE2/BLAKE2/blob/master/testvectors/blake2s-kat.txt at commit `ed1974ea83433eba7b2d95c5dcd9ac33cb847913`, fetched 2026-10-25. The file's sha256 is `a915b96fc07f6ece3b3355a5cbb813dbf9cd0ad37c22a9b89bf11f9c47922418`. Upstream licenses it under CC0 1.0, the OpenSSL License or Apache-2.0, at the user's choice. | |
| 109 | | Boundary cases (`boundary.txt`) | Generated once by `python3 test/oracle.py boundary` (CPython 3.12.3 `hashlib.blake2s`). The key is bytes `00 01 ..`; message byte i is `i mod 251`. The message lengths are 0, 1, 55, 56, 63, 64, 65, 127, 128, 129, 191, 192, 193, 1023, 1024, 1025, 65471, 65472, 65473, 65535 and 65536. Each length is run unkeyed and with a 32-byte key. The file also has keys of 1, 16 and 31 bytes, and digest lengths 1, 2, 16, 20, 28 and 31. | |
| 110 | | Differential corpus (`oracle.py diff SEED`) | Every length 0..1100 twice: unkeyed with a 32-byte digest, and keyed with `kn = 1 + L%32` and `outn = 32 - L%32`. Then 64 seeded-random cases: lengths 65535, 65536 and 62 random lengths in 0..65536, key length 0..32, outn 1..32. That makes 2266 cases. | |
| 111 | |
| 112 | ## Third-party material and notices |
| 113 | |
| 114 | | Copied or translated material | Owner and terms | Notice file | |
| 115 | |---|---|---| |
| 116 | | The `"abc"` digest (Appendix B) and the grand hash `blake2s_res` (Appendix E) in `test/vectors/rfc7693.txt`. The Appendix E self-test procedure (`selftest_seq`, the `md_len`/`in_len` tables), translated into Omega in `test/driver.om`. | Copyright (c) 2015 IETF Trust and the persons identified as the document authors. RFC 7693 is an Independent Submission stream document under BCP 78 and the IETF Trust Legal Provisions. | `test/vectors/rfc7693.NOTICE`: the RFC's Copyright Notice section, verbatim. | |
| 117 | | `test/vectors/blake2s-kat.txt`, copied verbatim. | The BLAKE2 repository offers it under CC0 1.0, the OpenSSL License or Apache-2.0, at the user's choice; this repository takes it under CC0 1.0. | `test/vectors/blake2s-kat.LICENSE`: upstream `COPYING` at `ed1974ea`, verbatim (sha256 `a2010f343487d3f7618affe54f789f5487602331c0a8d03f49e9a7c547cf0499`). | |
| 118 | |
| 119 | Section 4 of the IETF Trust Legal Provisions (TLP 5.0) is the BSD license for code components. |
| 120 | Section 8(f) says section 4 "shall not apply to documents in the Independent Submission Stream". |
| 121 | Source: https://trustee.ietf.org/wp-content/uploads/Corrected-TLP-5.0-legal-provsions.pdf, |
| 122 | fetched 2026-10-25. RFC 7693 material is therefore not under a BSD license: it is used here with |
| 123 | attribution only, and no license for it is claimed. Whether that attribution is sufficient was not |
| 124 | checked. |
| 125 | |
| 126 | Not copied as source text: `blake2s.om` was written for this unit from the RFC's prose and tables |
| 127 | (sections 2.1, 2.6, 2.7, 3.1-3.3). Its IV and SIGMA values are the RFC's constants, and it contains |
| 128 | no text from the RFC's Appendix D C code. `boundary.txt` and the differential corpus come from this |
| 129 | unit's own generator, `oracle.py`. |
| 130 | |
| 131 | ## Evidence (2026-10-25, linux arm64 host) |
| 132 | |
| 133 | ``` |
| 134 | $ sh om/blake2s/test/run.sh "$(tools/omc.sh)" |
| 135 | blake2s: omc sha256 734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61 |
| 136 | blake2s: driver sha256 c3c34c06f8e8aced3fc1666f3d4bb5dc7f0a8339f8cfeb6952246d8525ef11d3 |
| 137 | blake2s: rfc7693-appB 1/1 PASS |
| 138 | blake2s: rfc7693-appE 1/1 PASS (6a411f08ce25adcdfb02aba641451cec53c598b24f4fc787fbdc88797f4c1dfe) |
| 139 | blake2s: kat 256/256 PASS |
| 140 | blake2s: boundary 60/60 PASS |
| 141 | blake2s: differential seed 7693 |
| 142 | blake2s: differential 2266/2266 PASS |
| 143 | blake2s: mutant expected-digit detected PASS |
| 144 | blake2s: mutant iv0 detected PASS |
| 145 | blake2s: mutant final-flag detected PASS |
| 146 | ``` |
| 147 | |
| 148 | The run exited with code 0 and took about 0.3 s. |
| 149 | |
| 150 | Other runs: |
| 151 | |
| 152 | | Run | Result | |
| 153 | |---|---| |
| 154 | | Differential seeds 1..5 | 2266/2266 each. | |
| 155 | | `PATH` without python3 | Static vectors and mutants PASS, differential NOT_RUN, exit 0. | |
| 156 | | Throwaway trapping mutant `put w 58 tot` → `put w 58 div tot sub tot tot` (divides by zero on every hash) | `mutant trap did not complete (exit 102, selftest exit 102) FAIL`, suite exit 1. The mutant was removed afterwards. | |
| 157 | | Throwaway trapping mutant `put w 58 tot` → `put w 58 div tot sub tot 3` (traps only when the stream is 3 bytes long) | The same FAIL line, suite exit 1. The pre-fix harness counted this mutant as detected, because the selftest's empty output differed from the expected grand hash. | |
| 158 | |
| 159 | Hashes: |
| 160 | |
| 161 | | Item | Value | |
| 162 | |---|---| |
| 163 | | omega-mv pin | `acb3f15cdc691b1b046b48b0dad0f9255a12ba4b` | |
| 164 | | omc sha256 | `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61` | |
| 165 | | `blake2s.om` sha256 | `f2d79b93a79621db2393d22ed4d51a216f43c286ea32461d73e1681d80b5529f` | |
| 166 | | `test/driver.om` sha256 | `f73b0800c9c211d9ddf6f39d71aa4c9c8a3d242f5b8a717848c76d73e4203608` | |
| 167 | | driver binary sha256 | `c3c34c06f8e8aced3fc1666f3d4bb5dc7f0a8339f8cfeb6952246d8525ef11d3` | |
| 168 | |
| 169 | `note` lines do not change the binary. |
| 170 | |
| 171 | These are host runs only. They are not hardware qualification. |
| 172 | |
| 173 | ## Limits |
| 174 | |
| 175 | - **Not constant-time-audited.** Omega gives no timing guarantee, and no timing was measured. For |
| 176 | example, `b2sbyte` branches on the stream position and the lengths. Branching on data values was |
| 177 | not audited. |
| 178 | - One-shot only: there is no incremental (init/update/final) API. Messages are limited to 65536 |
| 179 | bytes. |
| 180 | - No salt or personalization parameters, and no tree-hashing parameters. The parameter block holds |
| 181 | only digest length, key length, fanout 1 and depth 1. |
| 182 | - The block loop has a bound of 2000. At most 1026 blocks are needed. |