interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / apps / interplane-site / native / lib / sha2.om

592 lines · 14347 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/sha2.om: SHA-256, SHA-384, SHA-512 (FIPS 180-4) streaming, as an OM-2 library unit
3note (no om2 line, no main, no globals). Module code SH; functions sha2_*, sha256_*, sha384_*, sha512_*.
4note
5note API
6note sha256_init c edit ulong * c: 40 ulong cells (caller owns all state)
7note sha256_update c edit ulong * m view uchar * any length, any chunking
8note sha256_final c edit ulong * o edit uchar * writes 32 bytes (o shorter traps 3)
9note sha384_* / sha512_* the same; final writes 48 / 64 bytes
10note sha2_copy d edit ulong * s view ulong * copies len s cells (a context snapshot)
11note sha2_digest alg ulong m view uchar * o edit uchar * to ulong
12note alg 256|384|512: one-shot digest into o, returns 32|48|64; any other alg returns 0, writes nothing
13note After final the context is spent (its state cells hold the last chaining value); init it again.
14note A context initialised for one algorithm must be used with that algorithm's update/final; the
15note common code reads the algorithm from cell 9, so sha256_update on a sha512 context hashes SHA-512.
16note
17note Context cells: 0..7 chaining value H (32-bit words zero-extended for SHA-256), 8 = bytes absorbed
18note mod 2^64, 9 = algorithm (256, 384, 512), 16..31 = pending block bytes packed big-endian (byte i
19note in cell 16 + i/8, bits 56 - 8*(i mod 8)), 10..15 and 32..39 = 0.
20note Constant time: every branch, loop count and index depends only on lengths and the algorithm,
21note never on message or state bytes. Local schedule/state/padding arrays are zeroed before return
22note (HMAC keys pass through them). omc emits plain AArch64; MUL/ADD/EOR/ROR/shift data-independent.
23note Message length: any length below 2^64 bytes; update cuts its input into runs of 2^25 bytes so no
24note loop passes 2^20 times (a rolled for traps 4 after 10^6 passes; the while bound 999999 of runs
25note allows inputs up to 999999 * 32 MiB per update call).
26
27note K{256}[0..64): first 32 bits of the fractional parts of the cube roots of the first 64 primes
28note (FIPS 180-4 4.2.2). Same values as om/sha256/sha256-om2.om; recomputed with Wolfram:
29note IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^32],16,8] for i = 1..64.
30fn sha2_k256 k edit uint 64 to void
31put k 0 0x428a2f98
32put k 1 0x71374491
33put k 2 0xb5c0fbcf
34put k 3 0xe9b5dba5
35put k 4 0x3956c25b
36put k 5 0x59f111f1
37put k 6 0x923f82a4
38put k 7 0xab1c5ed5
39put k 8 0xd807aa98
40put k 9 0x12835b01
41put k 10 0x243185be
42put k 11 0x550c7dc3
43put k 12 0x72be5d74
44put k 13 0x80deb1fe
45put k 14 0x9bdc06a7
46put k 15 0xc19bf174
47put k 16 0xe49b69c1
48put k 17 0xefbe4786
49put k 18 0x0fc19dc6
50put k 19 0x240ca1cc
51put k 20 0x2de92c6f
52put k 21 0x4a7484aa
53put k 22 0x5cb0a9dc
54put k 23 0x76f988da
55put k 24 0x983e5152
56put k 25 0xa831c66d
57put k 26 0xb00327c8
58put k 27 0xbf597fc7
59put k 28 0xc6e00bf3
60put k 29 0xd5a79147
61put k 30 0x06ca6351
62put k 31 0x14292967
63put k 32 0x27b70a85
64put k 33 0x2e1b2138
65put k 34 0x4d2c6dfc
66put k 35 0x53380d13
67put k 36 0x650a7354
68put k 37 0x766a0abb
69put k 38 0x81c2c92e
70put k 39 0x92722c85
71put k 40 0xa2bfe8a1
72put k 41 0xa81a664b
73put k 42 0xc24b8b70
74put k 43 0xc76c51a3
75put k 44 0xd192e819
76put k 45 0xd6990624
77put k 46 0xf40e3585
78put k 47 0x106aa070
79put k 48 0x19a4c116
80put k 49 0x1e376c08
81put k 50 0x2748774c
82put k 51 0x34b0bcb5
83put k 52 0x391c0cb3
84put k 53 0x4ed8aa4a
85put k 54 0x5b9cca4f
86put k 55 0x682e6ff3
87put k 56 0x748f82ee
88put k 57 0x78a5636f
89put k 58 0x84c87814
90put k 59 0x8cc70208
91put k 60 0x90befffa
92put k 61 0xa4506ceb
93put k 62 0xbef9a3f7
94put k 63 0xc67178f2
95end
96
97note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes
98note (FIPS 180-4 4.2.3). Wolfram: IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^64],16,16];
99note equal to the decimal table of omnet om/sha512/sha512.om.
100fn sha2_k512 k edit ulong 80 to void
101put k 0 0x428a2f98d728ae22
102put k 1 0x7137449123ef65cd
103put k 2 0xb5c0fbcfec4d3b2f
104put k 3 0xe9b5dba58189dbbc
105put k 4 0x3956c25bf348b538
106put k 5 0x59f111f1b605d019
107put k 6 0x923f82a4af194f9b
108put k 7 0xab1c5ed5da6d8118
109put k 8 0xd807aa98a3030242
110put k 9 0x12835b0145706fbe
111put k 10 0x243185be4ee4b28c
112put k 11 0x550c7dc3d5ffb4e2
113put k 12 0x72be5d74f27b896f
114put k 13 0x80deb1fe3b1696b1
115put k 14 0x9bdc06a725c71235
116put k 15 0xc19bf174cf692694
117put k 16 0xe49b69c19ef14ad2
118put k 17 0xefbe4786384f25e3
119put k 18 0x0fc19dc68b8cd5b5
120put k 19 0x240ca1cc77ac9c65
121put k 20 0x2de92c6f592b0275
122put k 21 0x4a7484aa6ea6e483
123put k 22 0x5cb0a9dcbd41fbd4
124put k 23 0x76f988da831153b5
125put k 24 0x983e5152ee66dfab
126put k 25 0xa831c66d2db43210
127put k 26 0xb00327c898fb213f
128put k 27 0xbf597fc7beef0ee4
129put k 28 0xc6e00bf33da88fc2
130put k 29 0xd5a79147930aa725
131put k 30 0x06ca6351e003826f
132put k 31 0x142929670a0e6e70
133put k 32 0x27b70a8546d22ffc
134put k 33 0x2e1b21385c26c926
135put k 34 0x4d2c6dfc5ac42aed
136put k 35 0x53380d139d95b3df
137put k 36 0x650a73548baf63de
138put k 37 0x766a0abb3c77b2a8
139put k 38 0x81c2c92e47edaee6
140put k 39 0x92722c851482353b
141put k 40 0xa2bfe8a14cf10364
142put k 41 0xa81a664bbc423001
143put k 42 0xc24b8b70d0f89791
144put k 43 0xc76c51a30654be30
145put k 44 0xd192e819d6ef5218
146put k 45 0xd69906245565a910
147put k 46 0xf40e35855771202a
148put k 47 0x106aa07032bbd1b8
149put k 48 0x19a4c116b8d2d0c8
150put k 49 0x1e376c085141ab53
151put k 50 0x2748774cdf8eeb99
152put k 51 0x34b0bcb5e19b48a8
153put k 52 0x391c0cb3c5c95a63
154put k 53 0x4ed8aa4ae3418acb
155put k 54 0x5b9cca4f7763e373
156put k 55 0x682e6ff3d6b2b8a3
157put k 56 0x748f82ee5defb2fc
158put k 57 0x78a5636f43172f60
159put k 58 0x84c87814a1f0ab72
160put k 59 0x8cc702081a6439ec
161put k 60 0x90befffa23631e28
162put k 61 0xa4506cebde82bde9
163put k 62 0xbef9a3f7b2c67915
164put k 63 0xc67178f2e372532b
165put k 64 0xca273eceea26619c
166put k 65 0xd186b8c721c0c207
167put k 66 0xeada7dd6cde0eb1e
168put k 67 0xf57d4f7fee6ed178
169put k 68 0x06f067aa72176fba
170put k 69 0x0a637dc5a2c898a6
171put k 70 0x113f9804bef90dae
172put k 71 0x1b710b35131c471b
173put k 72 0x28db77f523047d84
174put k 73 0x32caab7b40c72493
175put k 74 0x3c9ebe0a15c9bebc
176put k 75 0x431d67c49c100d4c
177put k 76 0x4cc5d4becb3e42b6
178put k 77 0x597f299cfc657e2a
179put k 78 0x5fcb6fab3ad6faec
180put k 79 0x6c44198c4a475817
181end
182
183note H(0) (FIPS 180-4 5.3.3 SHA-256, 5.3.4 SHA-384, 5.3.5 SHA-512). Wolfram: fractional parts of
184note sqrt(Prime[i]) * 2^32 (i 1..8), * 2^64 (i 1..8 for SHA-512, i 9..16 for SHA-384).
185note Any other alg traps 9 (caller contract).
186fn sha2_init c edit ulong * alg ulong to void
187for i ulong 40
188put c i 0
189end
190put c 9 alg
191if eq alg 256
192put c 0 0x6a09e667
193put c 1 0xbb67ae85
194put c 2 0x3c6ef372
195put c 3 0xa54ff53a
196put c 4 0x510e527f
197put c 5 0x9b05688c
198put c 6 0x1f83d9ab
199put c 7 0x5be0cd19
200ret
201end
202if eq alg 384
203put c 0 0xcbbb9d5dc1059ed8
204put c 1 0x629a292a367cd507
205put c 2 0x9159015a3070dd17
206put c 3 0x152fecd8f70e5939
207put c 4 0x67332667ffc00b31
208put c 5 0x8eb44a8768581511
209put c 6 0xdb0c2e0d64f98fa7
210put c 7 0x47b5481dbefa4fa4
211ret
212end
213if eq alg 512
214put c 0 0x6a09e667f3bcc908
215put c 1 0xbb67ae8584caa73b
216put c 2 0x3c6ef372fe94f82b
217put c 3 0xa54ff53a5f1d36f1
218put c 4 0x510e527fade682d1
219put c 5 0x9b05688c2b3e6c1f
220put c 6 0x1f83d9abfb41bd6b
221put c 7 0x5be0cd19137e2179
222ret
223end
224trap 9
225end
226
227note SHA-256 message schedule (FIPS 180-4 6.2.2 step 1) of the block m[o .. o+64).
228fn sha2_s256 w edit uint 64 m view uchar * o ulong to void
229q = ulong 0
230x = uint 0
231y = uint 0
232for i ulong 16 unroll
233q = add o mul i 4
234x = uint get m q
235x = or shl x 8 uint get m add q 1
236x = or shl x 8 uint get m add q 2
237put w i or shl x 8 uint get m add q 3
238end
239for i ulong 48 unroll
240x = get w add i 1
241y = get w add i 14
242x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3
243put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10
244end
245end
246
247note SHA-256 rounds (6.2.2 steps 2-4). Ch(e,f,g) = g xor (e and (f xor g)); Maj(a,b,c) =
248note (a and b) or (c and (a or b)); Sigma1 = ROTR 6,11,25; Sigma0 = ROTR 2,13,22 (4.1.2).
249fn sha2_r256 s edit uint 8 w view uint 64 kt view uint 64 to void
250a = get s 0
251b = get s 1
252c = get s 2
253d = get s 3
254e = get s 4
255f = get s 5
256g = get s 6
257h = get s 7
258t = uint 0
259for i ulong 64 unroll
260h = add h get kt i
261h = add h get w i
262t = xor rotr e 6 rotr e 11
263t = xor t rotr e 25
264h = add h t
265h = add h xor g and e xor f g
266d = add d h
267t = xor rotr a 2 rotr a 13
268t = xor t rotr a 22
269h = add h t
270t = and c or a b
271h = add h or and a b t
272t = h
273h = g
274g = f
275f = e
276e = d
277d = c
278c = b
279b = a
280a = t
281end
282put s 0 add get s 0 a
283put s 1 add get s 1 b
284put s 2 add get s 2 c
285put s 3 add get s 3 d
286put s 4 add get s 4 e
287put s 5 add get s 5 f
288put s 6 add get s 6 g
289put s 7 add get s 7 h
290end
291
292note the whole 64-byte blocks of m (len m a multiple of 64, at most 2^25) into c's chaining value
293fn sha2_b256 c edit ulong * m view uchar * to void
294s = new uint 8 0
295w = new uint 64 0
296kt = new uint 64 0
297sha2_k256 kt
298for i ulong 8 unroll
299put s i uint get c i
300end
301for j ulong shr len m 6
302sha2_s256 w m shl j 6
303sha2_r256 s w kt
304end
305for i ulong 8 unroll
306put c i ulong get s i
307put s i 0
308end
309for i ulong 64
310put w i 0
311end
312end
313
314note SHA-512 message schedule (6.4.2 step 1) of the block m[o .. o+128); sigma0 = ROTR 1,8 SHR 7,
315note sigma1 = ROTR 19,61 SHR 6 (4.1.3).
316fn sha2_s512 w edit ulong 80 m view uchar * o ulong to void
317q = ulong 0
318x = ulong 0
319y = ulong 0
320for i ulong 16 unroll
321q = add o mul i 8
322x = ulong get m q
323x = or shl x 8 ulong get m add q 1
324x = or shl x 8 ulong get m add q 2
325x = or shl x 8 ulong get m add q 3
326x = or shl x 8 ulong get m add q 4
327x = or shl x 8 ulong get m add q 5
328x = or shl x 8 ulong get m add q 6
329put w i or shl x 8 ulong get m add q 7
330end
331for i ulong 64 unroll
332x = get w add i 1
333y = get w add i 14
334x = add add get w i get w add i 9 xor xor rotr x 1 rotr x 8 shr x 7
335put w add i 16 add x xor xor rotr y 19 rotr y 61 shr y 6
336end
337end
338
339note SHA-512 rounds (6.4.2 steps 2-4), a rolled loop (unroll allows at most 64 copies); Sigma1 = ROTR 14,18,41; Sigma0 = ROTR 28,34,39 (4.1.3).
340fn sha2_r512 s edit ulong 8 w view ulong 80 kt view ulong 80 to void
341a = get s 0
342b = get s 1
343c = get s 2
344d = get s 3
345e = get s 4
346f = get s 5
347g = get s 6
348h = get s 7
349t = ulong 0
350for i ulong 80
351h = add h get kt i
352h = add h get w i
353t = xor rotr e 14 rotr e 18
354t = xor t rotr e 41
355h = add h t
356h = add h xor g and e xor f g
357d = add d h
358t = xor rotr a 28 rotr a 34
359t = xor t rotr a 39
360h = add h t
361t = and c or a b
362h = add h or and a b t
363t = h
364h = g
365g = f
366f = e
367e = d
368d = c
369c = b
370b = a
371a = t
372end
373put s 0 add get s 0 a
374put s 1 add get s 1 b
375put s 2 add get s 2 c
376put s 3 add get s 3 d
377put s 4 add get s 4 e
378put s 5 add get s 5 f
379put s 6 add get s 6 g
380put s 7 add get s 7 h
381end
382
383note the whole 128-byte blocks of m (len m a multiple of 128, at most 2^25) into c
384fn sha2_b512 c edit ulong * m view uchar * to void
385s = new ulong 8 0
386w = new ulong 80 0
387kt = new ulong 80 0
388sha2_k512 kt
389for i ulong 8 unroll
390put s i get c i
391end
392for j ulong shr len m 7
393sha2_s512 w m shl j 7
394sha2_r512 s w kt
395end
396for i ulong 8 unroll
397put c i get s i
398put s i 0
399end
400for i ulong 80
401put w i 0
402end
403end
404
405note whole blocks of m (len m a multiple of the block size) in runs of at most 2^25 bytes
406fn sha2_run c edit ulong * m view uchar * to void
407n = len m
408q = ulong 0
409k = ulong 0
410while lt q n bound 999999
411k = sub n q
412if gt k 0x2000000
413k = 0x2000000
414end
415if eq get c 9 256
416sha2_b256 c part m q k
417else
418sha2_b512 c part m q k
419end
420q = add q k
421end
422end
423
424note pending byte i (i < 128) of the packed buffer
425fn sha2_bget c view ulong * i ulong to uchar
426x = shr get c add 16 shr i 3 sub 56 shl and i 7 3
427ret uchar and x 0xff
428end
429
430note set pending byte i of the packed buffer to v
431fn sha2_bput c edit ulong * i ulong v uchar to void
432q = add 16 shr i 3
433sh = sub 56 shl and i 7 3
434x = and get c q not shl ulong 0xff sh
435put c q or x shl ulong v sh
436end
437
438note the bytes m[0..k) into the buffer at bi (bi + k <= block size)
439fn sha2_fill c edit ulong * bi ulong m view uchar * k ulong to void
440for i ulong k
441sha2_bput c add bi i get m i
442end
443end
444
445fn sha2_update c edit ulong * m view uchar * to void
446blk = new uchar 128 0
447n = len m
448cnt = get c 8
449bs = ulong 128
450if eq get c 9 256
451bs = 64
452end
453bi = and cnt sub bs 1
454put c 8 add cnt n
455off = ulong 0
456if gt bi 0
457off = sub bs bi
458if lt n off
459off = n
460end
461sha2_fill c bi m off
462if eq add bi off bs
463for i ulong bs
464put blk i sha2_bget c i
465end
466sha2_run c part blk 0 bs
467for i ulong bs
468put blk i 0
469end
470end
471end
472nb = and sub n off not sub bs 1
473sha2_run c part m off nb
474off = add off nb
475sha2_fill c 0 part m off sub n off sub n off
476end
477
478note H(N) as big-endian bytes: 32 (SHA-256, 8 words of 32 bits), 48 (SHA-384, first 6 words), 64
479fn sha2_put c view ulong * o edit uchar * to ulong
480alg = get c 9
481sh = ulong 0
482if eq alg 256
483for i ulong 32
484sh = sub 24 shl and i 3 3
485put o i uchar and shr get c shr i 2 sh 0xff
486end
487ret 32
488end
489dl = ulong 64
490if eq alg 384
491dl = 48
492end
493for i ulong dl
494sh = sub 56 shl and i 7 3
495put o i uchar and shr get c shr i 3 sh 0xff
496end
497ret dl
498end
499
500note padding (FIPS 180-4 5.1.1 / 5.1.2): 0x80, zeros, the bit length big-endian in the last 8 (SHA-256)
501note or 16 (SHA-384/512) bytes of one or two blocks; returns the digest length
502fn sha2_fin c edit ulong * o edit uchar * to ulong
503p = new uchar 256 0
504bs = ulong 128
505ls = ulong 16
506if eq get c 9 256
507bs = 64
508ls = 8
509end
510cnt = get c 8
511k = and cnt sub bs 1
512for i ulong k
513put p i sha2_bget c i
514end
515put p k 0x80
516e = bs
517if ge add k ls bs
518e = shl bs 1
519end
520x = shl cnt 3
521for i ulong 8 unroll
522put p sub e add i 1 uchar and shr x mul i 8 0xff
523end
524if eq ls 16
525put p sub e 9 uchar shr cnt 61
526end
527sha2_run c part p 0 e
528for i ulong 256
529put p i 0
530end
531ret sha2_put c o
532end
533
534fn sha256_init c edit ulong * to void
535sha2_init c 256
536end
537
538fn sha384_init c edit ulong * to void
539sha2_init c 384
540end
541
542fn sha512_init c edit ulong * to void
543sha2_init c 512
544end
545
546fn sha256_update c edit ulong * m view uchar * to void
547sha2_update c m
548end
549
550fn sha384_update c edit ulong * m view uchar * to void
551sha2_update c m
552end
553
554fn sha512_update c edit ulong * m view uchar * to void
555sha2_update c m
556end
557
558fn sha256_final c edit ulong * o edit uchar * to void
559dl = sha2_fin c o
560end
561
562fn sha384_final c edit ulong * o edit uchar * to void
563dl = sha2_fin c o
564end
565
566fn sha512_final c edit ulong * o edit uchar * to void
567dl = sha2_fin c o
568end
569
570fn sha2_copy d edit ulong * s view ulong * to void
571for i ulong len s
572put d i get s i
573end
574end
575
576fn sha2_digest alg ulong m view uchar * o edit uchar * to ulong
577c = new ulong 40 0
578if ne alg 256
579if ne alg 384
580if ne alg 512
581ret 0
582end
583end
584end
585sha2_init c alg
586sha2_update c m
587dl = sha2_fin c o
588for i ulong 40
589put c i 0
590end
591ret dl
592end