omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / argon2 / argon2.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note argon2.om: Argon2i version 0x13 (RFC 9106) building blocks in Omega, hashed with om/blake2b. |
| 3 | note Library unit: functions only, no main, no globals. Every name starts with a2. |
| 4 | note Concatenate after om/blake2b/blake2b.om. The memory matrix (up to 16 MiB) does not fit in an |
| 5 | note Omega local array (at most 4 MiB per function, 8 MiB of stack per program) and a global cannot |
| 6 | note be passed to a function (error 19), so the segment loop that reads and writes the matrix lives |
| 7 | note in the program-mode companion argon2pool.om, which owns the matrix as a global. This file holds |
| 8 | note everything that does not touch the matrix: parameter checks, H0, H', the first blocks, the |
| 9 | note compression G, the Argon2i address blocks, the reference index and the tag. |
| 10 | note |
| 11 | note Parameters prm (view ulong 8): 0 t passes, 1 m KiB, 2 p lanes, 3 T tag bytes, |
| 12 | note 4 |P|, 5 |S|, 6 |K|, 7 |X|; inp (view uchar 65536) holds P || S || K || X back to back. |
| 13 | note State st (ulong 16): 0 t, 1 m' blocks, 2 p, 3 q = m'/p, 4 segment length q/4, |
| 14 | note 5 pass r, 6 slice s, 7 lane l, 8 T, 9 m, 10 type y = 1 (Argon2i). |
| 15 | note Accepted ranges (else a2setup returns 1): 1 <= t <= 999999, 1 <= p, 8p <= m <= 16384, |
| 16 | note 4 <= T <= 1024. |P|, |S|, |K|, |X| each <= 65536 with |P|+|S|+|K|+|X| <= 65496 is the caller's |
| 17 | note contract (the H0 input must fit one 65536-byte BLAKE2b message); breaking it traps 3. |
| 18 | note Blocks are ulong 128 arrays, word i = little-endian bytes 8i..8i+7 of the 1024-byte block. |
| 19 | note NOT constant-time-audited: Omega gives no timing guarantee and none was measured. Argon2i's |
| 20 | note memory access pattern is independent of the password by design (RFC 9106 3.4.1.2). |
| 21 | |
| 22 | note b[at .. at+4) = LE32(v) |
| 23 | fn a2le32 b edit uchar 65536 at ulong v ulong to void |
| 24 | for i ulong 4 |
| 25 | put b add at i uchar and shr v mul i 8 255 |
| 26 | end |
| 27 | end |
| 28 | |
| 29 | note o[at .. at+n) = v[0 .. n) |
| 30 | fn a2cp v view uchar 64 o edit uchar 1024 at ulong n ulong to void |
| 31 | for i ulong n |
| 32 | put o add at i get v i |
| 33 | end |
| 34 | end |
| 35 | |
| 36 | note b[0 .. 64) = v[0 .. 64) |
| 37 | fn a2cp64 v view uchar 64 b edit uchar 65536 to void |
| 38 | for i ulong 64 |
| 39 | put b i get v i |
| 40 | end |
| 41 | end |
| 42 | |
| 43 | note RFC 9106 3.3 H'^T(x[0..xn)): T <= 64 is H^T(LE32(T) || x); else r = ceil(T/32) - 2, |
| 44 | note V1 = H^64(LE32(T) || x), Vi = H^64(V(i-1)) for i = 2..r, V(r+1) = H^(T-32r)(Vr), and the |
| 45 | note output is the first 32 bytes of V1..Vr followed by V(r+1). Caller: 1 <= T <= 1024, xn <= 65532. |
| 46 | fn a2hp tl ulong x view uchar 65536 xn ulong out edit uchar 1024 to void |
| 47 | b = new uchar 65536 0 |
| 48 | k = new uchar 64 0 |
| 49 | v = new uchar 64 0 |
| 50 | a2le32 b 0 tl |
| 51 | for i ulong xn |
| 52 | put b add 4 i get x i |
| 53 | end |
| 54 | if le tl 64 |
| 55 | b2bhash b add xn 4 k 0 tl v |
| 56 | a2cp v out 0 tl |
| 57 | else |
| 58 | r = sub div add tl 31 32 2 |
| 59 | b2bhash b add xn 4 k 0 64 v |
| 60 | a2cp v out 0 32 |
| 61 | at = ulong 32 |
| 62 | for j ulong sub r 1 |
| 63 | a2cp64 v b |
| 64 | b2bhash b 64 k 0 64 v |
| 65 | a2cp v out at 32 |
| 66 | at = add at 32 |
| 67 | end |
| 68 | a2cp64 v b |
| 69 | last = sub tl mul 32 r |
| 70 | b2bhash b 64 k 0 last v |
| 71 | a2cp v out at last |
| 72 | end |
| 73 | end |
| 74 | |
| 75 | note checks prm against the accepted ranges and fills st; 0 = accepted, 1 = parameter out of range |
| 76 | fn a2setup prm view ulong 8 st edit ulong 16 to ulong |
| 77 | for i ulong 4 |
| 78 | if gt get prm add 4 i 65536 |
| 79 | trap 3 |
| 80 | end |
| 81 | end |
| 82 | s = add add get prm 4 get prm 5 add get prm 6 get prm 7 |
| 83 | if gt s 65496 |
| 84 | trap 3 |
| 85 | end |
| 86 | t = get prm 0 |
| 87 | m = get prm 1 |
| 88 | p = get prm 2 |
| 89 | tl = get prm 3 |
| 90 | if or lt t 1 gt t 999999 |
| 91 | ret 1 |
| 92 | end |
| 93 | if or lt p 1 gt p 2048 |
| 94 | ret 1 |
| 95 | end |
| 96 | if or lt m mul 8 p gt m 16384 |
| 97 | ret 1 |
| 98 | end |
| 99 | if or lt tl 4 gt tl 1024 |
| 100 | ret 1 |
| 101 | end |
| 102 | mp = mul mul 4 p div m mul 4 p |
| 103 | put st 0 t |
| 104 | put st 1 mp |
| 105 | put st 2 p |
| 106 | put st 3 div mp p |
| 107 | put st 4 div mp mul 4 p |
| 108 | put st 8 tl |
| 109 | put st 9 m |
| 110 | put st 10 1 |
| 111 | ret 0 |
| 112 | end |
| 113 | |
| 114 | note RFC 9106 3.2 step 1: h = H^64(LE32(p) || LE32(T) || LE32(m) || LE32(t) || LE32(0x13) || |
| 115 | note LE32(y) || LE32(|P|) || P || LE32(|S|) || S || LE32(|K|) || K || LE32(|X|) || X) |
| 116 | fn a2h0 prm view ulong 8 inp view uchar 65536 h edit uchar 64 to void |
| 117 | b = new uchar 65536 0 |
| 118 | a2le32 b 0 get prm 2 |
| 119 | a2le32 b 4 get prm 3 |
| 120 | a2le32 b 8 get prm 1 |
| 121 | a2le32 b 12 get prm 0 |
| 122 | a2le32 b 16 19 |
| 123 | a2le32 b 20 1 |
| 124 | at = ulong 24 |
| 125 | src = ulong 0 |
| 126 | for f ulong 4 |
| 127 | fl = get prm add 4 f |
| 128 | a2le32 b at fl |
| 129 | at = add at 4 |
| 130 | for i ulong fl |
| 131 | put b add at i get inp add src i |
| 132 | end |
| 133 | at = add at fl |
| 134 | src = add src fl |
| 135 | end |
| 136 | k = new uchar 64 0 |
| 137 | b2bhash b at k 0 64 h |
| 138 | end |
| 139 | |
| 140 | note blk = the 128 little-endian words of o |
| 141 | fn a2b2w o view uchar 1024 blk edit ulong 128 to void |
| 142 | for i ulong 128 |
| 143 | v = ulong 0 |
| 144 | for j ulong 8 |
| 145 | v = or shl v 8 ulong get o add mul i 8 sub 7 j |
| 146 | end |
| 147 | put blk i v |
| 148 | end |
| 149 | end |
| 150 | |
| 151 | note RFC 9106 3.2 steps 3-4: blk = B[l][j] = H'^1024(H0 || LE32(j) || LE32(l)), j = 0 or 1 |
| 152 | fn a2first h0 view uchar 64 j ulong l ulong blk edit ulong 128 to void |
| 153 | x = new uchar 65536 0 |
| 154 | for i ulong 64 |
| 155 | put x i get h0 i |
| 156 | end |
| 157 | a2le32 x 64 j |
| 158 | a2le32 x 68 l |
| 159 | o = new uchar 1024 0 |
| 160 | a2hp 1024 x 72 o |
| 161 | a2b2w o blk |
| 162 | end |
| 163 | |
| 164 | note RFC 9106 3.6 GB with the BlaMka multiply: x + y + 2 * lo32(x) * lo32(y), all modulo 2^64 |
| 165 | fn a2fm x ulong y ulong to ulong |
| 166 | ret add add x y shl mul and x 4294967295 and y 4294967295 1 |
| 167 | end |
| 168 | |
| 169 | note RFC 9106 3.6 GB(a, b, c, d) on r[a] r[b] r[c] r[d]; rotations right by 32, 24, 16, 63 |
| 170 | fn a2gb r edit ulong 128 a ulong b ulong c ulong d ulong to void |
| 171 | put r a a2fm get r a get r b |
| 172 | t = xor get r d get r a |
| 173 | put r d or shr t 32 shl t 32 |
| 174 | put r c a2fm get r c get r d |
| 175 | t = xor get r b get r c |
| 176 | put r b or shr t 24 shl t 40 |
| 177 | put r a a2fm get r a get r b |
| 178 | t = xor get r d get r a |
| 179 | put r d or shr t 16 shl t 48 |
| 180 | put r c a2fm get r c get r d |
| 181 | t = xor get r b get r c |
| 182 | put r b or shr t 63 shl t 1 |
| 183 | end |
| 184 | |
| 185 | note word index of input k = 0..15 of P on row i (md 0: words 16i..16i+15) or on column i |
| 186 | note (md 1: the 16-byte registers R(i), R(i+8), .., R(i+56), i.e. words 2i + 16(k/2) + k%2) |
| 187 | fn a2ix md ulong i ulong k ulong to ulong |
| 188 | if eq md 0 |
| 189 | ret add mul i 16 k |
| 190 | end |
| 191 | ret add add mul i 2 mul shr k 1 16 and k 1 |
| 192 | end |
| 193 | |
| 194 | note RFC 9106 3.6 P on the 16 words selected by a2ix md i |
| 195 | fn a2p r edit ulong 128 md ulong i ulong to void |
| 196 | x = new ulong 16 0 |
| 197 | for k ulong 16 |
| 198 | put x k a2ix md i k |
| 199 | end |
| 200 | a2gb r get x 0 get x 4 get x 8 get x 12 |
| 201 | a2gb r get x 1 get x 5 get x 9 get x 13 |
| 202 | a2gb r get x 2 get x 6 get x 10 get x 14 |
| 203 | a2gb r get x 3 get x 7 get x 11 get x 15 |
| 204 | a2gb r get x 0 get x 5 get x 10 get x 15 |
| 205 | a2gb r get x 1 get x 6 get x 11 get x 12 |
| 206 | a2gb r get x 2 get x 7 get x 8 get x 13 |
| 207 | a2gb r get x 3 get x 4 get x 9 get x 14 |
| 208 | end |
| 209 | |
| 210 | note RFC 9106 3.5 G(X, Y): R = X ^ Y, Q = P on each row of R, Z = P on each column of Q, |
| 211 | note G = Z ^ R. out = G(x, y) when xr = 0; out = out ^ G(x, y) when xr = 1 (3.2 step 6, v 0x13) |
| 212 | fn a2comp x view ulong 128 y view ulong 128 out edit ulong 128 xr ulong to void |
| 213 | r = new ulong 128 0 |
| 214 | z = new ulong 128 0 |
| 215 | for i ulong 128 |
| 216 | v = xor get x i get y i |
| 217 | put r i v |
| 218 | put z i v |
| 219 | end |
| 220 | for i ulong 8 |
| 221 | a2p z 0 i |
| 222 | end |
| 223 | for i ulong 8 |
| 224 | a2p z 1 i |
| 225 | end |
| 226 | for i ulong 128 |
| 227 | w = xor get z i get r i |
| 228 | if eq xr 1 |
| 229 | w = xor w get out i |
| 230 | end |
| 231 | put out i w |
| 232 | end |
| 233 | end |
| 234 | |
| 235 | note RFC 9106 3.4.1.2 input block Z of segment (r, l, s): LE64 r, l, s, m', t, y, counter 0, zeros |
| 236 | fn a2zin st view ulong 16 z edit ulong 128 to void |
| 237 | for i ulong 128 |
| 238 | put z i 0 |
| 239 | end |
| 240 | put z 0 get st 5 |
| 241 | put z 1 get st 7 |
| 242 | put z 2 get st 6 |
| 243 | put z 3 get st 1 |
| 244 | put z 4 get st 0 |
| 245 | put z 5 get st 10 |
| 246 | end |
| 247 | |
| 248 | note next address block: counter z[6] += 1, adr = G(0, G(0, Z)) |
| 249 | fn a2addr z edit ulong 128 adr edit ulong 128 to void |
| 250 | put z 6 add get z 6 1 |
| 251 | zr = new ulong 128 0 |
| 252 | t = new ulong 128 0 |
| 253 | a2comp zr z t 0 |
| 254 | a2comp zr t adr 0 |
| 255 | end |
| 256 | |
| 257 | note RFC 9106 3.4.1.1: reference lane from J2 = rnd >> 32; in pass 0 slice 0 the current lane |
| 258 | fn a2rlane st view ulong 16 rnd ulong to ulong |
| 259 | if and eq get st 5 0 eq get st 6 0 |
| 260 | ret get st 7 |
| 261 | end |
| 262 | ret rem shr rnd 32 get st 2 |
| 263 | end |
| 264 | |
| 265 | note RFC 9106 3.4.2: size of the reference area for index i of the current segment with reference |
| 266 | note lane rl: every finished block that may be read, minus the previous block; the same lane also |
| 267 | note takes the blocks of this segment before i |
| 268 | fn a2area st view ulong 16 rl ulong i ulong to ulong |
| 269 | s = get st 6 |
| 270 | sl = get st 4 |
| 271 | same = eq rl get st 7 |
| 272 | area = mul 3 sl |
| 273 | if same |
| 274 | area = add area i |
| 275 | end |
| 276 | if eq get st 5 0 |
| 277 | area = mul s sl |
| 278 | if or eq s 0 same |
| 279 | area = add area i |
| 280 | end |
| 281 | end |
| 282 | if or eq i 0 same |
| 283 | area = sub area 1 |
| 284 | end |
| 285 | ret area |
| 286 | end |
| 287 | |
| 288 | note RFC 9106 3.4.2: absolute block number (lane * q + column) referenced by index i of the current |
| 289 | note segment, from the 64-bit pseudo-random value rnd (J1 = low 32 bits, J2 = high 32 bits): the area |
| 290 | note starts after the current segment in later passes, at column 0 in pass 0 |
| 291 | fn a2ref st view ulong 16 rnd ulong i ulong to ulong |
| 292 | rl = a2rlane st rnd |
| 293 | area = a2area st rl i |
| 294 | from = mul rem add get st 6 1 4 get st 4 |
| 295 | if eq get st 5 0 |
| 296 | from = 0 |
| 297 | end |
| 298 | x = and rnd 4294967295 |
| 299 | x = shr mul x x 32 |
| 300 | x = shr mul area x 32 |
| 301 | q = get st 3 |
| 302 | c = rem sub add from area add x 1 q |
| 303 | ret add mul rl q c |
| 304 | end |
| 305 | |
| 306 | note RFC 9106 3.2 step 8: out[0 .. T) = H'^T(c as 1024 little-endian bytes) |
| 307 | fn a2tag c view ulong 128 tl ulong out edit uchar 1024 to void |
| 308 | b = new uchar 65536 0 |
| 309 | for i ulong 128 |
| 310 | v = get c i |
| 311 | for j ulong 8 |
| 312 | y = uchar and shr v mul j 8 255 |
| 313 | put b add mul i 8 j y |
| 314 | end |
| 315 | end |
| 316 | a2hp tl b 1024 out |
| 317 | end |