Skip to content

Commit

Permalink
Use Enum for Paddings and reduce the number of tests for CBC ByteBuffer
Browse files Browse the repository at this point in the history
+ Sometimes MacOS JDK11 runs out of memory for CBC ByteBuffer tests
  • Loading branch information
amirhosv committed May 10, 2024
1 parent c996c1b commit d890290
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
20 changes: 18 additions & 2 deletions src/com/amazon/corretto/crypto/provider/AesCbcSpi.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ class AesCbcSpi extends CipherSpi {
public static final int NO_PADDING = 0;
public static final int PKCS7_PADDING = 1;
public static final int ISO10126_PADDING = 2;

enum Padding {
NONE(AesCbcSpi.NO_PADDING),
PKCS7(AesCbcSpi.PKCS7_PADDING),
ISO10126(AesCbcSpi.ISO10126_PADDING);
private final int value;

Padding(final int value) {
this.value = value;
}

int getValue() {
return value;
}
}

public static final Set<String> AES_CBC_NO_PADDING_NAMES;
public static final Set<String> AES_CBC_PKCS7_PADDING_NAMES;
public static final Set<String> AES_CBC_ISO10126_PADDING_NAMES;
Expand Down Expand Up @@ -103,8 +119,8 @@ private enum CipherState {
// initialized.
private byte[] lastBlock;

AesCbcSpi(final int padding, final boolean saveContext) {
this.padding = padding;
AesCbcSpi(final Padding padding, final boolean saveContext) {
this.padding = padding.getValue();
this.cipherState = CipherState.NEEDS_INITIALIZATION;
this.unprocessedInput = 0;
this.opMode = MODE_NOT_SET;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -337,23 +337,23 @@ public Object newInstance(final Object constructorParameter) throws NoSuchAlgori
final boolean saveContext =
AmazonCorrettoCryptoProvider.this.nativeContextReleaseStrategy
== Utils.NativeContextReleaseStrategy.LAZY;
return new AesCbcSpi(AesCbcSpi.PKCS7_PADDING, saveContext);
return new AesCbcSpi(AesCbcSpi.Padding.PKCS7, saveContext);
}

if ("Cipher".equalsIgnoreCase(type)
&& AES_CBC_NO_PADDING_NAMES.contains(algo.toLowerCase())) {
final boolean saveContext =
AmazonCorrettoCryptoProvider.this.nativeContextReleaseStrategy
== Utils.NativeContextReleaseStrategy.LAZY;
return new AesCbcSpi(AesCbcSpi.NO_PADDING, saveContext);
return new AesCbcSpi(AesCbcSpi.Padding.NONE, saveContext);
}

if ("Cipher".equalsIgnoreCase(type)
&& AES_CBC_ISO10126_PADDING_NAMES.contains(algo.toLowerCase())) {
final boolean saveContext =
AmazonCorrettoCryptoProvider.this.nativeContextReleaseStrategy
== Utils.NativeContextReleaseStrategy.LAZY;
return new AesCbcSpi(AesCbcSpi.ISO10126_PADDING, saveContext);
return new AesCbcSpi(AesCbcSpi.Padding.ISO10126, saveContext);
}

throw new NoSuchAlgorithmException(String.format("No service class for %s/%s", type, algo));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ public void testMultiStepByteBuffer(
private static Stream<Arguments> byteBufferTestParams() {
final List<Arguments> result = new ArrayList<>();
for (final int keySize : new int[] {128}) {
for (int i = 0; i != 1024; i++) {
for (int i = 0; i != 512; i++) {
for (final boolean isPaddingEnabled : new boolean[] {true, false}) {
for (final boolean inputReadOnly : new boolean[] {true, false}) {
for (final boolean inputDirect : new boolean[] {true, false}) {
Expand Down

0 comments on commit d890290

Please sign in to comment.