in modules/kdf-ctr-mode-node/src/kdfctr.ts [33:82]
export function kdfCounterMode({
digestAlgorithm,
ikm,
nonce,
purpose,
expectedLength,
}: KdfCtrInput): Buffer {
/* Precondition: the ikm must be 32, 48, 66 bytes long */
needs(
SUPPORTED_IKM_LENGTHS.includes(ikm.length),
`Unsupported IKM length ${ikm.length}`
)
/* Precondition: the nonce is required */
needs(nonce, 'The nonce must be provided')
/* Precondition: the nonce must be 16, 32 bytes long */
needs(
SUPPORTED_NONCE_LENGTHS.includes(nonce.length),
`Unsupported nonce length ${nonce.length}`
)
/* Precondition: the expected length must be 32, 64 bytes */
/* Precondition: the expected length * 8 must be under the max 32-bit signed integer */
needs(
SUPPORTED_DERIVED_KEY_LENGTHS.includes(expectedLength) &&
expectedLength * 8 < INT32_MAX_LIMIT &&
expectedLength * 8 > 0,
`Unsupported requested length ${expectedLength}`
)
const label = purpose || Buffer.alloc(0)
const info = nonce
const internalLength = 8 + SEPARATION_INDICATOR.length
/* Precondition: the input length must be under the max 32-bit signed integer */
needs(
internalLength + label.length + info.length < INT32_MAX_LIMIT,
`Input Length ${
internalLength + label.length + info.length
} must be under ${INT32_MAX_LIMIT} bytes`
)
const lengthBits = Buffer.from(uInt32BE(expectedLength * 8))
const explicitInfo = Buffer.concat([
label,
SEPARATION_INDICATOR,
info,
lengthBits,
])
return rawDerive(ikm, explicitInfo, expectedLength, digestAlgorithm)
}