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/blake2b/blake2b.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 / blake2b / blake2b.om` 202 lines ยท 6137 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note blake2b.om: BLAKE2b (RFC 7693) in Omega, keyed or unkeyed, digest length 1..64 bytes. 3 note Library unit: functions only, no main, no globals. Every name starts with b2b. 4 note 5 note b2bhash 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 64 key bytes; only k[0..kn) are read 9 note kn ulong key length, 0..64 (0 = unkeyed) 10 note outn ulong digest length, 1..64 11 note o edit uchar 64 o[0..outn) receives the digest, o[outn..64) 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 64-bit words live in ulong cells; ulong add wraps modulo 2^64, which is the RFC's addition. 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 (low 64 bits; the high 64 bits are always 0 here), 59 SIGMA row of the 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-512 IV, 6A09E667F3BCC908 BB67AE8584CAA73B 3C6EF372FE94F82B 22 note A54FF53A5F1D36F1 510E527FADE682D1 9B05688C2B3E6C1F 1F83D9ABFB41BD6B 5BE0CD19137E2179 23 fn b2binit w edit ulong 64 to void 24 put w 40 `7640891576956012808` 25 put w 41 `13503953896175478587` 26 put w 42 `4354685564936845355` 27 put w 43 `11912009170470909681` 28 put w 44 `5840696475078001361` 29 put w 45 `11170449401992604703` 30 put w 46 `2270897969802886507` 31 put w 47 `6620516959819538809` 32 note SIGMA[0] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 33 put w 48 `18364758544493064720` 34 note SIGMA[1] 14 10 4 8 9 15 13 6 1 12 0 2 11 7 5 3 35 put w 49 `3853709921291437230` 36 note SIGMA[2] 11 8 12 0 5 2 15 13 10 14 3 6 7 1 9 4 37 put w 50 `5266788149650328715` 38 note SIGMA[3] 7 9 3 1 13 12 11 14 2 6 5 10 4 0 15 8 39 put w 51 `10305543691612001175` 40 note SIGMA[4] 9 0 5 7 2 4 10 15 14 1 11 12 6 8 3 13 41 put w 52 `15242093322790139145` 42 note SIGMA[5] 2 12 6 10 0 11 8 3 4 13 7 5 15 14 1 9 43 put w 53 `10515720223929181890` 44 note SIGMA[6] 12 5 1 15 14 13 4 10 0 7 6 3 9 2 8 11 45 put w 54 `13270197634454188380` 46 note SIGMA[7] 13 11 7 14 12 1 3 9 5 0 15 4 8 6 2 10 47 put w 55 `11702690517083809725` 48 note SIGMA[8] 6 15 14 9 11 3 0 8 12 2 13 7 1 4 10 5 49 put w 56 `6503616966983130870` 50 note SIGMA[9] 10 2 8 4 7 6 1 5 15 11 9 14 3 12 13 0 51 put w 57 `991893350865389610` 52 end 53 54 note message word m[s[e]] of the current round, e = 0..15 55 fn b2bmw w edit ulong 64 e ulong to ulong 56 s = and shr get w 59 mul e 4 15 57 ret get w add 16 s 58 end 59 60 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 = 32 24 16 63 61 fn b2bg w edit ulong 64 a ulong b ulong c ulong d ulong j ulong to void 62 x = b2bmw w mul j 2 63 put w a add add get w a get w b x 64 t = xor get w d get w a 65 put w d or shr t 32 shl t 32 66 put w c add get w c get w d 67 t = xor get w b get w c 68 put w b or shr t 24 shl t 40 69 x = b2bmw w add mul j 2 1 70 put w a add add get w a get w b x 71 t = xor get w d get w a 72 put w d or shr t 16 shl t 48 73 put w c add get w c get w d 74 t = xor get w b get w c 75 put w b or shr t 63 shl t 1 76 end 77 78 note one round i of RFC 7693 3.2 with s = SIGMA[i mod 10] 79 fn b2bround w edit ulong 64 i ulong to void 80 put w 59 get w add 48 rem i 10 81 b2bg w 0 4 8 12 0 82 b2bg w 1 5 9 13 1 83 b2bg w 2 6 10 14 2 84 b2bg w 3 7 11 15 3 85 b2bg w 0 5 10 15 4 86 b2bg w 1 6 11 12 5 87 b2bg w 2 7 8 13 6 88 b2bg w 3 4 9 14 7 89 end 90 91 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; 12 rounds 92 fn b2bcomp w edit ulong 64 last ulong to void 93 for i ulong 8 94 put w i get w add 32 i 95 put w add i 8 get w add 40 i 96 end 97 put w 12 xor get w 12 get w 58 98 if eq last 1 99 put w 14 xor get w 14 `18446744073709551615` 100 end 101 for r ulong 12 102 b2bround w r 103 end 104 for i ulong 8 105 put w add 32 i xor get w add 32 i xor get w i get w add i 8 106 end 107 end 108 109 note byte p of the padded stream d = key block (128 bytes, only when kn > 0) || m[0..n), 0 past its end 110 fn b2bbyte m view uchar 65536 n ulong k view uchar 64 kn ulong p ulong to ulong 111 q = p 112 if gt kn 0 113 if lt p 128 114 if lt p kn 115 ret ulong get k p 116 end 117 ret 0 118 end 119 q = sub p 128 120 end 121 if lt q n 122 ret ulong get m q 123 end 124 ret 0 125 end 126 127 note little-endian word of stream bytes d[p .. p+8) 128 fn b2bword m view uchar 65536 n ulong k view uchar 64 kn ulong p ulong to ulong 129 v = ulong 0 130 for j ulong 8 131 v = or shl v 8 b2bbyte m n k kn sub add p 7 j 132 end 133 ret v 134 end 135 136 note load the 16 little-endian words of stream block d[b .. b+128) into m (w 16..31) 137 fn b2bload w edit ulong 64 m view uchar 65536 n ulong k view uchar 64 kn ulong b ulong to void 138 for i ulong 16 139 put w add 16 i b2bword m n k kn add b mul i 8 140 end 141 end 142 143 note h := IV, then RFC 7693 3.3 parameter block: h[0] ^= 0x01010000 ^ (kn << 8) ^ outn 144 fn b2bstart w edit ulong 64 kn ulong outn ulong to void 145 b2binit w 146 for i ulong 8 147 put w add 32 i get w add 40 i 148 end 149 p = xor `16842752` xor shl kn 8 outn 150 put w 32 xor get w 32 p 151 end 152 153 note h as little-endian bytes: o[0..outn) the digest, o[outn..64) zero 154 fn b2bout w edit ulong 64 outn ulong o edit uchar 64 to void 155 for i ulong 64 156 put o i 0 157 if lt i outn 158 v = get w add 32 shr i 3 159 put o i uchar and shr v mul and i 7 8 255 160 end 161 end 162 end 163 164 note RFC 7693 3.3 over the stream d (key block when kn > 0, then m): every 128-byte block but the 165 note last is compressed with t = bytes so far, the last (zero-padded) block with t = the whole stream 166 note length and the final flag; an empty stream (kn = 0, n = 0) is one zero block with t = 0. 167 note At most 513 blocks (128 + 65536 bytes); the block loop bound is 1000. 168 fn b2bhash m view uchar 65536 n ulong k view uchar 64 kn ulong outn ulong o edit uchar 64 to void 169 if gt n 65536 170 trap 3 171 end 172 if gt kn 64 173 trap 3 174 end 175 if eq outn 0 176 trap 3 177 end 178 if gt outn 64 179 trap 3 180 end 181 w = new ulong 64 0 182 b2bstart w kn outn 183 tot = n 184 if gt kn 0 185 tot = add n 128 186 end 187 nb = ulong 1 188 if gt tot 0 189 nb = div add tot 127 128 190 end 191 b = ulong 0 192 while lt add b 1 nb bound 1000 193 b2bload w m n k kn mul b 128 194 put w 58 mul add b 1 128 195 b2bcomp w 0 196 b = add b 1 197 end 198 b2bload w m n k kn mul b 128 199 put w 58 tot 200 b2bcomp w 1 201 b2bout w outn o 202 end