node-jose icon indicating copy to clipboard operation
node-jose copied to clipboard

concat-kdf algorithm returns too many bytes in some cases

Open ljoy913 opened this issue 3 years ago • 0 comments

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))   

ljoy913 avatar May 20 '21 23:05 ljoy913