Skip to content

Commit

Permalink
Don't throw when resetting an uninitialised Mac. (#1178)
Browse files Browse the repository at this point in the history
Turns out Android, SunJCE and BC all allow this and the javadoc
has nothing to say on the subject.  Caught this as a side
effect of an unrelated Android test, so added an explicit check
in our own ServiceTester test which runs against all installed
Providers.

Added a new field to track initialized state because if the
native context ever becomes null after that (e.g. concurrency
bug) that's still an error.
  • Loading branch information
prbprbprb authored Oct 31, 2023
1 parent bc1a34b commit 7e90d8d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
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

0 comments on commit 7e90d8d

Please sign in to comment.