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