interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / argon2 / argon2.om

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