diff --git a/package.json b/package.json index 9b80d2e..1d98f14 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ "@hapi/boom": "^9.1.3", "axios": "^0.24.0", "curve25519-js": "^0.0.4", + "futoin-hkdf": "^1.5.0", "libsignal": "git+https://github.com/adiwajshing/libsignal-node", "music-metadata": "^7.4.1", "node-cache": "^5.1.2", diff --git a/src/Utils/crypto.ts b/src/Utils/crypto.ts index 85b8bd1..cb9a385 100644 --- a/src/Utils/crypto.ts +++ b/src/Utils/crypto.ts @@ -1,5 +1,6 @@ import { createCipheriv, createDecipheriv, createHash, createHmac, randomBytes } from 'crypto' import * as curveJs from 'curve25519-js' +import HKDF from 'futoin-hkdf' import { KeyPair } from '../Types' export const Curve = { @@ -67,33 +68,6 @@ export function sha256(buffer: Buffer) { } // HKDF key expansion -// from: https://github.com/benadida/node-hkdf -export function hkdf(buffer: Uint8Array, expandedLength: number, { info, salt }: { salt?: Buffer, info?: string }) { - const hashAlg = 'sha256' - const hashLength = 32 - salt = salt || Buffer.alloc(hashLength) - // now we compute the PRK - const prk = createHmac(hashAlg, salt).update(buffer).digest() - - let prev = Buffer.from([]) - const buffers = [] - const num_blocks = Math.ceil(expandedLength / hashLength) - - const infoBuff = Buffer.from(info || []) - - for(var i = 0; i < num_blocks; i++) { - const hmac = createHmac(hashAlg, prk) - // XXX is there a more optimal way to build up buffers? - const input = Buffer.concat([ - prev, - infoBuff, - Buffer.from(String.fromCharCode(i + 1)) - ]) - hmac.update(input) - - prev = hmac.digest() - buffers.push(prev) - } - - return Buffer.concat(buffers, expandedLength) +export function hkdf(buffer: Uint8Array | Buffer, expandedLength: number, info: { salt?: Buffer, info?: string }) { + return HKDF(!Buffer.isBuffer(buffer) ? Buffer.from(buffer) : buffer, expandedLength, info) } \ No newline at end of file