From bb719b69ca1f070320413c440c340f64e8785efb Mon Sep 17 00:00:00 2001 From: Pushkar Kulkarni Date: Wed, 8 Jan 2025 11:25:01 +0530 Subject: [PATCH 1/2] Add a initial implementation of MessageDigestSpi.engineReset() --- src/main/java/com/canonical/openssl/md/OpenSSLMD.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/canonical/openssl/md/OpenSSLMD.java b/src/main/java/com/canonical/openssl/md/OpenSSLMD.java index 03617de..db2dc0b 100644 --- a/src/main/java/com/canonical/openssl/md/OpenSSLMD.java +++ b/src/main/java/com/canonical/openssl/md/OpenSSLMD.java @@ -81,7 +81,8 @@ protected int engineDigest(byte[] buf, int offset, int len) throws DigestExcepti @Override protected void engineReset() { - // TODO + nativeHandle = doInit0(mdName); + cleanable = cleaner.register(nativeHandle, new MDState(nativeHandle)); } @Override From 386e40fdc598a59a108807405cfa249c3ada3d20 Mon Sep 17 00:00:00 2001 From: Pushkar Kulkarni Date: Wed, 8 Jan 2025 11:29:36 +0530 Subject: [PATCH 2/2] tests: add an elaborate MD test --- src/test/java/MDTest.java | 61 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/test/java/MDTest.java b/src/test/java/MDTest.java index a9e12a9..0363dd2 100644 --- a/src/test/java/MDTest.java +++ b/src/test/java/MDTest.java @@ -21,6 +21,7 @@ import java.util.List; import java.security.Security; import java.security.MessageDigest; +import java.security.SecureRandom; import com.canonical.openssl.provider.OpenSSLFIPSProvider; import org.junit.Test; @@ -28,7 +29,9 @@ import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; public class MDTest { @@ -61,6 +64,64 @@ public void messageDigestTest() throws Exception { } } + @Test + public void messageDigestElaborateTest() throws Exception { + SecureRandom hmac = SecureRandom.getInstance("HashSHA512","OpenSSLFIPSProvider"); + for (String name: List.of("MDSHA1", "MDSHA224", "MDSHA3_384", "MDSHA3_512")) { + byte[] bytes1 = new byte[10240]; + hmac.nextBytes(bytes1); + + byte[] bytes2 = new byte[10240]; + hmac.nextBytes(bytes2); + + byte[] bytes3 = new byte[20480]; + hmac.nextBytes(bytes3); + + byte[] bytes4 = new byte[10240]; + hmac.nextBytes(bytes4); + + MessageDigest md = MessageDigest.getInstance(name, "OpenSSLFIPSProvider"); + + // update 1 + for (byte b : bytes1) { + md.update(b); + } + + // update 2 + md.update(bytes2); + + // update 3 + md.update(bytes3, 100, 10240); + + // update 4 + md.update(ByteBuffer.wrap(bytes4)); + + // get digest + byte[] digest1 = md.digest(); + + // reset + md.reset(); + + // update 1 + md.update(ByteBuffer.wrap(bytes1)); + + // update 2 + for (byte b : bytes2) { + md.update(b); + } + + // update 3 + md.update(bytes3, 100, 10240); + + // update 4 and get digest + byte[] digest2 = md.digest(bytes4); + + assertEquals("Elaborate test for Message Digest " + name + " failed.", md.getDigestLength(), digest2.length); + assertTrue("Elaborate test for Message Digest " + name + " failed.", MessageDigest.isEqual(digest1, digest2)); + + } + } + @BeforeClass public static void addProvider() { Security.addProvider(new OpenSSLFIPSProvider());