Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle RsaCipher#engineDoFinal with no input bytes #147

Merged
merged 4 commits into from
Mar 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ method.
* 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)
* Gracefully handle calling `Cipher.doFinal()` without any input bytes in `RsaCipher` [PR #147](https://github.com/corretto/amazon-corretto-crypto-provider/pull/147)

## 1.5.0
### Breaking Change Warning
Expand Down
8 changes: 8 additions & 0 deletions src/com/amazon/corretto/crypto/provider/RsaCipher.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ protected int engineDoFinal(byte[] input, int inputOffset, int inputLen, final b
inputOffset = 0;
inputLen = buffer_.size();
}
// One-shot, no input. Cipher only calls engineDoFinal with null input in doFinal overloads
// that don't take an input buffer, and in those cases, inputOffset and inputLen are 0.
// We set them here anyways to be safe, because the API makes no such guarantee.
else if (input == null) {
input = Utils.EMPTY_ARRAY;
inputOffset = 0;
inputLen = 0;
}

if (output.length - outputOffset < engineGetOutputSize(inputLen)) {
throw new ShortBufferException();
Expand Down
11 changes: 11 additions & 0 deletions tst/com/amazon/corretto/crypto/provider/test/RsaCipherTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,17 @@ public void threadStorm() throws Throwable {
}
}

@Test
public void noInputDoFinal() throws Exception {
assumeMinimumVersion("1.6.0", AmazonCorrettoCryptoProvider.INSTANCE);
final Cipher enc = Cipher.getInstance(NO_PADDING, NATIVE_PROVIDER);
enc.init(Cipher.ENCRYPT_MODE, PAIR_1024.getPublic());
final byte[] result = enc.doFinal();
for (final byte b : result) {
assertEquals(b, 0);
}
}

private void testNative2Jce(final String padding, final int keySize) throws GeneralSecurityException {
final Cipher jceC = Cipher.getInstance(padding);
final Cipher nativeC = Cipher.getInstance(padding, NATIVE_PROVIDER);
Expand Down