diff options
author | Florian Dold <florian@dold.me> | 2022-12-23 12:58:26 +0100 |
---|---|---|
committer | Florian Dold <florian@dold.me> | 2022-12-23 13:19:41 +0100 |
commit | d98711cb51d13bb2da3682014c7c6e75d7fbb4f0 (patch) | |
tree | 827847cbb1c2d80c9f6b8241c46593a9a1da16f7 /packages/taler-util/src/kdf.ts | |
parent | dc002f99a96752d3f0a10efe44a8a4d0503e8529 (diff) | |
download | wallet-core-d98711cb51d13bb2da3682014c7c6e75d7fbb4f0.tar.xz |
use native KDF / hash state if available
Diffstat (limited to 'packages/taler-util/src/kdf.ts')
-rw-r--r-- | packages/taler-util/src/kdf.ts | 40 |
1 files changed, 0 insertions, 40 deletions
diff --git a/packages/taler-util/src/kdf.ts b/packages/taler-util/src/kdf.ts index dd8a2a459..8f4314340 100644 --- a/packages/taler-util/src/kdf.ts +++ b/packages/taler-util/src/kdf.ts @@ -16,7 +16,6 @@ import * as nacl from "./nacl-fast.js"; import { sha256 } from "./sha256.js"; -import { useNative } from "./taler-crypto.js"; export function sha512(data: Uint8Array): Uint8Array { return nacl.hash(data); @@ -59,42 +58,3 @@ export function hmacSha512(key: Uint8Array, message: Uint8Array): Uint8Array { export function hmacSha256(key: Uint8Array, message: Uint8Array): Uint8Array { return hmac(sha256, 64, key, message); } - -export function kdf( - outputLength: number, - ikm: Uint8Array, - salt?: Uint8Array, - info?: Uint8Array, -): Uint8Array { - if (useNative && "_kdf" in globalThis) { - // @ts-ignore - return globalThis._kdf(outputLength, ikm, salt, info); - } - salt = salt ?? new Uint8Array(64); - // extract - const prk = hmacSha512(salt, ikm); - - info = info ?? new Uint8Array(0); - - // expand - const N = Math.ceil(outputLength / 32); - const output = new Uint8Array(N * 32); - for (let i = 0; i < N; i++) { - let buf; - if (i == 0) { - buf = new Uint8Array(info.byteLength + 1); - buf.set(info, 0); - } else { - buf = new Uint8Array(info.byteLength + 1 + 32); - for (let j = 0; j < 32; j++) { - buf[j] = output[(i - 1) * 32 + j]; - } - buf.set(info, 32); - } - buf[buf.length - 1] = i + 1; - const chunk = hmacSha256(prk, buf); - output.set(chunk, i * 32); - } - - return output.slice(0, outputLength); -} |