interplane.Get Omega
Read in your language

omtls

omtls: a TLS 1.3 client (RFC 8446) written fresh in seeded Omega, for mailhand. Not audited by a cryptographer. Free (Apache-2.0).

Brought by drakestapleton

branch main Branches Tags History

omtls / src / hmac.om

130 lines · 2700 bytes Raw Blame History
1note omtls HMAC (RFC 2104) over SHA-256 (alg 0, vendored), SHA-384 (1), SHA-512 (2).
2note Internal helpers keep two contexts (c5 for SHA-384/512, c2 for SHA-256) and dispatch on the public alg.
3fn ot_hm_bs alg ulong to ulong
4if eq alg 0
5ret 64
6end
7if or eq alg 1 eq alg 2
8ret 128
9end
10ret 0
11end
12fn ot_hm_hl alg ulong to ulong
13if eq alg 0
14ret 32
15end
16if eq alg 1
17ret 48
18end
19if eq alg 2
20ret 64
21end
22ret 0
23end
24fn ot_hm_hi alg ulong c5 edit ulong 32 c2 edit uchar 104 to void
25if eq alg 0
26sha256_init c2
27else
28ot_s5_init c5 eq alg 1
29end
30end
31fn ot_hm_hu alg ulong c5 edit ulong 32 c2 edit uchar 104 d view uchar * to void
32if eq alg 0
33sha256_update c2 d
34else
35ot_s5_update c5 d
36end
37end
38fn ot_hm_hf alg ulong c5 edit ulong 32 c2 edit uchar 104 o edit uchar 64 to void
39t = new uchar 32 0
40if eq alg 0
41sha256_final c2 t
42for i ulong 32 unroll
43put o i get t i
44put t i 0
45put o add 32 i 0
46end
47else
48ot_s5_final c5 o
49end
50end
51fn ot_hm_zc c5 edit ulong 32 c2 edit uchar 104 to void
52for i ulong 32 unroll
53put c5 i 0
54end
55for i ulong 104
56put c2 i 0
57end
58end
59note The key block: the key, or its hash when longer than the block size, zero-padded to 128 bytes.
60fn ot_hm_kb alg ulong key view uchar * kb edit uchar 128 to void
61for i ulong 128
62put kb i 0
63end
64n = len key
65t = new uchar 64 0
66if gt n ot_hm_bs alg
67hl = ot_hash alg key t
68for i ulong 64 unroll
69put kb i get t i
70put t i 0
71end
72ret
73end
74for i ulong n
75put kb i get key i
76end
77end
78note Start the inner hash: H(K xor ipad || ...).
79fn ot_hm_beg alg ulong kb view uchar 128 c5 edit ulong 32 c2 edit uchar 104 to void
80pad = new uchar 128 0
81for i ulong 128
82put pad i xor get kb i 54
83end
84ot_hm_hi alg c5 c2
85ot_hm_hu alg c5 c2 view part pad 0 ot_hm_bs alg
86for i ulong 128
87put pad i 0
88end
89end
90note Finish: inner digest, then H(K xor opad || inner) into o (bytes past the digest length zero).
91fn ot_hm_end alg ulong kb view uchar 128 c5 edit ulong 32 c2 edit uchar 104 o edit uchar 64 to void
92pad = new uchar 128 0
93ib = new uchar 64 0
94ot_hm_hf alg c5 c2 ib
95for i ulong 128
96put pad i xor get kb i 92
97end
98ot_hm_hi alg c5 c2
99ot_hm_hu alg c5 c2 view part pad 0 ot_hm_bs alg
100ot_hm_hu alg c5 c2 view part ib 0 ot_hm_hl alg
101ot_hm_hf alg c5 c2 o
102for i ulong 128
103put pad i 0
104end
105for i ulong 64 unroll
106put ib i 0
107end
108ot_hm_zc c5 c2
109end
110note HMAC(key, msg) into o; gives the tag length (32/48/64), or 0 with o all zero for an unknown alg.
111export fn ot_hm_mac alg ulong key view uchar * msg view uchar * o edit uchar 64 to ulong
112hl = ot_hm_hl alg
113kb = new uchar 128 0
114c5 = new ulong 32 0
115c2 = new uchar 104 0
116for i ulong 64 unroll
117put o i 0
118end
119if eq hl 0
120ret 0
121end
122ot_hm_kb alg key kb
123ot_hm_beg alg kb c5 c2
124ot_hm_hu alg c5 c2 msg
125ot_hm_end alg kb c5 c2 o
126for i ulong 128
127put kb i 0
128end
129ret hl
130end