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

Add an initial implementation of engineReset() for MessageDigestSpi #58

Merged
merged 2 commits into from
Jan 8, 2025
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
3 changes: 2 additions & 1 deletion src/main/java/com/canonical/openssl/md/OpenSSLMD.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
61 changes: 61 additions & 0 deletions src/test/java/MDTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,17 @@
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;
import org.junit.BeforeClass;
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 {

Expand Down Expand Up @@ -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());
Expand Down
Loading