Skip to content

Commit

Permalink
Validate that AesGcmSpi#engineInit gets non-null key (#146)
Browse files Browse the repository at this point in the history
* Validate that AesGcmSpi#engineInit gets non-null key

* Update CHANGELOG

* Only run AesTest#test_initNullKey for appropriate versions

Co-authored-by: SalusaSecondus <SalusaSecondus@users.noreply.github.com>

* make `key` final

* Fix indent

* bump to re-run CI checks

Co-authored-by: SalusaSecondus <SalusaSecondus@users.noreply.github.com>
  • Loading branch information
alex-chew and SalusaSecondus authored Mar 13, 2021
1 parent c518c38 commit 6bead18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ method.
### Patches
* Add version gating to some tests introduced in 1.5.0 [PR #128](https://github.com/corretto/amazon-corretto-crypto-provider/pull/128)
* More accurate output size estimates from `Cipher.getOutputSize()` [PR #138](https://github.com/corretto/amazon-corretto-crypto-provider/pull/138)
* Validate that `AesGcmSpi` receives a non-null key on init to prevent unncessarily late NPE [PR #146](https://github.com/corretto/amazon-corretto-crypto-provider/pull/146)

## 1.5.0
### Breaking Change Warning
Expand Down
4 changes: 4 additions & 0 deletions src/com/amazon/corretto/crypto/provider/AesGcmSpi.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,10 @@ protected synchronized void engineInit(int opMode, Key key, SecureRandom secureR
protected synchronized void engineInit(
int jceOpMode, Key key, AlgorithmParameterSpec algorithmParameterSpec, SecureRandom secureRandom
) throws InvalidKeyException, InvalidAlgorithmParameterException {
if (key == null) {
throw new InvalidKeyException("Key can't be null");
}

final GCMParameterSpec spec;
if (algorithmParameterSpec instanceof GCMParameterSpec) {
spec = (GCMParameterSpec) algorithmParameterSpec;
Expand Down
21 changes: 21 additions & 0 deletions tst/com/amazon/corretto/crypto/provider/test/AesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.Security;
import java.security.spec.AlgorithmParameterSpec;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -579,6 +581,25 @@ public void test_initParameters() throws Throwable {
assertArrayEquals(PLAINTEXT, decrypted);
}

@SuppressWarnings("ConstantConditions")
@Test
public void test_initNullKey() throws Throwable {
assumeMinimumVersion("1.6.0", AmazonCorrettoCryptoProvider.INSTANCE);
jceC.init(Cipher.ENCRYPT_MODE, key);

final Key key = null;
AlgorithmParameters params = jceC.getParameters();
AlgorithmParameterSpec spec = params.getParameterSpec(GCMParameterSpec.class);
SecureRandom random = TestUtil.MISC_SECURE_RANDOM.get();

assertThrows(InvalidKeyException.class, () -> amznC.init(Cipher.ENCRYPT_MODE, key));
assertThrows(InvalidKeyException.class, () -> amznC.init(Cipher.ENCRYPT_MODE, key, params));
assertThrows(InvalidKeyException.class, () -> amznC.init(Cipher.ENCRYPT_MODE, key, params, random));
assertThrows(InvalidKeyException.class, () -> amznC.init(Cipher.ENCRYPT_MODE, key, random));
assertThrows(InvalidKeyException.class, () -> amznC.init(Cipher.ENCRYPT_MODE, key, spec));
assertThrows(InvalidKeyException.class, () -> amznC.init(Cipher.ENCRYPT_MODE, key, spec, random));
}

@Test
public void test_bufferOverflows() throws Throwable {
final SecureRandom rnd = TestUtil.MISC_SECURE_RANDOM.get();
Expand Down

0 comments on commit 6bead18

Please sign in to comment.