Skip to content

AES/CFB/NoPadding: no-argument Cipher.doFinal() throws IllegalArgumentException #570

Description

@sfc-gh-grubin

This was discovered as part of my work on #560

AES/CFB/NoPadding will incorrectly throw IllegalArgumentException when the no-argument doFinal() method is called because this implicitly calls engineDoFinal(byte[], int, int, byte[], int) with a null input array. That is then passed directly to Utils.checkArrayLimits() which requires the array to be non-null.

Repro:

import java.security.Security;
import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import com.amazon.corretto.crypto.provider.AmazonCorrettoCryptoProvider;

public class CfbEmptyDoFinalRepro {
  public static void main(String[] args) throws Exception {
    Security.insertProviderAt(AmazonCorrettoCryptoProvider.INSTANCE, 1);

    Cipher cipher = Cipher.getInstance("AES/CFB/NoPadding", "AmazonCorrettoCryptoProvider");
    cipher.init(
        Cipher.ENCRYPT_MODE,
        new SecretKeySpec(new byte[16], "AES"),
        new IvParameterSpec(new byte[16]));

    byte[] result = cipher.doFinal(); // no-arg doFinal: JDK passes a null input array to the SPI
    System.out.println("doFinal() returned " + result.length + " bytes");
  }
}

Expected output:

doFinal() returned 0 bytes

Actual output:

Exception in thread "main" java.lang.IllegalArgumentException: Bad argument: bytes cannot be null.
	at com.amazon.corretto.crypto.provider.Utils.checkArrayLimits(Utils.java:574)
	at com.amazon.corretto.crypto.provider.AesCfbSpi.engineDoFinal(AesCfbSpi.java:248)
	at com.amazon.corretto.crypto.provider.AesCfbSpi.engineDoFinal(AesCfbSpi.java:233)
	at java.base/javax.crypto.Cipher.doFinal(Cipher.java:2103)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions