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 / om / tls / drbg.om

239 lines · 5923 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note drbg.om: HMAC_DRBG with HMAC-SHA-256 (NIST SP 800-90A Rev. 1 section 10.1.2), security
3note strength 256 bits. Library unit: no main, no mutable globals, no I/O and no entropy access of its
4note own; the caller (fips.om) gathers entropy input and nonce from getrandom, runs the SP 800-90B
5note health tests on them and passes the concatenated seed material in. Needs sha2.om and hkdf.om
6note composed before it (om/tls/build.sh). Tested against the ACVP/CAVP HMAC_DRBG test methods
7note (test/drbg/run.sh); not NIST-validated.
8note
9note State st (a caller-owned uchar array of DRSZ bytes, zeroed = not instantiated):
10note st[0..32) K st[32..64) V
11note st[64..72) reseed_counter (little-endian)
12note st[72..80) reseed_interval (little-endian; 0 = DRRSMX = 2^48, SP 800-90A Table 2)
13note st[80] 1 instantiated st[81] 1 error state (stuck output seen; only drbg_wipe clears)
14note st[82] 1 a previous output fingerprint is held
15note st[88..104) fingerprint of the previous output block: the first 16 bytes of SHA-256(block),
16note so the continuous check holds no output bytes (a 2^-128 false-alarm chance)
17note Every function runs the same instructions for every value of K and V: the only branches are on
18note lengths, flags and the counter, all public. HMAC contexts and temporaries are wiped before return.
19note
20note Return codes: 0 ok; 1 reseed required (reseed_counter > reseed_interval; generate refused, no
21note output); 2 not instantiated; 3 bad length (seed material under DRMINSM / DRMINRS bytes, a request
22note over DRMAXRQ bytes, or a reseed interval outside 1..DRRSMX); 4 error state (a generated block
23note equalled the previous one; output zeroed; state stays refused until drbg_wipe).
24
25const DRSZ ulong 128
26const DRCTR ulong 64
27const DRLIM ulong 72
28const DRFL ulong 80
29const DRERR ulong 81
30const DRHAVE ulong 82
31const DRFP ulong 88
32const DRRSMX ulong 281474976710656
33const DRMAXRQ ulong 65536
34const DRMINSM ulong 48
35const DRMINRS ulong 32
36
37fn drbg_rd st view uchar * o ulong to ulong
38v = ulong 0
39for i ulong 8
40v = or v shl ulong get st add o i mul i 8
41end
42ret v
43end
44
45fn drbg_wr st edit uchar * o ulong v ulong to void
46for i ulong 8
47put st add o i uchar and shr v mul i 8 255
48end
49end
50
51fn drbg_zx x edit ulong * to void
52for i ulong len x
53put x i 0
54end
55end
56
57fn drbg_zb x edit uchar * to void
58for i ulong len x
59put x i 0
60end
61end
62
63note V = HMAC(K, V) using the scratch context x
64fn drbg_next st edit uchar * x edit ulong * to void
65hmac_init 256 x part st 0 32
66hmac_update x part st 32 32
67n = hmac_final x part st 32 32
68end
69
70note HMAC_DRBG_Update (10.1.2.2): K = HMAC(K, V || 0x00 || pd), V = HMAC(K, V); when pd is not empty
71note again with 0x01. Empty pd is the standard's Null input.
72fn drbg_upd st edit uchar * pd view uchar * to void
73x = new ulong 96 0
74b = new uchar 1 0
75for r ulong 2
76if eq r 1
77if eq len pd 0
78break
79end
80end
81put b 0 uchar r
82hmac_init 256 x part st 0 32
83hmac_update x part st 32 32
84hmac_update x b
85hmac_update x pd
86n = hmac_final x part st 0 32
87drbg_next st x
88end
89drbg_zx x
90end
91
92note Instantiate (10.1.2.3): sm = entropy_input || nonce || personalization_string. K = 0x00..,
93note V = 0x01.., Update(sm), reseed_counter = 1. The reseed interval and error state are reset.
94fn drbg_inst st edit uchar * sm view uchar * to ulong
95if lt len st DRSZ
96ret 3
97end
98if lt len sm DRMINSM
99ret 3
100end
101drbg_zb part st 0 DRSZ
102for i ulong 32
103put st add 32 i 1
104end
105drbg_upd st sm
106drbg_wr st DRCTR 1
107put st DRFL 1
108ret 0
109end
110
111note Reseed (10.1.2.4): sm = entropy_input || additional_input. Update(sm), reseed_counter = 1.
112fn drbg_reseed st edit uchar * sm view uchar * to ulong
113if lt len st DRSZ
114ret 3
115end
116if ne get st DRFL 1
117ret 2
118end
119if eq get st DRERR 1
120ret 4
121end
122if lt len sm DRMINRS
123ret 3
124end
125drbg_upd st sm
126drbg_wr st DRCTR 1
127ret 0
128end
129
130note Lower the reseed interval (tests use it to reach the limit); 1 <= n <= DRRSMX.
131fn drbg_limit st edit uchar * n ulong to ulong
132if lt len st DRSZ
133ret 3
134end
135if eq n 0
136ret 3
137end
138if gt n DRRSMX
139ret 3
140end
141drbg_wr st DRLIM n
142ret 0
143end
144
145note continuous check on one fresh block V: 0 when it differs from the previous block, 4 when
146note equal (the error state is latched). Only the first 16 bytes of SHA-256(V) are kept.
147fn drbg_cont st edit uchar * to ulong
148c = new ulong 40 0
149f = new uchar 32 0
150sha256_init c
151sha256_update c part st 32 32
152sha256_final c f
153d = ulong 0
154for i ulong 16
155d = or d ulong xor get f i get st add DRFP i
156end
157h = ulong get st DRHAVE
158for i ulong 16
159put st add DRFP i get f i
160end
161put st DRHAVE 1
162drbg_zx c
163drbg_zb f
164if eq h 1
165if eq d 0
166put st DRERR 1
167ret 4
168end
169end
170ret 0
171end
172
173note dst[o..) gets V, at most 32 bytes; returns the next o
174fn drbg_cp st view uchar * dst edit uchar * o ulong to ulong
175k = sub len dst o
176if gt k 32
177k = 32
178end
179for i ulong k
180put dst add o i get st add 32 i
181end
182ret add o k
183end
184
185note fills dst with V = HMAC(K, V) blocks (10.1.2.5 step 4), running the continuous check on each;
186note returns 0, or 4 when a block repeated (the whole loop still runs)
187fn drbg_fill st edit uchar * dst edit uchar * to ulong
188x = new ulong 96 0
189o = ulong 0
190e = ulong 0
191while lt o len dst bound 2049
192drbg_next st x
193e = or e drbg_cont st
194o = drbg_cp st dst o
195end
196drbg_zx x
197ret e
198end
199
200note Generate (10.1.2.5): dst gets len dst bytes; ad = additional_input (empty = Null).
201fn drbg_gen st edit uchar * dst edit uchar * ad view uchar * to ulong
202if lt len st DRSZ
203ret 3
204end
205if ne get st DRFL 1
206ret 2
207end
208if eq get st DRERR 1
209drbg_zb dst
210ret 4
211end
212if gt len dst DRMAXRQ
213ret 3
214end
215lim = drbg_rd st DRLIM
216if eq lim 0
217lim = DRRSMX
218end
219ctr = drbg_rd st DRCTR
220if gt ctr lim
221ret 1
222end
223if gt len ad 0
224drbg_upd st ad
225end
226e = drbg_fill st dst
227if ne e 0
228drbg_zb dst
229ret 4
230end
231drbg_upd st ad
232drbg_wr st DRCTR add ctr 1
233ret 0
234end
235
236note Uninstantiate (10.1.2 / 9.4): zero the whole state, including the error state.
237fn drbg_wipe st edit uchar * to void
238drbg_zb st
239end