interplane.Get Omega
Read in your language

aienos.com

Source of www.aienos.com, served from my own hardware

Brought by drakestapleton

branch main Branches Tags History

aienos.com / om / agora / tls / hkdf.om

170 lines · 4896 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/tls/hkdf.om: HMAC-SHA256/384/512 (RFC 2104, FIPS 198-1) streaming and HKDF (RFC 5869) as an
3note OM-2 library unit (no om2 line, no main, no globals). Needs om/tls/sha2.om in the same unit.
4note Functions hmac_*, hkdf_*.
5note
6note API
7note hmac_init alg ulong c edit ulong * key view uchar * alg 256|384|512; c: 96 ulong cells
8note a key longer than the block size (64 / 128 bytes) is hashed first (RFC 2104 section 2).
9note Any other alg traps 9 (caller contract).
10note hmac_update c edit ulong * m view uchar * any length, any chunking
11note hmac_final c edit ulong * o edit uchar * to ulong writes the full tag (32|48|64 bytes,
12note o shorter traps 3), returns its length. The context is spent afterwards; truncated tags
13note compare a prefix (the caller's constant-time comparison).
14note hkdf_extract alg ulong si view uchar * slen ulong prk edit uchar * to ulong
15note si = salt || ikm, salt = first slen bytes (slen 0 = no salt = HashLen zero bytes, RFC 5869 2.2,
16note which HMAC's zero padding makes identical). Writes PRK, returns its length (32|48|64);
17note returns 0 and writes nothing for an unsupported alg or slen > len si.
18note hkdf_expand alg ulong pi view uchar * plen ulong okm edit uchar * to ulong
19note pi = prk || info, prk = first plen bytes; fills all len okm bytes. Returns 0 ok,
20note 1 unsupported alg, 2 plen > len pi, 3 len okm > 255 * HashLen (RFC 5869 2.3); okm untouched
21note on a nonzero return.
22note Context cells: 0..39 inner SHA-2 context (key xor ipad absorbed), 40..79 outer SHA-2 context
23note (key xor opad absorbed), 80 = alg, 81..95 = 0.
24note Constant time: no branch, loop count or index depends on key, message or PRK bytes; only on
25note their lengths and alg (lengths are treated as public). Key-derived local arrays are zeroed
26note before return. The SHA-2 layer has the same property (see sha2.om).
27
28note the inner and outer contexts of HMAC under key (RFC 2104: K0 = key, or H(key) when longer than
29note the block, zero-padded to the block; inner absorbs K0 xor 0x36.., outer K0 xor 0x5c..)
30fn hmac_init alg ulong c edit ulong * key view uchar * to void
31kb = new uchar 128 0
32pad = new uchar 128 0
33bs = ulong 128
34if eq alg 256
35bs = 64
36end
37dl = ulong 0
38if gt len key bs
39dl = sha2_digest alg key kb
40else
41for i ulong len key
42put kb i get key i
43end
44end
45for i ulong bs
46put pad i xor get kb i 0x36
47end
48sha2_init part c 0 40 alg
49sha2_update part c 0 40 part pad 0 bs
50for i ulong bs
51put pad i xor get kb i 0x5c
52end
53sha2_init part c 40 40 alg
54sha2_update part c 40 40 part pad 0 bs
55for i ulong 15
56put c add 81 i 0
57end
58put c 80 alg
59for i ulong 128
60put kb i 0
61put pad i 0
62end
63end
64
65fn hmac_update c edit ulong * m view uchar * to void
66sha2_update part c 0 40 m
67end
68
69note tag = H(K0 xor opad || H(K0 xor ipad || m))
70fn hmac_final c edit ulong * o edit uchar * to ulong
71dg = new uchar 64 0
72dl = sha2_fin part c 0 40 dg
73sha2_update part c 40 40 part dg 0 dl
74dl = sha2_fin part c 40 40 o
75for i ulong 64
76put dg i 0
77end
78ret dl
79end
80
81note PRK = HMAC-Hash(salt, IKM) (RFC 5869 2.2)
82fn hkdf_extract alg ulong si view uchar * slen ulong prk edit uchar * to ulong
83c = new ulong 96 0
84if ne alg 256
85if ne alg 384
86if ne alg 512
87ret 0
88end
89end
90end
91if gt slen len si
92ret 0
93end
94hmac_init alg c part si 0 slen
95hmac_update c part si slen sub len si slen
96dl = hmac_final c prk
97for i ulong 96
98put c i 0
99end
100ret dl
101end
102
103note one block T(n) = HMAC(PRK, T(n-1) || info || n) on the keyed copy c (RFC 5869 2.3).
104note t[0..t[65]) holds T(n-1) (t[65] is 0 for n = 1), t[64] the counter n; T(n) goes to t[0..HashLen).
105fn hkdf_t c edit ulong * t edit uchar * nf view uchar * to ulong
106tl = ulong get t 65
107hmac_update c part t 0 tl
108hmac_update c nf
109hmac_update c part t 64 1
110ret hmac_final c t
111end
112
113note copies T(n) (all of t, or what is left of okm) to okm at d; returns the next d
114fn hkdf_cp okm edit uchar * t view uchar * d ulong to ulong
115k = sub len okm d
116if gt k len t
117k = len t
118end
119for j ulong k
120put okm add d j get t j
121end
122ret add d k
123end
124
125note okm = T(1) || T(2) || .. cut to len okm, from the keyed template tp (len okm <= 255 * HashLen)
126fn hkdf_fill tp edit ulong * okm edit uchar * nf view uchar * to void
127w = new ulong 96 0
128t = new uchar 66 0
129hl = shr get tp 80 3
130done = ulong 0
131n = ulong 0
132k = ulong 0
133while lt done len okm bound 256
134n = add n 1
135put t 64 uchar n
136sha2_copy w tp
137k = hkdf_t w t nf
138put t 65 uchar hl
139done = hkdf_cp okm part t 0 hl done
140end
141for i ulong 96
142put w i 0
143end
144for i ulong 66
145put t i 0
146end
147end
148
149fn hkdf_expand alg ulong pi view uchar * plen ulong okm edit uchar * to ulong
150tp = new ulong 96 0
151if ne alg 256
152if ne alg 384
153if ne alg 512
154ret 1
155end
156end
157end
158if gt plen len pi
159ret 2
160end
161if gt len okm mul 255 shr alg 3
162ret 3
163end
164hmac_init alg tp part pi 0 plen
165hkdf_fill tp okm part pi plen sub len pi plen
166for i ulong 96
167put tp i 0
168end
169ret 0
170end