node-jose
node-jose copied to clipboard
concat-kdf algorithm returns too many bytes in some cases
The caller passes in the desired keyLength which is expected as a bit count. The slice call that returns the derived bytes, keyLength, as bits instead of bytes, returns the entire result without truncating.
concat.js
function concatDeriveFn(name) {
...
var N = Math.ceil(keyLen / hashLen), <== keyLen is expected to be in bits to compute # of iterations
idx = 0,
okm = [];
function step() {
if (N === idx++) {
return Buffer.concat(okm).slice(0, keyLen ); <== keyLen is expected to be a byte count for slice()}
...
}
Dividing by 8 should fix the problem:
return Buffer.concat(okm).slice(0, Math.ceil(keyLen / 8))