interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / blake2s / blake2s.om

201 lines · 6007 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note blake2s.om: BLAKE2s (RFC 7693) in Omega, keyed or unkeyed, digest length 1..32 bytes.
3note Library unit: functions only, no main, no globals. Every name starts with b2s.
4note
5note b2shash m n k kn outn o
6note m view uchar 65536 message bytes; only m[0..n) are read
7note n ulong message length, 0..65536
8note k view uchar 32 key bytes; only k[0..kn) are read
9note kn ulong key length, 0..32 (0 = unkeyed)
10note outn ulong digest length, 1..32
11note o edit uchar 32 o[0..outn) receives the digest, o[outn..32) is set to 0
12note A length outside its range traps 3 (program exit 103) before any output is written.
13note
14note 32-bit words live in ulong cells and are masked to 32 bits after every add and rotate.
15note Working words w (one array: a library unit has no globals):
16note 0..15 v (RFC 7693 3.2), 16..31 m, 32..39 h, 40..47 IV (2.6),
17note 48..57 SIGMA rows (2.7) packed as sixteen 4-bit indexes, entry i in bits 4i..4i+3,
18note 58 byte counter t, 59 SIGMA row of the current round.
19note NOT constant-time-audited: Omega gives no timing guarantee and none was measured.
20
21note RFC 7693 2.6: IV[i] = SHA-256 IV, 6A09E667 BB67AE85 3C6EF372 A54FF53A 510E527F 9B05688C 1F83D9AB 5BE0CD19
22fn b2sinit w edit ulong 64 to void
23put w 40 1779033703
24put w 41 3144134277
25put w 42 1013904242
26put w 43 2773480762
27put w 44 1359893119
28put w 45 2600822924
29put w 46 528734635
30put w 47 1541459225
31note SIGMA[0] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
32put w 48 18364758544493064720
33note SIGMA[1] 14 10 4 8 9 15 13 6 1 12 0 2 11 7 5 3
34put w 49 3853709921291437230
35note SIGMA[2] 11 8 12 0 5 2 15 13 10 14 3 6 7 1 9 4
36put w 50 5266788149650328715
37note SIGMA[3] 7 9 3 1 13 12 11 14 2 6 5 10 4 0 15 8
38put w 51 10305543691612001175
39note SIGMA[4] 9 0 5 7 2 4 10 15 14 1 11 12 6 8 3 13
40put w 52 15242093322790139145
41note SIGMA[5] 2 12 6 10 0 11 8 3 4 13 7 5 15 14 1 9
42put w 53 10515720223929181890
43note SIGMA[6] 12 5 1 15 14 13 4 10 0 7 6 3 9 2 8 11
44put w 54 13270197634454188380
45note SIGMA[7] 13 11 7 14 12 1 3 9 5 0 15 4 8 6 2 10
46put w 55 11702690517083809725
47note SIGMA[8] 6 15 14 9 11 3 0 8 12 2 13 7 1 4 10 5
48put w 56 6503616966983130870
49note SIGMA[9] 10 2 8 4 7 6 1 5 15 11 9 14 3 12 13 0
50put w 57 991893350865389610
51end
52
53note message word m[s[e]] of the current round, e = 0..15
54fn b2smw w edit ulong 64 e ulong to ulong
55s = and shr get w 59 mul e 4 15
56ret get w add 16 s
57end
58
59note 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 = 16 12 8 7
60fn b2sg w edit ulong 64 a ulong b ulong c ulong d ulong j ulong to void
61x = b2smw w mul j 2
62t = add get w a get w b
63put w a and add t x 4294967295
64t = xor get w d get w a
65put w d and or shr t 16 shl t 16 4294967295
66put w c and add get w c get w d 4294967295
67t = xor get w b get w c
68put w b and or shr t 12 shl t 20 4294967295
69x = b2smw w add mul j 2 1
70t = add get w a get w b
71put w a and add t x 4294967295
72t = xor get w d get w a
73put w d and or shr t 8 shl t 24 4294967295
74put w c and add get w c get w d 4294967295
75t = xor get w b get w c
76put w b and or shr t 7 shl t 25 4294967295
77end
78
79note one round i of RFC 7693 3.2 with s = SIGMA[i mod 10]
80fn b2sround w edit ulong 64 i ulong to void
81put w 59 get w add 48 rem i 10
82b2sg w 0 4 8 12 0
83b2sg w 1 5 9 13 1
84b2sg w 2 6 10 14 2
85b2sg w 3 7 11 15 3
86b2sg w 0 5 10 15 4
87b2sg w 1 6 11 12 5
88b2sg w 2 7 8 13 6
89b2sg w 3 4 9 14 7
90end
91
92note RFC 7693 3.2 F(h, m, t, f): m in w 16..31, t in w 58, last = 1 for the final block
93fn b2scomp w edit ulong 64 last ulong to void
94for i ulong 8
95put w i get w add 32 i
96put w add i 8 get w add 40 i
97end
98t = get w 58
99put w 12 xor get w 12 and t 4294967295
100put w 13 xor get w 13 shr t 32
101if eq last 1
102put w 14 xor get w 14 4294967295
103end
104for r ulong 10
105b2sround w r
106end
107for i ulong 8
108put w add 32 i xor get w add 32 i xor get w i get w add i 8
109end
110end
111
112note byte p of the padded stream d = key block (64 bytes, only when kn > 0) || m[0..n), 0 past its end
113fn b2sbyte m view uchar 65536 n ulong k view uchar 32 kn ulong p ulong to ulong
114q = p
115if gt kn 0
116if lt p 64
117if lt p kn
118ret ulong get k p
119end
120ret 0
121end
122q = sub p 64
123end
124if lt q n
125ret ulong get m q
126end
127ret 0
128end
129
130note load the 16 little-endian words of stream block d[b .. b+64) into m (w 16..31)
131fn b2sload w edit ulong 64 m view uchar 65536 n ulong k view uchar 32 kn ulong b ulong to void
132for i ulong 16
133o = add b mul i 4
134v = b2sbyte m n k kn add o 3
135v = or shl v 8 b2sbyte m n k kn add o 2
136v = or shl v 8 b2sbyte m n k kn add o 1
137v = or shl v 8 b2sbyte m n k kn o
138put w add 16 i v
139end
140end
141
142note h := IV, then RFC 7693 3.3 parameter block: h[0] ^= 0x01010000 ^ (kn << 8) ^ outn
143fn b2sstart w edit ulong 64 kn ulong outn ulong to void
144b2sinit w
145for i ulong 8
146put w add 32 i get w add 40 i
147end
148p = xor 16842752 xor shl kn 8 outn
149put w 32 xor get w 32 p
150end
151
152note h as little-endian bytes: o[0..outn) the digest, o[outn..32) zero
153fn b2sout w edit ulong 64 outn ulong o edit uchar 32 to void
154for i ulong 32
155put o i 0
156if lt i outn
157v = get w add 32 shr i 2
158put o i uchar and shr v mul and i 3 8 255
159end
160end
161end
162
163note RFC 7693 3.3 over the stream d (key block when kn > 0, then m): every 64-byte block but the last
164note is compressed with t = bytes so far, the last (zero-padded) block with t = the whole stream
165note length and the final flag; an empty stream (kn = 0, n = 0) is one zero block with t = 0.
166note At most 1026 blocks (64 + 65536 bytes); the block loop bound is 2000.
167fn b2shash m view uchar 65536 n ulong k view uchar 32 kn ulong outn ulong o edit uchar 32 to void
168if gt n 65536
169trap 3
170end
171if gt kn 32
172trap 3
173end
174if eq outn 0
175trap 3
176end
177if gt outn 32
178trap 3
179end
180w = new ulong 64 0
181b2sstart w kn outn
182tot = n
183if gt kn 0
184tot = add n 64
185end
186nb = ulong 1
187if gt tot 0
188nb = div add tot 63 64
189end
190b = ulong 0
191while lt add b 1 nb bound 2000
192b2sload w m n k kn mul b 64
193put w 58 mul add b 1 64
194b2scomp w 0
195b = add b 1
196end
197b2sload w m n k kn mul b 64
198put w 58 tot
199b2scomp w 1
200b2sout w outn o
201end