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