Skip to content

Commit

Permalink
Add a Message Digests wrapper, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pushkarnk committed Mar 7, 2024
1 parent 21c347a commit 5187162
Show file tree
Hide file tree
Showing 5 changed files with 254 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Expand Up @@ -30,6 +30,8 @@ build: java-build
src/com_canonical_openssl_OpenSSLKEMRSA_RSAKEMEncapsulator.c -o build/bin/com_canonical_openssl_OpenSSLKEMRSA_RSAKEMEncapsulator.o && \
cc -I./include -I${JAVA_HOME}/include/linux/ -I${JAVA_HOME}/include/ -c -fPIC \
src/com_canonical_openssl_OpenSSLMACSpi.c -o build/bin/com_canonical_openssl_OpenSSLMACSpi.o && \
cc -I./include -I${JAVA_HOME}/include/linux/ -I${JAVA_HOME}/include/ -c -fPIC \
src/com_canonical_openssl_OpenSSLMDSpi.c -o build/bin/com_canonical_openssl_OpenSSLMDSpi.o && \
cc -shared -fPIC -Wl,-soname,libjssl.so -o build/bin/libjssl.so \
build/bin/evp_utils.o \
build/bin/jni_utils.o \
Expand All @@ -48,8 +50,13 @@ build: java-build
build/bin/com_canonical_openssl_OpenSSLKEMRSA_RSAKEMEncapsulator.o \
build/bin/com_canonical_openssl_OpenSSLKEMRSA_RSAKEMDecapsulator.o \
build/bin/com_canonical_openssl_OpenSSLMACSpi.o \
build/bin/com_canonical_openssl_OpenSSLMDSpi.o \
-L/usr/local/lib64 -lcrypto -lssl

test-java-md: build
@mkdir -p build/test/java && ${JAVA_HOME}/bin/javac -cp build/classes -d build/test/java test/java/MDTest.java && \
LD_LIBRARY_PATH=./build/bin ${JAVA_HOME}/bin/java -Djava.library.path=${LIBPATH} -cp build/classes:build/test/java MDTest

test-java-mac: build
@mkdir -p build/test/java && ${JAVA_HOME}/bin/javac -cp build/classes -d build/test/java test/java/MacTest.java && \
LD_LIBRARY_PATH=./build/bin ${JAVA_HOME}/bin/java -Djava.library.path=${LIBPATH} -cp build/classes:build/test/java MacTest
Expand Down
37 changes: 37 additions & 0 deletions include/com_canononical_openssl_OpenSSLMDSpi.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 44 additions & 0 deletions src/com_canonical_openssl_OpenSSLMDSpi.c
@@ -0,0 +1,44 @@
#include "com_canononical_openssl_OpenSSLMDSpi.h"
#include "jssl.h"
#include "md.h"
#include "jni_utils.h"

extern OSSL_LIB_CTX *global_libctx;

/*
* Class: OpenSSLMDSpi
* Method: doInit0
* Signature: (Ljava/lang/String;)J
*/
JNIEXPORT jlong JNICALL Java_OpenSSLMDSpi_doInit0
(JNIEnv *env, jobject this, jstring algorithm) {
return (jlong) md_init(global_libctx, (const char*)jstring_to_char_array(env, algorithm));
}

/*
* Class: OpenSSLMDSpi
* Method: doUpdate0
* Signature: ([B)V
*/
JNIEXPORT void JNICALL Java_OpenSSLMDSpi_doUpdate0
(JNIEnv *env, jobject this, jbyteArray data) {
md_context *ctx = (md_context*) get_long_field(env, this, "nativeHandle");
byte *data_array = jbyteArray_to_byte_array(env, data);
int length = array_length(env, data);
md_update(ctx, data_array, length);
}

/*
* Class: OpenSSLMDSpi
* Method: doFinal0
* Signature: ()[B
*/
JNIEXPORT jbyteArray JNICALL Java_OpenSSLMDSpi_doFinal0
(JNIEnv *env, jobject this) {
byte digest[1024];
int digest_length = 0;
md_context *ctx = (md_context*) get_long_field(env, this, "nativeHandle");
md_digest(ctx, digest, &digest_length);
return byte_array_to_jbyteArray(env, digest, digest_length);
}

124 changes: 124 additions & 0 deletions src/java/com/canonical/openssl/OpenSSLMDSpi.java
@@ -0,0 +1,124 @@
import java.nio.ByteBuffer;
import java.security.DigestException;
import java.security.MessageDigestSpi;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Set;

public class OpenSSLMDSpi extends MessageDigestSpi {

static {
System.loadLibrary("jssl");
}

private String mdName;
private long nativeHandle;

private enum MDAlgorithms {
SHA1 (Set.of("SHA", "SHA1", "SHA-1"), 20),
SHA224 (Set.of("SHA224", "SHA-224", "SHA2-224"), 28),
SHA256 (Set.of("SHA256", "SHA-256", "SHA2-256"), 32),
SHA384 (Set.of("SHA384", "SHA-384", "SHA2-384"), 48),
SHA512 (Set.of("SHA512", "SHA-512", "SHA2-512"), 64),
SHA3_224 (Set.of("SHA3-224"), 28),
SHA3_256 (Set.of("SHA3-256"), 32),
SHA3_384 (Set.of("SHA3-384"), 48),
SHA3_512 (Set.of("SHA3-512"), 64),
KECCAK_KEMAK_128 (Set.of("KECCAK-KEMAK-128", "KECCAK-KEMAK128"), 16),
KECCAK_KEMAK_256 (Set.of("KECCAK-KEMAK-256", "KECCAK-KEMAK256"), 32);

final Set<String> names;
final int hashSize;

MDAlgorithms (Set<String> names, int hashSize) {
this.names = names;
this.hashSize = hashSize;
}

Set<String> getNames() {
return this.names;
}

int getHashSize() {
return hashSize;
}
}

private boolean initialized = false;

OpenSSLMDSpi(String algorithm) throws NoSuchAlgorithmException {
boolean found = false;
for (var algo : MDAlgorithms.values()) {
if (algo.getNames().contains(algorithm)) {
found = true;
}
}

if (!found) {
throw new NoSuchAlgorithmException(algorithm);
}

this.mdName = algorithm;
}

@Override
protected byte[] engineDigest() {
return doFinal0();
}

@Override
protected int engineDigest(byte[] buf, int offset, int len) throws DigestException {
byte[] digest = engineDigest();
if (len < digest.length) {
throw new DigestException("Digest length = " + digest.length + " is greater than len = " + len);
}
System.arraycopy(digest, 0, buf, offset, len);
return len;
}

@Override
protected int engineGetDigestLength() {
for (var algo : MDAlgorithms.values()) {
if (algo.getNames().contains(mdName)) {
return algo.getHashSize();
}
}
// we should never come here
System.out.println("[WARNING] Algo not supported");
return 0;
}

@Override
protected void engineReset() {
// TODO
}

@Override
protected void engineUpdate(byte input) {
engineUpdate(new byte[] { input });
}

@Override
protected void engineUpdate(byte []input, int offset, int len) {
engineUpdate(Arrays.copyOfRange(input, offset, len));
}

@Override
protected void engineUpdate(ByteBuffer data) {
engineUpdate(data.array());
}

private void engineUpdate(byte[] data) {
synchronized(this) {
if (!this.initialized) {
nativeHandle = doInit0(mdName);
this.initialized = true;
}
}
doUpdate0(data);
}

private native long doInit0(String name);
private native void doUpdate0(byte[] data);
private native byte[] doFinal0();
}
42 changes: 42 additions & 0 deletions test/java/MDTest.java
@@ -0,0 +1,42 @@
import java.util.Arrays;
import java.util.function.*;
import java.util.List;

public class MDTest {

private static byte[] input = """
From that time on, the world was hers for the reading.
She would never be lonely again, never miss the lack of intimate friends.
Books became her friends and there was one for every mood.""".getBytes();

private static byte[] input1 = """
From that time on, the world was hers for the reading.
She would never be lonely again, never miss the lack of intimate friends.
Books became her friends and there was one for every mood""".getBytes();

private static BiFunction<OpenSSLMDSpi, byte[], byte[]> macCompute = (md, input) -> {
md.engineUpdate(input, 0, input.length);
return md.engineDigest();
};

private static void runTest(String name) throws Exception {
System.out.print("Testing " + name + ": ");
byte[] output1 = macCompute.apply(new OpenSSLMDSpi(name), input);
byte[] output2 = macCompute.apply(new OpenSSLMDSpi(name), input);
byte[] output3 = macCompute.apply(new OpenSSLMDSpi(name), input1);
if (Arrays.equals(output1, output2) && !Arrays.equals(output2, output3)) {
System.out.println("PASSED");
} else {
System.out.println("FAILED");
}
}

public static void main(String[] args) throws Exception {
List<String> tests = List.of("SHA1", "SHA224", "SHA256", "SHA384", "SHA512",
"SHA2-224", "SHA2-256", "SHA2-384", "SHA2-512",
"SHA3-224", "SHA3-256", "SHA3-384", "SHA3-512");
for (var test: tests) {
runTest(test);
}
}
}

0 comments on commit 5187162

Please sign in to comment.