Skip to content

Commit

Permalink
Reuse state in MessageDigest
Browse files Browse the repository at this point in the history
  • Loading branch information
SalusaSecondus committed Oct 15, 2020
1 parent bdfb7cf commit c69cfe2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ method.
### Improvements
* Stricter guarantees about which curves are used for EC key generation. [PR #127](https://github.com/corretto/amazon-corretto-crypto-provider/pull/127)
* Reduce timing signal from trimming zeros of TLSPremasterSecrets from DH KeyAgreement. [PR #129](https://github.com/corretto/amazon-corretto-crypto-provider/pull/129)
* Reuse state in `MessageDigest` to decrease object allocation rate. [PR #131](https://github.com/corretto/amazon-corretto-crypto-provider/pull/131)

### Patches
* Add version gating to some tests introduced in 1.5.0 [PR #128](https://github.com/corretto/amazon-corretto-crypto-provider/pull/128)
Expand Down
7 changes: 2 additions & 5 deletions src/com/amazon/corretto/crypto/provider/InputBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public static interface StateSupplier<S> extends Supplier<S> {
//@ spec_public
private /*@ { Consumer.Local<S> } @*/ Consumer<S> stateResetter = (ignored) -> { }; // NOP
//@ spec_public
private StateSupplier<S> stateSupplier = () -> null;
private StateSupplier<S> stateSupplier = () -> state;
//@ spec_public
private Optional<Function<S, S>> stateCloner = Optional.empty();
// If absent, delegates to arrayUpdater
Expand Down Expand Up @@ -229,10 +229,7 @@ public static interface StateSupplier<S> extends Supplier<S> {
public void reset() {
buff.reset();
firstData = true;
if (state != null) {
stateResetter.accept(state);
}
state = stateSupplier.get();
state = null;
/*@ set bytesReceived = 0;
@ set bytesProcessed = 0;
@ set bufferState = ((bufferState == BufferState.Uninitialized)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public final class TemplateHashSpi extends MessageDigestSpi implements Cloneable
private static final int HASH_SIZE;
private static final byte[] INITIAL_CONTEXT;

private byte[] myContext;
private byte[] oneByteArray = null;
private InputBuffer<byte[], byte[]> buffer;

Expand Down Expand Up @@ -108,11 +109,17 @@ private static void synchronizedFinish(byte[] context, byte[] digest, int offset
}
}

private byte[] resetContext() {
System.arraycopy(INITIAL_CONTEXT, 0, myContext, 0, INITIAL_CONTEXT.length);
return myContext;
}

public TemplateHashSpi() {
Loader.checkNativeLibraryAvailability();
myContext = INITIAL_CONTEXT.clone();

this.buffer = new InputBuffer<byte[], byte[]>(1024)
.withInitialStateSupplier(INITIAL_CONTEXT::clone)
.withInitialStateSupplier(this::resetContext)
.withUpdater(TemplateHashSpi::synchronizedUpdateContextByteArray)
.withUpdater(TemplateHashSpi::synchronizedUpdateNativeByteBuffer)
.withDoFinal((context) -> {
Expand Down Expand Up @@ -158,6 +165,7 @@ public Object clone() {
try {
TemplateHashSpi clonedObject = (TemplateHashSpi)super.clone();

clonedObject.myContext = myContext.clone();
clonedObject.buffer = (InputBuffer<byte[], byte[]>) buffer.clone();

return clonedObject;
Expand Down

0 comments on commit c69cfe2

Please sign in to comment.