node.jsencryptioncryptographyblock-cipher

Got “ERR_CRYPTO_INVALID_IV.” Why?


Command prompt — node

What have I done wrong? It’s Node.js 16.1.0.

> crypto.createCipheriv("AES-128-CTR", Buffer.allocUnsafe(16), Buffer.allocUnsafe(16));
Cipheriv {…}
> crypto.createCipheriv("AES-128-OCB", Buffer.allocUnsafe(16), Buffer.allocUnsafe(16));
Uncaught TypeError: Invalid initialization vector
    at Cipheriv.createCipherBase (node:internal/crypto/cipher:119:19)
    at Cipheriv.createCipherWithIV (node:internal/crypto/cipher:138:3)
    at new Cipheriv (node:internal/crypto/cipher:246:3)
    at Object.createCipheriv (node:crypto:131:10) {
  code: 'ERR_CRYPTO_INVALID_IV'
}

Solution

  • The initial vector for the OCB mode should be 12 octets; 96 bits, that is.