Search code examples
node.jscryptographyaessubtlecrypto

aes256-wrap Error 'ERR_CRYPTO_INVALID_IV' But AES-KW Does Not Require An Initialization Vector?


MDN mentions that:

AES-KW does not require an initialization vector (iv)

But when I try and encrypt my content encryption key (CEK) using the key encryption key (KEK) like below Node.js Crypto createCipheriv(algorithm, key, iv[, options]) throws an error ERR_CRYPTO_INVALID_IV. Does anyone know if AES-KW requires an iv? The Advanced Encryption Standard (AES) Key Wrap Algorithm RFC does not mention "initialization vector" at all.

let cipherCEK = createCipheriv('aes256-wrap', keyEncryptionKey, null);
let encryptedCEK = cipher.update(contentEncryptionKey);
encryptedCEK += cipher.final(); //buffer

Solution

  • I'm not sure why you say that RFC 3394 doesn't mention an IV. It's covered in section 2.2.3, and is used for data integrity (i.e. to make sure that the key was decrypted correctly). The default value recommended is A6A6A6A6A6A6A6A6. I haven't tested with createCipheriv, but I would recommend passing this value.

    I expect that MDN says it doesn't "require" an IV because there is a default one that Microsoft automatically uses, so you don't need to pass it. I expect Node.js just isn't quite that developer-friendly.