interplane.Get Omega
Read in your language

omnet

aien-dev/omnet

Brought by drakestapleton

branch main Branches Tags History

omnet / om / sha512 / sha512.om

332 lines · 8985 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note sha512.om: SHA-512 (FIPS 180-4 section 6.4) as a library unit: functions only, no main, no
3note globals. Every function name starts with s512.
4note
5note API (one shot):
6note fn s512hash m view uchar 65536 n ulong o edit uchar 64 to void
7note writes SHA-512(m[0..n)) as 64 bytes to o[0..64); n > 65536 traps 3.
8note fn s512pre p view uchar 64 m view uchar 65536 n ulong o edit uchar 64 to void
9note writes SHA-512(p[0..64) || m[0..n)) to o[0..64); n > 65536 traps 3.
10note
11note Words are ulong (64-bit unsigned, wrapping add is the mod 2^64 addition of FIPS 180-4 section 3.2).
12note Omega has no rotate word, so ROTR^r(x) = (x >> r) | (x << (64 - r)) (FIPS 180-4 section 3.2 item 4).
13note Not constant-time audited.
14
15note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes
16note (FIPS 180-4 section 4.2.3)
17fn s512k k edit ulong 80 to void
18put k 0 4794697086780616226
19put k 1 8158064640168781261
20put k 2 13096744586834688815
21put k 3 16840607885511220156
22put k 4 4131703408338449720
23put k 5 6480981068601479193
24put k 6 10538285296894168987
25put k 7 12329834152419229976
26put k 8 15566598209576043074
27put k 9 1334009975649890238
28put k 10 2608012711638119052
29put k 11 6128411473006802146
30put k 12 8268148722764581231
31put k 13 9286055187155687089
32put k 14 11230858885718282805
33put k 15 13951009754708518548
34put k 16 16472876342353939154
35put k 17 17275323862435702243
36put k 18 1135362057144423861
37put k 19 2597628984639134821
38put k 20 3308224258029322869
39put k 21 5365058923640841347
40put k 22 6679025012923562964
41put k 23 8573033837759648693
42put k 24 10970295158949994411
43put k 25 12119686244451234320
44put k 26 12683024718118986047
45put k 27 13788192230050041572
46put k 28 14330467153632333762
47put k 29 15395433587784984357
48put k 30 489312712824947311
49put k 31 1452737877330783856
50put k 32 2861767655752347644
51put k 33 3322285676063803686
52put k 34 5560940570517711597
53put k 35 5996557281743188959
54put k 36 7280758554555802590
55put k 37 8532644243296465576
56put k 38 9350256976987008742
57put k 39 10552545826968843579
58put k 40 11727347734174303076
59put k 41 12113106623233404929
60put k 42 14000437183269869457
61put k 43 14369950271660146224
62put k 44 15101387698204529176
63put k 45 15463397548674623760
64put k 46 17586052441742319658
65put k 47 1182934255886127544
66put k 48 1847814050463011016
67put k 49 2177327727835720531
68put k 50 2830643537854262169
69put k 51 3796741975233480872
70put k 52 4115178125766777443
71put k 53 5681478168544905931
72put k 54 6601373596472566643
73put k 55 7507060721942968483
74put k 56 8399075790359081724
75put k 57 8693463985226723168
76put k 58 9568029438360202098
77put k 59 10144078919501101548
78put k 60 10430055236837252648
79put k 61 11840083180663258601
80put k 62 13761210420658862357
81put k 63 14299343276471374635
82put k 64 14566680578165727644
83put k 65 15097957966210449927
84put k 66 16922976911328602910
85put k 67 17689382322260857208
86put k 68 500013540394364858
87put k 69 748580250866718886
88put k 70 1242879168328830382
89put k 71 1977374033974150939
90put k 72 2944078676154940804
91put k 73 3659926193048069267
92put k 74 4368137639120453308
93put k 75 4836135668995329356
94put k 76 5532061633213252278
95put k 77 6448918945643986474
96put k 78 6902733635092675308
97put k 79 7801388544844847127
98end
99
100note H(0) for SHA-512: first 64 bits of the fractional parts of the square roots of the first 8 primes
101note (FIPS 180-4 section 5.3.5)
102fn s512iv h edit ulong 8 to void
103put h 0 7640891576956012808
104put h 1 13503953896175478587
105put h 2 4354685564936845355
106put h 3 11912009170470909681
107put h 4 5840696475078001361
108put h 5 11170449401992604703
109put h 6 2270897969802886507
110put h 7 6620516959819538809
111end
112
113note upper-case Sigma0{512}(x) = ROTR^28 ^ ROTR^34 ^ ROTR^39 (FIPS 180-4 section 4.1.3, (4.10))
114fn s512bs0 x ulong to ulong
115r = or shr x 28 shl x 36
116r = xor r or shr x 34 shl x 30
117r = xor r or shr x 39 shl x 25
118ret r
119end
120
121note upper-case Sigma1{512}(x) = ROTR^14 ^ ROTR^18 ^ ROTR^41 (section 4.1.3, (4.11))
122fn s512bs1 x ulong to ulong
123r = or shr x 14 shl x 50
124r = xor r or shr x 18 shl x 46
125r = xor r or shr x 41 shl x 23
126ret r
127end
128
129note lower-case sigma0{512}(x) = ROTR^1 ^ ROTR^8 ^ SHR^7 (section 4.1.3, (4.12))
130fn s512ss0 x ulong to ulong
131r = or shr x 1 shl x 63
132r = xor r or shr x 8 shl x 56
133r = xor r shr x 7
134ret r
135end
136
137note lower-case sigma1{512}(x) = ROTR^19 ^ ROTR^61 ^ SHR^6 (section 4.1.3, (4.13))
138fn s512ss1 x ulong to ulong
139r = or shr x 19 shl x 45
140r = xor r or shr x 61 shl x 3
141r = xor r shr x 6
142ret r
143end
144
145note Ch(x,y,z) = (x and y) xor (not x and z), written as z xor (x and (y xor z)) (section 4.1.3, (4.8))
146fn s512ch x ulong y ulong z ulong to ulong
147ret xor z and x xor y z
148end
149
150note Maj(x,y,z) = (x and y) xor (x and z) xor (y and z), written as (x and y) or (z and (x or y))
151note (section 4.1.3, (4.9))
152fn s512mj x ulong y ulong z ulong to ulong
153ret or and x y and z or x y
154end
155
156note big-endian 64-bit word of the 8 bytes at m[q..q+8) (section 3.1 item 3)
157fn s512bem m view uchar 65536 q ulong to ulong
158v = ulong 0
159for j ulong 8
160v = or shl v 8 ulong get m add q j
161end
162ret v
163end
164
165note the same for the 256-byte padding buffer
166fn s512bep p view uchar 256 q ulong to ulong
167v = ulong 0
168for j ulong 8
169v = or shl v 8 ulong get p add q j
170end
171ret v
172end
173
174note the same for the 64-byte prefix
175fn s512bex p view uchar 64 q ulong to ulong
176v = ulong 0
177for j ulong 8
178v = or shl v 8 ulong get p add q j
179end
180ret v
181end
182
183note message block M(i) at byte o of the stream p[0..pl) || m into w[0..16) (section 6.4.2 step 1,
184note t = 0..15). pl is 0 or 64 and o is a multiple of 128, so every word lies wholly in p or in m.
185fn s512ldm p view uchar 64 pl ulong m view uchar 65536 o ulong w edit ulong 80 to void
186for i ulong 16
187q = add o mul i 8
188if lt q pl
189put w i s512bex p q
190else
191put w i s512bem m sub q pl
192end
193end
194end
195
196note the block at byte o of the padding buffer pb into w[0..16)
197fn s512ldp pb view uchar 256 o ulong w edit ulong 80 to void
198for i ulong 16
199put w i s512bep pb add o mul i 8
200end
201end
202
203note one SHA-512 compression (section 6.4.2 steps 1-4); w[0..16) holds the block on entry
204fn s512blk h edit ulong 8 w edit ulong 80 k view ulong 80 to void
205for i ulong 64
206t = add i 16
207v = s512ss1 get w sub t 2
208v = add v get w sub t 7
209v = add v s512ss0 get w sub t 15
210v = add v get w i
211put w t v
212end
213a = get h 0
214b = get h 1
215c = get h 2
216d = get h 3
217e = get h 4
218f = get h 5
219g = get h 6
220x = get h 7
221for i ulong 80
222t = add x s512bs1 e
223t = add t s512ch e f g
224t = add t get k i
225t = add t get w i
226u = add s512bs0 a s512mj a b c
227x = g
228g = f
229f = e
230e = add d t
231d = c
232c = b
233b = a
234a = add t u
235end
236put h 0 add get h 0 a
237put h 1 add get h 1 b
238put h 2 add get h 2 c
239put h 3 add get h 3 d
240put h 4 add get h 4 e
241put h 5 add get h 5 f
242put h 6 add get h 6 g
243put h 7 add get h 7 x
244end
245
246note Padding (section 5.1.2) of the stream p[0..pl) || m, tot bytes long, whose bytes from off on
247note (fewer than 128) are not yet compressed: they go to pb[0..r), then one 1 bit (byte 128), zero bytes
248note up to 112 mod 128, then l = 8 * tot as a 128-bit big-endian integer. pb must be all zero on entry.
249note Returns the padded length, 128 when r < 112, else 256. The upper 64 bits of l are always 0 here
250note because tot <= 65600.
251fn s512pad p view uchar 64 pl ulong m view uchar 65536 off ulong tot ulong pb edit uchar 256 to ulong
252r = sub tot off
253for i ulong r
254q = add off i
255if lt q pl
256put pb i get p q
257else
258put pb i get m sub q pl
259end
260end
261put pb r 128
262e = ulong 128
263if ge r 112
264e = 256
265end
266s512len pb e mul tot 8
267ret e
268end
269
270note the low 64 bits of the 128-bit big-endian length field ending at pb[e-1]
271fn s512len pb edit uchar 256 e ulong bits ulong to void
272for i ulong 8
273y = sub sub e 1 i
274put pb y uchar and shr bits mul i 8 255
275end
276end
277
278note H(N) as the 64-byte big-endian digest (section 6.4.2, final hash value)
279fn s512out h view ulong 8 o edit uchar 64 to void
280for i ulong 8
281v = get h i
282for j ulong 8
283put o add mul i 8 j uchar and shr v mul sub 7 j 8 255
284end
285end
286end
287
288note SHA-512 of the stream p[0..pl) || m[0..n), pl 0 or 64 (else trap 3), n <= 65536 (else trap 3),
289note digest to o[0..64).
290fn s512core p view uchar 64 pl ulong m view uchar 65536 n ulong o edit uchar 64 to void
291if gt n 65536
292trap 3
293end
294if ne pl 0
295if ne pl 64
296trap 3
297end
298end
299h = new ulong 8 0
300w = new ulong 80 0
301k = new ulong 80 0
302pb = new uchar 256 0
303s512k k
304s512iv h
305tot = add pl n
306off = ulong 0
307while le add off 128 tot bound 514
308s512ldm p pl m off w
309s512blk h w k
310off = add off 128
311end
312e = s512pad p pl m off tot pb
313s512ldp pb 0 w
314s512blk h w k
315if eq e 256
316s512ldp pb 128 w
317s512blk h w k
318end
319s512out h o
320end
321
322note SHA-512(m[0..n)), n <= 65536 (else trap 3), digest to o[0..64)
323fn s512hash m view uchar 65536 n ulong o edit uchar 64 to void
324z = new uchar 64 0
325s512core z 0 m n o
326end
327
328note SHA-512(p[0..64) || m[0..n)), n <= 65536 (else trap 3), digest to o[0..64); for Ed25519's
329note SHA-512(R || A || M) without copying M (RFC 8032 section 5.1.7 step 2)
330fn s512pre p view uchar 64 m view uchar 65536 n ulong o edit uchar 64 to void
331s512core p 64 m n o
332end