interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / blake2b / blake2b.om

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