Differences From
Artifact [340864c560]:
10 10 end;
11 11 toobig = -lib.pk.MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE;
12 12 }
13 13 const.maxpemsz = math.floor((const.keybits / 8)*6.4) + 128 -- idk why this formula works but it basically seems to
14 14
15 15 local ctx = lib.pk.mbedtls_pk_context
16 16
17 +local struct hashalg { id: uint8 bytes: intptr }
17 18 local m = {
18 19 pemfile = uint8[const.maxpemsz];
20 + alg = {
21 + sha1 = `hashalg {id = lib.md.MBEDTLS_MD_SHA1; bytes = 160/8};
22 + sha256 = `hashalg {id = lib.md.MBEDTLS_MD_SHA256; bytes = 256/8};
23 + sha512 = `hashalg {id = lib.md.MBEDTLS_MD_SHA512; bytes = 512/8};
24 + sha384 = `hashalg {id = lib.md.MBEDTLS_MD_SHA384; bytes = 384/8};
25 + sha224 = `hashalg {id = lib.md.MBEDTLS_MD_SHA224; bytes = 224/8};
26 + -- md5 = {id = lib.md.MBEDTLS_MD_MD5};-- !!!
27 + };
19 28 }
20 29 local callbacks = {}
21 30 if config.feat.randomizer == 'kern' then
22 31 local rnd = terralib.externfunction('getrandom', {&opaque, intptr, uint} -> ptrdiff);
23 32 terra callbacks.randomize(ctx: &opaque, dest: &uint8, sz: intptr): int
24 33 return rnd(dest, sz, 0)
25 34 end
................................................................................
132 141 if lib.pk.mbedtls_pk_verify(pk, hk, hash, 0, [&uint8](sig), siglen) == 0 then
133 142 return true, secl
134 143 end
135 144 end
136 145 lib.dbg('all hash algorithms failed')
137 146 return false, 0
138 147 end
148 +
149 +terra m.hmac(alg: hashalg, key: lib.mem.ptr(uint8), txt: lib.mem.ptr(int8), buf: &uint8)
150 + lib.md.mbedtls_md_hmac(
151 + lib.md.mbedtls_md_info_from_type(alg.id),
152 + key.ptr, key.ct,
153 + [&uint8](txt.ptr), txt.ct,
154 + buf) -- sz(buf) >= hash output size
155 +end
156 +
157 +terra m.hmaca(alg: hashalg, key: lib.mem.ptr(uint8), txt: lib.mem.ptr(int8))
158 + var buf = lib.mem.heapa(uint8, alg.bytes)
159 + m.hmac(alg, key, txt, buf.ptr)
160 + return buf
161 +end
162 +
163 +terra m.hotp(key: &(uint8[10]), counter: uint64)
164 + var hmac: uint8[20]
165 + var ctr = [lib.mem.ptr(int8)]{ptr = [&int8](&counter), ct = 8}
166 + m.hmac(m.alg.sha1,
167 + [lib.mem.ptr(uint8)]{ptr = [&uint8](key), ct = 10},
168 + ctr, hmac)
169 +
170 + var ofs = hmac[19] and 0x0F
171 + var p: uint8[4]
172 + for i=0,4 do p[i] = hmac[ofs + i] end
173 +
174 + return (@[&uint32](&p)) and 0x7FFFFFFF -- one hopes it's that easy
175 +end
139 176
140 177 return m