IV Size Mismatch in AES-256-GCM Encryption Between Node.js and Rust

I am working with AES-256-GCM encryption in both Node.js and Rust using the aes_gcm library, but I am encountering an issue with the IV (Initialization Vector). When I use a 16-byte IV in Node.js with the crypto module, both encryption and decryption work fine. However, when I encrypt data in Node.js with a 16-byte IV and try to decrypt it in Rust, the Rust AES crate does not support a 16-byte IV. It only supports a 12-byte IV.

assertion left == right failed

  • left: 16*
  • right: 12*

Note: I can run with the RUST_BACKTRACE=1 environment variable to display a backtrace.

What is the reason behind this, and what would be the best solution to resolve this issue?

may be because RFC-5116 (section 3.2) recommends it shoud be 12 bytes.

Is the Node.js library base64-encoding the IV to give 16 bytes? You may need to decode it in the Rust to get the 12 bytes you need.

I have already decoded it using base64 and i got 16 bytes of iv because it is encrypt with 16 bytes in nodejs but i need to decode it in the rust and the error is occured

You can explicitly specify the IV size with the NonceSize type parameter: AesGcm in aes_gcm - Rust

1 Like