diff --git a/csrc/hash_template.cpp.template b/csrc/hash_template.cpp.template index 5535fbfa..4eec618f 100644 --- a/csrc/hash_template.cpp.template +++ b/csrc/hash_template.cpp.template @@ -130,7 +130,7 @@ JNIEXPORT void JNICALL JNI_NAME(updateNativeByteBuffer)( } JNIEXPORT void JNICALL JNI_NAME(fastDigest)( - JNIEnv* pEnv, jclass, jbyteArray digestArray, jbyteArray dataArray, jint dataLength) + JNIEnv* pEnv, jclass, jbyteArray digestArray, jbyteArray dataArray, jint bufOffset, jint dataLength) { // As this method needs to be extremely high speed, we are omitting use of java_buffer // to avoid the extra JNI calls it requires. Instead we are trusting that dataLength @@ -147,14 +147,14 @@ JNIEXPORT void JNICALL JNI_NAME(fastDigest)( } if (static_cast(dataLength) > scratchSize) { - java_buffer dataBuffer = java_buffer::from_array(env, dataArray, 0, dataLength); + java_buffer dataBuffer = java_buffer::from_array(env, dataArray, bufOffset, dataLength); jni_borrow dataBorrow(env, dataBuffer, "data"); if (unlikely(!OP(Update)(ctx, dataBorrow.data(), dataBorrow.len()))) { throw java_ex::from_openssl(EX_RUNTIME_CRYPTO, "Unable to update context"); } } else { SecureBuffer scratch; - env->GetByteArrayRegion(dataArray, 0, dataLength, reinterpret_cast(scratch.buf)); + env->GetByteArrayRegion(dataArray, bufOffset, dataLength, reinterpret_cast(scratch.buf)); if (unlikely(!OP(Update)(ctx, scratch, dataLength))) { throw java_ex::from_openssl(EX_RUNTIME_CRYPTO, "Unable to update context"); } diff --git a/template-src/com/amazon/corretto/crypto/provider/TemplateHashSpi.java b/template-src/com/amazon/corretto/crypto/provider/TemplateHashSpi.java index c1142ffd..99cd4752 100644 --- a/template-src/com/amazon/corretto/crypto/provider/TemplateHashSpi.java +++ b/template-src/com/amazon/corretto/crypto/provider/TemplateHashSpi.java @@ -45,7 +45,7 @@ public final class TemplateHashSpi extends MessageDigestSpi implements Cloneable * @param buf Input buffer */ // NOTE: This method trusts that all of the array lengths and bufLen are sane. - static native void fastDigest(byte[] digest, byte[] buf, int bufLen); + static native void fastDigest(byte[] digest, byte[] buf, int bufOffset, int bufLen); /** * @return The size of result hashes for this hash function @@ -123,12 +123,8 @@ private static byte[] doFinal(byte[] context) { } private static byte[] singlePass(byte[] src, int offset, int length) { - if (offset != 0 || length != src.length) { - src = Arrays.copyOf(src, length); - offset = 0; - } final byte[] result = new byte[HASH_SIZE]; - fastDigest(result, src, src.length); + fastDigest(result, src, offset, length); return result; }