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

Don't throw when resetting an uninitialised Mac. #1178

Merged
merged 1 commit into from
Oct 31, 2023
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
5 changes: 5 additions & 0 deletions common/src/main/java/org/conscrypt/OpenSSLMac.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public abstract class OpenSSLMac extends MacSpi {
* Holds a dummy buffer for writing single bytes to the digest.
*/
private final byte[] singleByte = new byte[1];
protected boolean initialized = false;

private OpenSSLMac(int size) {
this.size = size;
Expand Down Expand Up @@ -84,6 +85,7 @@ protected void engineInit(Key key, AlgorithmParameterSpec params) throws Invalid
} catch (RuntimeException e) {
throw new InvalidKeyException("invalid key", e);
}
initialized = true;
}

@Override
Expand Down Expand Up @@ -140,6 +142,9 @@ protected byte[] engineDoFinal() {

@Override
protected void engineReset() {
if (!initialized) {
return;
}
resetContext();
}

Expand Down
2 changes: 2 additions & 0 deletions common/src/test/java/org/conscrypt/MacTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ public void test(final Provider provider, final String algorithm) throws Excepti
mac = Mac.getInstance(algorithm, provider);
assertEquals(algorithm, mac.getAlgorithm());
assertEquals(provider, mac.getProvider());
// It's not an error to reset an uninitialised Mac.
mac.reset();
if (key != null) {
// TODO(prb) Ensure we have at least one test vector for every
// MAC in Conscrypt and Android.
Expand Down
Loading