omnet
aien-dev/omnet
Brought by drakestapleton
omnet / om / argon2 / argon2pool.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note argon2pool.om: program-mode companion of argon2.om. It owns the Argon2 memory matrix and runs |
| 3 | note the segment loop over it. Concatenate after om/blake2b/blake2b.om and om/argon2/argon2.om. |
| 4 | note Every name starts with a2. |
| 5 | note |
| 6 | note Why a global: the matrix for the TKA parameters is 16384 blocks of 1024 bytes (16 MiB). An |
| 7 | note Omega local array is limited to 4 MiB per function (error 23) inside an 8 MiB stack, and a |
| 8 | note global cannot be passed as an argument (error 19). The one place 16 MiB can live is a program |
| 9 | note global, and only functions that name it can reach it. This file is that one place; the |
| 10 | note algorithm pieces stay in the pure library argon2.om. A program includes this file once. |
| 11 | note |
| 12 | note a2hash prm inp out -> ulong |
| 13 | note prm view ulong 8 t, m KiB, p, T, |P|, |S|, |K|, |X| (ranges: argon2.om header) |
| 14 | note inp view uchar 65536 P || S || K || X |
| 15 | note out edit uchar 1024 out[0..T) receives the tag, out[T..1024) is set to 0 |
| 16 | note returns 0, or 1 when a parameter is out of range (out is then left unchanged) |
| 17 | note a2tka sec sn o |
| 18 | note sec view uchar 1024 disablement secret; only sec[0..sn) is read |
| 19 | note sn ulong secret length, 0..1024; more traps 3 (caller contract) |
| 20 | note o edit uchar 32 receives the Tailnet Lock disablement value: Argon2i with |
| 21 | note S = "tailscale network-lock disablement salt", t = 4, |
| 22 | note m = 16384 KiB, p = 4, T = 32 (golang.org/x/crypto/argon2.Key) |
| 23 | note The matrix is shared by every call; calls run one after another (Omega has no threads). |
| 24 | note NOT constant-time-audited: Omega gives no timing guarantee and none was measured. |
| 25 | |
| 26 | note 2097152 words = 16384 blocks of 128 words = 16 MiB, the largest m accepted by a2setup |
| 27 | global a2mem ulong 2097152 |
| 28 | text a2tsalt "tailscale network-lock disablement salt" |
| 29 | |
| 30 | note blk = matrix block b |
| 31 | fn a2get blk edit ulong 128 b ulong to void |
| 32 | o = mul b 128 |
| 33 | for i ulong 128 |
| 34 | put blk i get a2mem add o i |
| 35 | end |
| 36 | end |
| 37 | |
| 38 | note matrix block b = blk |
| 39 | fn a2put blk view ulong 128 b ulong to void |
| 40 | o = mul b 128 |
| 41 | for i ulong 128 |
| 42 | put a2mem add o i get blk i |
| 43 | end |
| 44 | end |
| 45 | |
| 46 | note first index of the current segment: 2 in pass 0 slice 0 (B[l][0], B[l][1] are already set) |
| 47 | fn a2i0 st view ulong 16 to ulong |
| 48 | if and eq get st 5 0 eq get st 6 0 |
| 49 | ret 2 |
| 50 | end |
| 51 | ret 0 |
| 52 | end |
| 53 | |
| 54 | note absolute block number of index i of the current segment: l * q + s * (q/4) + i |
| 55 | fn a2abs st view ulong 16 i ulong to ulong |
| 56 | b = add mul get st 7 get st 3 mul get st 6 get st 4 |
| 57 | ret add b i |
| 58 | end |
| 59 | |
| 60 | note block before index i0 of the current segment: the lane's last block when it is column 0 |
| 61 | fn a2prev st view ulong 16 i0 ulong to ulong |
| 62 | b = a2abs st i0 |
| 63 | if and eq i0 0 eq get st 6 0 |
| 64 | b = add b get st 3 |
| 65 | end |
| 66 | ret sub b 1 |
| 67 | end |
| 68 | |
| 69 | note matrix block cur (index i of the segment, pv = the block before it) from the pseudo-random |
| 70 | note value rnd: pass 0 writes G(prev, ref), later passes write B ^ G(prev, ref) (version 0x13); |
| 71 | note pv then holds the new block, the previous block of index i + 1 |
| 72 | fn a2blk st view ulong 16 pv edit ulong 128 rnd ulong i ulong cur ulong to void |
| 73 | rf = new ulong 128 0 |
| 74 | cu = new ulong 128 0 |
| 75 | a2get rf a2ref st rnd i |
| 76 | xr = ulong 0 |
| 77 | if gt get st 5 0 |
| 78 | xr = 1 |
| 79 | a2get cu cur |
| 80 | end |
| 81 | a2comp pv rf cu xr |
| 82 | a2put cu cur |
| 83 | for j ulong 128 |
| 84 | put pv j get cu j |
| 85 | end |
| 86 | end |
| 87 | |
| 88 | note RFC 9106 3.2 steps 5-6 for one segment (pass st5, slice st6, lane st7) with Argon2i addressing |
| 89 | note (3.4.1.2): a new address block before the first index and at every multiple of 128 |
| 90 | fn a2seg st view ulong 16 to void |
| 91 | z = new ulong 128 0 |
| 92 | adr = new ulong 128 0 |
| 93 | pv = new ulong 128 0 |
| 94 | a2zin st z |
| 95 | i0 = a2i0 st |
| 96 | sl = get st 4 |
| 97 | cur = a2abs st i0 |
| 98 | a2get pv a2prev st i0 |
| 99 | i = i0 |
| 100 | while lt i sl bound 999999 |
| 101 | if or eq i i0 eq rem i 128 0 |
| 102 | a2addr z adr |
| 103 | end |
| 104 | a2blk st pv get adr rem i 128 i cur |
| 105 | cur = add cur 1 |
| 106 | i = add i 1 |
| 107 | end |
| 108 | end |
| 109 | |
| 110 | note RFC 9106 3.2 steps 1-4: H0, then B[l][0] and B[l][1] of every lane |
| 111 | fn a2init st view ulong 16 prm view ulong 8 inp view uchar 65536 to void |
| 112 | h0 = new uchar 64 0 |
| 113 | a2h0 prm inp h0 |
| 114 | blk = new ulong 128 0 |
| 115 | q = get st 3 |
| 116 | for l ulong get st 2 |
| 117 | a2first h0 0 l blk |
| 118 | a2put blk mul l q |
| 119 | a2first h0 1 l blk |
| 120 | a2put blk add mul l q 1 |
| 121 | end |
| 122 | end |
| 123 | |
| 124 | note RFC 9106 3.2 steps 5-6: t passes of 4 slices over p lanes; the lanes of one slice never read |
| 125 | note each other's segment, so running them in order gives the parallel result |
| 126 | fn a2fill st edit ulong 16 to void |
| 127 | r = ulong 0 |
| 128 | while lt r get st 0 bound 999999 |
| 129 | put st 5 r |
| 130 | for s ulong 4 |
| 131 | put st 6 s |
| 132 | for l ulong get st 2 |
| 133 | put st 7 l |
| 134 | a2seg st |
| 135 | end |
| 136 | end |
| 137 | r = add r 1 |
| 138 | end |
| 139 | end |
| 140 | |
| 141 | note RFC 9106 3.2 steps 7-8: C = XOR of the last block of every lane, out = H'^T(C) |
| 142 | fn a2fin st view ulong 16 out edit uchar 1024 to void |
| 143 | q = get st 3 |
| 144 | c = new ulong 128 0 |
| 145 | blk = new ulong 128 0 |
| 146 | a2get c sub q 1 |
| 147 | for l ulong sub get st 2 1 |
| 148 | a2get blk sub mul add l 2 q 1 |
| 149 | for i ulong 128 |
| 150 | put c i xor get c i get blk i |
| 151 | end |
| 152 | end |
| 153 | for i ulong 1024 |
| 154 | put out i 0 |
| 155 | end |
| 156 | a2tag c get st 8 out |
| 157 | end |
| 158 | |
| 159 | fn a2hash prm view ulong 8 inp view uchar 65536 out edit uchar 1024 to ulong |
| 160 | st = new ulong 16 0 |
| 161 | e = a2setup prm st |
| 162 | if ne e 0 |
| 163 | ret e |
| 164 | end |
| 165 | a2init st prm inp |
| 166 | a2fill st |
| 167 | a2fin st out |
| 168 | ret 0 |
| 169 | end |
| 170 | |
| 171 | fn a2tka sec view uchar 1024 sn ulong o edit uchar 32 to void |
| 172 | if gt sn 1024 |
| 173 | trap 3 |
| 174 | end |
| 175 | prm = new ulong 8 0 |
| 176 | put prm 0 4 |
| 177 | put prm 1 16384 |
| 178 | put prm 2 4 |
| 179 | put prm 3 32 |
| 180 | put prm 4 sn |
| 181 | put prm 5 39 |
| 182 | inp = new uchar 65536 0 |
| 183 | for i ulong sn |
| 184 | put inp i get sec i |
| 185 | end |
| 186 | for i ulong 39 |
| 187 | put inp add sn i get a2tsalt i |
| 188 | end |
| 189 | t = new uchar 1024 0 |
| 190 | if ne a2hash prm inp t 0 |
| 191 | trap 1 |
| 192 | end |
| 193 | for i ulong 32 |
| 194 | put o i get t i |
| 195 | end |
| 196 | end |