Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omnet/blob/main/om/blake2s/blake2s.om # omnet aien-dev/omnet Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omnet / om / blake2s / blake2s.om` 201 lines ยท 6007 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note blake2s.om: BLAKE2s (RFC 7693) in Omega, keyed or unkeyed, digest length 1..32 bytes. 3 note Library unit: functions only, no main, no globals. Every name starts with b2s. 4 note 5 note b2shash m n k kn outn o 6 note m view uchar 65536 message bytes; only m[0..n) are read 7 note n ulong message length, 0..65536 8 note k view uchar 32 key bytes; only k[0..kn) are read 9 note kn ulong key length, 0..32 (0 = unkeyed) 10 note outn ulong digest length, 1..32 11 note o edit uchar 32 o[0..outn) receives the digest, o[outn..32) is set to 0 12 note A length outside its range traps 3 (program exit 103) before any output is written. 13 note 14 note 32-bit words live in ulong cells and are masked to 32 bits after every add and rotate. 15 note Working words w (one array: a library unit has no globals): 16 note 0..15 v (RFC 7693 3.2), 16..31 m, 32..39 h, 40..47 IV (2.6), 17 note 48..57 SIGMA rows (2.7) packed as sixteen 4-bit indexes, entry i in bits 4i..4i+3, 18 note 58 byte counter t, 59 SIGMA row of the current round. 19 note NOT constant-time-audited: Omega gives no timing guarantee and none was measured. 20 21 note RFC 7693 2.6: IV[i] = SHA-256 IV, 6A09E667 BB67AE85 3C6EF372 A54FF53A 510E527F 9B05688C 1F83D9AB 5BE0CD19 22 fn b2sinit w edit ulong 64 to void 23 put w 40 `1779033703` 24 put w 41 `3144134277` 25 put w 42 `1013904242` 26 put w 43 `2773480762` 27 put w 44 `1359893119` 28 put w 45 `2600822924` 29 put w 46 `528734635` 30 put w 47 `1541459225` 31 note SIGMA[0] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 32 put w 48 `18364758544493064720` 33 note SIGMA[1] 14 10 4 8 9 15 13 6 1 12 0 2 11 7 5 3 34 put w 49 `3853709921291437230` 35 note SIGMA[2] 11 8 12 0 5 2 15 13 10 14 3 6 7 1 9 4 36 put w 50 `5266788149650328715` 37 note SIGMA[3] 7 9 3 1 13 12 11 14 2 6 5 10 4 0 15 8 38 put w 51 `10305543691612001175` 39 note SIGMA[4] 9 0 5 7 2 4 10 15 14 1 11 12 6 8 3 13 40 put w 52 `15242093322790139145` 41 note SIGMA[5] 2 12 6 10 0 11 8 3 4 13 7 5 15 14 1 9 42 put w 53 `10515720223929181890` 43 note SIGMA[6] 12 5 1 15 14 13 4 10 0 7 6 3 9 2 8 11 44 put w 54 `13270197634454188380` 45 note SIGMA[7] 13 11 7 14 12 1 3 9 5 0 15 4 8 6 2 10 46 put w 55 `11702690517083809725` 47 note SIGMA[8] 6 15 14 9 11 3 0 8 12 2 13 7 1 4 10 5 48 put w 56 `6503616966983130870` 49 note SIGMA[9] 10 2 8 4 7 6 1 5 15 11 9 14 3 12 13 0 50 put w 57 `991893350865389610` 51 end 52 53 note message word m[s[e]] of the current round, e = 0..15 54 fn b2smw w edit ulong 64 e ulong to ulong 55 s = and shr get w 59 mul e 4 15 56 ret get w add 16 s 57 end 58 59 note RFC 7693 3.1 G on v[a] v[b] v[c] v[d] with x = m[s[2j]], y = m[s[2j+1]]; R1..R4 = 16 12 8 7 60 fn b2sg w edit ulong 64 a ulong b ulong c ulong d ulong j ulong to void 61 x = b2smw w mul j 2 62 t = add get w a get w b 63 put w a and add t x `4294967295` 64 t = xor get w d get w a 65 put w d and or shr t 16 shl t 16 `4294967295` 66 put w c and add get w c get w d `4294967295` 67 t = xor get w b get w c 68 put w b and or shr t 12 shl t 20 `4294967295` 69 x = b2smw w add mul j 2 1 70 t = add get w a get w b 71 put w a and add t x `4294967295` 72 t = xor get w d get w a 73 put w d and or shr t 8 shl t 24 `4294967295` 74 put w c and add get w c get w d `4294967295` 75 t = xor get w b get w c 76 put w b and or shr t 7 shl t 25 `4294967295` 77 end 78 79 note one round i of RFC 7693 3.2 with s = SIGMA[i mod 10] 80 fn b2sround w edit ulong 64 i ulong to void 81 put w 59 get w add 48 rem i 10 82 b2sg w 0 4 8 12 0 83 b2sg w 1 5 9 13 1 84 b2sg w 2 6 10 14 2 85 b2sg w 3 7 11 15 3 86 b2sg w 0 5 10 15 4 87 b2sg w 1 6 11 12 5 88 b2sg w 2 7 8 13 6 89 b2sg w 3 4 9 14 7 90 end 91 92 note RFC 7693 3.2 F(h, m, t, f): m in w 16..31, t in w 58, last = 1 for the final block 93 fn b2scomp w edit ulong 64 last ulong to void 94 for i ulong 8 95 put w i get w add 32 i 96 put w add i 8 get w add 40 i 97 end 98 t = get w 58 99 put w 12 xor get w 12 and t `4294967295` 100 put w 13 xor get w 13 shr t 32 101 if eq last 1 102 put w 14 xor get w 14 `4294967295` 103 end 104 for r ulong 10 105 b2sround w r 106 end 107 for i ulong 8 108 put w add 32 i xor get w add 32 i xor get w i get w add i 8 109 end 110 end 111 112 note byte p of the padded stream d = key block (64 bytes, only when kn > 0) || m[0..n), 0 past its end 113 fn b2sbyte m view uchar 65536 n ulong k view uchar 32 kn ulong p ulong to ulong 114 q = p 115 if gt kn 0 116 if lt p 64 117 if lt p kn 118 ret ulong get k p 119 end 120 ret 0 121 end 122 q = sub p 64 123 end 124 if lt q n 125 ret ulong get m q 126 end 127 ret 0 128 end 129 130 note load the 16 little-endian words of stream block d[b .. b+64) into m (w 16..31) 131 fn b2sload w edit ulong 64 m view uchar 65536 n ulong k view uchar 32 kn ulong b ulong to void 132 for i ulong 16 133 o = add b mul i 4 134 v = b2sbyte m n k kn add o 3 135 v = or shl v 8 b2sbyte m n k kn add o 2 136 v = or shl v 8 b2sbyte m n k kn add o 1 137 v = or shl v 8 b2sbyte m n k kn o 138 put w add 16 i v 139 end 140 end 141 142 note h := IV, then RFC 7693 3.3 parameter block: h[0] ^= 0x01010000 ^ (kn << 8) ^ outn 143 fn b2sstart w edit ulong 64 kn ulong outn ulong to void 144 b2sinit w 145 for i ulong 8 146 put w add 32 i get w add 40 i 147 end 148 p = xor `16842752` xor shl kn 8 outn 149 put w 32 xor get w 32 p 150 end 151 152 note h as little-endian bytes: o[0..outn) the digest, o[outn..32) zero 153 fn b2sout w edit ulong 64 outn ulong o edit uchar 32 to void 154 for i ulong 32 155 put o i 0 156 if lt i outn 157 v = get w add 32 shr i 2 158 put o i uchar and shr v mul and i 3 8 255 159 end 160 end 161 end 162 163 note RFC 7693 3.3 over the stream d (key block when kn > 0, then m): every 64-byte block but the last 164 note is compressed with t = bytes so far, the last (zero-padded) block with t = the whole stream 165 note length and the final flag; an empty stream (kn = 0, n = 0) is one zero block with t = 0. 166 note At most 1026 blocks (64 + 65536 bytes); the block loop bound is 2000. 167 fn b2shash m view uchar 65536 n ulong k view uchar 32 kn ulong outn ulong o edit uchar 32 to void 168 if gt n 65536 169 trap 3 170 end 171 if gt kn 32 172 trap 3 173 end 174 if eq outn 0 175 trap 3 176 end 177 if gt outn 32 178 trap 3 179 end 180 w = new ulong 64 0 181 b2sstart w kn outn 182 tot = n 183 if gt kn 0 184 tot = add n 64 185 end 186 nb = ulong 1 187 if gt tot 0 188 nb = div add tot 63 64 189 end 190 b = ulong 0 191 while lt add b 1 nb bound 2000 192 b2sload w m n k kn mul b 64 193 put w 58 mul add b 1 64 194 b2scomp w 0 195 b = add b 1 196 end 197 b2sload w m n k kn mul b 64 198 put w 58 tot 199 b2scomp w 1 200 b2sout w outn o 201 end