Eric Mastro acd03ff831 feat: reorg project to add nimble build system
Project has been updated to use nimble as a build system. All required dependencies have been added.

All nim-eth mirrored files were added to a private folder in the libp2pdht module. A libp2pdht/discv5 module was added to alias the nim-eth modules (which will change over time).

Test have been updated to use status-im/asynctest. This PR uses a branch of asynctest that supports async suite before/after. This seemed like the only the tests would work without throwing gcsafe errors.

All tests working.
2022-02-25 09:10:43 +11:00

31 lines
711 B
Nim

import nimcrypto
proc hkdf*(HashType: typedesc, ikm, salt, info: openArray[byte],
output: var openArray[byte]) =
var ctx: HMAC[HashType]
ctx.init(salt)
ctx.update(ikm)
let prk = ctx.finish().data
const hashLen = HashType.bits div 8
var t: MDigest[HashType.bits]
var numIters = output.len div hashLen
if output.len mod hashLen != 0:
inc numIters
for i in 0 ..< numIters:
ctx.init(prk)
if i != 0:
ctx.update(t.data)
ctx.update(info)
ctx.update([uint8(i + 1)])
t = ctx.finish()
let iStart = i * hashLen
var sz = hashLen
if iStart + sz >= output.len:
sz = output.len - iStart
copyMem(addr output[iStart], addr t.data, sz)
ctx.clear()