interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / argon2 / argon2pool.om

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